From 09c82decb32ef99581dd3133f1edd2d5836a7d4e Mon Sep 17 00:00:00 2001 From: "jelwell%netscape.com" Date: Thu, 21 Mar 2002 23:14:14 +0000 Subject: [PATCH] Bug 128377, Generalize Turbo Code r=law, sr=alecf, r=ssu, sr=slogan a=asa git-svn-id: svn://10.0.0.236/trunk@117096 18797224-902f-48f8-a5cc-f745e15eee43 --- .../xpfe/bootstrap/nsNativeAppSupportWin.cpp | 2 +- .../resources/content/pref-advanced.xul | 6 +- .../components/winhooks/nsIWindowsHooks.idl | 19 +- .../components/winhooks/nsWindowsHooks.cpp | 184 ++++++++++++++---- 4 files changed, 157 insertions(+), 54 deletions(-) diff --git a/mozilla/xpfe/bootstrap/nsNativeAppSupportWin.cpp b/mozilla/xpfe/bootstrap/nsNativeAppSupportWin.cpp index 85d4f754c96..2b0090b1cc9 100644 --- a/mozilla/xpfe/bootstrap/nsNativeAppSupportWin.cpp +++ b/mozilla/xpfe/bootstrap/nsNativeAppSupportWin.cpp @@ -890,7 +890,7 @@ struct MessageWindow { } nsCOMPtr winHooksService ( do_GetService( NS_IWINDOWSHOOKS_CONTRACTID, &rv ) ); if ( NS_SUCCEEDED( rv ) ) - winHooksService->StartupTurboDisable(); + winHooksService->StartupRemoveOption("-turbo"); nsCOMPtr appShell = do_GetService( "@mozilla.org/appshell/appShellService;1", &rv ); if ( NS_SUCCEEDED( rv ) ) { diff --git a/mozilla/xpfe/components/prefwindow/resources/content/pref-advanced.xul b/mozilla/xpfe/components/prefwindow/resources/content/pref-advanced.xul index 43252ba0ec2..fc8bfcdcf53 100644 --- a/mozilla/xpfe/components/prefwindow/resources/content/pref-advanced.xul +++ b/mozilla/xpfe/components/prefwindow/resources/content/pref-advanced.xul @@ -82,7 +82,7 @@ } var winhooksService = Components.classes["@mozilla.org/winhooks;1"].getService(Components.interfaces.nsIWindowsHooks); if (winhooksService) { - parent.isTurboEnabled = winhooksService.isStartupTurboEnabled(); + parent.isTurboEnabled = winhooksService.isOptionEnabled("-turbo"); document.getElementById("enableTurbo").checked = parent.isTurboEnabled; } @@ -111,9 +111,9 @@ var winhooksService = Components.classes["@mozilla.org/winhooks;1"].getService(Components.interfaces.nsIWindowsHooks); if (winhooksService && parent.isTurboEnabled != -1) { if (parent.isTurboEnabled) - winhooksService.startupTurboEnable(); + winhooksService.startupAddOption("-turbo"); else - winhooksService.startupTurboDisable(); + winhooksService.startupRemoveOption("-turbo"); var appShell = Components.classes['@mozilla.org/appshell/appShellService;1'].getService(); appShell = appShell.QueryInterface( Components.interfaces.nsIAppShellService ); var nativeAppSupport = null; diff --git a/mozilla/xpfe/components/winhooks/nsIWindowsHooks.idl b/mozilla/xpfe/components/winhooks/nsIWindowsHooks.idl index 98d14e840b7..6fb26f9de3f 100644 --- a/mozilla/xpfe/components/winhooks/nsIWindowsHooks.idl +++ b/mozilla/xpfe/components/winhooks/nsIWindowsHooks.idl @@ -173,23 +173,24 @@ interface nsIWindowsHooks : nsISupports { boolean checkSettings( in nsIDOMWindowInternal aParent ); /** - * Returns true if the "(appname) QuickLaunch" value is in the + * Returns true if command is in the "(appname) QuickLaunch" value in the * HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run - * key (indicating that the app will preload when Windows starts). + * key. */ - boolean isStartupTurboEnabled(); + boolean isOptionEnabled(in string option); /** - * Adds the "(appname) QuickLaunch" value to the key mentioned above, - * with data "(path\to\app.exe) -turbo", if not done already. + * Adds the option to the "(appname) QuickLaunch" value to the key mentioned above, + * with data "(path\to\app.exe) option", if not done already. */ - void startupTurboEnable(); + void startupAddOption(in string option); /** - * Removes the "(appname) QuickLaunch" value from the key mentioned above, - * if not done already. + * Removes the commnand from the "(appname) QuickLaunch" value from + * the key mentioned above, if not done already. And deletes the + * "(appname) QuickLaunch" value entirely if there are no options left */ - void startupTurboDisable(); + void startupRemoveOption(in string option); /** * Accepts an element, either an HTML img element or an element with diff --git a/mozilla/xpfe/components/winhooks/nsWindowsHooks.cpp b/mozilla/xpfe/components/winhooks/nsWindowsHooks.cpp index 4c0f1b18414..5565141c089 100644 --- a/mozilla/xpfe/components/winhooks/nsWindowsHooks.cpp +++ b/mozilla/xpfe/components/winhooks/nsWindowsHooks.cpp @@ -22,6 +22,7 @@ * Contributor(s): * Bill Law * Syd Logan added turbo mode stuff + * Joe Elwell * Håkan Waara * * Alternatively, the contents of this file may be used under the terms of @@ -76,6 +77,8 @@ #include "nsIFileStream.h" #include "nsFileSpec.h" +#define RUNKEY "Software\\Microsoft\\Windows\\CurrentVersion\\Run" + // Objects that describe the Windows registry entries that we need to tweak. static ProtocolRegistryEntry http( "http" ), @@ -646,55 +649,154 @@ nsWindowsHooks::SetRegistry() { // nsIWindowsHooks.idl for documentation -NS_IMETHODIMP nsWindowsHooks::IsStartupTurboEnabled(PRBool *_retval) -{ - *_retval = PR_FALSE; - HKEY key; - LONG result = ::RegOpenKeyEx( HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_QUERY_VALUE, &key ); - if ( result == ERROR_SUCCESS ) { - result = ::RegQueryValueEx( key, NS_QUICKLAUNCH_RUN_KEY, NULL, NULL, NULL, NULL ); - ::RegCloseKey( key ); - if ( result == ERROR_SUCCESS ) - *_retval = PR_TRUE; - } +/* + * Name: IsOptionEnabled + * Arguments: + * PRUnichar* option - the option line switch we check to see if it is in the registry key + * + * Return Value: + * PRBool* _retval - PR_TRUE if option is already in the registry key, otherwise PR_FALSE + * + * Description: + * This function merely checks if the passed in string exists in the (appname) Quick Launch Key or not. + * + * Author: + * Joseph Elwell 3/1/2002 +*/ +NS_IMETHODIMP nsWindowsHooks::IsOptionEnabled(const char* option, PRBool *_retval) { + NS_ASSERTION(option, "nsWindowsHooks::IsOptionEnabled requires something like \"-turbo\""); + *_retval = PR_FALSE; + RegistryEntry startup ( HKEY_CURRENT_USER, RUNKEY, NS_QUICKLAUNCH_RUN_KEY, NULL ); + nsCString cargs = startup.currentSetting(); + if (cargs.Find(option, PR_TRUE) != kNotFound) + *_retval = PR_TRUE; return NS_OK; } -NS_IMETHODIMP nsWindowsHooks::StartupTurboEnable() -{ - PRBool startupFound; - IsStartupTurboEnabled( &startupFound ); - - if ( startupFound == PR_TRUE ) - return NS_OK; // already enabled, no need to do this - - HKEY hKey; - LONG res = ::RegCreateKeyEx( HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_SET_VALUE, NULL, &hKey, NULL ); - if ( res != ERROR_SUCCESS ) - return NS_OK; - char fileName[_MAX_PATH]; - int rv = ::GetModuleFileName( NULL, fileName, sizeof fileName ); - if ( rv ) { - strcat( fileName, " -turbo" ); - ::RegSetValueEx( hKey, NS_QUICKLAUNCH_RUN_KEY, 0, REG_SZ, ( LPBYTE )fileName, strlen( fileName ) ); - } - ::RegCloseKey( hKey ); - return NS_OK; +/* + * Name: grabArgs + * Arguments: + * char* optionline - the full optionline as read from the (appname) Quick Launch registry key + * + * Return Value: + * char** args - A pointer to the arguments (string in optionline) + * passed to the executable in the (appname) Quick Launch registry key + * + * Description: + * This function seperates out the arguments from the optinline string + * Returning a pointer into the first arguments buffer. + * This function is used only locally, and is meant to reduce code size and readability. + * + * Author: + * Joseph Elwell 3/1/2002 +*/ +void grabArgs(char* optionline, char** args) { + nsCRT::strtok(optionline, "\"", &optionline); + if (optionline != NULL) + *args = nsCRT::strtok(optionline, "\"", &optionline); } -NS_IMETHODIMP nsWindowsHooks::StartupTurboDisable() -{ +/* + * Name: StartupAddOption + * Arguments: + * PRUnichar* option - the option line switch we want to add to the registry key + * + * Return Value: none + * + * Description: + * This function adds the given option line argument to the (appname) Quick Launch registry key + * + * Author: + * Joseph Elwell 3/1/2002 +*/ +NS_IMETHODIMP nsWindowsHooks::StartupAddOption(const char* option) { + NS_ASSERTION(option, "nsWindowsHooks::StartupAddOption requires something like \"-turbo\""); + PRBool retval; + IsOptionEnabled(option, &retval); + if (retval) return NS_OK; //already in there + + RegistryEntry startup ( HKEY_CURRENT_USER, RUNKEY, NS_QUICKLAUNCH_RUN_KEY, NULL ); + nsCString cargs = startup.currentSetting(); + nsCAutoString newsetting; + newsetting.Assign('\"'); + newsetting.Append(thisApplication()); + newsetting.Append('\"'); + if (!cargs.IsEmpty()) + { + char* args; + // exploiting the fact that nsString's storage is also a char* buffer. + // NS_CONST_CAST is safe here because nsCRT::strtok will only modify + // the existing buffer + grabArgs(NS_CONST_CAST(char *, cargs.get()), &args); + if (args != NULL) + newsetting.Append(args); + else + { + // check for the old style registry key that doesnot quote its executable + IsOptionEnabled("-turbo", &retval); + if (retval) + newsetting.Append(" -turbo"); + } + } + newsetting.Append(' '); + newsetting.Append(option); + startup.setting = newsetting; + startup.set(); +} + +/* + * Name: StartupRemoveOption + * Arguments: + * PRUnichar* option - the option line switch we want to remove from the registry key + * + * Return Value: none. + * + * Description: + * This function removes the given option from the (appname) Quick Launch Key. + * And deletes the key entirely if no options are left. + * + * Author: + * Joseph Elwell 3/1/2002 +*/ +NS_IMETHODIMP nsWindowsHooks::StartupRemoveOption(const char* option) { + NS_ASSERTION(option, "nsWindowsHooks::StartupRemoveOption requires something like \"-turbo\""); PRBool startupFound; - IsStartupTurboEnabled( &startupFound ); + IsOptionEnabled(option, &startupFound ); if ( !startupFound ) - return NS_OK; // already disabled, no need to do this + return NS_OK; // already disabled, no need to do anything - HKEY hKey; - LONG res = ::RegOpenKeyEx( HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_SET_VALUE, &hKey ); - if ( res != ERROR_SUCCESS ) - return NS_OK; - res = ::RegDeleteValue( hKey, NS_QUICKLAUNCH_RUN_KEY ); - ::RegCloseKey( hKey ); + RegistryEntry startup ( HKEY_CURRENT_USER, RUNKEY, NS_QUICKLAUNCH_RUN_KEY, NULL ); + nsCString cargs = startup.currentSetting(); + char* args; + // exploiting the fact that nsString's storage is also a char* buffer. + // NS_CONST_CAST is safe here because nsCRT::strtok will only modify + // the existing buffer + grabArgs(NS_CONST_CAST(char *, cargs.get()), &args); + + nsCAutoString launchcommand; + if (args) + { + launchcommand.Assign(args); + PRInt32 optionlocation = launchcommand.Find(option, PR_TRUE); + // modify by one to get rid of the space we prepended in StartupAddOption + if (optionlocation != kNotFound) + launchcommand.Cut(optionlocation - 1, nsCRT::strlen(option) + 1); + } + + if (launchcommand.IsEmpty()) + { + startup.set(); + } + else + { + nsCAutoString ufileName; + ufileName.Assign('\"'); + ufileName.Append(thisApplication()); + ufileName.Append('\"'); + ufileName.Append(launchcommand); + startup.setting = ufileName; + startup.set(); + } return NS_OK; }