~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/extensions/xmlterm/base/mozXMLTermShell.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * The contents of this file are subject to the Mozilla Public
 
3
 * License Version 1.1 (the "MPL"); you may not use this file
 
4
 * except in compliance with the MPL. You may obtain a copy of
 
5
 * the MPL at http://www.mozilla.org/MPL/
 
6
 * 
 
7
 * Software distributed under the MPL is distributed on an "AS
 
8
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 
9
 * implied. See the MPL for the specific language governing
 
10
 * rights and limitations under the MPL.
 
11
 * 
 
12
 * The Original Code is XMLterm.
 
13
 * 
 
14
 * The Initial Developer of the Original Code is Ramalingam Saravanan.
 
15
 * Portions created by Ramalingam Saravanan <svn@xmlterm.org> are
 
16
 * Copyright (C) 1999 Ramalingam Saravanan. All Rights Reserved.
 
17
 * 
 
18
 * Contributor(s):
 
19
 *   Pierre Phaneuf <pp@ludusdesign.com>
 
20
 */
 
21
 
 
22
// mozXMLTermShell.cpp: implementation of mozIXMLTermShell
 
23
// providing an XPCONNECT wrapper to the XMLTerminal interface,
 
24
// thus allowing easy (and controlled) access from scripts
 
25
 
 
26
#include <stdio.h>
 
27
 
 
28
#undef NEW_XMLTERM_IMP
 
29
 
 
30
#include "nscore.h"
 
31
#include "nsCOMPtr.h"
 
32
#include "nsString.h"
 
33
 
 
34
#include "nsIDocumentViewer.h"
 
35
#include "nsIDocument.h"
 
36
 
 
37
#include "nsIDocShell.h"
 
38
#include "nsIPresShell.h"
 
39
#include "nsIPresContext.h"
 
40
#include "nsIScriptGlobalObject.h"
 
41
 
 
42
#include "nsIServiceManager.h"
 
43
 
 
44
#include "nsIAppShellService.h"
 
45
#include "nsAppShellCIDs.h"
 
46
 
 
47
#include "nsIDOMDocument.h"
 
48
#include "nsISelection.h"
 
49
#include "nsIDOMWindowInternal.h"
 
50
 
 
51
#include "mozXMLT.h"
 
52
#include "mozLineTerm.h"
 
53
#include "mozXMLTermUtils.h"
 
54
#include "mozXMLTermShell.h"
 
55
 
 
56
// Define Class IDs
 
57
static NS_DEFINE_IID(kAppShellServiceCID,    NS_APPSHELL_SERVICE_CID);
 
58
 
 
59
 
 
60
/////////////////////////////////////////////////////////////////////////
 
61
// mozXMLTermShell implementation
 
62
/////////////////////////////////////////////////////////////////////////
 
63
 
 
64
NS_GENERIC_FACTORY_CONSTRUCTOR(mozXMLTermShell)
 
65
 
 
66
NS_IMPL_THREADSAFE_ISUPPORTS1(mozXMLTermShell, 
 
67
                              mozIXMLTermShell)
 
68
 
 
69
PRBool mozXMLTermShell::mLoggingInitialized = PR_FALSE;
 
70
 
 
71
NS_METHOD
 
72
mozXMLTermShell::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult)
 
73
{
 
74
 
 
75
  if (!mLoggingInitialized) {
 
76
    // Set initial debugging message level for XMLterm
 
77
    int messageLevel = 0;
 
78
    char* debugStr = (char*) PR_GetEnv("XMLT_DEBUG");
 
79
                      
 
80
    if (debugStr && (strlen(debugStr) == 1)) {
 
81
      messageLevel = 98;
 
82
      debugStr = nsnull;
 
83
    }
 
84
 
 
85
    tlog_init(stderr);
 
86
    tlog_set_level(XMLT_TLOG_MODULE, messageLevel, debugStr);
 
87
    mLoggingInitialized = PR_TRUE;
 
88
  }
 
89
 
 
90
  return mozXMLTermShellConstructor( aOuter,
 
91
                                  aIID,
 
92
                                  aResult );
 
93
}
 
94
 
 
95
NS_METHOD
 
96
mozXMLTermShell::RegisterProc(nsIComponentManager *aCompMgr,
 
97
                              nsIFile *aPath,
 
98
                              const char *registryLocation,
 
99
                              const char *componentType,
 
100
                              const nsModuleComponentInfo *info)
 
101
{
 
102
  // Component specific actions at registration time
 
103
  PR_LogPrint("mozXMLTermShell::RegisterProc: registered mozXMLTermShell\n");
 
104
  return NS_OK;
 
105
}
 
106
 
 
107
NS_METHOD
 
108
mozXMLTermShell::UnregisterProc(nsIComponentManager *aCompMgr,
 
109
                                nsIFile *aPath,
 
110
                                const char *registryLocation,
 
111
                                const nsModuleComponentInfo *info)
 
112
{
 
113
  // Component specific actions at unregistration time
 
114
  return NS_OK;  // Return value is not used
 
115
}
 
116
 
 
117
 
 
118
mozXMLTermShell::mozXMLTermShell() :
 
119
  mInitialized(PR_FALSE),
 
120
  mContentWindow(nsnull),
 
121
  mContentAreaDocShell(nsnull),
 
122
  mXMLTerminal(nsnull)
 
123
{
 
124
}
 
125
 
 
126
mozXMLTermShell::~mozXMLTermShell()
 
127
{
 
128
  Finalize();
 
129
}
 
130
 
 
131
 
 
132
 
 
133
NS_IMETHODIMP mozXMLTermShell::GetCurrentEntryNumber(PRInt32 *aNumber)
 
134
{
 
135
  if (mXMLTerminal) {
 
136
    return mXMLTerminal->GetCurrentEntryNumber(aNumber);
 
137
  } else {
 
138
    return NS_ERROR_NOT_INITIALIZED;
 
139
  }
 
140
}
 
141
 
 
142
 
 
143
/** Sets command history buffer count
 
144
 * @param aHistory history buffer count
 
145
 * @param aCookie document.cookie string for authentication
 
146
 */
 
147
NS_IMETHODIMP mozXMLTermShell::SetHistory(PRInt32 aHistory,
 
148
                                          const PRUnichar* aCookie)
 
149
{
 
150
  if (mXMLTerminal) {
 
151
    nsresult result;
 
152
    PRBool matchesCookie;
 
153
    result = mXMLTerminal->MatchesCookie(aCookie, &matchesCookie);
 
154
    if (NS_FAILED(result) || !matchesCookie)
 
155
      return NS_ERROR_FAILURE;
 
156
 
 
157
    return mXMLTerminal->SetHistory(aHistory);
 
158
 
 
159
  } else {
 
160
    return NS_ERROR_NOT_INITIALIZED;
 
161
  }
 
162
}
 
163
 
 
164
 
 
165
/** Sets command prompt
 
166
 * @param aPrompt command prompt string (HTML)
 
167
 * @param aCookie document.cookie string for authentication
 
168
 */
 
169
NS_IMETHODIMP mozXMLTermShell::SetPrompt(const PRUnichar* aPrompt,
 
170
                                         const PRUnichar* aCookie)
 
171
{
 
172
  if (mXMLTerminal) {
 
173
    nsresult result;
 
174
    PRBool matchesCookie;
 
175
    result = mXMLTerminal->MatchesCookie(aCookie, &matchesCookie);
 
176
    if (NS_FAILED(result) || !matchesCookie)
 
177
      return NS_ERROR_FAILURE;
 
178
 
 
179
    return mXMLTerminal->SetPrompt(aPrompt);
 
180
 
 
181
  } else {
 
182
    return NS_ERROR_NOT_INITIALIZED;
 
183
  }
 
184
}
 
185
 
 
186
 
 
187
/** Exports HTML to file, with META REFRESH, if refreshSeconds is non-zero.
 
188
 * Nothing is done if display has not changed since last export, unless
 
189
 * forceExport is true. Returns true if export actually takes place.
 
190
 * If filename is a null string, HTML is written to STDERR.
 
191
 */
 
192
NS_IMETHODIMP mozXMLTermShell::ExportHTML(const PRUnichar* aFilename,
 
193
                                          PRInt32 permissions,
 
194
                                          const PRUnichar* style,
 
195
                                          PRUint32 refreshSeconds,
 
196
                                          PRBool forceExport,
 
197
                                          const PRUnichar* aCookie,
 
198
                                          PRBool* exported)
 
199
{
 
200
  if (!mXMLTerminal)
 
201
    return NS_ERROR_NOT_INITIALIZED;
 
202
 
 
203
  nsresult result;
 
204
  PRBool matchesCookie;
 
205
  result = mXMLTerminal->MatchesCookie(aCookie, &matchesCookie);
 
206
  if (NS_FAILED(result) || !matchesCookie)
 
207
    return NS_ERROR_FAILURE;
 
208
 
 
209
  return mXMLTerminal->ExportHTML( aFilename, permissions, style,
 
210
                                   refreshSeconds, forceExport,
 
211
                                   exported);
 
212
}
 
213
 
 
214
/** Ignore key press events
 
215
 * (workaround for form input being transmitted to xmlterm)
 
216
 * @param aIgnore ignore flag (PR_TRUE/PR_FALSE)
 
217
 * @param aCookie document.cookie string for authentication
 
218
 */
 
219
NS_IMETHODIMP mozXMLTermShell::IgnoreKeyPress(PRBool aIgnore,
 
220
                                              const PRUnichar* aCookie)
 
221
{
 
222
  if (mXMLTerminal) {
 
223
    nsresult result;
 
224
    PRBool matchesCookie;
 
225
    result = mXMLTerminal->MatchesCookie(aCookie, &matchesCookie);
 
226
    if (NS_FAILED(result) || !matchesCookie)
 
227
      return NS_ERROR_FAILURE;
 
228
 
 
229
    return mXMLTerminal->SetKeyIgnore(aIgnore);
 
230
 
 
231
  } else {
 
232
    return NS_ERROR_NOT_INITIALIZED;
 
233
  }
 
234
}
 
235
 
 
236
 
 
237
// Initialize XMLTermShell
 
238
NS_IMETHODIMP    
 
239
mozXMLTermShell::Init(nsIDOMWindowInternal* aContentWin,
 
240
                      const PRUnichar* URL,
 
241
                      const PRUnichar* args)
 
242
{
 
243
  nsresult result;
 
244
 
 
245
  XMLT_LOG(mozXMLTermShell::Init,10,("\n"));
 
246
 
 
247
  if (mInitialized)
 
248
    return NS_ERROR_ALREADY_INITIALIZED;
 
249
 
 
250
  if (!aContentWin)
 
251
      return NS_ERROR_NULL_POINTER;
 
252
 
 
253
  mInitialized = PR_TRUE;
 
254
 
 
255
  mContentWindow = aContentWin;  // no addref
 
256
 
 
257
  nsCOMPtr<nsIScriptGlobalObject> globalObj = do_QueryInterface(mContentWindow,
 
258
                                                                &result);
 
259
  if (NS_FAILED(result) || !globalObj)
 
260
    return NS_ERROR_FAILURE;
 
261
 
 
262
  nsIDocShell *docShell = globalObj->GetDocShell();
 
263
  if (!docShell)
 
264
    return NS_ERROR_FAILURE;
 
265
    
 
266
  mContentAreaDocShell = docShell;  // SVN: does this assignment addref?
 
267
 
 
268
  // Create XMLTerminal
 
269
  nsCOMPtr<mozIXMLTerminal> newXMLTerminal = do_CreateInstance(
 
270
                                                MOZXMLTERMINAL_CONTRACTID,
 
271
                                                &result);
 
272
  if(NS_FAILED(result))
 
273
    return result;
 
274
 
 
275
  // Initialize XMLTerminal with non-owning reference to us
 
276
  result = newXMLTerminal->Init(mContentAreaDocShell, this, URL, args);
 
277
 
 
278
  if (NS_FAILED(result))
 
279
    return result;
 
280
 
 
281
  mXMLTerminal = newXMLTerminal;
 
282
  return NS_OK;
 
283
}
 
284
 
 
285
 
 
286
/** Closes XMLterm, freeing resources
 
287
 * @param aCookie document.cookie string for authentication
 
288
 */
 
289
NS_IMETHODIMP
 
290
mozXMLTermShell::Close(const PRUnichar* aCookie)
 
291
{
 
292
  XMLT_LOG(mozXMLTermShell::Close,10,("\n"));
 
293
 
 
294
  if (mInitialized && mXMLTerminal) {
 
295
    nsresult result;
 
296
    PRBool matchesCookie;
 
297
    result = mXMLTerminal->MatchesCookie(aCookie, &matchesCookie);
 
298
    if (NS_FAILED(result) || !matchesCookie)
 
299
      return NS_ERROR_FAILURE;
 
300
 
 
301
    Finalize();
 
302
  }
 
303
 
 
304
  return NS_OK;
 
305
}
 
306
 
 
307
 
 
308
// De-initialize XMLTermShell and free resources
 
309
NS_IMETHODIMP
 
310
mozXMLTermShell::Finalize(void)
 
311
{
 
312
  if (!mInitialized)
 
313
    return NS_OK;
 
314
 
 
315
  XMLT_LOG(mozXMLTermShell::Finalize,10,("\n"));
 
316
 
 
317
  mInitialized = PR_FALSE;
 
318
 
 
319
  if (mXMLTerminal) {
 
320
    // Finalize and release reference to XMLTerm object owned by us
 
321
    mXMLTerminal->Finalize();
 
322
    mXMLTerminal = nsnull;
 
323
  }
 
324
 
 
325
  mContentAreaDocShell = nsnull;
 
326
  mContentWindow =       nsnull;
 
327
 
 
328
  XMLT_LOG(mozXMLTermShell::Finalize,12,("END\n"));
 
329
 
 
330
  return NS_OK;
 
331
}
 
332
 
 
333
 
 
334
// Poll for readable data from XMLTerminal
 
335
NS_IMETHODIMP mozXMLTermShell::Poll(void)
 
336
{
 
337
  if (!mXMLTerminal)
 
338
    return NS_ERROR_NOT_INITIALIZED;
 
339
 
 
340
  return mXMLTerminal->Poll();
 
341
}
 
342
 
 
343
 
 
344
/** Resizes XMLterm to match a resized window.
 
345
 */
 
346
NS_IMETHODIMP mozXMLTermShell::Resize(void)
 
347
{
 
348
  if (!mXMLTerminal)
 
349
    return NS_ERROR_NOT_INITIALIZED;
 
350
 
 
351
  return mXMLTerminal->Resize();
 
352
}
 
353
 
 
354
 
 
355
// Send string to LineTerm as if the user had typed it
 
356
NS_IMETHODIMP mozXMLTermShell::SendText(const PRUnichar* aString,
 
357
                                        const PRUnichar* aCookie)
 
358
{
 
359
  if (!mXMLTerminal)
 
360
    return NS_ERROR_FAILURE;
 
361
 
 
362
  XMLT_LOG(mozXMLTermShell::SendText,10,("\n"));
 
363
 
 
364
  return mXMLTerminal->SendText(aString, aCookie);
 
365
}
 
366
 
 
367
 
 
368
// Exit XMLTerm window
 
369
NS_IMETHODIMP    
 
370
mozXMLTermShell::Exit()
 
371
{  
 
372
  nsIAppShellService* appShell = nsnull;
 
373
 
 
374
  XMLT_LOG(mozXMLTermShell::Exit,10,("\n"));
 
375
 
 
376
  // Create the Application Shell instance...
 
377
  nsresult result = nsServiceManager::GetService(kAppShellServiceCID,
 
378
                                                 NS_GET_IID(nsIAppShellService),
 
379
                                                 (nsISupports**)&appShell);
 
380
  if (NS_SUCCEEDED(result)) {
 
381
    nsServiceManager::ReleaseService(kAppShellServiceCID, appShell);
 
382
  } 
 
383
  return NS_OK;
 
384
}