Back to index

lightning-sunbird  0.9+nobinonly
CAppFileLocationProvider.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla Communicator client code, released
00016  * March 31, 1998.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 2000
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 #include "CAppFileLocationProvider.h"
00041 #include "nsAppDirectoryServiceDefs.h"
00042 #include "nsDirectoryServiceDefs.h"
00043 #include "nsIAtom.h"
00044 #include "nsILocalFile.h"
00045 #include "nsString.h"
00046 #include "nsXPIDLString.h"
00047 #include "nsCRT.h"
00048 
00049 #include <Folders.h>
00050 #include <Script.h>
00051 #include <Processes.h>
00052 #include "nsILocalFileMac.h"
00053 
00054 #include "ApplIDs.h"  
00055 
00056 //*****************************************************************************
00057 // CAppFileLocationProvider::Constructor/Destructor
00058 //*****************************************************************************   
00059 
00060 CAppFileLocationProvider::CAppFileLocationProvider(const nsAString& aAppDataDirName) :
00061     mAppDataDirName(aAppDataDirName)
00062 {
00063 }
00064 
00065 CAppFileLocationProvider::~CAppFileLocationProvider()
00066 {
00067 }
00068 
00069 
00070 //*****************************************************************************
00071 // CAppFileLocationProvider::nsISupports
00072 //*****************************************************************************   
00073 
00074 NS_IMPL_ISUPPORTS1(CAppFileLocationProvider, nsIDirectoryServiceProvider)
00075 
00076 //*****************************************************************************
00077 // CAppFileLocationProvider::nsIDirectoryServiceProvider
00078 //*****************************************************************************   
00079 
00080 NS_IMETHODIMP
00081 CAppFileLocationProvider::GetFile(const char *prop, PRBool *persistant, nsIFile **_retval)
00082 {    
00083     nsCOMPtr<nsILocalFile>  localFile;
00084     nsresult rv = NS_ERROR_FAILURE;
00085 
00086     *_retval = nsnull;
00087     *persistant = PR_TRUE;
00088        
00089     if (strcmp(prop, NS_APP_APPLICATION_REGISTRY_DIR) == 0)
00090     {
00091         rv = GetAppDataDirectory(getter_AddRefs(localFile));
00092     }
00093     else if (strcmp(prop, NS_APP_APPLICATION_REGISTRY_FILE) == 0)
00094     {
00095         rv = GetAppDataDirectory(getter_AddRefs(localFile));
00096         if (NS_SUCCEEDED(rv))
00097             rv = localFile->AppendNative(nsDependentCString("Application Registry"));
00098     }
00099     else if (strcmp(prop, NS_APP_USER_PROFILES_ROOT_DIR) == 0)
00100     {
00101         rv = GetAppDataDirectory(getter_AddRefs(localFile));
00102         if (NS_FAILED(rv))
00103           return rv;
00104         rv = localFile->AppendNative(nsDependentCString("Profiles"));
00105         if (NS_FAILED(rv))
00106           return rv;
00107           
00108         PRBool exists;
00109         rv = localFile->Exists(&exists);
00110         if (NS_SUCCEEDED(rv) && !exists)
00111           rv = localFile->Create(nsIFile::DIRECTORY_TYPE, 0775);
00112         if (NS_FAILED(rv))
00113           return rv;
00114     }
00115     
00116     if (localFile && NS_SUCCEEDED(rv))
00117       rv = localFile->QueryInterface(NS_GET_IID(nsIFile), (void**)_retval);
00118               
00119     return rv;
00120 }
00121 
00122 //----------------------------------------------------------------------------------------
00123 // GetAppDataDirectory - Gets the directory which contains the application data folder
00124 //----------------------------------------------------------------------------------------
00125 NS_METHOD CAppFileLocationProvider::GetAppDataDirectory(nsILocalFile **aLocalFile)
00126 {
00127     NS_ENSURE_ARG_POINTER(aLocalFile);
00128     NS_ENSURE_TRUE(!mAppDataDirName.IsEmpty(), NS_ERROR_NOT_INITIALIZED);
00129     
00130     nsresult rv;
00131     PRBool exists;
00132     nsCOMPtr<nsILocalFile> localDir;
00133 
00134     nsCOMPtr<nsIProperties> directoryService = 
00135              do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID, &rv);
00136     if (NS_FAILED(rv)) return rv;
00137 
00138     OSErr   err;
00139  
00140 #if defined(XP_MACOSX)
00141     FSRef fsRef;
00142     err = ::FSFindFolder(kUserDomain, kApplicationSupportFolderType, kCreateFolder, &fsRef);
00143     if (err) return NS_ERROR_FAILURE;
00144     NS_NewLocalFile(EmptyString(), PR_TRUE, getter_AddRefs(localDir));
00145     if (!localDir) return NS_ERROR_FAILURE;
00146     nsCOMPtr<nsILocalFileMac> localDirMac(do_QueryInterface(localDir));
00147     rv = localDirMac->InitWithFSRef(&fsRef);
00148     if (NS_FAILED(rv)) return rv;
00149 #else   
00150     long    response;
00151     err = ::Gestalt(gestaltSystemVersion, &response);
00152     const char *prop = (!err && response >= 0x00001000) ? NS_MAC_USER_LIB_DIR : NS_MAC_DOCUMENTS_DIR;
00153     rv = directoryService->Get(prop, NS_GET_IID(nsILocalFile), getter_AddRefs(localDir));
00154     if (NS_FAILED(rv)) return rv;   
00155 #endif
00156 
00157     rv = localDir->Append(mAppDataDirName);
00158     if (NS_FAILED(rv))
00159         return rv;
00160     rv = localDir->Exists(&exists);
00161     if (NS_SUCCEEDED(rv) && !exists)
00162         rv = localDir->Create(nsIFile::DIRECTORY_TYPE, 0775);
00163     if (NS_FAILED(rv))
00164         return rv;
00165 
00166     *aLocalFile = localDir;
00167     NS_ADDREF(*aLocalFile);
00168 
00169     return rv; 
00170 }