Back to index

lightning-sunbird  0.9+nobinonly
nsComposerRegistration.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Michael Judge  <mjudge@netscape.com>
00024  *   Charles Manske <cmanske@netscape.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #include "nsIGenericFactory.h"
00041 
00042 #include "nsEditingSession.h"       // for the CID
00043 #include "nsComposerController.h"   // for the CID
00044 #include "nsEditorSpellCheck.h"     // for the CID
00045 #include "nsComposeTxtSrvFilter.h"
00046 #include "nsIController.h"
00047 #include "nsIControllerContext.h"
00048 #include "nsIControllerCommandTable.h"
00049 
00050 #include "nsServiceManagerUtils.h"
00051 
00052 #define NS_HTMLEDITOR_COMMANDTABLE_CID \
00053 { 0x13e50d8d, 0x9cee, 0x4ad1, { 0xa3, 0xa2, 0x4a, 0x44, 0x2f, 0xdf, 0x7d, 0xfa } }
00054 
00055 #define NS_HTMLEDITOR_DOCSTATE_COMMANDTABLE_CID \
00056 { 0xa33982d3, 0x1adf, 0x4162, { 0x99, 0x41, 0xf7, 0x34, 0xbc, 0x45, 0xe4, 0xed } }
00057 
00058 
00059 static NS_DEFINE_CID(kHTMLEditorCommandTableCID, NS_HTMLEDITOR_COMMANDTABLE_CID);
00060 static NS_DEFINE_CID(kHTMLEditorDocStateCommandTableCID, NS_HTMLEDITOR_DOCSTATE_COMMANDTABLE_CID);
00061 
00062 
00064 // Define the contructor function for the objects
00065 //
00066 // NOTE: This creates an instance of objects by using the default constructor
00067 //
00068 
00069 NS_GENERIC_FACTORY_CONSTRUCTOR(nsEditingSession)
00070 NS_GENERIC_FACTORY_CONSTRUCTOR(nsEditorSpellCheck)
00071 
00072 // There are no macros that enable us to have 2 constructors 
00073 // for the same object
00074 //
00075 // Here we are creating the same object with two different contract IDs
00076 // and then initializing it different.
00077 // Basically, we need to tell the filter whether it is doing mail or not
00078 static NS_METHOD
00079 nsComposeTxtSrvFilterConstructor(nsISupports *aOuter, REFNSIID aIID,
00080                                  void **aResult, PRBool aIsForMail)
00081 {
00082     *aResult = NULL;
00083     if (NULL != aOuter) 
00084     {
00085         return NS_ERROR_NO_AGGREGATION;
00086     }
00087     nsComposeTxtSrvFilter * inst;
00088     NS_NEWXPCOM(inst, nsComposeTxtSrvFilter);
00089     if (NULL == inst) 
00090     {
00091         return NS_ERROR_OUT_OF_MEMORY;
00092     }
00093     NS_ADDREF(inst);
00094          inst->Init(aIsForMail);
00095     nsresult rv = inst->QueryInterface(aIID, aResult);
00096     NS_RELEASE(inst);
00097     return rv;
00098 }
00099 
00100 static NS_METHOD
00101 nsComposeTxtSrvFilterConstructorForComposer(nsISupports *aOuter, 
00102                                             REFNSIID aIID,
00103                                             void **aResult)
00104 {
00105     return nsComposeTxtSrvFilterConstructor(aOuter, aIID, aResult, PR_FALSE);
00106 }
00107 
00108 static NS_METHOD
00109 nsComposeTxtSrvFilterConstructorForMail(nsISupports *aOuter, 
00110                                         REFNSIID aIID,
00111                                         void **aResult)
00112 {
00113     return nsComposeTxtSrvFilterConstructor(aOuter, aIID, aResult, PR_TRUE);
00114 }
00115 
00116 
00117 // Constructor for a controller set up with a command table specified
00118 // by the CID passed in. This function uses do_GetService to get the
00119 // command table, so that every controller shares a single command
00120 // table, for space-efficiency.
00121 // 
00122 // The only reason to go via the service manager for the command table
00123 // is that it holds onto the singleton for us, avoiding static variables here.
00124 static nsresult
00125 CreateControllerWithSingletonCommandTable(const nsCID& inCommandTableCID, nsIController **aResult)
00126 {
00127   nsresult rv;
00128   nsCOMPtr<nsIController> controller = do_CreateInstance("@mozilla.org/embedcomp/base-command-controller;1", &rv);
00129   if (NS_FAILED(rv)) return rv;
00130 
00131   nsCOMPtr<nsIControllerCommandTable> composerCommandTable = do_GetService(inCommandTableCID, &rv);
00132   if (NS_FAILED(rv)) return rv;
00133   
00134   // this guy is a singleton, so make it immutable
00135   composerCommandTable->MakeImmutable();
00136   
00137   nsCOMPtr<nsIControllerContext> controllerContext = do_QueryInterface(controller, &rv);
00138   if (NS_FAILED(rv)) return rv;
00139   
00140   rv = controllerContext->Init(composerCommandTable);
00141   if (NS_FAILED(rv)) return rv;
00142   
00143   *aResult = controller;
00144   NS_ADDREF(*aResult);
00145   return NS_OK;
00146 }
00147 
00148 
00149 // Here we make an instance of the controller that holds doc state commands.
00150 // We set it up with a singleton command table.
00151 static NS_METHOD
00152 nsHTMLEditorDocStateControllerConstructor(nsISupports *aOuter, REFNSIID aIID, 
00153                                               void **aResult)
00154 {
00155   nsCOMPtr<nsIController> controller;
00156   nsresult rv = CreateControllerWithSingletonCommandTable(kHTMLEditorDocStateCommandTableCID, getter_AddRefs(controller));
00157   if (NS_FAILED(rv)) return rv;
00158 
00159   return controller->QueryInterface(aIID, aResult);
00160 }
00161 
00162 // Tere we make an instance of the controller that holds composer commands.
00163 // We set it up with a singleton command table.
00164 static NS_METHOD
00165 nsHTMLEditorControllerConstructor(nsISupports *aOuter, REFNSIID aIID, void **aResult)
00166 {
00167   nsCOMPtr<nsIController> controller;
00168   nsresult rv = CreateControllerWithSingletonCommandTable(kHTMLEditorCommandTableCID, getter_AddRefs(controller));
00169   if (NS_FAILED(rv)) return rv;
00170 
00171   return controller->QueryInterface(aIID, aResult);
00172 }
00173 
00174 // Constructor for a command table that is pref-filled with HTML editor commands
00175 static NS_METHOD
00176 nsHTMLEditorCommandTableConstructor(nsISupports *aOuter, REFNSIID aIID, 
00177                                               void **aResult)
00178 {
00179   nsresult rv;
00180   nsCOMPtr<nsIControllerCommandTable> commandTable =
00181       do_CreateInstance(NS_CONTROLLERCOMMANDTABLE_CONTRACTID, &rv);
00182   if (NS_FAILED(rv)) return rv;
00183   
00184   rv = nsComposerController::RegisterHTMLEditorCommands(commandTable);
00185   if (NS_FAILED(rv)) return rv;
00186   
00187   // we don't know here whether we're being created as an instance,
00188   // or a service, so we can't become immutable
00189   
00190   return commandTable->QueryInterface(aIID, aResult);
00191 }
00192 
00193 
00194 // Constructor for a command table that is pref-filled with HTML editor doc state commands
00195 static NS_METHOD
00196 nsHTMLEditorDocStateCommandTableConstructor(nsISupports *aOuter, REFNSIID aIID, 
00197                                               void **aResult)
00198 {
00199   nsresult rv;
00200   nsCOMPtr<nsIControllerCommandTable> commandTable =
00201       do_CreateInstance(NS_CONTROLLERCOMMANDTABLE_CONTRACTID, &rv);
00202   if (NS_FAILED(rv)) return rv;
00203   
00204   rv = nsComposerController::RegisterEditorDocStateCommands(commandTable);
00205   if (NS_FAILED(rv)) return rv;
00206   
00207   // we don't know here whether we're being created as an instance,
00208   // or a service, so we can't become immutable
00209   
00210   return commandTable->QueryInterface(aIID, aResult);
00211 }
00212 
00214 // Define a table of CIDs implemented by this module along with other
00215 // information like the function to create an instance, contractid, and
00216 // class name.
00217 //
00218 static const nsModuleComponentInfo components[] = {
00219 
00220     { "HTML Editor Controller", NS_HTMLEDITORCONTROLLER_CID,
00221       "@mozilla.org/editor/htmleditorcontroller;1",
00222       nsHTMLEditorControllerConstructor, },
00223 
00224     { "HTML Editor DocState Controller", NS_EDITORDOCSTATECONTROLLER_CID,
00225       "@mozilla.org/editor/editordocstatecontroller;1",
00226       nsHTMLEditorDocStateControllerConstructor, },
00227 
00228     { "HTML Editor command table", NS_HTMLEDITOR_COMMANDTABLE_CID,
00229       "", // no point using a contract-ID
00230       nsHTMLEditorCommandTableConstructor, },
00231 
00232     { "HTML Editor doc state command table", NS_HTMLEDITOR_DOCSTATE_COMMANDTABLE_CID,
00233       "", // no point using a contract-ID
00234       nsHTMLEditorDocStateCommandTableConstructor, },
00235 
00236     { "Editing Session", NS_EDITINGSESSION_CID,
00237       "@mozilla.org/editor/editingsession;1", nsEditingSessionConstructor, },
00238 
00239     { "Editor Spell Checker", NS_EDITORSPELLCHECK_CID,
00240       "@mozilla.org/editor/editorspellchecker;1",
00241       nsEditorSpellCheckConstructor,},
00242 
00243     { "TxtSrv Filter", NS_COMPOSERTXTSRVFILTER_CID,
00244       COMPOSER_TXTSRVFILTER_CONTRACTID,
00245       nsComposeTxtSrvFilterConstructorForComposer, },
00246 
00247     { "TxtSrv Filter For Mail", NS_COMPOSERTXTSRVFILTERMAIL_CID,
00248       COMPOSER_TXTSRVFILTERMAIL_CONTRACTID,
00249       nsComposeTxtSrvFilterConstructorForMail, },
00250 };
00251 
00253 // Implement the NSGetModule() exported function for your module
00254 // and the entire implementation of the module object.
00255 //
00256 NS_IMPL_NSGETMODULE(nsComposerModule, components)