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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
|
/* GTK - The GIMP Toolkit
* gtkfilesystem.c: Filesystem abstraction functions.
* Copyright (C) 2003, Red Hat, Inc.
* Copyright (C) 2007-2008 Carlos Garnacho
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This library 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
* Authors: Carlos Garnacho <carlos@imendio.com>
*/
#include "config.h"
#include <string.h>
#include <glib/gi18n-lib.h>
#include "gtkfilechooser.h"
#include "gtkfilesystem.h"
#include "gtkicontheme.h"
#include "gtkprivate.h"
/* #define DEBUG_MODE */
#ifdef DEBUG_MODE
#define DEBUG(x) g_debug (x);
#else
#define DEBUG(x)
#endif
#define FILES_PER_QUERY 100
/* The pointers we return for a GtkFileSystemVolume are opaque tokens; they are
* really pointers to GDrive, GVolume or GMount objects. We need an extra
* token for the fake "File System" volume. So, we'll return a pointer to
* this particular string.
*/
static const gchar *root_volume_token = N_("File System");
#define IS_ROOT_VOLUME(volume) ((gpointer) (volume) == (gpointer) root_volume_token)
enum {
PROP_0,
PROP_FILE,
PROP_ENUMERATOR,
PROP_ATTRIBUTES
};
enum {
BOOKMARKS_CHANGED,
VOLUMES_CHANGED,
FS_LAST_SIGNAL
};
enum {
FILES_ADDED,
FILES_REMOVED,
FILES_CHANGED,
FINISHED_LOADING,
DELETED,
FOLDER_LAST_SIGNAL
};
static guint fs_signals [FS_LAST_SIGNAL] = { 0, };
typedef struct AsyncFuncData AsyncFuncData;
struct GtkFileSystemPrivate
{
GVolumeMonitor *volume_monitor;
/* This list contains elements that can be
* of type GDrive, GVolume and GMount
*/
GSList *volumes;
/* This list contains GtkFileSystemBookmark structs */
GSList *bookmarks;
GFileMonitor *bookmarks_monitor;
};
struct AsyncFuncData
{
GtkFileSystem *file_system;
GFile *file;
GCancellable *cancellable;
gchar *attributes;
gpointer callback;
gpointer data;
};
struct GtkFileSystemBookmark
{
GFile *file;
gchar *label;
};
G_DEFINE_TYPE (GtkFileSystem, _gtk_file_system, G_TYPE_OBJECT)
/* GtkFileSystemBookmark methods */
void
_gtk_file_system_bookmark_free (GtkFileSystemBookmark *bookmark)
{
g_object_unref (bookmark->file);
g_free (bookmark->label);
g_slice_free (GtkFileSystemBookmark, bookmark);
}
/* GtkFileSystem methods */
static void
volumes_changed (GVolumeMonitor *volume_monitor,
gpointer volume,
gpointer user_data)
{
GtkFileSystem *file_system;
gdk_threads_enter ();
file_system = GTK_FILE_SYSTEM (user_data);
g_signal_emit (file_system, fs_signals[VOLUMES_CHANGED], 0, volume);
gdk_threads_leave ();
}
static void
gtk_file_system_dispose (GObject *object)
{
GtkFileSystem *file_system = GTK_FILE_SYSTEM (object);
GtkFileSystemPrivate *priv = file_system->priv;
DEBUG ("dispose");
if (priv->volumes)
{
g_slist_foreach (priv->volumes, (GFunc) g_object_unref, NULL);
g_slist_free (priv->volumes);
priv->volumes = NULL;
}
if (priv->volume_monitor)
{
g_signal_handlers_disconnect_by_func (priv->volume_monitor, volumes_changed, object);
g_object_unref (priv->volume_monitor);
priv->volume_monitor = NULL;
}
G_OBJECT_CLASS (_gtk_file_system_parent_class)->dispose (object);
}
static void
gtk_file_system_finalize (GObject *object)
{
GtkFileSystem *file_system = GTK_FILE_SYSTEM (object);
GtkFileSystemPrivate *priv = file_system->priv;
DEBUG ("finalize");
if (priv->bookmarks_monitor)
g_object_unref (priv->bookmarks_monitor);
if (priv->bookmarks)
{
g_slist_foreach (priv->bookmarks, (GFunc) _gtk_file_system_bookmark_free, NULL);
g_slist_free (priv->bookmarks);
}
G_OBJECT_CLASS (_gtk_file_system_parent_class)->finalize (object);
}
static void
_gtk_file_system_class_init (GtkFileSystemClass *class)
{
GObjectClass *object_class = G_OBJECT_CLASS (class);
object_class->dispose = gtk_file_system_dispose;
object_class->finalize = gtk_file_system_finalize;
fs_signals[BOOKMARKS_CHANGED] =
g_signal_new ("bookmarks-changed",
G_TYPE_FROM_CLASS (object_class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GtkFileSystemClass, bookmarks_changed),
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
fs_signals[VOLUMES_CHANGED] =
g_signal_new ("volumes-changed",
G_TYPE_FROM_CLASS (object_class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GtkFileSystemClass, volumes_changed),
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
g_type_class_add_private (object_class, sizeof (GtkFileSystemPrivate));
}
static GFile *
get_legacy_bookmarks_file (void)
{
GFile *file;
gchar *filename;
filename = g_build_filename (g_get_home_dir (), ".gtk-bookmarks", NULL);
file = g_file_new_for_path (filename);
g_free (filename);
return file;
}
static GFile *
get_bookmarks_file (void)
{
GFile *file;
gchar *filename;
filename = g_build_filename (g_get_user_config_dir (), "gtk-3.0", "bookmarks", NULL);
file = g_file_new_for_path (filename);
g_free (filename);
return file;
}
static GSList *
read_bookmarks (GFile *file)
{
gchar *contents;
gchar **lines, *space;
GSList *bookmarks = NULL;
gint i;
if (!g_file_load_contents (file, NULL, &contents,
NULL, NULL, NULL))
return NULL;
lines = g_strsplit (contents, "\n", -1);
for (i = 0; lines[i]; i++)
{
GtkFileSystemBookmark *bookmark;
if (!*lines[i])
continue;
if (!g_utf8_validate (lines[i], -1, NULL))
continue;
bookmark = g_slice_new0 (GtkFileSystemBookmark);
if ((space = strchr (lines[i], ' ')) != NULL)
{
space[0] = '\0';
bookmark->label = g_strdup (space + 1);
}
bookmark->file = g_file_new_for_uri (lines[i]);
bookmarks = g_slist_prepend (bookmarks, bookmark);
}
bookmarks = g_slist_reverse (bookmarks);
g_strfreev (lines);
g_free (contents);
return bookmarks;
}
static void
save_bookmarks (GFile *bookmarks_file,
GSList *bookmarks)
{
GError *error = NULL;
GString *contents;
GSList *l;
GFile *parent_file;
gchar *path;
contents = g_string_new ("");
for (l = bookmarks; l; l = l->next)
{
GtkFileSystemBookmark *bookmark = l->data;
gchar *uri;
uri = g_file_get_uri (bookmark->file);
if (!uri)
continue;
g_string_append (contents, uri);
if (bookmark->label)
g_string_append_printf (contents, " %s", bookmark->label);
g_string_append_c (contents, '\n');
g_free (uri);
}
parent_file = g_file_get_parent (bookmarks_file);
path = g_file_get_path (parent_file);
if (g_mkdir_with_parents (path, 0700) == 0)
{
if (!g_file_replace_contents (bookmarks_file,
contents->str,
strlen (contents->str),
NULL, FALSE, 0, NULL,
NULL, &error))
{
g_critical ("%s", error->message);
g_error_free (error);
}
}
g_free (path);
g_object_unref (parent_file);
g_string_free (contents, TRUE);
}
static void
bookmarks_file_changed (GFileMonitor *monitor,
GFile *file,
GFile *other_file,
GFileMonitorEvent event,
gpointer data)
{
GtkFileSystem *file_system = GTK_FILE_SYSTEM (data);
GtkFileSystemPrivate *priv = file_system->priv;
switch (event)
{
case G_FILE_MONITOR_EVENT_CHANGED:
case G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT:
case G_FILE_MONITOR_EVENT_CREATED:
case G_FILE_MONITOR_EVENT_DELETED:
g_slist_foreach (priv->bookmarks, (GFunc) _gtk_file_system_bookmark_free, NULL);
g_slist_free (priv->bookmarks);
priv->bookmarks = read_bookmarks (file);
gdk_threads_enter ();
g_signal_emit (data, fs_signals[BOOKMARKS_CHANGED], 0);
gdk_threads_leave ();
break;
default:
/* ignore at the moment */
break;
}
}
static gboolean
mount_referenced_by_volume_activation_root (GList *volumes, GMount *mount)
{
GList *l;
GFile *mount_root;
gboolean ret;
ret = FALSE;
mount_root = g_mount_get_root (mount);
for (l = volumes; l != NULL; l = l->next)
{
GVolume *volume = G_VOLUME (l->data);
GFile *volume_activation_root;
volume_activation_root = g_volume_get_activation_root (volume);
if (volume_activation_root != NULL)
{
if (g_file_has_prefix (volume_activation_root, mount_root))
{
ret = TRUE;
g_object_unref (volume_activation_root);
break;
}
g_object_unref (volume_activation_root);
}
}
g_object_unref (mount_root);
return ret;
}
static void
get_volumes_list (GtkFileSystem *file_system)
{
GtkFileSystemPrivate *priv = file_system->priv;
GList *l, *ll;
GList *drives;
GList *volumes;
GList *mounts;
GDrive *drive;
GVolume *volume;
GMount *mount;
if (priv->volumes)
{
g_slist_foreach (priv->volumes, (GFunc) g_object_unref, NULL);
g_slist_free (priv->volumes);
priv->volumes = NULL;
}
/* first go through all connected drives */
drives = g_volume_monitor_get_connected_drives (priv->volume_monitor);
for (l = drives; l != NULL; l = l->next)
{
drive = l->data;
volumes = g_drive_get_volumes (drive);
if (volumes)
{
for (ll = volumes; ll != NULL; ll = ll->next)
{
volume = ll->data;
mount = g_volume_get_mount (volume);
if (mount)
{
/* Show mounted volume */
priv->volumes = g_slist_prepend (priv->volumes, g_object_ref (mount));
g_object_unref (mount);
}
else
{
/* Do show the unmounted volumes in the sidebar;
* this is so the user can mount it (in case automounting
* is off).
*
* Also, even if automounting is enabled, this gives a visual
* cue that the user should remember to yank out the media if
* he just unmounted it.
*/
priv->volumes = g_slist_prepend (priv->volumes, g_object_ref (volume));
}
g_object_unref (volume);
}
g_list_free (volumes);
}
else if (g_drive_is_media_removable (drive) && !g_drive_is_media_check_automatic (drive))
{
/* If the drive has no mountable volumes and we cannot detect media change.. we
* display the drive in the sidebar so the user can manually poll the drive by
* right clicking and selecting "Rescan..."
*
* This is mainly for drives like floppies where media detection doesn't
* work.. but it's also for human beings who like to turn off media detection
* in the OS to save battery juice.
*/
priv->volumes = g_slist_prepend (priv->volumes, g_object_ref (drive));
}
g_object_unref (drive);
}
g_list_free (drives);
/* add all volumes that is not associated with a drive */
volumes = g_volume_monitor_get_volumes (priv->volume_monitor);
for (l = volumes; l != NULL; l = l->next)
{
volume = l->data;
drive = g_volume_get_drive (volume);
if (drive)
{
g_object_unref (drive);
continue;
}
mount = g_volume_get_mount (volume);
if (mount)
{
/* show this mount */
priv->volumes = g_slist_prepend (priv->volumes, g_object_ref (mount));
g_object_unref (mount);
}
else
{
/* see comment above in why we add an icon for a volume */
priv->volumes = g_slist_prepend (priv->volumes, g_object_ref (volume));
}
g_object_unref (volume);
}
/* add mounts that has no volume (/etc/mtab mounts, ftp, sftp,...) */
mounts = g_volume_monitor_get_mounts (priv->volume_monitor);
for (l = mounts; l != NULL; l = l->next)
{
mount = l->data;
volume = g_mount_get_volume (mount);
if (volume)
{
g_object_unref (volume);
continue;
}
/* if there's exists one or more volumes with an activation root inside the mount,
* don't display the mount
*/
if (mount_referenced_by_volume_activation_root (volumes, mount))
{
g_object_unref (mount);
continue;
}
/* show this mount */
priv->volumes = g_slist_prepend (priv->volumes, g_object_ref (mount));
g_object_unref (mount);
}
g_list_free (volumes);
g_list_free (mounts);
}
static void
_gtk_file_system_init (GtkFileSystem *file_system)
{
GtkFileSystemPrivate *priv;
GFile *bookmarks_file;
GError *error = NULL;
DEBUG ("init");
file_system->priv = G_TYPE_INSTANCE_GET_PRIVATE (file_system,
GTK_TYPE_FILE_SYSTEM,
GtkFileSystemPrivate);
priv = file_system->priv;
/* Volumes */
priv->volume_monitor = g_volume_monitor_get ();
g_signal_connect (priv->volume_monitor, "mount-added",
G_CALLBACK (volumes_changed), file_system);
g_signal_connect (priv->volume_monitor, "mount-removed",
G_CALLBACK (volumes_changed), file_system);
g_signal_connect (priv->volume_monitor, "mount-changed",
G_CALLBACK (volumes_changed), file_system);
g_signal_connect (priv->volume_monitor, "volume-added",
G_CALLBACK (volumes_changed), file_system);
g_signal_connect (priv->volume_monitor, "volume-removed",
G_CALLBACK (volumes_changed), file_system);
g_signal_connect (priv->volume_monitor, "volume-changed",
G_CALLBACK (volumes_changed), file_system);
g_signal_connect (priv->volume_monitor, "drive-connected",
G_CALLBACK (volumes_changed), file_system);
g_signal_connect (priv->volume_monitor, "drive-disconnected",
G_CALLBACK (volumes_changed), file_system);
g_signal_connect (priv->volume_monitor, "drive-changed",
G_CALLBACK (volumes_changed), file_system);
/* Bookmarks */
bookmarks_file = get_bookmarks_file ();
priv->bookmarks = read_bookmarks (bookmarks_file);
if (!priv->bookmarks)
{
GFile *legacy_bookmarks_file;
/* Read the legacy one and write it to the new one */
legacy_bookmarks_file = get_legacy_bookmarks_file ();
priv->bookmarks = read_bookmarks (legacy_bookmarks_file);
save_bookmarks (bookmarks_file, priv->bookmarks);
g_object_unref (legacy_bookmarks_file);
}
priv->bookmarks_monitor = g_file_monitor_file (bookmarks_file,
G_FILE_MONITOR_NONE,
NULL, &error);
if (error)
{
g_warning ("%s", error->message);
g_error_free (error);
}
else
g_signal_connect (priv->bookmarks_monitor, "changed",
G_CALLBACK (bookmarks_file_changed), file_system);
g_object_unref (bookmarks_file);
}
/* GtkFileSystem public methods */
GtkFileSystem *
_gtk_file_system_new (void)
{
return g_object_new (GTK_TYPE_FILE_SYSTEM, NULL);
}
GSList *
_gtk_file_system_list_volumes (GtkFileSystem *file_system)
{
GtkFileSystemPrivate *priv = file_system->priv;
GSList *list;
DEBUG ("list_volumes");
get_volumes_list (file_system);
list = g_slist_copy (priv->volumes);
#ifndef G_OS_WIN32
/* Prepend root volume */
list = g_slist_prepend (list, (gpointer) root_volume_token);
#endif
return list;
}
GSList *
_gtk_file_system_list_bookmarks (GtkFileSystem *file_system)
{
GtkFileSystemPrivate *priv = file_system->priv;
GSList *bookmarks, *files = NULL;
DEBUG ("list_bookmarks");
bookmarks = priv->bookmarks;
while (bookmarks)
{
GtkFileSystemBookmark *bookmark;
bookmark = bookmarks->data;
bookmarks = bookmarks->next;
files = g_slist_prepend (files, g_object_ref (bookmark->file));
}
return g_slist_reverse (files);
}
static void
free_async_data (AsyncFuncData *async_data)
{
g_object_unref (async_data->file_system);
g_object_unref (async_data->file);
g_object_unref (async_data->cancellable);
g_free (async_data->attributes);
g_free (async_data);
}
static void
query_info_callback (GObject *source_object,
GAsyncResult *result,
gpointer user_data)
{
AsyncFuncData *async_data;
GError *error = NULL;
GFileInfo *file_info;
GFile *file;
DEBUG ("query_info_callback");
file = G_FILE (source_object);
async_data = (AsyncFuncData *) user_data;
file_info = g_file_query_info_finish (file, result, &error);
if (async_data->callback)
{
gdk_threads_enter ();
((GtkFileSystemGetInfoCallback) async_data->callback) (async_data->cancellable,
file_info, error, async_data->data);
gdk_threads_leave ();
}
if (file_info)
g_object_unref (file_info);
if (error)
g_error_free (error);
free_async_data (async_data);
}
GCancellable *
_gtk_file_system_get_info (GtkFileSystem *file_system,
GFile *file,
const gchar *attributes,
GtkFileSystemGetInfoCallback callback,
gpointer data)
{
GCancellable *cancellable;
AsyncFuncData *async_data;
g_return_val_if_fail (GTK_IS_FILE_SYSTEM (file_system), NULL);
g_return_val_if_fail (G_IS_FILE (file), NULL);
cancellable = g_cancellable_new ();
async_data = g_new0 (AsyncFuncData, 1);
async_data->file_system = g_object_ref (file_system);
async_data->file = g_object_ref (file);
async_data->cancellable = g_object_ref (cancellable);
async_data->callback = callback;
async_data->data = data;
g_file_query_info_async (file,
attributes,
G_FILE_QUERY_INFO_NONE,
G_PRIORITY_DEFAULT,
cancellable,
query_info_callback,
async_data);
return cancellable;
}
static void
drive_poll_for_media_cb (GObject *source_object,
GAsyncResult *result,
gpointer user_data)
{
AsyncFuncData *async_data;
GError *error = NULL;
g_drive_poll_for_media_finish (G_DRIVE (source_object), result, &error);
async_data = (AsyncFuncData *) user_data;
gdk_threads_enter ();
((GtkFileSystemVolumeMountCallback) async_data->callback) (async_data->cancellable,
(GtkFileSystemVolume *) source_object,
error, async_data->data);
gdk_threads_leave ();
if (error)
g_error_free (error);
}
static void
volume_mount_cb (GObject *source_object,
GAsyncResult *result,
gpointer user_data)
{
AsyncFuncData *async_data;
GError *error = NULL;
g_volume_mount_finish (G_VOLUME (source_object), result, &error);
async_data = (AsyncFuncData *) user_data;
gdk_threads_enter ();
((GtkFileSystemVolumeMountCallback) async_data->callback) (async_data->cancellable,
(GtkFileSystemVolume *) source_object,
error, async_data->data);
gdk_threads_leave ();
if (error)
g_error_free (error);
}
GCancellable *
_gtk_file_system_mount_volume (GtkFileSystem *file_system,
GtkFileSystemVolume *volume,
GMountOperation *mount_operation,
GtkFileSystemVolumeMountCallback callback,
gpointer data)
{
GCancellable *cancellable;
AsyncFuncData *async_data;
gboolean handled = FALSE;
DEBUG ("volume_mount");
cancellable = g_cancellable_new ();
async_data = g_new0 (AsyncFuncData, 1);
async_data->file_system = g_object_ref (file_system);
async_data->cancellable = g_object_ref (cancellable);
async_data->callback = callback;
async_data->data = data;
if (G_IS_DRIVE (volume))
{
/* this path happens for drives that are not polled by the OS and where the last media
* check indicated that no media was available. So the thing to do here is to
* invoke poll_for_media() on the drive
*/
g_drive_poll_for_media (G_DRIVE (volume), cancellable, drive_poll_for_media_cb, async_data);
handled = TRUE;
}
else if (G_IS_VOLUME (volume))
{
g_volume_mount (G_VOLUME (volume), G_MOUNT_MOUNT_NONE, mount_operation, cancellable, volume_mount_cb, async_data);
handled = TRUE;
}
if (!handled)
free_async_data (async_data);
return cancellable;
}
static void
enclosing_volume_mount_cb (GObject *source_object,
GAsyncResult *result,
gpointer user_data)
{
GtkFileSystemVolume *volume;
AsyncFuncData *async_data;
GError *error = NULL;
async_data = (AsyncFuncData *) user_data;
g_file_mount_enclosing_volume_finish (G_FILE (source_object), result, &error);
volume = _gtk_file_system_get_volume_for_file (async_data->file_system, G_FILE (source_object));
/* Silently drop G_IO_ERROR_ALREADY_MOUNTED error for gvfs backends without visible mounts. */
/* Better than doing query_info with additional I/O every time. */
if (error && g_error_matches (error, G_IO_ERROR, G_IO_ERROR_ALREADY_MOUNTED))
g_clear_error (&error);
gdk_threads_enter ();
((GtkFileSystemVolumeMountCallback) async_data->callback) (async_data->cancellable, volume,
error, async_data->data);
gdk_threads_leave ();
if (error)
g_error_free (error);
_gtk_file_system_volume_unref (volume);
}
GCancellable *
_gtk_file_system_mount_enclosing_volume (GtkFileSystem *file_system,
GFile *file,
GMountOperation *mount_operation,
GtkFileSystemVolumeMountCallback callback,
gpointer data)
{
GCancellable *cancellable;
AsyncFuncData *async_data;
g_return_val_if_fail (GTK_IS_FILE_SYSTEM (file_system), NULL);
g_return_val_if_fail (G_IS_FILE (file), NULL);
DEBUG ("mount_enclosing_volume");
cancellable = g_cancellable_new ();
async_data = g_new0 (AsyncFuncData, 1);
async_data->file_system = g_object_ref (file_system);
async_data->file = g_object_ref (file);
async_data->cancellable = g_object_ref (cancellable);
async_data->callback = callback;
async_data->data = data;
g_file_mount_enclosing_volume (file,
G_MOUNT_MOUNT_NONE,
mount_operation,
cancellable,
enclosing_volume_mount_cb,
async_data);
return cancellable;
}
gboolean
_gtk_file_system_insert_bookmark (GtkFileSystem *file_system,
GFile *file,
gint position,
GError **error)
{
GtkFileSystemPrivate *priv = file_system->priv;
GSList *bookmarks;
GtkFileSystemBookmark *bookmark;
gboolean result = TRUE;
GFile *bookmarks_file;
bookmarks = priv->bookmarks;
while (bookmarks)
{
bookmark = bookmarks->data;
bookmarks = bookmarks->next;
if (g_file_equal (bookmark->file, file))
{
/* File is already in bookmarks */
result = FALSE;
break;
}
}
if (!result)
{
gchar *uri = g_file_get_uri (file);
g_set_error (error,
GTK_FILE_CHOOSER_ERROR,
GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
"%s already exists in the bookmarks list",
uri);
g_free (uri);
return FALSE;
}
bookmark = g_slice_new0 (GtkFileSystemBookmark);
bookmark->file = g_object_ref (file);
priv->bookmarks = g_slist_insert (priv->bookmarks, bookmark, position);
bookmarks_file = get_bookmarks_file ();
save_bookmarks (bookmarks_file, priv->bookmarks);
g_object_unref (bookmarks_file);
g_signal_emit (file_system, fs_signals[BOOKMARKS_CHANGED], 0);
return TRUE;
}
gboolean
_gtk_file_system_remove_bookmark (GtkFileSystem *file_system,
GFile *file,
GError **error)
{
GtkFileSystemPrivate *priv = file_system->priv;
GtkFileSystemBookmark *bookmark;
GSList *bookmarks;
gboolean result = FALSE;
GFile *bookmarks_file;
if (!priv->bookmarks)
return FALSE;
bookmarks = priv->bookmarks;
while (bookmarks)
{
bookmark = bookmarks->data;
if (g_file_equal (bookmark->file, file))
{
result = TRUE;
priv->bookmarks = g_slist_remove_link (priv->bookmarks, bookmarks);
_gtk_file_system_bookmark_free (bookmark);
g_slist_free_1 (bookmarks);
break;
}
bookmarks = bookmarks->next;
}
if (!result)
{
gchar *uri = g_file_get_uri (file);
g_set_error (error,
GTK_FILE_CHOOSER_ERROR,
GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
"%s does not exist in the bookmarks list",
uri);
g_free (uri);
return FALSE;
}
bookmarks_file = get_bookmarks_file ();
save_bookmarks (bookmarks_file, priv->bookmarks);
g_object_unref (bookmarks_file);
g_signal_emit (file_system, fs_signals[BOOKMARKS_CHANGED], 0);
return TRUE;
}
gchar *
_gtk_file_system_get_bookmark_label (GtkFileSystem *file_system,
GFile *file)
{
GtkFileSystemPrivate *priv = file_system->priv;
GSList *bookmarks;
gchar *label = NULL;
DEBUG ("get_bookmark_label");
bookmarks = priv->bookmarks;
while (bookmarks)
{
GtkFileSystemBookmark *bookmark;
bookmark = bookmarks->data;
bookmarks = bookmarks->next;
if (g_file_equal (file, bookmark->file))
{
label = g_strdup (bookmark->label);
break;
}
}
return label;
}
void
_gtk_file_system_set_bookmark_label (GtkFileSystem *file_system,
GFile *file,
const gchar *label)
{
GtkFileSystemPrivate *priv = file_system->priv;
gboolean changed = FALSE;
GFile *bookmarks_file;
GSList *bookmarks;
DEBUG ("set_bookmark_label");
bookmarks = priv->bookmarks;
while (bookmarks)
{
GtkFileSystemBookmark *bookmark;
bookmark = bookmarks->data;
bookmarks = bookmarks->next;
if (g_file_equal (file, bookmark->file))
{
g_free (bookmark->label);
bookmark->label = g_strdup (label);
changed = TRUE;
break;
}
}
bookmarks_file = get_bookmarks_file ();
save_bookmarks (bookmarks_file, priv->bookmarks);
g_object_unref (bookmarks_file);
if (changed)
g_signal_emit_by_name (file_system, "bookmarks-changed", 0);
}
GtkFileSystemVolume *
_gtk_file_system_get_volume_for_file (GtkFileSystem *file_system,
GFile *file)
{
GMount *mount;
DEBUG ("get_volume_for_file");
mount = g_file_find_enclosing_mount (file, NULL, NULL);
if (!mount && g_file_is_native (file))
return (GtkFileSystemVolume *) root_volume_token;
return (GtkFileSystemVolume *) mount;
}
/* GtkFileSystemVolume public methods */
gchar *
_gtk_file_system_volume_get_display_name (GtkFileSystemVolume *volume)
{
DEBUG ("volume_get_display_name");
if (IS_ROOT_VOLUME (volume))
return g_strdup (_(root_volume_token));
if (G_IS_DRIVE (volume))
return g_drive_get_name (G_DRIVE (volume));
else if (G_IS_MOUNT (volume))
return g_mount_get_name (G_MOUNT (volume));
else if (G_IS_VOLUME (volume))
return g_volume_get_name (G_VOLUME (volume));
return NULL;
}
gboolean
_gtk_file_system_volume_is_mounted (GtkFileSystemVolume *volume)
{
gboolean mounted;
DEBUG ("volume_is_mounted");
if (IS_ROOT_VOLUME (volume))
return TRUE;
mounted = FALSE;
if (G_IS_MOUNT (volume))
mounted = TRUE;
else if (G_IS_VOLUME (volume))
{
GMount *mount;
mount = g_volume_get_mount (G_VOLUME (volume));
if (mount)
{
mounted = TRUE;
g_object_unref (mount);
}
}
return mounted;
}
GFile *
_gtk_file_system_volume_get_root (GtkFileSystemVolume *volume)
{
GFile *file = NULL;
DEBUG ("volume_get_base");
if (IS_ROOT_VOLUME (volume))
return g_file_new_for_uri ("file:///");
if (G_IS_MOUNT (volume))
file = g_mount_get_root (G_MOUNT (volume));
else if (G_IS_VOLUME (volume))
{
GMount *mount;
mount = g_volume_get_mount (G_VOLUME (volume));
if (mount)
{
file = g_mount_get_root (mount);
g_object_unref (mount);
}
}
return file;
}
static GdkPixbuf *
get_pixbuf_from_gicon (GIcon *icon,
GtkWidget *widget,
gint icon_size,
GError **error)
{
GdkScreen *screen;
GtkIconTheme *icon_theme;
GtkIconInfo *icon_info;
GdkPixbuf *pixbuf;
screen = gtk_widget_get_screen (GTK_WIDGET (widget));
icon_theme = gtk_icon_theme_get_for_screen (screen);
icon_info = gtk_icon_theme_lookup_by_gicon (icon_theme,
icon,
icon_size,
GTK_ICON_LOOKUP_USE_BUILTIN);
if (!icon_info)
return NULL;
pixbuf = gtk_icon_info_load_icon (icon_info, error);
gtk_icon_info_free (icon_info);
return pixbuf;
}
GdkPixbuf *
_gtk_file_system_volume_render_icon (GtkFileSystemVolume *volume,
GtkWidget *widget,
gint icon_size,
GError **error)
{
GIcon *icon = NULL;
GdkPixbuf *pixbuf;
DEBUG ("volume_get_icon_name");
if (IS_ROOT_VOLUME (volume))
icon = g_themed_icon_new ("drive-harddisk");
else if (G_IS_DRIVE (volume))
icon = g_drive_get_icon (G_DRIVE (volume));
else if (G_IS_VOLUME (volume))
icon = g_volume_get_icon (G_VOLUME (volume));
else if (G_IS_MOUNT (volume))
icon = g_mount_get_icon (G_MOUNT (volume));
if (!icon)
return NULL;
pixbuf = get_pixbuf_from_gicon (icon, widget, icon_size, error);
g_object_unref (icon);
return pixbuf;
}
GtkFileSystemVolume *
_gtk_file_system_volume_ref (GtkFileSystemVolume *volume)
{
if (IS_ROOT_VOLUME (volume))
return volume;
if (G_IS_MOUNT (volume) ||
G_IS_VOLUME (volume) ||
G_IS_DRIVE (volume))
g_object_ref (volume);
return volume;
}
void
_gtk_file_system_volume_unref (GtkFileSystemVolume *volume)
{
/* Root volume doesn't need to be freed */
if (IS_ROOT_VOLUME (volume))
return;
if (G_IS_MOUNT (volume) ||
G_IS_VOLUME (volume) ||
G_IS_DRIVE (volume))
g_object_unref (volume);
}
/* GFileInfo helper functions */
GdkPixbuf *
_gtk_file_info_render_icon (GFileInfo *info,
GtkWidget *widget,
gint icon_size)
{
GIcon *icon;
GdkPixbuf *pixbuf = NULL;
const gchar *thumbnail_path;
thumbnail_path = g_file_info_get_attribute_byte_string (info, G_FILE_ATTRIBUTE_THUMBNAIL_PATH);
if (thumbnail_path)
pixbuf = gdk_pixbuf_new_from_file_at_size (thumbnail_path,
icon_size, icon_size,
NULL);
if (!pixbuf)
{
icon = g_file_info_get_icon (info);
if (icon)
pixbuf = get_pixbuf_from_gicon (icon, widget, icon_size, NULL);
if (!pixbuf)
{
/* Use general fallback for all files without icon */
icon = g_themed_icon_new ("text-x-generic");
pixbuf = get_pixbuf_from_gicon (icon, widget, icon_size, NULL);
g_object_unref (icon);
}
}
return pixbuf;
}
gboolean
_gtk_file_info_consider_as_directory (GFileInfo *info)
{
GFileType type = g_file_info_get_file_type (info);
return (type == G_FILE_TYPE_DIRECTORY ||
type == G_FILE_TYPE_MOUNTABLE ||
type == G_FILE_TYPE_SHORTCUT);
}
|