ebbfce1e1bc3692196b3637412c3047f96d6e879
[prss.git] / src / webbrowser.c
1 /*
2  * Copyright (C) 2012-2013 jeanfi@gmail.com
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301 USA
18  */
19
20 #include <webkit/webkit.h>
21
22 #include "webbrowser.h"
23
24 static WebKitWebView *view;
25 static GtkLabel *w_status;
26
27 static gboolean new_window_requested_cbk(WebKitWebView *view,
28                                          WebKitWebFrame *frame,
29                                          WebKitNetworkRequest *rq,
30                                          WebKitWebNavigationAction *action,
31                                          WebKitWebPolicyDecision *decision)
32 {
33         webkit_web_view_load_uri(view, webkit_network_request_get_uri(rq));
34
35         webkit_web_policy_decision_ignore(decision);
36
37         return TRUE;
38 }
39
40 static void hovering_over_link_cbk(WebKitWebView *web_view,
41                                    gchar *title,
42                                    gchar *uri,
43                                    gpointer user_data)
44 {
45         if (uri)
46                 gtk_label_set_label(w_status, uri);
47         else
48                 gtk_label_set_label(w_status, "");
49 }
50
51 GtkWidget *web_get_widget()
52 {
53         if (!view) {
54                 view = WEBKIT_WEB_VIEW(webkit_web_view_new());
55                 g_signal_connect(view,
56                                  "new-window-policy-decision-requested",
57                                  G_CALLBACK(new_window_requested_cbk),
58                                  view);
59                 g_signal_connect(view,
60                                  "hovering-over-link",
61                                  G_CALLBACK(hovering_over_link_cbk),
62                                  NULL);
63         }
64
65         return GTK_WIDGET(view);
66 }
67
68 void web_load(const char *str)
69 {
70         webkit_web_view_load_string(view, str, NULL, "UTF-8", "file://");
71 }
72
73 void webbrowser_init(GtkLabel *status)
74 {
75         w_status = status;
76 }