1
/* vi:set et ai sw=2 sts=2 ts=2: */
3
* Copyright (c) 2009-2010 Jannis Pohlmann <jannis@xfce.org>
5
* This program is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU General Public License as
7
* published by the Free Software Foundation; either version 2 of
8
* the License, or (at your option) 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
16
* License along with this program; if not, write to the Free
17
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18
* Boston, MA 02110-1301, USA.
27
#include <gio/gunixmounts.h>
31
#include <libxfce4util/libxfce4util.h>
33
#include <thunar/thunar-file.h>
34
#include <thunar/thunar-gio-extensions.h>
35
#include <thunar/thunar-private.h>
36
#include <thunar/thunar-util.h>
41
thunar_g_file_new_for_home (void)
43
return g_file_new_for_path (xfce_get_homedir ());
49
thunar_g_file_new_for_root (void)
51
return g_file_new_for_uri ("file:///");
57
thunar_g_file_new_for_trash (void)
59
return g_file_new_for_uri ("trash:///");
65
thunar_g_file_new_for_desktop (void)
67
return g_file_new_for_path (g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP));
73
thunar_g_file_new_for_user_special_dir (GUserDirectory dir)
77
_thunar_return_val_if_fail (dir < G_USER_N_DIRECTORIES, NULL);
79
path = g_get_user_special_dir (dir);
81
path = xfce_get_homedir ();
83
return g_file_new_for_path (path);
89
thunar_g_file_is_root (GFile *file)
92
gboolean is_root = TRUE;
94
parent = g_file_get_parent (file);
95
if (G_UNLIKELY (parent != NULL))
98
g_object_unref (parent);
107
thunar_g_file_is_trashed (GFile *file)
109
_thunar_return_val_if_fail (G_IS_FILE (file), FALSE);
110
return g_file_has_uri_scheme (file, "trash");
116
thunar_g_file_is_home (GFile *file)
119
gboolean is_home = FALSE;
121
_thunar_return_val_if_fail (G_IS_FILE (file), FALSE);
123
home = thunar_g_file_new_for_home ();
124
is_home = g_file_equal (home, file);
125
g_object_unref (home);
133
thunar_g_file_is_desktop (GFile *file)
138
_thunar_return_val_if_fail (G_IS_FILE (file), FALSE);
140
desktop = thunar_g_file_new_for_desktop ();
141
is_desktop = g_file_equal (desktop, file);
142
g_object_unref (desktop);
150
thunar_g_file_query_key_file (GFile *file,
151
GCancellable *cancellable,
155
gchar *contents = NULL;
158
_thunar_return_val_if_fail (G_IS_FILE (file), NULL);
159
_thunar_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
160
_thunar_return_val_if_fail (error == NULL || *error == NULL, NULL);
162
/* try to load the entire file into memory */
163
if (!g_file_load_contents (file, cancellable, &contents, &length, NULL, error))
166
/* allocate a new key file */
167
key_file = g_key_file_new ();
169
/* try to parse the key file from the contents of the file */
170
if (G_LIKELY (length == 0
171
|| g_key_file_load_from_data (key_file, contents, length,
172
G_KEY_FILE_KEEP_COMMENTS
173
| G_KEY_FILE_KEEP_TRANSLATIONS,
182
g_key_file_free (key_file);
190
thunar_g_file_write_key_file (GFile *file,
192
GCancellable *cancellable,
198
_thunar_return_val_if_fail (G_IS_FILE (file), FALSE);
199
_thunar_return_val_if_fail (key_file != NULL, FALSE);
200
_thunar_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
201
_thunar_return_val_if_fail (error == NULL || *error == NULL, FALSE);
203
/* write the key file into the contents buffer */
204
contents = g_key_file_to_data (key_file, &length, NULL);
206
/* try to replace the file contents with the key file data */
207
if (!g_file_replace_contents (file, contents, length, NULL, FALSE,
208
G_FILE_CREATE_REPLACE_DESTINATION,
209
NULL, cancellable, error))
224
thunar_g_file_get_location (GFile *file)
228
_thunar_return_val_if_fail (G_IS_FILE (file), NULL);
230
location = g_file_get_path (file);
231
if (location == NULL)
232
location = g_file_get_uri (file);
240
thunar_g_vfs_is_uri_scheme_supported (const gchar *scheme)
242
const gchar * const *supported_schemes;
243
gboolean supported = FALSE;
247
_thunar_return_val_if_fail (scheme != NULL && *scheme != '\0', FALSE);
249
gvfs = g_vfs_get_default ();
250
supported_schemes = g_vfs_get_supported_uri_schemes (gvfs);
252
for (n = 0; !supported && supported_schemes[n] != NULL; ++n)
253
if (g_strcmp0 (supported_schemes[n], scheme) == 0)
262
thunar_g_file_list_get_type (void)
264
static GType type = G_TYPE_INVALID;
266
if (G_UNLIKELY (type == G_TYPE_INVALID))
268
type = g_boxed_type_register_static (I_("ThunarGFileList"),
269
(GBoxedCopyFunc) thunar_g_file_list_copy,
270
(GBoxedFreeFunc) thunar_g_file_list_free);
279
* thunar_g_file_list_new_from_string:
280
* @string : a string representation of an URI list.
282
* Splits an URI list conforming to the text/uri-list
283
* mime type defined in RFC 2483 into individual URIs,
284
* discarding any comments and whitespace. The resulting
285
* list will hold one #GFile for each URI.
287
* If @string contains no URIs, this function
290
* Return value: the list of #GFile<!---->s or %NULL.
293
thunar_g_file_list_new_from_string (const gchar *string)
299
uris = g_uri_list_extract_uris (string);
301
for (n = 0; uris != NULL && uris[n] != NULL; ++n)
302
list = g_list_append (list, g_file_new_for_uri (uris[n]));
312
* thunar_g_file_list_to_string:
313
* @list : a list of #GFile<!---->s.
315
* Free the returned value using g_free() when you
318
* Return value: the string representation of @list conforming to the
319
* text/uri-list mime type defined in RFC 2483.
322
thunar_g_file_list_to_string (GList *list)
328
/* allocate initial string */
329
string = g_string_new (NULL);
331
for (lp = list; lp != NULL; lp = lp->next)
333
uri = g_file_get_uri (lp->data);
334
string = g_string_append (string, uri);
337
string = g_string_append (string, "\r\n");
340
return g_string_free (string, FALSE);
346
thunar_g_file_list_append (GList *list,
349
return g_list_append (list, g_object_ref (file));
355
thunar_g_file_list_prepend (GList *list,
358
return g_list_prepend (list, g_object_ref (file));
364
* thunar_g_file_list_copy:
365
* @list : a list of #GFile<!---->s.
367
* Takes a deep copy of @list and returns the
368
* result. The caller is responsible to free the
369
* returned list using thunar_g_file_list_free().
371
* Return value: a deep copy of @list.
374
thunar_g_file_list_copy (GList *list)
379
for (lp = g_list_last (list); lp != NULL; lp = lp->prev)
380
copy = g_list_prepend (copy, g_object_ref (lp->data));
388
* thunar_g_file_list_free:
389
* @list : a list of #GFile<!---->s.
391
* Frees the #GFile<!---->s in @list and
395
thunar_g_file_list_free (GList *list)
398
for (lp = list; lp != NULL; lp = lp->next)
399
g_object_unref (lp->data);
407
thunar_g_mount_is_internal (GMount *mount)
409
const gchar *point_mount_path;
410
gboolean is_internal = FALSE;
416
_thunar_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
418
/* determine the mount path */
419
root = g_mount_get_root (mount);
420
mount_path = g_file_get_path (root);
421
g_object_unref (root);
423
/* assume non-internal if we cannot determine the path */
424
if (mount_path == NULL)
427
if (g_unix_is_mount_path_system_internal (mount_path))
429
/* mark as internal */
434
/* get a list of all mount points */
435
mount_points = g_unix_mount_points_get (NULL);
437
/* search for the mount point associated with the mount entry */
438
for (lp = mount_points; !is_internal && lp != NULL; lp = lp->next)
440
point_mount_path = g_unix_mount_point_get_mount_path (lp->data);
442
/* check if this is the mount point we are looking for */
443
if (g_strcmp0 (mount_path, point_mount_path) == 0)
445
/* mark as internal if the user cannot mount this device */
446
if (!g_unix_mount_point_is_user_mountable (lp->data))
450
/* free the mount point, we no longer need it */
451
g_unix_mount_point_free (lp->data);
454
/* free the mount point list */
455
g_list_free (mount_points);
467
thunar_g_volume_is_removable (GVolume *volume)
469
gboolean can_eject = FALSE;
470
gboolean can_mount = FALSE;
471
gboolean can_unmount = FALSE;
472
gboolean is_removable = FALSE;
473
gboolean is_internal = FALSE;
477
_thunar_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
479
/* check if the volume can be ejected */
480
can_eject = g_volume_can_eject (volume);
482
/* determine the drive for the volume */
483
drive = g_volume_get_drive (volume);
486
/*check if the drive media can be removed */
487
is_removable = g_drive_is_media_removable (drive);
489
/* release the drive */
490
g_object_unref (drive);
493
/* determine the mount for the volume (if it is mounted at all) */
494
mount = g_volume_get_mount (volume);
498
is_internal = thunar_g_mount_is_internal (mount);
501
/* check if the volume can be unmounted */
502
can_unmount = g_mount_can_unmount (mount);
504
/* release the mount */
505
g_object_unref (mount);
508
/* determine whether the device can be mounted */
509
can_mount = g_volume_can_mount (volume);
511
return (!is_internal) && (can_eject || can_unmount || is_removable || can_mount);
517
thunar_g_volume_is_mounted (GVolume *volume)
519
gboolean is_mounted = FALSE;
522
_thunar_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
524
/* determine the mount for this volume (if it is mounted at all) */
525
mount = g_volume_get_mount (volume);
529
g_object_unref (mount);
538
thunar_g_volume_is_present (GVolume *volume)
540
gboolean has_media = FALSE;
541
gboolean is_shadowed = FALSE;
545
_thunar_return_val_if_fail (G_IS_VOLUME (volume), FALSE);
547
drive = g_volume_get_drive (volume);
550
has_media = g_drive_has_media (drive);
551
g_object_unref (drive);
554
mount = g_volume_get_mount (volume);
557
is_shadowed = g_mount_is_shadowed (mount);
558
g_object_unref (mount);
561
return has_media && !is_shadowed;
567
thunar_g_app_info_launch (GAppInfo *info,
568
GFile *working_directory,
570
GAppLaunchContext *context,
573
gboolean result = FALSE;
574
gchar *new_path = NULL;
575
gchar *old_path = NULL;
577
_thunar_return_val_if_fail (G_IS_APP_INFO (info), FALSE);
578
_thunar_return_val_if_fail (working_directory == NULL || G_IS_FILE (working_directory), FALSE);
579
_thunar_return_val_if_fail (path_list != NULL, FALSE);
580
_thunar_return_val_if_fail (G_IS_APP_LAUNCH_CONTEXT (context), FALSE);
581
_thunar_return_val_if_fail (error == NULL || *error == NULL, FALSE);
583
/* check if we want to set the working directory of the spawned app */
584
if (working_directory != NULL)
586
/* determine the working directory path */
587
new_path = g_file_get_path (working_directory);
588
if (new_path != NULL)
590
/* switch to the desired working directory, remember that of Thunar itself */
591
old_path = thunar_util_change_working_directory (new_path);
593
/* forget about the new working directory path */
598
/* launch the paths with the specified app info */
599
result = g_app_info_launch (info, path_list, context, error);
601
/* check if we need to reset the working directory to the one Thunar was
603
if (old_path != NULL)
605
/* switch to Thunar's original working directory */
606
new_path = thunar_util_change_working_directory (old_path);