Update ooo320-m1
[ooovba.git] / connectivity / source / drivers / mozab / bootstrap / MNSProfileDirServiceProvider.cxx
blob370b02cc1cda800d123e8fe985a12c70d1dbd3d8
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: MNSProfileDirServiceProvider.cxx,v $
10 * $Revision: 1.6 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_connectivity.hxx"
34 #if defined _MSC_VER
35 #pragma warning(disable:4710)
36 #endif
38 #include "pre_include_mozilla.h"
39 #include "MNSProfileDirServiceProvider.hxx"
40 #include "nsIAtom.h"
41 #include "nsStaticAtom.h"
42 #include "nsILocalFile.h"
43 #include "nsDirectoryServiceDefs.h"
44 #include "nsAppDirectoryServiceDefs.h"
45 #include "nsISupportsUtils.h"
46 #include "post_include_mozilla.h"
50 #define PREFS_FILE_50_NAME NS_LITERAL_CSTRING("prefs.js")
53 nsIAtom* nsProfileDirServiceProvider::sApp_PrefsDirectory50;
54 nsIAtom* nsProfileDirServiceProvider::sApp_PreferencesFile50;
55 nsIAtom* nsProfileDirServiceProvider::sApp_UserProfileDirectory50;
58 //*****************************************************************************
59 // nsProfileDirServiceProvider::nsProfileDirServiceProvider
60 //*****************************************************************************
62 nsProfileDirServiceProvider::nsProfileDirServiceProvider()
67 nsProfileDirServiceProvider::~nsProfileDirServiceProvider()
71 nsresult
72 nsProfileDirServiceProvider::SetProfileDir(nsIFile* aProfileDir)
74 if (mProfileDir) {
75 PRBool isEqual;
76 if (aProfileDir &&
77 NS_SUCCEEDED(aProfileDir->Equals(mProfileDir, &isEqual)) && isEqual) {
78 NS_WARNING("Setting profile dir to same as current");
79 return NS_OK;
81 UndefineFileLocations();
83 mProfileDir = aProfileDir;
84 if (!mProfileDir)
85 return NS_OK;
87 nsresult rv = EnsureProfileFileExists(mProfileDir);
88 return rv;
92 nsresult
93 nsProfileDirServiceProvider::Register()
95 nsCOMPtr<nsIDirectoryService> directoryService =
96 do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID);
97 if (!directoryService)
98 return NS_ERROR_FAILURE;
99 return directoryService->RegisterProvider(this);
102 nsresult
103 nsProfileDirServiceProvider::Shutdown()
105 nsCOMPtr<nsIDirectoryService> directoryService =
106 do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID);
107 if (!directoryService)
108 return NS_ERROR_FAILURE;
109 return directoryService->UnregisterProvider(this);
112 // nsProfileDirServiceProvider::nsISupports
114 NS_IMPL_THREADSAFE_ISUPPORTS1(nsProfileDirServiceProvider,
115 nsIDirectoryServiceProvider)
117 // nsProfileDirServiceProvider::nsIDirectoryServiceProvider
119 NS_IMETHODIMP
120 nsProfileDirServiceProvider::GetFile(const char *prop, PRBool *persistant, nsIFile **_retval)
122 NS_ENSURE_ARG(prop);
123 NS_ENSURE_ARG_POINTER(persistant);
124 NS_ENSURE_ARG_POINTER(_retval);
126 if (!mProfileDir)
127 return NS_ERROR_FAILURE;
129 *persistant = PR_TRUE;
130 nsIFile* domainDir = mProfileDir;
133 nsCOMPtr<nsIFile> localFile;
134 nsresult rv = NS_ERROR_FAILURE;
136 nsIAtom* inAtom = NS_NewAtom(prop);
137 NS_ENSURE_TRUE(inAtom, NS_ERROR_OUT_OF_MEMORY);
139 if (inAtom == sApp_PrefsDirectory50) {
140 rv = domainDir->Clone(getter_AddRefs(localFile));
142 else if (inAtom == sApp_PreferencesFile50) {
143 rv = domainDir->Clone(getter_AddRefs(localFile));
144 if (NS_SUCCEEDED(rv))
145 rv = localFile->AppendNative(PREFS_FILE_50_NAME);
147 else if (inAtom == sApp_UserProfileDirectory50) {
148 rv = domainDir->Clone(getter_AddRefs(localFile));
151 NS_RELEASE(inAtom);
153 if (localFile && NS_SUCCEEDED(rv))
154 return CallQueryInterface(localFile, _retval);
156 return rv;
159 //*****************************************************************************
160 // Protected methods
161 //*****************************************************************************
163 nsresult
164 nsProfileDirServiceProvider::Initialize()
167 static const nsStaticAtom provider_atoms[] = {
168 { NS_APP_PREFS_50_DIR, &sApp_PrefsDirectory50 },
169 { NS_APP_PREFS_50_FILE, &sApp_PreferencesFile50 },
170 { NS_APP_USER_PROFILE_50_DIR, &sApp_UserProfileDirectory50 },
171 { NS_APP_PROFILE_DEFAULTS_NLOC_50_DIR, nsnull },
174 // Register our directory atoms
175 NS_RegisterStaticAtoms(provider_atoms, NS_ARRAY_LENGTH(provider_atoms));
177 return NS_OK;
180 nsresult
181 nsProfileDirServiceProvider::EnsureProfileFileExists(nsIFile *aFile)
183 nsresult rv;
184 PRBool exists;
186 rv = aFile->Exists(&exists);
187 if (NS_FAILED(rv))
188 return rv;
189 if (exists)
190 return NS_OK;
192 nsCOMPtr<nsIFile> defaultsFile;
194 // Attempt first to get the localized subdir of the defaults
195 rv = NS_GetSpecialDirectory(NS_APP_PROFILE_DEFAULTS_50_DIR, getter_AddRefs(defaultsFile));
196 if (NS_FAILED(rv)) {
197 // If that has not been defined, use the top level of the defaults
198 rv = NS_GetSpecialDirectory(NS_APP_PROFILE_DEFAULTS_NLOC_50_DIR, getter_AddRefs(defaultsFile));
199 if (NS_FAILED(rv))
200 return rv;
203 mProfileDir = defaultsFile;
204 return rv;
208 nsresult
209 nsProfileDirServiceProvider::UndefineFileLocations()
211 nsresult rv;
213 nsCOMPtr<nsIProperties> directoryService =
214 do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID, &rv);
215 NS_ENSURE_TRUE(directoryService, NS_ERROR_FAILURE);
217 (void) directoryService->Undefine(NS_APP_PREFS_50_DIR);
218 (void) directoryService->Undefine(NS_APP_PREFS_50_FILE);
219 (void) directoryService->Undefine(NS_APP_USER_PROFILE_50_DIR);
221 return NS_OK;
224 //*****************************************************************************
225 // Global creation function
226 //*****************************************************************************
228 nsresult NS_NewProfileDirServiceProvider(nsProfileDirServiceProvider** aProvider)
230 NS_ENSURE_ARG_POINTER(aProvider);
231 *aProvider = nsnull;
233 nsProfileDirServiceProvider *prov = new nsProfileDirServiceProvider();
234 if (!prov)
235 return NS_ERROR_OUT_OF_MEMORY;
236 nsresult rv = prov->Initialize();
237 if (NS_FAILED(rv)) {
238 delete prov;
239 return rv;
241 NS_ADDREF(*aProvider = prov);
242 return NS_OK;