aboutsummaryrefslogtreecommitdiffstats
path: root/src/popup-commands.c
blob: 7a95c7611a4469102898ea3cfb71add75785b768 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/*
 *  Copyright (C) 2000-2003 Marco Pesenti Gritti
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 *  $Id$
 */

#include "config.h"

#include "popup-commands.h"
#include "ephy-shell.h"
#include "ephy-new-bookmark.h"
#include "ephy-embed-factory.h"
#include "ephy-embed-persist.h"
#include "ephy-prefs.h"
#include "eel-gconf-extensions.h"
#include "ephy-file-helpers.h"

#include <string.h>
#include <glib/gi18n.h>
#include <gtk/gtkclipboard.h>
#include <libgnomevfs/gnome-vfs-utils.h>
#include <libgnomevfs/gnome-vfs-file-info.h>
#include <libgnomevfs/gnome-vfs-ops.h>
#include <libgnomevfs/gnome-vfs-mime-handlers.h>

static EphyEmbedEvent *
get_event_info (EphyWindow *window)
{
    EphyEmbedEvent *info;

    info = EPHY_EMBED_EVENT (g_object_get_data
        (G_OBJECT (window), "context_event"));
    g_return_val_if_fail (info != NULL, NULL);

    return info;
}

void
popup_cmd_link_in_new_window (GtkAction *action,
                      EphyWindow *window)
{
    EphyEmbedEvent *info;
    EphyTab *tab;
    const GValue *value;

    tab = ephy_window_get_active_tab (window);

    info = get_event_info (window);

    ephy_embed_event_get_property (info, "link", &value);

    ephy_shell_new_tab (ephy_shell, NULL, tab,
                g_value_get_string (value),
                EPHY_NEW_TAB_OPEN_PAGE |
                EPHY_NEW_TAB_IN_NEW_WINDOW);
}

void
popup_cmd_link_in_new_tab (GtkAction *action,
                   EphyWindow *window)
{
    EphyEmbedEvent *info;
    EphyTab *tab;
    const GValue *value;

    tab = ephy_window_get_active_tab (window);

    info = get_event_info (window);

    ephy_embed_event_get_property (info, "link", &value);

    ephy_shell_new_tab (ephy_shell, window, tab,
                g_value_get_string (value),
                EPHY_NEW_TAB_OPEN_PAGE |
                EPHY_NEW_TAB_IN_EXISTING_WINDOW);
}

void
popup_cmd_bookmark_link (GtkAction *action,
             EphyWindow *window)
{
    GtkWidget *new_bookmark;
    EphyBookmarks *bookmarks;
    EphyEmbedEvent *info;
    const GValue *link_title;
    const GValue *link_rel;
    const GValue *link;
    const GValue *link_is_smart;
    const GValue *linktext;
    const char *title;
    const char *location;
    const char *rel;
    gboolean is_smart;

    info = get_event_info (window);

    ephy_embed_event_get_property (info, "link_is_smart", &link_is_smart);
    ephy_embed_event_get_property (info, "link", &link);
    ephy_embed_event_get_property (info, "link_title", &link_title);
    ephy_embed_event_get_property (info, "link_rel", &link_rel);
    ephy_embed_event_get_property (info, "linktext", &linktext);

    location = g_value_get_string (link);
    g_return_if_fail (location);

    rel = g_value_get_string (link_rel);
    is_smart = g_value_get_int (link_is_smart);

    title = g_value_get_string (link_title);

    if (title == NULL || title[0] == '\0')
    {
        title = g_value_get_string (linktext);
    }

    if (title == NULL || title[0] == '\0')
    {
        title = location;
    }

    bookmarks = ephy_shell_get_bookmarks (ephy_shell);
    if (ephy_new_bookmark_is_unique (bookmarks, GTK_WINDOW (window),
                     location))
    {
        new_bookmark = ephy_new_bookmark_new
            (bookmarks, GTK_WINDOW (window),
             is_smart ? rel : location);
        ephy_new_bookmark_set_title
            (EPHY_NEW_BOOKMARK (new_bookmark), title);
        gtk_widget_show (new_bookmark);
    }
}

static void
popup_cmd_copy_to_clipboard (EphyWindow *window, const char *text)
{
    gtk_clipboard_set_text (gtk_clipboard_get (GDK_NONE),
                text, -1);
    gtk_clipboard_set_text (gtk_clipboard_get (GDK_SELECTION_PRIMARY),
                text, -1);
}

void
popup_cmd_copy_link_address (GtkAction *action,
                 EphyWindow *window)
{
    EphyEmbedEvent *event;
    EphyEmbedEventContext context;
    const char *address;
    const GValue *value;

    event = get_event_info (window);
    g_return_if_fail (EPHY_IS_EMBED_EVENT (event));

    context = ephy_embed_event_get_context (event);

    if (context & EMBED_CONTEXT_EMAIL_LINK)
    {
        ephy_embed_event_get_property (event, "email", &value);
        address = g_value_get_string (value);
        popup_cmd_copy_to_clipboard (window, address);
    }
    else if (context & EMBED_CONTEXT_LINK)
    {
        ephy_embed_event_get_property (event, "link", &value);
        address = g_value_get_string (value);
        popup_cmd_copy_to_clipboard (window, address);
    }
}

static void
save_property_url (GtkAction *action,
           const char *title,
           EphyWindow *window,
           gboolean ask_dest,
           const char *property)
{
    EphyEmbedEvent *info;
    const char *location;
    const GValue *value;
    EphyEmbedPersist *persist;
    EphyEmbed *embed;

    embed = ephy_window_get_active_embed (window);
    g_return_if_fail (embed != NULL);

    info = get_event_info (window);
    ephy_embed_event_get_property (info, property, &value);
    location = g_value_get_string (value);

    persist = EPHY_EMBED_PERSIST
        (ephy_embed_factory_new_object (EPHY_TYPE_EMBED_PERSIST));

    ephy_embed_persist_set_embed (persist, embed);
    ephy_embed_persist_set_fc_title (persist, title);
    ephy_embed_persist_set_fc_parent (persist, GTK_WINDOW (window));
    ephy_embed_persist_set_flags
        (persist, ask_dest ? EMBED_PERSIST_ASK_DESTINATION : 0);
    ephy_embed_persist_set_persist_key
        (persist, CONF_STATE_SAVE_DIR);
    ephy_embed_persist_set_source (persist, location);

    ephy_embed_persist_save (persist);

    g_object_unref (G_OBJECT(persist));
}

void
popup_cmd_open_link (GtkAction *action,
             EphyWindow *window)
{
    EphyEmbedEvent *info;
    const char *location;
    const GValue *value;
    EphyEmbed *embed;

    embed = ephy_window_get_active_embed (window);
    g_return_if_fail (embed != NULL);

    info = get_event_info (window);
    ephy_embed_event_get_property (info, "link", &value);
    location = g_value_get_string (value);

    ephy_embed_load_url (embed, location);
}

void
popup_cmd_download_link (GtkAction *action,
             EphyWindow *window)
{
    save_property_url (action, _("Download Link"), window, 
               FALSE, "link");
}

void
popup_cmd_download_link_as (GtkAction *action,
                EphyWindow *window)
{
    save_property_url (action, _("Save Link As"), window, 
               TRUE, "link");
}
void
popup_cmd_save_image_as (GtkAction *action,
             EphyWindow *window)
{
    save_property_url (action, _("Save Image As"),
               window, TRUE, "image");
}

#define CONF_DESKTOP_BG_PICTURE "/desktop/gnome/background/picture_filename"
#define CONF_DESKTOP_BG_TYPE "/desktop/gnome/background/picture_options"

static void
background_download_completed (EphyEmbedPersist *persist,
                   gpointer data)
{
    const char *bg;
    char *type;

    bg = ephy_embed_persist_get_dest (persist);
    eel_gconf_set_string (CONF_DESKTOP_BG_PICTURE, bg);

    type = eel_gconf_get_string (CONF_DESKTOP_BG_TYPE);
    if (type == NULL || strcmp (type, "none") == 0)
    {
        eel_gconf_set_string (CONF_DESKTOP_BG_TYPE, "wallpaper");
    }
    g_free (type);

    g_object_unref (persist);
}

void
popup_cmd_set_image_as_background (GtkAction *action,
                   EphyWindow *window)
{
    EphyEmbedEvent *info;
    const char *location;
    char *dest, *base, *base_converted;
    const GValue *value;
    EphyEmbedPersist *persist;
    EphyEmbed *embed;

    embed = ephy_window_get_active_embed (window);
    g_return_if_fail (embed != NULL);

    info = get_event_info (window);
    ephy_embed_event_get_property (info, "image", &value);
    location = g_value_get_string (value);

    persist = EPHY_EMBED_PERSIST
        (ephy_embed_factory_new_object (EPHY_TYPE_EMBED_PERSIST));

    base = g_path_get_basename (location);
    base_converted = g_filename_from_utf8 (base, -1, NULL, NULL, NULL);
    dest = g_build_filename (ephy_dot_dir (), base_converted, NULL);

    ephy_embed_persist_set_embed (persist, embed);
    ephy_embed_persist_set_dest (persist, dest);
    ephy_embed_persist_set_flags (persist, EMBED_PERSIST_NO_VIEW);
    ephy_embed_persist_set_source (persist, location);

    g_signal_connect (persist, "completed",
              G_CALLBACK (background_download_completed),
              NULL);

    ephy_embed_persist_save (persist);

    g_free (dest);
    g_free (base);
    g_free (base_converted);
}

void
popup_cmd_copy_image_location (GtkAction *action,
                   EphyWindow *window)
{
    EphyEmbedEvent *info;
    const char *location;
    const GValue *value;

    info = get_event_info (window);
    ephy_embed_event_get_property (info, "image", &value);
    location = g_value_get_string (value);
    popup_cmd_copy_to_clipboard (window, location);
}

void
popup_cmd_save_background_as (GtkAction *action,
                  EphyWindow *window)
{
    save_property_url (action, _("Save Background As"),
               window, TRUE, "background_image");
}

void
popup_cmd_open_frame (GtkAction *action,
              EphyWindow *window)
{
    char *location;
    EphyEmbed *embed;

    embed = ephy_window_get_active_embed (window);
    g_return_if_fail (embed != NULL);

    location = ephy_embed_get_location (embed, FALSE);

    ephy_embed_load_url (embed, location);

    g_free (location);
}

static void
image_open_uri (const char *address,
        gboolean delete)
{
    GList *uris = NULL;
    char *canonical;
    GnomeVFSMimeApplication *app = NULL;
    GnomeVFSFileInfo *info;

    canonical = gnome_vfs_make_uri_canonical (address);

    info = gnome_vfs_file_info_new ();
    if (gnome_vfs_get_file_info (canonical, info,
                     GNOME_VFS_FILE_INFO_GET_MIME_TYPE |
                     GNOME_VFS_FILE_INFO_FORCE_SLOW_MIME_TYPE) == GNOME_VFS_OK &&
        (info->valid_fields & GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE) &&
        info->mime_type != NULL &&
        info->mime_type[0] != '\0' &&
        ephy_file_check_mime (info->mime_type) == EPHY_MIME_PERMISSION_SAFE)
    {
        app = gnome_vfs_mime_get_default_application (info->mime_type);
    }

    if (app != NULL)
    {
        /* FIXME rename tmp file to right extension ? */
        uris = g_list_append (uris, canonical);
        gnome_vfs_mime_application_launch (app, uris);
        gnome_vfs_mime_application_free (app);
        g_list_free (uris);
        if (delete)
        {
            ephy_file_delete_on_exit (address);
        }
    }
    else
    {
        /* FIXME We should really warn the user here */

        g_warning ("Cannot find an application to open %s with.", address);
        if (delete)
        {
            gnome_vfs_unlink (address);
        }
    }

    g_free (canonical);
    gnome_vfs_file_info_unref (info);
}

static void
save_source_completed_cb (EphyEmbedPersist *persist)
{
    const char *dest;

    dest = ephy_embed_persist_get_dest (persist);
    g_return_if_fail (dest != NULL);

    image_open_uri (dest, TRUE);
}

static void
save_temp_source (const char *address)
{
    char *tmp, *base;
    EphyEmbedPersist *persist;
    const char *static_temp_dir;

    static_temp_dir = ephy_file_tmp_dir ();
    if (static_temp_dir == NULL)
    {
        return;
    }

    base = g_build_filename (static_temp_dir, "viewimageXXXXXX", NULL);
    tmp = ephy_file_tmp_filename (base, "tmp"); /* FIXME */
    g_free (base);
    if (tmp == NULL)
    {
        return;
    }

    persist = EPHY_EMBED_PERSIST
        (ephy_embed_factory_new_object (EPHY_TYPE_EMBED_PERSIST));

    ephy_embed_persist_set_source (persist, address);
    ephy_embed_persist_set_flags (persist, EMBED_PERSIST_NO_VIEW);
    ephy_embed_persist_set_dest (persist, tmp);

    g_signal_connect (persist, "completed",
              G_CALLBACK (save_source_completed_cb), NULL);

    ephy_embed_persist_save (persist);
    g_object_unref (persist);

    g_free (tmp);
}

void
popup_cmd_open_image (GtkAction *action,
              EphyWindow *window)
{
    EphyEmbedEvent *info;
    const char *address;
    char *scheme;
    const GValue *value;
    EphyEmbed *embed;

    embed = ephy_window_get_active_embed (window);
    g_return_if_fail (embed != NULL);

    info = get_event_info (window);
    ephy_embed_event_get_property (info, "image", &value);
    address = g_value_get_string (value);

    scheme = gnome_vfs_get_uri_scheme (address);

    if (strcmp (scheme, "file") == 0)
    {
        image_open_uri (address, FALSE);
    }
    else
    {
        save_temp_source (address);
    }

    g_free (scheme);
}