Initial import of ephy (rev# 7126) from svn
[ephy-soc.git] / src / popup-commands.c
bloba9d9385e64cfdfca7fd1b41482bc7c3f22b4d9e4
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
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)
8 * any later version.
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 $
22 #include "config.h"
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"
33 #include <string.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>
42 void
43 popup_cmd_link_in_new_window (GtkAction *action,
44 EphyWindow *window)
46 EphyEmbedEvent *event;
47 EphyTab *tab;
48 const GValue *value;
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);
63 void
64 popup_cmd_link_in_new_tab (GtkAction *action,
65 EphyWindow *window)
67 EphyEmbedEvent *event;
68 EphyTab *tab;
69 const GValue *value;
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);
84 void
85 popup_cmd_bookmark_link (GtkAction *action,
86 EphyWindow *window)
88 EphyEmbedEvent *event;
89 const GValue *link_title;
90 const GValue *link_rel;
91 const GValue *link;
92 const GValue *link_is_smart;
93 const GValue *linktext;
94 const char *title;
95 const char *location;
96 const char *rel;
97 gboolean is_smart;
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')
123 title = location;
126 if (is_smart)
128 location = rel;
131 ephy_bookmarks_ui_add_bookmark (GTK_WINDOW (window), location, title);
134 static void
135 popup_cmd_copy_to_clipboard (EphyWindow *window, const char *text)
137 gtk_clipboard_set_text (gtk_clipboard_get (GDK_NONE),
138 text, -1);
139 gtk_clipboard_set_text (gtk_clipboard_get (GDK_SELECTION_PRIMARY),
140 text, -1);
143 void
144 popup_cmd_copy_link_address (GtkAction *action,
145 EphyWindow *window)
147 EphyEmbedEvent *event;
148 EphyEmbedEventContext context;
149 const char *address;
150 const GValue *value;
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);
171 static void
172 save_property_url_completed_cb (EphyEmbedPersist *persist)
174 if (!(ephy_embed_persist_get_flags (persist) &
175 EPHY_EMBED_PERSIST_ASK_DESTINATION))
177 const char *dest;
178 guint32 user_time;
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);
191 static void
192 save_property_url (GtkAction *action,
193 const char *title,
194 EphyWindow *window,
195 gboolean ask_dest,
196 const char *property)
198 EphyEmbedEvent *event;
199 const char *location;
200 const GValue *value;
201 EphyEmbedPersist *persist;
202 EphyEmbed *embed;
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));
233 void
234 popup_cmd_open_link (GtkAction *action,
235 EphyWindow *window)
237 EphyEmbedEvent *event;
238 const char *location;
239 const GValue *value;
240 EphyEmbed *embed;
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);
252 void
253 popup_cmd_download_link (GtkAction *action,
254 EphyWindow *window)
256 save_property_url (action, _("Download Link"), window,
257 FALSE, "link");
260 void
261 popup_cmd_download_link_as (GtkAction *action,
262 EphyWindow *window)
264 save_property_url (action, _("Save Link As"), window,
265 TRUE, "link");
267 void
268 popup_cmd_save_image_as (GtkAction *action,
269 EphyWindow *window)
271 save_property_url (action, _("Save Image As"),
272 window, TRUE, "image");
275 #define GNOME_BACKGROUND_PREFERENCES "gnome-background-properties"
277 static void
278 background_download_completed (EphyEmbedPersist *persist)
280 const char *bg;
281 guint32 user_time;
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);
299 void
300 popup_cmd_set_image_as_background (GtkAction *action,
301 EphyWindow *window)
303 EphyEmbedEvent *event;
304 const char *location;
305 char *dest, *base, *base_converted;
306 const GValue *value;
307 EphyEmbedPersist *persist;
308 EphyEmbed *embed;
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),
333 NULL);
335 ephy_embed_persist_save (persist);
337 g_free (dest);
338 g_free (base);
339 g_free (base_converted);
342 void
343 popup_cmd_copy_image_location (GtkAction *action,
344 EphyWindow *window)
346 EphyEmbedEvent *event;
347 const char *location;
348 const GValue *value;
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);
356 void
357 popup_cmd_open_frame (GtkAction *action,
358 EphyWindow *window)
360 char *location;
361 EphyEmbed *embed;
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);
370 g_free (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.
376 static void
377 image_open_uri (const char *remote_address,
378 const char *local_address,
379 guint32 user_time)
381 gboolean success;
383 success = ephy_file_launch_handler (NULL, local_address, user_time);
385 if (!success)
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)
394 if (success)
395 ephy_file_delete_on_exit (local_address);
396 else
397 gnome_vfs_unlink (local_address);
401 static void
402 save_source_completed_cb (EphyEmbedPersist *persist)
404 const char *dest;
405 const char *source;
406 guint32 user_time;
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);
416 static void
417 save_temp_source (const char *address)
419 char *tmp, *base;
420 EphyEmbedPersist *persist;
421 const char *static_temp_dir;
423 static_temp_dir = ephy_file_tmp_dir ();
424 if (static_temp_dir == NULL)
426 return;
429 base = g_build_filename (static_temp_dir, "viewimageXXXXXX", NULL);
430 tmp = ephy_file_tmp_filename (base, "tmp"); /* FIXME */
431 g_free (base);
432 if (tmp == NULL)
434 return;
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);
451 g_free (tmp);
454 void
455 popup_cmd_open_image (GtkAction *action,
456 EphyWindow *window)
458 EphyEmbedEvent *event;
459 const char *address;
460 char *scheme;
461 const GValue *value;
462 EphyEmbed *embed;
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 ());
481 else
483 save_temp_source (address);
486 g_free (scheme);