Back to index

lightning-sunbird  0.9+nobinonly
mozilla_api.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  *   Doug Turner <dougt@meer.net>  Branched from TestGtkEmbed.cpp
00024  *
00025  *   The 10LE Team (in alphabetical order)
00026  *   -------------------------------------
00027  *
00028  *    Ilias Biris       <ext-ilias.biris@indt.org.br> - Coordinator
00029  *    Afonso Costa      <afonso.costa@indt.org.br>
00030  *    Antonio Gomes     <antonio.gomes@indt.org.br>
00031  *    Diego Gonzalez    <diego.gonzalez@indt.org.br>
00032  *    Raoni Novellino   <raoni.novellino@indt.org.br>
00033  *    Andre Pedralho    <andre.pedralho@indt.org.br>
00034  *
00035  *
00036  * Alternatively, the contents of this file may be used under the terms of
00037  * either the GNU General Public License Version 2 or later (the "GPL"), or
00038  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00039  * in which case the provisions of the GPL or the LGPL are applicable instead
00040  * of those above. If you wish to allow use of your version of this file only
00041  * under the terms of either the GPL or the LGPL, and not to allow others to
00042  * use your version of this file under the terms of the MPL, indicate your
00043  * decision by deleting the provisions above and replace them with the notice
00044  * and other provisions required by the GPL or the LGPL. If you do not delete
00045  * the provisions above, a recipient may use your version of this file under
00046  * the terms of any one of the MPL, the GPL or the LGPL.
00047  *
00048  * ***** END LICENSE BLOCK ***** */
00049 
00050 #include "mozilla_api.h"
00051 
00052 /*
00053  * mozilla_locale_to_unicode: Decodes a string encoded for the current
00054  * locale into unicode. Used for getting text entered in a GTK+ entry
00055  * into a form which mozilla can use.
00056  *
00057  */
00058 PRUnichar *
00059 mozilla_locale_to_unicode (const gchar *locStr)
00060 {
00061   if (locStr == NULL)
00062   {
00063     return (NULL);
00064   }
00065   nsAutoString autoStr;
00066   autoStr.AssignWithConversion (locStr);       
00067   PRUnichar *uniStr = ToNewUnicode(autoStr);
00068   return ( uniStr );
00069 }
00070 
00071 /*
00072  * mozilla_find: Look for a text in the current shown webpage
00073  *
00074  */
00075 gboolean 
00076 mozilla_find(GtkMozEmbed *b, const char *exp, PRBool IgnoreCase,
00077              PRBool SearchBackWards, PRBool DoWrapFind,
00078              PRBool SearchEntireWord, PRBool SearchInFrames,
00079              PRBool DidFind)
00080 {
00081   PRUnichar *search_string;
00082   g_return_val_if_fail(b != NULL, FALSE);
00083   nsIWebBrowser *wb = nsnull;
00084   gtk_moz_embed_get_nsIWebBrowser( b, &wb);
00085   nsCOMPtr<nsIWebBrowserFind> finder(do_GetInterface(wb));
00086   search_string = mozilla_locale_to_unicode(exp);
00087   finder->SetSearchString(search_string);
00088   
00089   finder->SetFindBackwards(SearchBackWards);
00090   finder->SetWrapFind(DoWrapFind);
00091   finder->SetEntireWord(SearchEntireWord);
00092   finder->SetSearchFrames(SearchInFrames);
00093   finder->SetMatchCase(IgnoreCase);
00094   finder->FindNext(&DidFind);
00095   
00096   g_free(search_string);
00097   return ( DidFind );
00098 }
00099 
00100 /*
00101  * mozilla_preference_set: Method used to set up some specific text-based parameters of the gecko render engine
00102  *
00103  */
00104 gboolean
00105 mozilla_preference_set (const char *preference_name, const char *new_value)
00106 {
00107   g_return_val_if_fail (preference_name != NULL, FALSE);
00108   g_return_val_if_fail (new_value != NULL, FALSE);
00109   
00110   nsCOMPtr<nsIPref> pref = do_CreateInstance(PREF_ID);
00111   
00112   if (pref)
00113   {
00114     nsresult rv = pref->SetCharPref (preference_name, new_value);
00115     return ( NS_SUCCEEDED (rv) ? TRUE : FALSE );
00116   }
00117   return (FALSE);
00118 }
00119 
00120 /*
00121  * mozilla_preference_set: Method used to set up some specific boolean-based parameters of the gecko render engine
00122  *
00123  */
00124 gboolean
00125 mozilla_preference_set_boolean (const char *preference_name, gboolean new_boolean_value)
00126 {
00127   
00128   g_return_val_if_fail (preference_name != NULL, FALSE);
00129   
00130   nsCOMPtr<nsIPref> pref = do_CreateInstance(PREF_ID);
00131   
00132   if (pref)
00133   {
00134     nsresult rv = pref->SetBoolPref (preference_name,  new_boolean_value ? PR_TRUE : PR_FALSE);
00135     return ( NS_SUCCEEDED (rv) ? TRUE : FALSE );
00136   }
00137   
00138   return (FALSE);
00139 }      
00140 
00141 /*
00142  * mozilla_preference_set: Method used to set up some specific integer-based parameters of the gecko render engine
00143  *
00144  */
00145 gboolean
00146 mozilla_preference_set_int (const char *preference_name, gint  new_int_value)
00147 {
00148   g_return_val_if_fail (preference_name != NULL, FALSE);
00149   
00150   nsCOMPtr<nsIPref> pref = do_CreateInstance(PREF_ID);
00151   
00152   if (pref)
00153   {
00154     nsresult rv = pref->SetIntPref (preference_name, new_int_value);
00155     return ( NS_SUCCEEDED (rv) ? TRUE : FALSE );
00156   }
00157   
00158   return (FALSE);
00159   
00160 }
00161 
00162 /*
00163  * mozilla_save: Method used to save an webpage and its files into the disk
00164  *
00165  */
00166 gboolean
00167 mozilla_save(GtkMozEmbed *b, gchar *file_name, gint all)
00168 {
00169   
00170   nsCOMPtr<nsIWebBrowser> wb;
00171   gint i = 0;
00172   gchar *relative_path = NULL;
00173   g_return_val_if_fail(b != NULL, FALSE);
00174   
00175   gtk_moz_embed_get_nsIWebBrowser(b,getter_AddRefs(wb));
00176   if (!wb) return (FALSE);
00177   
00178   nsCOMPtr<nsIWebNavigation> nav(do_QueryInterface(wb));
00179   
00180   nsCOMPtr<nsIDOMDocument> domDoc;
00181   nav->GetDocument(getter_AddRefs(domDoc));
00182   
00183   if (!domDoc) return (FALSE);
00184   nsCOMPtr<nsIWebBrowserPersist> persist(do_QueryInterface(wb));
00185   
00186   if (persist) {
00187     nsCOMPtr<nsILocalFile> file;
00188     nsCOMPtr<nsILocalFile> relative = nsnull;
00189     if (all) {
00190       
00191       relative_path = g_strdup(file_name);
00192       relative_path = g_strconcat(relative_path,"_files/", NULL);
00193       
00194       for (i=strlen(relative_path)-1 ; i >= 0; --i)
00195         if (relative_path[i] == '/')
00196         {
00197           relative_path[i] = '\0';
00198           break;
00199         }
00200       
00201       mkdir(relative_path,0755);
00202       
00203       nsAutoString s; s.AssignWithConversion(relative_path);
00204       NS_NewLocalFile(s, PR_TRUE, getter_AddRefs(relative));
00205     }
00206     
00207     nsAutoString s; s.AssignWithConversion(file_name);
00208     NS_NewLocalFile(s,PR_TRUE,getter_AddRefs(file));
00209     
00210     if (file) persist->SaveDocument(domDoc,file,relative, nsnull, 0, 0);
00211     
00212     if (all) g_free(relative_path);
00213     
00214     return (TRUE);
00215   }
00216   return (FALSE);
00217 }
00218 
00219 void 
00220 mozilla_save_image(GtkMozEmbed *b,gchar *location, gchar *FullPath)
00221 {
00222   nsresult rv = NS_OK;
00223   if (strlen(location) == 0 || !location) return;
00224   nsCOMPtr<nsIURI> linkURI;
00225   rv = NS_NewURI(getter_AddRefs(linkURI), location);
00226   if (NS_FAILED(rv)) return;
00227   nsIWebBrowser *wb = nsnull;
00228   gtk_moz_embed_get_nsIWebBrowser(b,&wb);
00229   nsCOMPtr<nsIWebBrowserPersist> persist(do_QueryInterface(wb));
00230   if (persist) {
00231     nsCOMPtr<nsILocalFile> file;
00232     nsAutoString s; s.AssignWithConversion(FullPath);
00233     NS_NewLocalFile(s, PR_TRUE, getter_AddRefs(file));
00234     if (!file) return;
00235     persist->SaveURI(linkURI, nsnull, nsnull, nsnull, nsnull, file);
00236   }
00237   return;
00238 }
00239 
00240 /*
00241  * mozilla_get_link_message: Method used to get "link messages" from the Gecko Render Engine. 
00242  * eg. When the user put the mouse over a link, the render engine throws messages that can be cautch for after 
00243  * used by Minimo Browser.
00244  *
00245  */
00246 
00247 gchar *
00248 mozilla_get_link_message (GtkWidget *embed) 
00249 {  
00250   gchar *link;
00251   
00252   link = gtk_moz_embed_get_link_message (GTK_MOZ_EMBED (embed));
00253   return link;  
00254 }
00255 
00256  
00257 gboolean 
00258 mozilla_save_prefs (void) 
00259 {
00260   nsCOMPtr<nsIPrefService> prefService = do_GetService (PREF_ID);
00261   g_return_val_if_fail (prefService != nsnull, FALSE);
00262   nsresult rv = prefService->SavePrefFile (nsnull);
00263   return NS_SUCCEEDED (rv) ? TRUE : FALSE;
00264 }
00265 
00266 glong 
00267 mozilla_get_context_menu_type(GtkMozEmbed *b, gpointer event,  gchar **img,  gchar **link, gchar **linktext)
00268 {
00269   nsresult result;
00270   *img = *link = NULL;
00271   
00272   nsIDOMMouseEvent *aMouseEvent =  (nsIDOMMouseEvent*)event;
00273   
00274   nsCOMPtr<nsIDOMEventTarget> OriginalTarget;
00275   
00276   nsCOMPtr<nsIDOMNSEvent> aEvent = do_QueryInterface(aMouseEvent);
00277   if (!aEvent) return(CONTEXT_NONE);
00278   result = aEvent->GetOriginalTarget(getter_AddRefs(OriginalTarget));
00279   
00280   if (NS_FAILED(result) || !OriginalTarget) return (CONTEXT_NONE);
00281   
00282   nsCOMPtr<nsIDOMNode> OriginalNode = do_QueryInterface(OriginalTarget);
00283   if (!OriginalNode) return (CONTEXT_NONE);
00284   
00285   nsString nodename;
00286   OriginalNode->GetNodeName(nodename);
00287   
00288   if (nodename.EqualsLiteral ("xul:thumb") ||
00289       nodename.EqualsLiteral ("xul:slider"))
00290     return (CONTEXT_NONE);
00291   
00292   nsCOMPtr<nsIDOMEventTarget> EventTarget;
00293   result = aMouseEvent->GetTarget(getter_AddRefs(EventTarget));
00294   if (NS_FAILED(result) || !EventTarget) return (  CONTEXT_NONE );
00295   
00296   nsCOMPtr<nsIDOMNode> node = do_QueryInterface(EventTarget);
00297   if (!node) return (CONTEXT_NONE);
00298   
00299   nsCOMPtr<nsIDOMDocument> domDoc;
00300   result = node->GetOwnerDocument(getter_AddRefs(domDoc));
00301   if (!NS_SUCCEEDED (result) || !domDoc) return (CONTEXT_NONE);
00302   
00303   nsCOMPtr<nsIDocument> doc = do_QueryInterface(domDoc);
00304   if(!doc) return (CONTEXT_NONE);
00305   
00306   nsCOMPtr<nsIDOMNSDocument> nsDoc = do_QueryInterface(domDoc);
00307   if (!nsDoc) return (CONTEXT_NONE);
00308   
00309   nsIURI *baseURI;
00310   
00311   baseURI = doc->GetBaseURI ();
00312   
00313   if (!baseURI) return (CONTEXT_NONE);
00314   
00315   nsString mime;
00316   nsDoc->GetContentType(mime);
00317   if (mime.EqualsLiteral("text/xul"))  return (CONTEXT_NONE);
00318   
00319   PRUint32 flags = CONTEXT_NONE;
00320   nsCOMPtr<nsIDOMHTMLElement> element;
00321   
00322   do {
00323     PRUint16 type;
00324     node->GetNodeType(&type);
00325     
00326     element = do_QueryInterface(node);
00327     if (element)
00328     {
00329       nsAutoString tag;
00330       element->GetTagName(tag);
00331       
00332       if (tag.EqualsIgnoreCase("input"))
00333       {
00334         flags |= CONTEXT_INPUT;
00335       }
00336       else if (tag.EqualsIgnoreCase("img")) //if (tag.EqualsWithConversion("img", PR_TRUE))
00337       {
00338         flags |= CONTEXT_IMAGE;
00339         
00340         char *src = mozilla_get_attribute (node, "src");
00341         if (!src)  return (  CONTEXT_NONE );
00342         
00343         nsAutoString srca;
00344         srca.AssignWithConversion(src);
00345         
00346         nsCString srcc,imgc;
00347         srcc.AssignWithConversion(srca);
00348         result = baseURI->Resolve(srcc, imgc);
00349         g_free(src);
00350         
00351         *img = ToNewCString(imgc);
00352         
00353         if (!img) return (CONTEXT_NONE);
00354               
00355       }
00356       
00357       else
00358       {
00359         flags |= CONTEXT_OTHER;
00360       }
00361       
00362       nsCOMPtr<nsIDOMNamedNodeMap> attributes;
00363       node->GetAttributes(getter_AddRefs(attributes));
00364       if (attributes)
00365       {
00366         nsCOMPtr<nsIDOMNode> hrefNode;
00367         nsAutoString href;
00368         href.AssignWithConversion("href");
00369         attributes->GetNamedItem(href, getter_AddRefs(hrefNode));
00370         if (hrefNode)
00371         {
00372           flags |= CONTEXT_LINK;
00373           nsAutoString linkhtml;
00374           nsCOMPtr<nsIDOMNSHTMLElement> nsElement;
00375                      
00376           nsElement = do_QueryInterface (element);
00377           if (nsElement)
00378           {
00379             result = nsElement->GetInnerHTML(linkhtml);
00380             if (NS_SUCCEEDED(result) && ToNewCString(linkhtml))
00381               *linktext = g_strdup(ToNewCString(linkhtml));
00382           }
00383           
00384           char *href =  mozilla_get_attribute (node, "href");
00385           if (!href)   return (CONTEXT_NONE );
00386           
00387           nsAutoString hrefa;
00388           hrefa.AssignWithConversion(href);
00389           
00390           nsCString hrefc,linkc;
00391           hrefc.AssignWithConversion(hrefa);
00392           
00393           result = baseURI->Resolve(hrefc,linkc);
00394           g_free(href);
00395           
00396           *link = ToNewCString(linkc);
00397           
00398           if (!link)  return (CONTEXT_NONE);
00399           
00400           break;
00401         }
00402       }
00403     }
00404     nsCOMPtr<nsIDOMNode> parentNode;
00405     node->GetParentNode(getter_AddRefs(parentNode));
00406     
00407     if (!parentNode)
00408     {
00409       node = nsnull;
00410       flags |= CONTEXT_DOCUMENT;
00411       break;
00412     }
00413     node = parentNode;
00414   } while (node);
00415   
00416   return(flags);
00417 }
00418 
00419 gchar *
00420 mozilla_get_attribute (nsIDOMNode *node, gchar *attribute)
00421 {
00422   nsresult result;
00423   
00424   nsCOMPtr<nsIDOMNamedNodeMap> attributes;
00425   result = node->GetAttributes(getter_AddRefs (attributes));
00426   if (!NS_SUCCEEDED (result) || !attributes) return ( NULL );
00427   
00428   nsAutoString attr;
00429   
00430   attr.AssignWithConversion (attribute);
00431   
00432   nsCOMPtr<nsIDOMNode> attrNode;
00433   result = attributes->GetNamedItem (attr, getter_AddRefs (attrNode));
00434   if (!NS_SUCCEEDED (result) || !attrNode)  return (NULL);
00435   
00436   nsAutoString nodeValue;
00437   
00438   result = attrNode->GetNodeValue (nodeValue);
00439   if (!NS_SUCCEEDED (result))  return (NULL);
00440   
00441   char *cstr = ToNewCString(nodeValue);
00442   char *lifeSucks = g_strdup (cstr);
00443   
00444   nsMemory::Free (cstr);
00445   
00446   return (lifeSucks);
00447 }