Back to index

lightning-sunbird  0.9+nobinonly
mozXMLTermShell.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is XMLterm.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Ramalingam Saravanan.
00018  * Portions created by the Initial Developer are Copyright (C) 1999
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   Pierre Phaneuf <pp@ludusdesign.com>
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 // mozXMLTermShell.cpp: implementation of mozIXMLTermShell
00039 // providing an XPCONNECT wrapper to the XMLTerminal interface,
00040 // thus allowing easy (and controlled) access from scripts
00041 
00042 #include <stdio.h>
00043 
00044 #undef NEW_XMLTERM_IMP
00045 
00046 #include "nscore.h"
00047 #include "nsCOMPtr.h"
00048 #include "nsString.h"
00049 
00050 #include "nsIDocumentViewer.h"
00051 #include "nsIDocument.h"
00052 
00053 #include "nsIDocShell.h"
00054 #include "nsIPresShell.h"
00055 #include "nsPresContext.h"
00056 #include "nsIScriptGlobalObject.h"
00057 
00058 #include "nsIServiceManager.h"
00059 
00060 #include "nsIAppShellService.h"
00061 
00062 #include "nsIDOMDocument.h"
00063 #include "nsISelection.h"
00064 #include "nsIDOMWindowInternal.h"
00065 
00066 #include "mozXMLT.h"
00067 #include "mozLineTerm.h"
00068 #include "mozXMLTermUtils.h"
00069 #include "mozXMLTermShell.h"
00070 #include "nsAppShellCID.h"
00071 
00073 // mozXMLTermShell implementation
00075 
00076 NS_GENERIC_FACTORY_CONSTRUCTOR(mozXMLTermShell)
00077 
00078 NS_IMPL_THREADSAFE_ISUPPORTS1(mozXMLTermShell, 
00079                               mozIXMLTermShell)
00080 
00081 PRBool mozXMLTermShell::mLoggingInitialized = PR_FALSE;
00082 
00083 NS_METHOD
00084 mozXMLTermShell::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult)
00085 {
00086 
00087   if (!mLoggingInitialized) {
00088     // Set initial debugging message level for XMLterm
00089     int messageLevel = 0;
00090     char* debugStr = (char*) PR_GetEnv("XMLT_DEBUG");
00091                       
00092     if (debugStr && (strlen(debugStr) == 1)) {
00093       messageLevel = 98;
00094       debugStr = nsnull;
00095     }
00096 
00097     tlog_init(stderr);
00098     tlog_set_level(XMLT_TLOG_MODULE, messageLevel, debugStr);
00099     mLoggingInitialized = PR_TRUE;
00100   }
00101 
00102   return mozXMLTermShellConstructor( aOuter,
00103                                   aIID,
00104                                   aResult );
00105 }
00106 
00107 NS_METHOD
00108 mozXMLTermShell::RegisterProc(nsIComponentManager *aCompMgr,
00109                               nsIFile *aPath,
00110                               const char *registryLocation,
00111                               const char *componentType,
00112                               const nsModuleComponentInfo *info)
00113 {
00114   // Component specific actions at registration time
00115   PR_LogPrint("mozXMLTermShell::RegisterProc: registered mozXMLTermShell\n");
00116   return NS_OK;
00117 }
00118 
00119 NS_METHOD
00120 mozXMLTermShell::UnregisterProc(nsIComponentManager *aCompMgr,
00121                                 nsIFile *aPath,
00122                                 const char *registryLocation,
00123                                 const nsModuleComponentInfo *info)
00124 {
00125   // Component specific actions at unregistration time
00126   return NS_OK;  // Return value is not used
00127 }
00128 
00129 
00130 mozXMLTermShell::mozXMLTermShell() :
00131   mInitialized(PR_FALSE),
00132   mContentWindow(nsnull),
00133   mContentAreaDocShell(nsnull),
00134   mXMLTerminal(nsnull)
00135 {
00136 }
00137 
00138 mozXMLTermShell::~mozXMLTermShell()
00139 {
00140   Finalize();
00141 }
00142 
00143 
00144 
00145 NS_IMETHODIMP mozXMLTermShell::GetCurrentEntryNumber(PRInt32 *aNumber)
00146 {
00147   if (mXMLTerminal) {
00148     return mXMLTerminal->GetCurrentEntryNumber(aNumber);
00149   } else {
00150     return NS_ERROR_NOT_INITIALIZED;
00151   }
00152 }
00153 
00154 
00159 NS_IMETHODIMP mozXMLTermShell::SetHistory(PRInt32 aHistory,
00160                                           const PRUnichar* aCookie)
00161 {
00162   if (mXMLTerminal) {
00163     nsresult result;
00164     PRBool matchesCookie;
00165     result = mXMLTerminal->MatchesCookie(aCookie, &matchesCookie);
00166     if (NS_FAILED(result) || !matchesCookie)
00167       return NS_ERROR_FAILURE;
00168 
00169     return mXMLTerminal->SetHistory(aHistory);
00170 
00171   } else {
00172     return NS_ERROR_NOT_INITIALIZED;
00173   }
00174 }
00175 
00176 
00181 NS_IMETHODIMP mozXMLTermShell::SetPrompt(const PRUnichar* aPrompt,
00182                                          const PRUnichar* aCookie)
00183 {
00184   if (mXMLTerminal) {
00185     nsresult result;
00186     PRBool matchesCookie;
00187     result = mXMLTerminal->MatchesCookie(aCookie, &matchesCookie);
00188     if (NS_FAILED(result) || !matchesCookie)
00189       return NS_ERROR_FAILURE;
00190 
00191     return mXMLTerminal->SetPrompt(aPrompt);
00192 
00193   } else {
00194     return NS_ERROR_NOT_INITIALIZED;
00195   }
00196 }
00197 
00198 
00204 NS_IMETHODIMP mozXMLTermShell::ExportHTML(const PRUnichar* aFilename,
00205                                           PRInt32 permissions,
00206                                           const PRUnichar* style,
00207                                           PRUint32 refreshSeconds,
00208                                           PRBool forceExport,
00209                                           const PRUnichar* aCookie,
00210                                           PRBool* exported)
00211 {
00212   if (!mXMLTerminal)
00213     return NS_ERROR_NOT_INITIALIZED;
00214 
00215   nsresult result;
00216   PRBool matchesCookie;
00217   result = mXMLTerminal->MatchesCookie(aCookie, &matchesCookie);
00218   if (NS_FAILED(result) || !matchesCookie)
00219     return NS_ERROR_FAILURE;
00220 
00221   return mXMLTerminal->ExportHTML( aFilename, permissions, style,
00222                                    refreshSeconds, forceExport,
00223                                    exported);
00224 }
00225 
00231 NS_IMETHODIMP mozXMLTermShell::IgnoreKeyPress(PRBool aIgnore,
00232                                               const PRUnichar* aCookie)
00233 {
00234   if (mXMLTerminal) {
00235     nsresult result;
00236     PRBool matchesCookie;
00237     result = mXMLTerminal->MatchesCookie(aCookie, &matchesCookie);
00238     if (NS_FAILED(result) || !matchesCookie)
00239       return NS_ERROR_FAILURE;
00240 
00241     return mXMLTerminal->SetKeyIgnore(aIgnore);
00242 
00243   } else {
00244     return NS_ERROR_NOT_INITIALIZED;
00245   }
00246 }
00247 
00248 
00249 // Initialize XMLTermShell
00250 NS_IMETHODIMP    
00251 mozXMLTermShell::Init(nsIDOMWindowInternal* aContentWin,
00252                       const PRUnichar* URL,
00253                       const PRUnichar* args)
00254 {
00255   nsresult result;
00256 
00257   XMLT_LOG(mozXMLTermShell::Init,10,("\n"));
00258 
00259   if (mInitialized)
00260     return NS_ERROR_ALREADY_INITIALIZED;
00261 
00262   if (!aContentWin)
00263       return NS_ERROR_NULL_POINTER;
00264 
00265   mInitialized = PR_TRUE;
00266 
00267   mContentWindow = aContentWin;  // no addref
00268 
00269   nsCOMPtr<nsIScriptGlobalObject> globalObj = do_QueryInterface(mContentWindow,
00270                                                                 &result);
00271   if (NS_FAILED(result) || !globalObj)
00272     return NS_ERROR_FAILURE;
00273 
00274   nsIDocShell *docShell = globalObj->GetDocShell();
00275   if (!docShell)
00276     return NS_ERROR_FAILURE;
00277     
00278   mContentAreaDocShell = docShell;  // SVN: does this assignment addref?
00279 
00280   // Create XMLTerminal
00281   nsCOMPtr<mozIXMLTerminal> newXMLTerminal = do_CreateInstance(
00282                                                 MOZXMLTERMINAL_CONTRACTID,
00283                                                 &result);
00284   if(NS_FAILED(result))
00285     return result;
00286 
00287   // Initialize XMLTerminal with non-owning reference to us
00288   result = newXMLTerminal->Init(mContentAreaDocShell, this, URL, args);
00289 
00290   if (NS_FAILED(result))
00291     return result;
00292 
00293   mXMLTerminal = newXMLTerminal;
00294   return NS_OK;
00295 }
00296 
00297 
00301 NS_IMETHODIMP
00302 mozXMLTermShell::Close(const PRUnichar* aCookie)
00303 {
00304   XMLT_LOG(mozXMLTermShell::Close,10,("\n"));
00305 
00306   if (mInitialized && mXMLTerminal) {
00307     nsresult result;
00308     PRBool matchesCookie;
00309     result = mXMLTerminal->MatchesCookie(aCookie, &matchesCookie);
00310     if (NS_FAILED(result) || !matchesCookie)
00311       return NS_ERROR_FAILURE;
00312 
00313     Finalize();
00314   }
00315 
00316   return NS_OK;
00317 }
00318 
00319 
00320 // De-initialize XMLTermShell and free resources
00321 NS_IMETHODIMP
00322 mozXMLTermShell::Finalize(void)
00323 {
00324   if (!mInitialized)
00325     return NS_OK;
00326 
00327   XMLT_LOG(mozXMLTermShell::Finalize,10,("\n"));
00328 
00329   mInitialized = PR_FALSE;
00330 
00331   if (mXMLTerminal) {
00332     // Finalize and release reference to XMLTerm object owned by us
00333     mXMLTerminal->Finalize();
00334     mXMLTerminal = nsnull;
00335   }
00336 
00337   mContentAreaDocShell = nsnull;
00338   mContentWindow =       nsnull;
00339 
00340   XMLT_LOG(mozXMLTermShell::Finalize,12,("END\n"));
00341 
00342   return NS_OK;
00343 }
00344 
00345 
00346 // Poll for readable data from XMLTerminal
00347 NS_IMETHODIMP mozXMLTermShell::Poll(void)
00348 {
00349   if (!mXMLTerminal)
00350     return NS_ERROR_NOT_INITIALIZED;
00351 
00352   return mXMLTerminal->Poll();
00353 }
00354 
00355 
00358 NS_IMETHODIMP mozXMLTermShell::Resize(void)
00359 {
00360   if (!mXMLTerminal)
00361     return NS_ERROR_NOT_INITIALIZED;
00362 
00363   return mXMLTerminal->Resize();
00364 }
00365 
00366 
00367 // Send string to LineTerm as if the user had typed it
00368 NS_IMETHODIMP mozXMLTermShell::SendText(const PRUnichar* aString,
00369                                         const PRUnichar* aCookie)
00370 {
00371   if (!mXMLTerminal)
00372     return NS_ERROR_FAILURE;
00373 
00374   XMLT_LOG(mozXMLTermShell::SendText,10,("\n"));
00375 
00376   return mXMLTerminal->SendText(aString, aCookie);
00377 }
00378 
00379 
00380 // Exit XMLTerm window
00381 NS_IMETHODIMP    
00382 mozXMLTermShell::Exit()
00383 {  
00384   XMLT_LOG(mozXMLTermShell::Exit,10,("\n"));
00385 
00386   // Create the Application Shell instance...
00387   // XXXbsmedberg what the hell is this?
00388   nsCOMPtr<nsIAppShellService> appShell =
00389       do_GetService(NS_APPSHELLSERVICE_CONTRACTID);
00390   return NS_OK;
00391 }