1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 
3 /*
4  *  GThumb
5  *
6  *  Copyright (C) 2010 Free Software Foundation, Inc.
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #include <config.h>
23 #include <glib/gi18n.h>
24 #include <webkit2/webkit2.h>
25 #include "oauth-ask-authorization-dialog.h"
26 
27 
28 #define GET_WIDGET(x) (_gtk_builder_get_widget (self->priv->builder, (x)))
29 #define _LOADING_PAGE 1
30 #define _WEB_VIEW_PAGE 0
31 
32 
33 /* Signals */
34 enum {
35 	LOAD_REQUEST,
36 	LOADED,
37 	REDIRECTED,
38 	LAST_SIGNAL
39 };
40 
41 
42 static guint oauth_ask_authorization_dialog_signals[LAST_SIGNAL] = { 0 };
43 
44 
45 struct _OAuthAskAuthorizationDialogPrivate {
46 	GtkWidget  *view;
47 	GtkBuilder *builder;
48 };
49 
50 
G_DEFINE_TYPE_WITH_CODE(OAuthAskAuthorizationDialog,oauth_ask_authorization_dialog,GTK_TYPE_DIALOG,G_ADD_PRIVATE (OAuthAskAuthorizationDialog))51 G_DEFINE_TYPE_WITH_CODE (OAuthAskAuthorizationDialog,
52 			 oauth_ask_authorization_dialog,
53 			 GTK_TYPE_DIALOG,
54 			 G_ADD_PRIVATE (OAuthAskAuthorizationDialog))
55 
56 
57 static void
58 oauth_ask_authorization_dialog_finalize (GObject *obj)
59 {
60 	OAuthAskAuthorizationDialog *self;
61 
62 	self = OAUTH_ASK_AUTHORIZATION_DIALOG (obj);
63 	_g_object_unref (self->priv->builder);
64 
65 	G_OBJECT_CLASS (oauth_ask_authorization_dialog_parent_class)->finalize (obj);
66 }
67 
68 
69 static void
oauth_ask_authorization_dialog_class_init(OAuthAskAuthorizationDialogClass * klass)70 oauth_ask_authorization_dialog_class_init (OAuthAskAuthorizationDialogClass *klass)
71 {
72 	GObjectClass   *object_class;
73 
74 	object_class = G_OBJECT_CLASS (klass);
75 	object_class->finalize = oauth_ask_authorization_dialog_finalize;
76 
77 	oauth_ask_authorization_dialog_signals[LOAD_REQUEST] =
78 		g_signal_new ("load-request",
79 			      G_TYPE_FROM_CLASS (klass),
80 			      G_SIGNAL_RUN_LAST,
81 			      G_STRUCT_OFFSET (OAuthAskAuthorizationDialogClass, load_request),
82 			      NULL, NULL,
83 			      g_cclosure_marshal_VOID__VOID,
84 			      G_TYPE_NONE,
85 			      0);
86 	oauth_ask_authorization_dialog_signals[LOADED] =
87 		g_signal_new ("loaded",
88 			      G_TYPE_FROM_CLASS (klass),
89 			      G_SIGNAL_RUN_LAST,
90 			      G_STRUCT_OFFSET (OAuthAskAuthorizationDialogClass, loaded),
91 			      NULL, NULL,
92 			      g_cclosure_marshal_VOID__VOID,
93 			      G_TYPE_NONE,
94 			      0);
95 	oauth_ask_authorization_dialog_signals[REDIRECTED] =
96 		g_signal_new ("redirected",
97 			      G_TYPE_FROM_CLASS (klass),
98 			      G_SIGNAL_RUN_LAST,
99 			      G_STRUCT_OFFSET (OAuthAskAuthorizationDialogClass, redirected),
100 			      NULL, NULL,
101 			      g_cclosure_marshal_VOID__VOID,
102 			      G_TYPE_NONE,
103 			      0);
104 }
105 
106 
107 static void
webkit_view_load_changed_cb(WebKitWebView * web_view,WebKitLoadEvent load_event,gpointer user_data)108 webkit_view_load_changed_cb (WebKitWebView   *web_view,
109                 	     WebKitLoadEvent  load_event,
110                 	     gpointer         user_data)
111 {
112 	OAuthAskAuthorizationDialog *self = user_data;
113 
114 	switch (load_event) {
115 	case WEBKIT_LOAD_STARTED:
116 	case WEBKIT_LOAD_COMMITTED:
117 		gtk_notebook_set_current_page (GTK_NOTEBOOK(GET_WIDGET ("dialog_content")), _LOADING_PAGE);
118 		g_signal_emit (self, oauth_ask_authorization_dialog_signals[LOAD_REQUEST], 0);
119 		break;
120 	case WEBKIT_LOAD_REDIRECTED:
121 		g_signal_emit (self, oauth_ask_authorization_dialog_signals[REDIRECTED], 0);
122 		break;
123 	case WEBKIT_LOAD_FINISHED:
124 		gtk_notebook_set_current_page (GTK_NOTEBOOK(GET_WIDGET ("dialog_content")), _WEB_VIEW_PAGE);
125 		gtk_widget_grab_focus (self->priv->view);
126 		g_signal_emit (self, oauth_ask_authorization_dialog_signals[LOADED], 0);
127 		break;
128 	default:
129 		break;
130 	}
131 }
132 
133 
134 static GtkWidget * _webkit_web_view_new (OAuthAskAuthorizationDialog *self);
135 
136 
137 static GtkWidget *
webkit_view_create_cb(WebKitWebView * web_view,gpointer user_data)138 webkit_view_create_cb (WebKitWebView *web_view,
139 		       gpointer       user_data)
140 {
141 	return oauth_ask_authorization_dialog_get_view (OAUTH_ASK_AUTHORIZATION_DIALOG (user_data));
142 }
143 
144 
145 static void
webkit_view_ready_to_show_cb(WebKitWebView * web_view,gpointer user_data)146 webkit_view_ready_to_show_cb (WebKitWebView *web_view,
147 			      gpointer       user_data)
148 {
149 	GtkWindow              *window;
150 	WebKitWindowProperties *prop;
151 	GdkRectangle            geometry;
152 
153 	window = _gtk_widget_get_toplevel_if_window (GTK_WIDGET (web_view));
154 	if (window == NULL)
155 		return;
156 
157 	prop = webkit_web_view_get_window_properties (web_view);
158 	webkit_window_properties_get_geometry (prop, &geometry);
159 
160 	gtk_window_set_default_size (window, geometry.width, geometry.height);
161 	gtk_widget_show_all (GTK_WIDGET (window));
162 }
163 
164 
165 static GtkWidget *
_webkit_web_view_new(OAuthAskAuthorizationDialog * self)166 _webkit_web_view_new (OAuthAskAuthorizationDialog *self)
167 {
168 	WebKitWebContext    *context;
169 	GtkWidget           *view;
170 	WebKitSettings      *settings;
171 	WebKitCookieManager *cookie_manager;
172 	GFile               *file;
173 	char                *cookie_filename;
174 
175 	context = webkit_web_context_get_default ();
176 	view = webkit_web_view_new_with_context (context);
177 
178 	settings = webkit_settings_new ();
179 	webkit_settings_set_enable_javascript (settings, TRUE);
180 	webkit_settings_set_javascript_can_open_windows_automatically (settings, TRUE);
181 	webkit_web_view_set_settings (WEBKIT_WEB_VIEW (view), settings);
182 
183 	file = gth_user_dir_get_file_for_write (GTH_DIR_CACHE, GTHUMB_DIR, "cookies", NULL);
184 	cookie_filename = g_file_get_path (file);
185 
186 	cookie_manager = webkit_web_context_get_cookie_manager (context);
187 	webkit_cookie_manager_set_accept_policy (cookie_manager, WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS);
188 	webkit_cookie_manager_set_persistent_storage (cookie_manager, cookie_filename, WEBKIT_COOKIE_PERSISTENT_STORAGE_TEXT);
189 	webkit_web_context_set_cache_model (context, WEBKIT_CACHE_MODEL_DOCUMENT_BROWSER);
190 
191 	g_free (cookie_filename);
192 	g_object_unref (file);
193 
194 	g_signal_connect (view,
195 			  "create",
196 			  G_CALLBACK (webkit_view_create_cb),
197 			  self);
198 	g_signal_connect (view,
199 			  "ready-to-show",
200 			  G_CALLBACK (webkit_view_ready_to_show_cb),
201 			  self);
202 
203 	return view;
204 }
205 
206 
207 static void
oauth_ask_authorization_dialog_init(OAuthAskAuthorizationDialog * self)208 oauth_ask_authorization_dialog_init (OAuthAskAuthorizationDialog *self)
209 {
210 	GtkWidget *dialog_content;
211 
212 	self->priv = oauth_ask_authorization_dialog_get_instance_private (self);
213 	self->priv->builder = _gtk_builder_new_from_file ("oauth-ask-authorization.ui", "oauth");
214 
215 	gtk_window_set_default_size (GTK_WINDOW (self), 500, 500);
216 
217 	dialog_content = GET_WIDGET ("dialog_content");
218 	gtk_widget_show (dialog_content);
219 	gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (self))), dialog_content, TRUE, TRUE, 0);
220 
221 	self->priv->view = _webkit_web_view_new (self);
222 	gtk_widget_show (self->priv->view);
223 	gtk_box_pack_start (GTK_BOX (GET_WIDGET ("webkit_view_container")), self->priv->view, TRUE, TRUE, 0);
224 
225 	g_signal_connect (self->priv->view,
226 			  "load-changed",
227 			  G_CALLBACK (webkit_view_load_changed_cb),
228 			  self);
229 
230 	gtk_dialog_add_button (GTK_DIALOG (self), _GTK_LABEL_CANCEL, GTK_RESPONSE_CANCEL);
231 }
232 
233 
234 GtkWidget *
oauth_ask_authorization_dialog_new(const char * uri)235 oauth_ask_authorization_dialog_new (const char *uri)
236 {
237 	OAuthAskAuthorizationDialog *self;
238 
239 	self = g_object_new (OAUTH_TYPE_ASK_AUTHORIZATION_DIALOG,
240 			     "title", _("Authorization Required"),
241 			     "resizable", TRUE,
242 			     "use-header-bar", _gtk_settings_get_dialogs_use_header (),
243 			     NULL);
244 	if (uri != NULL)
245 		webkit_web_view_load_uri (WEBKIT_WEB_VIEW (self->priv->view), uri);
246 
247 	return (GtkWidget *) self;
248 }
249 
250 
251 GtkWidget *
oauth_ask_authorization_dialog_get_view(OAuthAskAuthorizationDialog * self)252 oauth_ask_authorization_dialog_get_view (OAuthAskAuthorizationDialog *self)
253 {
254 	return self->priv->view;
255 }
256 
257 
258 const char *
oauth_ask_authorization_dialog_get_uri(OAuthAskAuthorizationDialog * self)259 oauth_ask_authorization_dialog_get_uri (OAuthAskAuthorizationDialog *self)
260 {
261 	return webkit_web_view_get_uri (WEBKIT_WEB_VIEW (self->priv->view));
262 }
263 
264 
265 const char *
oauth_ask_authorization_dialog_get_title(OAuthAskAuthorizationDialog * self)266 oauth_ask_authorization_dialog_get_title (OAuthAskAuthorizationDialog *self)
267 {
268 	return webkit_web_view_get_title (WEBKIT_WEB_VIEW (self->priv->view));
269 }
270