Back to index

lightning-sunbird  0.9+nobinonly
nsComm4xMail.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is mozilla.org code.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 2002
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   Srilatha Moturi <srilatha@netscape.com>
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nsCOMPtr.h"
00039 #include "nsReadableUtils.h"
00040 #include "nsIServiceManager.h"
00041 #include "nsMsgBaseCID.h"
00042 #include "nsMsgI18N.h"
00043 #include "nsComm4xMail.h"
00044 #include "nsIImportService.h"
00045 #include "nsIImportMailboxDescriptor.h"
00046 #include "nsComm4xMailStringBundle.h"
00047 #include "nsComm4xMailImport.h"
00048 #include "nsUnicharUtils.h"
00049 #include "Comm4xMailDebugLog.h"
00050 #include "prmem.h"
00051 #include "nsNativeCharsetUtils.h"
00052 
00053 #define       kCopyBufferSize             8192
00054 #define       kMailReadBufferSize  16384
00055 
00056 
00057 static PRBool
00058 nsStringEndsWith(nsString& name, const char *ending)
00059 {
00060     if (!ending) return PR_FALSE;
00061 
00062     PRInt32 len = name.Length();
00063     if (len == 0) return PR_FALSE;
00064 
00065     PRInt32 endingLen = strlen(ending);
00066     if (len > endingLen && name.RFind(ending, PR_TRUE) == len - endingLen) {
00067         return PR_TRUE;
00068     }
00069     else {
00070         return PR_FALSE;
00071     }
00072 }
00073 
00074 static PRBool
00075 nsShouldIgnoreFile(nsString& name)
00076 {
00077     PRUnichar firstChar=name.CharAt(0);
00078     if (firstChar == '.' || firstChar == '#' || name.CharAt(name.Length() - 1) == '~')
00079       return PR_TRUE;
00080  
00081     if (name.LowerCaseEqualsLiteral("rules.dat") || name.LowerCaseEqualsLiteral("rulesbackup.dat"))
00082         return PR_TRUE;
00083  
00084  
00085     // don't add summary files to the list of folders;
00086     // don't add popstate files to the list either, or rules (sort.dat). 
00087     if (nsStringEndsWith(name, ".snm") ||
00088         name.LowerCaseEqualsLiteral("popstate.dat") ||
00089         name.LowerCaseEqualsLiteral("sort.dat") ||
00090         name.LowerCaseEqualsLiteral("mailfilt.log") ||
00091         name.LowerCaseEqualsLiteral("filters.js") ||
00092         nsStringEndsWith(name, ".toc")||
00093         nsStringEndsWith(name,".sbd"))
00094         return PR_TRUE;
00095  
00096     return PR_FALSE;
00097 }
00098 
00099 nsComm4xMail::nsComm4xMail()
00100 {
00101 }
00102 
00103 nsComm4xMail::~nsComm4xMail()
00104 {
00105 }
00106 
00107 nsresult nsComm4xMail::FindMailboxes(nsIFileSpec *pRoot, nsISupportsArray **ppArray)
00108 {
00109     nsresult rv = NS_NewISupportsArray(ppArray);
00110     if (NS_FAILED(rv)) {
00111         IMPORT_LOG0("FAILED to allocate the nsISupportsArray\n");
00112         return rv;
00113     }
00114         
00115     nsCOMPtr<nsIImportService> impSvc(do_GetService(NS_IMPORTSERVICE_CONTRACTID, &rv));
00116     if (NS_FAILED(rv))
00117         return rv;
00118     
00119     m_depth = 0;
00120 
00121     return (ScanMailDir(pRoot, *ppArray, impSvc));
00122 }
00123 
00124 
00125 nsresult nsComm4xMail::ScanMailDir(nsIFileSpec *pFolder, nsISupportsArray *pArray, nsIImportService *pImport)
00126 {
00127     
00128     nsCOMPtr<nsIFileSpec>    descMap;
00129     nsresult                rv;
00130 
00131     if (NS_FAILED(rv = NS_NewFileSpec(getter_AddRefs(descMap))))
00132         return rv;
00133 
00134     m_depth++;
00135 
00136     descMap->FromFileSpec(pFolder);
00137     rv = IterateMailDir(pFolder, pArray, pImport);
00138     
00139     m_depth--;
00140 
00141     return rv;            
00142 }
00143 
00144 nsresult nsComm4xMail::IterateMailDir(nsIFileSpec *pFolder, nsISupportsArray *pArray, nsIImportService *pImport)
00145 {
00146     nsCOMPtr<nsIDirectoryIterator>    dir;
00147     nsresult rv = NS_NewDirectoryIterator(getter_AddRefs(dir));
00148     if (NS_FAILED(rv))
00149         return rv;
00150 
00151     PRBool    exists = PR_FALSE;
00152     rv = dir->Init(pFolder, PR_TRUE);
00153     if (NS_FAILED(rv))
00154         return rv;
00155 
00156     rv = dir->Exists(&exists);
00157     if (NS_FAILED(rv))
00158         return rv;
00159     
00160     PRBool                    isFile;
00161     nsCOMPtr<nsIFileSpec>    entry;
00162     nsXPIDLCString                    pName;
00163     nsXPIDLCString                 dirName;
00164     nsAutoString currentFolderNameStr;
00165     PRBool                    isDirectory;
00166     nsAutoString ext;
00167 
00168     while (exists && NS_SUCCEEDED(rv)) {
00169         rv = dir->GetCurrentSpec(getter_AddRefs(entry));
00170         if (NS_SUCCEEDED(rv)) {
00171             rv = entry->GetLeafName(getter_Copies(pName));
00172             NS_CopyNativeToUnicode(pName, currentFolderNameStr);
00173             isFile = PR_FALSE;
00174             entry->IsFile(&isFile);
00175             if (isFile) {
00176                 if (!nsShouldIgnoreFile(currentFolderNameStr)) {
00177                     rv = FoundMailbox(entry, &currentFolderNameStr, pArray, pImport);
00178                     if (NS_FAILED(rv))
00179                         return rv;
00180                     entry->GetNativePath(getter_Copies(dirName));
00181                     dirName.Append(".sbd");
00182                     rv = entry->SetNativePath(dirName.get());
00183                     if (NS_FAILED(rv))
00184                         return rv;
00185                     exists = PR_FALSE;
00186                     entry->Exists(&exists);
00187                     isDirectory = PR_FALSE;
00188                     entry->IsDirectory(&isDirectory);
00189                     if (exists && isDirectory) {
00190                         rv = ScanMailDir (entry, pArray, pImport);
00191                         if (NS_FAILED(rv))
00192                             return rv;
00193                     }
00194 
00195                 }
00196             }
00197         }
00198         rv = dir->Next();
00199         if (NS_SUCCEEDED(rv))
00200             rv = dir->Exists(&exists);
00201     }
00202 
00203     return rv;
00204 }
00205 
00206 nsresult nsComm4xMail::FoundMailbox(nsIFileSpec *mailFile, nsAutoString *pName, nsISupportsArray *pArray, nsIImportService *pImport)
00207 {
00208     nsCOMPtr<nsIImportMailboxDescriptor>    desc;
00209 
00210     nsXPIDLCString pPath;
00211     mailFile->GetNativePath(getter_Copies(pPath));
00212     if (!pPath.IsEmpty())
00213       IMPORT_LOG2("Found comm4x mailbox: %s, m_depth = %d\n", pPath.get(), m_depth);
00214     else
00215       IMPORT_LOG2("Can't get native path but found comm4x mailbox: %s, m_depth = %d\n", NS_ConvertUTF16toUTF8(*pName).get(), m_depth);
00216 
00217     nsresult rv = pImport->CreateNewMailboxDescriptor(getter_AddRefs(desc));
00218     if (NS_SUCCEEDED(rv)) {
00219         PRUint32        sz = 0;
00220         mailFile->GetFileSize(&sz);    
00221         desc->SetDisplayName(pName->get());
00222         desc->SetDepth(m_depth);
00223         desc->SetSize(sz);
00224         nsCOMPtr <nsIFileSpec> pSpec;
00225         desc->GetFileSpec(getter_AddRefs(pSpec));
00226         if (pSpec) {
00227             pSpec->FromFileSpec(mailFile);
00228         }
00229         nsCOMPtr <nsISupports> pInterface = do_QueryInterface(desc);
00230         if (pInterface)
00231             pArray->AppendElement(pInterface);
00232     }
00233     return NS_OK;
00234 }
00235