Follow-on fix for bug 457825. Use sheet principal for agent and user sheets. r=dbaron...
[wine-gecko.git] / embedding / qa / testembed / ProfileMgr.cpp
blob7157235f793ee760028b2b0a29b098808ec79454
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
16 * The Original Code is the Mozilla browser.
18 * The Initial Developer of the Original Code is
19 * Netscape Communications, Inc.
20 * Portions created by the Initial Developer are Copyright (C) 1999
21 * the Initial Developer. All Rights Reserved.
23 * Contributor(s):
24 * Conrad Carlen <ccarlen@netscape.com>
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 // Local Includes
41 #include "stdafx.h"
42 #include "Testembed.h"
43 #include "ProfileMgr.h"
44 #include "ProfilesDlg.h"
46 // Mozilla Includes
47 #include "nsString.h"
48 #include "nsXPIDLString.h"
49 #include "nsIRegistry.h"
50 #include "nsIProfile.h"
52 // Constants
53 #define kRegistryGlobalPrefsSubtreeString (NS_LITERAL_STRING("global-prefs"))
54 #define kRegistryShowProfilesAtStartup "start-show-dialog"
56 //*****************************************************************************
57 //*** CProfileMgr: Object Management
58 //*****************************************************************************
60 CProfileMgr::CProfileMgr()
64 CProfileMgr::~CProfileMgr()
69 //*****************************************************************************
70 //*** CProfileMgr: Public Methods
71 //*****************************************************************************
73 nsresult CProfileMgr::StartUp()
75 nsresult rv;
77 //NS_WITH_SERVICE(nsIProfile, profileService, NS_PROFILE_CONTRACTID, &rv);
78 nsCOMPtr<nsIProfile> profileService(do_GetService(NS_PROFILE_CONTRACTID,&rv));
79 if (NS_FAILED(rv)) return rv;
81 PRInt32 profileCount;
82 rv = profileService->GetProfileCount(&profileCount);
83 if (NS_FAILED(rv)) return rv;
84 if (profileCount == 0)
86 // Make a new default profile
87 NS_NAMED_LITERAL_STRING(newProfileName, "default");
89 rv = profileService->CreateNewProfile(newProfileName.get(), nsnull, nsnull, PR_FALSE);
90 if (NS_FAILED(rv)) return rv;
91 rv = profileService->SetCurrentProfile(newProfileName.get());
92 if (NS_FAILED(rv)) return rv;
94 else
96 // Use our flag here to chek for whether to show profile mgr UI. Ifc the flag
97 // says don't show it, just start with the last used profile.
99 PRBool showIt;
100 rv = GetShowDialogOnStart(&showIt);
102 if (NS_FAILED(rv) || (profileCount > 1 && showIt))
104 DoManageProfilesDialog(TRUE);
106 else
108 // GetCurrentProfile returns the profile which was last used but is not nescesarily
109 // active. Call SetCurrentProfile to make it installed and active.
111 nsXPIDLString currProfileName;
112 rv = profileService->GetCurrentProfile(getter_Copies(currProfileName));
113 if (NS_FAILED(rv)) return rv;
114 rv = profileService->SetCurrentProfile(currProfileName);
115 if (NS_FAILED(rv)) return rv;
119 return NS_OK;
122 nsresult CProfileMgr::DoManageProfilesDialog(PRBool bAtStartUp)
124 CProfilesDlg dialog;
125 nsresult rv;
126 PRBool showIt;
128 rv = GetShowDialogOnStart(&showIt);
129 dialog.m_bAtStartUp = bAtStartUp;
130 dialog.m_bAskAtStartUp = NS_SUCCEEDED(rv) ? showIt : TRUE;
132 if (dialog.DoModal() == IDOK)
134 SetShowDialogOnStart(dialog.m_bAskAtStartUp);
136 //NS_WITH_SERVICE(nsIProfile, profileService, NS_PROFILE_CONTRACTID, &rv);
137 nsCOMPtr<nsIProfile> profileService(do_GetService(NS_PROFILE_CONTRACTID,&rv));
138 if (NS_FAILED(rv))
139 rv = profileService->SetCurrentProfile(dialog.m_SelectedProfile.get());
141 return NS_OK;
145 //*****************************************************************************
146 //*** CProfileMgr: Protected Methods
147 //*****************************************************************************
149 nsresult CProfileMgr::GetShowDialogOnStart(PRBool* showIt)
151 nsresult rv = NS_OK;
153 *showIt = PR_TRUE;
155 nsCOMPtr<nsIRegistry> registry(do_CreateInstance(NS_REGISTRY_CONTRACTID, &rv));
156 rv = registry->OpenWellKnownRegistry(nsIRegistry::ApplicationRegistry);
157 if (NS_FAILED(rv)) return rv;
159 nsRegistryKey profilesTreeKey;
161 rv = registry->GetKey(nsIRegistry::Common,
162 kRegistryGlobalPrefsSubtreeString.get(),
163 &profilesTreeKey);
165 if (NS_SUCCEEDED(rv))
167 PRInt32 flagValue;
168 rv = registry->GetInt(profilesTreeKey,
169 kRegistryShowProfilesAtStartup,
170 &flagValue);
172 if (NS_SUCCEEDED(rv))
173 *showIt = (flagValue != 0);
175 return rv;
178 nsresult CProfileMgr::SetShowDialogOnStart(PRBool showIt)
180 nsresult rv = NS_OK;
182 nsCOMPtr<nsIRegistry> registry(do_CreateInstance(NS_REGISTRY_CONTRACTID, &rv));
183 rv = registry->OpenWellKnownRegistry(nsIRegistry::ApplicationRegistry);
184 if (NS_FAILED(rv)) return rv;
186 nsRegistryKey profilesTreeKey;
188 rv = registry->GetKey(nsIRegistry::Common,
189 kRegistryGlobalPrefsSubtreeString.get(),
190 &profilesTreeKey);
192 if (NS_FAILED(rv))
194 rv = registry->AddKey(nsIRegistry::Common,
195 kRegistryGlobalPrefsSubtreeString.get(),
196 &profilesTreeKey);
198 if (NS_SUCCEEDED(rv))
201 rv = registry->SetInt(profilesTreeKey,
202 kRegistryShowProfilesAtStartup,
203 showIt);
206 return rv;