Compute lucene-style scores for our hits.
[beagle.git] / glue / gecko-utils.cpp
blob41bca7c2ceeebaf538fbf3f1fb2fda3b273c4681
2 // -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 //
4 // Copyright (C) 2004 Imendio AB
5 // Copyright (C) 2004 Marco Pesenti Gritti
6 //
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a
9 // copy of this software and associated documentation files (the "Software"),
10 // to deal in the Software without restriction, including without limitation
11 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 // and/or sell copies of the Software, and to permit persons to whom the
13 // Software is furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 // DEALINGS IN THE SOFTWARE.
26 #include <config.h>
28 #include <gtkmozembed.h>
30 #include <stdlib.h>
32 #include <nsEmbedString.h>
33 #include <nsIPrefService.h>
34 #include <nsIServiceManager.h>
36 #if defined (HAVE_CHROME_NSICHROMEREGISTRYSEA_H)
37 #include <chrome/nsIChromeRegistrySea.h>
38 #elif defined(MOZ_NSIXULCHROMEREGISTRY_SELECTSKIN)
39 #include <nsIChromeRegistry.h>
40 #endif
42 #ifdef ALLOW_PRIVATE_API
43 // FIXME: For setting the locale. hopefully gtkmozembed will do itself soon
44 #include <nsILocaleService.h>
45 #endif
48 #include "gecko-utils.h"
50 static gboolean
51 blam_util_split_font_string (const gchar *font_name, gchar **name, gint *size)
53 gchar *tmp_name, *ch;
55 tmp_name = g_strdup (font_name);
57 ch = g_utf8_strrchr (tmp_name, -1, ' ');
58 if (!ch || ch == tmp_name) {
59 return FALSE;
62 *ch = '\0';
64 *name = g_strdup (tmp_name);
65 *size = strtol (ch + 1, (char **) NULL, 10);
67 /* Temporary hack to make the font a bit bigger ;) */
68 /* *size = *size + 3; */
70 return TRUE;
73 static gboolean
74 gecko_prefs_set_string (const gchar *key, const gchar *value)
76 nsCOMPtr<nsIPrefService> prefService =
77 do_GetService (NS_PREFSERVICE_CONTRACTID);
78 nsCOMPtr<nsIPrefBranch> pref;
79 prefService->GetBranch ("", getter_AddRefs (pref));
81 if (pref) {
82 nsresult rv = pref->SetCharPref (key, value);
83 return NS_SUCCEEDED (rv) ? TRUE : FALSE;
86 return FALSE;
90 static gboolean
91 gecko_prefs_set_int (const gchar *key, gint value)
93 nsCOMPtr<nsIPrefService> prefService =
94 do_GetService (NS_PREFSERVICE_CONTRACTID);
95 nsCOMPtr<nsIPrefBranch> pref;
96 prefService->GetBranch ("", getter_AddRefs (pref));
98 if (pref) {
99 nsresult rv = pref->SetIntPref (key, value);
100 return NS_SUCCEEDED (rv) ? TRUE : FALSE;
103 return FALSE;
106 extern "C" void
107 blam_gecko_utils_set_font (gint type, const gchar *fontname)
109 gchar *name;
110 gint size;
112 name = NULL;
113 if (!blam_util_split_font_string (fontname, &name, &size)) {
114 g_free (name);
115 return;
118 switch (type) {
119 case BLAM_GECKO_PREF_FONT_VARIABLE:
120 gecko_prefs_set_string ("font.name.variable.x-western",
121 name);
122 gecko_prefs_set_int ("font.size.variable.x-western",
123 size);
124 break;
125 case BLAM_GECKO_PREF_FONT_FIXED:
126 gecko_prefs_set_string ("font.name.fixed.x-western",
127 name);
128 gecko_prefs_set_int ("font.size.fixed.x-western",
129 size);
130 break;
133 g_free (name);
136 static nsresult
137 getUILang (nsAString& aUILang)
139 nsresult rv;
141 nsCOMPtr<nsILocaleService> localeService = do_GetService (NS_LOCALESERVICE_CONTRACTID);
142 if (!localeService)
144 g_warning ("Could not get locale service!\n");
145 return NS_ERROR_FAILURE;
148 rv = localeService->GetLocaleComponentForUserAgent (aUILang);
150 if (NS_FAILED (rv))
152 g_warning ("Could not determine locale!\n");
153 return NS_ERROR_FAILURE;
156 return NS_OK;
159 static nsresult
160 gecko_utils_init_chrome (void)
162 /* FIXME: can we just omit this on new-toolkit ? */
163 #if defined(MOZ_NSIXULCHROMEREGISTRY_SELECTSKIN) || defined(HAVE_CHROME_NSICHROMEREGISTRYSEA_H)
164 nsresult rv;
165 nsEmbedString uiLang;
167 #ifdef HAVE_CHROME_NSICHROMEREGISTRYSEA_H
168 nsCOMPtr<nsIChromeRegistrySea> chromeRegistry = do_GetService (NS_CHROMEREGISTRY_CONTRACTID);
169 #else
170 nsCOMPtr<nsIXULChromeRegistry> chromeRegistry = do_GetService (NS_CHROMEREGISTRY_CONTRACTID);
171 #endif
172 NS_ENSURE_TRUE (chromeRegistry, NS_ERROR_FAILURE);
174 // Set skin to 'classic' so we get native scrollbars.
175 rv = chromeRegistry->SelectSkin (nsEmbedCString("classic/1.0"), PR_FALSE);
176 NS_ENSURE_SUCCESS (rv, NS_ERROR_FAILURE);
178 // set locale
179 rv = chromeRegistry->SetRuntimeProvider(PR_TRUE);
180 NS_ENSURE_SUCCESS (rv, NS_ERROR_FAILURE);
182 rv = getUILang(uiLang);
183 NS_ENSURE_SUCCESS (rv, NS_ERROR_FAILURE);
185 nsEmbedCString cUILang;
186 NS_UTF16ToCString (uiLang, NS_CSTRING_ENCODING_UTF8, cUILang);
188 return chromeRegistry->SelectLocale (cUILang, PR_FALSE);
189 #else
190 return NS_OK;
191 #endif
194 extern "C" void
195 blam_gecko_utils_init_services (void)
197 gchar *profile_dir;
199 gtk_moz_embed_set_comp_path (MOZILLA_HOME);
201 profile_dir = g_build_filename (g_getenv ("HOME"),
202 ".gnome2",
203 "blam",
204 "mozilla", NULL);
206 gtk_moz_embed_set_profile_path (profile_dir, "blam");
207 g_free (profile_dir);
209 gtk_moz_embed_push_startup ();
211 gecko_prefs_set_string ("font.size.unit", "pt");
212 gecko_utils_init_chrome ();
215 extern "C" void
216 blam_gecko_utils_set_proxy (gboolean use_proxy, gchar *host, gint port)
218 if (!use_proxy) {
219 gecko_prefs_set_int ("network.proxy.type", 0);
220 } else {
221 gecko_prefs_set_int ("network.proxy.type", 1);
222 gecko_prefs_set_string ("network.proxy.http", host);
223 gecko_prefs_set_int ("network.proxy.http_port", port);