Back to index

lightning-sunbird  0.9+nobinonly
ProfileMgr.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: Mozilla-sample-code 1.0
00003  *
00004  * Copyright (c) 2002 Netscape Communications Corporation and
00005  * other contributors
00006  *
00007  * Permission is hereby granted, free of charge, to any person obtaining a
00008  * copy of this Mozilla sample software and associated documentation files
00009  * (the "Software"), to deal in the Software without restriction, including
00010  * without limitation the rights to use, copy, modify, merge, publish,
00011  * distribute, sublicense, and/or sell copies of the Software, and to permit
00012  * persons to whom the Software is furnished to do so, subject to the
00013  * following conditions:
00014  *
00015  * The above copyright notice and this permission notice shall be included
00016  * in all copies or substantial portions of the Software.
00017  *
00018  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00019  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00020  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
00021  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00022  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00023  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00024  * DEALINGS IN THE SOFTWARE.
00025  *
00026  * Contributor(s):
00027  *   Conrad Carlen <ccarlen@netscape.com>
00028  *
00029  * ***** END LICENSE BLOCK ***** */
00030 
00031 // Local Includes
00032 #include "stdafx.h"
00033 #include "mfcembed.h"
00034 #include "ProfileMgr.h"
00035 #include "ProfilesDlg.h"
00036 
00037 // Mozilla Includes
00038 #include "nsEmbedString.h"
00039 #include "nsIRegistry.h"
00040 #include "nsIProfile.h"
00041 #include "nsServiceManagerUtils.h"
00042 #include "nsComponentManagerUtils.h"
00043 #include "nsCOMPtr.h"
00044 #include "nsMemory.h"
00045 
00046 // Constants
00047 #define kRegistryGlobalPrefsSubtreeString (nsEmbedString(L"global-prefs"))
00048 #define kRegistryShowProfilesAtStartup "start-show-dialog"
00049 
00050 //*****************************************************************************
00051 //***    CProfileMgr: Object Management
00052 //*****************************************************************************
00053 
00054 CProfileMgr::CProfileMgr()
00055 {
00056 }
00057 
00058 CProfileMgr::~CProfileMgr()
00059 {
00060 }
00061 
00062 
00063 //*****************************************************************************
00064 //***    CProfileMgr: Public Methods
00065 //*****************************************************************************
00066     
00067 nsresult CProfileMgr::StartUp()
00068 {
00069     nsresult rv;
00070          
00071     nsCOMPtr<nsIProfile> profileService = 
00072              do_GetService(NS_PROFILE_CONTRACTID, &rv);
00073     if (NS_FAILED(rv)) return rv;
00074         
00075     PRInt32 profileCount;
00076     rv = profileService->GetProfileCount(&profileCount);
00077     if (NS_FAILED(rv)) return rv;
00078     if (profileCount == 0)
00079     {
00080         // Make a new default profile
00081         nsEmbedString newProfileName(L"default");
00082 
00083         rv = profileService->CreateNewProfile(newProfileName.get(), nsnull, nsnull, PR_FALSE);
00084         if (NS_FAILED(rv)) return rv;
00085         rv = profileService->SetCurrentProfile(newProfileName.get());
00086         if (NS_FAILED(rv)) return rv;
00087     }
00088     else
00089     {
00090         // Use our flag here to check for whether to show profile mgr UI. If the flag
00091         // says don't show it, just start with the last used profile.
00092         
00093         PRBool showIt;
00094         rv = GetShowDialogOnStart(&showIt);
00095                         
00096         if (NS_FAILED(rv) || (profileCount > 1 && showIt))
00097         {
00098             DoManageProfilesDialog(TRUE);
00099         }
00100         else
00101         {
00102             // GetCurrentProfile returns the profile which was last used but is not nescesarily
00103             // active. Call SetCurrentProfile to make it installed and active.
00104             
00105             PRUnichar *currProfileName = nsnull;
00106             rv = profileService->GetCurrentProfile(&currProfileName);
00107             if (NS_FAILED(rv)) return rv;
00108             rv = profileService->SetCurrentProfile(currProfileName);
00109             nsMemory::Free(currProfileName);
00110             if (NS_FAILED(rv)) return rv;
00111         }    
00112     }
00113 
00114     return NS_OK;
00115 }
00116 
00117 nsresult CProfileMgr::DoManageProfilesDialog(PRBool bAtStartUp)
00118 {
00119     CProfilesDlg    dialog;
00120     nsresult        rv;
00121     PRBool          showIt;
00122 
00123     rv = GetShowDialogOnStart(&showIt);
00124     dialog.m_bAtStartUp = bAtStartUp;
00125     dialog.m_bAskAtStartUp = NS_SUCCEEDED(rv) ? showIt : TRUE;
00126 
00127     if (dialog.DoModal() == IDOK)
00128     {
00129         SetShowDialogOnStart(dialog.m_bAskAtStartUp);
00130          
00131         nsCOMPtr<nsIProfile> profileService = 
00132                  do_GetService(NS_PROFILE_CONTRACTID, &rv);
00133         if (NS_SUCCEEDED(rv))
00134                rv = profileService->SetCurrentProfile(dialog.m_SelectedProfile.get());
00135     }
00136     return NS_OK;
00137 }
00138     
00139  
00140 //*****************************************************************************
00141 //***    CProfileMgr: Protected Methods
00142 //*****************************************************************************
00143      
00144 nsresult CProfileMgr::GetShowDialogOnStart(PRBool* showIt)
00145 {
00146     nsresult rv = NS_OK;
00147         
00148     *showIt = PR_TRUE;
00149                 
00150     nsCOMPtr<nsIRegistry> registry(do_CreateInstance(NS_REGISTRY_CONTRACTID, &rv));
00151     rv = registry->OpenWellKnownRegistry(nsIRegistry::ApplicationRegistry);
00152     if (NS_FAILED(rv)) return rv;
00153 
00154     nsRegistryKey profilesTreeKey;
00155     
00156     rv = registry->GetKey(nsIRegistry::Common, 
00157                           kRegistryGlobalPrefsSubtreeString.get(), 
00158                           &profilesTreeKey);
00159 
00160     if (NS_SUCCEEDED(rv)) 
00161     {
00162         PRInt32 flagValue;
00163         rv = registry->GetInt(profilesTreeKey, 
00164                               kRegistryShowProfilesAtStartup, 
00165                               &flagValue);
00166          
00167         if (NS_SUCCEEDED(rv))
00168             *showIt = (flagValue != 0);
00169     }
00170     return rv;        
00171 }
00172 
00173 nsresult CProfileMgr::SetShowDialogOnStart(PRBool showIt)
00174 {
00175     nsresult rv = NS_OK;
00176                         
00177     nsCOMPtr<nsIRegistry> registry(do_CreateInstance(NS_REGISTRY_CONTRACTID, &rv));
00178     rv = registry->OpenWellKnownRegistry(nsIRegistry::ApplicationRegistry);
00179     if (NS_FAILED(rv)) return rv;
00180 
00181     nsRegistryKey profilesTreeKey;
00182     
00183     rv = registry->GetKey(nsIRegistry::Common, 
00184                           kRegistryGlobalPrefsSubtreeString.get(), 
00185                           &profilesTreeKey);
00186 
00187     if (NS_FAILED(rv)) 
00188     {
00189         rv = registry->AddKey(nsIRegistry::Common, 
00190                               kRegistryGlobalPrefsSubtreeString.get(), 
00191                               &profilesTreeKey);
00192     }
00193     if (NS_SUCCEEDED(rv))
00194     {
00195     
00196         rv = registry->SetInt(profilesTreeKey, 
00197                               kRegistryShowProfilesAtStartup, 
00198                               showIt);
00199     }
00200     
00201     return rv;        
00202 }
00203