~mitya57/ubuntu/precise/nautilus/desktop-window-fixes

« back to all changes in this revision

Viewing changes to libnautilus-private/nautilus-vfs-file.c

  • Committer: Bazaar Package Importer
  • Author(s): Josselin Mouette
  • Date: 2009-09-23 20:11:11 UTC
  • mto: (1.2.5 upstream) (2.4.1 sid)
  • mto: This revision was merged to the branch mainline in revision 138.
  • Revision ID: james.westby@ubuntu.com-20090923201111-r4e2b6x5biik0ldh
Tags: upstream-2.28.0
ImportĀ upstreamĀ versionĀ 2.28.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
#include <config.h>
27
27
#include "nautilus-vfs-file.h"
28
28
 
 
29
#include "nautilus-directory-notify.h"
29
30
#include "nautilus-directory-private.h"
30
31
#include "nautilus-file-private.h"
31
32
#include "nautilus-autorun.h"
89
90
                 file_attributes);
90
91
}
91
92
 
 
93
static void
 
94
set_metadata_get_info_callback (GObject *source_object,
 
95
                                GAsyncResult *res,
 
96
                                gpointer callback_data)
 
97
{
 
98
        NautilusFile *file;
 
99
        GFileInfo *new_info;
 
100
        GError *error;
 
101
 
 
102
        file = callback_data;
 
103
 
 
104
        error = NULL;
 
105
        new_info = g_file_query_info_finish (G_FILE (source_object), res, &error);
 
106
        if (new_info != NULL) {
 
107
                if (nautilus_file_update_info (file, new_info)) {
 
108
                        nautilus_file_changed (file);
 
109
                }
 
110
                g_object_unref (new_info);
 
111
        }
 
112
        nautilus_file_unref (file);
 
113
        if (error) {
 
114
                g_error_free (error);
 
115
        }
 
116
}
 
117
 
 
118
static void
 
119
set_metadata_callback (GObject *source_object,
 
120
                       GAsyncResult *result,
 
121
                       gpointer callback_data)
 
122
{
 
123
        NautilusFile *file;
 
124
        GError *error;
 
125
        gboolean res;
 
126
 
 
127
        file = callback_data;
 
128
 
 
129
        error = NULL;
 
130
        res = g_file_set_attributes_finish (G_FILE (source_object),
 
131
                                            result,
 
132
                                            NULL,
 
133
                                            &error);
 
134
 
 
135
        if (res) {
 
136
                g_file_query_info_async (G_FILE (source_object),
 
137
                                         NAUTILUS_FILE_DEFAULT_ATTRIBUTES,
 
138
                                         0,
 
139
                                         G_PRIORITY_DEFAULT,
 
140
                                         NULL,
 
141
                                         set_metadata_get_info_callback, file);
 
142
        } else {
 
143
                nautilus_file_unref (file);
 
144
                g_error_free (error);
 
145
        }
 
146
}
 
147
 
 
148
static void
 
149
vfs_file_set_metadata (NautilusFile           *file,
 
150
                       const char             *key,
 
151
                       const char             *value)
 
152
{
 
153
        GFileInfo *info;
 
154
        GFile *location;
 
155
        char *gio_key;
 
156
 
 
157
        info = g_file_info_new ();
 
158
        
 
159
        gio_key = g_strconcat ("metadata::", key, NULL);
 
160
        if (value != NULL) {
 
161
                g_file_info_set_attribute_string (info, gio_key, value);
 
162
        } else {
 
163
                /* Unset the key */
 
164
                g_file_info_set_attribute (info, gio_key,
 
165
                                           G_FILE_ATTRIBUTE_TYPE_INVALID,
 
166
                                           NULL);
 
167
        }
 
168
        g_free (gio_key);
 
169
 
 
170
        location = nautilus_file_get_location (file);
 
171
        g_file_set_attributes_async (location,
 
172
                                     info,
 
173
                                     0,
 
174
                                     G_PRIORITY_DEFAULT,
 
175
                                     NULL,
 
176
                                     set_metadata_callback,
 
177
                                     nautilus_file_ref (file));
 
178
        g_object_unref (location);
 
179
        g_object_unref (info);
 
180
}
 
181
 
 
182
static void
 
183
vfs_file_set_metadata_as_list (NautilusFile           *file,
 
184
                               const char             *key,
 
185
                               char                  **value)
 
186
{
 
187
        GFile *location;
 
188
        GFileInfo *info;
 
189
        char *gio_key;
 
190
 
 
191
        info = g_file_info_new ();
 
192
 
 
193
        gio_key = g_strconcat ("metadata::", key, NULL);
 
194
        g_file_info_set_attribute_stringv (info, gio_key, value);
 
195
        g_free (gio_key);
 
196
 
 
197
        location = nautilus_file_get_location (file);
 
198
        g_file_set_attributes_async (location,
 
199
                                     info,
 
200
                                     0,
 
201
                                     G_PRIORITY_DEFAULT,
 
202
                                     NULL,
 
203
                                     set_metadata_callback,
 
204
                                     nautilus_file_ref (file));
 
205
        g_object_unref (info);
 
206
        g_object_unref (location);
 
207
}
 
208
 
92
209
static gboolean
93
210
vfs_file_get_item_count (NautilusFile *file, 
94
211
                         guint *count,
296
413
        op = callback_data;
297
414
 
298
415
        error = NULL;
299
 
        unmounted = g_file_unmount_mountable_finish (G_FILE (source_object),
300
 
                                                    res, &error);
 
416
        unmounted = g_file_unmount_mountable_with_operation_finish (G_FILE (source_object),
 
417
                                                                    res, &error);
301
418
        
302
419
    if (!unmounted &&
303
420
        error->domain == G_IO_ERROR && 
314
431
}
315
432
 
316
433
static void
317
 
vfs_file_unmount (NautilusFile *file)
 
434
vfs_file_unmount (NautilusFile                   *file,
 
435
                  GMountOperation                *mount_op,
 
436
                  GCancellable                   *cancellable,
 
437
                  NautilusFileOperationCallback   callback,
 
438
                  gpointer                        callback_data)
318
439
{
319
440
        NautilusFileOperation *op;
320
441
        GFile *location;
321
442
        
322
 
        op = nautilus_file_operation_new (file, NULL, NULL);
 
443
        op = nautilus_file_operation_new (file, callback, callback_data);
 
444
        if (cancellable) {
 
445
                g_object_unref (op->cancellable);
 
446
                op->cancellable = g_object_ref (cancellable);
 
447
        }
323
448
 
324
449
        location = nautilus_file_get_location (file);
325
 
        g_file_unmount_mountable (location,
326
 
                                G_MOUNT_UNMOUNT_NONE,
327
 
                                op->cancellable,
328
 
                                vfs_file_unmount_callback,
329
 
                                op);
 
450
        g_file_unmount_mountable_with_operation (location,
 
451
                                                 G_MOUNT_UNMOUNT_NONE,
 
452
                                                 mount_op,
 
453
                                                 op->cancellable,
 
454
                                                 vfs_file_unmount_callback,
 
455
                                                 op);
330
456
        g_object_unref (location);
331
457
}
332
458
 
342
468
        op = callback_data;
343
469
 
344
470
        error = NULL;
345
 
        ejected = g_file_eject_mountable_finish (G_FILE (source_object),
346
 
                                                    res, &error);
 
471
        ejected = g_file_eject_mountable_with_operation_finish (G_FILE (source_object),
 
472
                                                                res, &error);
347
473
 
348
474
        if (!ejected &&
349
475
            error->domain == G_IO_ERROR &&
360
486
}
361
487
 
362
488
static void
363
 
vfs_file_eject (NautilusFile *file)
 
489
vfs_file_eject (NautilusFile                   *file,
 
490
                GMountOperation                *mount_op,
 
491
                GCancellable                   *cancellable,
 
492
                NautilusFileOperationCallback   callback,
 
493
                gpointer                        callback_data)
364
494
{
365
495
        NautilusFileOperation *op;
366
496
        GFile *location;
367
497
        
368
 
        op = nautilus_file_operation_new (file, NULL, NULL);
369
 
 
370
 
        location = nautilus_file_get_location (file);
371
 
        g_file_eject_mountable (location,
372
 
                                G_MOUNT_UNMOUNT_NONE,
 
498
        op = nautilus_file_operation_new (file, callback, callback_data);
 
499
        if (cancellable) {
 
500
                g_object_unref (op->cancellable);
 
501
                op->cancellable = g_object_ref (cancellable);
 
502
        }
 
503
 
 
504
        location = nautilus_file_get_location (file);
 
505
        g_file_eject_mountable_with_operation (location,
 
506
                                               G_MOUNT_UNMOUNT_NONE,
 
507
                                               mount_op,
 
508
                                               op->cancellable,
 
509
                                               vfs_file_eject_callback,
 
510
                                               op);
 
511
        g_object_unref (location);
 
512
}
 
513
 
 
514
static void
 
515
vfs_file_start_callback (GObject *source_object,
 
516
                         GAsyncResult *res,
 
517
                         gpointer callback_data)
 
518
{
 
519
        NautilusFileOperation *op;
 
520
        gboolean started;
 
521
        GError *error;
 
522
 
 
523
        op = callback_data;
 
524
 
 
525
        error = NULL;
 
526
        started = g_file_start_mountable_finish (G_FILE (source_object),
 
527
                                                 res, &error);
 
528
 
 
529
        if (!started &&
 
530
            error->domain == G_IO_ERROR &&
 
531
            (error->code == G_IO_ERROR_FAILED_HANDLED ||
 
532
             error->code == G_IO_ERROR_CANCELLED)) {
 
533
                g_error_free (error);
 
534
                error = NULL;
 
535
        }
 
536
 
 
537
        nautilus_file_operation_complete (op, G_FILE (source_object), error);
 
538
        if (error) {
 
539
                g_error_free (error);
 
540
        }
 
541
}
 
542
 
 
543
 
 
544
static void
 
545
vfs_file_start (NautilusFile                   *file,
 
546
                GMountOperation                *mount_op,
 
547
                GCancellable                   *cancellable,
 
548
                NautilusFileOperationCallback   callback,
 
549
                gpointer                        callback_data)
 
550
{
 
551
        NautilusFileOperation *op;
 
552
        GError *error;
 
553
        GFile *location;
 
554
 
 
555
        if (file->details->type != G_FILE_TYPE_MOUNTABLE) {
 
556
                if (callback) {
 
557
                        error = NULL;
 
558
                        g_set_error_literal (&error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
 
559
                                             _("This file cannot be started"));
 
560
                        callback (file, NULL, error, callback_data);
 
561
                        g_error_free (error);
 
562
                }
 
563
                return;
 
564
        }
 
565
 
 
566
        op = nautilus_file_operation_new (file, callback, callback_data);
 
567
        if (cancellable) {
 
568
                g_object_unref (op->cancellable);
 
569
                op->cancellable = g_object_ref (cancellable);
 
570
        }
 
571
 
 
572
        location = nautilus_file_get_location (file);
 
573
        g_file_start_mountable (location,
 
574
                                0,
 
575
                                mount_op,
373
576
                                op->cancellable,
374
 
                                vfs_file_eject_callback,
 
577
                                vfs_file_start_callback,
375
578
                                op);
376
579
        g_object_unref (location);
377
580
}
378
581
 
379
582
static void
 
583
vfs_file_stop_callback (GObject *source_object,
 
584
                        GAsyncResult *res,
 
585
                        gpointer callback_data)
 
586
{
 
587
        NautilusFileOperation *op;
 
588
        gboolean stopped;
 
589
        GError *error;
 
590
 
 
591
        op = callback_data;
 
592
 
 
593
        error = NULL;
 
594
        stopped = g_file_stop_mountable_finish (G_FILE (source_object),
 
595
                                                res, &error);
 
596
 
 
597
        if (!stopped &&
 
598
            error->domain == G_IO_ERROR &&
 
599
            (error->code == G_IO_ERROR_FAILED_HANDLED ||
 
600
             error->code == G_IO_ERROR_CANCELLED)) {
 
601
                g_error_free (error);
 
602
                error = NULL;
 
603
        }
 
604
 
 
605
        nautilus_file_operation_complete (op, G_FILE (source_object), error);
 
606
        if (error) {
 
607
                g_error_free (error);
 
608
        }
 
609
}
 
610
 
 
611
static void
 
612
vfs_file_stop (NautilusFile                   *file,
 
613
               GMountOperation                *mount_op,
 
614
               GCancellable                   *cancellable,
 
615
               NautilusFileOperationCallback   callback,
 
616
               gpointer                        callback_data)
 
617
{
 
618
        NautilusFileOperation *op;
 
619
        GFile *location;
 
620
 
 
621
        op = nautilus_file_operation_new (file, callback, callback_data);
 
622
        if (cancellable) {
 
623
                g_object_unref (op->cancellable);
 
624
                op->cancellable = g_object_ref (cancellable);
 
625
        }
 
626
 
 
627
        location = nautilus_file_get_location (file);
 
628
        g_file_stop_mountable (location,
 
629
                               G_MOUNT_UNMOUNT_NONE,
 
630
                               mount_op,
 
631
                               op->cancellable,
 
632
                               vfs_file_stop_callback,
 
633
                               op);
 
634
        g_object_unref (location);
 
635
}
 
636
 
 
637
static void
 
638
vfs_file_poll_callback (GObject *source_object,
 
639
                        GAsyncResult *res,
 
640
                        gpointer callback_data)
 
641
{
 
642
        NautilusFileOperation *op;
 
643
        gboolean stopped;
 
644
        GError *error;
 
645
 
 
646
        op = callback_data;
 
647
 
 
648
        error = NULL;
 
649
        stopped = g_file_poll_mountable_finish (G_FILE (source_object),
 
650
                                                res, &error);
 
651
 
 
652
        if (!stopped &&
 
653
            error->domain == G_IO_ERROR &&
 
654
            (error->code == G_IO_ERROR_FAILED_HANDLED ||
 
655
             error->code == G_IO_ERROR_CANCELLED)) {
 
656
                g_error_free (error);
 
657
                error = NULL;
 
658
        }
 
659
 
 
660
        nautilus_file_operation_complete (op, G_FILE (source_object), error);
 
661
        if (error) {
 
662
                g_error_free (error);
 
663
        }
 
664
}
 
665
 
 
666
static void
 
667
vfs_file_poll_for_media (NautilusFile *file)
 
668
{
 
669
        NautilusFileOperation *op;
 
670
        GFile *location;
 
671
 
 
672
        op = nautilus_file_operation_new (file, NULL, NULL);
 
673
 
 
674
        location = nautilus_file_get_location (file);
 
675
        g_file_poll_mountable (location,
 
676
                               op->cancellable,
 
677
                               vfs_file_poll_callback,
 
678
                               op);
 
679
        g_object_unref (location);
 
680
}
 
681
 
 
682
static void
380
683
nautilus_vfs_file_init (gpointer object, gpointer klass)
381
684
{
382
685
        NautilusVFSFile *file;
401
704
        file_class->get_deep_counts = vfs_file_get_deep_counts;
402
705
        file_class->get_date = vfs_file_get_date;
403
706
        file_class->get_where_string = vfs_file_get_where_string;
 
707
        file_class->set_metadata = vfs_file_set_metadata;
 
708
        file_class->set_metadata_as_list = vfs_file_set_metadata_as_list;
404
709
        file_class->mount = vfs_file_mount;
405
710
        file_class->unmount = vfs_file_unmount;
406
711
        file_class->eject = vfs_file_eject;
 
712
        file_class->start = vfs_file_start;
 
713
        file_class->stop = vfs_file_stop;
 
714
        file_class->poll_for_media = vfs_file_poll_for_media;
407
715
}