gecko-dev/xpfe/components/winhooks/nsWindowsHooks.cpp

383 lines
13 KiB
C++

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/*
* The contents of this file are subject to the Netscape Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
* Bill Law <law@netscape.com>
*/
#include "nsIWindowsHooks.h"
#include "nsIGenericFactory.h"
#include <windows.h>
// Implementation utilities.
#include "nsWindowsHooksUtil.cpp"
// Objects that describe the Windows registry entries that we need to tweak.
static ProtocolRegistryEntry
http( "http" ),
https( "https" ),
ftp( "ftp" ),
chrome( "chrome" );
const char *jpgExts[] = { ".jpg", ".jpeg", 0 };
const char *gifExts[] = { ".gif", 0 };
const char *pngExts[] = { ".png", 0 };
const char *xmlExts[] = { ".xml", 0 };
const char *xulExts[] = { ".xul", 0 };
const char *htmExts[] = { ".htm", ".html", 0 };
static FileTypeRegistryEntry
jpg( jpgExts, "MozillaJPEG", "Mozilla JPEG Image File" ),
gif( gifExts, "MozillaGIF", "Mozilla GIF Image File" ),
png( pngExts, "MozillaPNG", "Mozilla Portable Network Graphic Image File" ),
xml( xmlExts, "MozillaXML", "Mozilla Extensible Markup Language Document" ),
xul( xulExts, "MozillaXUL", "Mozilla Extensible User-interface Language Document" );
static EditableFileTypeRegistryEntry
mozillaMarkup( htmExts, "MozillaHTML", "Mozilla Hypertext Markup Language Document" );
/* c09bc130-0a71-11d4-8076-00600811a9c3 */
#define NS_IWINDOWSHOOKS_CID \
{ 0xc09bc130, 0x0a71, 0x11d4, {0x80, 0x76, 0x00, 0x60, 0x08, 0x11, 0xa9, 0xc3} }
// Implementation of the nsIWindowsHooksSettings interface.
class nsWindowsHooksSettings : public nsIWindowsHooksSettings {
public:
// ctor/dtor
nsWindowsHooksSettings();
virtual ~nsWindowsHooksSettings();
// Declare all interface methods we must implement.
NS_DECL_ISUPPORTS
NS_DECL_NSIWINDOWSHOOKSSETTINGS
// Typedef for nsIWindowsHooksSettings getter/setter member functions.
typedef nsresult (__stdcall nsIWindowsHooksSettings::*getter)( PRBool* );
typedef nsresult (__stdcall nsIWindowsHooksSettings::*setter)( PRBool );
protected:
// General purpose getter.
NS_IMETHOD Get( PRBool *result, PRBool nsWindowsHooksSettings::*member );
// General purpose setter.
NS_IMETHOD Set( PRBool value, PRBool nsWindowsHooksSettings::*member );
private:
// Internet shortcut protocols.
struct {
PRBool mHandleHTTP;
PRBool mHandleHTTPS;
PRBool mHandleFTP;
PRBool mHandleCHROME;
};
// File types.
struct {
PRBool mHandleHTML;
PRBool mHandleJPEG;
PRBool mHandleGIF;
PRBool mHandlePNG;
PRBool mHandleXML;
PRBool mHandleXUL;
};
friend class nsWindowsHooks;
}; // nsWindowsHooksSettings
// Use standard implementation of nsISupports stuff.
NS_IMPL_ISUPPORTS1( nsWindowsHooksSettings, nsIWindowsHooksSettings );
nsWindowsHooksSettings::nsWindowsHooksSettings() {
NS_INIT_ISUPPORTS();
}
nsWindowsHooksSettings::~nsWindowsHooksSettings() {
}
// Generic getter.
NS_IMETHODIMP
nsWindowsHooksSettings::Get( PRBool *result, PRBool nsWindowsHooksSettings::*member ) {
NS_ENSURE_ARG( result );
NS_ENSURE_ARG( member );
*result = this->*member;
return NS_OK;
}
// Generic setter.
NS_IMETHODIMP
nsWindowsHooksSettings::Set( PRBool value, PRBool nsWindowsHooksSettings::*member ) {
NS_ENSURE_ARG( member );
this->*member = value;
return NS_OK;
}
// Macros to define specific getter/setter methods.
#define DEFINE_GETTER_AND_SETTER( attr, member ) \
NS_IMETHODIMP \
nsWindowsHooksSettings::Get##attr ( PRBool *result ) { \
return this->Get( result, &nsWindowsHooksSettings::member ); \
} \
NS_IMETHODIMP \
nsWindowsHooksSettings::Set##attr ( PRBool value ) { \
return this->Set( value, &nsWindowsHooksSettings::member ); \
}
// Define all the getter/setter methods:
DEFINE_GETTER_AND_SETTER( IsHandlingHTML, mHandleHTML )
DEFINE_GETTER_AND_SETTER( IsHandlingJPEG, mHandleJPEG )
DEFINE_GETTER_AND_SETTER( IsHandlingGIF, mHandleGIF )
DEFINE_GETTER_AND_SETTER( IsHandlingPNG, mHandlePNG )
DEFINE_GETTER_AND_SETTER( IsHandlingXML, mHandleXML )
DEFINE_GETTER_AND_SETTER( IsHandlingXUL, mHandleXUL )
DEFINE_GETTER_AND_SETTER( IsHandlingHTTP, mHandleHTTP )
DEFINE_GETTER_AND_SETTER( IsHandlingHTTPS, mHandleHTTPS )
DEFINE_GETTER_AND_SETTER( IsHandlingFTP, mHandleFTP )
DEFINE_GETTER_AND_SETTER( IsHandlingCHROME, mHandleCHROME )
// Implementation of the nsIWindowsHooks interface.
class nsWindowsHooks : public nsIWindowsHooks {
public:
// ctor/dtor
nsWindowsHooks();
virtual ~nsWindowsHooks();
// Declare all interface methods we must implement.
NS_DECL_ISUPPORTS
NS_DECL_NSIWINDOWSHOOKS
protected:
// Internal flavor of GetPreferences.
NS_IMETHOD GetSettings( nsWindowsHooksSettings ** );
// Set registry according to settings.
NS_IMETHOD SetRegistry();
private:
// Module stuff.
static NS_METHOD CreateWindowsHooks( nsISupports *aOuter,
REFNSIID aIID,
void **aResult );
static nsWindowsHooks *mInstance;
public:
static nsModuleComponentInfo components[];
}; // nsWindowsHooksSettings
// Use standard implementation of nsISupports stuff.
NS_IMPL_ISUPPORTS1( nsWindowsHooks, nsIWindowsHooks );
nsWindowsHooks::nsWindowsHooks() {
NS_INIT_ISUPPORTS();
}
nsWindowsHooks::~nsWindowsHooks() {
}
// Internal GetPreferences.
NS_IMETHODIMP
nsWindowsHooks::GetSettings( nsWindowsHooksSettings **result ) {
nsresult rv = NS_OK;
// Validate input arg.
NS_ENSURE_ARG( result );
// Allocate prefs object.
nsWindowsHooksSettings *prefs = *result = new nsWindowsHooksSettings;
NS_ENSURE_TRUE( prefs, NS_ERROR_OUT_OF_MEMORY );
// Got it, increment ref count.
NS_ADDREF( prefs );
// Get each registry value and copy to prefs structure.
prefs->mHandleHTTP = (void*)( BoolRegistryEntry( "isHandlingHTTP" ) ) ? PR_TRUE : PR_FALSE;
prefs->mHandleHTTPS = (void*)( BoolRegistryEntry( "isHandlingHTTPS" ) ) ? PR_TRUE : PR_FALSE;
prefs->mHandleFTP = (void*)( BoolRegistryEntry( "isHandlingFTP" ) ) ? PR_TRUE : PR_FALSE;
prefs->mHandleCHROME = (void*)( BoolRegistryEntry( "isHandlingCHROME" ) ) ? PR_TRUE : PR_FALSE;
prefs->mHandleHTML = (void*)( BoolRegistryEntry( "isHandlingHTML" ) ) ? PR_TRUE : PR_FALSE;
prefs->mHandleJPEG = (void*)( BoolRegistryEntry( "isHandlingJPEG" ) ) ? PR_TRUE : PR_FALSE;
prefs->mHandleGIF = (void*)( BoolRegistryEntry( "isHandlingGIF" ) ) ? PR_TRUE : PR_FALSE;
prefs->mHandlePNG = (void*)( BoolRegistryEntry( "isHandlingPNG" ) ) ? PR_TRUE : PR_FALSE;
prefs->mHandleXML = (void*)( BoolRegistryEntry( "isHandlingXML" ) ) ? PR_TRUE : PR_FALSE;
prefs->mHandleXUL = (void*)( BoolRegistryEntry( "isHandlingXUL" ) ) ? PR_TRUE : PR_FALSE;
#ifdef DEBUG_law
NS_WARN_IF_FALSE( NS_SUCCEEDED( rv ), "GetPreferences failed" );
#endif
return rv;
}
// Public interface uses internal plus a QI to get to the proper result.
NS_IMETHODIMP
nsWindowsHooks::GetSettings( nsIWindowsHooksSettings **_retval ) {
// Allocate prefs object.
nsWindowsHooksSettings *prefs;
nsresult rv = this->GetSettings( &prefs );
if ( NS_SUCCEEDED( rv ) ) {
// QI to proper interface.
rv = prefs->QueryInterface( NS_GET_IID( nsIWindowsHooksSettings ), (void**)_retval );
// Release (to undo our Get...).
NS_RELEASE( prefs );
}
return rv;
}
// Utility to set PRBool registry value from getter method.
nsresult putPRBoolIntoRegistry( const char* valueName,
nsIWindowsHooksSettings *prefs,
nsWindowsHooksSettings::getter memFun ) {
// Use getter method to extract attribute from prefs.
PRBool boolValue;
(void)(prefs->*memFun)( &boolValue );
// Convert to DWORD.
DWORD dwordValue = boolValue;
// Store into registry.
BoolRegistryEntry pref( valueName );
nsresult rv = boolValue ? pref.set() : pref.reset();
return rv;
}
/* void setPreferences (in nsIWindowsHooksSettings prefs); */
NS_IMETHODIMP
nsWindowsHooks::SetSettings(nsIWindowsHooksSettings *prefs) {
nsresult rv = NS_ERROR_FAILURE;
putPRBoolIntoRegistry( "isHandlingHTTP", prefs, &nsIWindowsHooksSettings::GetIsHandlingHTTP );
putPRBoolIntoRegistry( "isHandlingHTTPS", prefs, &nsIWindowsHooksSettings::GetIsHandlingHTTPS );
putPRBoolIntoRegistry( "isHandlingFTP", prefs, &nsIWindowsHooksSettings::GetIsHandlingFTP );
putPRBoolIntoRegistry( "isHandlingCHROME", prefs, &nsIWindowsHooksSettings::GetIsHandlingCHROME );
putPRBoolIntoRegistry( "isHandlingHTML", prefs, &nsIWindowsHooksSettings::GetIsHandlingHTML );
putPRBoolIntoRegistry( "isHandlingJPEG", prefs, &nsIWindowsHooksSettings::GetIsHandlingJPEG );
putPRBoolIntoRegistry( "isHandlingGIF", prefs, &nsIWindowsHooksSettings::GetIsHandlingGIF );
putPRBoolIntoRegistry( "isHandlingPNG", prefs, &nsIWindowsHooksSettings::GetIsHandlingPNG );
putPRBoolIntoRegistry( "isHandlingXML", prefs, &nsIWindowsHooksSettings::GetIsHandlingXML );
putPRBoolIntoRegistry( "isHandlingXUL", prefs, &nsIWindowsHooksSettings::GetIsHandlingXUL );
rv = SetRegistry();
return rv;
}
// Get preferences and start handling everything selected.
NS_IMETHODIMP
nsWindowsHooks::SetRegistry() {
nsresult rv = NS_OK;
// Get raw prefs object.
nsWindowsHooksSettings *prefs;
rv = this->GetSettings( &prefs );
NS_ENSURE_TRUE( NS_SUCCEEDED( rv ), rv );
if ( prefs->mHandleHTML ) {
(void) mozillaMarkup.set();
} else {
(void) mozillaMarkup.reset();
}
if ( prefs->mHandleJPEG ) {
(void) jpg.set();
} else {
(void) jpg.reset();
}
if ( prefs->mHandleGIF ) {
(void) gif.set();
} else {
(void) gif.reset();
}
if ( prefs->mHandlePNG ) {
(void) png.set();
} else {
(void) png.reset();
}
if ( prefs->mHandleXML ) {
(void) xml.set();
} else {
(void) xml.reset();
}
if ( prefs->mHandleXUL ) {
(void) xul.set();
} else {
(void) xul.reset();
}
if ( prefs->mHandleHTTP ) {
(void) http.set();
} else {
(void) http.reset();
}
if ( prefs->mHandleHTTPS ) {
(void) https.set();
} else {
(void) https.reset();
}
if ( prefs->mHandleFTP ) {
(void) ftp.set();
} else {
(void) ftp.reset();
}
if ( prefs->mHandleCHROME ) {
(void) ftp.set();
} else {
(void) ftp.reset();
}
return NS_OK;
}
NS_IMETHODIMP
nsWindowsHooks::CreateWindowsHooks( nsISupports *aOuter,
REFNSIID aIID,
void **aResult ) {
if ( !aResult ) {
return NS_ERROR_INVALID_POINTER;
}
if ( aOuter ) {
*aResult = nsnull;
return NS_ERROR_NO_AGGREGATION;
}
if (mInstance == nsnull) {
mInstance = new nsWindowsHooks();
}
if ( mInstance == nsnull )
return NS_ERROR_OUT_OF_MEMORY;
nsresult rv = mInstance->QueryInterface( aIID, aResult );
if ( NS_FAILED(rv) ) {
*aResult = nsnull;
}
return rv;
}
nsWindowsHooks* nsWindowsHooks::mInstance = nsnull;
nsModuleComponentInfo nsWindowsHooks::components[] = {
{ NS_IWINDOWSHOOKS_CLASSNAME,
NS_IWINDOWSHOOKS_CID,
NS_IWINDOWSHOOKS_PROGID,
nsWindowsHooks::CreateWindowsHooks },
};
NS_IMPL_NSGETMODULE( "nsWindowsHooks", nsWindowsHooks::components )