Back to index

lightning-sunbird  0.9+nobinonly
PtMozilla.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 mozilla.org code.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Christopher Blizzard. Portions created by Christopher Blizzard are Copyright (C) Christopher Blizzard.  All Rights Reserved.
00018  * Portions created by the Initial Developer are Copyright (C) 2001
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   Christopher Blizzard <blizzard@mozilla.org>
00023  *   Ramiro Estrugo <ramiro@eazel.com>
00024  *   Brian Edmond <briane@qnx.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * 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 #include <stdlib.h>
00040 
00041 #include "nsCWebBrowser.h"
00042 #include "nsILocalFile.h"
00043 #include "nsISelectionController.h"
00044 #include "nsEmbedAPI.h"
00045 
00046 #include "nsWidgetsCID.h"
00047 #include "nsIAppShell.h"
00048 
00049 #include "nsIContentViewer.h"
00050 #include "nsIMarkupDocumentViewer.h"
00051 
00052 // for profiles
00053 #include <nsProfileDirServiceProvider.h>
00054 
00055 #include "nsIDocumentLoaderFactory.h"
00056 #include "nsILoadGroup.h"
00057 #include "nsIHistoryEntry.h"
00058 
00059 #include "nsIWebBrowserPrint.h"
00060 #include "nsIPrintOptions.h"
00061 
00062 #include "nsIDOMEventReceiver.h"
00063 #include "nsIDOMEventListener.h"
00064 #include "nsIDOMKeyListener.h"
00065 #include "nsIDOMMouseListener.h"
00066 #include "nsIDOMMouseEvent.h"
00067 #include "nsIDOMWindow.h"
00068 #include "nsIObserverService.h"
00069 #include "nsIObserver.h"
00070 #include "nsIDeviceContext.h"
00071 #include "nsIViewManager.h"
00072 #include "nsNetUtil.h"
00073 #include "nsIFocusController.h"
00074 #include <nsIWebBrowserFind.h>
00075 
00076 #include "nsReadableUtils.h"
00077 
00078 #include "nsIPresShell.h"
00079 
00080 #include "nsIDirectoryService.h"
00081 #include "nsAppDirectoryServiceDefs.h"
00082 #include "nsIFile.h"
00083 #include "nsIURIFixup.h"
00084 #include "nsPIDOMWindow.h"
00085 
00086 #include "nsIEventQueueService.h"
00087 #include "nsIServiceManager.h"
00088 #include "nsIComponentRegistrar.h"
00089 #include "nsUnknownContentTypeHandler.h"
00090 
00091 #include "EmbedPrivate.h"
00092 #include "EmbedWindow.h"
00093 #include "EmbedDownload.h"
00094 #include "HeaderSniffer.h"
00095 #include "PromptService.h"
00096 #include "PtMozilla.h"
00097 
00098 //#include "nsUnknownContentTypeHandler.h"
00099 
00100 class WindowChangeObserver : public nsIObserver,
00101                              public nsSupportsWeakReference
00102 {
00103   public:
00104     WindowChangeObserver();
00105 
00106     NS_DECL_ISUPPORTS
00107     NS_DECL_NSIOBSERVER
00108 };
00109 
00110 //-----------------------------------------------------------------------------
00111 // WindowChangeObserver
00112 //-----------------------------------------------------------------------------
00113 
00114 NS_IMPL_THREADSAFE_ISUPPORTS2(WindowChangeObserver, nsIObserver, nsISupportsWeakReference)
00115 
00116 WindowChangeObserver::WindowChangeObserver()
00117 {
00118 }
00119 
00120 NS_IMETHODIMP WindowChangeObserver::Observe(nsISupports *aSubject, const char *aTopic, const PRUnichar *someData)
00121 {
00122     nsresult rv = NS_OK;
00123 
00124     if (strcmp(aTopic, "domwindowclosed") == 0)
00125     {
00126               // This point is too late to prevent the window from closing
00127     }
00128 
00129     return rv;
00130 }
00131 
00132 #ifdef _BUILD_STATIC_BIN
00133 #include "nsStaticComponents.h"
00134 nsresult PR_CALLBACK
00135 ph_getModuleInfo(nsStaticModuleInfo **info, PRUint32 *count);
00136 #endif
00137 
00138 // Macro for converting from nscolor to PtColor_t
00139 // Photon RGB values are stored as 00 RR GG BB
00140 // nscolor RGB values are 00 BB GG RR
00141 #define NS_TO_PH_RGB(ns) (ns & 0xff) << 16 | (ns & 0xff00) | ((ns >> 16) & 0xff)
00142 #define PH_TO_NS_RGB(ns) (ns & 0xff) << 16 | (ns & 0xff00) | ((ns >> 16) & 0xff)
00143 
00144 // Class IDs
00145 static NS_DEFINE_CID(kAppShellCID, NS_APPSHELL_CID);
00146 
00147 #define NS_PROMPTSERVICE_CID \
00148  {0xa2112d6a, 0x0e28, 0x421f, {0xb4, 0x6a, 0x25, 0xc0, 0xb3, 0x8, 0xcb, 0xd0}}
00149 static NS_DEFINE_CID(kPromptServiceCID, NS_PROMPTSERVICE_CID);
00150 
00151 static void mozilla_set_pref( PtWidget_t *widget, char *option, char *value );
00152 static void mozilla_get_pref( PtWidget_t *widget, char *option, char *value );
00153 static void fix_translation_string( char *string );
00154 
00155 PtWidgetClass_t *PtCreateMozillaClass( void );
00156 #ifndef _PHSLIB
00157   PtWidgetClassRef_t __PtMozilla = { NULL, PtCreateMozillaClass };
00158   PtWidgetClassRef_t *PtMozilla = &__PtMozilla; 
00159 #endif
00160 
00161 /* globals */
00162 char *g_Print_Left_Header_String, *g_Print_Right_Header_String, *g_Print_Left_Footer_String, *g_Print_Right_Footer_String;
00163 
00164 void 
00165 MozSetPreference(PtWidget_t *widget, int type, char *pref, void *data)
00166 {
00167   PtMozillaWidget_t *moz = (PtMozillaWidget_t *) widget;
00168   nsIPref *prefs = moz->EmbedRef->GetPrefs();
00169 
00170   switch (type)
00171   {
00172     case Pt_MOZ_PREF_CHAR:
00173       prefs->SetCharPref(pref, (char *)data);
00174       break;
00175     case Pt_MOZ_PREF_BOOL:
00176       prefs->SetBoolPref(pref, (int)data);
00177       break;
00178     case Pt_MOZ_PREF_INT:
00179       prefs->SetIntPref(pref, (int)data);
00180       break;
00181     case Pt_MOZ_PREF_COLOR:
00182 // not supported yet
00183 //      prefs->SetColorPrefDWord(pref, (uint32) data);
00184       break;
00185   }
00186 }
00187 
00188 static void 
00189 MozLoadURL(PtMozillaWidget_t *moz, char *url)
00190 {
00191     // If the widget isn't realized, just return.
00192   if (!(PtWidgetFlags((PtWidget_t *)moz) & Pt_REALIZED))
00193     return;
00194 
00195   moz->EmbedRef->SetURI(url);
00196   moz->EmbedRef->LoadCurrentURI();
00197 }
00198 
00199 /* watch for an Ph_EV_INFO event in order to detect an Ph_OFFSCREEN_INVALID */
00200 static int EvInfo( PtWidget_t *widget, void *data, PtCallbackInfo_t *cbinfo )
00201 {
00202   if( cbinfo->event && cbinfo->event->type == Ph_EV_INFO && cbinfo->event->subtype == Ph_OFFSCREEN_INVALID ) {
00203     PtMozillaWidget_t *moz = ( PtMozillaWidget_t * ) widget;
00204     nsIPref *pref = moz->EmbedRef->GetPrefs();
00205     PRBool displayInternalChange = PR_FALSE;
00206     pref->GetBoolPref("browser.display.internaluse.graphics_changed", &displayInternalChange);
00207     pref->SetBoolPref("browser.display.internaluse.graphics_changed", !displayInternalChange);
00208     }
00209   return Pt_CONTINUE;
00210 }
00211 
00212 const char* const kPersistContractID = "@mozilla.org/embedding/browser/nsWebBrowserPersist;1";
00213 
00214 void MozSaveTarget( char *url, PtMozillaWidget_t *moz )
00215 {
00216   nsresult rv;
00217   nsCOMPtr<nsIWebBrowserPersist> webPersist(do_CreateInstance(kPersistContractID, &rv));
00218   if( !webPersist ) return;
00219 
00220   nsCOMPtr<nsIURI> uri;
00221   NS_NewURI( getter_AddRefs(uri), url );
00222 
00223   /* create a temporary file */
00224   char tmp_path[1024];
00225   tmpnam( tmp_path );
00226   nsCOMPtr<nsILocalFile> tmpFile;
00227   NS_NewNativeLocalFile(nsDependentCString( tmp_path ), PR_TRUE, getter_AddRefs(tmpFile));
00228 
00229   /* create a download object, use to sniff the headers for a location indication */
00230   HeaderSniffer *sniffer = new HeaderSniffer( webPersist, moz, uri, tmpFile );
00231 
00232   webPersist->SetProgressListener( sniffer );
00233   webPersist->SaveURI( uri, nsnull, nsnull, nsnull, nsnull, tmpFile );
00234 }
00235 
00236 // defaults function, called on creation of a widget
00237 static void 
00238 mozilla_defaults( PtWidget_t *widget )
00239 {
00240   PtMozillaWidget_t   *moz = (PtMozillaWidget_t *) widget;
00241   PtBasicWidget_t   *basic = (PtBasicWidget_t *) widget;
00242   PtContainerWidget_t *cntr = (PtContainerWidget_t*) widget;
00243 
00244   moz->EmbedRef = new EmbedPrivate();
00245   moz->EmbedRef->Init(widget);
00246   moz->EmbedRef->Setup();
00247 
00248   moz->disable_new_windows = 0;
00249   moz->disable_exception_dlg = 0;
00250   moz->text_zoom = 100;
00251   moz->actual_text_zoom = 100;
00252   moz->toActivate = 0;
00253 
00254   // widget related
00255   basic->flags = Pt_ALL_OUTLINES | Pt_ALL_BEVELS | Pt_FLAT_FILL;
00256   widget->resize_flags &= ~Pt_RESIZE_XY_BITS; // fixed size.
00257   widget->anchor_flags = Pt_TOP_ANCHORED_TOP | Pt_LEFT_ANCHORED_LEFT | \
00258       Pt_BOTTOM_ANCHORED_TOP | Pt_RIGHT_ANCHORED_LEFT | Pt_ANCHORS_INVALID;
00259 
00260   cntr->flags |= Pt_CHILD_GETTING_FOCUS;
00261 
00262   PtAddEventHandler( widget, Ph_EV_INFO, EvInfo, NULL );
00263 }
00264 
00265 // widget destroy function
00266 static void 
00267 mozilla_destroy( PtWidget_t *widget ) 
00268 {
00269   PtMozillaWidget_t *moz = (PtMozillaWidget_t *) widget;
00270 
00271   if (moz->EmbedRef)
00272   {
00273     moz->EmbedRef->Destroy();
00274     delete moz->EmbedRef;
00275   }
00276 }
00277 
00278 static int child_getting_focus( PtWidget_t *widget, PtWidget_t *child, PhEvent_t *ev ) {
00279   PtMozillaWidget_t *moz = (PtMozillaWidget_t *) widget;
00280   nsCOMPtr<nsPIDOMWindow> piWin;
00281 
00282   moz->EmbedRef->GetPIDOMWindow( getter_AddRefs( piWin ) );
00283   if( !piWin ) return Pt_CONTINUE;
00284 
00285   nsIFocusController *focusController = piWin->GetRootFocusController();
00286   if( focusController )
00287     focusController->SetActive( PR_TRUE );
00288 
00289   if( moz->toActivate ) {
00290     moz->toActivate = 0;
00291     piWin->Activate();
00292     }
00293 
00294   return Pt_CONTINUE;
00295   }
00296 
00297 static int child_losing_focus( PtWidget_t *widget, PtWidget_t *child, PhEvent_t *ev ) {
00298   PtMozillaWidget_t *moz = (PtMozillaWidget_t *) widget;
00299   nsCOMPtr<nsPIDOMWindow> piWin;
00300 
00301   moz->EmbedRef->GetPIDOMWindow( getter_AddRefs( piWin ) );
00302   if( !piWin ) return Pt_CONTINUE;
00303 
00304   nsIFocusController *focusController = piWin->GetRootFocusController();
00305   if( focusController )
00306     focusController->SetActive( PR_FALSE );
00307 
00308   piWin->Deactivate();
00309   moz->toActivate = 1;
00310 
00311   return Pt_CONTINUE;
00312   }
00313 
00314 static void 
00315 mozilla_extent(PtWidget_t *widget)
00316 {
00317   PtMozillaWidget_t *moz = (PtMozillaWidget_t *) widget;
00318 
00319   PtSuperClassExtent(PtContainer, widget);
00320 
00321   moz->EmbedRef->Position(widget->area.pos.x, widget->area.pos.y);
00322   moz->EmbedRef->Size(widget->area.size.w, widget->area.size.h);
00323 }
00324 
00325 // set resources function
00326 static void 
00327 mozilla_modify( PtWidget_t *widget, PtArg_t const *argt, PtResourceRec_t const *mod )
00328 {
00329   PtMozillaWidget_t *moz = (PtMozillaWidget_t *)widget;
00330   nsIPref *pref = moz->EmbedRef->GetPrefs();
00331 
00332   switch( argt->type )  
00333   {
00334     case Pt_ARG_MOZ_GET_URL:
00335       MozLoadURL(moz, (char *)(argt->value));
00336       break;
00337 
00338     case Pt_ARG_MOZ_NAVIGATE_PAGE:
00339       if (moz->EmbedRef)
00340       {
00341         if (argt->value == Pt_WEB_DIRECTION_FWD)
00342           moz->EmbedRef->Forward();
00343         else if (argt->value == Pt_WEB_DIRECTION_BACK)
00344           moz->EmbedRef->Back();
00345         else 
00346         {
00347           int incr;
00348 
00349           incr = argt->len ? argt->len : 1;
00350 
00351           if (argt->value == Pt_WEB_DIRECTION_UP)
00352             moz->EmbedRef->ScrollUp(incr);
00353           else if (argt->value == Pt_WEB_DIRECTION_DOWN)
00354             moz->EmbedRef->ScrollDown(incr);
00355           else if (argt->value == Pt_WEB_DIRECTION_LEFT)
00356             moz->EmbedRef->ScrollLeft(incr);
00357           else if (argt->value == Pt_WEB_DIRECTION_RIGHT)
00358             moz->EmbedRef->ScrollRight(incr);
00359         }
00360       }
00361       break;
00362 
00363     case Pt_ARG_MOZ_SCROLL_TO:
00364       if (moz->EmbedRef)
00365         moz->EmbedRef->ScrollTo(argt->value, argt->len);
00366       break;
00367 
00368     case Pt_ARG_MOZ_STOP:
00369       if (moz->EmbedRef)
00370         moz->EmbedRef->Stop();
00371       break;
00372 
00373     case Pt_ARG_MOZ_RELOAD:
00374       if (moz->EmbedRef)
00375         moz->EmbedRef->Reload(0);
00376       break;
00377 
00378     case Pt_ARG_MOZ_PRINT: 
00379       {
00380       PpPrintContext_t *pc = ( PpPrintContext_t * ) argt->value;
00381       moz->EmbedRef->Print(pc);
00382       }
00383       break;
00384 
00385     case Pt_ARG_MOZ_OPTION:
00386       mozilla_set_pref(widget, (char*)argt->len, (char*)argt->value);
00387       break;
00388 
00389     case Pt_ARG_MOZ_ENCODING: {
00390       char translation[1024];
00391       strcpy( translation, (char*)argt->value );
00392       fix_translation_string( translation );
00393       pref->SetUnicharPref( "intl.charset.default", NS_ConvertASCIItoUCS2( translation ).get());
00394       pref->SavePrefFile( nsnull );
00395       }
00396       break;
00397 
00398     case Pt_ARG_MOZ_COMMAND: {
00399       PtWebClient2Command_t *wdata = ( PtWebClient2Command_t * ) argt->len;
00400       switch ((int)(argt->value)) 
00401       {
00402         case Pt_MOZ_COMMAND_CUT: {
00403           moz->EmbedRef->Cut(wdata?wdata->ClipboardInfo.input_group:1);
00404           }
00405           break;
00406         case Pt_MOZ_COMMAND_COPY: {
00407           moz->EmbedRef->Copy(wdata?wdata->ClipboardInfo.input_group:1);
00408           }
00409           break;
00410         case Pt_MOZ_COMMAND_PASTE: {
00411           moz->EmbedRef->Paste(wdata?wdata->ClipboardInfo.input_group:1);
00412           }
00413           break;
00414         case Pt_MOZ_COMMAND_SELECTALL: {
00415           moz->EmbedRef->SelectAll();
00416           }
00417           break;
00418         case Pt_MOZ_COMMAND_CLEAR: {
00419           moz->EmbedRef->Clear();
00420           }
00421           break;
00422 
00423         case Pt_MOZ_COMMAND_FIND: {
00424           nsCOMPtr<nsIWebBrowserFind> finder( do_GetInterface( moz->EmbedRef->mWindow->mWebBrowser ) );
00425           finder->SetSearchString( NS_ConvertASCIItoUCS2(wdata->FindInfo.string).get() );
00426           finder->SetMatchCase( wdata->FindInfo.flags & Pt_WEB_FIND_MATCH_CASE );
00427           finder->SetFindBackwards( wdata->FindInfo.flags & Pt_WEB_FIND_GO_BACKWARDS );
00428           finder->SetWrapFind( wdata->FindInfo.flags & Pt_WEB_FIND_START_AT_TOP );
00429           finder->SetEntireWord( wdata->FindInfo.flags & Pt_WEB_FIND_MATCH_WHOLE_WORDS );
00430 
00431           PRBool didFind;
00432           finder->FindNext( &didFind );
00433           break;
00434           }
00435 
00436         case Pt_MOZ_COMMAND_SAVEAS: {
00437           char *dirname = ( char * ) calloc( 1, strlen( wdata->SaveasInfo.filename + 7 ) );
00438           if( dirname ) {
00439             sprintf( dirname, "%s_files", wdata->SaveasInfo.filename );
00440             moz->EmbedRef->SaveAs( wdata->SaveasInfo.filename, dirname );
00441             free( dirname );
00442             }
00443           break;
00444           }
00445         }
00446       }
00447       break;
00448 
00449     case Pt_ARG_MOZ_WEB_DATA_URL:
00450 #if 0
00451 //        moz->MyBrowser->WebBrowserContainer->OpenStream( moz->MyBrowser->WebBrowser, "file://", "text/html" );
00452 //        strcpy( moz->url, (char*)argt->value );
00453 #endif
00454         break;
00455 
00456     case Pt_ARG_MOZ_UNKNOWN_RESP: {
00457       PtWebClient2UnknownData_t *unknown = ( PtWebClient2UnknownData_t * ) argt->value;
00458       if( unknown->response == Pt_WEB_RESPONSE_CANCEL ) {
00459         EmbedDownload *d = FindDownload( moz, unknown->download_ticket );
00460         if( d ) {
00461           if( d->mLauncher ) d->mLauncher->Cancel(NS_BINDING_ABORTED); /* this will also call the EmbedDownload destructor */
00462           else if( d->mPersist ) d->mPersist->CancelSave(); /* this will also call the EmbedDownload destructor */
00463           else delete d; /* just in case neither d->mLauncher or d->mPersist was set */
00464           }
00465         }
00466       }
00467       break;
00468 
00469     case Pt_ARG_MOZ_DOWNLOAD: 
00470       {
00471         MozSaveTarget( (char*)argt->value, moz );
00472       }
00473       break;
00474 
00475     case Pt_ARG_MOZ_WEB_DATA: {
00476 #if 0
00477         WWWRequest *req = ( WWWRequest * ) argt->value;
00478         char *hdata;
00479         int len;
00480 
00481         if( strcmp( moz->url, req->DataHdr.url ) ) break;
00482 
00483         hdata = (char*) argt->len;
00484         len = req->DataHdr.length;
00485 
00486         switch( req->DataHdr.type ) {
00487           case Pt_WEB_DATA_HEADER:
00488             if( !moz->MyBrowser->WebBrowserContainer->IsStreaming( ) ) break;
00489 
00490             /* request the document body */
00491             if( moz->web_data_req_cb ) {
00492               PtCallbackInfo_t  cbinfo;
00493               PtMozillaDataRequestCb_t cb;
00494 
00495               memset( &cbinfo, 0, sizeof( cbinfo ) );
00496               cbinfo.reason = Pt_CB_MOZ_WEB_DATA_REQ;
00497               cbinfo.cbdata = &cb;
00498               cb.type = Pt_WEB_DATA_BODY;
00499               cb.length = 32768;
00500               cb.url = moz->url;
00501               PtInvokeCallbackList( moz->web_data_req_cb, (PtWidget_t *)moz, &cbinfo);
00502               }
00503             break;
00504           case Pt_WEB_DATA_BODY: {
00505             if( !moz->MyBrowser->WebBrowserContainer->IsStreaming( ) ) break;
00506             if( len )
00507               moz->MyBrowser->WebBrowserContainer->AppendToStream( hdata, len );
00508             else moz->MyBrowser->WebBrowserContainer->CloseStream( );
00509 
00510             /* request the next piece of the body */
00511             PtCallbackInfo_t  cbinfo;
00512             PtMozillaDataRequestCb_t cb;
00513 
00514             memset( &cbinfo, 0, sizeof( cbinfo ) );
00515             cbinfo.reason = Pt_CB_MOZ_WEB_DATA_REQ;
00516             cbinfo.cbdata = &cb;
00517             cb.type = len ? Pt_WEB_DATA_BODY : Pt_WEB_DATA_CLOSE;
00518             cb.length = 32768;
00519             cb.url = moz->url;
00520             PtInvokeCallbackList( moz->web_data_req_cb, (PtWidget_t *)moz, &cbinfo);
00521             }
00522             break;
00523           case Pt_WEB_DATA_CLOSE:
00524             if( !moz->MyBrowser->WebBrowserContainer->IsStreaming( ) ) break;
00525             moz->MyBrowser->WebBrowserContainer->CloseStream( );
00526             break;
00527           }
00528 #endif
00529         }
00530       break;
00531     }
00532 
00533   return;
00534 }
00535 
00536 
00537 // get resources function
00538 static int 
00539 mozilla_get_info( PtWidget_t *widget, PtArg_t *argt, PtResourceRec_t const *mod )
00540 {
00541   PtMozillaWidget_t *moz = (PtMozillaWidget_t *) widget;
00542   nsIPref *pref = moz->EmbedRef->GetPrefs();
00543 
00544   switch (argt->type)
00545   {
00546     case Pt_ARG_MOZ_NAVIGATE_PAGE:
00547       moz->navigate_flags = 0;
00548       if (moz->EmbedRef->CanGoBack())
00549         moz->navigate_flags |= ( 1 << Pt_WEB_DIRECTION_BACK );
00550       if (moz->EmbedRef->CanGoForward())
00551         moz->navigate_flags |= ( 1 << Pt_WEB_DIRECTION_FWD );
00552       *((int **)argt->value) = &moz->navigate_flags;
00553       break;
00554 
00555     case Pt_ARG_MOZ_OPTION:
00556       mozilla_get_pref( widget, (char*)argt->len, (char*) argt->value );
00557       break;
00558 
00559     case Pt_ARG_MOZ_GET_CONTEXT: {
00560       if( argt->len & Pt_MOZ_CONTEXT_LINK )
00561         *(char**) argt->value = moz->rightClickUrl_link;
00562       else if( argt->len & Pt_MOZ_CONTEXT_IMAGE )
00563         *(char**) argt->value = moz->rightClickUrl_image;
00564       else *(char**) argt->value = moz->rightClickUrl_link;
00565       }
00566       break;
00567 
00568     case Pt_ARG_MOZ_ENCODING: 
00569       {
00570       PRUnichar *charset = nsnull;
00571       static char encoding[256];
00572       pref->GetLocalizedUnicharPref( "intl.charset.default", &charset );
00573       strcpy( encoding, NS_ConvertUCS2toUTF8(charset).get() );
00574       *(char**)argt->value = encoding;
00575       }
00576       break;
00577 
00578     case Pt_ARG_MOZ_GET_HISTORY: 
00579       {
00580         PtWebClientHistory_t *hist_info = (PtWebClientHistory_t *)argt->len;
00581         PtWebClientHistoryData_t *HistoryReplyBuf = (PtWebClientHistoryData_t *) argt->value;
00582         int i, j;
00583         PRInt32 total;
00584         moz->EmbedRef->mSessionHistory->GetCount( &total );
00585 
00586         for( i=total-2, j=0; i>=0 && j<hist_info->num; i--, j++) 
00587         {
00588           nsIHistoryEntry *entry;
00589           moz->EmbedRef->mSessionHistory->GetEntryAtIndex( i, PR_FALSE, &entry );
00590         
00591           PRUnichar *title;
00592           nsIURI *url;
00593           entry->GetTitle( &title );
00594           entry->GetURI( &url );
00595 
00596           nsString stitle( title );
00597           HistoryReplyBuf[j].title = strdup( ToNewCString(stitle) );
00598 
00599           nsCAutoString specString;
00600           url->GetSpec(specString);
00601           HistoryReplyBuf[j].url = strdup( (char *) specString.get() );
00602         }
00603       }
00604       break;
00605   }
00606 
00607   return (Pt_CONTINUE);
00608 }
00609 
00610 
00611 static void 
00612 mozilla_set_pref( PtWidget_t *widget, char *option, char *value ) 
00613 {
00614   PtMozillaWidget_t *moz = ( PtMozillaWidget_t * ) widget;
00615   nsIPref *pref = moz->EmbedRef->GetPrefs();
00616   char buffer[1024];
00617 
00618   mozilla_get_pref( widget, option, buffer );
00619   if( buffer[0] && !strcmp( value, buffer ) )
00620     return; /* the option is already set */
00621 
00622 /* HTML Options */
00623   if( !strcmp( option, "A:visited color" ) ) {
00624     pref->SetUnicharPref( "browser.visited_color", NS_ConvertASCIItoUCS2(value).get() );
00625     }
00626   else if( !strcmp( option, "A:link color" ) ) {
00627     pref->SetUnicharPref( "browser.anchor_color", NS_ConvertASCIItoUCS2(value).get() );
00628     }
00629 
00630 /* the mozserver already has A:link color == browser.anchor_color for this */
00631 //  else if( !strcmp( option, "A:active color" ) ) {
00632 //    pref->SetUnicharPref( "browser.anchor_color", NS_ConvertASCIItoUCS2(value).get() );
00633 //    }
00634 
00635   else if( !strcmp( option, "BODY color" ) ) {
00636     pref->SetUnicharPref( "browser.display.foreground_color", NS_ConvertASCIItoUCS2(value).get() );
00637     }
00638   else if( !strcmp( option, "BODY background" ) ) {
00639     pref->SetUnicharPref( "browser.display.background_color", NS_ConvertASCIItoUCS2(value).get() );
00640     }
00641   else if( !strcmp( option, "bIgnoreDocumentAttributes" ) )
00642     pref->SetBoolPref( "browser.display.use_document_colors", stricmp( value, "TRUE" ) ? PR_FALSE : PR_TRUE );
00643   else if( !strcmp( option, "bUnderlineLinks" ) ) {
00644     pref->SetBoolPref( "browser.underline_anchors", !stricmp( value, "TRUE" ) ? PR_TRUE : PR_FALSE );
00645     }
00646   else if( !strcmp( option, "iUserTextSize" ) ) {
00647        int zoom = atoi( value );
00648     nsCOMPtr<nsIDOMWindow> domWindow;
00649     moz->EmbedRef->mWindow->mWebBrowser->GetContentDOMWindow(getter_AddRefs(domWindow));
00650     if(domWindow) {
00651       domWindow->SetTextZoom( zoom/100. );
00652       moz->text_zoom = zoom;
00653       float vv;
00654       domWindow->GetTextZoom( &vv );
00655       moz->actual_text_zoom = (int) ( vv * 100 );
00656       }
00657     }
00658   else if( !strcmp( option, "iZoomPercent" ) ) {
00659        int zoom = atoi( value );
00660        //nsCOMPtr<nsIBaseWindow> baseWindow = do_QueryInterface(moz->EmbedRef->mWindow->mWebBrowser);
00661     nsCOMPtr<nsIDocShell> docShell = do_GetInterface( moz->EmbedRef->mWindow->mWebBrowser );
00662        if (docShell) {
00663          nsCOMPtr<nsIPresShell> shell;
00664          docShell->GetPresShell(getter_AddRefs(shell));
00665          if (shell) {
00666            nsIViewManager* vm = shell->GetViewManager();
00667            if (vm) {
00668           nsIDeviceContext *dev;
00669              vm->GetDeviceContext(dev);
00670           if(dev) {
00671             nsCOMPtr<nsIDOMWindow> domWindow;
00672             moz->EmbedRef->mWindow->mWebBrowser->GetContentDOMWindow(getter_AddRefs(domWindow));
00673             if(domWindow) {
00674                        int prev_zoom = moz->text_zoom;
00675                        int x,y;
00676                        //
00677                        // The iZoomPercent option is meant for kwww, which is set to
00678                        // always zoom images along with text. See nsDocumentViewer.cpp's
00679                        // DocumentViewerImpl::SetTextZoom() which also calls
00680                        // SetCanonicalPixelScale() for the device context in the kwww
00681                        // case. The extra SetCanonicalPixelScale() done here is only done
00682                        // so the images grow/shrink right away when the text sizing
00683                        // initiates a reflow.
00684                        // BUG: The immediate draw is not perfect
00685                        // and a reload is needed anyways on certain web pages, or after
00686                        // multiple zoomings.
00687                        //
00688                        domWindow->GetScrollX( &x );
00689                        domWindow->GetScrollY( &y );
00690 
00691                        //struct _Pf_ctrl *pfc = PfDefaultContext();
00692                        //int rc = PfSetRenderingDPICx( pfc, 192, 192 );
00693                        //printf("Got _Pf_ctrl* 0x%x, rc %d\n", pfc, rc);
00694 
00695               //dev->SetCanonicalPixelScale( zoom/100. );
00696               domWindow->SetTextZoom( zoom/100.0 );
00697               moz->text_zoom = zoom;
00698 
00699               float vv;
00700               domWindow->GetTextZoom( &vv );
00701               moz->actual_text_zoom = (int) ( vv * 100 );
00702                        //printf("*** Scroll pos %d,%d, actual zoom: %d, scrolling to %f,%f\n", x,y, moz->actual_text_zoom, (double)zoom*x/prev_zoom, (double)zoom*y/prev_zoom);
00703                     domWindow->ScrollTo(zoom*x/prev_zoom, zoom*y/prev_zoom);
00704                        }
00705             }
00706              }
00707            }
00708          }
00709     }
00710   else if( !strcmp( option, "iReflow" ) ) {
00711        int zoom = atoi( value );
00712        //nsCOMPtr<nsIBaseWindow> baseWindow = do_QueryInterface(moz->EmbedRef->mWindow->mWebBrowser);
00713     nsCOMPtr<nsIDocShell> docShell = do_GetInterface( moz->EmbedRef->mWindow->mWebBrowser );
00714        if (docShell) {
00715          nsCOMPtr<nsIPresShell> shell;
00716          docShell->GetPresShell(getter_AddRefs(shell));
00717          if (shell) {
00718            nsIViewManager* vm = shell->GetViewManager();
00719            if (vm) {
00720           nsIDeviceContext *dev;
00721              vm->GetDeviceContext(dev);
00722           if(dev) {
00723             nsCOMPtr<nsIDOMWindow> domWindow;
00724             moz->EmbedRef->mWindow->mWebBrowser->GetContentDOMWindow(getter_AddRefs(domWindow));
00725             if(domWindow) {
00726                        int prev_zoom = moz->text_zoom;
00727                        int x,y;
00728 
00729                        domWindow->GetScrollX( &x );
00730                        domWindow->GetScrollY( &y );
00731 
00732                        dev->FlushFontCache();
00733                        printf("** Called dev->FlushFontCache()\n");
00734                        }
00735             }
00736              }
00737            }
00738          }
00739     }
00740   else if( !strcmp( option, "BODY font-family" ) ) {
00741     /* set the current font */
00742     char *font_default = NULL;
00743     char preference[256];
00744 
00745     pref->CopyCharPref( "font.default.x-western", &font_default );
00746     if( !font_default ) font_default = "serif";
00747 
00748     sprintf( preference, "font.name.%s.x-western", font_default );
00749     pref->SetCharPref( preference, value );
00750     }
00751   else if( !strcmp( option, "PRE font-family" ) ) {
00752     pref->SetCharPref( "font.name.monospace.x-western", value );
00753     }
00754   else if( !strcmp( option, "H* font-family" ) ) {
00755     /* do not set these - use the BODY font-family instead */
00756     }
00757 
00758 /* SOCKS options */
00759   else if( !strcmp( option, "socks_server" ) )  ; /* not used */
00760   else if( !strcmp( option, "socks_port" ) )    ; /* not used */
00761   else if( !strcmp( option, "socks_user" ) )    ; /* not used */
00762   else if( !strcmp( option, "socks_app" ) )     ; /* not used */
00763 
00764 /* HTTP options */
00765 
00766   else if( !strcmp( option, "enable_proxy" ) ) {
00767     if( value && !stricmp( value, "yes" ) )
00768       pref->SetIntPref( "network.proxy.type", 1 );
00769       else pref->SetIntPref( "network.proxy.type", 0 );
00770     }
00771   else if( !strcmp( option, "http_proxy_host" ) ) {
00772     pref->SetCharPref( "network.proxy.http", value );
00773     }
00774   else if( !strcmp( option, "http_proxy_port" ) ) {
00775     pref->SetIntPref( "network.proxy.http_port", atoi(value) );
00776     }
00777   else if( !strcmp( option, "proxy_overrides" ) ) {
00778     pref->SetCharPref( "network.proxy.no_proxies_on", value );
00779     }
00780   else if( !strcmp( option, "https_proxy_host" ) ) {
00781     pref->SetCharPref( "network.proxy.ssl", value );
00782     }
00783   else if( !strcmp( option, "https_proxy_port" ) ) {
00784     pref->SetIntPref( "network.proxy.ssl_port", atoi(value) );
00785     }
00786 
00787 
00788 /* FTP options */
00789   else if( !strcmp( option, "ftp_proxy_host" ) )
00790     pref->SetCharPref( "network.proxy.ftp", value );
00791   else if( !strcmp( option, "ftp_proxy_port" ) )
00792     pref->SetIntPref( "network.proxy.ftp_port", atoi(value) );
00793   else if( !strcmp( option, "email_address" ) )   ; /* not used */
00794 
00795 /* Gopher options */
00796   else if( !strcmp( option, "gopher_proxy_host" ) )
00797     pref->SetCharPref( "network.proxy.gopher", value );
00798   else if( !strcmp( option, "gopher_proxy_port" ) )
00799     pref->SetIntPref( "network.proxy.gopher_port", atoi(value) );
00800 
00801 /* TCP/IP options */
00802   else if( !strcmp( option, "max_connections" ) )
00803     pref->SetIntPref( "network.http.max-connections", atoi( value ) );
00804 
00805 /* Disk-cache options */
00806   else if( !strcmp( option, "main_cache_kb_size" ) )
00807     pref->SetIntPref( "browser.cache.disk.capacity", atoi( value ) );
00808   else if( !strcmp( option, "enable_disk_cache" ) )
00809     pref->SetBoolPref( "browser.cache.disk.enable", !stricmp( value, "TRUE" ) ? PR_TRUE : PR_FALSE );
00810   else if( !strcmp( option, "dcache_verify_policy" ) ) {
00811     int n = atoi( value ), moz_value=3;
00812     if( n == 0 ) moz_value = 2; /* never */
00813     else if( n == 1 ) moz_value = 0; /* once */
00814     else if( n == 2 ) moz_value = 1; /* always */
00815 
00816 // mozilla wants: 0 = once-per-session, 1 = each-time, 2 = never, 3 = when-appropriate/automatically
00817     pref->SetIntPref( "browser.cache.check_doc_frequency", moz_value );
00818     }
00819   else if( !strcmp( option, "main_cache_dir" ) )    ; /* not used */
00820   else if( !strcmp( option, "main_index_file" ) )     ; /* not used */
00821   else if( !strcmp( option, "clear_main_cache_on_exit" ) )    ; /* not used */
00822   else if( !strcmp( option, "keep_index_file_updated" ) )     ; /* not used */
00823 
00824 /* memory cache options */
00825   else if( !strcmp( option, "memory_cache_kb_size" ) || !strcmp( option, "image_cache_size_KB" ) ) {
00826     int kb = atoi( value );
00827     if( kb <= 0 ) kb = 100; /* have a minimum threshold */
00828     pref->SetIntPref( "browser.cache.memory.capacity", kb );
00829     }
00830 
00831 /* Miscellaneous options */
00832   else if( !strcmp( option, "History_Expire" ) )
00833     pref->SetIntPref( "browser.history_expire_days", atoi( value ) );
00834   else if( !strcmp( option, "Page_History_Length" ) )
00835     pref->SetIntPref( "browser.sessionhistory.max_entries", atoi( value ) );
00836 
00837 
00838 
00839 /* HTML Options */
00840   else if( !strcmp( option, "bAutoLoadImages" ) )     ; /* not used */
00841   else if( !strcmp( option, "iReformatHandling" ) )   ; /* not used */
00842   else if( !strcmp( option, "BODY margin-top" ) )   ; /* not used */
00843   else if( !strcmp( option, "BODY margin-right" ) )   ; /* not used */
00844   else if( !strcmp( option, "iScrollbarSize" ) )    ; /* not used */
00845   else if( !strcmp( option, "top_focus_color" ) )   ; /* not used */
00846   else if( !strcmp( option, "bot_focus_color" ) )   ; /* not used */
00847   else if( !strcmp( option, "top_border_color" ) )    ; /* not used */
00848   else if( !strcmp( option, "bot_border_color" ) )    ; /* not used */
00849   else if( !strcmp( option, "bview_source" ) )    ; /* not used */
00850   else if( !strcmp( option, "underline_width" ) )   ; /* not used */
00851   else if( !strcmp( option, "bkey_mode" ) )   ; /* not used */
00852   else if( !strcmp( option, "mono_form_font" ) )    ; /* not used */
00853   else if( !strcmp( option, "form_font" ) )   ; /* not used */
00854   else if( !strcmp( option, "frame_spacing" ) )   ; /* not used */
00855   else if( !strcmp( option, "disable_new_windows" ) ) /* disable new windows*/
00856   {
00857     if (strcmp(value, "TRUE") == 0)
00858       moz->disable_new_windows = 1;
00859     else
00860       moz->disable_new_windows = 0;
00861   }
00862   else if( !strcmp( option, "disable_exception_dlg" ) ) /* disable exception dialogs */ 
00863   {
00864     if (strcmp(value, "TRUE") == 0)
00865       moz->disable_exception_dlg = 1;
00866     else
00867       moz->disable_exception_dlg = 0;
00868   }
00869 
00870   else if( !strcmp( option, "bDisableHighlight" ) )   ; /* not used */
00871 
00872 /* HTTP cookie options */
00873   else if( !strcmp( option, "cookiejar_path" ) )    ; /* not used */
00874   else if( !strcmp( option, "cookiejar_name" ) )    ; /* not used */
00875   else if( !strcmp( option, "cookiejar_size" ) )    ; /* not used */
00876   else if( !strcmp( option, "cookiejar_save_always" ) )   ; /* not used */
00877 
00878 /* Authentication options */
00879   else if( !strcmp( option, "max_password_guesses" ) )    ; /* not used */
00880 
00881 /* File options */
00882   else if( !strcmp( option, "file_display_dir" ) )    ; /* not used */
00883 
00884 /* Image options */
00885   else if( !strcmp( option, "bProgressiveImageDisplay" ) )    ; /* not used */
00886   else if( !strcmp( option, "quantize_jpegs" ) )    ; /* not used */
00887   else if( !strcmp( option, "concurrent_decodes" ) )    ; /* not used */
00888 
00889 
00890 /* Print options */
00891   else if( !strcmp( option, "Print_Header_Font" ) )     ; /* not used */
00892   else if( !strcmp( option, "Print_Header_Font_Size" ) )    ; /* not used */
00893   else if( !strcmp( option, "Print_Left_Header_String" ) ) {
00894     if( g_Print_Left_Header_String ) free( g_Print_Left_Header_String );
00895     g_Print_Left_Header_String = strdup( value );
00896     }
00897   else if( !strcmp( option, "Print_Right_Header_String" ) ) {
00898     if( g_Print_Right_Header_String ) free( g_Print_Right_Header_String );
00899     g_Print_Right_Header_String = strdup( value );
00900     }
00901   else if( !strcmp( option, "Print_Left_Footer_String" ) ) {
00902     if( g_Print_Left_Footer_String ) free( g_Print_Left_Footer_String );
00903     g_Print_Left_Footer_String = strdup( value );
00904     }
00905   else if( !strcmp( option, "Print_Right_Footer_String" ) ) {
00906     if( g_Print_Right_Footer_String ) free( g_Print_Right_Footer_String );
00907     g_Print_Right_Footer_String = strdup( value );
00908     }
00909 
00910 
00911 /* Miscellaneous options */
00912   else if( !strcmp( option, "Global_History_File" ) )     ; /* not used */
00913   else if( !strcmp( option, "IBeam_Cursor" ) )    ; /* not used */
00914   else if( !strcmp( option, "Image_Cache_Size_KB" ) )     ; /* not used */
00915   else if( !strcmp( option, "ImageMap_Cursor" ) )     ; /* not used */
00916   else if( !strcmp( option, "ImageMap_Cursor_NoLink" ) )    ; /* not used */
00917   else if( !strcmp( option, "Link_Cursor" ) )     ; /* not used */
00918   else if( !strcmp( option, "LinkWait_Cursor" ) )     ; /* not used */
00919   else if( !strcmp( option, "Normal_Cursor" ) )     ; /* not used */
00920   else if( !strcmp( option, "NormalWait_Cursor" ) )     ; /* not used */
00921   else if( !strcmp( option, "Page_Cache_Size" ) )     ; /* not used */
00922   else if( !strcmp( option, "Safe_Memory_Free" ) )    ; /* not used */
00923   else if( !strcmp( option, "Site_History_Length" ) )     ; /* not used */
00924   else if( !strcmp( option, "Show_Server_Errors" ) )    ; /* not used */
00925   else if( !strcmp( option, "Use_Anti_Alias" ) )    ; /* not used */
00926   else if( !strcmp( option, "Use_Explicit_Accept_Headers" ) )     ; /* not used */
00927   else if( !strcmp( option, "Visitation_Horizon" ) )    ; /* not used */
00928 
00929   else if( !strcmp( option, "Print_Frame" ) )
00930     pref->SetCharPref( "user.print.print_frame", value );
00931   else if( !strcmp( option, "SetPrintBGColors" ) )
00932     pref->SetCharPref( "user.print.SetPrintBGColors", value );
00933   else if( !strcmp( option, "SetPrintBGImages" ) )
00934     pref->SetCharPref( "user.print.SetPrintBGImages", value );
00935 
00936   pref->SavePrefFile( nsnull );
00937   }
00938 
00939 static void mozilla_get_pref( PtWidget_t *widget, char *option, char *value ) {
00940   PtMozillaWidget_t *moz = ( PtMozillaWidget_t * ) widget;
00941   nsIPref *pref = moz->EmbedRef->GetPrefs();
00942 
00943   /* HTML Options */
00944   if( !strcmp( option, "A:link color" ) || !strcmp( option, "A:active color" ) ) {
00945     nsXPIDLCString colorStr;
00946     if( pref->CopyCharPref( "browser.anchor_color", getter_Copies(colorStr) ) == NS_OK )
00947       strcpy( value, colorStr );
00948     }
00949   else if( !strcmp( option, "A:visited color" ) ) {
00950     nsXPIDLCString colorStr;
00951     if( pref->CopyCharPref( "browser.visited_color", getter_Copies(colorStr) ) == NS_OK )
00952       strcpy( value, colorStr );
00953     }
00954   else if( !strcmp( option, "BODY color" ) ) {
00955     nsXPIDLCString colorStr;
00956     if( pref->CopyCharPref( "browser.display.foreground_color", getter_Copies(colorStr) ) == NS_OK )
00957       strcpy( value, colorStr );
00958     }
00959   else if( !strcmp( option, "BODY background" ) ) {
00960     nsXPIDLCString colorStr;
00961     if( pref->CopyCharPref( "browser.display.background_color", getter_Copies(colorStr) ) == NS_OK )
00962       strcpy( value, colorStr );
00963     }
00964   else if( !strcmp( option, "bIgnoreDocumentAttributes" ) ) {
00965     PRBool val;
00966     pref->GetBoolPref( "browser.display.use_document_colors", &val );
00967     sprintf( value, "%s", val == PR_TRUE ? "FALSE" : "TRUE" );
00968     }
00969   else if( !strcmp( option, "disable_new_windows" ) ) {
00970     sprintf( value, "%s", (moz->disable_new_windows == 1) ? "TRUE" : "FALSE" );
00971     }
00972   else if( !strcmp( option, "disable_exception_dlg" ) ) {
00973     sprintf( value, "%s", (moz->disable_exception_dlg == 1) ? "TRUE" : "FALSE" );
00974     }
00975   else if( !strcmp( option, "bUnderlineLinks" ) ) {
00976     PRBool val;
00977     pref->GetBoolPref( "browser.underline_anchors", &val );
00978     sprintf( value, "%s", val == PR_TRUE ? "TRUE" : "FALSE" );
00979     }
00980   else if( !strcmp( option, "iUserTextSize" ) ) {
00981     sprintf( value, "%d", moz->text_zoom );
00982     }
00983   else if( !strcmp( option, "BODY font-family" ) || !strcmp( option, "H* font-family" ) ) {
00984     /* set the current font */
00985     char *font_default = NULL, *font;
00986     char preference[256];
00987 
00988     pref->CopyCharPref( "font.default.x-western", &font_default );
00989     if( !font_default ) font_default = "serif";
00990 
00991     sprintf( preference, "font.name.%s.x-western", font_default );
00992     if( pref->CopyCharPref( preference, &font ) == NS_OK )
00993       strcpy( value, font );
00994     }
00995   else if( !strcmp( option, "PRE font-family" ) ) {
00996     /* set the current font */
00997     char *font;
00998     if( pref->CopyCharPref( "font.name.monospace.x-western", &font ) == NS_OK )
00999       strcpy( value, font );
01000     }
01001 
01002 /* HTTP options */
01003   else if( !strcmp( option, "http_proxy_host" ) ) {
01004     char *s;
01005     if( pref->CopyCharPref( "network.proxy.http", &s ) == NS_OK )
01006       strcpy( value, s );
01007     }
01008   else if( !strcmp( option, "http_proxy_port" ) ) {
01009     int n;
01010     pref->GetIntPref( "network.proxy.http_port", &n );
01011     sprintf( value, "%d", n );
01012     }
01013   else if( !strcmp( option, "proxy_overrides" ) ) {
01014     char *s;
01015     if( pref->CopyCharPref( "network.proxy.no_proxies_on", &s ) == NS_OK )
01016       strcpy( value, s );
01017     }
01018   else if( !strcmp( option, "https_proxy_host" ) ) {
01019     char *s;
01020     if( pref->CopyCharPref( "network.proxy.ssl", &s ) == NS_OK )
01021       strcpy( value, s );
01022     }
01023   else if( !strcmp( option, "https_proxy_port" ) ) {
01024     int n;
01025     pref->GetIntPref( "network.proxy.ssl_port", &n );
01026     sprintf( value, "%d", n );
01027     }
01028 
01029 
01030 /* FTP options */
01031   else if( !strcmp( option, "ftp_proxy_host" ) ) {
01032     char *s;
01033     if( pref->CopyCharPref( "network.proxy.ftp", &s ) == NS_OK )
01034       strcpy( value, s );
01035     }
01036   else if( !strcmp( option, "ftp_proxy_port" ) ) {
01037     int n;
01038     pref->GetIntPref( "network.proxy.ftp_port", &n );
01039     sprintf( value, "%d", n );
01040     }
01041 
01042 /* Gopher options */
01043   else if( !strcmp( option, "gopher_proxy_host" ) ) {
01044     char *s;
01045     if( pref->CopyCharPref( "network.proxy.gopher", &s ) == NS_OK )
01046       strcpy( value, s );
01047     }
01048   else if( !strcmp( option, "gopher_proxy_port" ) ) {
01049     int n;
01050     pref->GetIntPref( "gopher_proxy_port", &n );
01051     sprintf( value, "%d", n );
01052     }
01053 
01054 /* TCP/IP options */
01055   else if( !strcmp( option, "max_connections" ) ) {
01056     int n;
01057     pref->GetIntPref( "network.http.max-connections", &n );
01058     sprintf( value, "%d", n );
01059     }
01060 
01061 /* Disk-cache options */
01062   else if( !strcmp( option, "main_cache_kb_size" ) ) {
01063     int n;
01064     pref->GetIntPref( "browser.cache.disk.capacity", &n );
01065     sprintf( value, "%d", n );
01066     }
01067   else if( !strcmp( option, "enable_disk_cache" ) ) {
01068     PRBool val;
01069     pref->GetBoolPref( "browser.cache.disk.enable", &val );
01070     sprintf( value, "%s", val == PR_TRUE ? "TRUE" : "FALSE" );
01071     }
01072   else if( !strcmp( option, "dcache_verify_policy" ) ) {
01073     int n, voyager_value = 0;
01074     pref->GetIntPref( "browser.cache.check_doc_frequency", &n );
01075 // 0 = once-per-session, 1 = each-time, 2 = never, 3 = when-appropriate/automatically
01076     if( n == 0 ) voyager_value = 1; /* voyager: 1 = once per session */
01077     else if( n == 1 ) voyager_value = 2; /* voyager: 2 = always */
01078     else if( n == 2 ) voyager_value = 0; /* voyager: 0 = never */
01079     else voyager_value = 1; /* mapp the when-appropriate/automatically to the once per session */
01080     sprintf( value, "%d", voyager_value );
01081     }
01082 
01083 /* Miscellaneous options */
01084   else if( !strcmp( option, "History_Expire" ) ) {
01085     int n;
01086     pref->GetIntPref( "browser.history_expire_days", &n );
01087     sprintf( value, "%d", n );
01088     }
01089   else if( !strcmp( option, "Page_History_Length" ) ) {
01090     int n;
01091     pref->GetIntPref( "browser.sessionhistory.max_entries", &n );
01092     sprintf( value, "%d", n );
01093     }
01094 
01095   else if( !strcmp( option, "clear_main_cache_on_exit" ) ) {
01096     strcpy( value, "FALSE" ); /* even if not used, match this with the default value */
01097     }
01098   else if( !strcmp( option, "quantize_jpegs" ) ) {
01099     strcpy( value, "FALSE" ); /* even if not used, match this with the default value */
01100     }
01101   else if( !strcmp( option, "ServerId" ) ) {
01102     strcpy( value, "mozserver" );
01103     }
01104   else *value = 0;
01105 
01106   }
01107 
01108 int sProfileDirCreated;
01109 static int StartupProfile( char *sProfileDir )
01110 {
01111   // initialize profiles
01112   if(sProfileDir) 
01113   {
01114     nsresult rv;
01115     nsCOMPtr<nsILocalFile> profileDir;
01116     PRBool exists = PR_FALSE;
01117     PRBool isDir = PR_FALSE;
01118     profileDir = do_CreateInstance(NS_LOCAL_FILE_CONTRACTID);
01119     rv = profileDir->InitWithNativePath(nsDependentCString(sProfileDir));
01120     if (NS_FAILED(rv))
01121         return NS_ERROR_FAILURE;
01122     profileDir->Exists(&exists);
01123     profileDir->IsDirectory(&isDir);
01124     // if it exists and it isn't a directory then give up now.
01125     if (!exists) 
01126     {
01127       rv = profileDir->Create(nsIFile::DIRECTORY_TYPE, 0700);
01128       if( NS_FAILED(rv) )
01129         return NS_ERROR_FAILURE;
01130       sProfileDirCreated = 1;
01131     }
01132     else if (exists && !isDir)
01133       return NS_ERROR_FAILURE;
01134 
01135     nsCOMPtr<nsProfileDirServiceProvider> locProvider;
01136     NS_NewProfileDirServiceProvider(PR_TRUE, getter_AddRefs(locProvider));
01137     if (!locProvider)
01138       return NS_ERROR_FAILURE;
01139 
01140     // Directory service holds an strong reference to any
01141     // provider that is registered with it. Let it hold the
01142     // only ref. locProvider won't die when we leave this scope.
01143     rv = locProvider->Register();
01144     if (NS_FAILED(rv))
01145       return rv;
01146     rv = locProvider->SetProfileDir(profileDir);
01147     if (NS_FAILED(rv))
01148       return rv;
01149     }
01150   
01151   return NS_OK;
01152 }
01153 
01154 // startup the mozilla embedding engine
01155 static int StartupEmbedding()
01156 {
01157   nsresult rv;
01158 
01159 #ifdef _BUILD_STATIC_BIN
01160   rv = NS_InitEmbedding(nsnull, nsnull, kPStaticModules, kStaticModuleCount);
01161 #else
01162   rv = NS_InitEmbedding(nsnull, nsnull);
01163 #endif
01164 
01165   if( NS_FAILED( rv ) ) return -1;
01166 
01167   char *profile_dir;
01168   profile_dir = (char *)alloca(strlen(getenv("HOME")) + strlen("/.ph/mozilla") + 1);
01169   sprintf(profile_dir, "%s/.ph/mozilla", getenv("HOME"));
01170   rv = StartupProfile( profile_dir );
01171   if( NS_FAILED( rv ) )
01172     NS_WARNING("Warning: Failed to start up profiles.\n");
01173     
01174   nsCOMPtr<nsIAppShell> appShell;
01175   appShell = do_CreateInstance(kAppShellCID);
01176   if( !appShell ) {
01177       NS_WARNING("Failed to create appshell in EmbedPrivate::PushStartup!\n");
01178       return (-1);
01179       }
01180   nsIAppShell * sAppShell = appShell.get();
01181   NS_ADDREF(sAppShell);
01182   sAppShell->Create(0, nsnull);
01183   sAppShell->Spinup();
01184   return 0;
01185 }
01186 
01187 /* the translation string that is passed by voyager ( Pt_ARG_WEB_ENCODING ) ( taken from /usr/photon/translations/charsets ->mime field ) */
01188 /* is required to be in a different form by the mozilla browser */
01189 static void fix_translation_string( char *string ) {
01190   /* for instance it has to be windows-1252 not Windows-1252 */
01191   if( !strncmp( string, "Windows", 7 ) ) *string = 'w';
01192   }
01193 
01194 // PtMozilla class creation function
01195 PtWidgetClass_t *PtCreateMozillaClass( void )
01196 {
01197   static const PtResourceRec_t resources[] =
01198   {
01199     { Pt_ARG_MOZ_GET_URL,           mozilla_modify, Pt_QUERY_PREVENT },
01200     { Pt_ARG_MOZ_NAVIGATE_PAGE,     mozilla_modify, mozilla_get_info },
01201     { Pt_ARG_MOZ_SCROLL_TO,         mozilla_modify, Pt_QUERY_PREVENT },
01202     { Pt_ARG_MOZ_RELOAD,            mozilla_modify, Pt_QUERY_PREVENT },
01203     { Pt_ARG_MOZ_STOP,              mozilla_modify, Pt_QUERY_PREVENT },
01204     { Pt_ARG_MOZ_PRINT,             mozilla_modify, Pt_QUERY_PREVENT },
01205     { Pt_ARG_MOZ_COMMAND,           mozilla_modify, Pt_QUERY_PREVENT },
01206     { Pt_ARG_MOZ_OPTION,            mozilla_modify, mozilla_get_info },
01207     { Pt_ARG_MOZ_GET_CONTEXT,       NULL,           mozilla_get_info },
01208     { Pt_ARG_MOZ_GET_HISTORY,       NULL,           mozilla_get_info },
01209     { Pt_ARG_MOZ_ENCODING,          mozilla_modify, mozilla_get_info },
01210     { Pt_ARG_MOZ_WEB_DATA_URL,      mozilla_modify, Pt_QUERY_PREVENT },
01211     { Pt_ARG_MOZ_WEB_DATA,          mozilla_modify, Pt_QUERY_PREVENT },
01212     { Pt_ARG_MOZ_UNKNOWN_RESP,      mozilla_modify, Pt_QUERY_PREVENT },
01213     { Pt_ARG_MOZ_DOWNLOAD,          mozilla_modify, Pt_QUERY_PREVENT },
01214     { Pt_ARG_MOZ_AUTH_CTRL,         NULL, NULL, Pt_ARG_IS_POINTER( PtMozillaWidget_t, moz_auth_ctrl ) },
01215     { Pt_ARG_MOZ_UNKNOWN_CTRL,      NULL, NULL, Pt_ARG_IS_POINTER( PtMozillaWidget_t, moz_unknown_ctrl ) },
01216     { Pt_CB_MOZ_INFO,       NULL, NULL, Pt_ARG_IS_CALLBACK_LIST(PtMozillaWidget_t, info_cb) },
01217     { Pt_CB_MOZ_START,        NULL, NULL, Pt_ARG_IS_CALLBACK_LIST(PtMozillaWidget_t, start_cb) },
01218     { Pt_CB_MOZ_COMPLETE,     NULL, NULL, Pt_ARG_IS_CALLBACK_LIST(PtMozillaWidget_t, complete_cb) },
01219     { Pt_CB_MOZ_PROGRESS,     NULL, NULL, Pt_ARG_IS_CALLBACK_LIST(PtMozillaWidget_t, progress_cb) },
01220     { Pt_CB_MOZ_URL,        NULL, NULL, Pt_ARG_IS_CALLBACK_LIST(PtMozillaWidget_t, url_cb) },
01221     { Pt_CB_MOZ_EVENT,        NULL, NULL, Pt_ARG_IS_CALLBACK_LIST(PtMozillaWidget_t, event_cb) },
01222     { Pt_CB_MOZ_NET_STATE,      NULL, NULL, Pt_ARG_IS_CALLBACK_LIST(PtMozillaWidget_t, net_state_cb) },
01223     { Pt_CB_MOZ_NEW_WINDOW,     NULL, NULL, Pt_ARG_IS_CALLBACK_LIST(PtMozillaWidget_t, new_window_cb) },
01224     { Pt_CB_MOZ_NEW_AREA,       NULL, NULL, Pt_ARG_IS_CALLBACK_LIST(PtMozillaWidget_t, resize_cb) },
01225     { Pt_CB_MOZ_DESTROY,      NULL, NULL, Pt_ARG_IS_CALLBACK_LIST(PtMozillaWidget_t, destroy_cb) },
01226     { Pt_CB_MOZ_VISIBILITY,     NULL, NULL, Pt_ARG_IS_CALLBACK_LIST(PtMozillaWidget_t, visibility_cb) },
01227     { Pt_CB_MOZ_OPEN,       NULL, NULL, Pt_ARG_IS_CALLBACK_LIST(PtMozillaWidget_t, open_cb) },
01228     { Pt_CB_MOZ_DIALOG,       NULL, NULL, Pt_ARG_IS_CALLBACK_LIST(PtMozillaWidget_t, dialog_cb) },
01229     { Pt_CB_MOZ_AUTHENTICATE,   NULL, NULL, Pt_ARG_IS_CALLBACK_LIST(PtMozillaWidget_t, auth_cb) },
01230     { Pt_CB_MOZ_PROMPT,       NULL, NULL, Pt_ARG_IS_CALLBACK_LIST(PtMozillaWidget_t, prompt_cb) },
01231     { Pt_CB_MOZ_CONTEXT,      NULL, NULL, Pt_ARG_IS_CALLBACK_LIST(PtMozillaWidget_t, context_cb) },
01232     { Pt_CB_MOZ_PRINT_STATUS,   NULL, NULL, Pt_ARG_IS_CALLBACK_LIST(PtMozillaWidget_t, print_status_cb) },
01233     { Pt_CB_MOZ_WEB_DATA_REQ,   NULL, NULL, Pt_ARG_IS_CALLBACK_LIST(PtMozillaWidget_t, web_data_req_cb) },
01234     { Pt_CB_MOZ_UNKNOWN,        NULL, NULL, Pt_ARG_IS_CALLBACK_LIST(PtMozillaWidget_t, web_unknown_cb) },
01235     { Pt_CB_MOZ_ERROR,          NULL, NULL, Pt_ARG_IS_CALLBACK_LIST(PtMozillaWidget_t, web_error_cb) },
01236     { Pt_CB_MOZ_DOWNLOAD,       NULL, NULL, Pt_ARG_IS_CALLBACK_LIST(PtMozillaWidget_t, web_download_cb) }
01237   };
01238 
01239   static const PtArg_t args[] = 
01240   {
01241     { Pt_SET_VERSION, 200},
01242     { Pt_SET_STATE_LEN, sizeof( PtMozillaWidget_t ) },
01243     { Pt_SET_DFLTS_F, (long)mozilla_defaults },
01244     { Pt_SET_EXTENT_F, (long)mozilla_extent },
01245     { Pt_SET_FLAGS, Pt_RECTANGULAR, Pt_RECTANGULAR },
01246     { Pt_SET_DESTROY_F, (long) mozilla_destroy },
01247     { Pt_SET_CHILD_GETTING_FOCUS_F, ( long ) child_getting_focus },
01248     { Pt_SET_CHILD_LOSING_FOCUS_F, ( long ) child_losing_focus },
01249     { Pt_SET_RESOURCES, (long) resources },
01250     { Pt_SET_NUM_RESOURCES, sizeof( resources )/sizeof( resources[0] ) },
01251     { Pt_SET_DESCRIPTION, (long) "PtMozilla" },
01252   };
01253 
01254   if (StartupEmbedding() == -1)
01255     return (NULL);
01256 
01257   Init_nsUnknownContentTypeHandler_Factory( );
01258 
01259   nsCOMPtr<nsIFactory> promptFactory;
01260   NS_NewPromptServiceFactory(getter_AddRefs(promptFactory));
01261 
01262   nsCOMPtr<nsIComponentRegistrar> registrar;
01263   NS_GetComponentRegistrar(getter_AddRefs(registrar));
01264   registrar->RegisterFactory(kPromptServiceCID, "Prompt Service", 
01265       "@mozilla.org/embedcomp/prompt-service;1",
01266       promptFactory);
01267 
01268   nsCOMPtr<nsIObserverService> observerService(do_GetService("@mozilla.org/observer-service;1"));
01269   WindowChangeObserver *observer = new WindowChangeObserver;
01270   observer->AddRef();
01271   observerService->AddObserver(NS_STATIC_CAST(nsIObserver *, observer), "domwindowclosed", PR_FALSE);
01272 
01273   PtMozilla->wclass = PtCreateWidgetClass(PtContainer, 0, sizeof(args)/sizeof(args[0]), args);
01274 
01275   return (PtMozilla->wclass);
01276 }