summaryrefslogtreecommitdiff
path: root/src/context-menu-commands.c
blob: 5960aa7427c65deb4e1aff5b8533fac55b61cca0 (plain)
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
/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/*
 *  Copyright © 2000-2003 Marco Pesenti Gritti
 *  Copyright © 2017 Igalia S.L.
 *
 *  This file is part of Epiphany.
 *
 *  Epiphany 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 3 of the License, or
 *  (at your option) any later version.
 *
 *  Epiphany 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 Epiphany.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"
#include "context-menu-commands.h"

#include "ephy-downloads-manager.h"
#include "ephy-embed-container.h"
#include "ephy-embed-utils.h"
#include "ephy-file-chooser.h"
#include "ephy-file-helpers.h"
#include "ephy-flatpak-utils.h"
#include "ephy-prefs.h"
#include "ephy-settings.h"
#include "ephy-shell.h"
#include "ephy-web-view.h"

#include <glib/gi18n.h>
#include <gtk/gtk.h>
#include <string.h>
#include <webkit/webkit.h>

typedef enum {
  NEW_WINDOW,
  NEW_TAB
} LinkDestination;

static void
view_in_destination (EphyWindow      *window,
                     const char      *property_name,
                     LinkDestination  destination)
{
  WebKitHitTestResult *hit_test_result;
  g_autofree char *value = NULL;
  EphyEmbed *embed;
  EphyEmbed *new_embed;
  EphyWebView *new_view;
  WebKitWebViewSessionState *session_state;
  EphyWindow *dest_window = window;
  EphyNewTabFlags flags = 0;

  hit_test_result = ephy_window_get_context_event (window);
  g_assert (hit_test_result != NULL);

  embed = ephy_embed_container_get_active_child (EPHY_EMBED_CONTAINER (window));
  g_assert (embed != NULL);

  g_object_get (hit_test_result, property_name, &value, NULL);
  switch (destination) {
    case NEW_WINDOW:
      dest_window = ephy_window_new ();
      break;
    case NEW_TAB:
      flags |= EPHY_NEW_TAB_APPEND_AFTER;
      if (g_settings_get_boolean (EPHY_SETTINGS_WEB, EPHY_PREFS_WEB_SWITCH_TO_NEW_TAB))
        flags |= EPHY_NEW_TAB_JUMP;
      break;
    default:
      g_assert_not_reached ();
  }

  new_embed = ephy_shell_new_tab (ephy_shell_get_default (),
                                  dest_window, embed, flags);

  new_view = ephy_embed_get_web_view (new_embed);
  session_state = webkit_web_view_get_session_state (WEBKIT_WEB_VIEW (ephy_embed_get_web_view (embed)));
  webkit_web_view_restore_session_state (WEBKIT_WEB_VIEW (new_view), session_state);
  webkit_web_view_session_state_unref (session_state);
  ephy_web_view_load_url (new_view, value);
}

void
context_cmd_link_in_new_window (GSimpleAction *action,
                                GVariant      *parameter,
                                gpointer       user_data)
{
  view_in_destination (EPHY_WINDOW (user_data), "link-uri", NEW_WINDOW);
}

void
context_cmd_media_in_new_window (GSimpleAction *action,
                                 GVariant      *parameter,
                                 gpointer       user_data)
{
  view_in_destination (EPHY_WINDOW (user_data), "media-uri", NEW_WINDOW);
}

static void
context_cmd_copy_to_clipboard (EphyWindow *window,
                               const char *text)
{
  GdkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (window));

  gdk_clipboard_set_text (clipboard, text);
}

void
context_cmd_copy_link_address (GSimpleAction *action,
                               GVariant      *parameter,
                               gpointer       user_data)
{
  WebKitHitTestResult *hit_test_result;
  guint context;
  const char *address;

  hit_test_result = ephy_window_get_context_event (EPHY_WINDOW (user_data));
  g_assert (hit_test_result != NULL);

  context = webkit_hit_test_result_get_context (hit_test_result);

  if (context & WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK) {
    address = webkit_hit_test_result_get_link_uri (hit_test_result);

    if (g_str_has_prefix (address, "mailto:"))
      address = address + 7;

    context_cmd_copy_to_clipboard (EPHY_WINDOW (user_data), address);
  }
}

static gboolean
cancel_download_idle_cb (EphyDownload *download)
{
  ephy_download_cancel (download);

  return FALSE;
}

typedef struct {
  char *title;
  EphyWindow *window;
  EphyDownload *download;
  GMainLoop *nested_loop;
} SavePropertyURLData;

static void
filename_confirmed_cb (GtkFileDialog       *dialog,
                       GAsyncResult        *result,
                       SavePropertyURLData *data)
{
  g_autoptr (GFile) file = NULL;

  file = gtk_file_dialog_save_finish (dialog, result, NULL);

  if (file) {
    g_autoptr (GFile) current_folder = NULL;
    g_autofree char *uri = NULL;
    g_autofree char *current_folder_path = NULL;
    WebKitDownload *webkit_download;

    uri = g_file_get_uri (file);
    ephy_download_set_destination_uri (data->download, uri);

    webkit_download = ephy_download_get_webkit_download (data->download);
    webkit_download_set_allow_overwrite (webkit_download, TRUE);

    ephy_downloads_manager_add_download (ephy_embed_shell_get_downloads_manager (ephy_embed_shell_get_default ()),
                                         data->download);

    current_folder = gtk_file_dialog_get_current_folder (dialog);
    current_folder_path = g_file_get_path (current_folder);
    g_settings_set_string (EPHY_SETTINGS_WEB,
                           EPHY_PREFS_WEB_LAST_DOWNLOAD_DIRECTORY,
                           current_folder_path);
  } else {
    g_idle_add_full (G_PRIORITY_DEFAULT,
                     (GSourceFunc)cancel_download_idle_cb,
                     g_object_ref (data->download),
                     g_object_unref);
  }

  g_main_loop_quit (data->nested_loop);

  g_free (data->title);
  g_object_unref (data->window);
  g_object_unref (data->download);
  g_main_loop_unref (data->nested_loop);
  g_free (data);
}

static void
filename_suggested_cb (EphyDownload        *download,
                       const char          *suggested_filename,
                       SavePropertyURLData *data)
{
  GtkFileDialog *dialog;
  const char *last_directory_path;
  g_autofree char *sanitized_filename = NULL;

  dialog = gtk_file_dialog_new ();
  ephy_file_dialog_add_shortcuts (dialog);

  last_directory_path = g_settings_get_string (EPHY_SETTINGS_WEB, EPHY_PREFS_WEB_LAST_DOWNLOAD_DIRECTORY);

  if (last_directory_path && last_directory_path[0]) {
    g_autoptr (GFile) last_directory = NULL;

    last_directory = g_file_new_for_path (last_directory_path);
    gtk_file_dialog_set_current_folder (dialog, last_directory);
  }

  sanitized_filename = ephy_sanitize_filename (g_strdup (suggested_filename));

  gtk_file_dialog_save (dialog,
                        GTK_WINDOW (data->window),
                        NULL,
                        sanitized_filename,
                        NULL,
                        (GAsyncReadyCallback)filename_confirmed_cb,
                        data);

  /* We have to set a download destination before this signal handler completes,
   * so we'll spin the default main context until the dialog is finished.
   * https://bugs.webkit.org/show_bug.cgi?id=238748
   */
  g_main_loop_run (data->nested_loop);
}

static void
save_property_url (const char *title,
                   EphyWindow *window,
                   const char *property)
{
  WebKitHitTestResult *hit_test_result;
  g_autofree char *location = NULL;
  EphyDownload *download;
  SavePropertyURLData *data;

  hit_test_result = ephy_window_get_context_event (window);
  g_assert (hit_test_result != NULL);

  g_object_get (hit_test_result, property, &location, NULL);
  download = ephy_download_new_for_uri (location);
  data = g_new (SavePropertyURLData, 1);
  data->title = g_strdup (title);
  data->window = g_object_ref (window);
  data->download = download;
  data->nested_loop = g_main_loop_new (NULL, FALSE);
  g_signal_connect (download, "filename-suggested",
                    G_CALLBACK (filename_suggested_cb),
                    data);
}

void
context_cmd_download_link_as (GSimpleAction *action,
                              GVariant      *parameter,
                              gpointer       user_data)
{
  save_property_url (_("Save Link As"), EPHY_WINDOW (user_data), "link-uri");
}

void
context_cmd_save_image_as (GSimpleAction *action,
                           GVariant      *parameter,
                           gpointer       user_data)
{
  save_property_url (_("Save Image As"), EPHY_WINDOW (user_data), "image-uri");
}

void
context_cmd_save_media_as (GSimpleAction *action,
                           GVariant      *parameter,
                           gpointer       user_data)
{
  save_property_url (_("Save Media As"), EPHY_WINDOW (user_data), "media-uri");
}

static void
background_download_completed (EphyDownload *download,
                               GtkWidget    *window)
{
  const char *uri;
  GSettings *settings;

  uri = ephy_download_get_destination_uri (download);
  settings = ephy_settings_get ("org.gnome.desktop.background");
  g_settings_set_string (settings, "picture-uri", uri);
}

void
context_cmd_set_image_as_background (GSimpleAction *action,
                                     GVariant      *parameter,
                                     gpointer       user_data)
{
  WebKitHitTestResult *hit_test_result;
  const char *location;
  char *dest_uri, *dest, *base, *base_converted;
  EphyDownload *download;

  /* FIXME: Use wallpaper portal */
  if (ephy_is_running_inside_sandbox ())
    return;

  hit_test_result = ephy_window_get_context_event (EPHY_WINDOW (user_data));
  g_assert (hit_test_result != NULL);

  location = webkit_hit_test_result_get_image_uri (hit_test_result);

  download = ephy_download_new_for_uri (location);

  base = g_path_get_basename (location);
  base_converted = g_filename_from_utf8 (base, -1, NULL, NULL, NULL);
  dest = g_build_filename (g_get_user_special_dir (G_USER_DIRECTORY_PICTURES), base_converted, NULL);
  dest_uri = g_filename_to_uri (dest, NULL, NULL);

  ephy_download_set_destination_uri (download, dest_uri);
  ephy_downloads_manager_add_download (ephy_embed_shell_get_downloads_manager (ephy_embed_shell_get_default ()),
                                       download);
  g_object_unref (download);

  g_signal_connect (download, "completed",
                    G_CALLBACK (background_download_completed), user_data);

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

static void
context_cmd_copy_location (EphyWindow *window,
                           const char *property_name)
{
  WebKitHitTestResult *hit_test_result;
  g_autofree char *location = NULL;

  hit_test_result = ephy_window_get_context_event (window);
  g_object_get (hit_test_result, property_name, &location, NULL);
  context_cmd_copy_to_clipboard (window, location);
}

void
context_cmd_copy_image_location (GSimpleAction *action,
                                 GVariant      *parameter,
                                 gpointer       user_data)
{
  context_cmd_copy_location (EPHY_WINDOW (user_data), "image-uri");
}

void
context_cmd_copy_media_location (GSimpleAction *action,
                                 GVariant      *parameter,
                                 gpointer       user_data)
{
  context_cmd_copy_location (EPHY_WINDOW (user_data), "media-uri");
}

void
context_cmd_link_in_new_tab (GSimpleAction *action,
                             GVariant      *parameter,
                             gpointer       user_data)
{
  view_in_destination (EPHY_WINDOW (user_data), "link-uri", NEW_TAB);
}

void
context_cmd_view_image_in_new_tab (GSimpleAction *action,
                                   GVariant      *parameter,
                                   gpointer       user_data)
{
  view_in_destination (EPHY_WINDOW (user_data), "image-uri", NEW_TAB);
}

void
context_cmd_media_in_new_tab (GSimpleAction *action,
                              GVariant      *parameter,
                              gpointer       user_data)
{
  view_in_destination (EPHY_WINDOW (user_data), "media-uri", NEW_TAB);
}

void
context_cmd_link_in_incognito_window (GSimpleAction *action,
                                      GVariant      *parameter,
                                      gpointer       user_data)
{
  WebKitHitTestResult *hit_test_result;
  const char *link_uri;

  hit_test_result = ephy_window_get_context_event (EPHY_WINDOW (user_data));
  g_assert (hit_test_result != NULL);

  link_uri = webkit_hit_test_result_get_link_uri (hit_test_result);

  ephy_open_incognito_window (link_uri);
}

void
context_cmd_search_selection (GSimpleAction *action,
                              GVariant      *parameter,
                              gpointer       user_data)
{
  EphyEmbed *embed, *new_embed;
  const char *search_term;
  char *search_url;

  embed = ephy_embed_container_get_active_child (EPHY_EMBED_CONTAINER (user_data));
  g_assert (EPHY_IS_EMBED (embed));

  search_term = g_variant_get_string (parameter, NULL);
  search_url = ephy_embed_utils_autosearch_address (search_term);
  new_embed = ephy_shell_new_tab (ephy_shell_get_default (),
                                  EPHY_WINDOW (user_data), embed, EPHY_NEW_TAB_APPEND_AFTER | EPHY_NEW_TAB_JUMP);
  ephy_web_view_load_url (ephy_embed_get_web_view (new_embed), search_url);
  g_free (search_url);
}

void
context_cmd_open_selection (GSimpleAction *action,
                            GVariant      *parameter,
                            gpointer       user_data)
{
  EphyEmbed *embed;
  const char *open_term;

  embed = ephy_embed_container_get_active_child (EPHY_EMBED_CONTAINER (user_data));
  g_assert (EPHY_IS_EMBED (embed));

  open_term = g_variant_get_string (parameter, NULL);
  ephy_web_view_load_url (ephy_embed_get_web_view (embed), open_term);
}

void
context_cmd_open_selection_in_new_tab (GSimpleAction *action,
                                       GVariant      *parameter,
                                       gpointer       user_data)
{
  EphyEmbed *embed, *new_embed;
  const char *open_term;

  embed = ephy_embed_container_get_active_child (EPHY_EMBED_CONTAINER (user_data));
  g_assert (EPHY_IS_EMBED (embed));

  open_term = g_variant_get_string (parameter, NULL);
  new_embed = ephy_shell_new_tab (ephy_shell_get_default (),
                                  EPHY_WINDOW (user_data), embed, EPHY_NEW_TAB_APPEND_AFTER | EPHY_NEW_TAB_JUMP);
  ephy_web_view_load_url (ephy_embed_get_web_view (new_embed), open_term);
}

void
context_cmd_open_selection_in_new_window (GSimpleAction *action,
                                          GVariant      *parameter,
                                          gpointer       user_data)
{
  EphyEmbed *embed, *new_embed;
  const char *open_term;

  embed = ephy_embed_container_get_active_child (EPHY_EMBED_CONTAINER (user_data));
  g_assert (EPHY_IS_EMBED (embed));

  open_term = g_variant_get_string (parameter, NULL);
  new_embed = ephy_shell_new_tab (ephy_shell_get_default (),
                                  ephy_window_new (), embed, 0);
  ephy_web_view_load_url (ephy_embed_get_web_view (new_embed), open_term);
}

void
context_cmd_open_selection_in_incognito_window (GSimpleAction *action,
                                                GVariant      *parameter,
                                                gpointer       user_data)
{
  const char *open_term;

  open_term = g_variant_get_string (parameter, NULL);
  ephy_open_incognito_window (open_term);
}