2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2012 Colin Leroy <colin@colino.net> and
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/>.
22 #include "claws-features.h"
27 #include <glib/gi18n.h>
28 #include <gdk/gdkkeysyms.h>
29 #include <sys/types.h>
32 #include "manage_window.h"
34 #include "mainwindow.h"
36 #include "alertpanel.h"
39 #include "prefs_common.h"
40 #include "prefs_common.h"
49 static struct URIOpener
52 GtkWidget
*hbox_scroll
;
57 GtkWidget
*scrolledwin
;
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
,
70 static void uri_opener_double_clicked(GtkTreeView
*list_view
,
72 GtkTreeViewColumn
*column
,
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
);
84 manage_window_set_transient(GTK_WINDOW(opener
.window
));
85 gtk_widget_grab_focus(opener
.close_btn
);
88 alertpanel_notice(_("There are no URLs in this email."));
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
,
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:"),
118 "markup", URI_OPENER_URL
,
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
;
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
),
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
);
165 static void uri_opener_create(void)
168 GtkWidget
*hbox_scroll
;
174 GtkWidget
*select_all_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
,
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
,
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
);
253 if (visible
&& strcmp(visible
, uri
->uri
))
254 label
= g_markup_printf_escaped("<b>%s</b>\n%s", visible
, uri
->uri
);
256 label
= g_markup_printf_escaped("\n%s", uri
->uri
);
258 if (row_iter
== NULL
) {
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
,
266 gtk_list_store_set(list_store
, row_iter
,
267 URI_OPENER_URL
, label
,
268 URI_OPENER_DATA
, uri
,
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
;
286 GtkTreeSelection
*selection
;
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
);
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
,
325 static gboolean
key_pressed(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
327 if (event
&& event
->keyval
== GDK_KEY_Escape
)
332 static void uri_opener_double_clicked(GtkTreeView
*list_view
,
334 GtkTreeViewColumn
*column
,
339 GtkTreeModel
*model
= gtk_tree_view_get_model(list_view
);
341 if (!gtk_tree_model_get_iter(model
, &iter
, path
))
344 gtk_tree_model_get(model
, &iter
,
345 URI_OPENER_DATA
, &uri
,
351 if (textview_uri_security_check(opener
.msgview
->mimeview
->textview
, uri
) == TRUE
)
353 prefs_common_get_uri_cmd());
356 static void uri_opener_open_cb(GtkWidget
*widget
,
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
);
370 for(cur
= selected
; cur
!= NULL
; cur
= g_list_next(cur
))
372 if(!gtk_tree_model_get_iter(model
, &sel
, (GtkTreePath
*)cur
->data
))
375 gtk_tree_model_get(model
, &sel
,
376 URI_OPENER_DATA
, &uri
,
381 if (textview_uri_security_check(opener
.msgview
->mimeview
->textview
, uri
) == TRUE
)
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
,
393 GtkTreeSelection
*selection
= gtk_tree_view_get_selection(
394 GTK_TREE_VIEW(opener
.urilist
));
395 gtk_tree_selection_select_all(selection
);