michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * vim: sw=4 ts=4 sts=4 et michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "mozilla/ArrayUtils.h" michael@0: michael@0: #include "nsCOMPtr.h" michael@0: #include "nsAutoPtr.h" michael@0: #include "nsDirectoryService.h" michael@0: #include "nsDirectoryServiceDefs.h" michael@0: #include "nsLocalFile.h" michael@0: #include "nsDebug.h" michael@0: #include "nsStaticAtom.h" michael@0: #include "nsEnumeratorUtils.h" michael@0: michael@0: #include "nsICategoryManager.h" michael@0: #include "nsISimpleEnumerator.h" michael@0: #include "nsIStringEnumerator.h" michael@0: michael@0: #if defined(XP_WIN) michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #elif defined(XP_UNIX) michael@0: #include michael@0: #include michael@0: #include michael@0: #include "prenv.h" michael@0: #ifdef MOZ_WIDGET_COCOA michael@0: #include michael@0: #include michael@0: #endif michael@0: #endif michael@0: michael@0: #include "SpecialSystemDirectory.h" michael@0: #include "nsAppFileLocationProvider.h" michael@0: michael@0: using namespace mozilla; michael@0: michael@0: // define home directory michael@0: // For Windows platform, We are choosing Appdata folder as HOME michael@0: #if defined (XP_WIN) michael@0: #define HOME_DIR NS_WIN_APPDATA_DIR michael@0: #elif defined (MOZ_WIDGET_COCOA) michael@0: #define HOME_DIR NS_OSX_HOME_DIR michael@0: #elif defined (XP_UNIX) michael@0: #define HOME_DIR NS_UNIX_HOME_DIR michael@0: #endif michael@0: michael@0: //---------------------------------------------------------------------------------------- michael@0: nsresult michael@0: nsDirectoryService::GetCurrentProcessDirectory(nsIFile** aFile) michael@0: //---------------------------------------------------------------------------------------- michael@0: { michael@0: if (NS_WARN_IF(!aFile)) michael@0: return NS_ERROR_INVALID_ARG; michael@0: *aFile = nullptr; michael@0: michael@0: // Set the component registry location: michael@0: if (!gService) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: nsresult rv; michael@0: michael@0: nsCOMPtr dirService; michael@0: rv = nsDirectoryService::Create(nullptr, michael@0: NS_GET_IID(nsIProperties), michael@0: getter_AddRefs(dirService)); // needs to be around for life of product michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: michael@0: if (dirService) michael@0: { michael@0: nsCOMPtr aLocalFile; michael@0: dirService->Get(NS_XPCOM_INIT_CURRENT_PROCESS_DIR, NS_GET_IID(nsIFile), getter_AddRefs(aLocalFile)); michael@0: if (aLocalFile) michael@0: { michael@0: *aFile = aLocalFile; michael@0: NS_ADDREF(*aFile); michael@0: return NS_OK; michael@0: } michael@0: } michael@0: michael@0: nsLocalFile* localFile = new nsLocalFile; michael@0: michael@0: if (localFile == nullptr) michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: NS_ADDREF(localFile); michael@0: michael@0: michael@0: michael@0: #ifdef XP_WIN michael@0: wchar_t buf[MAX_PATH + 1]; michael@0: SetLastError(ERROR_SUCCESS); michael@0: if (GetModuleFileNameW(0, buf, mozilla::ArrayLength(buf)) && michael@0: GetLastError() != ERROR_INSUFFICIENT_BUFFER) { michael@0: // chop off the executable name by finding the rightmost backslash michael@0: wchar_t* lastSlash = wcsrchr(buf, L'\\'); michael@0: if (lastSlash) michael@0: *(lastSlash + 1) = L'\0'; michael@0: michael@0: localFile->InitWithPath(nsDependentString(buf)); michael@0: *aFile = localFile; michael@0: return NS_OK; michael@0: } michael@0: michael@0: #elif defined(MOZ_WIDGET_COCOA) michael@0: // Works even if we're not bundled. michael@0: CFBundleRef appBundle = CFBundleGetMainBundle(); michael@0: if (appBundle != nullptr) michael@0: { michael@0: CFURLRef bundleURL = CFBundleCopyExecutableURL(appBundle); michael@0: if (bundleURL != nullptr) michael@0: { michael@0: CFURLRef parentURL = CFURLCreateCopyDeletingLastPathComponent(kCFAllocatorDefault, bundleURL); michael@0: if (parentURL) michael@0: { michael@0: // Pass true for the "resolveAgainstBase" arg to CFURLGetFileSystemRepresentation. michael@0: // This will resolve the relative portion of the CFURL against it base, giving a full michael@0: // path, which CFURLCopyFileSystemPath doesn't do. michael@0: char buffer[PATH_MAX]; michael@0: if (CFURLGetFileSystemRepresentation(parentURL, true, (UInt8 *)buffer, sizeof(buffer))) michael@0: { michael@0: #ifdef DEBUG_conrad michael@0: printf("nsDirectoryService - CurrentProcessDir is: %s\n", buffer); michael@0: #endif michael@0: rv = localFile->InitWithNativePath(nsDependentCString(buffer)); michael@0: if (NS_SUCCEEDED(rv)) michael@0: *aFile = localFile; michael@0: } michael@0: CFRelease(parentURL); michael@0: } michael@0: CFRelease(bundleURL); michael@0: } michael@0: } michael@0: michael@0: NS_ASSERTION(*aFile, "nsDirectoryService - Could not determine CurrentProcessDir.\n"); michael@0: if (*aFile) michael@0: return NS_OK; michael@0: michael@0: #elif defined(XP_UNIX) michael@0: michael@0: // In the absence of a good way to get the executable directory let michael@0: // us try this for unix: michael@0: // - if MOZILLA_FIVE_HOME is defined, that is it michael@0: // - else give the current directory michael@0: char buf[MAXPATHLEN]; michael@0: michael@0: // The MOZ_DEFAULT_MOZILLA_FIVE_HOME variable can be set at configure time with michael@0: // a --with-default-mozilla-five-home=foo autoconf flag. michael@0: // michael@0: // The idea here is to allow for builds that have a default MOZILLA_FIVE_HOME michael@0: // regardless of the environment. This makes it easier to write apps that michael@0: // embed mozilla without having to worry about setting up the environment michael@0: // michael@0: // We do this by putenv()ing the default value into the environment. Note that michael@0: // we only do this if it is not already set. michael@0: #ifdef MOZ_DEFAULT_MOZILLA_FIVE_HOME michael@0: const char *home = PR_GetEnv("MOZILLA_FIVE_HOME"); michael@0: if (!home || !*home) michael@0: { michael@0: putenv("MOZILLA_FIVE_HOME=" MOZ_DEFAULT_MOZILLA_FIVE_HOME); michael@0: } michael@0: #endif michael@0: michael@0: char *moz5 = PR_GetEnv("MOZILLA_FIVE_HOME"); michael@0: if (moz5 && *moz5) michael@0: { michael@0: if (realpath(moz5, buf)) { michael@0: localFile->InitWithNativePath(nsDependentCString(buf)); michael@0: *aFile = localFile; michael@0: return NS_OK; michael@0: } michael@0: } michael@0: #if defined(DEBUG) michael@0: static bool firstWarning = true; michael@0: michael@0: if((!moz5 || !*moz5) && firstWarning) { michael@0: // Warn that MOZILLA_FIVE_HOME not set, once. michael@0: printf("Warning: MOZILLA_FIVE_HOME not set.\n"); michael@0: firstWarning = false; michael@0: } michael@0: #endif /* DEBUG */ michael@0: michael@0: // Fall back to current directory. michael@0: if (getcwd(buf, sizeof(buf))) michael@0: { michael@0: localFile->InitWithNativePath(nsDependentCString(buf)); michael@0: *aFile = localFile; michael@0: return NS_OK; michael@0: } michael@0: michael@0: #endif michael@0: michael@0: NS_RELEASE(localFile); michael@0: michael@0: NS_ERROR("unable to get current process directory"); michael@0: return NS_ERROR_FAILURE; michael@0: } // GetCurrentProcessDirectory() michael@0: michael@0: nsDirectoryService* nsDirectoryService::gService = nullptr; michael@0: michael@0: nsDirectoryService::nsDirectoryService() michael@0: : mHashtable(256) michael@0: { michael@0: } michael@0: michael@0: nsresult michael@0: nsDirectoryService::Create(nsISupports *outer, REFNSIID aIID, void **aResult) michael@0: { michael@0: if (NS_WARN_IF(!aResult)) michael@0: return NS_ERROR_INVALID_ARG; michael@0: if (NS_WARN_IF(outer)) michael@0: return NS_ERROR_NO_AGGREGATION; michael@0: michael@0: if (!gService) michael@0: { michael@0: return NS_ERROR_NOT_INITIALIZED; michael@0: } michael@0: michael@0: return gService->QueryInterface(aIID, aResult); michael@0: } michael@0: michael@0: #define DIR_ATOM(name_, value_) nsIAtom* nsDirectoryService::name_ = nullptr; michael@0: #include "nsDirectoryServiceAtomList.h" michael@0: #undef DIR_ATOM michael@0: michael@0: #define DIR_ATOM(name_, value_) NS_STATIC_ATOM_BUFFER(name_##_buffer, value_) michael@0: #include "nsDirectoryServiceAtomList.h" michael@0: #undef DIR_ATOM michael@0: michael@0: static const nsStaticAtom directory_atoms[] = { michael@0: #define DIR_ATOM(name_, value_) NS_STATIC_ATOM(name_##_buffer, &nsDirectoryService::name_), michael@0: #include "nsDirectoryServiceAtomList.h" michael@0: #undef DIR_ATOM michael@0: }; michael@0: michael@0: NS_IMETHODIMP michael@0: nsDirectoryService::Init() michael@0: { michael@0: NS_NOTREACHED("nsDirectoryService::Init() for internal use only!"); michael@0: return NS_OK; michael@0: } michael@0: michael@0: void michael@0: nsDirectoryService::RealInit() michael@0: { michael@0: NS_ASSERTION(!gService, michael@0: "nsDirectoryService::RealInit Mustn't initialize twice!"); michael@0: michael@0: nsRefPtr self = new nsDirectoryService(); michael@0: michael@0: NS_RegisterStaticAtoms(directory_atoms); michael@0: michael@0: // Let the list hold the only reference to the provider. michael@0: nsAppFileLocationProvider *defaultProvider = new nsAppFileLocationProvider; michael@0: self->mProviders.AppendElement(defaultProvider); michael@0: michael@0: self.swap(gService); michael@0: } michael@0: michael@0: nsDirectoryService::~nsDirectoryService() michael@0: { michael@0: } michael@0: michael@0: NS_IMPL_ISUPPORTS(nsDirectoryService, nsIProperties, nsIDirectoryService, nsIDirectoryServiceProvider, nsIDirectoryServiceProvider2) michael@0: michael@0: michael@0: NS_IMETHODIMP michael@0: nsDirectoryService::Undefine(const char* prop) michael@0: { michael@0: if (NS_WARN_IF(!prop)) michael@0: return NS_ERROR_INVALID_ARG; michael@0: michael@0: nsDependentCString key(prop); michael@0: if (!mHashtable.Get(key, nullptr)) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: mHashtable.Remove(key); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsDirectoryService::GetKeys(uint32_t *count, char ***keys) michael@0: { michael@0: return NS_ERROR_NOT_IMPLEMENTED; michael@0: } michael@0: michael@0: struct FileData michael@0: { michael@0: FileData(const char* aProperty, michael@0: const nsIID& aUUID) : michael@0: property(aProperty), michael@0: data(nullptr), michael@0: persistent(true), michael@0: uuid(aUUID) {} michael@0: michael@0: const char* property; michael@0: nsISupports* data; michael@0: bool persistent; michael@0: const nsIID& uuid; michael@0: }; michael@0: michael@0: static bool FindProviderFile(nsIDirectoryServiceProvider* aElement, michael@0: FileData* aData) michael@0: { michael@0: nsresult rv; michael@0: if (aData->uuid.Equals(NS_GET_IID(nsISimpleEnumerator))) { michael@0: // Not all providers implement this iface michael@0: nsCOMPtr prov2 = do_QueryInterface(aElement); michael@0: if (prov2) michael@0: { michael@0: nsCOMPtr newFiles; michael@0: rv = prov2->GetFiles(aData->property, getter_AddRefs(newFiles)); michael@0: if (NS_SUCCEEDED(rv) && newFiles) { michael@0: if (aData->data) { michael@0: nsCOMPtr unionFiles; michael@0: michael@0: NS_NewUnionEnumerator(getter_AddRefs(unionFiles), michael@0: (nsISimpleEnumerator*) aData->data, newFiles); michael@0: michael@0: if (unionFiles) michael@0: unionFiles.swap(* (nsISimpleEnumerator**) &aData->data); michael@0: } michael@0: else michael@0: { michael@0: NS_ADDREF(aData->data = newFiles); michael@0: } michael@0: michael@0: aData->persistent = false; // Enumerators can never be persistent michael@0: return rv == NS_SUCCESS_AGGREGATE_RESULT; michael@0: } michael@0: } michael@0: } michael@0: else michael@0: { michael@0: rv = aElement->GetFile(aData->property, &aData->persistent, michael@0: (nsIFile **)&aData->data); michael@0: if (NS_SUCCEEDED(rv) && aData->data) michael@0: return false; michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsDirectoryService::Get(const char* prop, const nsIID & uuid, void* *result) michael@0: { michael@0: if (NS_WARN_IF(!prop)) michael@0: return NS_ERROR_INVALID_ARG; michael@0: michael@0: nsDependentCString key(prop); michael@0: michael@0: nsCOMPtr cachedFile = mHashtable.Get(key); michael@0: michael@0: if (cachedFile) { michael@0: nsCOMPtr cloneFile; michael@0: cachedFile->Clone(getter_AddRefs(cloneFile)); michael@0: return cloneFile->QueryInterface(uuid, result); michael@0: } michael@0: michael@0: // it is not one of our defaults, lets check any providers michael@0: FileData fileData(prop, uuid); michael@0: michael@0: for (int32_t i = mProviders.Length() - 1; i >= 0; i--) { michael@0: if (!FindProviderFile(mProviders[i], &fileData)) { michael@0: break; michael@0: } michael@0: } michael@0: if (fileData.data) michael@0: { michael@0: if (fileData.persistent) michael@0: { michael@0: Set(prop, static_cast(fileData.data)); michael@0: } michael@0: nsresult rv = (fileData.data)->QueryInterface(uuid, result); michael@0: NS_RELEASE(fileData.data); // addref occurs in FindProviderFile() michael@0: return rv; michael@0: } michael@0: michael@0: FindProviderFile(static_cast(this), &fileData); michael@0: if (fileData.data) michael@0: { michael@0: if (fileData.persistent) michael@0: { michael@0: Set(prop, static_cast(fileData.data)); michael@0: } michael@0: nsresult rv = (fileData.data)->QueryInterface(uuid, result); michael@0: NS_RELEASE(fileData.data); // addref occurs in FindProviderFile() michael@0: return rv; michael@0: } michael@0: michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsDirectoryService::Set(const char* prop, nsISupports* value) michael@0: { michael@0: if (NS_WARN_IF(!prop)) michael@0: return NS_ERROR_INVALID_ARG; michael@0: michael@0: nsDependentCString key(prop); michael@0: if (mHashtable.Get(key, nullptr) || !value) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: nsCOMPtr ourFile = do_QueryInterface(value); michael@0: if (ourFile) { michael@0: nsCOMPtr cloneFile; michael@0: ourFile->Clone (getter_AddRefs (cloneFile)); michael@0: mHashtable.Put(key, cloneFile); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsDirectoryService::Has(const char *prop, bool *_retval) michael@0: { michael@0: if (NS_WARN_IF(!prop)) michael@0: return NS_ERROR_INVALID_ARG; michael@0: michael@0: *_retval = false; michael@0: nsCOMPtr value; michael@0: nsresult rv = Get(prop, NS_GET_IID(nsIFile), getter_AddRefs(value)); michael@0: if (NS_FAILED(rv)) michael@0: return NS_OK; michael@0: michael@0: if (value) michael@0: { michael@0: *_retval = true; michael@0: } michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsDirectoryService::RegisterProvider(nsIDirectoryServiceProvider *prov) michael@0: { michael@0: if (!prov) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: mProviders.AppendElement(prov); michael@0: return NS_OK; michael@0: } michael@0: michael@0: void michael@0: nsDirectoryService::RegisterCategoryProviders() michael@0: { michael@0: nsCOMPtr catman michael@0: (do_GetService(NS_CATEGORYMANAGER_CONTRACTID)); michael@0: if (!catman) michael@0: return; michael@0: michael@0: nsCOMPtr entries; michael@0: catman->EnumerateCategory(XPCOM_DIRECTORY_PROVIDER_CATEGORY, michael@0: getter_AddRefs(entries)); michael@0: michael@0: nsCOMPtr strings(do_QueryInterface(entries)); michael@0: if (!strings) michael@0: return; michael@0: michael@0: bool more; michael@0: while (NS_SUCCEEDED(strings->HasMore(&more)) && more) { michael@0: nsAutoCString entry; michael@0: strings->GetNext(entry); michael@0: michael@0: nsXPIDLCString contractID; michael@0: catman->GetCategoryEntry(XPCOM_DIRECTORY_PROVIDER_CATEGORY, entry.get(), getter_Copies(contractID)); michael@0: michael@0: if (contractID) { michael@0: nsCOMPtr provider = do_GetService(contractID.get()); michael@0: if (provider) michael@0: RegisterProvider(provider); michael@0: } michael@0: } michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsDirectoryService::UnregisterProvider(nsIDirectoryServiceProvider *prov) michael@0: { michael@0: if (!prov) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: mProviders.RemoveElement(prov); michael@0: return NS_OK; michael@0: } michael@0: michael@0: // DO NOT ADD ANY LOCATIONS TO THIS FUNCTION UNTIL YOU TALK TO: dougt@netscape.com. michael@0: // This is meant to be a place of xpcom or system specific file locations, not michael@0: // application specific locations. If you need the later, register a callback for michael@0: // your application. michael@0: michael@0: NS_IMETHODIMP michael@0: nsDirectoryService::GetFile(const char *prop, bool *persistent, nsIFile **_retval) michael@0: { michael@0: nsCOMPtr localFile; michael@0: nsresult rv = NS_ERROR_FAILURE; michael@0: michael@0: *_retval = nullptr; michael@0: *persistent = true; michael@0: michael@0: nsCOMPtr inAtom = do_GetAtom(prop); michael@0: michael@0: // check to see if it is one of our defaults michael@0: michael@0: if (inAtom == nsDirectoryService::sCurrentProcess || michael@0: inAtom == nsDirectoryService::sOS_CurrentProcessDirectory ) michael@0: { michael@0: rv = GetCurrentProcessDirectory(getter_AddRefs(localFile)); michael@0: } michael@0: michael@0: // Unless otherwise set, the core pieces of the GRE exist michael@0: // in the current process directory. michael@0: else if (inAtom == nsDirectoryService::sGRE_Directory) michael@0: { michael@0: rv = GetCurrentProcessDirectory(getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sOS_DriveDirectory) michael@0: { michael@0: rv = GetSpecialSystemDirectory(OS_DriveDirectory, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sOS_TemporaryDirectory) michael@0: { michael@0: rv = GetSpecialSystemDirectory(OS_TemporaryDirectory, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sOS_CurrentProcessDirectory) michael@0: { michael@0: rv = GetSpecialSystemDirectory(OS_CurrentProcessDirectory, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sOS_CurrentWorkingDirectory) michael@0: { michael@0: rv = GetSpecialSystemDirectory(OS_CurrentWorkingDirectory, getter_AddRefs(localFile)); michael@0: } michael@0: michael@0: #if defined(MOZ_WIDGET_COCOA) michael@0: else if (inAtom == nsDirectoryService::sDirectory) michael@0: { michael@0: rv = GetOSXFolderType(kClassicDomain, kSystemFolderType, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sTrashDirectory) michael@0: { michael@0: rv = GetOSXFolderType(kClassicDomain, kTrashFolderType, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sStartupDirectory) michael@0: { michael@0: rv = GetOSXFolderType(kClassicDomain, kStartupFolderType, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sShutdownDirectory) michael@0: { michael@0: rv = GetOSXFolderType(kClassicDomain, kShutdownFolderType, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sAppleMenuDirectory) michael@0: { michael@0: rv = GetOSXFolderType(kClassicDomain, kAppleMenuFolderType, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sControlPanelDirectory) michael@0: { michael@0: rv = GetOSXFolderType(kClassicDomain, kControlPanelFolderType, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sExtensionDirectory) michael@0: { michael@0: rv = GetOSXFolderType(kClassicDomain, kExtensionFolderType, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sFontsDirectory) michael@0: { michael@0: rv = GetOSXFolderType(kClassicDomain, kFontsFolderType, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sPreferencesDirectory) michael@0: { michael@0: rv = GetOSXFolderType(kClassicDomain, kPreferencesFolderType, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sDocumentsDirectory) michael@0: { michael@0: rv = GetOSXFolderType(kClassicDomain, kDocumentsFolderType, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sInternetSearchDirectory) michael@0: { michael@0: rv = GetOSXFolderType(kClassicDomain, kInternetSearchSitesFolderType, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sUserLibDirectory) michael@0: { michael@0: rv = GetOSXFolderType(kUserDomain, kDomainLibraryFolderType, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sOS_HomeDirectory) michael@0: { michael@0: rv = GetOSXFolderType(kUserDomain, kDomainTopLevelFolderType, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sDefaultDownloadDirectory) michael@0: { michael@0: // 10.5 and later, we can use kDownloadsFolderType which is defined in michael@0: // Folders.h as "down". However, in order to support 10.4 still, we michael@0: // cannot use the named constant. We'll use it's value, and if it michael@0: // fails, fall back to the desktop. michael@0: #ifndef kDownloadsFolderType michael@0: #define kDownloadsFolderType 'down' michael@0: #endif michael@0: michael@0: rv = GetOSXFolderType(kUserDomain, kDownloadsFolderType, michael@0: getter_AddRefs(localFile)); michael@0: if (NS_FAILED(rv)) { michael@0: rv = GetOSXFolderType(kUserDomain, kDesktopFolderType, michael@0: getter_AddRefs(localFile)); michael@0: } michael@0: } michael@0: else if (inAtom == nsDirectoryService::sUserDesktopDirectory || michael@0: inAtom == nsDirectoryService::sOS_DesktopDirectory) michael@0: { michael@0: rv = GetOSXFolderType(kUserDomain, kDesktopFolderType, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sLocalDesktopDirectory) michael@0: { michael@0: rv = GetOSXFolderType(kLocalDomain, kDesktopFolderType, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sUserApplicationsDirectory) michael@0: { michael@0: rv = GetOSXFolderType(kUserDomain, kApplicationsFolderType, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sLocalApplicationsDirectory) michael@0: { michael@0: rv = GetOSXFolderType(kLocalDomain, kApplicationsFolderType, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sUserDocumentsDirectory) michael@0: { michael@0: rv = GetOSXFolderType(kUserDomain, kDocumentsFolderType, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sLocalDocumentsDirectory) michael@0: { michael@0: rv = GetOSXFolderType(kLocalDomain, kDocumentsFolderType, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sUserInternetPlugInDirectory) michael@0: { michael@0: rv = GetOSXFolderType(kUserDomain, kInternetPlugInFolderType, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sLocalInternetPlugInDirectory) michael@0: { michael@0: rv = GetOSXFolderType(kLocalDomain, kInternetPlugInFolderType, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sUserFrameworksDirectory) michael@0: { michael@0: rv = GetOSXFolderType(kUserDomain, kFrameworksFolderType, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sLocalFrameworksDirectory) michael@0: { michael@0: rv = GetOSXFolderType(kLocalDomain, kFrameworksFolderType, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sUserPreferencesDirectory) michael@0: { michael@0: rv = GetOSXFolderType(kUserDomain, kPreferencesFolderType, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sLocalPreferencesDirectory) michael@0: { michael@0: rv = GetOSXFolderType(kLocalDomain, kPreferencesFolderType, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sPictureDocumentsDirectory) michael@0: { michael@0: rv = GetOSXFolderType(kUserDomain, kPictureDocumentsFolderType, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sMovieDocumentsDirectory) michael@0: { michael@0: rv = GetOSXFolderType(kUserDomain, kMovieDocumentsFolderType, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sMusicDocumentsDirectory) michael@0: { michael@0: rv = GetOSXFolderType(kUserDomain, kMusicDocumentsFolderType, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sInternetSitesDirectory) michael@0: { michael@0: rv = GetOSXFolderType(kUserDomain, kInternetSitesFolderType, getter_AddRefs(localFile)); michael@0: } michael@0: #elif defined (XP_WIN) michael@0: else if (inAtom == nsDirectoryService::sSystemDirectory) michael@0: { michael@0: rv = GetSpecialSystemDirectory(Win_SystemDirectory, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sWindowsDirectory) michael@0: { michael@0: rv = GetSpecialSystemDirectory(Win_WindowsDirectory, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sWindowsProgramFiles) michael@0: { michael@0: rv = GetSpecialSystemDirectory(Win_ProgramFiles, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sOS_HomeDirectory) michael@0: { michael@0: rv = GetSpecialSystemDirectory(Win_HomeDirectory, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sDesktop) michael@0: { michael@0: rv = GetSpecialSystemDirectory(Win_Desktop, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sPrograms) michael@0: { michael@0: rv = GetSpecialSystemDirectory(Win_Programs, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sControls) michael@0: { michael@0: rv = GetSpecialSystemDirectory(Win_Controls, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sPrinters) michael@0: { michael@0: rv = GetSpecialSystemDirectory(Win_Printers, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sPersonal) michael@0: { michael@0: rv = GetSpecialSystemDirectory(Win_Personal, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sFavorites) michael@0: { michael@0: rv = GetSpecialSystemDirectory(Win_Favorites, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sStartup) michael@0: { michael@0: rv = GetSpecialSystemDirectory(Win_Startup, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sRecent) michael@0: { michael@0: rv = GetSpecialSystemDirectory(Win_Recent, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sSendto) michael@0: { michael@0: rv = GetSpecialSystemDirectory(Win_Sendto, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sBitbucket) michael@0: { michael@0: rv = GetSpecialSystemDirectory(Win_Bitbucket, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sStartmenu) michael@0: { michael@0: rv = GetSpecialSystemDirectory(Win_Startmenu, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sDesktopdirectory || michael@0: inAtom == nsDirectoryService::sOS_DesktopDirectory) michael@0: { michael@0: rv = GetSpecialSystemDirectory(Win_Desktopdirectory, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sDrives) michael@0: { michael@0: rv = GetSpecialSystemDirectory(Win_Drives, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sNetwork) michael@0: { michael@0: rv = GetSpecialSystemDirectory(Win_Network, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sNethood) michael@0: { michael@0: rv = GetSpecialSystemDirectory(Win_Nethood, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sFonts) michael@0: { michael@0: rv = GetSpecialSystemDirectory(Win_Fonts, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sTemplates) michael@0: { michael@0: rv = GetSpecialSystemDirectory(Win_Templates, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sCommon_Startmenu) michael@0: { michael@0: rv = GetSpecialSystemDirectory(Win_Common_Startmenu, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sCommon_Programs) michael@0: { michael@0: rv = GetSpecialSystemDirectory(Win_Common_Programs, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sCommon_Startup) michael@0: { michael@0: rv = GetSpecialSystemDirectory(Win_Common_Startup, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sCommon_Desktopdirectory) michael@0: { michael@0: rv = GetSpecialSystemDirectory(Win_Common_Desktopdirectory, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sCommon_AppData) michael@0: { michael@0: rv = GetSpecialSystemDirectory(Win_Common_AppData, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sAppdata) michael@0: { michael@0: rv = GetSpecialSystemDirectory(Win_Appdata, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sLocalAppdata) michael@0: { michael@0: rv = GetSpecialSystemDirectory(Win_LocalAppdata, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sPrinthood) michael@0: { michael@0: rv = GetSpecialSystemDirectory(Win_Printhood, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sWinCookiesDirectory) michael@0: { michael@0: rv = GetSpecialSystemDirectory(Win_Cookies, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sDefaultDownloadDirectory) michael@0: { michael@0: rv = GetSpecialSystemDirectory(Win_Downloads, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sDocs) michael@0: { michael@0: rv = GetSpecialSystemDirectory(Win_Documents, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sPictures) michael@0: { michael@0: rv = GetSpecialSystemDirectory(Win_Pictures, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sMusic) michael@0: { michael@0: rv = GetSpecialSystemDirectory(Win_Music, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sVideos) michael@0: { michael@0: rv = GetSpecialSystemDirectory(Win_Videos, getter_AddRefs(localFile)); michael@0: } michael@0: #elif defined (XP_UNIX) michael@0: michael@0: else if (inAtom == nsDirectoryService::sLocalDirectory) michael@0: { michael@0: rv = GetSpecialSystemDirectory(Unix_LocalDirectory, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sLibDirectory) michael@0: { michael@0: rv = GetSpecialSystemDirectory(Unix_LibDirectory, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sOS_HomeDirectory) michael@0: { michael@0: rv = GetSpecialSystemDirectory(Unix_HomeDirectory, getter_AddRefs(localFile)); michael@0: } michael@0: else if (inAtom == nsDirectoryService::sXDGDesktop || michael@0: inAtom == nsDirectoryService::sOS_DesktopDirectory) michael@0: { michael@0: rv = GetSpecialSystemDirectory(Unix_XDG_Desktop, getter_AddRefs(localFile)); michael@0: *persistent = false; michael@0: } michael@0: else if (inAtom == nsDirectoryService::sXDGDocuments) michael@0: { michael@0: rv = GetSpecialSystemDirectory(Unix_XDG_Documents, getter_AddRefs(localFile)); michael@0: *persistent = false; michael@0: } michael@0: else if (inAtom == nsDirectoryService::sXDGDownload || michael@0: inAtom == nsDirectoryService::sDefaultDownloadDirectory) michael@0: { michael@0: rv = GetSpecialSystemDirectory(Unix_XDG_Download, getter_AddRefs(localFile)); michael@0: *persistent = false; michael@0: } michael@0: else if (inAtom == nsDirectoryService::sXDGMusic) michael@0: { michael@0: rv = GetSpecialSystemDirectory(Unix_XDG_Music, getter_AddRefs(localFile)); michael@0: *persistent = false; michael@0: } michael@0: else if (inAtom == nsDirectoryService::sXDGPictures) michael@0: { michael@0: rv = GetSpecialSystemDirectory(Unix_XDG_Pictures, getter_AddRefs(localFile)); michael@0: *persistent = false; michael@0: } michael@0: else if (inAtom == nsDirectoryService::sXDGPublicShare) michael@0: { michael@0: rv = GetSpecialSystemDirectory(Unix_XDG_PublicShare, getter_AddRefs(localFile)); michael@0: *persistent = false; michael@0: } michael@0: else if (inAtom == nsDirectoryService::sXDGTemplates) michael@0: { michael@0: rv = GetSpecialSystemDirectory(Unix_XDG_Templates, getter_AddRefs(localFile)); michael@0: *persistent = false; michael@0: } michael@0: else if (inAtom == nsDirectoryService::sXDGVideos) michael@0: { michael@0: rv = GetSpecialSystemDirectory(Unix_XDG_Videos, getter_AddRefs(localFile)); michael@0: *persistent = false; michael@0: } michael@0: #endif michael@0: michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: michael@0: if (!localFile) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: localFile.forget(_retval); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsDirectoryService::GetFiles(const char *prop, nsISimpleEnumerator **_retval) michael@0: { michael@0: if (NS_WARN_IF(!_retval)) michael@0: return NS_ERROR_INVALID_ARG; michael@0: *_retval = nullptr; michael@0: michael@0: return NS_ERROR_FAILURE; michael@0: }