99d34e54bac8cb44c6bc42620a2cd0d95839b1a9
[claws.git] / src / uri_opener.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2012 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 3 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, see <http://www.gnu.org/licenses/>.
18  * 
19  */
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #include "claws-features.h"
23 #endif
24
25 #include <gtk/gtk.h>
26 #include <glib.h>
27 #include <glib/gi18n.h>
28 #include <gdk/gdkkeysyms.h>
29 #include <sys/types.h>
30 #include <dirent.h>
31
32 #include "manage_window.h"
33 #include "utils.h"
34 #include "mainwindow.h"
35 #include "gtkutils.h"
36 #include "alertpanel.h"
37 #include "textview.h"
38 #include "mimeview.h"
39 #include "prefs_common.h"
40
41 enum {
42         URI_OPENER_URL,
43         URI_OPENER_DATA,
44         N_URI_OPENER_COLUMNS
45 };
46
47
48 static struct URIOpener
49 {
50         GtkWidget *window;
51         GtkWidget *hbox_scroll;
52         GtkWidget *hbox1;
53         GtkWidget *vbox1;
54         GtkWidget *label;
55         GtkWidget *urilist;
56         GtkWidget *scrolledwin;
57         GtkWidget *open_btn;
58         GtkWidget *close_btn;
59         MessageView *msgview;
60         GSList    *uris;
61 } opener;
62
63 static void uri_opener_load_uris (void);
64 static void uri_opener_open_cb          (GtkWidget *widget, gpointer data);
65 static void uri_opener_close_cb         (GtkWidget *widget, gpointer data);
66 static void uri_opener_select_all_cb    (GtkWidget *widget, gpointer data);
67 static gboolean key_pressed             (GtkWidget *widget, GdkEventKey *event,
68                                          gpointer data);
69 static void uri_opener_double_clicked(GtkTreeView               *list_view,
70                                         GtkTreePath             *path,
71                                         GtkTreeViewColumn       *column,
72                                         gpointer                 data);
73 static void uri_opener_create(void);
74 void uri_opener_open(MessageView *msgview, GSList *uris)
75 {
76         cm_return_if_fail(msgview);
77         cm_return_if_fail(msgview->mimeview);
78         cm_return_if_fail(msgview->mimeview->textview);
79         cm_return_if_fail(msgview);
80         if (!opener.window)
81                 uri_opener_create();
82
83         manage_window_set_transient(GTK_WINDOW(opener.window));
84         gtk_widget_grab_focus(opener.close_btn);
85         
86         if (uris == NULL) {
87                 alertpanel_notice(_("There are no URLs in this email."));
88                 return;
89         }
90         
91         opener.msgview = msgview;
92         opener.uris = g_slist_copy(uris);
93         uri_opener_load_uris();
94
95         gtk_widget_show(opener.window);
96         gtk_widget_grab_focus(opener.urilist);
97         gtk_window_set_modal(GTK_WINDOW(opener.window), TRUE);
98 }
99
100 static GtkListStore* uri_opener_create_data_store(void)
101 {
102         return gtk_list_store_new(N_URI_OPENER_COLUMNS,
103                                   G_TYPE_STRING,
104                                   G_TYPE_POINTER,
105                                   -1);
106 }
107
108 static void uri_opener_create_list_view_columns(GtkWidget *list_view)
109 {
110         GtkTreeViewColumn *column;
111         GtkCellRenderer *renderer;
112
113         renderer = gtk_cell_renderer_text_new();
114         column = gtk_tree_view_column_new_with_attributes
115                 (_("Available URLs:"),
116                  renderer,
117                  "markup", URI_OPENER_URL,
118                  NULL);
119         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
120 }
121
122 static GtkWidget *uri_opener_list_view_create   (void)
123 {
124         GtkTreeView *list_view;
125         GtkTreeSelection *selector;
126         GtkTreeModel *model;
127
128         model = GTK_TREE_MODEL(uri_opener_create_data_store());
129         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
130         g_object_unref(model);  
131         
132         gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
133         
134         selector = gtk_tree_view_get_selection(list_view);
135         gtk_tree_selection_set_mode(selector, GTK_SELECTION_MULTIPLE);
136
137         g_signal_connect(G_OBJECT(list_view), "row_activated",
138                          G_CALLBACK(uri_opener_double_clicked),
139                          list_view);
140
141         /* create the columns */
142         uri_opener_create_list_view_columns(GTK_WIDGET(list_view));
143
144         return GTK_WIDGET(list_view);
145
146 }
147
148 static GtkWidget *uri_opener_scrolled_win_create(void)
149 {
150         GtkWidget *scrolledwin;
151
152         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
153         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
154                                             GTK_SHADOW_ETCHED_IN);
155         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
156                                        GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
157                                        
158         gtk_widget_set_size_request(scrolledwin, 200, 250);
159         gtk_widget_show(scrolledwin);
160         
161         return scrolledwin;
162 }
163
164 static void uri_opener_size_allocate_cb(GtkWidget *widget, GtkAllocation *allocation)
165 {
166         cm_return_if_fail(allocation != NULL);
167
168         prefs_common.uriopenerwin_width = allocation->width;
169         prefs_common.uriopenerwin_height = allocation->height;
170 }
171
172 static void uri_opener_create(void) 
173 {
174         GtkWidget *window;
175         GtkWidget *hbox_scroll;
176         GtkWidget *hbox;
177         GtkWidget *hbox1;
178         GtkWidget *vbox1;
179         GtkWidget *label;
180         GtkWidget *urilist;
181         GtkWidget *select_all_btn;
182         GtkWidget *open_btn;
183         GtkWidget *close_btn;
184         GtkWidget *scrolledwin;
185         static GdkGeometry geometry;
186
187         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "uri_opener");
188         gtk_window_set_title (GTK_WINDOW(window),
189                               C_("Dialog title", "Open URLs"));
190
191         gtk_container_set_border_width (GTK_CONTAINER (window), 8);
192         gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
193         gtk_window_set_resizable(GTK_WINDOW (window), TRUE);
194         g_signal_connect(G_OBJECT(window), "delete_event",
195                          G_CALLBACK(uri_opener_close_cb), NULL);
196         g_signal_connect (G_OBJECT(window), "size_allocate",
197                          G_CALLBACK (uri_opener_size_allocate_cb), NULL);
198         g_signal_connect(G_OBJECT(window), "key_press_event",
199                          G_CALLBACK(key_pressed), NULL);
200         MANAGE_WINDOW_SIGNALS_CONNECT (window);
201
202         vbox1 = gtk_vbox_new(FALSE, 6);
203         gtkut_stock_button_set_create(&hbox1, 
204                                       &open_btn, GTK_STOCK_OPEN,
205                                       &close_btn, GTK_STOCK_CLOSE,
206                                       NULL, NULL);
207
208         g_signal_connect(G_OBJECT(open_btn), "clicked",
209                          G_CALLBACK(uri_opener_open_cb), NULL);
210
211         g_signal_connect(G_OBJECT(close_btn), "clicked",
212                          G_CALLBACK(uri_opener_close_cb), NULL);
213
214         urilist = uri_opener_list_view_create();
215         
216         label = gtk_label_new(_("Please select the URL to open."));
217         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
218         gtk_box_pack_start(GTK_BOX(vbox1), label, FALSE, TRUE, 0);
219         
220         scrolledwin = uri_opener_scrolled_win_create();
221         hbox_scroll = gtk_hbox_new(FALSE, 0);
222         gtk_box_pack_start(GTK_BOX(hbox_scroll), scrolledwin, TRUE, TRUE, 0);
223         
224         select_all_btn = gtk_button_new_with_label(_("Select All"));
225         g_signal_connect(G_OBJECT(select_all_btn), "clicked",
226                          G_CALLBACK(uri_opener_select_all_cb), NULL);   
227
228         hbox = gtk_hbox_new(FALSE, 0);
229         gtk_box_pack_start(GTK_BOX(hbox), select_all_btn, FALSE, FALSE, 0);
230         gtk_box_pack_start(GTK_BOX(hbox), gtk_label_new(""), TRUE, TRUE, 0);
231         
232         gtk_container_add(GTK_CONTAINER(scrolledwin), urilist);
233         gtk_box_pack_start(GTK_BOX(vbox1), hbox_scroll, TRUE, TRUE, 0);
234         gtk_box_pack_start(GTK_BOX(vbox1), hbox, FALSE, FALSE, 0);
235         gtk_box_pack_start(GTK_BOX(vbox1), hbox1, FALSE, FALSE, 0);
236         
237         gtk_widget_show_all(vbox1);
238         gtk_container_add(GTK_CONTAINER (window), vbox1);
239
240         if (!geometry.min_height) {
241                 geometry.min_width = 450;
242                 geometry.min_height = 300;
243         }
244
245         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
246                                       GDK_HINT_MIN_SIZE);
247         gtk_widget_set_size_request(window, prefs_common.uriopenerwin_width,
248                                     prefs_common.uriopenerwin_height);
249
250         opener.window = window;
251         opener.hbox_scroll = hbox_scroll;
252         opener.hbox1 = hbox1;
253         opener.vbox1 = vbox1;
254         opener.label = label;
255         opener.urilist = urilist;
256         opener.scrolledwin = scrolledwin;
257         opener.open_btn = open_btn;
258         opener.close_btn = close_btn;
259
260 }
261
262 static void uri_opener_list_view_insert_uri(GtkWidget *list_view,
263                                                   GtkTreeIter *row_iter,
264                                                   ClickableText *uri) 
265 {
266         GtkTreeIter iter;
267         GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
268                                         (GTK_TREE_VIEW(list_view)));
269         gchar *visible = textview_get_visible_uri(opener.msgview->mimeview->textview, uri);
270         
271         gchar *label = NULL;
272         
273         if (visible && strcmp(visible, uri->uri))
274                 label = g_markup_printf_escaped("<b>%s</b>\n%s", visible, uri->uri);
275         else
276                 label = g_markup_printf_escaped("\n%s", uri->uri);
277
278         if (row_iter == NULL) {
279                 /* append new */
280                 gtk_list_store_append(list_store, &iter);
281                 gtk_list_store_set(list_store, &iter,
282                                    URI_OPENER_URL, label,
283                                    URI_OPENER_DATA, uri,
284                                    -1);
285         } else {
286                 gtk_list_store_set(list_store, row_iter,
287                                    URI_OPENER_URL, label,
288                                    URI_OPENER_DATA, uri,
289                                    -1);
290         }
291         g_free(visible);
292         g_free(label);
293 }
294
295 static void uri_opener_list_view_clear_uris(GtkWidget *list_view)
296 {
297         GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
298                                         (GTK_TREE_VIEW(list_view)));
299         gtk_list_store_clear(list_store);
300 }
301
302 static void uri_opener_load_uris (void) 
303 {
304         GSList *cur = opener.uris;
305         GtkTreeModel *model;
306         GtkTreeSelection *selection;
307         GtkTreeIter iter;
308         
309         uri_opener_list_view_clear_uris(opener.urilist);
310         for (; cur; cur = cur->next) {
311                 ClickableText *uri = (ClickableText *)cur->data;
312                 uri_opener_list_view_insert_uri(opener.urilist, NULL, uri);
313         }
314         
315         g_object_ref(opener.urilist);
316         gtk_container_remove(GTK_CONTAINER(opener.scrolledwin), opener.urilist);
317         gtk_widget_destroy(opener.scrolledwin);
318         
319         opener.scrolledwin = uri_opener_scrolled_win_create();
320         gtk_container_add(GTK_CONTAINER(opener.scrolledwin), opener.urilist);
321         gtk_box_pack_start(GTK_BOX(opener.hbox_scroll),
322                            opener.scrolledwin, TRUE, TRUE, 0);
323         g_object_unref(opener.urilist);
324         
325         model = gtk_tree_view_get_model(GTK_TREE_VIEW(opener.urilist));
326         if (!gtk_tree_model_get_iter_first(model, &iter))
327                 return;
328         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(opener.urilist));
329         gtk_tree_selection_select_iter(selection, &iter);
330 }
331
332 static void uri_opener_close(void) 
333 {
334         g_slist_free(opener.uris);
335         opener.uris = NULL;
336         gtk_window_set_modal(GTK_WINDOW(opener.window), FALSE);
337         gtk_widget_hide(opener.window);
338 }
339
340 static void uri_opener_close_cb(GtkWidget *widget,
341                                  gpointer data) 
342 {
343         uri_opener_close();
344 }
345
346 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
347 {
348         if (event && event->keyval == GDK_KEY_Escape)
349                 uri_opener_close();
350         return FALSE;
351 }
352
353 static void uri_opener_double_clicked(GtkTreeView               *list_view,
354                                         GtkTreePath             *path,
355                                         GtkTreeViewColumn       *column,
356                                         gpointer                 data)
357 {
358         ClickableText *uri;
359         GtkTreeIter iter;
360         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
361
362         if (!gtk_tree_model_get_iter(model, &iter, path))
363                 return;
364
365         gtk_tree_model_get(model, &iter, 
366                            URI_OPENER_DATA, &uri,
367                            -1);
368
369         if (!uri)
370                 return;
371
372         if (textview_uri_security_check(opener.msgview->mimeview->textview, uri) == TRUE) 
373                 open_uri(uri->uri,
374                          prefs_common_get_uri_cmd());
375 }
376
377 static void uri_opener_open_cb(GtkWidget *widget, 
378                                 gpointer data) 
379 {
380         ClickableText *uri;
381         GtkTreeIter sel;
382         GtkTreeModel *model;
383         GtkTreeSelection *selection;
384         GList *selected, *cur;
385
386         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(opener.urilist));
387         selected  = gtk_tree_selection_get_selected_rows(selection, &model);
388         cm_return_if_fail(selected);
389                 
390         for(cur = selected; cur != NULL; cur = g_list_next(cur))
391         { 
392                 if(!gtk_tree_model_get_iter(model, &sel, (GtkTreePath *)cur->data))
393                         continue;
394                 
395                 gtk_tree_model_get(model, &sel,
396                            URI_OPENER_DATA, &uri,
397                            -1);
398                 if (!uri)
399                         continue;
400
401                 if (textview_uri_security_check(opener.msgview->mimeview->textview, uri) == TRUE) 
402                         open_uri(uri->uri,
403                                  prefs_common_get_uri_cmd());
404         }
405         
406         g_list_foreach(selected, (GFunc)gtk_tree_path_free, NULL);
407         g_list_free(selected);
408 }
409
410 static void uri_opener_select_all_cb(GtkWidget *widget, 
411                                      gpointer data)
412 {
413         GtkTreeSelection *selection = gtk_tree_view_get_selection(
414                                                 GTK_TREE_VIEW(opener.urilist));
415         gtk_tree_selection_select_all(selection);
416 }