Back to index

lightning-sunbird  0.9+nobinonly
nsNoIncomingServer.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
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 "msgCore.h" // pre-compiled headers
00039 
00040 #include "prmem.h"
00041 #include "plstr.h"
00042 #include "prprf.h"
00043 
00044 #include "nsIFileSpec.h"
00045 #include "nsXPIDLString.h"
00046 
00047 #include "nsNoIncomingServer.h"
00048 
00049 #include "nsMsgLocalCID.h"
00050 #include "nsMsgFolderFlags.h"
00051 #include "nsIMsgLocalMailFolder.h"
00052 #include "nsIMsgMailSession.h"
00053 #include "nsMsgBaseCID.h"
00054 #include "nsIMsgAccountManager.h"
00055 #include "nsIPop3IncomingServer.h"
00056 
00057 NS_IMPL_ISUPPORTS_INHERITED2(nsNoIncomingServer,
00058                             nsMsgIncomingServer,
00059                             nsINoIncomingServer,
00060                          nsILocalMailIncomingServer)
00061 
00062                             
00063 
00064 nsNoIncomingServer::nsNoIncomingServer()
00065 {    
00066 }
00067 
00068 nsNoIncomingServer::~nsNoIncomingServer()
00069 {
00070 }
00071 
00072 nsresult
00073 nsNoIncomingServer::GetLocalStoreType(char **type)
00074 {
00075   NS_ENSURE_ARG_POINTER(type);
00076   *type = nsCRT::strdup("mailbox");
00077   return NS_OK;
00078 }
00079 
00080 NS_IMETHODIMP 
00081 nsNoIncomingServer::GetAccountManagerChrome(nsAString& aResult)
00082 {
00083   aResult.AssignLiteral("am-serverwithnoidentities.xul");
00084   return NS_OK;
00085 }
00086 
00087 NS_IMETHODIMP 
00088 nsNoIncomingServer::SetFlagsOnDefaultMailboxes()
00089 {
00090   nsCOMPtr<nsIMsgFolder> rootFolder;
00091   nsresult rv = GetRootFolder(getter_AddRefs(rootFolder));
00092   NS_ENSURE_SUCCESS(rv, rv);
00093 
00094   nsCOMPtr<nsIMsgLocalMailFolder> localFolder =
00095       do_QueryInterface(rootFolder, &rv);
00096   NS_ENSURE_SUCCESS(rv, rv);
00097 
00098   PRUint32 mailboxFlags = MSG_FOLDER_FLAG_SENTMAIL |
00099                           MSG_FOLDER_FLAG_DRAFTS |
00100                           MSG_FOLDER_FLAG_TEMPLATES |
00101                           MSG_FOLDER_FLAG_TRASH |
00102                           MSG_FOLDER_FLAG_JUNK |
00103                           MSG_FOLDER_FLAG_QUEUE;
00104  
00105   // "none" may have a inbox if it is getting deferred to.
00106   PRBool isDeferredTo;
00107   if (NS_SUCCEEDED(GetIsDeferredTo(&isDeferredTo)) && isDeferredTo)
00108     mailboxFlags |= MSG_FOLDER_FLAG_INBOX;
00109 
00110   localFolder->SetFlagsOnDefaultMailboxes(mailboxFlags);
00111 
00112   return NS_OK;
00113 }      
00114 
00115 NS_IMETHODIMP nsNoIncomingServer::CopyDefaultMessages(const char *folderNameOnDisk, nsIFileSpec *parentDir)
00116 {
00117   nsresult rv;
00118   PRBool exists;
00119   if (!folderNameOnDisk || !parentDir) return NS_ERROR_NULL_POINTER;
00120   
00121   nsCOMPtr<nsIMsgMailSession> mailSession = do_GetService(NS_MSGMAILSESSION_CONTRACTID, &rv);
00122   NS_ENSURE_SUCCESS(rv, rv);
00123   
00124   // Get defaults directory for messenger files. MailSession service appends 'messenger' to the 
00125   // the app defaults folder and returns it. Locale will be added to the path, if there is one.
00126   nsCOMPtr<nsIFile> defaultMessagesFile;
00127   rv = mailSession->GetDataFilesDir("messenger", getter_AddRefs(defaultMessagesFile));
00128   NS_ENSURE_SUCCESS(rv,rv);
00129   
00130   // check if bin/defaults/messenger/<folderNameOnDisk> 
00131   // (or bin/defaults/messenger/<locale>/<folderNameOnDisk> if we had a locale provide) exists.
00132   // it doesn't have to exist.  if it doesn't, return
00133   rv = defaultMessagesFile->AppendNative(nsDependentCString(folderNameOnDisk));
00134   if (NS_FAILED(rv)) return rv;
00135   rv = defaultMessagesFile->Exists(&exists);
00136   if (NS_FAILED(rv)) return rv;
00137   if (!exists) return NS_OK;
00138   
00139   // Make an nsILocalFile of the parentDir
00140   nsFileSpec parentDirSpec;
00141   nsCOMPtr<nsILocalFile> localParentDir;
00142   
00143   rv = parentDir->GetFileSpec(&parentDirSpec);
00144   if (NS_FAILED(rv)) return rv;
00145   rv = NS_FileSpecToIFile(&parentDirSpec, getter_AddRefs(localParentDir));
00146   if (NS_FAILED(rv)) return rv;
00147   
00148   // check if parentDir/<folderNameOnDisk> exists
00149   {
00150     nsCOMPtr<nsIFile> testDir;
00151     rv = localParentDir->Clone(getter_AddRefs(testDir));
00152     if (NS_FAILED(rv)) return rv;
00153     rv = testDir->AppendNative(nsDependentCString(folderNameOnDisk));
00154     if (NS_FAILED(rv)) return rv;
00155     rv = testDir->Exists(&exists);
00156     if (NS_FAILED(rv)) return rv;
00157   }
00158   
00159   // if it exists add to the end, else copy
00160   if (exists) 
00161   {
00162 #ifdef DEBUG_sspitzer
00163     printf("append default %s\n",folderNameOnDisk);
00164 #endif
00165     // todo for bug #1181
00166     // open folderFile, seek to end
00167     // read defaultMessagesFile, write to folderFile
00168   }
00169   else {
00170 #ifdef DEBUG_sspitzer
00171     printf("copy default %s\n",folderNameOnDisk);
00172 #endif
00173     rv = defaultMessagesFile->CopyTo(localParentDir, EmptyString());
00174     if (NS_FAILED(rv)) return rv;
00175   }
00176   return NS_OK;
00177 }
00178 
00179 
00180 NS_IMETHODIMP nsNoIncomingServer::CreateDefaultMailboxes(nsIFileSpec *path)
00181 {
00182   nsresult rv;
00183   if (!path) 
00184     return NS_ERROR_NULL_POINTER;
00185   
00186   // notice, no Inbox, unless we're deferred to...
00187    // need to have a leaf to start with
00188   rv = path->AppendRelativeUnixPath("Trash"); 
00189   PRBool isDeferredTo;
00190   if (NS_SUCCEEDED(GetIsDeferredTo(&isDeferredTo)) && isDeferredTo)
00191     CreateLocalFolder(path, "Inbox");
00192   CreateLocalFolder(path, "Trash");
00193   if (NS_FAILED(rv)) return rv;
00194   
00195   // copy the default templates into the Templates folder
00196   nsCOMPtr<nsIFileSpec> parentDir;
00197   rv = path->GetParent(getter_AddRefs(parentDir));
00198   if (NS_FAILED(rv)) return rv;
00199   rv = CopyDefaultMessages("Templates",parentDir);
00200   if (NS_FAILED(rv)) return rv;
00201   
00202   (void ) CreateLocalFolder(path, "Unsent Messages");
00203   return NS_OK;
00204 }
00205 
00206 NS_IMETHODIMP
00207 nsNoIncomingServer::GetNewMail(nsIMsgWindow *aMsgWindow, nsIUrlListener *aUrlListener, nsIMsgFolder *aInbox, nsIURI **aResult)
00208 {
00209   nsCOMPtr <nsISupportsArray> deferredServers;
00210   nsresult rv = GetDeferredServers(this, getter_AddRefs(deferredServers));
00211   NS_ENSURE_SUCCESS(rv, rv);
00212   PRUint32 count;
00213   deferredServers->Count(&count);
00214   if (count > 0)
00215   {
00216     nsCOMPtr <nsIPop3IncomingServer> firstServer(do_QueryElementAt(deferredServers, 0));
00217     if (firstServer)
00218     {
00219       rv = firstServer->DownloadMailFromServers(deferredServers, aMsgWindow,
00220                               aInbox, 
00221                               aUrlListener);
00222     }
00223   }
00224   // listener might be counting on us to send a notification.
00225   else if (aUrlListener)
00226     aUrlListener->OnStopRunningUrl(nsnull, NS_OK);
00227   return rv;
00228 }
00229 
00230 
00231 NS_IMETHODIMP
00232 nsNoIncomingServer::GetCanSearchMessages(PRBool *canSearchMessages)
00233 {
00234   NS_ENSURE_ARG_POINTER(canSearchMessages);
00235   *canSearchMessages = PR_TRUE;
00236   return NS_OK;
00237 }
00238 
00239 NS_IMETHODIMP 
00240 nsNoIncomingServer::GetServerRequiresPasswordForBiff(PRBool *aServerRequiresPasswordForBiff)
00241 {
00242   NS_ENSURE_ARG_POINTER(aServerRequiresPasswordForBiff);
00243   *aServerRequiresPasswordForBiff = PR_FALSE;  // for local folders, we don't require a password
00244   return NS_OK;
00245 }
00246 
00247