~noskcaj/ubuntu/vivid/thunar/1.6.4

« back to all changes in this revision

Viewing changes to thunar/thunar-browser.c

  • Committer: Bazaar Package Importer
  • Author(s): Lionel Le Folgoc
  • Date: 2010-12-04 16:46:20 UTC
  • mto: (2.1.3 experimental) (1.3.1)
  • mto: This revision was merged to the branch mainline in revision 69.
  • Revision ID: james.westby@ubuntu.com-20101204164620-h7p4t2e9z6hfhz6l
Tags: upstream-1.1.4
ImportĀ upstreamĀ versionĀ 1.1.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* vi:set et ai sw=2 sts=2 ts=2: */
 
2
/*-
 
3
 * Copyright (c) 2009 Jannis Pohlmann <jannis@xfce.org>
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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.
 
19
 */
 
20
 
 
21
#ifdef HAVE_CONFIG_H
 
22
#include <config.h>
 
23
#endif
 
24
 
 
25
#include <glib-object.h>
 
26
 
 
27
#include <thunar/thunar-browser.h>
 
28
#include <thunar/thunar-file.h>
 
29
#include <thunar/thunar-private.h>
 
30
#include <thunar/thunar-util.h>
 
31
 
 
32
 
 
33
 
 
34
typedef struct _PokeFileData   PokeFileData;
 
35
typedef struct _PokeVolumeData PokeVolumeData;
 
36
 
 
37
struct _PokeFileData
 
38
{
 
39
  ThunarBrowser            *browser;
 
40
  ThunarFile               *source;
 
41
  ThunarFile               *file;
 
42
  ThunarBrowserPokeFileFunc func;
 
43
  gpointer                  user_data;
 
44
};
 
45
 
 
46
struct _PokeVolumeData
 
47
{
 
48
  ThunarBrowser              *browser;
 
49
  GVolume                    *volume;
 
50
  ThunarFile                 *mount_point;
 
51
  ThunarBrowserPokeVolumeFunc func;
 
52
  gpointer                    user_data;
 
53
};
 
54
 
 
55
 
 
56
 
 
57
GType
 
58
thunar_browser_get_type (void)
 
59
{
 
60
  static volatile gsize type__volatile = 0;
 
61
  GType                 type;
 
62
 
 
63
  if (g_once_init_enter (&type__volatile))
 
64
    {
 
65
      type = g_type_register_static_simple (G_TYPE_INTERFACE,
 
66
                                            I_("ThunarBrowser"),
 
67
                                            sizeof (ThunarBrowserIface),
 
68
                                            NULL,
 
69
                                            0,
 
70
                                            NULL,
 
71
                                            0);
 
72
 
 
73
      g_type_interface_add_prerequisite (type, G_TYPE_OBJECT);
 
74
 
 
75
      g_once_init_leave (&type__volatile, type);
 
76
    }
 
77
 
 
78
  return type__volatile;
 
79
}
 
80
 
 
81
 
 
82
 
 
83
static PokeFileData *
 
84
thunar_browser_poke_file_data_new (ThunarBrowser            *browser,
 
85
                                   ThunarFile               *source,
 
86
                                   ThunarFile               *file,
 
87
                                   ThunarBrowserPokeFileFunc func,
 
88
                                   gpointer                  user_data)
 
89
{
 
90
  PokeFileData *poke_data;
 
91
 
 
92
  _thunar_return_val_if_fail (THUNAR_IS_BROWSER (browser), NULL);
 
93
  _thunar_return_val_if_fail (THUNAR_IS_FILE (source), NULL);
 
94
  _thunar_return_val_if_fail (THUNAR_IS_FILE (file), NULL);
 
95
 
 
96
  poke_data = g_slice_new0 (PokeFileData);
 
97
  poke_data->browser = g_object_ref (browser);
 
98
  poke_data->source = g_object_ref (source);
 
99
  poke_data->file = g_object_ref (file);
 
100
  poke_data->func = func;
 
101
  poke_data->user_data = user_data;
 
102
 
 
103
  return poke_data;
 
104
}
 
105
 
 
106
 
 
107
 
 
108
static void
 
109
thunar_browser_poke_file_data_free (PokeFileData *poke_data)
 
110
{
 
111
  _thunar_return_if_fail (poke_data != NULL);
 
112
  _thunar_return_if_fail (THUNAR_IS_BROWSER (poke_data->browser));
 
113
  _thunar_return_if_fail (THUNAR_IS_FILE (poke_data->source));
 
114
  _thunar_return_if_fail (THUNAR_IS_FILE (poke_data->file));
 
115
 
 
116
  g_object_unref (poke_data->browser);
 
117
  g_object_unref (poke_data->source);
 
118
  g_object_unref (poke_data->file);
 
119
 
 
120
  g_slice_free (PokeFileData, poke_data);
 
121
}
 
122
 
 
123
 
 
124
 
 
125
static PokeVolumeData *
 
126
thunar_browser_poke_volume_data_new (ThunarBrowser              *browser,
 
127
                                     GVolume                    *volume,
 
128
                                     ThunarBrowserPokeVolumeFunc func,
 
129
                                     gpointer                    user_data)
 
130
{
 
131
  PokeVolumeData *poke_data;
 
132
 
 
133
  _thunar_return_val_if_fail (THUNAR_IS_BROWSER (browser), NULL);
 
134
  _thunar_return_val_if_fail (G_IS_VOLUME (volume), NULL);
 
135
 
 
136
  poke_data = g_slice_new0 (PokeVolumeData);
 
137
  poke_data->browser = g_object_ref (browser);
 
138
  poke_data->volume = g_object_ref (volume);
 
139
  poke_data->func = func;
 
140
  poke_data->user_data = user_data;
 
141
 
 
142
  return poke_data;
 
143
}
 
144
 
 
145
 
 
146
 
 
147
static void
 
148
thunar_browser_poke_volume_data_free (PokeVolumeData *poke_data)
 
149
{
 
150
  _thunar_return_if_fail (poke_data != NULL);
 
151
  _thunar_return_if_fail (THUNAR_IS_BROWSER (poke_data->browser));
 
152
  _thunar_return_if_fail (G_IS_VOLUME (poke_data->volume));
 
153
 
 
154
  g_object_unref (poke_data->browser);
 
155
  g_object_unref (poke_data->volume);
 
156
 
 
157
  g_slice_free (PokeVolumeData, poke_data);
 
158
}
 
159
 
 
160
 
 
161
 
 
162
static GMountOperation *
 
163
thunar_browser_mount_operation_new (gpointer parent)
 
164
{
 
165
  GMountOperation *mount_operation;
 
166
  GtkWindow       *window = NULL;
 
167
  GdkScreen       *screen = NULL;
 
168
 
 
169
  mount_operation = gtk_mount_operation_new (NULL);
 
170
 
 
171
  screen = thunar_util_parse_parent (parent, &window);
 
172
  gtk_mount_operation_set_screen (GTK_MOUNT_OPERATION (mount_operation), screen);
 
173
  gtk_mount_operation_set_parent (GTK_MOUNT_OPERATION (mount_operation), window);
 
174
 
 
175
  return mount_operation;
 
176
}
 
177
 
 
178
 
 
179
 
 
180
static void
 
181
thunar_browser_poke_mountable_finish (GObject      *object,
 
182
                                      GAsyncResult *result,
 
183
                                      gpointer      user_data)
 
184
{
 
185
  PokeFileData *poke_data = user_data;
 
186
  ThunarFile   *target = NULL;
 
187
  GError       *error = NULL;
 
188
  GFile        *location;
 
189
 
 
190
  _thunar_return_if_fail (G_IS_FILE (object));
 
191
  _thunar_return_if_fail (G_IS_ASYNC_RESULT (result));
 
192
  _thunar_return_if_fail (user_data != NULL);
 
193
  _thunar_return_if_fail (THUNAR_IS_BROWSER (poke_data->browser));
 
194
  _thunar_return_if_fail (THUNAR_IS_FILE (poke_data->file));
 
195
 
 
196
  if (!g_file_mount_mountable_finish (G_FILE (object), result, &error))
 
197
    {
 
198
      if (error->domain == G_IO_ERROR)
 
199
        {
 
200
          if (error->code == G_IO_ERROR_ALREADY_MOUNTED)
 
201
            g_clear_error (&error);
 
202
        }
 
203
    }
 
204
 
 
205
  if (error == NULL)
 
206
    {
 
207
      thunar_file_reload (poke_data->file);
 
208
 
 
209
      location = thunar_file_get_target_location (poke_data->file);
 
210
      target = thunar_file_get (location, &error);
 
211
      g_object_unref (location);
 
212
    }
 
213
 
 
214
  if (poke_data->func != NULL)
 
215
    {
 
216
      (poke_data->func) (poke_data->browser, poke_data->source, target, error, 
 
217
                         poke_data->user_data);
 
218
    }
 
219
 
 
220
  g_clear_error (&error);
 
221
 
 
222
  if (target != NULL)
 
223
    g_object_unref (target);
 
224
 
 
225
  thunar_browser_poke_file_data_free (poke_data);
 
226
}
 
227
 
 
228
 
 
229
 
 
230
static void
 
231
thunar_browser_poke_file_finish (GObject      *object,
 
232
                                 GAsyncResult *result,
 
233
                                 gpointer      user_data)
 
234
{
 
235
  PokeFileData *poke_data = user_data;
 
236
  GError       *error = NULL;
 
237
 
 
238
  _thunar_return_if_fail (G_IS_FILE (object));
 
239
  _thunar_return_if_fail (G_IS_ASYNC_RESULT (result));
 
240
  _thunar_return_if_fail (user_data != NULL);
 
241
  _thunar_return_if_fail (THUNAR_IS_BROWSER (poke_data->browser));
 
242
  _thunar_return_if_fail (THUNAR_IS_FILE (poke_data->file));
 
243
 
 
244
  if (!g_file_mount_enclosing_volume_finish (G_FILE (object), result, &error))
 
245
    {
 
246
      if (error->domain == G_IO_ERROR)
 
247
        {
 
248
          if (error->code == G_IO_ERROR_ALREADY_MOUNTED)
 
249
            g_clear_error (&error);
 
250
        }
 
251
    }
 
252
 
 
253
  if (error == NULL)
 
254
    thunar_file_reload (poke_data->file);
 
255
 
 
256
  if (poke_data->func != NULL)
 
257
    {
 
258
      if (error == NULL)
 
259
        {
 
260
          (poke_data->func) (poke_data->browser, poke_data->source, poke_data->file, 
 
261
                             NULL, poke_data->user_data);
 
262
        }
 
263
      else
 
264
        {
 
265
          (poke_data->func) (poke_data->browser, poke_data->source, NULL, error,
 
266
                             poke_data->user_data);
 
267
        }
 
268
    }
 
269
 
 
270
  g_clear_error (&error);
 
271
 
 
272
  thunar_browser_poke_file_data_free (poke_data);
 
273
}
 
274
 
 
275
 
 
276
 
 
277
static void
 
278
thunar_browser_poke_file_internal (ThunarBrowser            *browser,
 
279
                                   ThunarFile               *source,
 
280
                                   ThunarFile               *file,
 
281
                                   gpointer                  widget,
 
282
                                   ThunarBrowserPokeFileFunc func,
 
283
                                   gpointer                  user_data)
 
284
{
 
285
  GMountOperation *mount_operation;
 
286
  ThunarFile      *target;
 
287
  PokeFileData    *poke_data;
 
288
  GError          *error = NULL;
 
289
  GFile           *location;
 
290
 
 
291
  _thunar_return_if_fail (THUNAR_IS_BROWSER (browser));
 
292
  _thunar_return_if_fail (THUNAR_IS_FILE (source));
 
293
  _thunar_return_if_fail (THUNAR_IS_FILE (file));
 
294
 
 
295
  if (thunar_file_get_kind (file) == G_FILE_TYPE_SHORTCUT)
 
296
    {
 
297
      location = thunar_file_get_target_location (file);
 
298
      target = thunar_file_get (location, &error);
 
299
      g_object_unref (location);
 
300
 
 
301
      if (target != NULL)
 
302
        {
 
303
          /* TODO in very rare occasions (shortcut X -> other shortcut -> shortcut X),
 
304
           * this can lead to endless recursion  */
 
305
          thunar_browser_poke_file_internal (browser, source, target, widget, 
 
306
                                             func, user_data);
 
307
        }
 
308
      else
 
309
        {
 
310
          if (func != NULL)
 
311
            func (browser, source, NULL, error, user_data);
 
312
        }
 
313
 
 
314
      g_clear_error (&error);
 
315
 
 
316
      if (target != NULL)
 
317
        g_object_unref (target);
 
318
    }
 
319
  else if (thunar_file_get_kind (file) == G_FILE_TYPE_MOUNTABLE)
 
320
    {
 
321
      if (thunar_file_is_mounted (file))
 
322
        {
 
323
          location = thunar_file_get_target_location (file);
 
324
          target = thunar_file_get (location, &error);
 
325
          g_object_unref (location);
 
326
 
 
327
          if (func != NULL)
 
328
            func (browser, source, target, error, user_data);
 
329
 
 
330
          g_clear_error (&error);
 
331
 
 
332
          if (target != NULL)
 
333
            g_object_unref (target);
 
334
        }
 
335
      else
 
336
        {
 
337
          poke_data = thunar_browser_poke_file_data_new (browser, source, file,
 
338
                                                         func, user_data);
 
339
 
 
340
          mount_operation = thunar_browser_mount_operation_new (widget);
 
341
 
 
342
          g_file_mount_mountable (thunar_file_get_file (file), 
 
343
                                  G_MOUNT_MOUNT_NONE, mount_operation, NULL,
 
344
                                  thunar_browser_poke_mountable_finish,
 
345
                                  poke_data);
 
346
 
 
347
          g_object_unref (mount_operation);
 
348
        }
 
349
    }
 
350
  else if (!thunar_file_is_mounted (file))
 
351
    {
 
352
      poke_data = thunar_browser_poke_file_data_new (browser, source, file,
 
353
                                                     func, user_data);
 
354
 
 
355
      mount_operation = thunar_browser_mount_operation_new (widget);
 
356
 
 
357
      g_file_mount_enclosing_volume (thunar_file_get_file (file),
 
358
                                     G_MOUNT_MOUNT_NONE, mount_operation, NULL,
 
359
                                     thunar_browser_poke_file_finish,
 
360
                                     poke_data);
 
361
 
 
362
      g_object_unref (mount_operation);
 
363
    }
 
364
  else
 
365
    {
 
366
      if (func != NULL)
 
367
        func (browser, source, file, NULL, user_data);
 
368
    }
 
369
}
 
370
 
 
371
 
 
372
 
 
373
/**
 
374
 * thunar_browser_poke_file:
 
375
 * @browser : a #ThunarBrowser.
 
376
 * @file      : a #ThunarFile.
 
377
 * @widget    : a #GtkWidget, a #GdkScreen or %NULL.
 
378
 * @func      : a #ThunarBrowserPokeFileFunc callback or %NULL.
 
379
 * @user_data : pointer to arbitrary user data or %NULL.
 
380
 *
 
381
 * Pokes a #ThunarFile to see what's behind it.
 
382
 *
 
383
 * If @file has the type %G_FILE_TYPE_SHORTCUT, it tries to load and mount 
 
384
 * the file that is referred to by the %G_FILE_ATTRIBUTE_STANDARD_TARGET_URI
 
385
 * of the @file.
 
386
 *
 
387
 * If @file has the type %G_FILE_TYPE_MOUNTABLE, it tries to mount the @file.
 
388
 *
 
389
 * In the other cases, if the enclosing volume of the @file is not mounted
 
390
 * yet, it tries to mount it.
 
391
 *
 
392
 * When finished or if an error occured, it calls @func with the provided
 
393
 * @user data. The #GError parameter to @func is set if, and only if there
 
394
 * was an error.
 
395
 **/
 
396
void
 
397
thunar_browser_poke_file (ThunarBrowser            *browser,
 
398
                          ThunarFile               *file,
 
399
                          gpointer                  widget,
 
400
                          ThunarBrowserPokeFileFunc func,
 
401
                          gpointer                  user_data)
 
402
{
 
403
  _thunar_return_if_fail (THUNAR_IS_BROWSER (browser));
 
404
  _thunar_return_if_fail (THUNAR_IS_FILE (file));
 
405
 
 
406
  thunar_browser_poke_file_internal (browser, file, file, widget, func, user_data);
 
407
}
 
408
 
 
409
 
 
410
 
 
411
static void
 
412
thunar_browser_poke_volume_finish (GObject      *object,
 
413
                                   GAsyncResult *result,
 
414
                                   gpointer      user_data)
 
415
{
 
416
  PokeVolumeData *poke_data = user_data;
 
417
  ThunarFile     *file;
 
418
  GError         *error = NULL;
 
419
  GMount         *mount;
 
420
  GFile          *mount_point;
 
421
 
 
422
  _thunar_return_if_fail (G_IS_VOLUME (object));
 
423
  _thunar_return_if_fail (G_IS_ASYNC_RESULT (result));
 
424
  _thunar_return_if_fail (user_data != NULL);
 
425
  _thunar_return_if_fail (THUNAR_IS_BROWSER (poke_data->browser));
 
426
  _thunar_return_if_fail (G_IS_VOLUME (poke_data->volume));
 
427
  _thunar_return_if_fail (G_VOLUME (object) == poke_data->volume);
 
428
 
 
429
  if (!g_volume_mount_finish (G_VOLUME (object), result, &error))
 
430
    {
 
431
      if (error->domain == G_IO_ERROR)
 
432
        {
 
433
          if (error->code == G_IO_ERROR_ALREADY_MOUNTED)
 
434
            g_clear_error (&error);
 
435
        }
 
436
    }
 
437
 
 
438
  if (error == NULL)
 
439
    {
 
440
      mount = g_volume_get_mount (poke_data->volume);
 
441
      mount_point = g_mount_get_root (mount);
 
442
 
 
443
      file = thunar_file_get (mount_point, &error);
 
444
 
 
445
      g_object_unref (mount_point);
 
446
      g_object_unref (mount);
 
447
 
 
448
      if (poke_data->func != NULL)
 
449
        {
 
450
          (poke_data->func) (poke_data->browser, poke_data->volume, file, error, 
 
451
                             poke_data->user_data);
 
452
        }
 
453
 
 
454
      if (file != NULL)
 
455
        g_object_unref (file);
 
456
    }
 
457
  else
 
458
    {
 
459
      if (poke_data->func != NULL)
 
460
        {
 
461
          (poke_data->func) (poke_data->browser, poke_data->volume, NULL, error,
 
462
                             poke_data->user_data);
 
463
        }
 
464
    }
 
465
 
 
466
  thunar_browser_poke_volume_data_free (poke_data);
 
467
}
 
468
 
 
469
 
 
470
 
 
471
/**
 
472
 * thunar_browser_poke_volume:
 
473
 * @browser   : a #ThunarBrowser.
 
474
 * @volume    : a #GVolume.
 
475
 * @widget    : a #GtkWidget, a #GdkScreen or %NULL.
 
476
 * @func      : a #ThunarBrowserPokeVolumeFunc callback or %NULL.
 
477
 * @user_data : pointer to arbitrary user data or %NULL.
 
478
 *
 
479
 * This function checks if @volume is mounted or not. If it is, it loads
 
480
 * a #ThunarFile for the mount root and calls @func. If it is not mounted,
 
481
 * it first mounts the volume asynchronously and calls @func with the
 
482
 * #ThunarFile corresponding to the mount root when the mounting is finished.
 
483
 *
 
484
 * The #ThunarFile passed to @func will be %NULL if, and only if mounting
 
485
 * the @volume failed. The #GError passed to @func will be set if, and only if
 
486
 * mounting failed.
 
487
 **/
 
488
void
 
489
thunar_browser_poke_volume (ThunarBrowser              *browser,
 
490
                            GVolume                    *volume,
 
491
                            gpointer                    widget,
 
492
                            ThunarBrowserPokeVolumeFunc func,
 
493
                            gpointer                    user_data)
 
494
{
 
495
  GMountOperation *mount_operation;
 
496
  PokeVolumeData  *poke_data;
 
497
  ThunarFile      *file;
 
498
  GError          *error = NULL;
 
499
  GMount          *mount;
 
500
  GFile           *mount_point;
 
501
 
 
502
  _thunar_return_if_fail (THUNAR_IS_BROWSER (browser));
 
503
  _thunar_return_if_fail (G_IS_VOLUME (volume));
 
504
 
 
505
  if (thunar_g_volume_is_mounted (volume))
 
506
    {
 
507
      mount = g_volume_get_mount (volume);
 
508
      mount_point = g_mount_get_root (mount);
 
509
 
 
510
      file = thunar_file_get (mount_point, &error);
 
511
 
 
512
      g_object_unref (mount_point);
 
513
      g_object_unref (mount);
 
514
 
 
515
      if (func != NULL)
 
516
        func (browser, volume, file, error, user_data);
 
517
 
 
518
      g_clear_error (&error);
 
519
 
 
520
      if (file != NULL)
 
521
        g_object_unref (file);
 
522
    }
 
523
  else
 
524
    {
 
525
      poke_data = thunar_browser_poke_volume_data_new (browser, volume, func, user_data);
 
526
 
 
527
      mount_operation = thunar_browser_mount_operation_new (widget);
 
528
 
 
529
      g_volume_mount (volume, G_MOUNT_MOUNT_NONE, mount_operation, NULL, 
 
530
                      thunar_browser_poke_volume_finish, poke_data);
 
531
 
 
532
      g_object_unref (mount_operation);
 
533
    }
 
534
}