Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Static Public Member Functions | Static Public Attributes | Private Member Functions | Static Private Member Functions | Private Attributes
nsDirectoryService Class Reference

#include <nsDirectoryService.h>

Inheritance diagram for nsDirectoryService:
Inheritance graph
[legend]
Collaboration diagram for nsDirectoryService:
Collaboration graph
[legend]

List of all members.

Public Member Functions

NS_DECL_ISUPPORTS
NS_DECL_NSIPROPERTIES
NS_DECL_NSIDIRECTORYSERVICE
NS_DECL_NSIDIRECTORYSERVICEPROVIDER
NS_DECL_NSIDIRECTORYSERVICEPROVIDER2 
nsDirectoryService ()
 ~nsDirectoryService ()
void RegisterCategoryProviders ()
void init ()
 init
void registerProvider (in nsIDirectoryServiceProvider prov)
 registerProvider
void unregisterProvider (in nsIDirectoryServiceProvider prov)
 unregisterProvider
void get (in string prop, in nsIIDRef iid,[iid_is(iid), retval] out nsQIResult result)
 Gets a property with a given name.
void set (in string prop, in nsISupports value)
 Sets a property with a given name to a given value.
boolean has (in string prop)
 Returns true if the property with the given name exists.
void undefine (in string prop)
 Undefines a property.
void getKeys (out PRUint32 count,[array, size_is(count), retval] out string keys)
 Returns an array of the keys.
nsISimpleEnumerator getFiles (in string prop)
 getFiles
nsIFile getFile (in string prop, out PRBool persistent)
 getFile

Static Public Member Functions

static nsresult RealInit ()
static NS_METHOD Create (nsISupports *aOuter, REFNSIID aIID, void **aResult)

Static Public Attributes

static nsDirectoryServicegService = nsnull
static nsIAtomsCurrentProcess = nsnull
static nsIAtomsComponentRegistry = nsnull
static nsIAtomsComponentDirectory = nsnull
static nsIAtomsXPTIRegistry = nsnull
static nsIAtomsGRE_Directory = nsnull
static nsIAtomsGRE_ComponentDirectory = nsnull
static nsIAtomsOS_DriveDirectory = nsnull
static nsIAtomsOS_TemporaryDirectory = nsnull
static nsIAtomsOS_CurrentProcessDirectory = nsnull
static nsIAtomsOS_CurrentWorkingDirectory = nsnull
static nsIAtomsOS_DesktopDirectory = nsnull
static nsIAtomsOS_HomeDirectory = nsnull

Private Member Functions

nsresult GetCurrentProcessDirectory (nsILocalFile **aFile)

Static Private Member Functions

static PRBool PR_CALLBACK ReleaseValues (nsHashKey *key, void *data, void *closure)

Private Attributes

nsSupportsHashtable mHashtable
nsCOMPtr< nsISupportsArraymProviders

Detailed Description

Definition at line 52 of file nsDirectoryService.h.


Constructor & Destructor Documentation

Definition at line 412 of file nsDirectoryService.cpp.

                                       :
    mHashtable(256, PR_TRUE)
{
}

Here is the caller graph for this function:

Definition at line 561 of file nsDirectoryService.cpp.

{
}

Member Function Documentation

NS_METHOD nsDirectoryService::Create ( nsISupports *  aOuter,
REFNSIID  aIID,
void **  aResult 
) [static]

Definition at line 418 of file nsDirectoryService.cpp.

Here is the caller graph for this function:

void nsIProperties::get ( in string  prop,
in nsIIDRef  iid,
[iid_is(iid), retval] out nsQIResult  result 
) [inherited]

Gets a property with a given name.

Returns:
NS_ERROR_FAILURE if a property with that name doesn't exist.
NS_ERROR_NO_INTERFACE if the found property fails to QI to the given iid.

Here is the caller graph for this function:

Definition at line 122 of file nsDirectoryService.cpp.

{
    NS_ENSURE_ARG_POINTER(aFile);
    *aFile = nsnull;
    
   //  Set the component registry location:
    if (!gService)
        return NS_ERROR_FAILURE;

    nsresult rv; 
 
    nsCOMPtr<nsIProperties> dirService;
    rv = nsDirectoryService::Create(nsnull, 
                                    NS_GET_IID(nsIProperties), 
                                    getter_AddRefs(dirService));  // needs to be around for life of product

    if (dirService)
    {
      nsCOMPtr <nsILocalFile> aLocalFile;
      dirService->Get(NS_XPCOM_INIT_CURRENT_PROCESS_DIR, NS_GET_IID(nsILocalFile), getter_AddRefs(aLocalFile));
      if (aLocalFile)
      {
        *aFile = aLocalFile;
        NS_ADDREF(*aFile);
        return NS_OK;
      }
    }

    nsLocalFile* localFile = new nsLocalFile;

    if (localFile == nsnull)
        return NS_ERROR_OUT_OF_MEMORY;
    NS_ADDREF(localFile);



#ifdef XP_WIN
    PRUnichar buf[MAX_PATH];
    if ( nsWinAPIs::mGetModuleFileName(0, buf, sizeof(buf)) )
    {
        // chop off the executable name by finding the rightmost backslash
        PRUnichar* lastSlash = wcsrchr(buf, L'\\');
        if (lastSlash)
            *(lastSlash + 1) = L'\0';

        localFile->InitWithPath(nsDependentString(buf));
        *aFile = localFile;
        return NS_OK;
    }

#elif defined(XP_MAC)
    // get info for the the current process to determine the directory
    // its located in
    OSErr err;
    ProcessSerialNumber psn = {kNoProcess, kCurrentProcess};
    ProcessInfoRec pInfo;
    FSSpec         tempSpec;

    // initialize ProcessInfoRec before calling
    // GetProcessInformation() or die horribly.
    pInfo.processName = nil;
    pInfo.processAppSpec = &tempSpec;
    pInfo.processInfoLength = sizeof(ProcessInfoRec);

    err = GetProcessInformation(&psn, &pInfo);
    if (!err)
    {
        // create an FSSpec from the volume and dirid of the app.
        FSSpec appFSSpec;
        ::FSMakeFSSpec(pInfo.processAppSpec->vRefNum, pInfo.processAppSpec->parID, 0, &appFSSpec);
        
        nsCOMPtr<nsILocalFileMac> localFileMac = do_QueryInterface((nsIFile*)localFile);
        if (localFileMac) 
        {
            localFileMac->InitWithFSSpec(&appFSSpec);
            *aFile = localFile;
            return NS_OK;
        }
    }
#elif defined(XP_MACOSX)
    // Works even if we're not bundled.
    CFBundleRef appBundle = CFBundleGetMainBundle();
    if (appBundle != nsnull)
    {
        CFURLRef bundleURL = CFBundleCopyExecutableURL(appBundle);
        if (bundleURL != nsnull)
        {
            CFURLRef parentURL = CFURLCreateCopyDeletingLastPathComponent(kCFAllocatorDefault, bundleURL);
            if (parentURL)
            {
                // Pass PR_TRUE for the "resolveAgainstBase" arg to CFURLGetFileSystemRepresentation.
                // This will resolve the relative portion of the CFURL against it base, giving a full
                // path, which CFURLCopyFileSystemPath doesn't do.
                char buffer[PATH_MAX];
                if (CFURLGetFileSystemRepresentation(parentURL, PR_TRUE, (UInt8 *)buffer, sizeof(buffer)))
                {
#ifdef DEBUG_conrad
                    printf("nsDirectoryService - CurrentProcessDir is: %s\n", buffer);
#endif
                    rv = localFile->InitWithNativePath(nsDependentCString(buffer));
                    if (NS_SUCCEEDED(rv))
                        *aFile = localFile;
                }
                CFRelease(parentURL);
            }
            CFRelease(bundleURL);
        }
    }
    
    NS_ASSERTION(*aFile, "nsDirectoryService - Could not determine CurrentProcessDir.\n");  
    if (*aFile)
        return NS_OK;

#elif defined(XP_UNIX)

    // In the absence of a good way to get the executable directory let
    // us try this for unix:
    // - if MOZILLA_FIVE_HOME is defined, that is it
    // - else give the current directory
    char buf[MAXPATHLEN];

    // The MOZ_DEFAULT_MOZILLA_FIVE_HOME variable can be set at configure time with
    // a --with-default-mozilla-five-home=foo autoconf flag.
    // 
    // The idea here is to allow for builds that have a default MOZILLA_FIVE_HOME
    // regardless of the environment.  This makes it easier to write apps that
    // embed mozilla without having to worry about setting up the environment 
    //
    // We do this py putenv()ing the default value into the environment.  Note that
    // we only do this if it is not already set.
#ifdef MOZ_DEFAULT_MOZILLA_FIVE_HOME
    if (PR_GetEnv("MOZILLA_FIVE_HOME") == nsnull)
    {
        putenv("MOZILLA_FIVE_HOME=" MOZ_DEFAULT_MOZILLA_FIVE_HOME);
    }
#endif

    char *moz5 = PR_GetEnv("MOZILLA_FIVE_HOME");

    if (moz5)
    {
        if (realpath(moz5, buf)) {
            localFile->InitWithNativePath(nsDependentCString(buf));
            *aFile = localFile;
            return NS_OK;
        }
    }
#if defined(DEBUG)
    static PRBool firstWarning = PR_TRUE;

    if(!moz5 && firstWarning) {
        // Warn that MOZILLA_FIVE_HOME not set, once.
        printf("Warning: MOZILLA_FIVE_HOME not set.\n");
        firstWarning = PR_FALSE;
    }
#endif /* DEBUG */

    // Fall back to current directory.
    if (getcwd(buf, sizeof(buf)))
    {
        localFile->InitWithNativePath(nsDependentCString(buf));
        *aFile = localFile;
        return NS_OK;
    }

#elif defined(XP_OS2)
    PPIB ppib;
    PTIB ptib;
    char buffer[CCHMAXPATH];
    DosGetInfoBlocks( &ptib, &ppib);
    DosQueryModuleName( ppib->pib_hmte, CCHMAXPATH, buffer);
    *strrchr( buffer, '\\') = '\0'; // XXX DBCS misery
    localFile->InitWithNativePath(nsDependentCString(buffer));
    *aFile = localFile;
    return NS_OK;

#elif defined(XP_BEOS)
    char buf[MAXPATHLEN];
    int32 cookie = 0;
    image_info info;
    char *p;
    *buf = 0;
    if(get_next_image_info(0, &cookie, &info) == B_OK)
    {
        strcpy(buf, info.name);
        if((p = strrchr(buf, '/')) != 0)
        {
            if( (p-2 >= buf) && *(p-2)=='/' && *(p-1)=='.')
                p -=2; 
            *p = 0;
            localFile->InitWithNativePath(nsDependentCString(buf));
            *aFile = localFile;
            return NS_OK;
        }
    }
#endif
    
    NS_RELEASE(localFile);

    NS_ERROR("unable to get current process directory");
    return NS_ERROR_FAILURE;
} // GetCurrentProcessDirectory()

Here is the call graph for this function:

nsIFile nsIDirectoryServiceProvider::getFile ( in string  prop,
out PRBool  persistent 
) [inherited]

getFile

Directory Service calls this when it gets the first request for a prop or on every request if the prop is not persistent.

Parameters:
propThe symbolic name of the file.
persistentTRUE - The returned file will be cached by Directory Service. Subsequent requests for this prop will bypass the provider and use the cache. FALSE - The provider will be asked for this prop each time it is requested.
Returns:
The file represented by the property.

getFiles

Directory Service calls this when it gets a request for a prop and the requested type is nsISimpleEnumerator.

Parameters:
propThe symbolic name of the file list.
Returns:
An enumerator for a list of file locations. The elements in the enumeration are nsIFile NS_SUCCESS_AGGREGATE_RESULT if this result should be aggregated with other "lower" providers.
void nsIProperties::getKeys ( out PRUint32  count,
[array, size_is(count), retval] out string  keys 
) [inherited]

Returns an array of the keys.

Here is the caller graph for this function:

boolean nsIProperties::has ( in string  prop) [inherited]

Returns true if the property with the given name exists.

Here is the caller graph for this function:

init

Must be called. Used internally by XPCOM initialization.

Definition at line 524 of file nsDirectoryService.cpp.

{
    NS_ASSERTION(!gService, "Mustn't initialize twice!");

    nsresult rv;

    nsRefPtr<nsDirectoryService> self = new nsDirectoryService();
    if (!self)
        return NS_ERROR_OUT_OF_MEMORY;

    rv = NS_NewISupportsArray(getter_AddRefs(((nsDirectoryService*) self)->mProviders));
    if (NS_FAILED(rv))
        return rv;

    NS_RegisterStaticAtoms(directory_atoms, NS_ARRAY_LENGTH(directory_atoms));
    
    // Let the list hold the only reference to the provider.
    nsAppFileLocationProvider *defaultProvider = new nsAppFileLocationProvider;
    if (!defaultProvider)
        return NS_ERROR_OUT_OF_MEMORY;
    // AppendElement returns PR_TRUE for success.
    rv = ((nsDirectoryService*) self)->mProviders->AppendElement(defaultProvider) ? NS_OK : NS_ERROR_FAILURE;
    if (NS_FAILED(rv))
        return rv;

    self.swap(gService);
    return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 748 of file nsDirectoryService.cpp.

{
    nsCOMPtr<nsICategoryManager> catman
        (do_GetService(NS_CATEGORYMANAGER_CONTRACTID));
    if (!catman)
        return;

    nsCOMPtr<nsISimpleEnumerator> entries;
    catman->EnumerateCategory(XPCOM_DIRECTORY_PROVIDER_CATEGORY,
                              getter_AddRefs(entries));

    nsCOMPtr<nsIUTF8StringEnumerator> strings(do_QueryInterface(entries));
    if (!strings)
        return;

    PRBool more;
    while (NS_SUCCEEDED(strings->HasMore(&more)) && more) {
        nsCAutoString entry;
        strings->GetNext(entry);

        nsXPIDLCString contractID;
        catman->GetCategoryEntry(XPCOM_DIRECTORY_PROVIDER_CATEGORY, entry.get(), getter_Copies(contractID));

        if (contractID) {
            nsCOMPtr<nsIDirectoryServiceProvider> provider = do_GetService(contractID.get());
            if (provider)
                RegisterProvider(provider);
        }
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

registerProvider

Register a provider with the service.

Parameters:
provThe service will keep a strong reference to this object. It will be released when the service is released.
PRBool nsDirectoryService::ReleaseValues ( nsHashKey *  key,
void data,
void closure 
) [static, private]

Definition at line 554 of file nsDirectoryService.cpp.

{
    nsISupports* value = (nsISupports*)data;
    NS_IF_RELEASE(value);
    return PR_TRUE;
}
void nsIProperties::set ( in string  prop,
in nsISupports  value 
) [inherited]

Sets a property with a given name to a given value.

Here is the caller graph for this function:

void nsIProperties::undefine ( in string  prop) [inherited]

Undefines a property.

Returns:
NS_ERROR_FAILURE if a property with that name doesn't already exist.

Here is the caller graph for this function:

unregisterProvider

Unregister a provider with the service.

Parameters:
prov

Member Data Documentation

Definition at line 78 of file nsDirectoryService.h.

nsSupportsHashtable nsDirectoryService::mHashtable [private]

Definition at line 84 of file nsDirectoryService.h.

Definition at line 85 of file nsDirectoryService.h.

Definition at line 90 of file nsDirectoryService.h.

Definition at line 89 of file nsDirectoryService.h.

Definition at line 88 of file nsDirectoryService.h.

Definition at line 93 of file nsDirectoryService.h.

Definition at line 92 of file nsDirectoryService.h.

Definition at line 96 of file nsDirectoryService.h.

Definition at line 97 of file nsDirectoryService.h.

Definition at line 98 of file nsDirectoryService.h.

Definition at line 94 of file nsDirectoryService.h.

Definition at line 99 of file nsDirectoryService.h.

Definition at line 95 of file nsDirectoryService.h.

Definition at line 91 of file nsDirectoryService.h.


The documentation for this class was generated from the following files: