main.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. /*
  2. * Copyright (C) 2006, 2007 Apple Inc.
  3. * Copyright (C) 2007 Alp Toker <alp@atoker.com>
  4. * Copyright (C) 2011 Lukasz Slachciak
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
  16. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  18. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
  19. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  23. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "autotoolsconfig.h"
  28. #include "LauncherInspectorWindow.h"
  29. #include <errno.h>
  30. #include <gdk/gdkkeysyms.h>
  31. #ifdef WTF_USE_GSTREAMER
  32. #include <gst/gst.h>
  33. #endif
  34. #include <gtk/gtk.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <webkit/webkit.h>
  38. static gint windowCount = 0;
  39. static GtkWidget* createWindow(WebKitWebView** outWebView);
  40. static void activateUriEntryCb(GtkWidget* entry, gpointer data)
  41. {
  42. WebKitWebView *webView = g_object_get_data(G_OBJECT(entry), "web-view");
  43. const gchar* uri = gtk_entry_get_text(GTK_ENTRY(entry));
  44. g_assert(uri);
  45. gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(entry), GTK_ENTRY_ICON_PRIMARY, 0);
  46. webkit_web_view_load_uri(webView, uri);
  47. }
  48. static void updateTitle(GtkWindow* window, WebKitWebView* webView)
  49. {
  50. GString *string = g_string_new(webkit_web_view_get_title(webView));
  51. gdouble loadProgress = webkit_web_view_get_progress(webView) * 100;
  52. g_string_append(string, " - WebKit Launcher");
  53. if (loadProgress < 100)
  54. g_string_append_printf(string, " (%f%%)", loadProgress);
  55. gchar *title = g_string_free(string, FALSE);
  56. gtk_window_set_title(window, title);
  57. g_free(title);
  58. }
  59. static void linkHoverCb(WebKitWebView* page, const gchar* title, const gchar* link, GtkStatusbar* statusbar)
  60. {
  61. guint statusContextId =
  62. GPOINTER_TO_UINT(g_object_get_data(G_OBJECT(statusbar), "link-hover-context"));
  63. /* underflow is allowed */
  64. gtk_statusbar_pop(statusbar, statusContextId);
  65. if (link)
  66. gtk_statusbar_push(statusbar, statusContextId, link);
  67. }
  68. static void notifyTitleCb(WebKitWebView* webView, GParamSpec* pspec, GtkWidget* window)
  69. {
  70. updateTitle(GTK_WINDOW(window), webView);
  71. }
  72. static void notifyLoadStatusCb(WebKitWebView* webView, GParamSpec* pspec, GtkWidget* uriEntry)
  73. {
  74. if (webkit_web_view_get_load_status(webView) == WEBKIT_LOAD_COMMITTED) {
  75. WebKitWebFrame *frame = webkit_web_view_get_main_frame(webView);
  76. const gchar *uri = webkit_web_frame_get_uri(frame);
  77. if (uri)
  78. gtk_entry_set_text(GTK_ENTRY(uriEntry), uri);
  79. }
  80. }
  81. static void notifyProgressCb(WebKitWebView* webView, GParamSpec* pspec, GtkWidget* window)
  82. {
  83. updateTitle(GTK_WINDOW(window), webView);
  84. }
  85. static void destroyCb(GtkWidget* widget, GtkWidget* window)
  86. {
  87. if (g_atomic_int_dec_and_test(&windowCount))
  88. gtk_main_quit();
  89. }
  90. static void goBackCb(GtkWidget* widget, WebKitWebView* webView)
  91. {
  92. webkit_web_view_go_back(webView);
  93. }
  94. static void goForwardCb(GtkWidget* widget, WebKitWebView* webView)
  95. {
  96. webkit_web_view_go_forward(webView);
  97. }
  98. static void reloadCb(GtkWidget* widget, WebKitWebView* webView)
  99. {
  100. webkit_web_view_reload(webView);
  101. }
  102. static WebKitWebView*
  103. createWebViewCb(WebKitWebView* webView, WebKitWebFrame* web_frame, GtkWidget* window)
  104. {
  105. WebKitWebView *newWebView;
  106. createWindow(&newWebView);
  107. webkit_web_view_set_settings(newWebView, webkit_web_view_get_settings(webView));
  108. return newWebView;
  109. }
  110. static gboolean webViewReadyCb(WebKitWebView* webView, GtkWidget* window)
  111. {
  112. gtk_widget_grab_focus(GTK_WIDGET(webView));
  113. gtk_widget_show_all(window);
  114. return FALSE;
  115. }
  116. static gboolean closeWebViewCb(WebKitWebView* webView, GtkWidget* window)
  117. {
  118. gtk_widget_destroy(window);
  119. return TRUE;
  120. }
  121. static gboolean webViewFullscreenMessageWindowClose(GtkWidget *dialog)
  122. {
  123. if (GTK_IS_WIDGET(dialog))
  124. gtk_widget_destroy(dialog);
  125. return FALSE;
  126. }
  127. static gboolean webViewWindowStateEvent(GtkWidget *widget, GdkEventWindowState *event, WebKitWebView *webView)
  128. {
  129. if (event->new_window_state & GDK_WINDOW_STATE_FULLSCREEN) {
  130. WebKitWebFrame *frame = webkit_web_view_get_main_frame(webView);
  131. const gchar *uri = webkit_web_frame_get_uri(frame);
  132. GtkWidget *window = gtk_widget_get_toplevel(GTK_WIDGET(webView));
  133. if (!gtk_widget_is_toplevel(window) || !GTK_IS_WINDOW(window) || GTK_IS_OFFSCREEN_WINDOW(window))
  134. window = 0;
  135. GtkWidget *dialog = gtk_message_dialog_new(window ? GTK_WINDOW(window) : 0,
  136. GTK_DIALOG_MODAL,
  137. GTK_MESSAGE_INFO,
  138. GTK_BUTTONS_CLOSE,
  139. "%s is now full screen. Press ESC or f to exit.", uri);
  140. g_signal_connect_swapped(dialog, "response", G_CALLBACK(gtk_widget_destroy), dialog);
  141. g_timeout_add(1500, (GSourceFunc) webViewFullscreenMessageWindowClose, dialog);
  142. gtk_dialog_run(GTK_DIALOG(dialog));
  143. }
  144. return TRUE;
  145. }
  146. static void hideWidget(GtkWidget* widget, gpointer data)
  147. {
  148. if (!GTK_IS_SCROLLED_WINDOW(widget))
  149. gtk_widget_hide(widget);
  150. }
  151. static void showWidget(GtkWidget* widget, gpointer data)
  152. {
  153. if (!GTK_IS_SCROLLED_WINDOW(widget))
  154. gtk_widget_show(widget);
  155. }
  156. static gboolean webViewEnteringFullScreen(WebKitWebView *webView, GObject *element, GtkWidget* vbox)
  157. {
  158. WebKitWebFrame *frame = webkit_web_view_get_main_frame(webView);
  159. const gchar *uri = webkit_web_frame_get_uri(frame);
  160. GtkWidget *window = gtk_widget_get_toplevel(GTK_WIDGET(webView));
  161. if (!gtk_widget_is_toplevel(window) || !GTK_IS_WINDOW(window) || GTK_IS_OFFSCREEN_WINDOW(window))
  162. window = 0;
  163. GtkWidget *dialog = gtk_message_dialog_new(window ? GTK_WINDOW(window) : 0,
  164. GTK_DIALOG_MODAL,
  165. GTK_MESSAGE_INFO,
  166. GTK_BUTTONS_YES_NO,
  167. "Allow full screen display of %s ?", uri);
  168. gint result = gtk_dialog_run(GTK_DIALOG(dialog));
  169. if (result == GTK_RESPONSE_YES) {
  170. gtk_container_foreach(GTK_CONTAINER(vbox), (GtkCallback) hideWidget, NULL);
  171. gtk_widget_destroy(GTK_WIDGET(dialog));
  172. return FALSE;
  173. }
  174. gtk_widget_destroy(GTK_WIDGET(dialog));
  175. return TRUE;
  176. }
  177. static gboolean webViewLeavingFullScreen(WebKitWebView *webView, GObject *element, GtkWidget* vbox)
  178. {
  179. GtkWidget *window = gtk_widget_get_toplevel(GTK_WIDGET(webView));
  180. if (gtk_widget_is_toplevel(window) && GTK_IS_WINDOW(window) && !GTK_IS_OFFSCREEN_WINDOW(window))
  181. g_signal_handlers_disconnect_by_func(window, G_CALLBACK(webViewWindowStateEvent), webView);
  182. gtk_container_foreach(GTK_CONTAINER(vbox), (GtkCallback) showWidget, NULL);
  183. return FALSE;
  184. }
  185. static void iconLoadedCb(WebKitWebView* webView, const char* iconURI, GtkWidget* uriEntry)
  186. {
  187. GdkPixbuf *icon = webkit_web_view_try_get_favicon_pixbuf(webView, 16, 16);
  188. if (!icon)
  189. return;
  190. gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(uriEntry), GTK_ENTRY_ICON_PRIMARY, icon);
  191. g_object_unref(icon);
  192. }
  193. static GtkWidget *inspectorInspectWebViewCb(WebKitWebInspector *inspector, WebKitWebView *webView, GtkWindow* window)
  194. {
  195. GtkWidget *inspectorWindow = launcherInspectorWindowNew(inspector, window);
  196. return GTK_WIDGET(launcherInspectorWindowGetWebView(LAUNCHER_INSPECTOR_WINDOW(inspectorWindow)));
  197. }
  198. static GtkWidget* createBrowser(GtkWidget* window, GtkWidget* uriEntry, GtkWidget* statusbar, WebKitWebView* webView, GtkWidget* vbox)
  199. {
  200. char *iconDatabasePath;
  201. GtkWidget *scrolledWindow = gtk_scrolled_window_new(NULL, NULL);
  202. gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledWindow), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
  203. gtk_container_add(GTK_CONTAINER(scrolledWindow), GTK_WIDGET(webView));
  204. iconDatabasePath = g_build_filename(g_get_user_data_dir(), "webkit", "icondatabase", NULL);
  205. webkit_favicon_database_set_path(webkit_get_favicon_database(), iconDatabasePath);
  206. g_free(iconDatabasePath);
  207. g_signal_connect(webView, "notify::title", G_CALLBACK(notifyTitleCb), window);
  208. g_signal_connect(webView, "notify::load-status", G_CALLBACK(notifyLoadStatusCb), uriEntry);
  209. g_signal_connect(webView, "notify::progress", G_CALLBACK(notifyProgressCb), window);
  210. g_signal_connect(webView, "icon-loaded", G_CALLBACK(iconLoadedCb), uriEntry);
  211. g_signal_connect(webView, "hovering-over-link", G_CALLBACK(linkHoverCb), statusbar);
  212. g_signal_connect(webView, "create-web-view", G_CALLBACK(createWebViewCb), window);
  213. g_signal_connect(webView, "web-view-ready", G_CALLBACK(webViewReadyCb), window);
  214. g_signal_connect(webView, "close-web-view", G_CALLBACK(closeWebViewCb), window);
  215. g_signal_connect(webView, "entering-fullscreen", G_CALLBACK(webViewEnteringFullScreen), vbox);
  216. g_signal_connect(webView, "leaving-fullscreen", G_CALLBACK(webViewLeavingFullScreen), vbox);
  217. g_signal_connect(webkit_web_view_get_inspector(webView), "inspect-web-view", G_CALLBACK(inspectorInspectWebViewCb), window);
  218. return scrolledWindow;
  219. }
  220. static GtkWidget* createStatusbar()
  221. {
  222. GtkStatusbar *statusbar = GTK_STATUSBAR(gtk_statusbar_new());
  223. guint statusContextId = gtk_statusbar_get_context_id(statusbar, "Link Hover");
  224. g_object_set_data(G_OBJECT(statusbar), "link-hover-context",
  225. GUINT_TO_POINTER(statusContextId));
  226. return GTK_WIDGET(statusbar);
  227. }
  228. static GtkWidget* createToolbar(GtkWidget* window, GtkWidget* uriEntry, WebKitWebView* webView)
  229. {
  230. GtkWidget *toolbar = gtk_toolbar_new();
  231. gtk_orientable_set_orientation(GTK_ORIENTABLE(toolbar), GTK_ORIENTATION_HORIZONTAL);
  232. gtk_toolbar_set_style(GTK_TOOLBAR(toolbar), GTK_TOOLBAR_BOTH_HORIZ);
  233. GtkToolItem *item;
  234. /* Keyboard accelerators */
  235. GtkAccelGroup *accelGroup = gtk_accel_group_new();
  236. gtk_window_add_accel_group(GTK_WINDOW(window), accelGroup);
  237. /* the back button */
  238. item = gtk_tool_button_new_from_stock(GTK_STOCK_GO_BACK);
  239. g_signal_connect(G_OBJECT(item), "clicked", G_CALLBACK(goBackCb), webView);
  240. gtk_toolbar_insert(GTK_TOOLBAR(toolbar), item, -1);
  241. /* The forward button */
  242. item = gtk_tool_button_new_from_stock(GTK_STOCK_GO_FORWARD);
  243. g_signal_connect(G_OBJECT(item), "clicked", G_CALLBACK(goForwardCb), webView);
  244. gtk_toolbar_insert(GTK_TOOLBAR(toolbar), item, -1);
  245. /* The reload button */
  246. item = gtk_tool_button_new_from_stock(GTK_STOCK_REFRESH);
  247. g_signal_connect(G_OBJECT(item), "clicked", G_CALLBACK(reloadCb), webView);
  248. gtk_toolbar_insert(GTK_TOOLBAR(toolbar), item, -1);
  249. gtk_widget_add_accelerator(GTK_WIDGET(item), "clicked", accelGroup, GDK_KEY_F5, 0, GTK_ACCEL_VISIBLE);
  250. /* The URL entry */
  251. item = gtk_tool_item_new();
  252. gtk_tool_item_set_expand(item, TRUE);
  253. gtk_container_add(GTK_CONTAINER(item), uriEntry);
  254. g_signal_connect(G_OBJECT(uriEntry), "activate", G_CALLBACK(activateUriEntryCb), NULL);
  255. gtk_toolbar_insert(GTK_TOOLBAR(toolbar), item, -1);
  256. /* The go button */
  257. g_object_set_data(G_OBJECT(uriEntry), "web-view", webView);
  258. item = gtk_tool_button_new_from_stock(GTK_STOCK_OK);
  259. g_signal_connect_swapped(G_OBJECT(item), "clicked", G_CALLBACK(activateUriEntryCb), (gpointer)uriEntry);
  260. gtk_toolbar_insert(GTK_TOOLBAR(toolbar), item, -1);
  261. return toolbar;
  262. }
  263. static GtkWidget* createWindow(WebKitWebView** outWebView)
  264. {
  265. WebKitWebView *webView;
  266. GtkWidget *vbox;
  267. GtkWidget *window;
  268. GtkWidget *uriEntry;
  269. GtkWidget *statusbar;
  270. g_atomic_int_inc(&windowCount);
  271. window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  272. gtk_window_set_default_size(GTK_WINDOW(window), 800, 600);
  273. gtk_widget_set_name(window, "GtkLauncher");
  274. webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
  275. uriEntry = gtk_entry_new();
  276. #ifdef GTK_API_VERSION_2
  277. vbox = gtk_vbox_new(FALSE, 0);
  278. #else
  279. vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
  280. #endif
  281. statusbar = createStatusbar(webView);
  282. gtk_box_pack_start(GTK_BOX(vbox), createToolbar(window, uriEntry, webView), FALSE, FALSE, 0);
  283. gtk_box_pack_start(GTK_BOX(vbox), createBrowser(window, uriEntry, statusbar, webView, vbox), TRUE, TRUE, 0);
  284. gtk_box_pack_start(GTK_BOX(vbox), statusbar, FALSE, FALSE, 0);
  285. gtk_container_add(GTK_CONTAINER(window), vbox);
  286. g_signal_connect(window, "destroy", G_CALLBACK(destroyCb), NULL);
  287. if (outWebView)
  288. *outWebView = webView;
  289. return window;
  290. }
  291. static gchar* filenameToURL(const char* filename)
  292. {
  293. if (!g_file_test(filename, G_FILE_TEST_EXISTS))
  294. return NULL;
  295. GFile *gfile = g_file_new_for_path(filename);
  296. gchar *fileURL = g_file_get_uri(gfile);
  297. g_object_unref(gfile);
  298. return fileURL;
  299. }
  300. static gboolean parseOptionEntryCallback(const gchar *optionNameFull, const gchar *value, WebKitWebSettings *webSettings, GError **error)
  301. {
  302. if (strlen(optionNameFull) <= 2) {
  303. g_set_error(error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED, "Invalid option %s", optionNameFull);
  304. return FALSE;
  305. }
  306. /* We have two -- in option name so remove them. */
  307. const gchar *optionName = optionNameFull + 2;
  308. GParamSpec *spec = g_object_class_find_property(G_OBJECT_GET_CLASS(webSettings), optionName);
  309. if (!spec) {
  310. g_set_error(error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED, "Cannot find web settings for option %s", optionNameFull);
  311. return FALSE;
  312. }
  313. switch (G_PARAM_SPEC_VALUE_TYPE(spec)) {
  314. case G_TYPE_BOOLEAN: {
  315. gboolean propertyValue = TRUE;
  316. if (value && g_ascii_strcasecmp(value, "true") && strcmp(value, "1"))
  317. propertyValue = FALSE;
  318. g_object_set(G_OBJECT(webSettings), optionName, propertyValue, NULL);
  319. break;
  320. }
  321. case G_TYPE_STRING:
  322. g_object_set(G_OBJECT(webSettings), optionName, value, NULL);
  323. break;
  324. case G_TYPE_INT: {
  325. glong propertyValue;
  326. gchar *end;
  327. errno = 0;
  328. propertyValue = g_ascii_strtoll(value, &end, 0);
  329. if (errno == ERANGE || propertyValue > G_MAXINT || propertyValue < G_MININT) {
  330. g_set_error(error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, "Integer value '%s' for %s out of range", value, optionNameFull);
  331. return FALSE;
  332. }
  333. if (errno || value == end) {
  334. g_set_error(error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, "Cannot parse integer value '%s' for %s", value, optionNameFull);
  335. return FALSE;
  336. }
  337. g_object_set(G_OBJECT(webSettings), optionName, propertyValue, NULL);
  338. break;
  339. }
  340. case G_TYPE_FLOAT: {
  341. gdouble propertyValue;
  342. gchar *end;
  343. errno = 0;
  344. propertyValue = g_ascii_strtod(value, &end);
  345. if (errno == ERANGE || propertyValue > G_MAXFLOAT || propertyValue < G_MINFLOAT) {
  346. g_set_error(error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, "Float value '%s' for %s out of range", value, optionNameFull);
  347. return FALSE;
  348. }
  349. if (errno || value == end) {
  350. g_set_error(error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, "Cannot parse float value '%s' for %s", value, optionNameFull);
  351. return FALSE;
  352. }
  353. g_object_set(G_OBJECT(webSettings), optionName, propertyValue, NULL);
  354. break;
  355. }
  356. default:
  357. g_assert_not_reached();
  358. }
  359. return TRUE;
  360. }
  361. static gboolean isValidParameterType(GType gParamType)
  362. {
  363. return (gParamType == G_TYPE_BOOLEAN || gParamType == G_TYPE_STRING || gParamType == G_TYPE_INT
  364. || gParamType == G_TYPE_FLOAT);
  365. }
  366. static GOptionEntry* getOptionEntriesFromWebKitWebSettings(WebKitWebSettings *webSettings)
  367. {
  368. GParamSpec **propertySpecs;
  369. GOptionEntry *optionEntries;
  370. guint numProperties, numEntries, i;
  371. propertySpecs = g_object_class_list_properties(G_OBJECT_GET_CLASS(webSettings), &numProperties);
  372. if (!propertySpecs)
  373. return NULL;
  374. optionEntries = g_new0(GOptionEntry, numProperties + 1);
  375. numEntries = 0;
  376. for (i = 0; i < numProperties; i++) {
  377. GParamSpec *param = propertySpecs[i];
  378. /* Fill in structures only for writable and not construct-only properties. */
  379. if (!param || !(param->flags & G_PARAM_WRITABLE) || (param->flags & G_PARAM_CONSTRUCT_ONLY))
  380. continue;
  381. GType gParamType = G_PARAM_SPEC_VALUE_TYPE(param);
  382. if (!isValidParameterType(gParamType))
  383. continue;
  384. GOptionEntry *optionEntry = &optionEntries[numEntries++];
  385. optionEntry->long_name = g_param_spec_get_name(param);
  386. /* There is no easy way to figure our short name for generated option entries.
  387. optionEntry.short_name=*/
  388. /* For bool arguments "enable" type make option argument not required. */
  389. if (gParamType == G_TYPE_BOOLEAN && (strstr(optionEntry->long_name, "enable")))
  390. optionEntry->flags = G_OPTION_FLAG_OPTIONAL_ARG;
  391. optionEntry->arg = G_OPTION_ARG_CALLBACK;
  392. optionEntry->arg_data = parseOptionEntryCallback;
  393. optionEntry->description = g_param_spec_get_blurb(param);
  394. optionEntry->arg_description = g_type_name(gParamType);
  395. }
  396. g_free(propertySpecs);
  397. return optionEntries;
  398. }
  399. static gboolean addWebSettingsGroupToContext(GOptionContext *context, WebKitWebSettings* webkitSettings)
  400. {
  401. GOptionEntry *optionEntries = getOptionEntriesFromWebKitWebSettings(webkitSettings);
  402. if (!optionEntries)
  403. return FALSE;
  404. GOptionGroup *webSettingsGroup = g_option_group_new("websettings",
  405. "WebKitWebSettings writable properties for default WebKitWebView",
  406. "WebKitWebSettings properties",
  407. webkitSettings,
  408. NULL);
  409. g_option_group_add_entries(webSettingsGroup, optionEntries);
  410. g_free(optionEntries);
  411. /* Option context takes ownership of the group. */
  412. g_option_context_add_group(context, webSettingsGroup);
  413. return TRUE;
  414. }
  415. int main(int argc, char* argv[])
  416. {
  417. WebKitWebSettings *webkitSettings = 0;
  418. const gchar **uriArguments = 0;
  419. const GOptionEntry commandLineOptions[] =
  420. {
  421. { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &uriArguments, 0, "[URL]" },
  422. { 0, 0, 0, 0, 0, 0, 0 }
  423. };
  424. gtk_init(&argc, &argv);
  425. GOptionContext *context = g_option_context_new(0);
  426. g_option_context_add_main_entries(context, commandLineOptions, 0);
  427. g_option_context_add_group(context, gtk_get_option_group(TRUE));
  428. #ifdef WTF_USE_GSTREAMER
  429. g_option_context_add_group(context, gst_init_get_option_group());
  430. #endif
  431. webkitSettings = webkit_web_settings_new();
  432. g_object_set(webkitSettings, "enable-developer-extras", TRUE, NULL);
  433. if (!addWebSettingsGroupToContext(context, webkitSettings)) {
  434. g_object_unref(webkitSettings);
  435. webkitSettings = 0;
  436. }
  437. GError *error = 0;
  438. if (!g_option_context_parse(context, &argc, &argv, &error)) {
  439. g_printerr("Cannot parse arguments: %s\n", error->message);
  440. g_error_free(error);
  441. g_option_context_free(context);
  442. return 1;
  443. }
  444. g_option_context_free(context);
  445. #ifdef SOUP_TYPE_PROXY_RESOLVER_DEFAULT
  446. soup_session_add_feature_by_type(webkit_get_default_session(), SOUP_TYPE_PROXY_RESOLVER_DEFAULT);
  447. #else
  448. const char *httpProxy = g_getenv("http_proxy");
  449. if (httpProxy) {
  450. SoupURI *proxyUri = soup_uri_new(httpProxy);
  451. g_object_set(webkit_get_default_session(), SOUP_SESSION_PROXY_URI, proxyUri, NULL);
  452. soup_uri_free(proxyUri);
  453. }
  454. #endif
  455. #ifdef WEBKIT_EXEC_PATH
  456. g_setenv("WEBKIT_INSPECTOR_PATH", WEBKIT_EXEC_PATH "resources/inspector", FALSE);
  457. #endif /* WEBKIT_EXEC_PATH */
  458. WebKitWebView *webView;
  459. GtkWidget *main_window = createWindow(&webView);
  460. if (webkitSettings) {
  461. webkit_web_view_set_settings(WEBKIT_WEB_VIEW(webView), webkitSettings);
  462. g_object_unref(webkitSettings);
  463. }
  464. const gchar *uri = (uriArguments ? uriArguments[0] : "http://www.google.com/");
  465. gchar *fileURL = filenameToURL(uri);
  466. webkit_web_view_load_uri(webView, fileURL ? fileURL : uri);
  467. g_free(fileURL);
  468. gtk_widget_grab_focus(GTK_WIDGET(webView));
  469. gtk_widget_show_all(main_window);
  470. gtk_main();
  471. return 0;
  472. }