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
|
/* $Id$ */
/*-
* Copyright (c) 2005 Benedikt Meurer <benny@xfce.org>
*
* 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 of the License, 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
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#include <thunar/thunar-file.h>
#include <thunar/thunar-icon-factory.h>
#include <thunar/thunar-local-file.h>
#include <thunar/thunar-trash-folder.h>
enum
{
CHANGED,
LAST_SIGNAL,
};
static void thunar_file_class_init (ThunarFileClass *klass);
static void thunar_file_finalize (GObject *object);
static ThunarFolder*thunar_file_real_open_as_folder (ThunarFile *file,
GError **error);
static const gchar *thunar_file_real_get_special_name (ThunarFile *file);
static void thunar_file_real_changed (ThunarFile *file);
static void thunar_file_destroyed (gpointer data,
GObject *object);
static GObjectClass *thunar_file_parent_class;
static GHashTable *file_cache;
static guint file_signals[LAST_SIGNAL];
GType
thunar_file_get_type (void)
{
static GType type = G_TYPE_INVALID;
if (G_UNLIKELY (type == G_TYPE_INVALID))
{
static const GTypeInfo info =
{
sizeof (ThunarFileClass),
NULL,
NULL,
(GClassInitFunc) thunar_file_class_init,
NULL,
NULL,
sizeof (ThunarFile),
0,
NULL,
};
type = g_type_register_static (GTK_TYPE_OBJECT,
"ThunarFile", &info,
G_TYPE_FLAG_ABSTRACT);
}
return type;
}
static void
thunar_file_class_init (ThunarFileClass *klass)
{
GObjectClass *gobject_class;
thunar_file_parent_class = g_type_class_peek_parent (klass);
gobject_class = G_OBJECT_CLASS (klass);
gobject_class->finalize = thunar_file_finalize;
klass->open_as_folder = thunar_file_real_open_as_folder;
klass->get_special_name = thunar_file_real_get_special_name;
klass->changed = thunar_file_real_changed;
/**
* ThunarFile::changed:
* @file : the #ThunarFile instance.
*
* Emitted whenever the system notices a change to @file.
**/
file_signals[CHANGED] =
g_signal_new ("changed",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (ThunarFileClass, changed),
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
}
static void
thunar_file_finalize (GObject *object)
{
ThunarFile *file = THUNAR_FILE (object);
/* destroy the cached icon if any */
if (G_LIKELY (file->cached_icon != NULL))
g_object_unref (G_OBJECT (file->cached_icon));
G_OBJECT_CLASS (thunar_file_parent_class)->finalize (object);
}
static ThunarFolder*
thunar_file_real_open_as_folder (ThunarFile *file,
GError **error)
{
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_NOTDIR, g_strerror (ENOTDIR));
return NULL;
}
static const gchar*
thunar_file_real_get_special_name (ThunarFile *file)
{
return THUNAR_FILE_GET_CLASS (file)->get_display_name (file);
}
static void
thunar_file_real_changed (ThunarFile *file)
{
/* destroy any cached icon, as we need to reload it */
if (G_LIKELY (file->cached_icon != NULL))
{
g_object_unref (G_OBJECT (file->cached_icon));
file->cached_icon = NULL;
}
}
static ThunarFile*
thunar_file_new_internal (ThunarVfsURI *uri,
GError **error)
{
switch (thunar_vfs_uri_get_scheme (uri))
{
case THUNAR_VFS_URI_SCHEME_FILE: return thunar_local_file_new (uri, error);
case THUNAR_VFS_URI_SCHEME_TRASH: return thunar_trash_folder_new (uri, error);
}
g_assert_not_reached ();
return NULL;
}
static void
thunar_file_destroyed (gpointer data,
GObject *object)
{
ThunarVfsURI *uri = THUNAR_VFS_URI (data);
g_return_if_fail (THUNAR_VFS_IS_URI (uri));
/* drop the entry from the cache */
g_hash_table_remove (file_cache, uri);
/* drop the reference on the uri */
g_object_unref (G_OBJECT (uri));
}
/**
* thunar_file_get_for_uri:
* @uri : an #ThunarVfsURI instance.
* @error : error return location.
*
* Tries to query the file referred to by @uri. Returns %NULL
* if the file could not be queried and @error will point
* to an error describing the problem, else the #ThunarFile
* instance will be returned, which needs to freed using
* #g_object_unref() when no longer needed.
*
* Note that this function is not thread-safe and may only
* be called from the main thread.
*
* Return value: the #ThunarFile instance or %NULL on error.
**/
ThunarFile*
thunar_file_get_for_uri (ThunarVfsURI *uri,
GError **error)
{
ThunarFile *file;
g_return_val_if_fail (THUNAR_VFS_IS_URI (uri), NULL);
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
/* allocate the ThunarFile cache on-demand */
if (G_UNLIKELY (file_cache == NULL))
{
file_cache = g_hash_table_new (thunar_vfs_uri_hash,
thunar_vfs_uri_equal);
}
/* see if we have the corresponding file cached already */
file = g_hash_table_lookup (file_cache, uri);
if (file == NULL)
{
/* allocate the new file object */
file = thunar_file_new_internal (uri, error);
if (G_LIKELY (file != NULL))
{
/* drop the floating reference */
g_assert (GTK_OBJECT_FLOATING (file));
g_object_ref (G_OBJECT (file));
gtk_object_sink (GTK_OBJECT (file));
/* insert the file into the cache */
g_object_weak_ref (G_OBJECT (file), thunar_file_destroyed, uri);
g_hash_table_insert (file_cache, uri, file);
g_object_ref (G_OBJECT (uri));
}
}
else
{
/* take another reference on the cached file */
g_object_ref (G_OBJECT (file));
}
return file;
}
/**
* thunar_file_get_parent:
* @file : a #ThunarFile instance.
* @error : return location for errors.
*
* Queries the parent #ThunarFile (the directory, which includes @file)
* for @file. If an error happens, %NULL will be returned and @error
* will be set to point to a #GError. Else the #ThunarFile will
* be returned. The method will automatically take a reference
* on the returned file for the caller, so you'll have to call
* #g_object_unref() when you're done with the returned file object.
*
* There's one special case to take care of: If @file refers to the
* root directory ("/"), %NULL will be returned, but @error won't
* be set. You'll have to handle this case gracefully.
*
* Return value: the parent #ThunarFile or %NULL.
**/
ThunarFile*
thunar_file_get_parent (ThunarFile *file,
GError **error)
{
ThunarVfsURI *parent_uri;
ThunarFile *parent_file;
g_return_val_if_fail (THUNAR_IS_FILE (file), NULL);
/* lookup the parent's URI */
parent_uri = thunar_vfs_uri_parent (thunar_file_get_uri (file));
if (G_UNLIKELY (parent_uri == NULL))
{
/* file has no parent (e.g. "/") */
return NULL;
}
/* lookup the file object for the parent_uri */
parent_file = thunar_file_get_for_uri (parent_uri, error);
/* release the reference on the parent_uri */
g_object_unref (G_OBJECT (parent_uri));
return parent_file;
}
/**
* thunar_file_open_as_folder:
* @file : a #ThunarFile instance.
* @error : return location for errors or %NULL.
*
* Tries to open the #ThunarFolder instance corresponding to @file. If @file
* does not refer to a folder in any way or you don't have permission to open
* the @file as a folder, %NULL will be returned and @error will be appropriately.
*
* You'll need to call #g_object_unref() on the returned instance when you're
* done with it.
*
* Return value: the #ThunarFolder corresponding to @file or %NULL.
**/
ThunarFolder*
thunar_file_open_as_folder (ThunarFile *file,
GError **error)
{
g_return_val_if_fail (THUNAR_IS_FILE (file), NULL);
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
return THUNAR_FILE_GET_CLASS (file)->open_as_folder (file, error);
}
/**
* thunar_file_get_uri:
* @file : a #ThunarFile instance.
*
* Returns the #ThunarVfsURI, that refers to the location of the @file.
*
* This method cannot return %NULL, unless there's a bug in the
* application. So you should never check the returned value for
* a possible %NULL pointer, except in the context of a #g_return_if_fail()
* or #g_assert() macro, which will not be compiled into the final
* binary.
*
* Note, that there's no reference taken for the caller on the
* returned #ThunarVfsURI, so if you need the object for a longer
* period, you'll need to take a reference yourself using the
* #g_object_ref() function.
*
* Return value: the URI to the @file.
**/
ThunarVfsURI*
thunar_file_get_uri (ThunarFile *file)
{
g_return_val_if_fail (THUNAR_IS_FILE (file), NULL);
return THUNAR_FILE_GET_CLASS (file)->get_uri (file);
}
/**
* thunar_file_get_mime_info:
* @file : a #ThunarFile instance.
*
* Returns the MIME type information for the given @file object. This
* function may return %NULL if it is unable to determine a MIME type.
* Therefore your component must be able to handle this case!
*
* Note, that there's no reference taken for the caller on the
* returned #ExoMimeInfo, so if you need the object for a longer
* period, you'll need to take a reference yourself using the
* #g_object_ref() function.
*
* Return value: the MIME type or %NULL.
**/
ExoMimeInfo*
thunar_file_get_mime_info (ThunarFile *file)
{
g_return_val_if_fail (THUNAR_IS_FILE (file), NULL);
return THUNAR_FILE_GET_CLASS (file)->get_mime_info (file);
}
/**
* thunar_file_get_display_name:
* @file : a #ThunarFile instance.
*
* Returns the @file name in the UTF-8 encoding, which is
* suitable for displaying the file name in the GUI.
*
* Return value: the @file name suitable for display.
**/
const gchar*
thunar_file_get_display_name (ThunarFile *file)
{
g_return_val_if_fail (THUNAR_IS_FILE (file), NULL);
return THUNAR_FILE_GET_CLASS (file)->get_display_name (file);
}
/**
* thunar_file_get_special_name:
* @file : a #ThunarFile instance.
*
* Returns the special name for the @file, e.g. "Filesystem" for the #ThunarFile
* referring to "/" and so on. If there's no special name for this @file, the
* value of the "display-name" property will be returned instead.
*
* Return value: the special name of @file.
**/
const gchar*
thunar_file_get_special_name (ThunarFile *file)
{
g_return_val_if_fail (THUNAR_IS_FILE (file), NULL);
return THUNAR_FILE_GET_CLASS (file)->get_special_name (file);
}
/**
* thunar_file_get_kind:
* @file : a #ThunarFile instance.
*
* Returns the kind of @file.
*
* Return value: the kind of @file.
**/
ThunarVfsFileType
thunar_file_get_kind (ThunarFile *file)
{
g_return_val_if_fail (THUNAR_IS_FILE (file), THUNAR_VFS_FILE_TYPE_UNKNOWN);
return THUNAR_FILE_GET_CLASS (file)->get_kind (file);
}
/**
* thunar_file_get_mode:
* @file : a #ThunarFile instance.
*
* Returns the permission bits of @file.
*
* Return value: the permission bits of @file.
**/
ThunarVfsFileMode
thunar_file_get_mode (ThunarFile *file)
{
g_return_val_if_fail (THUNAR_IS_FILE (file), 0);
return THUNAR_FILE_GET_CLASS (file)->get_mode (file);
}
/**
* thunar_file_get_size:
* @file : a #ThunarFile instance.
*
* Returns the size of @file in bytes.
*
* Return value: the size of @file in bytes.
**/
ThunarVfsFileSize
thunar_file_get_size (ThunarFile *file)
{
g_return_val_if_fail (THUNAR_IS_FILE (file), 0);
return THUNAR_FILE_GET_CLASS (file)->get_size (file);
}
/**
* thunar_file_get_mode_string:
* @file : a #ThunarFile instance.
*
* Returns the mode of @file as text. You'll need to free
* the result using #g_free() when you're done with it.
*
* Return value: the mode of @file as string.
**/
gchar*
thunar_file_get_mode_string (ThunarFile *file)
{
ThunarVfsFileType kind;
ThunarVfsFileMode mode;
gchar *text;
g_return_val_if_fail (THUNAR_IS_FILE (file), NULL);
kind = THUNAR_FILE_GET_CLASS (file)->get_kind (file);
mode = THUNAR_FILE_GET_CLASS (file)->get_mode (file);
text = g_new (gchar, 11);
/* file type */
switch (kind)
{
case THUNAR_VFS_FILE_TYPE_SOCKET: text[0] = 's'; break;
case THUNAR_VFS_FILE_TYPE_SYMLINK: text[0] = 'l'; break;
case THUNAR_VFS_FILE_TYPE_REGULAR: text[0] = '-'; break;
case THUNAR_VFS_FILE_TYPE_BLOCKDEV: text[0] = 'b'; break;
case THUNAR_VFS_FILE_TYPE_DIRECTORY: text[0] = 'd'; break;
case THUNAR_VFS_FILE_TYPE_CHARDEV: text[0] = 'c'; break;
case THUNAR_VFS_FILE_TYPE_FIFO: text[0] = 'f'; break;
case THUNAR_VFS_FILE_TYPE_UNKNOWN: text[0] = ' '; break;
default: g_assert_not_reached ();
}
/* permission flags */
text[1] = (mode & THUNAR_VFS_FILE_MODE_USR_READ) ? 'r' : '-';
text[2] = (mode & THUNAR_VFS_FILE_MODE_USR_WRITE) ? 'w' : '-';
text[3] = (mode & THUNAR_VFS_FILE_MODE_USR_EXEC) ? 'x' : '-';
text[4] = (mode & THUNAR_VFS_FILE_MODE_GRP_READ) ? 'r' : '-';
text[5] = (mode & THUNAR_VFS_FILE_MODE_GRP_WRITE) ? 'w' : '-';
text[6] = (mode & THUNAR_VFS_FILE_MODE_GRP_EXEC) ? 'x' : '-';
text[7] = (mode & THUNAR_VFS_FILE_MODE_OTH_READ) ? 'r' : '-';
text[8] = (mode & THUNAR_VFS_FILE_MODE_OTH_WRITE) ? 'w' : '-';
text[9] = (mode & THUNAR_VFS_FILE_MODE_OTH_EXEC) ? 'x' : '-';
/* special flags */
if (G_UNLIKELY (mode & THUNAR_VFS_FILE_MODE_SUID))
text[3] = 's';
if (G_UNLIKELY (mode & THUNAR_VFS_FILE_MODE_SGID))
text[6] = 's';
if (G_UNLIKELY (mode & THUNAR_VFS_FILE_MODE_STICKY))
text[9] = 't';
text[10] = '\0';
return text;
}
/**
* thunar_file_get_size_string:
* @file : a #ThunarFile instance.
*
* Returns the size of the file as text in a human readable
* format. You'll need to free the result using #g_free()
* if you're done with it.
*
* Return value: the size of @file in a human readable
* format.
**/
gchar*
thunar_file_get_size_string (ThunarFile *file)
{
g_return_val_if_fail (THUNAR_IS_FILE (file), NULL);
return thunar_vfs_humanize_size (THUNAR_FILE_GET_CLASS (file)->get_size (file), NULL, 0);
}
/**
* thunar_file_load_icon:
* @file : a #ThunarFile instance.
* @size : the icon size in pixels.
*
* You need to call #g_object_unref() on the returned icon
* when you don't need it any longer.
*
* Return value: the icon for @file at @size.
**/
GdkPixbuf*
thunar_file_load_icon (ThunarFile *file,
gint size)
{
ThunarIconFactory *icon_factory;
GtkIconTheme *icon_theme;
const gchar *icon_name;
g_return_val_if_fail (THUNAR_IS_FILE (file), NULL);
/* see if we have a cached icon that matches the request */
if (file->cached_icon != NULL && file->cached_size == size)
{
/* take a reference on the cached icon for the caller */
g_object_ref (G_OBJECT (file->cached_icon));
return file->cached_icon;
}
icon_factory = thunar_icon_factory_get_default ();
icon_theme = thunar_icon_factory_get_icon_theme (icon_factory);
icon_name = THUNAR_FILE_GET_CLASS (file)->get_icon_name (file, icon_theme);
return thunar_icon_factory_load_icon (icon_factory, icon_name, size, NULL, TRUE);
}
/**
* thunar_file_changed:
* @file : a #ThunarFile instance.
*
* Emits the ::changed signal on @file. This function is meant to be called
* by derived classes whenever they notice changes to the @file.
**/
void
thunar_file_changed (ThunarFile *file)
{
g_return_if_fail (THUNAR_IS_FILE (file));
g_signal_emit (G_OBJECT (file), file_signals[CHANGED], 0);
}
/**
* thunar_file_is_hidden:
* @file : a #ThunarFile instance.
*
* Checks whether @file can be considered a hidden file.
*
* Return value: %TRUE if @file is a hidden file, else %FALSE.
**/
gboolean
thunar_file_is_hidden (ThunarFile *file)
{
const gchar *p;
g_return_val_if_fail (THUNAR_IS_FILE (file), FALSE);
p = THUNAR_FILE_GET_CLASS (file)->get_display_name (file);
if (*p != '.' && *p != '\0')
{
for (; p[1] != '\0'; ++p)
;
return (*p == '~');
}
return TRUE;
}
|