1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /*
3 * Copyright © 2002 Jorn Baayen
4 * Copyright © 2003, 2004 Marco Pesenti Gritti
5 * Copyright © 2004, 2005, 2006 Christian Persch
6 * Copyright © 2012 Igalia S.L.
7 *
8 * This file is part of Epiphany.
9 *
10 * Epiphany is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * Epiphany is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with Epiphany. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include "config.h"
25 #include "ephy-file-helpers.h"
26
27 #include "ephy-debug.h"
28 #include "ephy-flatpak-utils.h"
29 #include "ephy-prefs.h"
30 #include "ephy-profile-utils.h"
31 #include "ephy-settings.h"
32 #include "ephy-string.h"
33 #include "ephy-web-app-utils.h"
34
35 #include <errno.h>
36 #include <gdk/gdk.h>
37 #include <gio/gdesktopappinfo.h>
38 #include <gio/gio.h>
39 #include <glib.h>
40 #include <glib/gi18n.h>
41 #include <glib/gstdio.h>
42 #include <gtk/gtk.h>
43 #include <libxml/xmlreader.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <sys/stat.h>
47 #include <unistd.h>
48
49 /**
50 * SECTION:ephy-file-helpers
51 * @short_description: miscellaneous file related utility functions
52 *
53 * File related functions, including functions to launch, browse or move files
54 * atomically.
55 */
56
57 #define DELAY_MAX_TICKS 64
58 #define INITIAL_TICKS 2
59
60 typedef enum {
61 EPHY_PROFILE_DIR_UNKNOWN,
62 EPHY_PROFILE_DIR_DEFAULT,
63 EPHY_PROFILE_DIR_WEB_APP,
64 EPHY_PROFILE_DIR_TEST
65 } EphyProfileDirType;
66
67 static GHashTable *files;
68 static GHashTable *mime_table;
69
70 static gboolean keep_directory;
71 static char *profile_dir_global;
72 static char *cache_dir;
73 static char *config_dir;
74 static char *tmp_dir;
75 static EphyProfileDirType profile_dir_type;
76
77 GQuark ephy_file_helpers_error_quark;
78
79 /**
80 * ephy_file_tmp_dir:
81 *
82 * Returns the name of the temp dir for the running Epiphany instance.
83 *
84 * Returns: the name of the temp dir, this string belongs to Epiphany.
85 **/
86 const char *
ephy_file_tmp_dir(void)87 ephy_file_tmp_dir (void)
88 {
89 if (tmp_dir == NULL) {
90 char *partial_name;
91 char *full_name;
92
93 partial_name = g_strconcat ("epiphany-", g_get_user_name (),
94 "-XXXXXX", NULL);
95 full_name = g_build_filename (g_get_tmp_dir (), partial_name,
96 NULL);
97 tmp_dir = mkdtemp (full_name);
98 g_free (partial_name);
99
100 if (tmp_dir == NULL) {
101 g_free (full_name);
102 }
103 }
104
105 return tmp_dir;
106 }
107
108 static char *
ephy_file_download_dir(void)109 ephy_file_download_dir (void)
110 {
111 const char *xdg_download_dir;
112
113 xdg_download_dir = g_get_user_special_dir (G_USER_DIRECTORY_DOWNLOAD);
114 if (xdg_download_dir != NULL)
115 return g_strdup (xdg_download_dir);
116
117 /* If we don't have XDG user dirs info, return an educated guess. */
118 return g_build_filename (g_get_home_dir (), _("Downloads"), NULL);
119 }
120
121 /**
122 * ephy_file_get_downloads_dir:
123 *
124 * Returns a proper downloads destination by checking the
125 * EPHY_PREFS_STATE_DOWNLOAD_DIR GSettings key and following this logic:
126 *
127 * - Under flatpak, always use the XDG downloads directory
128 *
129 * - An absolute path: considered user-set, use this value directly.
130 *
131 * - "Desktop" keyword in GSettings: the directory returned by
132 * ephy_file_desktop_dir().
133 *
134 * - "Downloads" keyword in GSettings, or any other value: the XDG
135 * downloads directory, or ~/Downloads.
136 *
137 * Returns: a newly-allocated string containing the path to the downloads dir.
138 **/
139 char *
ephy_file_get_downloads_dir(void)140 ephy_file_get_downloads_dir (void)
141 {
142 g_autofree char *download_dir = g_settings_get_string (EPHY_SETTINGS_STATE,
143 EPHY_PREFS_STATE_DOWNLOAD_DIR);
144
145 if (ephy_is_running_inside_flatpak ())
146 return ephy_file_download_dir ();
147
148 if (g_strcmp0 (download_dir, "Desktop") == 0)
149 return ephy_file_desktop_dir ();
150
151 if (g_strcmp0 (download_dir, "Downloads") == 0 ||
152 !g_path_is_absolute (download_dir))
153 return ephy_file_download_dir ();
154
155 return g_steal_pointer (&download_dir);
156 }
157
158 /**
159 * ephy_file_desktop_dir:
160 *
161 * Gets the XDG desktop dir path or a default homedir/Desktop alternative.
162 *
163 * Returns: a newly-allocated string containing the desktop dir path.
164 **/
165 char *
ephy_file_desktop_dir(void)166 ephy_file_desktop_dir (void)
167 {
168 const char *xdg_desktop_dir;
169
170 xdg_desktop_dir = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
171 if (xdg_desktop_dir != NULL)
172 return g_strdup (xdg_desktop_dir);
173
174 /* If we don't have XDG user dirs info, return an educated guess. */
175 return g_build_filename (g_get_home_dir (), _("Desktop"), NULL);
176 }
177
178 /**
179 * ephy_file_tmp_filename:
180 * @base: the base name of the temp file to create, containing "XXXXXX"
181 * @extension: an optional extension for @base or %NULL
182 *
183 * Gets a usable temp filename with g_mkstemp() using @base as the name
184 * with an optional @extension. @base should contain "XXXXXX" in it.
185 *
186 * Notice that this does not create the file. It only gets a valid
187 * filename.
188 *
189 * Returns: a newly-allocated string containing the name of the temp
190 * file name or %NULL.
191 **/
192 char *
ephy_file_tmp_filename(const char * base,const char * extension)193 ephy_file_tmp_filename (const char *base,
194 const char *extension)
195 {
196 int fd;
197 char *name = g_strdup (base);
198
199 fd = g_mkstemp (name);
200
201 if (fd != -1) {
202 unlink (name);
203 close (fd);
204 } else {
205 g_free (name);
206
207 return NULL;
208 }
209
210 if (extension) {
211 char *tmp;
212 tmp = g_strconcat (name, ".",
213 extension, NULL);
214 g_free (name);
215 name = tmp;
216 }
217
218 return name;
219 }
220
221 /**
222 * ephy_profile_dir:
223 *
224 * Gets Epiphany's configuration directory, usually .local/share/epiphany
225 * under user's homedir.
226 *
227 * Returns: the full path to Epiphany's configuration directory
228 **/
229 const char *
ephy_profile_dir(void)230 ephy_profile_dir (void)
231 {
232 return profile_dir_global;
233 }
234
235 /**
236 * ephy_config_dir:
237 *
238 * Gets Epiphany's configuration directory, usually .config/epiphany
239 * under user's homedir.
240 *
241 * Returns: the full path to Epiphany's configuration directory
242 **/
243 const char *
ephy_config_dir(void)244 ephy_config_dir (void)
245 {
246 return config_dir;
247 }
248
249 /**
250 * ephy_cache_dir:
251 *
252 * Gets Epiphany's cache directory, usually .cache/epiphany
253 * under user's homedir.
254 *
255 * Returns: the full path to Epiphany's cache directory
256 **/
257 const char *
ephy_cache_dir(void)258 ephy_cache_dir (void)
259 {
260 return cache_dir;
261 }
262
263
264 /**
265 * ephy_profile_dir_is_default:
266 *
267 * Returns whether the profile directory in use is the default one, found in
268 * ~/.local/share
269 *
270 * Returns: %TRUE if it is the default profile dir, %FALSE for others
271 **/
272 gboolean
ephy_profile_dir_is_default(void)273 ephy_profile_dir_is_default (void)
274 {
275 return profile_dir_type == EPHY_PROFILE_DIR_DEFAULT || profile_dir_type == EPHY_PROFILE_DIR_TEST;
276 }
277
278 /**
279 * ephy_profile_dir_is_web_application:
280 *
281 * Returns whether the profile directory in use is a web appplication one.
282 *
283 * Returns: %TRUE if it is a web application profile dir, %FALSE for others
284 */
285 gboolean
ephy_profile_dir_is_web_application(void)286 ephy_profile_dir_is_web_application (void)
287 {
288 return profile_dir_type == EPHY_PROFILE_DIR_WEB_APP;
289 }
290
291 /**
292 * ephy_default_profile_dir:
293 *
294 * Get the path to the default profile directory found in ~/.local/share
295 *
296 * Returns: a new allocated string, free with g_free() when done.
297 */
298 char *
ephy_default_profile_dir(void)299 ephy_default_profile_dir (void)
300 {
301 return profile_dir_type == EPHY_PROFILE_DIR_TEST ?
302 g_strdup (ephy_profile_dir ()) :
303 g_build_filename (g_get_user_data_dir (), "epiphany", NULL);
304 }
305
306 /**
307 * ephy_default_cache_dir:
308 *
309 * Get the path to the default cache directory found in ~/.cache
310 *
311 * Returns: a new allocated string, free with g_free() when done.
312 */
313 char *
ephy_default_cache_dir(void)314 ephy_default_cache_dir (void)
315 {
316 return profile_dir_type == EPHY_PROFILE_DIR_TEST ?
317 g_build_filename (ephy_profile_dir (), "cache", NULL) :
318 g_build_filename (g_get_user_cache_dir (), "epiphany", NULL);
319 }
320
321 /**
322 * ephy_default_config_dir:
323 *
324 * Get the path to the default config directory found in ~/.config
325 *
326 * Returns: a new allocated string, free with g_free() when done.
327 */
328 char *
ephy_default_config_dir(void)329 ephy_default_config_dir (void)
330 {
331 return profile_dir_type == EPHY_PROFILE_DIR_TEST ?
332 g_build_filename (ephy_profile_dir (), "config", NULL) :
333 g_build_filename (g_get_user_config_dir (), "epiphany", NULL);
334 }
335
336 /**
337 * ephy_file_helpers_init:
338 * @profile_dir: directory to use as Epiphany's profile
339 * @flags: the %EphyFileHelpersFlags for this session
340 * @error: an optional #GError
341 *
342 * Initializes Epiphany file helper functions, sets @profile_dir as Epiphany's
343 * profile dir and whether the running session will be private.
344 *
345 * Returns: %FALSE if the profile dir couldn't be created or accessed
346 **/
347 gboolean
ephy_file_helpers_init(const char * profile_dir,EphyFileHelpersFlags flags,GError ** error)348 ephy_file_helpers_init (const char *profile_dir,
349 EphyFileHelpersFlags flags,
350 GError **error)
351 {
352 gboolean ret = TRUE;
353 gboolean private_profile;
354 gboolean steal_data_from_profile;
355 g_autofree char *app_file = NULL;
356
357 ephy_file_helpers_error_quark = g_quark_from_static_string ("ephy-file-helpers-error");
358
359 files = g_hash_table_new_full (g_str_hash,
360 g_str_equal,
361 (GDestroyNotify)g_free,
362 (GDestroyNotify)g_free);
363
364 keep_directory = flags & EPHY_FILE_HELPERS_KEEP_DIR;
365 private_profile = (flags & EPHY_FILE_HELPERS_PRIVATE_PROFILE || flags & EPHY_FILE_HELPERS_TESTING_MODE);
366 steal_data_from_profile = flags & EPHY_FILE_HELPERS_STEAL_DATA;
367
368 if (profile_dir != NULL && !steal_data_from_profile) {
369 if (g_path_is_absolute (profile_dir)) {
370 profile_dir_global = g_strdup (profile_dir);
371 } else {
372 GFile *file = g_file_new_for_path (profile_dir);
373 profile_dir_global = g_file_get_path (file);
374 g_object_unref (file);
375 }
376
377 app_file = g_build_filename (profile_dir, ".app", NULL);
378 if (g_file_test (app_file, G_FILE_TEST_EXISTS)) {
379 const char *app_name = ephy_web_application_get_program_name_from_profile_directory (profile_dir_global);
380 cache_dir = g_build_filename (g_get_user_cache_dir (), app_name, NULL);
381 config_dir = g_build_filename (g_get_user_config_dir (), app_name, NULL);
382 profile_dir_type = EPHY_PROFILE_DIR_WEB_APP;
383 } else {
384 cache_dir = g_build_filename (profile_dir_global, "cache", NULL);
385 config_dir = g_build_filename (profile_dir_global, "config", NULL);
386 }
387 } else if (private_profile) {
388 if (ephy_file_tmp_dir () == NULL) {
389 g_set_error (error,
390 EPHY_FILE_HELPERS_ERROR_QUARK,
391 0,
392 _("Could not create a temporary directory in “%s”."),
393 g_get_tmp_dir ());
394 return FALSE;
395 }
396
397 profile_dir_global = g_build_filename (ephy_file_tmp_dir (),
398 "epiphany",
399 NULL);
400 cache_dir = g_build_filename (profile_dir_global, "cache", NULL);
401 config_dir = g_build_filename (profile_dir_global, "config", NULL);
402 if (flags & EPHY_FILE_HELPERS_TESTING_MODE)
403 profile_dir_type = EPHY_PROFILE_DIR_TEST;
404 }
405
406 if (profile_dir_global == NULL) {
407 profile_dir_type = EPHY_PROFILE_DIR_DEFAULT;
408 profile_dir_global = ephy_default_profile_dir ();
409 }
410
411 if (cache_dir == NULL)
412 cache_dir = ephy_default_cache_dir ();
413
414 if (config_dir == NULL)
415 config_dir = ephy_default_config_dir ();
416
417 if (flags & EPHY_FILE_HELPERS_ENSURE_EXISTS) {
418 ret = ephy_ensure_dir_exists (ephy_profile_dir (), error);
419 ephy_ensure_dir_exists (ephy_cache_dir (), NULL);
420 ephy_ensure_dir_exists (ephy_config_dir (), NULL);
421 ephy_ensure_dir_exists (ephy_file_tmp_dir (), NULL);
422 }
423
424 if (steal_data_from_profile && profile_dir) {
425 guint i;
426 const char *files_to_copy[] = { EPHY_HISTORY_FILE, EPHY_BOOKMARKS_FILE };
427
428 for (i = 0; i < G_N_ELEMENTS (files_to_copy); i++) {
429 char *filename;
430 GError *err = NULL;
431 GFile *source, *destination;
432
433 filename = g_build_filename (profile_dir,
434 files_to_copy[i],
435 NULL);
436 source = g_file_new_for_path (filename);
437 g_free (filename);
438
439 filename = g_build_filename (profile_dir_global,
440 files_to_copy[i],
441 NULL);
442 destination = g_file_new_for_path (filename);
443 g_free (filename);
444
445 g_file_copy (source, destination,
446 G_FILE_COPY_OVERWRITE,
447 NULL, NULL, NULL, &err);
448 if (err) {
449 printf ("Error stealing file %s from profile: %s\n", files_to_copy[i], err->message);
450 g_error_free (err);
451 }
452
453 g_object_unref (source);
454 g_object_unref (destination);
455 }
456 }
457
458 return ret;
459 }
460
461 /**
462 * ephy_file_helpers_shutdown:
463 *
464 * Cleans file helpers information, corresponds to ephy_file_helpers_init().
465 **/
466 void
ephy_file_helpers_shutdown(void)467 ephy_file_helpers_shutdown (void)
468 {
469 g_hash_table_destroy (files);
470
471 if (mime_table != NULL) {
472 LOG ("Destroying mime type hashtable");
473 g_hash_table_destroy (mime_table);
474 mime_table = NULL;
475 }
476
477 g_clear_pointer (&profile_dir_global, g_free);
478 g_clear_pointer (&cache_dir, g_free);
479 g_clear_pointer (&config_dir, g_free);
480
481 if (tmp_dir != NULL) {
482 if (!keep_directory) {
483 /* recursively delete the contents and the
484 * directory */
485 LOG ("shutdown: delete tmp_dir %s", tmp_dir);
486 ephy_file_delete_dir_recursively (tmp_dir, NULL);
487 }
488
489 g_free (tmp_dir);
490 tmp_dir = NULL;
491 }
492 }
493
494 /**
495 * ephy_ensure_dir_exists:
496 * @dir: path to a directory
497 * @error: an optional GError to fill or %NULL
498 *
499 * Checks if @dir exists and is a directory, if it it exists and it's not a
500 * directory %FALSE is returned. If @dir doesn't exist and can't be created
501 * then %FALSE is returned.
502 *
503 * Returns: %TRUE if @dir exists and is a directory
504 **/
505 gboolean
ephy_ensure_dir_exists(const char * dir,GError ** error)506 ephy_ensure_dir_exists (const char *dir,
507 GError **error)
508 {
509 if (g_file_test (dir, G_FILE_TEST_EXISTS) &&
510 !g_file_test (dir, G_FILE_TEST_IS_DIR)) {
511 g_set_error (error,
512 EPHY_FILE_HELPERS_ERROR_QUARK,
513 0,
514 _("The file “%s” exists. Please move it out of the way."),
515 dir);
516
517 return FALSE;
518 }
519
520 if (!g_file_test (dir, G_FILE_TEST_EXISTS)) {
521 if (g_mkdir_with_parents (dir, 488) == 0) {
522 if (dir == ephy_profile_dir ()) {
523 /* We need to set the .migrated file to the
524 * current profile migration version,
525 * otherwise the next time the browser runs
526 * things might go awry. */
527 ephy_profile_utils_set_migration_version (EPHY_PROFILE_MIGRATION_VERSION);
528 }
529 } else {
530 g_set_error (error,
531 EPHY_FILE_HELPERS_ERROR_QUARK,
532 0,
533 _("Failed to create directory “%s”."),
534 dir);
535
536 return FALSE;
537 }
538 }
539
540 return TRUE;
541 }
542
543 static gboolean
launch_application(GAppInfo * app,GList * files,guint32 user_time)544 launch_application (GAppInfo *app,
545 GList *files,
546 guint32 user_time)
547 {
548 g_autoptr (GdkAppLaunchContext) context = NULL;
549 g_autoptr (GError) error = NULL;
550 GdkDisplay *display;
551 GdkScreen *screen;
552 gboolean res;
553
554 /* This is impossible to implement inside flatpak. Higher layers must
555 * ensure we don't get here.
556 */
557 g_assert (!ephy_is_running_inside_flatpak ());
558
559 display = gdk_display_get_default ();
560 screen = gdk_screen_get_default ();
561
562 context = gdk_display_get_app_launch_context (display);
563 gdk_app_launch_context_set_screen (context, screen);
564 gdk_app_launch_context_set_timestamp (context, user_time);
565
566 res = g_app_info_launch (app, files,
567 G_APP_LAUNCH_CONTEXT (context), &error);
568 if (!res)
569 g_warning ("Failed to launch %s: %s", g_app_info_get_display_name (app), error->message);
570
571 return res;
572 }
573
574 /**
575 * ephy_file_launch_desktop_file:
576 * @filename: the path to the .desktop file
577 * @tag: used to guard against improper usage
578 *
579 * Launches the application described by the desktop file @filename.
580 *
581 * Returns: %TRUE if the application launch was successful
582 **/
583 gboolean
ephy_file_launch_desktop_file(const char * filename,guint32 user_time,EphyFileHelpersNotFlatpakTag tag)584 ephy_file_launch_desktop_file (const char *filename,
585 guint32 user_time,
586 EphyFileHelpersNotFlatpakTag tag)
587 {
588 g_autoptr (GDesktopAppInfo) app = NULL;
589
590 /* This is impossible to implement inside flatpak. Higher layers must
591 * ensure we don't get here.
592 */
593 g_assert (tag == EPHY_FILE_HELPERS_I_UNDERSTAND_I_MUST_NOT_USE_THIS_FUNCTION_UNDER_FLATPAK);
594 g_assert (!ephy_is_running_inside_flatpak ());
595
596 app = g_desktop_app_info_new (filename);
597
598 return launch_application (G_APP_INFO (app), NULL, user_time);
599 }
600
601 static gboolean
launch_via_uri_handler(GFile * file)602 launch_via_uri_handler (GFile *file)
603 {
604 const char *uri;
605 GdkDisplay *display;
606 GdkAppLaunchContext *context;
607 g_autoptr (GError) error = NULL;
608
609 display = gdk_display_get_default ();
610 context = gdk_display_get_app_launch_context (display);
611
612 uri = g_file_get_uri (file);
613
614 g_app_info_launch_default_for_uri (uri, G_APP_LAUNCH_CONTEXT (context), &error);
615 if (error) {
616 g_warning ("Failed to launch handler for URI %s: %s", uri, error->message);
617 return FALSE;
618 }
619
620 return TRUE;
621 }
622
623 /**
624 * ephy_file_launch_handler:
625 * @file: a #GFile to pass as argument
626 * @user_time: user time to prevent focus stealing
627 *
628 * Launches @file with its default handler application, if @mime_type is %NULL
629 * then @file will be queried for its type.
630 *
631 * Returns: %TRUE on success
632 **/
633 gboolean
ephy_file_launch_handler(GFile * file,guint32 user_time)634 ephy_file_launch_handler (GFile *file,
635 guint32 user_time)
636 {
637 GAppInfo *app = NULL;
638 gboolean ret = FALSE;
639 g_autoptr (GList) list = NULL;
640 g_autoptr (GError) error = NULL;
641
642 g_assert (file != NULL);
643
644 /* Launch via URI handler only under flatpak, because this way loses
645 * focus stealing prevention. There's no other way to open a file
646 * under flatpak, and focus stealing prevention becomes the
647 * responsibility of the portal in this case anyway. */
648 if (ephy_is_running_inside_flatpak ())
649 return launch_via_uri_handler (file);
650
651 app = g_file_query_default_handler (file, NULL, &error);
652 if (!app) {
653 g_autofree char *path = g_file_get_path (file);
654 g_warning ("No available application to open %s: %s", path, error->message);
655 return FALSE;
656 }
657
658 list = g_list_append (list, file);
659 ret = launch_application (app, list, user_time);
660
661 return ret;
662 }
663
664 static gboolean
open_in_default_handler(const char * uri,const char * mime_type,guint32 timestamp,GdkScreen * screen,EphyFileHelpersNotFlatpakTag tag)665 open_in_default_handler (const char *uri,
666 const char *mime_type,
667 guint32 timestamp,
668 GdkScreen *screen,
669 EphyFileHelpersNotFlatpakTag tag)
670 {
671 g_autoptr (GdkAppLaunchContext) context = NULL;
672 g_autoptr (GAppInfo) appinfo = NULL;
673 g_autoptr (GError) error = NULL;
674 GList uris;
675
676 /* This is impossible to implement inside flatpak. Higher layers must
677 * ensure we don't get here.
678 */
679 g_assert (tag == EPHY_FILE_HELPERS_I_UNDERSTAND_I_MUST_NOT_USE_THIS_FUNCTION_UNDER_FLATPAK);
680 g_assert (!ephy_is_running_inside_flatpak ());
681
682 context = gdk_display_get_app_launch_context (screen ? gdk_screen_get_display (screen) : gdk_display_get_default ());
683 gdk_app_launch_context_set_screen (context, screen);
684 gdk_app_launch_context_set_timestamp (context, timestamp);
685
686 appinfo = g_app_info_get_default_for_type (mime_type, TRUE);
687 if (!appinfo) {
688 g_warning ("Failed to get default app for MIME type: %s", mime_type);
689 return FALSE;
690 }
691
692 uris.data = (gpointer)uri;
693 uris.next = uris.prev = NULL;
694
695 if (!g_app_info_launch_uris (appinfo, &uris, G_APP_LAUNCH_CONTEXT (context), &error)) {
696 g_warning ("Failed to launch %s: %s", uri, error->message);
697 return FALSE;
698 }
699
700 return TRUE;
701 }
702
703 gboolean
ephy_file_open_uri_in_default_browser(const char * uri,guint32 user_time,GdkScreen * screen,EphyFileHelpersNotFlatpakTag tag)704 ephy_file_open_uri_in_default_browser (const char *uri,
705 guint32 user_time,
706 GdkScreen *screen,
707 EphyFileHelpersNotFlatpakTag tag)
708 {
709 return open_in_default_handler (uri, "x-scheme-handler/http", user_time, screen, tag);
710 }
711
712 /**
713 * ephy_file_browse_to:
714 * @file: a #GFile
715 * @user_time: user_time to prevent focus stealing
716 *
717 * Launches the default application for browsing directories to point to
718 * @file. E.g. nautilus will jump to @file within its directory and
719 * select it.
720 *
721 * Returns: %TRUE if the launch succeeded
722 **/
723 gboolean
ephy_file_browse_to(GFile * file,guint32 user_time)724 ephy_file_browse_to (GFile *file,
725 guint32 user_time)
726 {
727 g_autofree char *uri = g_file_get_uri (file);
728
729 if (ephy_is_running_inside_flatpak ()) {
730 ephy_open_directory_via_flatpak_portal (uri);
731 return TRUE;
732 }
733
734 return open_in_default_handler (uri, "inode/directory", user_time, NULL, EPHY_FILE_HELPERS_I_UNDERSTAND_I_MUST_NOT_USE_THIS_FUNCTION_UNDER_FLATPAK);
735 }
736
737 /**
738 * ephy_file_delete_dir_recursively:
739 * @directory: directory to remove
740 * @error: location to set any #GError
741 *
742 * Remove @path and its contents. Like calling rm -rf @path.
743 *
744 * Returns: %TRUE if delete succeeded
745 **/
746 gboolean
ephy_file_delete_dir_recursively(const char * directory,GError ** error)747 ephy_file_delete_dir_recursively (const char *directory,
748 GError **error)
749 {
750 GDir *dir;
751 const char *file_name;
752 gboolean failed = FALSE;
753
754 dir = g_dir_open (directory, 0, error);
755 if (!dir)
756 return FALSE;
757
758 file_name = g_dir_read_name (dir);
759 while (file_name && !failed) {
760 char *file_path;
761
762 file_path = g_build_filename (directory, file_name, NULL);
763 if (g_file_test (file_path, G_FILE_TEST_IS_DIR)) {
764 failed = !ephy_file_delete_dir_recursively (file_path, error);
765 } else {
766 int result = g_unlink (file_path);
767
768 if (result == -1) {
769 int errsv = errno;
770
771 g_set_error (error, G_IO_ERROR,
772 g_io_error_from_errno (errsv),
773 "Error removing file %s: %s",
774 file_path, g_strerror (errsv));
775 failed = TRUE;
776 }
777 }
778 g_free (file_path);
779 file_name = g_dir_read_name (dir);
780 }
781 g_dir_close (dir);
782
783 if (!failed) {
784 int result = g_rmdir (directory);
785
786 if (result == -1) {
787 int errsv = errno;
788
789 g_set_error (error, G_IO_ERROR,
790 g_io_error_from_errno (errsv),
791 "Error removing directory %s: %s",
792 directory, g_strerror (errsv));
793 failed = TRUE;
794 }
795 }
796
797 return !failed;
798 }
799
800 /**
801 * ephy_sanitize_filename:
802 * @filename: a filename
803 *
804 * Sanitize @filename to make sure it's a valid filename. If the
805 * filename contains directory separators, they will be converted to
806 * underscores, so that they are not interpreted as a path by the
807 * filesystem.
808 *
809 * Note that it modifies string in place. The return value is to allow nesting.
810 *
811 * Returns: the sanitized filename
812 */
813 char *
ephy_sanitize_filename(char * filename)814 ephy_sanitize_filename (char *filename)
815 {
816 g_assert (filename != NULL);
817
818 return g_strdelimit (filename, G_DIR_SEPARATOR_S, '_');
819 }
820
821 void
ephy_open_default_instance_window(void)822 ephy_open_default_instance_window (void)
823 {
824 GError *error = NULL;
825
826 g_spawn_command_line_async ("epiphany --new-window", &error);
827
828 if (error) {
829 g_warning ("Couldn't open default instance: %s", error->message);
830 g_error_free (error);
831 }
832 }
833
834 void
ephy_open_incognito_window(const char * uri)835 ephy_open_incognito_window (const char *uri)
836 {
837 char *command;
838 GError *error = NULL;
839
840 command = g_strdup_printf ("epiphany --incognito-mode --profile %s ", ephy_profile_dir ());
841
842 if (uri) {
843 char *str = g_strconcat (command, uri, NULL);
844 g_free (command);
845 command = str;
846 }
847
848 g_spawn_command_line_async (command, &error);
849
850 if (error) {
851 g_warning ("Couldn't open link in incognito window: %s", error->message);
852 g_error_free (error);
853 }
854
855 g_free (command);
856 }
857
858 void
ephy_copy_directory(const char * source,const char * target)859 ephy_copy_directory (const char *source,
860 const char *target)
861 {
862 g_autoptr (GError) error = NULL;
863 GFileType type;
864 g_autoptr (GFile) src_file = g_file_new_for_path (source);
865 g_autoptr (GFile) dest_file = g_file_new_for_path (target);
866
867 type = g_file_query_file_type (src_file, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL);
868
869 if (type == G_FILE_TYPE_DIRECTORY) {
870 g_autoptr (GFileEnumerator) enumerator = NULL;
871 g_autoptr (GFileInfo) info = NULL;
872
873 if (!g_file_make_directory_with_parents (dest_file, NULL, &error)) {
874 if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_EXISTS)) {
875 g_warning ("Could not create target directory for webextension: %s", error->message);
876 return;
877 }
878
879 g_error_free (error);
880 }
881
882 if (!g_file_copy_attributes (src_file, dest_file, G_FILE_COPY_NONE, NULL, &error)) {
883 g_warning ("Could not copy file attributes for webextension: %s", error->message);
884 return;
885 }
886
887 enumerator = g_file_enumerate_children (src_file, G_FILE_ATTRIBUTE_STANDARD_NAME, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL, &error);
888 if (!enumerator) {
889 g_warning ("Could not create file enumberator for webextensions: %s", error->message);
890 return;
891 }
892
893 for (info = g_file_enumerator_next_file (enumerator, NULL, NULL); info != NULL; info = g_file_enumerator_next_file (enumerator, NULL, NULL)) {
894 ephy_copy_directory (
895 g_build_filename (source, g_file_info_get_name (info), NULL),
896 g_build_filename (target, g_file_info_get_name (info), NULL));
897 }
898 } else if (type == G_FILE_TYPE_REGULAR) {
899 if (!g_file_copy (src_file, dest_file, G_FILE_COPY_NONE, NULL, NULL, NULL, &error)) {
900 if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_EXISTS)) {
901 g_warning ("Could not copy file for webextensions: %s", error->message);
902 return;
903 }
904 }
905 }
906 }
907