Fix bug #3574: Template addressing
[claws.git] / src / uri_opener.c
blob4929b52947dd3a05cd061dec26c1ef36731fc4d9
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
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.
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.
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/>.
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #include "claws-features.h"
23 #endif
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>
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 #include "prefs_common.h"
42 enum {
43 URI_OPENER_URL,
44 URI_OPENER_DATA,
45 N_URI_OPENER_COLUMNS
49 static struct URIOpener
51 GtkWidget *window;
52 GtkWidget *hbox_scroll;
53 GtkWidget *hbox1;
54 GtkWidget *vbox1;
55 GtkWidget *label;
56 GtkWidget *urilist;
57 GtkWidget *scrolledwin;
58 GtkWidget *open_btn;
59 GtkWidget *close_btn;
60 MessageView *msgview;
61 GSList *uris;
62 } opener;
64 static void uri_opener_load_uris (void);
65 static void uri_opener_open_cb (GtkWidget *widget, gpointer data);
66 static void uri_opener_close_cb (GtkWidget *widget, gpointer data);
67 static void uri_opener_select_all_cb (GtkWidget *widget, gpointer data);
68 static gboolean key_pressed (GtkWidget *widget, GdkEventKey *event,
69 gpointer data);
70 static void uri_opener_double_clicked(GtkTreeView *list_view,
71 GtkTreePath *path,
72 GtkTreeViewColumn *column,
73 gpointer data);
74 static void uri_opener_create(void);
75 void uri_opener_open(MessageView *msgview, GSList *uris)
77 cm_return_if_fail(msgview);
78 cm_return_if_fail(msgview->mimeview);
79 cm_return_if_fail(msgview->mimeview->textview);
80 cm_return_if_fail(msgview);
81 if (!opener.window)
82 uri_opener_create();
84 manage_window_set_transient(GTK_WINDOW(opener.window));
85 gtk_widget_grab_focus(opener.close_btn);
87 if (uris == NULL) {
88 alertpanel_notice(_("There are no URLs in this email."));
89 return;
92 opener.msgview = msgview;
93 opener.uris = g_slist_copy(uris);
94 uri_opener_load_uris();
96 gtk_widget_show(opener.window);
97 gtk_widget_grab_focus(opener.urilist);
98 gtk_window_set_modal(GTK_WINDOW(opener.window), TRUE);
101 static GtkListStore* uri_opener_create_data_store(void)
103 return gtk_list_store_new(N_URI_OPENER_COLUMNS,
104 G_TYPE_STRING,
105 G_TYPE_POINTER,
106 -1);
109 static void uri_opener_create_list_view_columns(GtkWidget *list_view)
111 GtkTreeViewColumn *column;
112 GtkCellRenderer *renderer;
114 renderer = gtk_cell_renderer_text_new();
115 column = gtk_tree_view_column_new_with_attributes
116 (_("Available URLs:"),
117 renderer,
118 "markup", URI_OPENER_URL,
119 NULL);
120 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
123 static GtkWidget *uri_opener_list_view_create (void)
125 GtkTreeView *list_view;
126 GtkTreeSelection *selector;
127 GtkTreeModel *model;
129 model = GTK_TREE_MODEL(uri_opener_create_data_store());
130 list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
131 g_object_unref(model);
133 gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
135 selector = gtk_tree_view_get_selection(list_view);
136 gtk_tree_selection_set_mode(selector, GTK_SELECTION_MULTIPLE);
138 g_signal_connect(G_OBJECT(list_view), "row_activated",
139 G_CALLBACK(uri_opener_double_clicked),
140 list_view);
142 /* create the columns */
143 uri_opener_create_list_view_columns(GTK_WIDGET(list_view));
145 return GTK_WIDGET(list_view);
149 static GtkWidget *uri_opener_scrolled_win_create(void)
151 GtkWidget *scrolledwin;
153 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
154 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
155 GTK_SHADOW_ETCHED_IN);
156 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
157 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
159 gtk_widget_set_size_request(scrolledwin, 500, 250);
160 gtk_widget_show(scrolledwin);
162 return scrolledwin;
165 static void uri_opener_create(void)
167 GtkWidget *window;
168 GtkWidget *hbox_scroll;
169 GtkWidget *hbox;
170 GtkWidget *hbox1;
171 GtkWidget *vbox1;
172 GtkWidget *label;
173 GtkWidget *urilist;
174 GtkWidget *select_all_btn;
175 GtkWidget *open_btn;
176 GtkWidget *close_btn;
177 GtkWidget *scrolledwin;
179 window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "uri_opener");
180 gtk_window_set_title (GTK_WINDOW(window),
181 C_("Dialog title", "Open URLs"));
183 gtk_container_set_border_width (GTK_CONTAINER (window), 8);
184 gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
185 gtk_window_set_resizable(GTK_WINDOW (window), TRUE);
186 g_signal_connect(G_OBJECT(window), "delete_event",
187 G_CALLBACK(uri_opener_close_cb), NULL);
188 g_signal_connect(G_OBJECT(window), "key_press_event",
189 G_CALLBACK(key_pressed), NULL);
190 MANAGE_WINDOW_SIGNALS_CONNECT (window);
192 vbox1 = gtk_vbox_new(FALSE, 6);
193 gtkut_stock_button_set_create(&hbox1,
194 &open_btn, GTK_STOCK_OPEN,
195 &close_btn, GTK_STOCK_CLOSE,
196 NULL, NULL);
198 g_signal_connect(G_OBJECT(open_btn), "clicked",
199 G_CALLBACK(uri_opener_open_cb), NULL);
201 g_signal_connect(G_OBJECT(close_btn), "clicked",
202 G_CALLBACK(uri_opener_close_cb), NULL);
204 urilist = uri_opener_list_view_create();
206 label = gtk_label_new(_("Please select the URL to open."));
207 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
208 gtk_box_pack_start(GTK_BOX(vbox1), label, FALSE, TRUE, 0);
210 scrolledwin = uri_opener_scrolled_win_create();
211 hbox_scroll = gtk_hbox_new(FALSE, 0);
212 gtk_box_pack_start(GTK_BOX(hbox_scroll), scrolledwin, TRUE, TRUE, 0);
214 select_all_btn = gtk_button_new_with_label(_("Select All"));
215 g_signal_connect(G_OBJECT(select_all_btn), "clicked",
216 G_CALLBACK(uri_opener_select_all_cb), NULL);
218 hbox = gtk_hbox_new(FALSE, 0);
219 gtk_box_pack_start(GTK_BOX(hbox), select_all_btn, FALSE, FALSE, 0);
220 gtk_box_pack_start(GTK_BOX(hbox), gtk_label_new(""), TRUE, TRUE, 0);
222 gtk_container_add(GTK_CONTAINER(scrolledwin), urilist);
223 gtk_box_pack_start(GTK_BOX(vbox1), hbox_scroll, TRUE, TRUE, 0);
224 gtk_box_pack_start(GTK_BOX(vbox1), hbox, FALSE, FALSE, 0);
225 gtk_box_pack_start(GTK_BOX(vbox1), hbox1, FALSE, FALSE, 0);
227 gtk_widget_show_all(vbox1);
228 gtk_container_add(GTK_CONTAINER (window), vbox1);
230 opener.window = window;
231 opener.hbox_scroll = hbox_scroll;
232 opener.hbox1 = hbox1;
233 opener.vbox1 = vbox1;
234 opener.label = label;
235 opener.urilist = urilist;
236 opener.scrolledwin = scrolledwin;
237 opener.open_btn = open_btn;
238 opener.close_btn = close_btn;
242 static void uri_opener_list_view_insert_uri(GtkWidget *list_view,
243 GtkTreeIter *row_iter,
244 ClickableText *uri)
246 GtkTreeIter iter;
247 GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
248 (GTK_TREE_VIEW(list_view)));
249 gchar *visible = textview_get_visible_uri(opener.msgview->mimeview->textview, uri);
251 gchar *label = NULL;
253 if (visible && strcmp(visible, uri->uri))
254 label = g_markup_printf_escaped("<b>%s</b>\n%s", visible, uri->uri);
255 else
256 label = g_markup_printf_escaped("\n%s", uri->uri);
258 if (row_iter == NULL) {
259 /* append new */
260 gtk_list_store_append(list_store, &iter);
261 gtk_list_store_set(list_store, &iter,
262 URI_OPENER_URL, label,
263 URI_OPENER_DATA, uri,
264 -1);
265 } else {
266 gtk_list_store_set(list_store, row_iter,
267 URI_OPENER_URL, label,
268 URI_OPENER_DATA, uri,
269 -1);
271 g_free(visible);
272 g_free(label);
275 static void uri_opener_list_view_clear_uris(GtkWidget *list_view)
277 GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
278 (GTK_TREE_VIEW(list_view)));
279 gtk_list_store_clear(list_store);
282 static void uri_opener_load_uris (void)
284 GSList *cur = opener.uris;
285 GtkTreeModel *model;
286 GtkTreeSelection *selection;
287 GtkTreeIter iter;
289 uri_opener_list_view_clear_uris(opener.urilist);
290 for (; cur; cur = cur->next) {
291 ClickableText *uri = (ClickableText *)cur->data;
292 uri_opener_list_view_insert_uri(opener.urilist, NULL, uri);
295 g_object_ref(opener.urilist);
296 gtk_container_remove(GTK_CONTAINER(opener.scrolledwin), opener.urilist);
297 gtk_widget_destroy(opener.scrolledwin);
299 opener.scrolledwin = uri_opener_scrolled_win_create();
300 gtk_container_add(GTK_CONTAINER(opener.scrolledwin), opener.urilist);
301 gtk_box_pack_start(GTK_BOX(opener.hbox_scroll),
302 opener.scrolledwin, TRUE, TRUE, 0);
303 g_object_unref(opener.urilist);
305 model = gtk_tree_view_get_model(GTK_TREE_VIEW(opener.urilist));
306 gtk_tree_model_get_iter_first(model, &iter);
307 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(opener.urilist));
308 gtk_tree_selection_select_iter(selection, &iter);
311 static void uri_opener_close(void)
313 g_slist_free(opener.uris);
314 opener.uris = NULL;
315 gtk_window_set_modal(GTK_WINDOW(opener.window), FALSE);
316 gtk_widget_hide(opener.window);
319 static void uri_opener_close_cb(GtkWidget *widget,
320 gpointer data)
322 uri_opener_close();
325 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
327 if (event && event->keyval == GDK_KEY_Escape)
328 uri_opener_close();
329 return FALSE;
332 static void uri_opener_double_clicked(GtkTreeView *list_view,
333 GtkTreePath *path,
334 GtkTreeViewColumn *column,
335 gpointer data)
337 ClickableText *uri;
338 GtkTreeIter iter;
339 GtkTreeModel *model = gtk_tree_view_get_model(list_view);
341 if (!gtk_tree_model_get_iter(model, &iter, path))
342 return;
344 gtk_tree_model_get(model, &iter,
345 URI_OPENER_DATA, &uri,
346 -1);
348 if (!uri)
349 return;
351 if (textview_uri_security_check(opener.msgview->mimeview->textview, uri) == TRUE)
352 open_uri(uri->uri,
353 prefs_common_get_uri_cmd());
356 static void uri_opener_open_cb(GtkWidget *widget,
357 gpointer data)
359 ClickableText *uri;
360 GtkTreeIter sel;
361 GtkTreeModel *model;
362 GtkTreeSelection *selection;
363 GList *selected, *cur;
365 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(opener.urilist));
366 selected = gtk_tree_selection_get_selected_rows(selection, &model);
367 if(!selected)
368 return;
370 for(cur = selected; cur != NULL; cur = g_list_next(cur))
372 if(!gtk_tree_model_get_iter(model, &sel, (GtkTreePath *)cur->data))
373 continue;
375 gtk_tree_model_get(model, &sel,
376 URI_OPENER_DATA, &uri,
377 -1);
378 if (!uri)
379 continue;
381 if (textview_uri_security_check(opener.msgview->mimeview->textview, uri) == TRUE)
382 open_uri(uri->uri,
383 prefs_common_get_uri_cmd());
386 g_list_foreach(selected, (GFunc)gtk_tree_path_free, NULL);
387 g_list_free(selected);
390 static void uri_opener_select_all_cb(GtkWidget *widget,
391 gpointer data)
393 GtkTreeSelection *selection = gtk_tree_view_get_selection(
394 GTK_TREE_VIEW(opener.urilist));
395 gtk_tree_selection_select_all(selection);