1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Netscape Communications Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 1998
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Michael Judge <mjudge@netscape.com>
24 : * Charles Manske <cmanske@netscape.com>
25 : *
26 : * Alternatively, the contents of this file may be used under the terms of
27 : * either of the GNU General Public License Version 2 or later (the "GPL"),
28 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 : * in which case the provisions of the GPL or the LGPL are applicable instead
30 : * of those above. If you wish to allow use of your version of this file only
31 : * under the terms of either the GPL or the LGPL, and not to allow others to
32 : * use your version of this file under the terms of the MPL, indicate your
33 : * decision by deleting the provisions above and replace them with the notice
34 : * and other provisions required by the GPL or the LGPL. If you do not delete
35 : * the provisions above, a recipient may use your version of this file under
36 : * the terms of any one of the MPL, the GPL or the LGPL.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 :
40 : #include "mozilla/ModuleUtils.h"
41 :
42 : #include "nsEditingSession.h" // for the CID
43 : #include "nsComposerController.h" // for the CID
44 : #include "nsEditorSpellCheck.h" // for the CID
45 : #include "nsComposeTxtSrvFilter.h"
46 : #include "nsIController.h"
47 : #include "nsIControllerContext.h"
48 : #include "nsIControllerCommandTable.h"
49 :
50 : #include "nsServiceManagerUtils.h"
51 :
52 : #define NS_HTMLEDITOR_COMMANDTABLE_CID \
53 : { 0x13e50d8d, 0x9cee, 0x4ad1, { 0xa3, 0xa2, 0x4a, 0x44, 0x2f, 0xdf, 0x7d, 0xfa } }
54 :
55 : #define NS_HTMLEDITOR_DOCSTATE_COMMANDTABLE_CID \
56 : { 0xa33982d3, 0x1adf, 0x4162, { 0x99, 0x41, 0xf7, 0x34, 0xbc, 0x45, 0xe4, 0xed } }
57 :
58 :
59 : static NS_DEFINE_CID(kHTMLEditorCommandTableCID, NS_HTMLEDITOR_COMMANDTABLE_CID);
60 : static NS_DEFINE_CID(kHTMLEditorDocStateCommandTableCID, NS_HTMLEDITOR_DOCSTATE_COMMANDTABLE_CID);
61 :
62 :
63 : ////////////////////////////////////////////////////////////////////////
64 : // Define the contructor function for the objects
65 : //
66 : // NOTE: This creates an instance of objects by using the default constructor
67 : //
68 :
69 0 : NS_GENERIC_FACTORY_CONSTRUCTOR(nsEditingSession)
70 0 : NS_GENERIC_FACTORY_CONSTRUCTOR(nsEditorSpellCheck)
71 :
72 : // There are no macros that enable us to have 2 constructors
73 : // for the same object
74 : //
75 : // Here we are creating the same object with two different contract IDs
76 : // and then initializing it different.
77 : // Basically, we need to tell the filter whether it is doing mail or not
78 : static nsresult
79 0 : nsComposeTxtSrvFilterConstructor(nsISupports *aOuter, REFNSIID aIID,
80 : void **aResult, bool aIsForMail)
81 : {
82 0 : *aResult = NULL;
83 0 : if (NULL != aOuter)
84 : {
85 0 : return NS_ERROR_NO_AGGREGATION;
86 : }
87 0 : nsComposeTxtSrvFilter * inst = new nsComposeTxtSrvFilter();
88 0 : if (NULL == inst)
89 : {
90 0 : return NS_ERROR_OUT_OF_MEMORY;
91 : }
92 0 : NS_ADDREF(inst);
93 0 : inst->Init(aIsForMail);
94 0 : nsresult rv = inst->QueryInterface(aIID, aResult);
95 0 : NS_RELEASE(inst);
96 0 : return rv;
97 : }
98 :
99 : static nsresult
100 0 : nsComposeTxtSrvFilterConstructorForComposer(nsISupports *aOuter,
101 : REFNSIID aIID,
102 : void **aResult)
103 : {
104 0 : return nsComposeTxtSrvFilterConstructor(aOuter, aIID, aResult, false);
105 : }
106 :
107 : static nsresult
108 0 : nsComposeTxtSrvFilterConstructorForMail(nsISupports *aOuter,
109 : REFNSIID aIID,
110 : void **aResult)
111 : {
112 0 : return nsComposeTxtSrvFilterConstructor(aOuter, aIID, aResult, true);
113 : }
114 :
115 :
116 : // Constructor for a controller set up with a command table specified
117 : // by the CID passed in. This function uses do_GetService to get the
118 : // command table, so that every controller shares a single command
119 : // table, for space-efficiency.
120 : //
121 : // The only reason to go via the service manager for the command table
122 : // is that it holds onto the singleton for us, avoiding static variables here.
123 : static nsresult
124 0 : CreateControllerWithSingletonCommandTable(const nsCID& inCommandTableCID, nsIController **aResult)
125 : {
126 : nsresult rv;
127 0 : nsCOMPtr<nsIController> controller = do_CreateInstance("@mozilla.org/embedcomp/base-command-controller;1", &rv);
128 0 : NS_ENSURE_SUCCESS(rv, rv);
129 :
130 0 : nsCOMPtr<nsIControllerCommandTable> composerCommandTable = do_GetService(inCommandTableCID, &rv);
131 0 : NS_ENSURE_SUCCESS(rv, rv);
132 :
133 : // this guy is a singleton, so make it immutable
134 0 : composerCommandTable->MakeImmutable();
135 :
136 0 : nsCOMPtr<nsIControllerContext> controllerContext = do_QueryInterface(controller, &rv);
137 0 : NS_ENSURE_SUCCESS(rv, rv);
138 :
139 0 : rv = controllerContext->Init(composerCommandTable);
140 0 : NS_ENSURE_SUCCESS(rv, rv);
141 :
142 0 : *aResult = controller;
143 0 : NS_ADDREF(*aResult);
144 0 : return NS_OK;
145 : }
146 :
147 :
148 : // Here we make an instance of the controller that holds doc state commands.
149 : // We set it up with a singleton command table.
150 : static nsresult
151 0 : nsHTMLEditorDocStateControllerConstructor(nsISupports *aOuter, REFNSIID aIID,
152 : void **aResult)
153 : {
154 0 : nsCOMPtr<nsIController> controller;
155 0 : nsresult rv = CreateControllerWithSingletonCommandTable(kHTMLEditorDocStateCommandTableCID, getter_AddRefs(controller));
156 0 : NS_ENSURE_SUCCESS(rv, rv);
157 :
158 0 : return controller->QueryInterface(aIID, aResult);
159 : }
160 :
161 : // Tere we make an instance of the controller that holds composer commands.
162 : // We set it up with a singleton command table.
163 : static nsresult
164 0 : nsHTMLEditorControllerConstructor(nsISupports *aOuter, REFNSIID aIID, void **aResult)
165 : {
166 0 : nsCOMPtr<nsIController> controller;
167 0 : nsresult rv = CreateControllerWithSingletonCommandTable(kHTMLEditorCommandTableCID, getter_AddRefs(controller));
168 0 : NS_ENSURE_SUCCESS(rv, rv);
169 :
170 0 : return controller->QueryInterface(aIID, aResult);
171 : }
172 :
173 : // Constructor for a command table that is pref-filled with HTML editor commands
174 : static nsresult
175 0 : nsHTMLEditorCommandTableConstructor(nsISupports *aOuter, REFNSIID aIID,
176 : void **aResult)
177 : {
178 : nsresult rv;
179 : nsCOMPtr<nsIControllerCommandTable> commandTable =
180 0 : do_CreateInstance(NS_CONTROLLERCOMMANDTABLE_CONTRACTID, &rv);
181 0 : NS_ENSURE_SUCCESS(rv, rv);
182 :
183 0 : rv = nsComposerController::RegisterHTMLEditorCommands(commandTable);
184 0 : NS_ENSURE_SUCCESS(rv, rv);
185 :
186 : // we don't know here whether we're being created as an instance,
187 : // or a service, so we can't become immutable
188 :
189 0 : return commandTable->QueryInterface(aIID, aResult);
190 : }
191 :
192 :
193 : // Constructor for a command table that is pref-filled with HTML editor doc state commands
194 : static nsresult
195 0 : nsHTMLEditorDocStateCommandTableConstructor(nsISupports *aOuter, REFNSIID aIID,
196 : void **aResult)
197 : {
198 : nsresult rv;
199 : nsCOMPtr<nsIControllerCommandTable> commandTable =
200 0 : do_CreateInstance(NS_CONTROLLERCOMMANDTABLE_CONTRACTID, &rv);
201 0 : NS_ENSURE_SUCCESS(rv, rv);
202 :
203 0 : rv = nsComposerController::RegisterEditorDocStateCommands(commandTable);
204 0 : NS_ENSURE_SUCCESS(rv, rv);
205 :
206 : // we don't know here whether we're being created as an instance,
207 : // or a service, so we can't become immutable
208 :
209 0 : return commandTable->QueryInterface(aIID, aResult);
210 : }
211 :
212 : NS_DEFINE_NAMED_CID(NS_HTMLEDITORCONTROLLER_CID);
213 : NS_DEFINE_NAMED_CID(NS_EDITORDOCSTATECONTROLLER_CID);
214 : NS_DEFINE_NAMED_CID(NS_HTMLEDITOR_COMMANDTABLE_CID);
215 : NS_DEFINE_NAMED_CID(NS_HTMLEDITOR_DOCSTATE_COMMANDTABLE_CID);
216 : NS_DEFINE_NAMED_CID(NS_EDITINGSESSION_CID);
217 : NS_DEFINE_NAMED_CID(NS_EDITORSPELLCHECK_CID);
218 : NS_DEFINE_NAMED_CID(NS_COMPOSERTXTSRVFILTER_CID);
219 : NS_DEFINE_NAMED_CID(NS_COMPOSERTXTSRVFILTERMAIL_CID);
220 :
221 :
222 : static const mozilla::Module::CIDEntry kComposerCIDs[] = {
223 : { &kNS_HTMLEDITORCONTROLLER_CID, false, NULL, nsHTMLEditorControllerConstructor },
224 : { &kNS_EDITORDOCSTATECONTROLLER_CID, false, NULL, nsHTMLEditorDocStateControllerConstructor },
225 : { &kNS_HTMLEDITOR_COMMANDTABLE_CID, false, NULL, nsHTMLEditorCommandTableConstructor },
226 : { &kNS_HTMLEDITOR_DOCSTATE_COMMANDTABLE_CID, false, NULL, nsHTMLEditorDocStateCommandTableConstructor },
227 : { &kNS_EDITINGSESSION_CID, false, NULL, nsEditingSessionConstructor },
228 : { &kNS_EDITORSPELLCHECK_CID, false, NULL, nsEditorSpellCheckConstructor },
229 : { &kNS_COMPOSERTXTSRVFILTER_CID, false, NULL, nsComposeTxtSrvFilterConstructorForComposer },
230 : { &kNS_COMPOSERTXTSRVFILTERMAIL_CID, false, NULL, nsComposeTxtSrvFilterConstructorForMail },
231 : { NULL }
232 : };
233 :
234 : static const mozilla::Module::ContractIDEntry kComposerContracts[] = {
235 : { "@mozilla.org/editor/htmleditorcontroller;1", &kNS_HTMLEDITORCONTROLLER_CID },
236 : { "@mozilla.org/editor/editordocstatecontroller;1", &kNS_EDITORDOCSTATECONTROLLER_CID },
237 : { "@mozilla.org/editor/editingsession;1", &kNS_EDITINGSESSION_CID },
238 : { "@mozilla.org/editor/editorspellchecker;1", &kNS_EDITORSPELLCHECK_CID },
239 : { COMPOSER_TXTSRVFILTER_CONTRACTID, &kNS_COMPOSERTXTSRVFILTER_CID },
240 : { COMPOSER_TXTSRVFILTERMAIL_CONTRACTID, &kNS_COMPOSERTXTSRVFILTERMAIL_CID },
241 : { NULL }
242 : };
243 :
244 : static const mozilla::Module kComposerModule = {
245 : mozilla::Module::kVersion,
246 : kComposerCIDs,
247 : kComposerContracts
248 : };
249 :
250 : NSMODULE_DEFN(nsComposerModule) = &kComposerModule;
|