Back to index

lightning-sunbird  0.9+nobinonly
ProfileMgr.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; 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 the Mozilla browser.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications, Inc.
00020  * Portions created by the Initial Developer are Copyright (C) 1999
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Conrad Carlen <ccarlen@netscape.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 // Local Includes
00041 #include "stdafx.h"
00042 #include "Testembed.h"
00043 #include "ProfileMgr.h"
00044 #include "ProfilesDlg.h"
00045 
00046 // Mozilla Includes
00047 #include "nsString.h"
00048 #include "nsXPIDLString.h"
00049 #include "nsIRegistry.h"
00050 #include "nsIProfile.h"
00051 
00052 // Constants
00053 #define kRegistryGlobalPrefsSubtreeString (NS_LITERAL_STRING("global-prefs"))
00054 #define kRegistryShowProfilesAtStartup "start-show-dialog"
00055 
00056 //*****************************************************************************
00057 //***    CProfileMgr: Object Management
00058 //*****************************************************************************
00059 
00060 CProfileMgr::CProfileMgr()
00061 {
00062 }
00063 
00064 CProfileMgr::~CProfileMgr()
00065 {
00066 }
00067 
00068 
00069 //*****************************************************************************
00070 //***    CProfileMgr: Public Methods
00071 //*****************************************************************************
00072     
00073 nsresult CProfileMgr::StartUp()
00074 {
00075     nsresult rv;
00076          
00077     //NS_WITH_SERVICE(nsIProfile, profileService, NS_PROFILE_CONTRACTID, &rv);
00078     nsCOMPtr<nsIProfile> profileService(do_GetService(NS_PROFILE_CONTRACTID,&rv));
00079     if (NS_FAILED(rv)) return rv;
00080         
00081     PRInt32 profileCount;
00082     rv = profileService->GetProfileCount(&profileCount);
00083     if (NS_FAILED(rv)) return rv;
00084     if (profileCount == 0)
00085     {
00086         // Make a new default profile
00087         NS_NAMED_LITERAL_STRING(newProfileName, "default");
00088 
00089         rv = profileService->CreateNewProfile(newProfileName.get(), nsnull, nsnull, PR_FALSE);
00090         if (NS_FAILED(rv)) return rv;
00091         rv = profileService->SetCurrentProfile(newProfileName.get());
00092         if (NS_FAILED(rv)) return rv;
00093     }
00094     else
00095     {
00096         // Use our flag here to chek for whether to show profile mgr UI. Ifc the flag
00097         // says don't show it, just start with the last used profile.
00098         
00099         PRBool showIt;
00100         rv = GetShowDialogOnStart(&showIt);
00101                         
00102         if (NS_FAILED(rv) || (profileCount > 1 && showIt))
00103         {
00104             DoManageProfilesDialog(TRUE);
00105         }
00106         else
00107         {
00108             // GetCurrentProfile returns the profile which was last used but is not nescesarily
00109             // active. Call SetCurrentProfile to make it installed and active.
00110             
00111             nsXPIDLString   currProfileName;
00112             rv = profileService->GetCurrentProfile(getter_Copies(currProfileName));
00113             if (NS_FAILED(rv)) return rv;
00114             rv = profileService->SetCurrentProfile(currProfileName);
00115             if (NS_FAILED(rv)) return rv;
00116         }    
00117     }
00118 
00119     return NS_OK;
00120 }
00121 
00122 nsresult CProfileMgr::DoManageProfilesDialog(PRBool bAtStartUp)
00123 {
00124     CProfilesDlg    dialog;
00125     nsresult        rv;
00126     PRBool          showIt;
00127 
00128     rv = GetShowDialogOnStart(&showIt);
00129     dialog.m_bAtStartUp = bAtStartUp;
00130     dialog.m_bAskAtStartUp = NS_SUCCEEDED(rv) ? showIt : TRUE;
00131 
00132     if (dialog.DoModal() == IDOK)
00133     {
00134         SetShowDialogOnStart(dialog.m_bAskAtStartUp);
00135          
00136         //NS_WITH_SERVICE(nsIProfile, profileService, NS_PROFILE_CONTRACTID, &rv);
00137            nsCOMPtr<nsIProfile> profileService(do_GetService(NS_PROFILE_CONTRACTID,&rv));
00138         if (NS_FAILED(rv))
00139                rv = profileService->SetCurrentProfile(dialog.m_SelectedProfile.get());
00140     }
00141     return NS_OK;
00142 }
00143     
00144  
00145 //*****************************************************************************
00146 //***    CProfileMgr: Protected Methods
00147 //*****************************************************************************
00148      
00149 nsresult CProfileMgr::GetShowDialogOnStart(PRBool* showIt)
00150 {
00151     nsresult rv = NS_OK;
00152         
00153     *showIt = PR_TRUE;
00154                 
00155     nsCOMPtr<nsIRegistry> registry(do_CreateInstance(NS_REGISTRY_CONTRACTID, &rv));
00156     rv = registry->OpenWellKnownRegistry(nsIRegistry::ApplicationRegistry);
00157     if (NS_FAILED(rv)) return rv;
00158 
00159     nsRegistryKey profilesTreeKey;
00160     
00161     rv = registry->GetKey(nsIRegistry::Common, 
00162                           kRegistryGlobalPrefsSubtreeString.get(), 
00163                           &profilesTreeKey);
00164 
00165     if (NS_SUCCEEDED(rv)) 
00166     {
00167         PRInt32 flagValue;
00168         rv = registry->GetInt(profilesTreeKey, 
00169                               kRegistryShowProfilesAtStartup, 
00170                               &flagValue);
00171          
00172         if (NS_SUCCEEDED(rv))
00173             *showIt = (flagValue != 0);
00174     }
00175     return rv;        
00176 }
00177 
00178 nsresult CProfileMgr::SetShowDialogOnStart(PRBool showIt)
00179 {
00180     nsresult rv = NS_OK;
00181                         
00182     nsCOMPtr<nsIRegistry> registry(do_CreateInstance(NS_REGISTRY_CONTRACTID, &rv));
00183     rv = registry->OpenWellKnownRegistry(nsIRegistry::ApplicationRegistry);
00184     if (NS_FAILED(rv)) return rv;
00185 
00186     nsRegistryKey profilesTreeKey;
00187     
00188     rv = registry->GetKey(nsIRegistry::Common, 
00189                           kRegistryGlobalPrefsSubtreeString.get(), 
00190                           &profilesTreeKey);
00191 
00192     if (NS_FAILED(rv)) 
00193     {
00194         rv = registry->AddKey(nsIRegistry::Common, 
00195                               kRegistryGlobalPrefsSubtreeString.get(), 
00196                               &profilesTreeKey);
00197     }
00198     if (NS_SUCCEEDED(rv))
00199     {
00200     
00201         rv = registry->SetInt(profilesTreeKey, 
00202                               kRegistryShowProfilesAtStartup, 
00203                               showIt);
00204     }
00205     
00206     return rv;        
00207 }
00208