2 * Copyright (C) 2006, 2007 Apple Inc.
3 * Copyright (C) 2007 Alp Toker <alp@atoker.com>
4 * Copyright (C) 2011 Lukasz Slachciak
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
28 #include "LauncherInspectorWindow.h"
33 #include <webkit/webkit.h>
35 static gint windowCount = 0;
37 static GtkWidget* createWindow(WebKitWebView** outWebView);
39 static void activateUriEntryCb(GtkWidget* entry, gpointer data)
41 WebKitWebView *webView = g_object_get_data(G_OBJECT(entry), "web-view");
42 const gchar* uri = gtk_entry_get_text(GTK_ENTRY(entry));
44 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(entry), GTK_ENTRY_ICON_PRIMARY, 0);
45 webkit_web_view_load_uri(webView, uri);
48 static void updateTitle(GtkWindow* window, WebKitWebView* webView)
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);
60 static void linkHoverCb(WebKitWebView* page, const gchar* title, const gchar* link, GtkStatusbar* statusbar)
62 guint statusContextId =
63 GPOINTER_TO_UINT(g_object_get_data(G_OBJECT(statusbar), "link-hover-context"));
64 /* underflow is allowed */
65 gtk_statusbar_pop(statusbar, statusContextId);
67 gtk_statusbar_push(statusbar, statusContextId, link);
70 static void notifyTitleCb(WebKitWebView* webView, GParamSpec* pspec, GtkWidget* window)
72 updateTitle(GTK_WINDOW(window), webView);
75 static void notifyLoadStatusCb(WebKitWebView* webView, GParamSpec* pspec, GtkWidget* uriEntry)
77 if (webkit_web_view_get_load_status(webView) == WEBKIT_LOAD_COMMITTED) {
78 WebKitWebFrame *frame = webkit_web_view_get_main_frame(webView);
79 const gchar *uri = webkit_web_frame_get_uri(frame);
81 gtk_entry_set_text(GTK_ENTRY(uriEntry), uri);
85 static void notifyProgressCb(WebKitWebView* webView, GParamSpec* pspec, GtkWidget* window)
87 updateTitle(GTK_WINDOW(window), webView);
90 static void destroyCb(GtkWidget* widget, GtkWidget* window)
92 if (g_atomic_int_dec_and_test(&windowCount))
96 static void goBackCb(GtkWidget* widget, WebKitWebView* webView)
98 webkit_web_view_go_back(webView);
101 static void goForwardCb(GtkWidget* widget, WebKitWebView* webView)
103 webkit_web_view_go_forward(webView);
106 static WebKitWebView*
107 createWebViewCb(WebKitWebView* webView, WebKitWebFrame* web_frame, GtkWidget* window)
109 WebKitWebView *newWebView;
110 createWindow(&newWebView);
111 webkit_web_view_set_settings(newWebView, webkit_web_view_get_settings(webView));
115 static gboolean webViewReadyCb(WebKitWebView* webView, GtkWidget* window)
117 gtk_widget_grab_focus(GTK_WIDGET(webView));
118 gtk_widget_show_all(window);
122 static gboolean closeWebViewCb(WebKitWebView* webView, GtkWidget* window)
124 gtk_widget_destroy(window);
128 static gboolean webViewFullscreenMessageWindowClose(GtkWidget *dialog)
130 if (GTK_IS_WIDGET(dialog))
131 gtk_widget_destroy(dialog);
135 static gboolean webViewWindowStateEvent(GtkWidget *widget, GdkEventWindowState *event, WebKitWebView *webView)
137 if (event->new_window_state & GDK_WINDOW_STATE_FULLSCREEN) {
138 WebKitWebFrame *frame = webkit_web_view_get_main_frame(webView);
139 const gchar *uri = webkit_web_frame_get_uri(frame);
140 GtkWidget *window = gtk_widget_get_toplevel(GTK_WIDGET(webView));
141 if (!gtk_widget_is_toplevel(window) || !GTK_IS_WINDOW(window) || GTK_IS_OFFSCREEN_WINDOW(window))
144 GtkWidget *dialog = gtk_message_dialog_new(window ? GTK_WINDOW(window) : 0,
148 "%s is now full screen. Press ESC or f to exit.", uri);
149 g_signal_connect_swapped(dialog, "response", G_CALLBACK(gtk_widget_destroy), dialog);
150 g_timeout_add(1500, (GSourceFunc) webViewFullscreenMessageWindowClose, dialog);
151 gtk_dialog_run(GTK_DIALOG(dialog));
156 static void hideWidget(GtkWidget* widget, gpointer data)
158 if (!GTK_IS_SCROLLED_WINDOW(widget))
159 gtk_widget_hide(widget);
162 static void showWidget(GtkWidget* widget, gpointer data)
164 if (!GTK_IS_SCROLLED_WINDOW(widget))
165 gtk_widget_show(widget);
168 static gboolean webViewEnteringFullScreen(WebKitWebView *webView, GObject *element, GtkWidget* vbox)
170 WebKitWebFrame *frame = webkit_web_view_get_main_frame(webView);
171 const gchar *uri = webkit_web_frame_get_uri(frame);
172 GtkWidget *window = gtk_widget_get_toplevel(GTK_WIDGET(webView));
173 if (!gtk_widget_is_toplevel(window) || !GTK_IS_WINDOW(window) || GTK_IS_OFFSCREEN_WINDOW(window))
176 GtkWidget *dialog = gtk_message_dialog_new(window ? GTK_WINDOW(window) : 0,
180 "Allow full screen display of %s ?", uri);
181 gint result = gtk_dialog_run(GTK_DIALOG(dialog));
182 if (result == GTK_RESPONSE_YES) {
183 gtk_container_foreach(GTK_CONTAINER(vbox), (GtkCallback) hideWidget, NULL);
184 gtk_widget_destroy(GTK_WIDGET(dialog));
187 gtk_widget_destroy(GTK_WIDGET(dialog));
191 static gboolean webViewLeavingFullScreen(WebKitWebView *webView, GObject *element, GtkWidget* vbox)
193 GtkWidget *window = gtk_widget_get_toplevel(GTK_WIDGET(webView));
194 if (gtk_widget_is_toplevel(window) && GTK_IS_WINDOW(window) && !GTK_IS_OFFSCREEN_WINDOW(window))
195 g_signal_handlers_disconnect_by_func(window, G_CALLBACK(webViewWindowStateEvent), webView);
196 gtk_container_foreach(GTK_CONTAINER(vbox), (GtkCallback) showWidget, NULL);
200 static void iconLoadedCb(WebKitWebView* webView, const char* iconURI, GtkWidget* uriEntry)
202 GdkPixbuf *icon = webkit_web_view_try_get_favicon_pixbuf(webView, 16, 16);
206 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(uriEntry), GTK_ENTRY_ICON_PRIMARY, icon);
207 g_object_unref(icon);
210 static GtkWidget *inspectorInspectWebViewCb(WebKitWebInspector *inspector, WebKitWebView *webView, GtkWindow* window)
212 GtkWidget *inspectorWindow = launcherInspectorWindowNew(inspector, window);
213 return GTK_WIDGET(launcherInspectorWindowGetWebView(LAUNCHER_INSPECTOR_WINDOW(inspectorWindow)));
216 static GtkWidget* createBrowser(GtkWidget* window, GtkWidget* uriEntry, GtkWidget* statusbar, WebKitWebView* webView, GtkWidget* vbox)
218 char *iconDatabasePath;
219 GtkWidget *scrolledWindow = gtk_scrolled_window_new(NULL, NULL);
220 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledWindow), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
222 gtk_container_add(GTK_CONTAINER(scrolledWindow), GTK_WIDGET(webView));
224 iconDatabasePath = g_build_filename(g_get_user_data_dir(), "webkit", "icondatabase", NULL);
225 webkit_favicon_database_set_path(webkit_get_favicon_database(), iconDatabasePath);
226 g_free(iconDatabasePath);
228 g_signal_connect(webView, "notify::title", G_CALLBACK(notifyTitleCb), window);
229 g_signal_connect(webView, "notify::load-status", G_CALLBACK(notifyLoadStatusCb), uriEntry);
230 g_signal_connect(webView, "notify::progress", G_CALLBACK(notifyProgressCb), window);
231 g_signal_connect(webView, "icon-loaded", G_CALLBACK(iconLoadedCb), uriEntry);
232 g_signal_connect(webView, "hovering-over-link", G_CALLBACK(linkHoverCb), statusbar);
233 g_signal_connect(webView, "create-web-view", G_CALLBACK(createWebViewCb), window);
234 g_signal_connect(webView, "web-view-ready", G_CALLBACK(webViewReadyCb), window);
235 g_signal_connect(webView, "close-web-view", G_CALLBACK(closeWebViewCb), window);
236 g_signal_connect(webView, "entering-fullscreen", G_CALLBACK(webViewEnteringFullScreen), vbox);
237 g_signal_connect(webView, "leaving-fullscreen", G_CALLBACK(webViewLeavingFullScreen), vbox);
238 g_signal_connect(webkit_web_view_get_inspector(webView), "inspect-web-view", G_CALLBACK(inspectorInspectWebViewCb), window);
240 return scrolledWindow;
243 static GtkWidget* createStatusbar()
245 GtkStatusbar *statusbar = GTK_STATUSBAR(gtk_statusbar_new());
246 guint statusContextId = gtk_statusbar_get_context_id(statusbar, "Link Hover");
247 g_object_set_data(G_OBJECT(statusbar), "link-hover-context",
248 GUINT_TO_POINTER(statusContextId));
250 return GTK_WIDGET(statusbar);
253 static GtkWidget* createToolbar(GtkWidget* uriEntry, WebKitWebView* webView)
255 GtkWidget *toolbar = gtk_toolbar_new();
257 #if GTK_CHECK_VERSION(2, 15, 0)
258 gtk_orientable_set_orientation(GTK_ORIENTABLE(toolbar), GTK_ORIENTATION_HORIZONTAL);
260 gtk_toolbar_set_orientation(GTK_TOOLBAR(toolbar), GTK_ORIENTATION_HORIZONTAL);
262 gtk_toolbar_set_style(GTK_TOOLBAR(toolbar), GTK_TOOLBAR_BOTH_HORIZ);
266 /* the back button */
267 item = gtk_tool_button_new_from_stock(GTK_STOCK_GO_BACK);
268 g_signal_connect(G_OBJECT(item), "clicked", G_CALLBACK(goBackCb), webView);
269 gtk_toolbar_insert(GTK_TOOLBAR(toolbar), item, -1);
271 /* The forward button */
272 item = gtk_tool_button_new_from_stock(GTK_STOCK_GO_FORWARD);
273 g_signal_connect(G_OBJECT(item), "clicked", G_CALLBACK(goForwardCb), webView);
274 gtk_toolbar_insert(GTK_TOOLBAR(toolbar), item, -1);
277 item = gtk_tool_item_new();
278 gtk_tool_item_set_expand(item, TRUE);
279 gtk_container_add(GTK_CONTAINER(item), uriEntry);
280 g_signal_connect(G_OBJECT(uriEntry), "activate", G_CALLBACK(activateUriEntryCb), NULL);
281 gtk_toolbar_insert(GTK_TOOLBAR(toolbar), item, -1);
284 g_object_set_data(G_OBJECT(uriEntry), "web-view", webView);
285 item = gtk_tool_button_new_from_stock(GTK_STOCK_OK);
286 g_signal_connect_swapped(G_OBJECT(item), "clicked", G_CALLBACK(activateUriEntryCb), (gpointer)uriEntry);
287 gtk_toolbar_insert(GTK_TOOLBAR(toolbar), item, -1);
292 static GtkWidget* createWindow(WebKitWebView** outWebView)
294 WebKitWebView *webView;
298 GtkWidget *statusbar;
300 g_atomic_int_inc(&windowCount);
302 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
303 gtk_window_set_default_size(GTK_WINDOW(window), 800, 600);
304 gtk_widget_set_name(window, "GtkLauncher");
306 webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
307 uriEntry = gtk_entry_new();
309 #ifdef GTK_API_VERSION_2
310 vbox = gtk_vbox_new(FALSE, 0);
312 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
314 statusbar = createStatusbar(webView);
315 gtk_box_pack_start(GTK_BOX(vbox), createToolbar(uriEntry, webView), FALSE, FALSE, 0);
316 gtk_box_pack_start(GTK_BOX(vbox), createBrowser(window, uriEntry, statusbar, webView, vbox), TRUE, TRUE, 0);
317 gtk_box_pack_start(GTK_BOX(vbox), statusbar, FALSE, FALSE, 0);
319 gtk_container_add(GTK_CONTAINER(window), vbox);
321 g_signal_connect(window, "destroy", G_CALLBACK(destroyCb), NULL);
324 *outWebView = webView;
329 static gchar* filenameToURL(const char* filename)
331 if (!g_file_test(filename, G_FILE_TEST_EXISTS))
334 GFile *gfile = g_file_new_for_path(filename);
335 gchar *fileURL = g_file_get_uri(gfile);
336 g_object_unref(gfile);
341 static gboolean parseOptionEntryCallback(const gchar *optionNameFull, const gchar *value, WebKitWebSettings *webSettings, GError **error)
343 if (strlen(optionNameFull) <= 2) {
344 g_set_error(error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED, "Invalid option %s", optionNameFull);
348 /* We have two -- in option name so remove them. */
349 const gchar *optionName = optionNameFull + 2;
350 GParamSpec *spec = g_object_class_find_property(G_OBJECT_GET_CLASS(webSettings), optionName);
352 g_set_error(error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED, "Cannot find web settings for option %s", optionNameFull);
356 switch (G_PARAM_SPEC_VALUE_TYPE(spec)) {
357 case G_TYPE_BOOLEAN: {
358 gboolean propertyValue = TRUE;
359 if (value && g_ascii_strcasecmp(value, "true") && strcmp(value, "1"))
360 propertyValue = FALSE;
361 g_object_set(G_OBJECT(webSettings), optionName, propertyValue, NULL);
365 g_object_set(G_OBJECT(webSettings), optionName, value, NULL);
372 propertyValue = g_ascii_strtoll(value, &end, 0);
373 if (errno == ERANGE || propertyValue > G_MAXINT || propertyValue < G_MININT) {
374 g_set_error(error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, "Integer value '%s' for %s out of range", value, optionNameFull);
377 if (errno || value == end) {
378 g_set_error(error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, "Cannot parse integer value '%s' for %s", value, optionNameFull);
381 g_object_set(G_OBJECT(webSettings), optionName, propertyValue, NULL);
385 gdouble propertyValue;
389 propertyValue = g_ascii_strtod(value, &end);
390 if (errno == ERANGE || propertyValue > G_MAXFLOAT || propertyValue < G_MINFLOAT) {
391 g_set_error(error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, "Float value '%s' for %s out of range", value, optionNameFull);
394 if (errno || value == end) {
395 g_set_error(error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, "Cannot parse float value '%s' for %s", value, optionNameFull);
398 g_object_set(G_OBJECT(webSettings), optionName, propertyValue, NULL);
402 g_assert_not_reached();
408 static gboolean isValidParameterType(GType gParamType)
410 return (gParamType == G_TYPE_BOOLEAN || gParamType == G_TYPE_STRING || gParamType == G_TYPE_INT
411 || gParamType == G_TYPE_FLOAT);
414 static GOptionEntry* getOptionEntriesFromWebKitWebSettings(WebKitWebSettings *webSettings)
416 GParamSpec **propertySpecs;
417 GOptionEntry *optionEntries;
418 guint numProperties, numEntries, i;
420 propertySpecs = g_object_class_list_properties(G_OBJECT_GET_CLASS(webSettings), &numProperties);
424 optionEntries = g_new0(GOptionEntry, numProperties + 1);
426 for (i = 0; i < numProperties; i++) {
427 GParamSpec *param = propertySpecs[i];
429 /* Fill in structures only for writable and not construct-only properties. */
430 if (!param || !(param->flags & G_PARAM_WRITABLE) || (param->flags & G_PARAM_CONSTRUCT_ONLY))
433 GType gParamType = G_PARAM_SPEC_VALUE_TYPE(param);
434 if (!isValidParameterType(gParamType))
437 GOptionEntry *optionEntry = &optionEntries[numEntries++];
438 optionEntry->long_name = g_param_spec_get_name(param);
440 /* There is no easy way to figure our short name for generated option entries.
441 optionEntry.short_name=*/
442 /* For bool arguments "enable" type make option argument not required. */
443 if (gParamType == G_TYPE_BOOLEAN && (strstr(optionEntry->long_name, "enable")))
444 optionEntry->flags = G_OPTION_FLAG_OPTIONAL_ARG;
445 optionEntry->arg = G_OPTION_ARG_CALLBACK;
446 optionEntry->arg_data = parseOptionEntryCallback;
447 optionEntry->description = g_param_spec_get_blurb(param);
448 optionEntry->arg_description = g_type_name(gParamType);
450 g_free(propertySpecs);
452 return optionEntries;
455 static gboolean addWebSettingsGroupToContext(GOptionContext *context, WebKitWebSettings* webkitSettings)
457 GOptionEntry *optionEntries = getOptionEntriesFromWebKitWebSettings(webkitSettings);
461 GOptionGroup *webSettingsGroup = g_option_group_new("websettings",
462 "WebKitWebSettings writable properties for default WebKitWebView",
463 "WebKitWebSettings properties",
466 g_option_group_add_entries(webSettingsGroup, optionEntries);
467 g_free(optionEntries);
469 /* Option context takes ownership of the group. */
470 g_option_context_add_group(context, webSettingsGroup);
475 int main(int argc, char* argv[])
477 WebKitWebSettings *webkitSettings = 0;
478 const gchar **uriArguments = 0;
479 const GOptionEntry commandLineOptions[] =
481 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &uriArguments, 0, "[URL]" },
482 { 0, 0, 0, 0, 0, 0, 0 }
485 gtk_init(&argc, &argv);
487 GOptionContext *context = g_option_context_new(0);
488 g_option_context_add_main_entries(context, commandLineOptions, 0);
489 g_option_context_add_group(context, gtk_get_option_group(TRUE));
491 webkitSettings = webkit_web_settings_new();
492 if (!addWebSettingsGroupToContext(context, webkitSettings)) {
493 g_object_unref(webkitSettings);
498 if (!g_option_context_parse(context, &argc, &argv, &error)) {
499 g_printerr("Cannot parse arguments: %s\n", error->message);
501 g_option_context_free(context);
505 g_option_context_free(context);
507 #ifdef SOUP_TYPE_PROXY_RESOLVER_DEFAULT
508 soup_session_add_feature_by_type(webkit_get_default_session(), SOUP_TYPE_PROXY_RESOLVER_DEFAULT);
510 const char *httpProxy = g_getenv("http_proxy");
512 SoupURI *proxyUri = soup_uri_new(httpProxy);
513 g_object_set(webkit_get_default_session(), SOUP_SESSION_PROXY_URI, proxyUri, NULL);
514 soup_uri_free(proxyUri);
518 WebKitWebView *webView;
519 GtkWidget *main_window = createWindow(&webView);
521 if (webkitSettings) {
522 webkit_web_view_set_settings(WEBKIT_WEB_VIEW(webView), webkitSettings);
523 g_object_unref(webkitSettings);
526 const gchar *uri = (uriArguments ? uriArguments[0] : "http://www.google.com/");
527 gchar *fileURL = filenameToURL(uri);
529 webkit_web_view_load_uri(webView, fileURL ? fileURL : uri);
532 gtk_widget_grab_focus(GTK_WIDGET(webView));
533 gtk_widget_show_all(main_window);