Back to index

lightning-sunbird  0.9+nobinonly
nsEditorRegistration.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 #include "nsEditorCID.h"
00042 #include "nsEditor.h"
00043 #include "nsPlaintextEditor.h"
00044 #include "nsEditorController.h" //CID
00045 #include "nsIController.h"
00046 #include "nsIControllerContext.h"
00047 #include "nsIControllerCommandTable.h"
00048 #include "nsIServiceManager.h"
00049 #include "nsIObserver.h"
00050 #include "nsIObserverService.h"
00051 #include "nsCOMPtr.h"
00052 #include "nsIParserService.h"
00053 
00054 #ifndef MOZILLA_PLAINTEXT_EDITOR_ONLY
00055 #include "nsHTMLEditor.h"
00056 #include "nsTextServicesDocument.h"
00057 #include "nsTextServicesCID.h"
00058 #endif
00059 
00060 #define NS_EDITORCOMMANDTABLE_CID \
00061 { 0x4f5e62b8, 0xd659, 0x4156, { 0x84, 0xfc, 0x2f, 0x60, 0x99, 0x40, 0x03, 0x69 }}
00062 
00063 static NS_DEFINE_CID(kEditorCommandTableCID, NS_EDITORCOMMANDTABLE_CID);
00064 
00065 PR_STATIC_CALLBACK(nsresult) Initialize(nsIModule* self);
00066 static void Shutdown();
00067 
00068 class EditorShutdownObserver : public nsIObserver
00069 {
00070 public:
00071   NS_DECL_ISUPPORTS
00072   NS_DECL_NSIOBSERVER
00073 };
00074 
00075 NS_IMPL_ISUPPORTS1(EditorShutdownObserver, nsIObserver)
00076 
00077 NS_IMETHODIMP
00078 EditorShutdownObserver::Observe(nsISupports *aSubject,
00079                                 const char *aTopic,
00080                                 const PRUnichar *someData)
00081 {
00082   if (!strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID))
00083     Shutdown();
00084   return NS_OK;
00085 }
00086 
00087 static PRBool gInitialized = PR_FALSE;
00088 
00089 nsIParserService *sParserService;
00090 
00091 PR_STATIC_CALLBACK(nsresult)
00092 Initialize(nsIModule* self)
00093 {
00094   NS_PRECONDITION(!gInitialized, "module already initialized");
00095   if (gInitialized) {
00096     return NS_OK;
00097   }
00098 
00099   gInitialized = PR_TRUE;
00100 
00101   nsresult rv = CallGetService("@mozilla.org/parser/parser-service;1",
00102                                &sParserService);
00103   if (NS_FAILED(rv)) {
00104     gInitialized = PR_FALSE;
00105 
00106     return rv;
00107   }
00108 #ifndef MOZILLA_PLAINTEXT_EDITOR_ONLY
00109   nsEditProperty::RegisterAtoms();
00110   nsTextServicesDocument::RegisterAtoms();
00111 #endif
00112 
00113   // Add our shutdown observer.
00114   nsCOMPtr<nsIObserverService> observerService =
00115     do_GetService("@mozilla.org/observer-service;1");
00116 
00117   if (observerService) {
00118     nsCOMPtr<EditorShutdownObserver> observer =
00119       new EditorShutdownObserver();
00120 
00121     if (!observer) {
00122       Shutdown();
00123 
00124       return NS_ERROR_OUT_OF_MEMORY;
00125     }
00126 
00127     observerService->AddObserver(observer, NS_XPCOM_SHUTDOWN_OBSERVER_ID, PR_FALSE);
00128   } else {
00129     NS_WARNING("Could not get an observer service.  We will leak on shutdown.");
00130   }
00131 
00132   return NS_OK;
00133 }
00134 
00135 // static
00136 void
00137 Shutdown()
00138 {
00139   NS_PRECONDITION(gInitialized, "module not initialized");
00140   if (!gInitialized)
00141     return;
00142 
00143   NS_IF_RELEASE(sParserService);
00144 
00145 #ifndef MOZILLA_PLAINTEXT_EDITOR_ONLY
00146   nsHTMLEditor::Shutdown();
00147   nsTextServicesDocument::Shutdown();
00148 #endif
00149 }
00150 
00151 NS_GENERIC_FACTORY_CONSTRUCTOR(nsPlaintextEditor)
00152 
00153 
00154 // Constructor of a controller which is set up to use, internally, a
00155 // singleton command-table pre-filled with editor commands.
00156 static NS_METHOD
00157 nsEditorControllerConstructor(nsISupports *aOuter, REFNSIID aIID,
00158                                             void **aResult)
00159 {
00160   nsresult rv;
00161   nsCOMPtr<nsIController> controller = do_CreateInstance("@mozilla.org/embedcomp/base-command-controller;1", &rv);
00162   if (NS_FAILED(rv)) return rv;
00163 
00164   nsCOMPtr<nsIControllerCommandTable> editorCommandTable = do_GetService(kEditorCommandTableCID, &rv);
00165   if (NS_FAILED(rv)) return rv;
00166   
00167   // this guy is a singleton, so make it immutable
00168   editorCommandTable->MakeImmutable();
00169   
00170   nsCOMPtr<nsIControllerContext> controllerContext = do_QueryInterface(controller, &rv);
00171   if (NS_FAILED(rv)) return rv;
00172   
00173   rv = controllerContext->Init(editorCommandTable);
00174   if (NS_FAILED(rv)) return rv;
00175   
00176   return controller->QueryInterface(aIID, aResult);
00177 }
00178 
00179 
00180 // Constructor for a command-table pref-filled with editor commands
00181 static NS_METHOD
00182 nsEditorCommandTableConstructor(nsISupports *aOuter, REFNSIID aIID,
00183                                             void **aResult)
00184 {
00185   nsresult rv;
00186   nsCOMPtr<nsIControllerCommandTable> commandTable =
00187       do_CreateInstance(NS_CONTROLLERCOMMANDTABLE_CONTRACTID, &rv);
00188   if (NS_FAILED(rv)) return rv;
00189   
00190   rv = nsEditorController::RegisterEditorCommands(commandTable);
00191   if (NS_FAILED(rv)) return rv;
00192   
00193   // we don't know here whether we're being created as an instance,
00194   // or a service, so we can't become immutable
00195 
00196   return commandTable->QueryInterface(aIID, aResult);
00197 }
00198 
00199 
00200 #ifndef MOZILLA_PLAINTEXT_EDITOR_ONLY
00201 NS_GENERIC_FACTORY_CONSTRUCTOR(nsTextServicesDocument)
00202 #ifdef ENABLE_EDITOR_API_LOG
00203 #include "nsHTMLEditorLog.h"
00204 NS_GENERIC_FACTORY_CONSTRUCTOR(nsHTMLEditorLog)
00205 #else
00206 NS_GENERIC_FACTORY_CONSTRUCTOR(nsHTMLEditor)
00207 #endif
00208 #endif
00209 
00211 // Define a table of CIDs implemented by this module along with other
00212 // information like the function to create an instance, contractid, and
00213 // class name.
00214 //
00215 static const nsModuleComponentInfo components[] = {
00216     { "Text Editor", NS_TEXTEDITOR_CID,
00217       "@mozilla.org/editor/texteditor;1", nsPlaintextEditorConstructor, },
00218 
00219 #ifndef MOZILLA_PLAINTEXT_EDITOR_ONLY
00220 #ifdef ENABLE_EDITOR_API_LOG
00221     { "HTML Editor", NS_HTMLEDITOR_CID,
00222       "@mozilla.org/editor/htmleditor;1", nsHTMLEditorLogConstructor, },
00223 #else
00224     { "HTML Editor", NS_HTMLEDITOR_CID,
00225       "@mozilla.org/editor/htmleditor;1", nsHTMLEditorConstructor, },
00226 #endif
00227 #endif
00228 
00229     { "Editor Controller", NS_EDITORCONTROLLER_CID,
00230       "@mozilla.org/editor/editorcontroller;1",
00231       nsEditorControllerConstructor, },
00232 
00233     { "Editor Command Table", NS_EDITORCOMMANDTABLE_CID,
00234       "",   // no point in using a contract ID
00235       nsEditorCommandTableConstructor, },
00236 
00237 #ifndef MOZILLA_PLAINTEXT_EDITOR_ONLY
00238     { NULL, NS_TEXTSERVICESDOCUMENT_CID,
00239       "@mozilla.org/textservices/textservicesdocument;1",
00240       nsTextServicesDocumentConstructor },
00241 #endif
00242 };
00243 
00245 // Implement the NSGetModule() exported function for your module
00246 // and the entire implementation of the module object.
00247 //
00248 NS_IMPL_NSGETMODULE_WITH_CTOR(nsEditorModule, components, Initialize)