2007-02-03 [colin] 2.7.2cvs17
[claws.git] / src / uri_opener.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2007 Colin Leroy <colin@colino.net> and 
4  * the Claws Mail team
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  */
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <gtk/gtkwidget.h>
25 #include <glib.h>
26 #include <glib/gi18n.h>
27 #include <gdk/gdkkeysyms.h>
28 #include <sys/types.h>
29 #include <dirent.h>
30
31 #include "manage_window.h"
32 #include "utils.h"
33 #include "mainwindow.h"
34 #include "gtkutils.h"
35 #include "alertpanel.h"
36 #include "textview.h"
37 #include "prefs_common.h"
38 #include "prefs_common.h"
39
40 enum {
41         URI_OPENER_URL,
42         URI_OPENER_DATA,
43         N_URI_OPENER_COLUMNS
44 };
45
46
47 static struct URIOpener
48 {
49         GtkWidget *window;
50         GtkWidget *hbox1;
51         GtkWidget *vbox1;
52         GtkWidget *label;
53         GtkWidget *urilist;
54         GtkWidget *open_btn;
55         GtkWidget *open_all_btn;
56         GtkWidget *close_btn;
57         MessageView *msgview;
58         GSList    *uris;
59 } opener;
60
61 static void uri_opener_load_uris (void);
62 static void uri_opener_open_cb          (GtkWidget *widget, gpointer data);
63 static void uri_opener_close_cb (GtkWidget *widget, gpointer data);
64 static gboolean key_pressed             (GtkWidget *widget, GdkEventKey *event,
65                                          gpointer data);
66 static void uri_opener_double_clicked(GtkTreeView               *list_view,
67                                         GtkTreePath             *path,
68                                         GtkTreeViewColumn       *column,
69                                         gpointer                 data);
70 static void uri_opener_create(void);
71 void uri_opener_open(MessageView *msgview, GSList *uris)
72 {
73         g_return_if_fail(msgview);
74         g_return_if_fail(msgview->mimeview);
75         g_return_if_fail(msgview->mimeview->textview);
76         g_return_if_fail(msgview);
77         if (!opener.window)
78                 uri_opener_create();
79
80         manage_window_set_transient(GTK_WINDOW(opener.window));
81         gtk_widget_grab_focus(opener.close_btn);
82         
83         if (uris == NULL) {
84                 alertpanel_notice(_("There are no URLs in this email."));
85                 return;
86         }
87         
88         opener.msgview = msgview;
89         opener.uris = g_slist_copy(uris);
90         uri_opener_load_uris();
91
92         gtk_widget_show(opener.window);
93         gtk_widget_grab_focus(opener.urilist);
94         gtk_window_set_modal(GTK_WINDOW(opener.window), TRUE);
95 }
96
97 static GtkListStore* uri_opener_create_data_store(void)
98 {
99         return gtk_list_store_new(N_URI_OPENER_COLUMNS,
100                                   G_TYPE_STRING,
101                                   G_TYPE_POINTER,
102                                   -1);
103 }
104
105 static void uri_opener_create_list_view_columns(GtkWidget *list_view)
106 {
107         GtkTreeViewColumn *column;
108         GtkCellRenderer *renderer;
109
110         renderer = gtk_cell_renderer_text_new();
111         column = gtk_tree_view_column_new_with_attributes
112                 (_("Available URLs:"),
113                  renderer,
114                  "text", URI_OPENER_URL,
115                  NULL);
116         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
117 }
118
119 static GtkWidget *uri_opener_list_view_create   (void)
120 {
121         GtkTreeView *list_view;
122         GtkTreeSelection *selector;
123         GtkTreeModel *model;
124
125         model = GTK_TREE_MODEL(uri_opener_create_data_store());
126         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
127         g_object_unref(model);  
128         
129         gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
130         
131         selector = gtk_tree_view_get_selection(list_view);
132         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
133
134         g_signal_connect(G_OBJECT(list_view), "row_activated",
135                          G_CALLBACK(uri_opener_double_clicked),
136                          list_view);
137
138         /* create the columns */
139         uri_opener_create_list_view_columns(GTK_WIDGET(list_view));
140
141         return GTK_WIDGET(list_view);
142
143 }
144
145 static void uri_opener_create(void) 
146 {
147         GtkWidget *window;
148         GtkWidget *hbox1;
149         GtkWidget *vbox1;
150         GtkWidget *label;
151         GtkWidget *urilist;
152         GtkWidget *open_btn;
153         GtkWidget *open_all_btn;
154         GtkWidget *close_btn;
155         GtkWidget *scrolledwin;
156
157         window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
158         gtk_window_set_title (GTK_WINDOW(window),
159                               _("Open URLs"));
160
161         gtk_container_set_border_width (GTK_CONTAINER (window), 8);
162         gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
163         gtk_window_set_resizable(GTK_WINDOW (window), TRUE);
164         g_signal_connect(G_OBJECT(window), "delete_event",
165                          G_CALLBACK(uri_opener_close_cb), NULL);
166         g_signal_connect(G_OBJECT(window), "key_press_event",
167                          G_CALLBACK(key_pressed), NULL);
168         MANAGE_WINDOW_SIGNALS_CONNECT (window);
169
170         vbox1 = gtk_vbox_new(FALSE, 6);
171         gtkut_stock_button_set_create(&hbox1, 
172                                       &close_btn, GTK_STOCK_CLOSE,
173                                       &open_all_btn, _("Open _All"),
174                                       &open_btn, GTK_STOCK_OPEN);
175
176         g_signal_connect(G_OBJECT(open_btn), "clicked",
177                          G_CALLBACK(uri_opener_open_cb), GINT_TO_POINTER(FALSE));
178
179         g_signal_connect(G_OBJECT(open_all_btn), "clicked",
180                          G_CALLBACK(uri_opener_open_cb), GINT_TO_POINTER(TRUE));
181
182         g_signal_connect(G_OBJECT(close_btn), "clicked",
183                          G_CALLBACK(uri_opener_close_cb), NULL);
184
185         urilist = uri_opener_list_view_create();
186         
187         label = gtk_label_new(_("Please select the URL to open."));
188         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
189         gtk_box_pack_start(GTK_BOX(vbox1), label, FALSE, TRUE, 0);
190         
191         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
192         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
193                                        GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
194                                        
195         gtk_widget_set_size_request(scrolledwin, 500, 250);
196
197         gtk_container_add(GTK_CONTAINER(scrolledwin), urilist);
198         gtk_box_pack_start(GTK_BOX(vbox1), scrolledwin, TRUE, TRUE, 0);
199         gtk_box_pack_start(GTK_BOX(vbox1), hbox1, FALSE, FALSE, 0);
200         
201         gtk_widget_show(label);
202         gtk_widget_show(scrolledwin);
203         gtk_widget_show(urilist);
204         gtk_widget_show(hbox1);
205         gtk_widget_show(vbox1);
206         gtk_widget_show(close_btn);
207         gtk_widget_show(open_btn);
208         gtk_widget_show(open_all_btn);
209         gtk_container_add(GTK_CONTAINER (window), vbox1);
210
211         opener.window = window;
212         opener.hbox1 = hbox1;
213         opener.vbox1 = vbox1;
214         opener.label = label;
215         opener.urilist = urilist;
216         opener.open_btn = open_btn;
217         opener.open_all_btn = open_all_btn;
218         opener.close_btn = close_btn;
219
220 }
221
222 static void uri_opener_list_view_insert_uri(GtkWidget *list_view,
223                                                   GtkTreeIter *row_iter,
224                                                   ClickableText *uri) 
225 {
226         GtkTreeIter iter;
227         GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
228                                         (GTK_TREE_VIEW(list_view)));
229         gchar *visible = textview_get_visible_uri(opener.msgview->mimeview->textview, uri);
230         
231         if (row_iter == NULL) {
232                 /* append new */
233                 gtk_list_store_append(list_store, &iter);
234                 gtk_list_store_set(list_store, &iter,
235                                    URI_OPENER_URL, visible?visible:uri->uri,
236                                    URI_OPENER_DATA, uri,
237                                    -1);
238         } else {
239                 gtk_list_store_set(list_store, row_iter,
240                                    URI_OPENER_URL, visible?visible:uri->uri,
241                                    URI_OPENER_DATA, uri,
242                                    -1);
243         }
244         g_free(visible);
245 }
246
247 static void uri_opener_list_view_clear_uris(GtkWidget *list_view)
248 {
249         GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
250                                         (GTK_TREE_VIEW(list_view)));
251         gtk_list_store_clear(list_store);
252 }
253
254 static void uri_opener_load_uris (void) 
255 {
256         GSList *cur = opener.uris;
257         
258         uri_opener_list_view_clear_uris(opener.urilist);
259         for (; cur; cur = cur->next) {
260                 ClickableText *uri = (ClickableText *)cur->data;
261                 uri_opener_list_view_insert_uri(opener.urilist, NULL, uri);
262         }
263 }
264
265 static void uri_opener_close(void) 
266 {
267         g_slist_free(opener.uris);
268         opener.uris = NULL;
269         gtk_widget_hide(opener.window);
270 }
271
272 static void uri_opener_close_cb(GtkWidget *widget,
273                                  gpointer data) 
274 {
275         uri_opener_close();
276 }
277
278 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
279 {
280         if (event && event->keyval == GDK_Escape)
281                 uri_opener_close();
282         return FALSE;
283 }
284
285 static void uri_opener_double_clicked(GtkTreeView               *list_view,
286                                         GtkTreePath             *path,
287                                         GtkTreeViewColumn       *column,
288                                         gpointer                 data)
289 {
290         ClickableText *uri;
291         GtkTreeIter iter;
292         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
293
294         if (!gtk_tree_model_get_iter(model, &iter, path))
295                 return;
296
297         gtk_tree_model_get(model, &iter, 
298                            URI_OPENER_DATA, &uri,
299                            -1);
300
301         if (!uri)
302                 return;
303
304         if (textview_uri_security_check(opener.msgview->mimeview->textview, uri) == TRUE) 
305                 open_uri(uri->uri,
306                          prefs_common.uri_cmd);
307 }
308
309
310
311
312 static void uri_opener_open_cb(GtkWidget *widget, 
313                                 gpointer data) 
314 {
315         ClickableText *uri;
316         GtkTreeIter sel;
317         GtkTreeModel *model;
318         gboolean all = GPOINTER_TO_INT(data);
319
320         if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection
321                                 (GTK_TREE_VIEW(opener.urilist)),
322                                 &model, &sel))
323                 return;
324         
325         if (!all) {
326                 gtk_tree_model_get(model, &sel,
327                            URI_OPENER_DATA, &uri,
328                            -1);
329                 if (!uri)
330                         return;
331
332                 if (textview_uri_security_check(opener.msgview->mimeview->textview, uri) == TRUE) 
333                         open_uri(uri->uri,
334                                  prefs_common.uri_cmd);
335         } else {
336                 GSList *cur = opener.uris;
337
338                 for (; cur; cur = cur->next) {
339                         uri = (ClickableText *)cur->data;
340                         if (textview_uri_security_check(opener.msgview->mimeview->textview, uri) == TRUE) 
341                                 open_uri(uri->uri,
342                                          prefs_common.uri_cmd);
343                 }
344         }
345 }