Back to index

lightning-sunbird  0.9+nobinonly
Classes | Defines | Functions | Variables
nsWindowsHooksUtil.cpp File Reference
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include "nsString.h"
#include "nsIStringBundle.h"
#include "nsDirectoryService.h"
#include "nsAppDirectoryServiceDefs.h"
#include "nsNativeCharsetUtils.h"
#include "nsINativeAppSupportWin.h"
#include "nsICmdLineHandler.h"
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  RegistryEntry
struct  BoolRegistryEntry
struct  SavedRegistryEntry
struct  ProtocolRegistryEntry
struct  ProtocolIconRegistryEntry
struct  DDERegistryEntry
struct  FileTypeRegistryEntry
struct  EditableFileTypeRegistryEntry

Defines

#define MOZ_HWND_BROADCAST_MSG_TIMEOUT   5000
#define MOZ_CLIENT_BROWSER_KEY   "Software\\Clients\\StartMenuInternet"

Functions

static nsCString thisApplication ()
static nsCString shortAppName ()
static PRBool IsNT ()
static void setWindowsXP ()
static void resetWindowsXP ()
static DWORD deleteKey (HKEY baseKey, const char *keyName)

Variables

const char *const mozillaKeyName = "Software\\Mozilla\\Desktop"
static const char shortcutSuffix [] = " -osint -url \"%1\""
static const char chromeSuffix [] = " -osint -chrome \"%1\""
static const char iconSuffix [] = ",0"

Define Documentation

#define MOZ_CLIENT_BROWSER_KEY   "Software\\Clients\\StartMenuInternet"

Definition at line 57 of file nsWindowsHooksUtil.cpp.

Definition at line 56 of file nsWindowsHooksUtil.cpp.


Function Documentation

static DWORD deleteKey ( HKEY  baseKey,
const char *  keyName 
) [static]

Definition at line 646 of file nsWindowsHooksUtil.cpp.

                                                            {
    // Make sure input subkey isn't null.
    DWORD rc;
    if ( keyName && ::strlen(keyName) ) {
        // Open subkey.
        HKEY key;
        rc = ::RegOpenKeyEx( baseKey,
                             keyName,
                             0,
                             KEY_ENUMERATE_SUB_KEYS | DELETE,
                             &key );
        // Continue till we get an error or are done.
        while ( rc == ERROR_SUCCESS ) {
            char subkeyName[_MAX_PATH];
            DWORD len = sizeof subkeyName;
            // Get first subkey name.  Note that we always get the
            // first one, then delete it.  So we need to get
            // the first one next time, also.
            rc = ::RegEnumKeyEx( key,
                                 0,
                                 subkeyName,
                                 &len,
                                 0,
                                 0,
                                 0,
                                 0 );
            if ( rc == ERROR_NO_MORE_ITEMS ) {
                // No more subkeys.  Delete the main one.
                rc = ::RegDeleteKey( baseKey, keyName );
                break;
            } else if ( rc == ERROR_SUCCESS ) {
                // Another subkey, delete it, recursively.
                rc = deleteKey( key, subkeyName );
            }
        }
        // Close the key we opened.
        ::RegCloseKey( key );
    } else {
        rc = ERROR_BADKEY;
    }
    return rc;
}

Here is the caller graph for this function:

static PRBool IsNT ( ) [static]

Definition at line 103 of file nsWindowsHooksUtil.cpp.

{
    static PRBool sInitialized = PR_FALSE;
    static PRBool sIsNT = PR_FALSE;

    if (!sInitialized) {
        OSVERSIONINFO osversion;
        ::ZeroMemory(&osversion, sizeof(OSVERSIONINFO));
        osversion.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
        if (::GetVersionEx(&osversion) && 
            osversion.dwPlatformId == VER_PLATFORM_WIN32_NT)
            sIsNT = PR_TRUE;
        sInitialized = PR_TRUE;
    }

    return sIsNT;
}

Here is the caller graph for this function:

static void resetWindowsXP ( ) [static]

Definition at line 601 of file nsWindowsHooksUtil.cpp.

                             {
    NS_NAMED_LITERAL_CSTRING( baseKey, "Software\\Clients\\StartMenuInternet" );
    // First, try to restore the HKLM setting.  This will fail if either we didn't
    // set that, or, if we don't have access).
    SavedRegistryEntry tmp_entry8( HKEY_LOCAL_MACHINE, baseKey.get(), "", shortAppName().get() );
    tmp_entry8.reset();

    // The HKCU setting is trickier.  We may have set it, but we may also have
    // removed it (see setWindowsXP(), above).  We first try to reverse the
    // setting.  If we had removed it, then this will fail.
    SavedRegistryEntry tmp_entry9( HKEY_CURRENT_USER, baseKey.get(), "", shortAppName().get() );
    tmp_entry9.reset();
    // Now, try to reverse the removal of this key.  This will fail if there is a  current
    // setting, and will only work if this key is unset, and, we have a saved value.
    SavedRegistryEntry tmp_entry10( HKEY_CURRENT_USER, baseKey.get(), "", 0 );
    tmp_entry10.reset();

    // Notify the system of the changes.
    ::SendMessageTimeout( HWND_BROADCAST,
                          WM_SETTINGCHANGE,
                          0,
                          (LPARAM)MOZ_CLIENT_BROWSER_KEY,
                          SMTO_NORMAL|SMTO_ABORTIFHUNG,
                          MOZ_HWND_BROADCAST_MSG_TIMEOUT,
                          NULL);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void setWindowsXP ( ) [static]

Definition at line 433 of file nsWindowsHooksUtil.cpp.

                           {
    // We test for the presence of this subkey as a WindowsXP test.
    // We do it this way so that vagueries of future Windows versions
    // are handled as best we can.
    HKEY key;
    nsCAutoString baseKey( NS_LITERAL_CSTRING( "Software\\Clients\\StartMenuInternet" ) );
    LONG rc = ::RegOpenKey( HKEY_LOCAL_MACHINE, baseKey.get(), &key );
    if ( rc == ERROR_SUCCESS ) {
        // OK, this is WindowsXP (or equivalent).  Add this application to the
        // set registered as "Start Menu internet apps."  These entries go
        // under the subkey MOZILLA.EXE (or whatever the name of this executable is),
        // that subkey name is generated by the utility function shortAppName.
        if ( rc == ERROR_SUCCESS ) {
            // The next 3 go under this subkey.
            nsCAutoString subkey( baseKey + NS_LITERAL_CSTRING( "\\" ) + shortAppName() );
            // Pretty name.  This goes into the LocalizedString value.  It is the
            // name of the executable (preceded by '@'), followed by ",-nnn" where
            // nnn is the resource identifier of the string in the .exe.  That value
            // comes from nsINativeAppSupportWin.h.
            char buffer[ _MAX_PATH + 8 ]; // Path, plus '@', comma, minus, and digits (5)
            _snprintf( buffer, sizeof buffer, "@%s,-%d", thisApplication().get(), IDS_STARTMENU_APPNAME );
            RegistryEntry tmp_entry1( HKEY_LOCAL_MACHINE, 
                           subkey.get(),
                           "LocalizedString", 
                           buffer );
            tmp_entry1.set();
            // Default icon (from .exe resource).
            RegistryEntry tmp_entry2( HKEY_LOCAL_MACHINE, 
                           nsCAutoString( subkey + NS_LITERAL_CSTRING( "\\DefaultIcon" ) ).get(),
                           "", 
                           nsCAutoString( thisApplication() + NS_LITERAL_CSTRING( ",0" ) ).get() );
            tmp_entry2.set();
            // Command to open.
            RegistryEntry tmp_entry3( HKEY_LOCAL_MACHINE,
                           nsCAutoString( subkey + NS_LITERAL_CSTRING( "\\shell\\open\\command" ) ).get(),
                           "", 
                           thisApplication().get() );
            tmp_entry3.set();
            // "Properties" verb.  The default value is the text that will appear in the menu.
            // The default value under the command subkey is the name of this application, with
            // arguments to cause the Preferences window to appear.
            nsCOMPtr<nsIStringBundleService> bundleService( do_GetService( "@mozilla.org/intl/stringbundle;1" ) );
            nsCOMPtr<nsIStringBundle> bundle;
            nsXPIDLString label;
            if ( bundleService &&
                 NS_SUCCEEDED( bundleService->CreateBundle( "chrome://global-platform/locale/nsWindowsHooks.properties",
                                                       getter_AddRefs( bundle ) ) ) &&
                 NS_SUCCEEDED( bundle->GetStringFromName( NS_LITERAL_STRING( "prefsLabel" ).get(), getter_Copies( label ) ) ) ) {
                // Set the label that will appear in the start menu context menu.
                RegistryEntry tmp_entry4( HKEY_LOCAL_MACHINE,
                               nsCAutoString( subkey + NS_LITERAL_CSTRING( "\\shell\\properties" ) ).get(),
                               "", 
                               NS_ConvertUCS2toUTF8( label ).get() );
                tmp_entry4.set();
            }
            RegistryEntry tmp_entry5( HKEY_LOCAL_MACHINE,
                           nsCAutoString( subkey + NS_LITERAL_CSTRING( "\\shell\\properties\\command" ) ).get(),
                           "", nsCAutoString( thisApplication() + 
                                               NS_LITERAL_CSTRING(" -chrome \"chrome://communicator/content/pref/pref.xul\"") ).get()
                          );
            tmp_entry5.set();

            // Now we need to select our application as the default start menu internet application.
            // This is accomplished by first trying to store our subkey name in 
            // HKLM\Software\Clients\StartMenuInternet's default value.  See
            // http://support.microsoft.com/directory/article.asp?ID=KB;EN-US;Q297878 for detail.
            SavedRegistryEntry hklmAppEntry( HKEY_LOCAL_MACHINE, baseKey.get(), "", shortAppName().get() );
            hklmAppEntry.set();
            // That may or may not have worked (depending on whether we have sufficient access).
            if ( hklmAppEntry.currentSetting() == hklmAppEntry.setting ) {
                // We've set the hklm entry, so we can delete the one under hkcu.
                SavedRegistryEntry tmp_entry6( HKEY_CURRENT_USER, baseKey.get(), "", 0 );
                tmp_entry6.set();
            } else {
                // All we can do is set the default start menu internet app for this user.
                SavedRegistryEntry tmp_entry7( HKEY_CURRENT_USER, baseKey.get(), "", shortAppName().get() );
            }
            // Notify the system of the changes.
            ::SendMessageTimeout( HWND_BROADCAST,
                                  WM_SETTINGCHANGE,
                                  0,
                                  (LPARAM)MOZ_CLIENT_BROWSER_KEY,
                                  SMTO_NORMAL|SMTO_ABORTIFHUNG,
                                  MOZ_HWND_BROADCAST_MSG_TIMEOUT,
                                  NULL);
        }
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static nsCString shortAppName ( ) [static]

Definition at line 84 of file nsWindowsHooksUtil.cpp.

                                {
    static nsCAutoString result;
    
    if ( result.IsEmpty() ) { 
        // Find last backslash in thisApplication().
        nsCAutoString thisApp( thisApplication() );
        PRInt32 n = thisApp.RFind( "\\" );
        if ( n != kNotFound ) {
            // Use what comes after the last backslash.
            result = (const char*)thisApp.get() + n + 1;
        } else {
            // Use entire string.
            result = thisApp;
        }
    }

    return result;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static nsCString thisApplication ( ) [static]

Definition at line 67 of file nsWindowsHooksUtil.cpp.

                                   {
    static nsCAutoString result;

    if ( result.IsEmpty() ) {
        char buffer[MAX_PATH] = { 0 };
       DWORD len = ::GetModuleFileName( NULL, buffer, sizeof buffer );
        len = ::GetShortPathName( buffer, buffer, sizeof buffer );
    
        result = buffer;
        ToUpperCase(result);
    }

    return result;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

const char chromeSuffix[] = " -osint -chrome \"%1\"" [static]

Definition at line 63 of file nsWindowsHooksUtil.cpp.

const char iconSuffix[] = ",0" [static]

Definition at line 64 of file nsWindowsHooksUtil.cpp.

const char* const mozillaKeyName = "Software\\Mozilla\\Desktop"

Definition at line 60 of file nsWindowsHooksUtil.cpp.

const char shortcutSuffix[] = " -osint -url \"%1\"" [static]

Definition at line 62 of file nsWindowsHooksUtil.cpp.