Back to index

lightning-sunbird  0.9+nobinonly
winEmbedFileLocProvider.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: Mozilla-sample-code 1.0
00004  *
00005  * Copyright (c) 2002 Netscape Communications Corporation and
00006  * other contributors
00007  *
00008  * Permission is hereby granted, free of charge, to any person obtaining a
00009  * copy of this Mozilla sample software and associated documentation files
00010  * (the "Software"), to deal in the Software without restriction, including
00011  * without limitation the rights to use, copy, modify, merge, publish,
00012  * distribute, sublicense, and/or sell copies of the Software, and to permit
00013  * persons to whom the Software is furnished to do so, subject to the
00014  * following conditions:
00015  *
00016  * The above copyright notice and this permission notice shall be included
00017  * in all copies or substantial portions of the Software.
00018  *
00019  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00020  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00021  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
00022  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00023  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00024  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00025  * DEALINGS IN THE SOFTWARE.
00026  *
00027  * Contributor(s):
00028  *   Conrad Carlen <conrad@ingress.com>
00029  *   Benjamin Smedberg <bsmedberg@covad.net>
00030  *
00031  * ***** END LICENSE BLOCK ***** */
00032  
00033 #include "winEmbedFileLocProvider.h"
00034 #include "nsXPCOM.h"
00035 #include "nsXPCOMGlue.h"
00036 #include "nsAppDirectoryServiceDefs.h"
00037 #include "nsDirectoryServiceDefs.h"
00038 #include "nsILocalFile.h"
00039 #include "nsIProperties.h"
00040 #include "nsServiceManagerUtils.h"
00041 
00042 #include <windows.h>
00043 #include <shlobj.h>
00044 
00045  
00046 // WARNING: These hard coded names need to go away. They need to
00047 // come from localizable resources
00048 #define APP_REGISTRY_NAME nsEmbedCString("registry.dat")
00049 
00050 #define PROFILE_ROOT_DIR_NAME       nsEmbedCString("Profiles")
00051 #define DEFAULTS_DIR_NAME           nsEmbedCString("defaults")
00052 #define DEFAULTS_PREF_DIR_NAME      nsEmbedCString("pref")
00053 #define DEFAULTS_PROFILE_DIR_NAME   nsEmbedCString("profile")
00054 #define RES_DIR_NAME                nsEmbedCString("res")
00055 #define CHROME_DIR_NAME             nsEmbedCString("chrome")
00056 #define PLUGINS_DIR_NAME            nsEmbedCString("plugins")
00057 #define SEARCH_DIR_NAME             nsEmbedCString("searchplugins")
00058 #define COMPONENTS_DIR_NAME         nsEmbedCString("components")
00059 
00060 //*****************************************************************************
00061 // winEmbedFileLocProvider::Constructor/Destructor
00062 //*****************************************************************************   
00063 
00064 winEmbedFileLocProvider::winEmbedFileLocProvider(const nsACString& aAppDataDirName)
00065 {
00066     mProductDirName = aAppDataDirName;
00067 }
00068 
00069 winEmbedFileLocProvider::~winEmbedFileLocProvider()
00070 {
00071 }
00072 
00073 
00074 //*****************************************************************************
00075 // winEmbedFileLocProvider::nsISupports
00076 //*****************************************************************************   
00077 
00078 NS_IMPL_ISUPPORTS1(winEmbedFileLocProvider, nsIDirectoryServiceProvider)
00079 
00080 //*****************************************************************************
00081 // winEmbedFileLocProvider::nsIDirectoryServiceProvider
00082 //*****************************************************************************   
00083 
00084 NS_IMETHODIMP
00085 winEmbedFileLocProvider::GetFile(const char *prop, PRBool *persistant, nsIFile **_retval)
00086 {    
00087     nsCOMPtr<nsILocalFile>  localFile;
00088     nsresult rv = NS_ERROR_FAILURE;
00089 
00090     *_retval = nsnull;
00091     *persistant = PR_TRUE;
00092     
00093     if (strcmp(prop, NS_APP_APPLICATION_REGISTRY_DIR) == 0)
00094     {
00095         rv = GetProductDirectory(getter_AddRefs(localFile));
00096     }
00097     else if (strcmp(prop, NS_APP_APPLICATION_REGISTRY_FILE) == 0)
00098     {
00099         rv = GetProductDirectory(getter_AddRefs(localFile));
00100         if (NS_SUCCEEDED(rv))
00101             rv = localFile->AppendNative(APP_REGISTRY_NAME);
00102     }
00103     else if (strcmp(prop, NS_APP_DEFAULTS_50_DIR) == 0)
00104     {
00105         rv = CloneMozBinDirectory(getter_AddRefs(localFile));
00106         if (NS_SUCCEEDED(rv))
00107             rv = localFile->AppendRelativeNativePath(DEFAULTS_DIR_NAME);
00108     }
00109     else if (strcmp(prop, NS_APP_PREF_DEFAULTS_50_DIR) == 0)
00110     {
00111         rv = CloneMozBinDirectory(getter_AddRefs(localFile));
00112         if (NS_SUCCEEDED(rv)) {
00113             rv = localFile->AppendRelativeNativePath(DEFAULTS_DIR_NAME);
00114             if (NS_SUCCEEDED(rv))
00115                 rv = localFile->AppendRelativeNativePath(DEFAULTS_PREF_DIR_NAME);
00116         }
00117     }
00118     else if (strcmp(prop, NS_APP_PROFILE_DEFAULTS_NLOC_50_DIR) == 0 ||
00119              strcmp(prop, NS_APP_PROFILE_DEFAULTS_50_DIR) == 0)
00120     {
00121         rv = CloneMozBinDirectory(getter_AddRefs(localFile));
00122         if (NS_SUCCEEDED(rv)) {
00123             rv = localFile->AppendRelativeNativePath(DEFAULTS_DIR_NAME);
00124             if (NS_SUCCEEDED(rv))
00125                 rv = localFile->AppendRelativeNativePath(DEFAULTS_PROFILE_DIR_NAME);
00126         }
00127     }
00128     else if (strcmp(prop, NS_APP_USER_PROFILES_ROOT_DIR) == 0)
00129     {
00130         rv = GetDefaultUserProfileRoot(getter_AddRefs(localFile));   
00131     }
00132     else if (strcmp(prop, NS_APP_RES_DIR) == 0)
00133     {
00134         rv = CloneMozBinDirectory(getter_AddRefs(localFile));
00135         if (NS_SUCCEEDED(rv))
00136             rv = localFile->AppendRelativeNativePath(RES_DIR_NAME);
00137     }
00138     else if (strcmp(prop, NS_APP_CHROME_DIR) == 0)
00139     {
00140         rv = CloneMozBinDirectory(getter_AddRefs(localFile));
00141         if (NS_SUCCEEDED(rv))
00142             rv = localFile->AppendRelativeNativePath(CHROME_DIR_NAME);
00143     }
00144     else if (strcmp(prop, NS_APP_PLUGINS_DIR) == 0)
00145     {
00146         rv = CloneMozBinDirectory(getter_AddRefs(localFile));
00147         if (NS_SUCCEEDED(rv))
00148             rv = localFile->AppendRelativeNativePath(PLUGINS_DIR_NAME);
00149     }
00150     else if (strcmp(prop, NS_APP_SEARCH_DIR) == 0)
00151     {
00152         rv = CloneMozBinDirectory(getter_AddRefs(localFile));
00153         if (NS_SUCCEEDED(rv))
00154             rv = localFile->AppendRelativeNativePath(SEARCH_DIR_NAME);
00155     }
00156 
00157 #ifdef XPCOM_GLUE
00158     //---------------------------------------------------------------
00159     // Note that by returning a valid localFile's for NS_GRE_DIR and
00160     // NS_GRE_COMPONENT_DIR your app is indicating to XPCOM that 
00161     // it found an GRE version with which it's compatible with and 
00162     // it intends to be "run against" that GRE
00163     //
00164     // Please see http://www.mozilla.org/projects/embedding/GRE.html
00165     // for more info. on GRE
00166     //---------------------------------------------------------------
00167     else if (strcmp(prop, NS_GRE_DIR) == 0)
00168     {
00169         rv = GRE_GetGREDirectory(getter_AddRefs(localFile));
00170     }
00171 #endif
00172 
00173     if (localFile && NS_SUCCEEDED(rv))
00174         return localFile->QueryInterface(NS_GET_IID(nsIFile), (void**)_retval);
00175         
00176     return rv;
00177 }
00178 
00179 NS_METHOD winEmbedFileLocProvider::CloneMozBinDirectory(nsILocalFile **aLocalFile)
00180 {
00181     NS_ENSURE_ARG_POINTER(aLocalFile);
00182     nsresult rv;
00183     
00184     if (!mMozBinDirectory)
00185     {        
00186         // Get the mozilla bin directory
00187         // 1. Check the directory service first for NS_XPCOM_CURRENT_PROCESS_DIR
00188         //    This will be set if a directory was passed to NS_InitXPCOM
00189         // 2. If that doesn't work, set it to be the current process directory
00190         
00191         nsCOMPtr<nsIProperties> directoryService = 
00192                  do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID, &rv);
00193         if (NS_FAILED(rv))
00194             return rv;
00195         
00196         rv = directoryService->Get(NS_XPCOM_CURRENT_PROCESS_DIR, NS_GET_IID(nsIFile), getter_AddRefs(mMozBinDirectory));
00197         if (NS_FAILED(rv)) {
00198             rv = directoryService->Get(NS_OS_CURRENT_PROCESS_DIR, NS_GET_IID(nsIFile), getter_AddRefs(mMozBinDirectory));
00199             if (NS_FAILED(rv))
00200                 return rv;
00201         }
00202     }
00203     
00204     nsCOMPtr<nsIFile> aFile;
00205     rv = mMozBinDirectory->Clone(getter_AddRefs(aFile));
00206     if (NS_FAILED(rv))
00207         return rv;
00208 
00209     nsCOMPtr<nsILocalFile> lfile = do_QueryInterface (aFile);
00210     if (!lfile)
00211         return NS_ERROR_FAILURE;
00212     
00213     NS_IF_ADDREF(*aLocalFile = lfile);
00214     return NS_OK;
00215 }
00216 
00217 
00218 //----------------------------------------------------------------------------------------
00219 // GetProductDirectory - Gets the directory which contains the application data folder
00220 //
00221 // WIN    : <Application Data folder on user's machine>\Mozilla 
00222 //----------------------------------------------------------------------------------------
00223 NS_METHOD winEmbedFileLocProvider::GetProductDirectory(nsILocalFile **aLocalFile)
00224 {
00225     NS_ENSURE_ARG_POINTER(aLocalFile);
00226     
00227     nsresult rv;
00228     PRBool exists;
00229     nsCOMPtr<nsILocalFile> localDir;
00230    
00231     nsCOMPtr<nsIProperties> directoryService = 
00232              do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID, &rv);
00233     if (NS_FAILED(rv)) return rv;
00234     rv = directoryService->Get(NS_WIN_APPDATA_DIR, NS_GET_IID(nsILocalFile), getter_AddRefs(localDir));
00235     if (NS_SUCCEEDED(rv))
00236         rv = localDir->Exists(&exists);
00237     if (NS_FAILED(rv) || !exists)
00238     {
00239         // On some Win95 machines, NS_WIN_APPDATA_DIR does not exist - revert to NS_WIN_WINDOWS_DIR
00240         localDir = nsnull;
00241         rv = directoryService->Get(NS_WIN_WINDOWS_DIR, NS_GET_IID(nsILocalFile), getter_AddRefs(localDir));
00242     }
00243     if (NS_FAILED(rv)) return rv;
00244 
00245     rv = localDir->AppendNative(mProductDirName);
00246     if (NS_FAILED(rv)) return rv;
00247     rv = localDir->Exists(&exists);
00248     if (NS_SUCCEEDED(rv) && !exists)
00249         rv = localDir->Create(nsIFile::DIRECTORY_TYPE, 0775);
00250     if (NS_FAILED(rv)) return rv;
00251 
00252     *aLocalFile = localDir;
00253     NS_ADDREF(*aLocalFile);
00254 
00255    return rv; 
00256 }
00257 
00258 
00259 //----------------------------------------------------------------------------------------
00260 // GetDefaultUserProfileRoot - Gets the directory which contains each user profile dir
00261 //
00262 // WIN    : <Application Data folder on user's machine>\Mozilla\Users50 
00263 //----------------------------------------------------------------------------------------
00264 NS_METHOD winEmbedFileLocProvider::GetDefaultUserProfileRoot(nsILocalFile **aLocalFile)
00265 {
00266     NS_ENSURE_ARG_POINTER(aLocalFile);
00267     
00268     nsresult rv;
00269     PRBool exists;
00270     nsCOMPtr<nsILocalFile> localDir;
00271    
00272     rv = GetProductDirectory(getter_AddRefs(localDir));
00273     if (NS_FAILED(rv)) return rv;
00274 
00275     // These 3 platforms share this part of the path - do them as one
00276     rv = localDir->AppendRelativeNativePath(PROFILE_ROOT_DIR_NAME);
00277     if (NS_FAILED(rv)) return rv;
00278     rv = localDir->Exists(&exists);
00279     if (NS_SUCCEEDED(rv) && !exists)
00280         rv = localDir->Create(nsIFile::DIRECTORY_TYPE, 0775);
00281     if (NS_FAILED(rv)) return rv;
00282 
00283     *aLocalFile = localDir;
00284     NS_ADDREF(*aLocalFile);
00285 
00286    return rv; 
00287 }
00288