1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright © 2000-2003 Marco Pesenti Gritti
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * $Id: popup-commands.c 7014 2007-04-24 19:45:19Z diegoe $
24 #include "popup-commands.h"
25 #include "ephy-shell.h"
26 #include "ephy-embed-factory.h"
27 #include "ephy-embed-persist.h"
28 #include "ephy-prefs.h"
29 #include "eel-gconf-extensions.h"
30 #include "ephy-file-helpers.h"
31 #include "ephy-bookmarks-ui.h"
34 #include <glib/gi18n.h>
35 #include <gtk/gtkclipboard.h>
36 #include <gtk/gtkmain.h>
37 #include <libgnomevfs/gnome-vfs-utils.h>
38 #include <libgnomevfs/gnome-vfs-file-info.h>
39 #include <libgnomevfs/gnome-vfs-ops.h>
40 #include <libgnomevfs/gnome-vfs-mime-handlers.h>
43 popup_cmd_link_in_new_window (GtkAction
*action
,
46 EphyEmbedEvent
*event
;
50 tab
= ephy_window_get_active_tab (window
);
52 event
= ephy_window_get_context_event (window
);
53 g_return_if_fail (event
!= NULL
);
55 value
= ephy_embed_event_get_property (event
, "link");
57 ephy_shell_new_tab (ephy_shell
, NULL
, tab
,
58 g_value_get_string (value
),
59 EPHY_NEW_TAB_OPEN_PAGE
|
60 EPHY_NEW_TAB_IN_NEW_WINDOW
);
64 popup_cmd_link_in_new_tab (GtkAction
*action
,
67 EphyEmbedEvent
*event
;
71 tab
= ephy_window_get_active_tab (window
);
73 event
= ephy_window_get_context_event (window
);
74 g_return_if_fail (event
!= NULL
);
76 value
= ephy_embed_event_get_property (event
, "link");
78 ephy_shell_new_tab (ephy_shell
, window
, tab
,
79 g_value_get_string (value
),
80 EPHY_NEW_TAB_OPEN_PAGE
|
81 EPHY_NEW_TAB_IN_EXISTING_WINDOW
);
85 popup_cmd_bookmark_link (GtkAction
*action
,
88 EphyEmbedEvent
*event
;
89 const GValue
*link_title
;
90 const GValue
*link_rel
;
92 const GValue
*link_is_smart
;
93 const GValue
*linktext
;
99 event
= ephy_window_get_context_event (window
);
100 g_return_if_fail (event
!= NULL
);
102 link_is_smart
= ephy_embed_event_get_property (event
, "link_is_smart");
103 link
= ephy_embed_event_get_property (event
, "link");
104 link_title
= ephy_embed_event_get_property (event
, "link_title");
105 link_rel
= ephy_embed_event_get_property (event
, "link_rel");
106 linktext
= ephy_embed_event_get_property (event
, "linktext");
108 location
= g_value_get_string (link
);
109 g_return_if_fail (location
);
111 rel
= g_value_get_string (link_rel
);
112 is_smart
= g_value_get_int (link_is_smart
);
114 title
= g_value_get_string (link_title
);
116 if (title
== NULL
|| title
[0] == '\0')
118 title
= g_value_get_string (linktext
);
121 if (title
== NULL
|| title
[0] == '\0')
131 ephy_bookmarks_ui_add_bookmark (GTK_WINDOW (window
), location
, title
);
135 popup_cmd_copy_to_clipboard (EphyWindow
*window
, const char *text
)
137 gtk_clipboard_set_text (gtk_clipboard_get (GDK_NONE
),
139 gtk_clipboard_set_text (gtk_clipboard_get (GDK_SELECTION_PRIMARY
),
144 popup_cmd_copy_link_address (GtkAction
*action
,
147 EphyEmbedEvent
*event
;
148 EphyEmbedEventContext context
;
152 event
= ephy_window_get_context_event (window
);
153 g_return_if_fail (event
!= NULL
);
155 context
= ephy_embed_event_get_context (event
);
157 if (context
& EPHY_EMBED_CONTEXT_EMAIL_LINK
)
159 value
= ephy_embed_event_get_property (event
, "email");
160 address
= g_value_get_string (value
);
161 popup_cmd_copy_to_clipboard (window
, address
);
163 else if (context
& EPHY_EMBED_CONTEXT_LINK
)
165 value
= ephy_embed_event_get_property (event
, "link");
166 address
= g_value_get_string (value
);
167 popup_cmd_copy_to_clipboard (window
, address
);
172 save_property_url_completed_cb (EphyEmbedPersist
*persist
)
174 if (!(ephy_embed_persist_get_flags (persist
) &
175 EPHY_EMBED_PERSIST_ASK_DESTINATION
))
180 user_time
= ephy_embed_persist_get_user_time (persist
);
181 dest
= ephy_embed_persist_get_dest (persist
);
183 g_return_if_fail (dest
!= NULL
);
185 /* If save location is the desktop, nautilus will not open */
186 ephy_file_browse_to (dest
, user_time
);
192 save_property_url (GtkAction
*action
,
196 const char *property
)
198 EphyEmbedEvent
*event
;
199 const char *location
;
201 EphyEmbedPersist
*persist
;
204 event
= ephy_window_get_context_event (window
);
205 g_return_if_fail (event
!= NULL
);
207 embed
= ephy_window_get_active_embed (window
);
208 g_return_if_fail (embed
!= NULL
);
210 value
= ephy_embed_event_get_property (event
, property
);
211 location
= g_value_get_string (value
);
213 persist
= EPHY_EMBED_PERSIST
214 (ephy_embed_factory_new_object (EPHY_TYPE_EMBED_PERSIST
));
216 ephy_embed_persist_set_fc_title (persist
, title
);
217 ephy_embed_persist_set_fc_parent (persist
, GTK_WINDOW (window
));
218 ephy_embed_persist_set_flags
219 (persist
, EPHY_EMBED_PERSIST_FROM_CACHE
|
220 (ask_dest
? EPHY_EMBED_PERSIST_ASK_DESTINATION
: 0));
221 ephy_embed_persist_set_persist_key
222 (persist
, CONF_STATE_SAVE_DIR
);
223 ephy_embed_persist_set_source (persist
, location
);
225 g_signal_connect (persist
, "completed",
226 G_CALLBACK (save_property_url_completed_cb
), NULL
);
228 ephy_embed_persist_save (persist
);
230 g_object_unref (G_OBJECT(persist
));
234 popup_cmd_open_link (GtkAction
*action
,
237 EphyEmbedEvent
*event
;
238 const char *location
;
242 embed
= ephy_window_get_active_embed (window
);
243 g_return_if_fail (embed
!= NULL
);
245 event
= ephy_window_get_context_event (window
);
246 value
= ephy_embed_event_get_property (event
, "link");
247 location
= g_value_get_string (value
);
249 ephy_embed_load_url (embed
, location
);
253 popup_cmd_download_link (GtkAction
*action
,
256 save_property_url (action
, _("Download Link"), window
,
261 popup_cmd_download_link_as (GtkAction
*action
,
264 save_property_url (action
, _("Save Link As"), window
,
268 popup_cmd_save_image_as (GtkAction
*action
,
271 save_property_url (action
, _("Save Image As"),
272 window
, TRUE
, "image");
275 #define GNOME_BACKGROUND_PREFERENCES "gnome-background-properties"
278 background_download_completed (EphyEmbedPersist
*persist
)
283 user_time
= ephy_embed_persist_get_user_time (persist
);
285 bg
= ephy_embed_persist_get_dest (persist
);
287 g_object_unref (persist
);
289 /* open the "Background Properties" capplet */
290 if (!ephy_file_launch_desktop_file ("background.desktop", bg
, user_time
))
292 /* If the above try didn't work, then we try the Fedora name.
293 * This is a fix for #387206, but is actually a workaround for
294 * bugzilla.redhat.com #201867 */
295 ephy_file_launch_desktop_file ("gnome-background.desktop", bg
, user_time
);
300 popup_cmd_set_image_as_background (GtkAction
*action
,
303 EphyEmbedEvent
*event
;
304 const char *location
;
305 char *dest
, *base
, *base_converted
;
307 EphyEmbedPersist
*persist
;
310 event
= ephy_window_get_context_event (window
);
311 g_return_if_fail (event
!= NULL
);
313 embed
= ephy_window_get_active_embed (window
);
314 g_return_if_fail (embed
!= NULL
);
316 value
= ephy_embed_event_get_property (event
, "image");
317 location
= g_value_get_string (value
);
319 persist
= EPHY_EMBED_PERSIST
320 (ephy_embed_factory_new_object (EPHY_TYPE_EMBED_PERSIST
));
322 base
= g_path_get_basename (location
);
323 base_converted
= g_filename_from_utf8 (base
, -1, NULL
, NULL
, NULL
);
324 dest
= g_build_filename (ephy_dot_dir (), base_converted
, NULL
);
326 ephy_embed_persist_set_dest (persist
, dest
);
327 ephy_embed_persist_set_flags (persist
, EPHY_EMBED_PERSIST_NO_VIEW
|
328 EPHY_EMBED_PERSIST_FROM_CACHE
);
329 ephy_embed_persist_set_source (persist
, location
);
331 g_signal_connect (persist
, "completed",
332 G_CALLBACK (background_download_completed
),
335 ephy_embed_persist_save (persist
);
339 g_free (base_converted
);
343 popup_cmd_copy_image_location (GtkAction
*action
,
346 EphyEmbedEvent
*event
;
347 const char *location
;
350 event
= ephy_window_get_context_event (window
);
351 value
= ephy_embed_event_get_property (event
, "image");
352 location
= g_value_get_string (value
);
353 popup_cmd_copy_to_clipboard (window
, location
);
357 popup_cmd_open_frame (GtkAction
*action
,
363 embed
= ephy_window_get_active_embed (window
);
364 g_return_if_fail (embed
!= NULL
);
366 location
= ephy_embed_get_location (embed
, FALSE
);
368 ephy_embed_load_url (embed
, location
);
373 /* Opens an image URI using its associated handler. Or, if that
374 * doesn't work, fallback to open the URI in a new browser window.
377 image_open_uri (const char *remote_address
,
378 const char *local_address
,
383 success
= ephy_file_launch_handler (NULL
, local_address
, user_time
);
387 ephy_shell_new_tab (ephy_shell
, NULL
, NULL
, remote_address
,
388 EPHY_NEW_TAB_OPEN_PAGE
|
389 EPHY_NEW_TAB_IN_NEW_WINDOW
);
392 if (strcmp (remote_address
, local_address
) != 0)
395 ephy_file_delete_on_exit (local_address
);
397 gnome_vfs_unlink (local_address
);
402 save_source_completed_cb (EphyEmbedPersist
*persist
)
408 user_time
= ephy_embed_persist_get_user_time (persist
);
409 dest
= ephy_embed_persist_get_dest (persist
);
410 source
= ephy_embed_persist_get_source (persist
);
411 g_return_if_fail (dest
!= NULL
);
413 image_open_uri (source
, dest
, user_time
);
417 save_temp_source (const char *address
)
420 EphyEmbedPersist
*persist
;
421 const char *static_temp_dir
;
423 static_temp_dir
= ephy_file_tmp_dir ();
424 if (static_temp_dir
== NULL
)
429 base
= g_build_filename (static_temp_dir
, "viewimageXXXXXX", NULL
);
430 tmp
= ephy_file_tmp_filename (base
, "tmp"); /* FIXME */
437 persist
= EPHY_EMBED_PERSIST
438 (ephy_embed_factory_new_object (EPHY_TYPE_EMBED_PERSIST
));
440 ephy_embed_persist_set_source (persist
, address
);
441 ephy_embed_persist_set_flags (persist
, EPHY_EMBED_PERSIST_FROM_CACHE
|
442 EPHY_EMBED_PERSIST_NO_VIEW
);
443 ephy_embed_persist_set_dest (persist
, tmp
);
445 g_signal_connect (persist
, "completed",
446 G_CALLBACK (save_source_completed_cb
), NULL
);
448 ephy_embed_persist_save (persist
);
449 g_object_unref (persist
);
455 popup_cmd_open_image (GtkAction
*action
,
458 EphyEmbedEvent
*event
;
464 event
= ephy_window_get_context_event (window
);
465 g_return_if_fail (event
!= NULL
);
467 embed
= ephy_window_get_active_embed (window
);
468 g_return_if_fail (embed
!= NULL
);
470 value
= ephy_embed_event_get_property (event
, "image");
471 address
= g_value_get_string (value
);
473 scheme
= gnome_vfs_get_uri_scheme (address
);
474 if (scheme
== NULL
) return;
476 if (strcmp (scheme
, "file") == 0)
478 image_open_uri (address
, address
,
479 gtk_get_current_event_time ());
483 save_temp_source (address
);