Back to index

lightning-sunbird  0.9+nobinonly
nsPref.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; 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 mozilla.org code.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 1998
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Pierre Phaneuf <pp@ludusdesign.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  * This Original Code has been modified by IBM Corporation.
00041  * Modifications made by IBM described herein are
00042  * Copyright (c) International Business Machines
00043  * Corporation, 2000
00044  *
00045  * Modifications to Mozilla code or documentation
00046  * identified per MPL Section 3.3
00047  *
00048  * Date         Modified by     Description of modification
00049  * 03/27/2000   IBM Corp.       Added PR_CALLBACK for Optlink
00050  *                               use in OS2
00051  */
00052 
00053 #include "nsIPref.h"
00054 #include "nsIPrefBranch.h"
00055 #include "nsIPrefBranchInternal.h"
00056 #include "nsIFactory.h"
00057 #include "nsIComponentManager.h"
00058 #include "nsIObserver.h"
00059 #include "nsCOMPtr.h"
00060 #include "nsMemory.h"
00061 #include "prefapi.h"
00062 
00063 #ifndef MOZ_NO_XPCOM_OBSOLETE
00064 #include "nsIFileSpec.h"
00065 #endif
00066 
00067 #include "nsString.h"
00068 #include "nsILocalFile.h"
00069 #include "nsIPrefBranch.h"
00070 #include "nsIPrefLocalizedString.h"
00071 #include "nsISecurityPref.h"
00072 #include "nsIPrefService.h"
00073 #include "nsISupportsPrimitives.h"
00074 #include "nsWeakReference.h"
00075 
00076 #include "nsIModule.h"
00077 #include "nsIGenericFactory.h"
00078 
00079 #include "plstr.h"
00080 #include "prmem.h"
00081 #include "prprf.h"
00082 
00083 
00084 class nsPref : public nsIPref,
00085                public nsIPrefService,
00086                public nsIObserver,
00087                public nsIPrefBranchInternal,
00088                public nsISecurityPref,
00089                public nsSupportsWeakReference
00090 {
00091 public:
00092   static nsPref *GetInstance();
00093 
00094   /* Use xpidl-generated macro to declare everything required by nsIPref */
00095   NS_DECL_ISUPPORTS
00096   NS_DECL_NSIPREFBRANCH
00097   NS_DECL_NSIPREFBRANCH2
00098   NS_DECL_NSISECURITYPREF
00099   NS_DECL_NSIOBSERVER
00100   NS_FORWARD_NSIPREFSERVICE(mPrefService->)
00101 
00102   NS_IMETHOD CopyCharPref(const char *pref, char ** return_buf);
00103 
00104   NS_IMETHODIMP GetDefaultBoolPref(const char *pref, PRBool *_retval)
00105                   { return mDefaultBranch->GetBoolPref(pref, _retval); }
00106   NS_IMETHODIMP CopyDefaultCharPref(const char *pref, char **_retval)
00107                   { return mDefaultBranch->GetCharPref(pref, _retval); }
00108   NS_IMETHODIMP GetDefaultIntPref(const char *pref, PRInt32 *_retval)
00109                   { return mDefaultBranch->GetIntPref(pref, _retval); }
00110   NS_IMETHODIMP SetDefaultBoolPref(const char *pref, PRBool value)
00111                   { return mDefaultBranch->SetBoolPref(pref, value); }
00112   NS_IMETHODIMP SetDefaultCharPref(const char *pref, const char *value)
00113                   { return mDefaultBranch->SetCharPref(pref, value); }
00114   NS_IMETHODIMP SetDefaultIntPref(const char *pref, PRInt32 value)
00115                   { return mDefaultBranch->SetIntPref(pref, value); }
00116 
00117   NS_IMETHOD CopyUnicharPref(const char *pref, PRUnichar **_retval);
00118   NS_IMETHOD CopyDefaultUnicharPref(const char *pref, PRUnichar **_retval);
00119   NS_IMETHOD SetUnicharPref(const char *pref, const PRUnichar *value);
00120   NS_IMETHOD SetDefaultUnicharPref(const char *pref, const PRUnichar *value);
00121   NS_IMETHOD GetLocalizedUnicharPref(const char *pref, PRUnichar **_retval);
00122   NS_IMETHOD GetDefaultLocalizedUnicharPref(const char *pref, PRUnichar **_retval);
00123 
00124   NS_IMETHOD GetFilePref(const char *pref, nsIFileSpec **_retval);
00125   NS_IMETHOD SetFilePref(const char *pref, nsIFileSpec *value, PRBool setDefault);
00126   NS_IMETHOD GetFileXPref(const char *pref, nsILocalFile **_retval);
00127   NS_IMETHOD SetFileXPref(const char *pref, nsILocalFile *value);
00128 
00129   NS_IMETHOD RegisterCallback(const char *domain, PrefChangedFunc callback, void * closure);
00130   NS_IMETHOD UnregisterCallback(const char *domain, PrefChangedFunc callback, void * closure);
00131   NS_IMETHOD EnumerateChildren(const char *parent, PrefEnumerationFunc callback, void * data); 
00132 
00133 protected:
00134   nsPref();
00135   virtual ~nsPref();
00136 
00137   static nsPref *gInstance;
00138 
00139 private:
00140   nsCOMPtr<nsIPrefService> mPrefService;
00141   nsCOMPtr<nsIPrefBranch>  mDefaultBranch;
00142 };
00143 
00144 nsPref* nsPref::gInstance = NULL;
00145 static PRInt32 g_InstanceCount = 0;
00146 
00147 
00148 NS_IMPL_THREADSAFE_ISUPPORTS8(nsPref,
00149                               nsIPref,
00150                               nsIPrefService,
00151                               nsIObserver,
00152                               nsIPrefBranch,
00153                               nsIPrefBranch2,
00154                               nsIPrefBranchInternal,
00155                               nsISecurityPref,
00156                               nsISupportsWeakReference)
00157 
00158 //----------------------------------------------------------------------------------------
00159 nsPref::nsPref()
00160 //----------------------------------------------------------------------------------------
00161 {
00162   PR_AtomicIncrement(&g_InstanceCount);
00163 
00164   mPrefService = do_GetService(NS_PREFSERVICE_CONTRACTID);
00165   NS_ASSERTION(mPrefService, "Preference Service failed to start up!!");
00166 
00167   if (mPrefService)
00168     mPrefService->GetDefaultBranch("", getter_AddRefs(mDefaultBranch));
00169 }
00170 
00171 //----------------------------------------------------------------------------------------
00172 nsPref::~nsPref()
00173 //----------------------------------------------------------------------------------------
00174 {
00175   PR_AtomicDecrement(&g_InstanceCount);
00176   gInstance = NULL;
00177 }
00178 
00179 /*
00180  * Implementations to pass branch calls through the PrefService.
00181  */
00182 
00183 NS_IMETHODIMP nsPref::GetRoot(char * *aRoot)
00184 {
00185   nsresult rv;
00186 
00187   nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
00188   if (NS_SUCCEEDED(rv))
00189     rv = prefBranch->GetRoot(aRoot);
00190   return rv;
00191 }
00192 
00193 NS_IMETHODIMP nsPref::GetPrefType(const char *aPrefName, PRInt32 *_retval)
00194 {
00195   nsresult rv;
00196 
00197   nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
00198   if (NS_SUCCEEDED(rv))
00199     rv = prefBranch->GetPrefType(aPrefName, _retval);
00200   return rv;
00201 }
00202 
00203 NS_IMETHODIMP nsPref::CopyCharPref(const char *pref, char ** return_buf)
00204 {
00205   nsresult rv;
00206 
00207   nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
00208   if (NS_SUCCEEDED(rv))
00209     rv = prefBranch->GetCharPref(pref, return_buf);
00210   return rv;
00211 }
00212 
00213 NS_IMETHODIMP nsPref::GetBoolPref(const char *aPrefName, PRBool *_retval)
00214 {
00215   nsresult rv;
00216 
00217   nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
00218   if (NS_SUCCEEDED(rv))
00219     rv = prefBranch->GetBoolPref(aPrefName, _retval);
00220   return rv;
00221 }
00222 
00223 NS_IMETHODIMP nsPref::SetBoolPref(const char *aPrefName, PRInt32 aValue)
00224 {
00225   nsresult rv;
00226 
00227   nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
00228   if (NS_SUCCEEDED(rv))
00229     rv = prefBranch->SetBoolPref(aPrefName, aValue);
00230   return rv;
00231 }
00232 
00233 NS_IMETHODIMP nsPref::GetCharPref(const char *aPrefName, char **_retval)
00234 {
00235   nsresult rv;
00236 
00237   nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
00238   if (NS_SUCCEEDED(rv))
00239     rv = prefBranch->GetCharPref(aPrefName, _retval);
00240   return rv;
00241 }
00242 
00243 NS_IMETHODIMP nsPref::SetCharPref(const char *aPrefName, const char *aValue)
00244 {
00245   nsresult rv;
00246 
00247   nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
00248   if (NS_SUCCEEDED(rv))
00249     rv = prefBranch->SetCharPref(aPrefName, aValue);
00250   return rv;
00251 }
00252 
00253 NS_IMETHODIMP nsPref::GetIntPref(const char *aPrefName, PRInt32 *_retval)
00254 {
00255   nsresult rv;
00256 
00257   nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
00258   if (NS_SUCCEEDED(rv))
00259     rv = prefBranch->GetIntPref(aPrefName, _retval);
00260   return rv;
00261 }
00262 
00263 NS_IMETHODIMP nsPref::SetIntPref(const char *aPrefName, PRInt32 aValue)
00264 {
00265   nsresult rv;
00266 
00267   nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
00268   if (NS_SUCCEEDED(rv))
00269     rv = prefBranch->SetIntPref(aPrefName, aValue);
00270   return rv;
00271 }
00272 
00273 NS_IMETHODIMP nsPref::GetComplexValue(const char *aPrefName, const nsIID & aType, void * *aValue)
00274 {
00275   nsresult rv;
00276 
00277   nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
00278   if (NS_SUCCEEDED(rv))
00279     rv = prefBranch->GetComplexValue(aPrefName, aType, aValue);
00280   return rv;
00281 }
00282 
00283 NS_IMETHODIMP nsPref::SetComplexValue(const char *aPrefName, const nsIID & aType, nsISupports *aValue)
00284 {
00285   nsresult rv;
00286 
00287   nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
00288   if (NS_SUCCEEDED(rv))
00289     rv = prefBranch->SetComplexValue(aPrefName, aType, aValue);
00290   return rv;
00291 }
00292 
00293 NS_IMETHODIMP nsPref::ClearUserPref(const char *aPrefName)
00294 {
00295   nsresult rv;
00296 
00297   nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
00298   if (NS_SUCCEEDED(rv))
00299     rv = prefBranch->ClearUserPref(aPrefName);
00300   return rv;
00301 }
00302 
00303 NS_IMETHODIMP nsPref::LockPref(const char *aPrefName)
00304 {
00305   nsresult rv;
00306 
00307   nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
00308   if (NS_SUCCEEDED(rv))
00309     rv = prefBranch->LockPref(aPrefName);
00310   return rv;
00311 }
00312 
00313 NS_IMETHODIMP nsPref::PrefIsLocked(const char *aPrefName, PRBool *_retval)
00314 {
00315   nsresult rv;
00316 
00317   nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
00318   if (NS_SUCCEEDED(rv))
00319     rv = prefBranch->PrefIsLocked(aPrefName, _retval);
00320   return rv;
00321 }
00322 
00323 NS_IMETHODIMP nsPref::PrefHasUserValue(const char *aPrefName, PRBool *_retval)
00324 {
00325   nsresult rv;
00326 
00327   nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
00328   if (NS_SUCCEEDED(rv))
00329     rv = prefBranch->PrefHasUserValue(aPrefName, _retval);
00330   return rv;
00331 }
00332 
00333 NS_IMETHODIMP nsPref::UnlockPref(const char *aPrefName)
00334 {
00335   nsresult rv;
00336 
00337   nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
00338   if (NS_SUCCEEDED(rv))
00339     rv = prefBranch->UnlockPref(aPrefName);
00340   return rv;
00341 }
00342 
00343 NS_IMETHODIMP nsPref::ResetBranch(const char *aStartingAt)
00344 {
00345   nsresult rv;
00346 
00347   nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
00348   if (NS_SUCCEEDED(rv))
00349     rv = prefBranch->ResetBranch(aStartingAt);
00350   return rv;
00351 }
00352 
00353 NS_IMETHODIMP nsPref::DeleteBranch(const char *aStartingAt)
00354 {
00355   nsresult rv;
00356 
00357   nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
00358   if (NS_SUCCEEDED(rv))
00359     rv = prefBranch->DeleteBranch(aStartingAt);
00360   return rv;
00361 }
00362 
00363 NS_IMETHODIMP nsPref::GetChildList(const char *aStartingAt, PRUint32 *aCount, char ***aChildArray)
00364 {
00365   nsresult rv;
00366 
00367   nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
00368   if (NS_SUCCEEDED(rv))
00369     rv = prefBranch->GetChildList(aStartingAt, aCount, aChildArray);
00370   return rv;
00371 }
00372 
00373 NS_IMETHODIMP nsPref::AddObserver(const char *aDomain, nsIObserver *aObserver, PRBool aHoldWeak)
00374 {
00375   nsresult rv;
00376 
00377   nsCOMPtr<nsIPrefBranch2> prefBranch = do_QueryInterface(mPrefService, &rv);
00378   if (NS_SUCCEEDED(rv))
00379     rv = prefBranch->AddObserver(aDomain, aObserver, aHoldWeak);
00380   return rv;
00381 }
00382 
00383 NS_IMETHODIMP nsPref::RemoveObserver(const char *aDomain, nsIObserver *aObserver)
00384 {
00385   nsresult rv;
00386 
00387   nsCOMPtr<nsIPrefBranch2> prefBranch = do_QueryInterface(mPrefService, &rv);
00388   if (NS_SUCCEEDED(rv))
00389     rv = prefBranch->RemoveObserver(aDomain, aObserver);
00390   return rv;
00391 }
00392 
00393 NS_IMETHODIMP nsPref::Observe(nsISupports *aSubject, const char *aTopic, const PRUnichar *someData)
00394 {
00395   nsresult rv;
00396 
00397   nsCOMPtr<nsIObserver> observer = do_QueryInterface(mPrefService, &rv);
00398   if (NS_SUCCEEDED(rv))
00399     rv = observer->Observe(aSubject, aTopic, someData);
00400   return rv;
00401 }
00402 
00403 
00404 /*
00405  * Some temporary support for deprecated functions.
00406  */
00407 
00408 /*
00409  * Items replaced by Get/SetComplexValue
00410  */
00411 
00412 NS_IMETHODIMP nsPref::CopyUnicharPref(const char *pref, PRUnichar **_retval)
00413 {
00414   nsresult rv;
00415 
00416   nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
00417   if (NS_SUCCEEDED(rv)) {
00418     nsCOMPtr<nsISupportsString> theString;
00419     rv = prefBranch->GetComplexValue(pref, NS_GET_IID(nsISupportsString),
00420                                      getter_AddRefs(theString));
00421     if (NS_FAILED(rv))
00422       return rv;
00423 
00424     return theString->ToString(_retval);
00425   }
00426   return rv;
00427 }
00428 
00429 NS_IMETHODIMP nsPref::CopyDefaultUnicharPref(const char *pref, PRUnichar **_retval)
00430 {
00431   nsresult rv;
00432   nsCOMPtr<nsISupportsString> theString;
00433 
00434   rv = mDefaultBranch->GetComplexValue(pref, NS_GET_IID(nsISupportsString),
00435                                        getter_AddRefs(theString));
00436   if (NS_FAILED(rv))
00437     return rv;
00438 
00439   return theString->ToString(_retval);
00440 }
00441 
00442 NS_IMETHODIMP nsPref::SetUnicharPref(const char *pref, const PRUnichar *value)
00443 {
00444   nsresult rv;
00445 
00446   nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
00447   if (NS_SUCCEEDED(rv)) {
00448     nsCOMPtr<nsISupportsString> theString = do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID, &rv);
00449     if (NS_SUCCEEDED(rv)) {
00450       theString->SetData(nsDependentString(value));
00451       rv = prefBranch->SetComplexValue(pref, NS_GET_IID(nsISupportsString), theString);
00452     }
00453   }
00454   return rv;
00455 }
00456 
00457 NS_IMETHODIMP nsPref::SetDefaultUnicharPref(const char *pref, const PRUnichar *value)
00458 {
00459   nsresult rv;
00460 
00461   nsCOMPtr<nsISupportsString> theString = do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID, &rv);
00462   if (NS_SUCCEEDED(rv)) {
00463     theString->SetData(nsDependentString(value));
00464     rv = mDefaultBranch->SetComplexValue(pref, NS_GET_IID(nsISupportsString), theString);
00465   }
00466   return rv;
00467 }
00468 
00469 NS_IMETHODIMP nsPref::GetLocalizedUnicharPref(const char *pref, PRUnichar **_retval)
00470 {
00471   nsresult rv;
00472 
00473   nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
00474   if (NS_SUCCEEDED(rv)) {
00475     nsCOMPtr<nsIPrefLocalizedString> theString;
00476     rv = prefBranch->GetComplexValue(pref, NS_GET_IID(nsIPrefLocalizedString),
00477                                        getter_AddRefs(theString));
00478     if (NS_SUCCEEDED(rv)) {
00479       rv = theString->ToString(_retval);
00480     }
00481   }
00482   return rv;
00483 }
00484 
00485 NS_IMETHODIMP nsPref::GetDefaultLocalizedUnicharPref(const char *pref, PRUnichar **_retval)
00486 {
00487   nsresult rv;
00488   nsCOMPtr<nsIPrefLocalizedString> theString;
00489 
00490   rv = mDefaultBranch->GetComplexValue(pref, NS_GET_IID(nsIPrefLocalizedString),
00491                                        getter_AddRefs(theString));
00492   if (NS_SUCCEEDED(rv)) {
00493     rv = theString->ToString(_retval);
00494   }
00495   
00496   return rv;
00497 }
00498 
00499 NS_IMETHODIMP nsPref::GetFilePref(const char *pref, nsIFileSpec **_retval)
00500 {
00501 #ifdef MOZ_NO_XPCOM_OBSOLETE
00502   return NS_ERROR_NOT_IMPLEMENTED;
00503 #else
00504   nsresult rv;
00505 
00506   nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
00507   if (NS_SUCCEEDED(rv))
00508     rv = prefBranch->GetComplexValue(pref, NS_GET_IID(nsIFileSpec), (void **)_retval);
00509   return rv;
00510 #endif
00511 }
00512 
00513 NS_IMETHODIMP nsPref::SetFilePref(const char *pref, nsIFileSpec *value, PRBool setDefault)
00514 {
00515 #ifdef MOZ_NO_XPCOM_OBSOLETE
00516   return NS_ERROR_NOT_IMPLEMENTED;
00517 #else
00518   nsresult  rv;
00519 
00520   if (setDefault) {
00521     rv = mDefaultBranch->SetComplexValue(pref, NS_GET_IID(nsIFileSpec), value);
00522   } else {
00523     nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
00524     if (NS_SUCCEEDED(rv))
00525       rv = prefBranch->SetComplexValue(pref, NS_GET_IID(nsIFileSpec), value);
00526   }
00527     return rv;
00528 #endif
00529 }
00530 
00531 NS_IMETHODIMP nsPref::GetFileXPref(const char *pref, nsILocalFile **_retval)
00532 {
00533   nsresult rv;
00534 
00535   nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
00536   if (NS_SUCCEEDED(rv))
00537     rv = prefBranch->GetComplexValue(pref, NS_GET_IID(nsILocalFile), (void **)_retval);
00538   return rv;
00539 }
00540 
00541 NS_IMETHODIMP nsPref::SetFileXPref(const char *pref, nsILocalFile *value)
00542 {
00543   nsresult rv;
00544 
00545   NS_ENSURE_ARG_POINTER(value);
00546   nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
00547   if (NS_SUCCEEDED(rv))
00548     rv = prefBranch->SetComplexValue(pref, NS_GET_IID(nsILocalFile), value);
00549   return rv;
00550 }
00551 
00552 
00553 /*
00554  * Callbacks
00555  */
00556 
00557 //----------------------------------------------------------------------------------------
00558 NS_IMETHODIMP nsPref::RegisterCallback( const char* domain,
00559                     PrefChangedFunc callback, 
00560                     void* instance_data )
00561 //----------------------------------------------------------------------------------------
00562 {
00563     PREF_RegisterCallback(domain, callback, instance_data);
00564     return NS_OK;
00565 }
00566 
00567 //----------------------------------------------------------------------------------------
00568 NS_IMETHODIMP nsPref::UnregisterCallback( const char* domain,
00569                         PrefChangedFunc callback, 
00570                         void* instance_data )
00571 //----------------------------------------------------------------------------------------
00572 {
00573   return PREF_UnregisterCallback(domain, callback, instance_data);
00574 }
00575 
00576 /*
00577  * Preference enumeration
00578  */
00579 
00580 NS_IMETHODIMP nsPref::EnumerateChildren(const char *parent, PrefEnumerationFunc callback, void *arg) 
00581 {
00582   PRUint32 theCount;
00583   PRUint32 i;
00584   nsresult rv;
00585   char     **childArray;
00586   char     *prefName;
00587 
00588   rv = GetChildList(parent, &theCount, &childArray);
00589   if (NS_SUCCEEDED(rv)) {
00590     // now that we've built up the list, run the callback on all the matching elements
00591     for (i = 0; i < theCount; i++) {
00592       prefName = (char *)childArray[i];
00593       (*callback)((char*)prefName, arg);
00594     }
00595 
00596     // now release all the memory
00597     NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(theCount, childArray);
00598   }
00599 
00600   return NS_OK;
00601 }
00602 
00603 /*
00604  * Pref access without security check - these are here
00605  * to support nsScriptSecurityManager.
00606  * These functions are part of nsISecurityPref, not nsIPref.
00607  * **PLEASE** do not call these functions from elsewhere
00608  */
00609 NS_IMETHODIMP nsPref::SecurityGetBoolPref(const char *pref, PRBool * return_val)
00610 {
00611   nsresult rv;
00612 
00613   nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
00614   if (NS_FAILED(rv))
00615     return rv;
00616 
00617   nsCOMPtr<nsISecurityPref> securityPref = do_QueryInterface(prefBranch, &rv);
00618   if (NS_SUCCEEDED(rv))
00619     rv = securityPref->SecurityGetBoolPref(pref, return_val);
00620   return rv;
00621 }
00622 
00623 NS_IMETHODIMP nsPref::SecuritySetBoolPref(const char *pref, PRBool value)
00624 {
00625   nsresult rv;
00626 
00627   nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
00628   if (NS_FAILED(rv))
00629     return rv;
00630 
00631   nsCOMPtr<nsISecurityPref> securityPref = do_QueryInterface(prefBranch, &rv);
00632   if (NS_SUCCEEDED(rv))
00633     rv = securityPref->SecuritySetBoolPref(pref, value);
00634   return rv;
00635 }
00636 
00637 NS_IMETHODIMP nsPref::SecurityGetCharPref(const char *pref, char ** return_buf)
00638 {
00639   nsresult rv;
00640 
00641   nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
00642   if (NS_FAILED(rv))
00643     return rv;
00644 
00645   nsCOMPtr<nsISecurityPref> securityPref = do_QueryInterface(prefBranch, &rv);
00646   if (NS_SUCCEEDED(rv))
00647     rv = securityPref->SecurityGetCharPref(pref, return_buf);
00648   return rv;
00649 }
00650 
00651 NS_IMETHODIMP nsPref::SecuritySetCharPref(const char *pref, const char* value)
00652 {
00653   nsresult rv;
00654 
00655   nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
00656   if (NS_FAILED(rv))
00657     return rv;
00658 
00659   nsCOMPtr<nsISecurityPref> securityPref = do_QueryInterface(prefBranch, &rv);
00660   if (NS_SUCCEEDED(rv))
00661     rv = securityPref->SecuritySetCharPref(pref, value);
00662   return rv;
00663 }
00664 
00665 NS_IMETHODIMP nsPref::SecurityGetIntPref(const char *pref, PRInt32 * return_val)
00666 {
00667   nsresult rv;
00668 
00669   nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
00670   if (NS_FAILED(rv))
00671     return rv;
00672 
00673   nsCOMPtr<nsISecurityPref> securityPref = do_QueryInterface(prefBranch, &rv);
00674   if (NS_SUCCEEDED(rv))
00675     rv = securityPref->SecurityGetIntPref(pref, return_val);
00676   return rv;
00677 }
00678 
00679 NS_IMETHODIMP nsPref::SecuritySetIntPref(const char *pref, PRInt32 value)
00680 {
00681   nsresult rv;
00682 
00683   nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
00684   if (NS_FAILED(rv))
00685     return rv;
00686 
00687   nsCOMPtr<nsISecurityPref> securityPref = do_QueryInterface(prefBranch, &rv);
00688   if (NS_SUCCEEDED(rv))
00689     rv = securityPref->SecuritySetIntPref(pref, value);
00690   return rv;
00691 }
00692 
00693 NS_IMETHODIMP nsPref::SecurityClearUserPref(const char *pref_name)
00694 {
00695   nsresult rv;
00696 
00697   nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(mPrefService, &rv);
00698   if (NS_FAILED(rv))
00699     return rv;
00700 
00701   nsCOMPtr<nsISecurityPref> securityPref = do_QueryInterface(prefBranch, &rv);
00702   if (NS_SUCCEEDED(rv))
00703     rv = securityPref->SecurityClearUserPref(pref_name);
00704   return rv;
00705 }
00706 
00707 
00708 //----------------------------------------------------------------------------------------
00709 nsPref* nsPref::GetInstance()
00710 //----------------------------------------------------------------------------------------
00711 {
00712   if (!gInstance)
00713   {
00714     NS_NEWXPCOM(gInstance, nsPref);
00715   }
00716   return gInstance;
00717 } // nsPref::GetInstance
00718 
00719 
00720 //----------------------------------------------------------------------------------------
00721 // Functions used to create new instances of a given object by the
00722 // generic factory.
00723 
00724 
00726 // Hand implement the GenericFactory constructor macro so I can make it
00727 // non static. This is simply to keep us from having to make an nsPref.h
00728 // file because we are trying to remove this object, not add to it.
00729 //
00730 
00731 NS_IMETHODIMP nsPrefConstructor(nsISupports *aOuter, REFNSIID aIID, void **aResult)
00732 {
00733   nsresult rv;
00734     
00735   if (NULL == aResult) {
00736     rv = NS_ERROR_NULL_POINTER;
00737     return rv;
00738   }
00739   *aResult = NULL;
00740   if (NULL != aOuter) {
00741     rv = NS_ERROR_NO_AGGREGATION;
00742     return rv;
00743   }
00744 
00745   nsPref *inst = nsPref::GetInstance();
00746 
00747   if (NULL == inst) {
00748     rv = NS_ERROR_OUT_OF_MEMORY;
00749     return rv;
00750   }
00751   NS_ADDREF(inst);
00752   rv = inst->QueryInterface(aIID, aResult);
00753   NS_RELEASE(inst);
00754 
00755   return rv;
00756 }