d117672aac7df8a748578208db1d3b1d1d9f24a6
[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 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 #endif
23
24 #include <gtk/gtk.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 *scrolledwin;
55         GtkWidget *open_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 void uri_opener_select_all_cb    (GtkWidget *widget, gpointer data);
65 static gboolean key_pressed             (GtkWidget *widget, GdkEventKey *event,
66                                          gpointer data);
67 static void uri_opener_double_clicked(GtkTreeView               *list_view,
68                                         GtkTreePath             *path,
69                                         GtkTreeViewColumn       *column,
70                                         gpointer                 data);
71 static void uri_opener_create(void);
72 void uri_opener_open(MessageView *msgview, GSList *uris)
73 {
74         GtkAdjustment *hadj, *vadj;
75         
76         g_return_if_fail(msgview);
77         g_return_if_fail(msgview->mimeview);
78         g_return_if_fail(msgview->mimeview->textview);
79         g_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         vadj = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(
100                                                    opener.scrolledwin));
101         gtk_adjustment_set_value(vadj, 0);
102         hadj = gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(
103                                                    opener.scrolledwin));
104         gtk_adjustment_set_value(hadj, 0);
105
106 }
107
108 static GtkListStore* uri_opener_create_data_store(void)
109 {
110         return gtk_list_store_new(N_URI_OPENER_COLUMNS,
111                                   G_TYPE_STRING,
112                                   G_TYPE_POINTER,
113                                   -1);
114 }
115
116 static void uri_opener_create_list_view_columns(GtkWidget *list_view)
117 {
118         GtkTreeViewColumn *column;
119         GtkCellRenderer *renderer;
120
121         renderer = gtk_cell_renderer_text_new();
122         column = gtk_tree_view_column_new_with_attributes
123                 (_("Available URLs:"),
124                  renderer,
125                  "text", URI_OPENER_URL,
126                  NULL);
127         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
128 }
129
130 static GtkWidget *uri_opener_list_view_create   (void)
131 {
132         GtkTreeView *list_view;
133         GtkTreeSelection *selector;
134         GtkTreeModel *model;
135
136         model = GTK_TREE_MODEL(uri_opener_create_data_store());
137         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
138         g_object_unref(model);  
139         
140         gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
141         
142         selector = gtk_tree_view_get_selection(list_view);
143         gtk_tree_selection_set_mode(selector, GTK_SELECTION_MULTIPLE);
144
145         g_signal_connect(G_OBJECT(list_view), "row_activated",
146                          G_CALLBACK(uri_opener_double_clicked),
147                          list_view);
148
149         /* create the columns */
150         uri_opener_create_list_view_columns(GTK_WIDGET(list_view));
151
152         return GTK_WIDGET(list_view);
153
154 }
155
156 static void uri_opener_create(void) 
157 {
158         GtkWidget *window;
159         GtkWidget *hbox;
160         GtkWidget *hbox1;
161         GtkWidget *vbox1;
162         GtkWidget *label;
163         GtkWidget *urilist;
164         GtkWidget *select_all_btn;
165         GtkWidget *open_btn;
166         GtkWidget *close_btn;
167         GtkWidget *scrolledwin;
168
169         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "uri_opener");
170         gtk_window_set_title (GTK_WINDOW(window),
171                               Q_("Dialog title|Open URLs"));
172
173         gtk_container_set_border_width (GTK_CONTAINER (window), 8);
174         gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
175         gtk_window_set_resizable(GTK_WINDOW (window), TRUE);
176         g_signal_connect(G_OBJECT(window), "delete_event",
177                          G_CALLBACK(uri_opener_close_cb), NULL);
178         g_signal_connect(G_OBJECT(window), "key_press_event",
179                          G_CALLBACK(key_pressed), NULL);
180         MANAGE_WINDOW_SIGNALS_CONNECT (window);
181
182         vbox1 = gtk_vbox_new(FALSE, 6);
183         gtkut_stock_button_set_create(&hbox1, 
184                                       &open_btn, GTK_STOCK_OPEN,
185                                       &close_btn, GTK_STOCK_CLOSE,
186                                       NULL, NULL);
187
188         g_signal_connect(G_OBJECT(open_btn), "clicked",
189                          G_CALLBACK(uri_opener_open_cb), NULL);
190
191         g_signal_connect(G_OBJECT(close_btn), "clicked",
192                          G_CALLBACK(uri_opener_close_cb), NULL);
193
194         urilist = uri_opener_list_view_create();
195         
196         label = gtk_label_new(_("Please select the URL to open."));
197         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
198         gtk_box_pack_start(GTK_BOX(vbox1), label, FALSE, TRUE, 0);
199         
200         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
201         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
202                                             GTK_SHADOW_ETCHED_IN);
203         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
204                                        GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
205                                        
206         gtk_widget_set_size_request(scrolledwin, 500, 250);
207
208         select_all_btn = gtk_button_new_with_label(_("Select All"));
209         g_signal_connect(G_OBJECT(select_all_btn), "clicked",
210                          G_CALLBACK(uri_opener_select_all_cb), NULL);   
211
212         hbox = gtk_hbox_new(FALSE, 0);
213         gtk_box_pack_start(GTK_BOX(hbox), select_all_btn, FALSE, FALSE, 0);
214         gtk_box_pack_start(GTK_BOX(hbox), gtk_label_new(""), TRUE, TRUE, 0);
215         
216         gtk_container_add(GTK_CONTAINER(scrolledwin), urilist);
217         gtk_box_pack_start(GTK_BOX(vbox1), scrolledwin, TRUE, TRUE, 0);
218         gtk_box_pack_start(GTK_BOX(vbox1), hbox, FALSE, FALSE, 0);
219         gtk_box_pack_start(GTK_BOX(vbox1), hbox1, FALSE, FALSE, 0);
220         
221         gtk_widget_show_all(vbox1);
222         gtk_container_add(GTK_CONTAINER (window), vbox1);
223
224         opener.window = window;
225         opener.hbox1 = hbox1;
226         opener.vbox1 = vbox1;
227         opener.label = label;
228         opener.urilist = urilist;
229         opener.scrolledwin = scrolledwin;
230         opener.open_btn = open_btn;
231         opener.close_btn = close_btn;
232
233 }
234
235 static void uri_opener_list_view_insert_uri(GtkWidget *list_view,
236                                                   GtkTreeIter *row_iter,
237                                                   ClickableText *uri) 
238 {
239         GtkTreeIter iter;
240         GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
241                                         (GTK_TREE_VIEW(list_view)));
242         gchar *visible = textview_get_visible_uri(opener.msgview->mimeview->textview, uri);
243         
244         if (row_iter == NULL) {
245                 /* append new */
246                 gtk_list_store_append(list_store, &iter);
247                 gtk_list_store_set(list_store, &iter,
248                                    URI_OPENER_URL, visible?visible:uri->uri,
249                                    URI_OPENER_DATA, uri,
250                                    -1);
251         } else {
252                 gtk_list_store_set(list_store, row_iter,
253                                    URI_OPENER_URL, visible?visible:uri->uri,
254                                    URI_OPENER_DATA, uri,
255                                    -1);
256         }
257         g_free(visible);
258 }
259
260 static void uri_opener_list_view_clear_uris(GtkWidget *list_view)
261 {
262         GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
263                                         (GTK_TREE_VIEW(list_view)));
264         gtk_list_store_clear(list_store);
265 }
266
267 static void uri_opener_load_uris (void) 
268 {
269         GSList *cur = opener.uris;
270         GtkTreeModel *model;
271         GtkTreeSelection *selection;
272         GtkTreeIter iter;
273         
274         uri_opener_list_view_clear_uris(opener.urilist);
275         for (; cur; cur = cur->next) {
276                 ClickableText *uri = (ClickableText *)cur->data;
277                 uri_opener_list_view_insert_uri(opener.urilist, NULL, uri);
278         }
279         
280         model = gtk_tree_view_get_model(GTK_TREE_VIEW(opener.urilist));
281         gtk_tree_model_get_iter_first(model, &iter);
282         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(opener.urilist));
283         gtk_tree_selection_select_iter(selection, &iter);
284 }
285
286 static void uri_opener_close(void) 
287 {
288         g_slist_free(opener.uris);
289         opener.uris = NULL;
290         gtk_widget_hide(opener.window);
291 }
292
293 static void uri_opener_close_cb(GtkWidget *widget,
294                                  gpointer data) 
295 {
296         uri_opener_close();
297 }
298
299 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
300 {
301         if (event && event->keyval == GDK_Escape)
302                 uri_opener_close();
303         return FALSE;
304 }
305
306 static void uri_opener_double_clicked(GtkTreeView               *list_view,
307                                         GtkTreePath             *path,
308                                         GtkTreeViewColumn       *column,
309                                         gpointer                 data)
310 {
311         ClickableText *uri;
312         GtkTreeIter iter;
313         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
314
315         if (!gtk_tree_model_get_iter(model, &iter, path))
316                 return;
317
318         gtk_tree_model_get(model, &iter, 
319                            URI_OPENER_DATA, &uri,
320                            -1);
321
322         if (!uri)
323                 return;
324
325         if (textview_uri_security_check(opener.msgview->mimeview->textview, uri) == TRUE) 
326                 open_uri(uri->uri,
327                          prefs_common_get_uri_cmd());
328 }
329
330 static void uri_opener_open_cb(GtkWidget *widget, 
331                                 gpointer data) 
332 {
333         ClickableText *uri;
334         GtkTreeIter sel;
335         GtkTreeModel *model;
336         GtkTreeSelection *selection;
337         GList *selected, *cur;
338
339         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(opener.urilist));
340         selected  = gtk_tree_selection_get_selected_rows(selection, &model);
341         if(!selected)
342                 return;
343                 
344         for(cur = selected; cur != NULL; cur = g_list_next(cur))
345         { 
346                 if(!gtk_tree_model_get_iter(model, &sel, (GtkTreePath *)cur->data))
347                         continue;
348                 
349                 gtk_tree_model_get(model, &sel,
350                            URI_OPENER_DATA, &uri,
351                            -1);
352                 if (!uri)
353                         continue;
354
355                 if (textview_uri_security_check(opener.msgview->mimeview->textview, uri) == TRUE) 
356                         open_uri(uri->uri,
357                                  prefs_common_get_uri_cmd());
358         }
359         
360         g_list_foreach(selected, (GFunc)gtk_tree_path_free, NULL);
361         g_list_free(selected);
362 }
363
364 static void uri_opener_select_all_cb(GtkWidget *widget, 
365                                      gpointer data)
366 {
367         GtkTreeSelection *selection = gtk_tree_view_get_selection(
368                                                 GTK_TREE_VIEW(opener.urilist));
369         gtk_tree_selection_select_all(selection);
370 }