Back to index

lightning-sunbird  0.9+nobinonly
nsMsgAccountManager.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is mozilla.org code.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 1998
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK *****
00038  * This Original Code has been modified by IBM Corporation. Modifications made by IBM 
00039  * described herein are Copyright (c) International Business Machines Corporation, 2000.
00040  * Modifications to Mozilla code or documentation identified per MPL Section 3.3
00041  *
00042  * Date             Modified by     Description of modification
00043  * 04/20/2000       IBM Corp.      OS/2 VisualAge build.
00044  */
00045 
00046 #include "nscore.h"
00047 #include "nsIMsgAccountManager.h"
00048 #include "nsCOMPtr.h"
00049 #include "nsHashtable.h"
00050 #include "nsISmtpServer.h"
00051 #include "nsIPrefBranch.h"
00052 #include "nsIMsgFolderCache.h"
00053 #include "nsIMsgFolder.h"
00054 #include "nsIObserver.h"
00055 #include "nsWeakReference.h"
00056 #include "nsIUrlListener.h"
00057 #include "nsCOMArray.h"
00058 
00059 class nsIRDFService;
00060 
00061 class nsMsgAccountManager: public nsIMsgAccountManager,
00062     public nsIObserver,
00063     public nsSupportsWeakReference,
00064     public nsIUrlListener,
00065     public nsIFolderListener
00066 {
00067 public:
00068 
00069   nsMsgAccountManager();
00070   virtual ~nsMsgAccountManager();
00071   
00072   NS_DECL_ISUPPORTS
00073  
00074   /* nsIMsgAccountManager methods */
00075   
00076   NS_DECL_NSIMSGACCOUNTMANAGER
00077   NS_DECL_NSIOBSERVER  
00078   NS_DECL_NSIURLLISTENER
00079   NS_DECL_NSIFOLDERLISTENER
00080 
00081   nsresult Init();
00082   nsresult Shutdown();
00083 
00084 private:
00085 
00086   PRBool m_accountsLoaded;
00087   nsCOMPtr <nsIMsgFolderCache>     m_msgFolderCache;
00088   nsCOMPtr<nsIAtom> kDefaultServerAtom;
00089   nsCOMPtr<nsISupportsArray> m_accounts;
00090   nsHashtable m_identities;
00091   nsHashtable m_incomingServers;
00092   nsCOMPtr<nsIMsgAccount> m_defaultAccount;
00093   nsCOMArray<nsIIncomingServerListener> m_incomingServerListeners;
00094   nsCOMArray<nsIDBChangeListener> m_virtualFolderListeners;
00095   nsCOMPtr<nsIMsgFolder> m_folderDoingEmptyTrash;
00096   nsCOMPtr<nsIMsgFolder> m_folderDoingCleanupInbox;
00097   PRBool m_emptyTrashInProgress;
00098   PRBool m_cleanupInboxInProgress;
00099 
00100   nsCString mAccountKeyList;
00101   
00102   PRBool m_haveShutdown;
00103   PRBool m_shutdownInProgress;
00104   PRBool m_userAuthenticated;
00105   PRBool m_loadingVirtualFolders;
00106   PRBool m_virtualFoldersLoaded;
00107 
00108   /* we call FindServer() a lot.  so cache the last server found */
00109   nsCOMPtr <nsIMsgIncomingServer> m_lastFindServerResult;
00110   nsCString m_lastFindServerHostName;
00111   nsCString m_lastFindServerUserName;
00112   PRInt32 m_lastFindServerPort;
00113   nsCString m_lastFindServerType;
00114 
00115   nsresult SetLastServerFound(nsIMsgIncomingServer *server, const char *hostname, const char *username, const PRInt32 port, const char *type);
00116 
00117   /* internal creation routines - updates m_identities and m_incomingServers */
00118   nsresult createKeyedAccount(const char* key,
00119                               nsIMsgAccount **_retval);
00120   nsresult createKeyedServer(const char*key,
00121                              const char* username,
00122                              const char* password,
00123                              const char* type,
00124                              nsIMsgIncomingServer **_retval);
00125 
00126   nsresult createKeyedIdentity(const char* key,
00127                                nsIMsgIdentity **_retval);
00128 
00129   /* internal destruction routines - fixes prefs */
00130   nsresult removeKeyedAccount(const char *key);
00131 
00132 
00133   // sets the pref for the defualt server
00134   nsresult setDefaultAccountPref(nsIMsgAccount *aDefaultAccount);
00135 
00136   // fires notifications to the appropriate root folders
00137   nsresult notifyDefaultServerChange(nsIMsgAccount *aOldAccount,
00138                                      nsIMsgAccount *aNewAccount);
00139     
00140   static void LogoutOfServer(nsIMsgIncomingServer *aServer);
00141   // hash table enumerators
00142 
00143   // add each member of a hash table to an nsISupports array
00144   static PRBool hashElementToArray(nsHashKey *aKey, void *aData,
00145                                    void *closure);
00146 
00147   // called by EnumerateRemove to release all elements
00148   static PRBool PR_CALLBACK hashElementRelease(nsHashKey *aKey, void *aData,
00149                                    void *closure);
00150 
00151   // Send unload server notification.
00152   static PRBool PR_CALLBACK hashUnloadServer(nsHashKey *aKey, void *aData,
00153                                      void *closure);
00154 
00155   // shutdown server and forget cached password
00156   static PRBool PR_CALLBACK hashLogoutOfServer(nsHashKey *aKey, void *aData,
00157                                      void *closure);
00158 
00159   // clean up on exit
00160   static PRBool PR_CALLBACK cleanupOnExit(nsHashKey *aKey, void *aData,
00161                                      void *closure);
00162   //
00163   // account enumerators
00164   // ("element" is always an account)
00165   //
00166   
00167   // append the account keys to the given string
00168   static PRBool getAccountList(nsISupports *aKey, void *aData);
00169 
00170   // find the identities that correspond to the given server
00171   static PRBool findIdentitiesForServer(nsISupports *element, void *aData);
00172 
00173   // find the servers that correspond to the given identity
00174   static PRBool findServersForIdentity (nsISupports *element, void *aData);
00175 
00176   static PRBool findServerIndexByServer(nsISupports *element, void *aData);
00177   // find the account with the given key
00178   static PRBool findAccountByKey (nsISupports *element, void *aData);
00179 
00180   static PRBool findAccountByServerKey (nsISupports *element, void *aData);
00181 
00182   // load up the identities into the given nsISupportsArray
00183   static PRBool getIdentitiesToArray(nsISupports *element, void *aData);
00184 
00185   // add identities if they don't alreadby exist in the given nsISupportsArray
00186   static PRBool addIdentityIfUnique(nsISupports *element, void *aData);
00187 
00188   //
00189   // server enumerators
00190   // ("element" is always a server)
00191   //
00192   
00193   // load up the servers into the given nsISupportsArray
00194   static PRBool PR_CALLBACK getServersToArray(nsHashKey* aKey, void *element, void *aData);
00195 
00196   // find the server given by {username, hostname, type}
00197   static PRBool findServer(nsISupports *aElement, void *data);
00198 
00199   // find the server given by {username, hostname, port, type}
00200   static PRBool findServerUrl(nsISupports *aElement, void *data);
00201 
00202   // write out the server's cache through the given folder cache
00203   static PRBool PR_CALLBACK writeFolderCache(nsHashKey *aKey, void *aData, void *closure);
00204   static PRBool PR_CALLBACK shutdown(nsHashKey *aKey, void *aData, void *closure);
00205   static PRBool PR_CALLBACK closeCachedConnections(nsHashKey *aKey, void *aData, void *closure);
00206 
00207   // handle virtual folders
00208   nsresult GetVirtualFoldersFile(nsCOMPtr<nsILocalFile>& file);
00209   nsresult WriteLineToOutputStream(const char *prefix, const char * line, nsIOutputStream *outputStream);
00210   nsresult AddVFListenersForVF(nsIMsgFolder *virtualFolder, 
00211                                const char *srchFolderUris,
00212                                nsIRDFService *rdf,
00213                                nsIMsgDBService *msgDBService);
00214 
00215   static void getUniqueKey(const char* prefix,
00216                            nsHashtable *hashTable,
00217                            nsCString& aResult);
00218   static void getUniqueAccountKey(const char* prefix,
00219                                   nsISupportsArray *accounts,
00220                                   nsCString& aResult);
00221 
00222   
00223   nsresult SetSendLaterUriPref(nsIMsgIncomingServer *server);
00224  
00225   nsresult getPrefService();
00226   nsCOMPtr<nsIPrefBranch> m_prefs;
00227 
00228   nsresult InternalFindServer(const char* username, const char* hostname, const char* type, PRBool useRealSetting, nsIMsgIncomingServer** aResult);
00229 
00230   //
00231   // root folder listener stuff
00232   //
00233   
00234   // this array is for folder listeners that are supposed to be listening
00235   // on the root folders.
00236   // When a new server is created, all of the the folder listeners
00237   //    should be added to the new server
00238   // When a new listener is added, it should be added to all root folders.
00239   // similar for when servers are deleted or listeners removed
00240   nsCOMPtr<nsISupportsArray> mFolderListeners;
00241   
00242   // add and remove listeners from the given server
00243   static PRBool PR_CALLBACK addListener(nsHashKey *aKey, void *element, void *aData);
00244   static PRBool PR_CALLBACK removeListener(nsHashKey *aKey, void *element, void *aData);
00245   
00246   // folder listener enumerators
00247   static PRBool addListenerToFolder(nsISupports *element, void *data);
00248   static PRBool removeListenerFromFolder(nsISupports *element, void *data);
00249 };
00250