4f77fed794f8a4d76791cfcf0a442b3b51970c36
[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 GtkStatusbar *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         guint id;
46
47         id = gtk_statusbar_get_context_id(w_status, "info");
48
49         if (uri)
50                 gtk_statusbar_push(w_status, id, uri);
51         else
52                 gtk_statusbar_pop(w_status, id);
53 }
54
55 GtkWidget *web_get_widget()
56 {
57         if (!view) {
58                 view = WEBKIT_WEB_VIEW(webkit_web_view_new());
59                 g_signal_connect(view,
60                                  "new-window-policy-decision-requested",
61                                  G_CALLBACK(new_window_requested_cbk),
62                                  view);
63                 g_signal_connect(view,
64                                  "hovering-over-link",
65                                  G_CALLBACK(hovering_over_link_cbk),
66                                  NULL);
67         }
68
69         return GTK_WIDGET(view);
70 }
71
72 void web_load(const char *str)
73 {
74         webkit_web_view_load_string(view, str, NULL, "UTF-8", "file://");
75 }
76
77 void webbrowser_init(GtkStatusbar *status)
78 {
79         w_status = status;
80 }