1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: MNSProfileDirServiceProvider.cxx,v $
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"
35 #pragma warning(disable:4710)
38 #include "pre_include_mozilla.h"
39 #include "MNSProfileDirServiceProvider.hxx"
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()
72 nsProfileDirServiceProvider::SetProfileDir(nsIFile
* aProfileDir
)
77 NS_SUCCEEDED(aProfileDir
->Equals(mProfileDir
, &isEqual
)) && isEqual
) {
78 NS_WARNING("Setting profile dir to same as current");
81 UndefineFileLocations();
83 mProfileDir
= aProfileDir
;
87 nsresult rv
= EnsureProfileFileExists(mProfileDir
);
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);
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
120 nsProfileDirServiceProvider::GetFile(const char *prop
, PRBool
*persistant
, nsIFile
**_retval
)
123 NS_ENSURE_ARG_POINTER(persistant
);
124 NS_ENSURE_ARG_POINTER(_retval
);
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
));
153 if (localFile
&& NS_SUCCEEDED(rv
))
154 return CallQueryInterface(localFile
, _retval
);
159 //*****************************************************************************
161 //*****************************************************************************
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
));
181 nsProfileDirServiceProvider::EnsureProfileFileExists(nsIFile
*aFile
)
186 rv
= aFile
->Exists(&exists
);
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
));
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
));
203 mProfileDir
= defaultsFile
;
209 nsProfileDirServiceProvider::UndefineFileLocations()
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
);
224 //*****************************************************************************
225 // Global creation function
226 //*****************************************************************************
228 nsresult
NS_NewProfileDirServiceProvider(nsProfileDirServiceProvider
** aProvider
)
230 NS_ENSURE_ARG_POINTER(aProvider
);
233 nsProfileDirServiceProvider
*prov
= new nsProfileDirServiceProvider();
235 return NS_ERROR_OUT_OF_MEMORY
;
236 nsresult rv
= prov
->Initialize();
241 NS_ADDREF(*aProvider
= prov
);