gf-icon-view.c 81.5 KB
Newer Older
Alberts Muktupāvels's avatar
Alberts Muktupāvels committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
 * Copyright (C) 2019 Alberts Muktupāvels
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"
#include "gf-icon-view.h"

21
#include <gdk/gdkx.h>
22
23
#include <glib/gi18n.h>

24
#include "dbus/gf-file-manager-gen.h"
25
#include "dbus/gf-nautilus2-gen.h"
26
#include "gf-create-folder-dialog.h"
27
#include "gf-desktop-enum-types.h"
28
#include "gf-desktop-enums.h"
29
#include "gf-dummy-icon.h"
30
#include "gf-home-icon.h"
31
#include "gf-icon.h"
32
#include "gf-monitor-view.h"
33
#include "gf-trash-icon.h"
34
#include "gf-utils.h"
35
#include "gf-workarea-watcher.h"
36

37
38
39
40
41
42
43
typedef struct
{
  GtkWidget *icon;

  GtkWidget *view;
} GfIconInfo;

Alberts Muktupāvels's avatar
Alberts Muktupāvels committed
44
45
struct _GfIconView
{
46
  GtkEventBox         parent;
47

48
49
  GfWorkareaWatcher  *workarea_watcher;

50
  GfThumbnailFactory *thumbnail_factory;
51

52
53
  GtkGesture         *multi_press;
  GtkGesture         *drag;
54

55
56
  GFile              *desktop;
  GFileMonitor       *monitor;
57

58
  GSettings          *settings;
59

60
61
  GfPlacement         placement;
  GfSortBy            sort_by;
62

63
  GtkWidget          *fixed;
64

65
  GtkWidget          *dummy_icon;
66

67
  GCancellable       *cancellable;
68

69
  GList              *icons;
70

71
72
  GfIconInfo         *home_info;
  GfIconInfo         *trash_info;
73

74
  GList              *selected_icons;
75

76
77
78
79
80
  GtkCssProvider     *rubberband_css;
  GtkStyleContext    *rubberband_style;
  GdkRectangle        rubberband_rect;
  GList              *rubberband_icons;

81
  GfNautilus2Gen     *nautilus;
82
  GfFileManagerGen   *file_manager;
83

84
  GtkWidget          *create_folder_dialog;
85

86
87
  GfIcon             *last_selected_icon;
  GfIcon             *extend_from_icon;
88
89

  GPtrArray          *drag_rectagles;
Alberts Muktupāvels's avatar
Alberts Muktupāvels committed
90
91
};

92
93
94
95
96
enum
{
  SELECT_ALL,
  UNSELECT_ALL,

97
  ACTIVATE,
98

99
100
  RENAME,

101
  TRASH,
102
  DELETE,
103

104
105
106
  TOGGLE,

  MOVE,
107

108
109
110
111
112
  LAST_SIGNAL
};

static guint view_signals[LAST_SIGNAL] = { 0 };

113
G_DEFINE_TYPE (GfIconView, gf_icon_view, GTK_TYPE_EVENT_BOX)
Alberts Muktupāvels's avatar
Alberts Muktupāvels committed
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
static GVariant *
get_platform_data (GfIconView *self,
                   guint32     timestamp)
{
  GVariantBuilder builder;
  GtkWidget *toplevel;
  GdkWindow *window;
  char *parent_handle;

  g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));

  toplevel = gtk_widget_get_toplevel (GTK_WIDGET (self));
  window = gtk_widget_get_window (toplevel);
  parent_handle = g_strdup_printf ("x11:%lx", gdk_x11_window_get_xid (window));

  g_variant_builder_add (&builder,
                         "{sv}",
                         "parent-handle",
                         g_variant_new_take_string (parent_handle));

  g_variant_builder_add (&builder,
                         "{sv}",
                         "timestamp",
                         g_variant_new_uint32 (timestamp));

  g_variant_builder_add (&builder,
                         "{sv}",
                         "window-position",
                         g_variant_new_string ("center"));

  return g_variant_builder_end (&builder);
}

148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
static char *
build_attributes_list (const char *first,
                       ...)
{
  GString *attributes;
  va_list args;
  const char *attribute;

  attributes = g_string_new (first);
  va_start (args, first);

  while ((attribute = va_arg (args, const char *)) != NULL)
    g_string_append_printf (attributes, ",%s", attribute);

  va_end (args);

  return g_string_free (attributes, FALSE);
}

167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
static GfIconInfo *
gf_icon_info_new (GtkWidget *icon)
{
  GfIconInfo *info;

  info = g_new0 (GfIconInfo, 1);
  info->icon = g_object_ref_sink (icon);

  info->view = NULL;

  return info;
}

static void
gf_icon_info_free (gpointer data)
{
  GfIconInfo *info;

  info = (GfIconInfo *) data;

  g_clear_pointer (&info->icon, g_object_unref);
  g_free (info);
}

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
static GList *
get_monitor_views (GfIconView *self)
{
  GList *views;
  GList *children;
  GList *l;

  views = NULL;

  children = gtk_container_get_children (GTK_CONTAINER (self->fixed));

  for (l = children; l != NULL; l = l->next)
    {
      GfMonitorView *view;

      view = GF_MONITOR_VIEW (l->data);

      if (gf_monitor_view_is_primary (view))
        views = g_list_prepend (views, view);
      else
        views = g_list_append (views, view);
    }

  g_list_free (children);
  return views;
}

static void
add_icons (GfIconView *self)
{
  GList *views;
  GList *view;
  GList *l;

  views = get_monitor_views (self);
  view = views;

  for (l = self->icons; l != NULL; l = l->next)
    {
230
231
232
233
      GfIconInfo *info;

      info = (GfIconInfo *) l->data;

234
235
236
      if (gf_icon_is_hidden (GF_ICON (info->icon)))
        continue;

237
238
239
      if (info->view != NULL)
        continue;

240
241
      while (view != NULL)
        {
242
243
          if (!gf_monitor_view_add_icon (GF_MONITOR_VIEW (view->data),
                                         info->icon))
244
245
246
247
248
            {
              view = view->next;
              continue;
            }

249
250
          info->view = view->data;

251
252
253
254
255
256
257
258
259
260
          break;
        }

      if (view == NULL)
        break;
    }

  g_list_free (views);
}

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
static int
compare_directory (GfIcon     *a,
                   GfIcon     *b,
                   GfIconView *self)
{
  gboolean is_dir_a;
  gboolean is_dir_b;

  is_dir_a = gf_icon_get_file_type (a) == G_FILE_TYPE_DIRECTORY;
  is_dir_b = gf_icon_get_file_type (b) == G_FILE_TYPE_DIRECTORY;

  if (is_dir_a != is_dir_b)
    return is_dir_a ? -1 : 1;

  return 0;
}

static int
compare_name (GfIcon     *a,
              GfIcon     *b,
              GfIconView *self)
{
  const char *name_a;
  const char *name_b;

  name_a = gf_icon_get_name_collated (a);
  name_b = gf_icon_get_name_collated (b);

  return g_strcmp0 (name_a, name_b);
}

static int
compare_modified (GfIcon     *a,
                  GfIcon     *b,
                  GfIconView *self)
{
  guint64 modified_a;
  guint64 modified_b;

  modified_a = gf_icon_get_time_modified (a);
  modified_b = gf_icon_get_time_modified (b);

  if (modified_a < modified_b)
    return -1;
  else if (modified_a > modified_b)
    return 1;

  return 0;
}

static int
compare_size (GfIcon     *a,
              GfIcon     *b,
              GfIconView *self)
{
  guint64 size_a;
  guint64 size_b;

  size_a = gf_icon_get_size (a);
  size_b = gf_icon_get_size (b);

  if (size_a < size_b)
    return -1;
  else if (size_a > size_b)
    return 1;

  return 0;
}

static int
compare_name_func (gconstpointer a,
                   gconstpointer b,
                   gpointer      user_data)
334
335
{
  GfIconView *self;
336
337
338
339
340
  GfIconInfo *info_a;
  GfIcon *icon_a;
  GfIconInfo *info_b;
  GfIcon *icon_b;
  int result;
341
342
343

  self = GF_ICON_VIEW (user_data);

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
  info_a = (GfIconInfo *) a;
  icon_a = GF_ICON (info_a->icon);

  info_b = (GfIconInfo *) b;
  icon_b = GF_ICON (info_b->icon);

  result = compare_directory (icon_a, icon_b, self);

  if (result == 0)
    result = compare_name (icon_a, icon_b, self);

  return result;
}

static int
compare_modified_func (gconstpointer a,
                       gconstpointer b,
                       gpointer      user_data)
{
  GfIconView *self;
  GfIconInfo *info_a;
  GfIcon *icon_a;
  GfIconInfo *info_b;
  GfIcon *icon_b;
  int result;

  self = GF_ICON_VIEW (user_data);

  info_a = (GfIconInfo *) a;
  icon_a = GF_ICON (info_a->icon);

  info_b = (GfIconInfo *) b;
  icon_b = GF_ICON (info_b->icon);

  result = compare_directory (icon_a, icon_b, self);

  if (result == 0)
    result = compare_modified (icon_a, icon_b, self);

  return result;
}

static int
compare_size_func (gconstpointer a,
                   gconstpointer b,
                   gpointer      user_data)
{
  GfIconView *self;
  GfIconInfo *info_a;
  GfIcon *icon_a;
  GfIconInfo *info_b;
  GfIcon *icon_b;
  int result;

  self = GF_ICON_VIEW (user_data);

  info_a = (GfIconInfo *) a;
  icon_a = GF_ICON (info_a->icon);

  info_b = (GfIconInfo *) b;
  icon_b = GF_ICON (info_b->icon);

  result = compare_directory (icon_a, icon_b, self);

  if (result == 0)
    result = compare_size (icon_a, icon_b, self);

  return result;
}

static gboolean
sort_icons (GfIconView *self)
{
  GList *old_list;
  gboolean changed;
  GList *l1;
  GList *l2;

  old_list = g_list_copy (self->icons);

  if (self->sort_by == GF_SORT_BY_NAME)
    self->icons = g_list_sort_with_data (self->icons, compare_name_func, self);
  else if (self->sort_by == GF_SORT_BY_DATE_MODIFIED)
    self->icons = g_list_sort_with_data (self->icons, compare_modified_func, self);
  else if (self->sort_by == GF_SORT_BY_SIZE)
    self->icons = g_list_sort_with_data (self->icons, compare_size_func, self);

  changed = FALSE;
  for (l1 = self->icons, l2 = old_list;
       l1 != NULL;
       l1 = l1->next, l2 = l2->next)
    {
      if (l1->data == l2->data)
        continue;
438

439
440
441
442
443
444
      changed = TRUE;
      break;
    }

  g_list_free (old_list);
  return changed;
445
446
447
}

static void
448
remove_and_readd_icons (GfIconView *self)
449
{
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
  GList *l;

  for (l = self->icons; l != NULL; l = l->next)
    {
      GfIconInfo *info;

      info = (GfIconInfo *) l->data;

      if (info->view == NULL)
        continue;

      gf_monitor_view_remove_icon (GF_MONITOR_VIEW (info->view), info->icon);
      info->view = NULL;
    }

  add_icons (self);
}
467

468
static void
469
470
resort_icons (GfIconView *self,
              gboolean    force)
471
{
472
  if (!sort_icons (self) && !force)
473
    return;
474

475
  remove_and_readd_icons (self);
476
477
}

478
479
480
481
static void
unselect_cb (gpointer data,
             gpointer user_data)
{
482
  gf_icon_set_selected (data, FALSE);
483
484
485
486
487
488
489
490
491
492
493
494
495
}

static void
unselect_icons (GfIconView *self)
{
  if (self->selected_icons == NULL)
    return;

  g_list_foreach (self->selected_icons, unselect_cb, NULL);
  g_clear_pointer (&self->selected_icons, g_list_free);
}

static void
496
497
icon_selected_cb (GfIcon     *icon,
                  GfIconView *self)
498
{
499
  if (gf_icon_get_selected (icon))
500
    self->selected_icons = g_list_append (self->selected_icons, icon);
501
  else
502
503
504
    self->selected_icons = g_list_remove (self->selected_icons, icon);
}

505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
static void
icon_has_focus_cb (GtkWidget  *widget,
                   GParamSpec *pspec,
                   GfIconView *self)
{
  if (gtk_widget_has_focus (widget))
    self->last_selected_icon = GF_ICON (widget);
}

static void
icon_clicked_cb (GtkWidget  *widget,
                 GfIconView *self)
{
  self->last_selected_icon = GF_ICON (widget);
  self->extend_from_icon = NULL;
}

522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
static void
show_item_properties_cb (GObject      *object,
                         GAsyncResult *res,
                         gpointer      user_data)
{
  GError *error;

  error = NULL;
  gf_file_manager_gen_call_show_item_properties_finish (GF_FILE_MANAGER_GEN (object),
                                                        res, &error);

  if (error != NULL)
    {
      if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
        g_warning ("Error showing properties: %s", error->message);
      g_error_free (error);
    }
}

541
542
543
544
545
546
547
548
static void
empty_trash_cb (GObject      *object,
                GAsyncResult *res,
                gpointer      user_data)
{
  GError *error;

  error = NULL;
549
550
  gf_nautilus2_gen_call_empty_trash_finish (GF_NAUTILUS2_GEN (object),
                                            res, &error);
551
552
553
554

  if (error != NULL)
    {
      if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
555
        g_warning ("Error emptying trash: %s", error->message);
556
557
558
559
      g_error_free (error);
    }
}

560
static void
561
562
563
trash_uris_cb (GObject      *object,
               GAsyncResult *res,
               gpointer      user_data)
564
565
566
567
{
  GError *error;

  error = NULL;
568
  gf_nautilus2_gen_call_trash_uris_finish (GF_NAUTILUS2_GEN (object),
569
570
571
572
573
574
575
576
577
578
                                           res, &error);

  if (error != NULL)
    {
      if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
        g_warning ("Error moving files to trash: %s", error->message);
      g_error_free (error);
    }
}

579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
static void
delete_uris_cb (GObject      *object,
                GAsyncResult *res,
                gpointer      user_data)
{
  GError *error;

  error = NULL;
  gf_nautilus2_gen_call_delete_uris_finish (GF_NAUTILUS2_GEN (object),
                                            res, &error);

  if (error != NULL)
    {
      if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
        g_warning ("Error deleting files: %s", error->message);
      g_error_free (error);
    }
}

598
static void
599
600
601
rename_uri_cb (GObject      *object,
               GAsyncResult *res,
               gpointer      user_data)
602
603
604
605
{
  GError *error;

  error = NULL;
606
  gf_nautilus2_gen_call_rename_uri_finish (GF_NAUTILUS2_GEN (object),
607
608
609
610
611
612
613
614
615
616
                                           res, &error);

  if (error != NULL)
    {
      if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
        g_warning ("Error renaming file: %s", error->message);
      g_error_free (error);
    }
}

617
618
619
620
621
622
623
624
static void
copy_uris_cb (GObject      *object,
              GAsyncResult *res,
              gpointer      user_data)
{
  GError *error;

  error = NULL;
625
626
  gf_nautilus2_gen_call_copy_uris_finish (GF_NAUTILUS2_GEN (object),
                                          res, &error);
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643

  if (error != NULL)
    {
      if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
        g_warning ("Error copying uris: %s", error->message);
      g_error_free (error);
    }
}

static void
move_uris_cb (GObject      *object,
              GAsyncResult *res,
              gpointer      user_data)
{
  GError *error;

  error = NULL;
644
645
  gf_nautilus2_gen_call_move_uris_finish (GF_NAUTILUS2_GEN (object),
                                          res, &error);
646
647
648
649
650
651
652
653
654

  if (error != NULL)
    {
      if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
        g_warning ("Error moving uris: %s", error->message);
      g_error_free (error);
    }
}

655
656
static GfIconInfo *
create_icon_info (GfIconView *self,
657
                  GtkWidget  *icon)
658
{
659
660
661
662
  g_signal_connect (icon, "selected",
                    G_CALLBACK (icon_selected_cb),
                    self);

663
664
665
666
667
668
669
670
  g_signal_connect (icon, "notify::has-focus",
                    G_CALLBACK (icon_has_focus_cb),
                    self);

  g_signal_connect (icon, "clicked",
                    G_CALLBACK (icon_clicked_cb),
                    self);

671
672
673
674
675
676
677
678
679
680
681
  g_settings_bind (self->settings, "icon-size",
                   icon, "icon-size",
                   G_SETTINGS_BIND_GET);

  g_settings_bind (self->settings, "extra-text-width",
                   icon, "extra-text-width",
                   G_SETTINGS_BIND_GET);

  return gf_icon_info_new (icon);
}

682
683
684
685
686
687
688
689
690
static void
query_info_cb (GObject      *object,
               GAsyncResult *res,
               gpointer      user_data)
{
  GFile *file;
  GFileInfo *file_info;
  GError *error;
  GfIconView *self;
691
  GtkWidget *icon;
692
  GfIconInfo *icon_info;
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709

  file = G_FILE (object);

  error = NULL;
  file_info = g_file_query_info_finish (file, res, &error);

  if (error != NULL)
    {
      if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
        g_warning ("%s", error->message);

      g_error_free (error);
      return;
    }

  self = GF_ICON_VIEW (user_data);

710
  icon = gf_icon_new (self, file, file_info);
711
712
  g_object_unref (file_info);

713
  icon_info = create_icon_info (self, icon);
714
  self->icons = g_list_prepend (self->icons, icon_info);
715

716
  if (self->placement == GF_PLACEMENT_AUTO_ARRANGE_ICONS)
717
    resort_icons (self, TRUE);
718
719
  else
    add_icons (self);
720
721
}

722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
static GfIconInfo *
find_icon_info_by_file (GfIconView *self,
                        GFile      *file)
{
  GList *l;

  for (l = self->icons; l != NULL; l = l->next)
    {
      GfIconInfo *info;
      GFile *icon_file;

      info = (GfIconInfo *) l->data;
      icon_file = gf_icon_get_file (GF_ICON (info->icon));

      if (g_file_equal (icon_file, file))
        return info;
    }

  return NULL;
}

743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
static void
remove_icon_from_view (GfIconView *self,
                       GfIconInfo *info)
{
  GfIcon *icon;

  if (info->view == NULL)
    return;

  icon = GF_ICON (info->icon);

  gf_monitor_view_remove_icon (GF_MONITOR_VIEW (info->view), GTK_WIDGET (icon));
  info->view = NULL;

  self->selected_icons = g_list_remove (self->selected_icons, icon);
  self->rubberband_icons = g_list_remove (self->rubberband_icons, icon);

  if (icon == self->last_selected_icon)
    self->last_selected_icon = NULL;

  if (icon == self->extend_from_icon)
    self->extend_from_icon = NULL;
}

767
768
769
770
771
772
773
774
775
776
777
778
779
static void
remove_icon (GfIconView *self,
             GfIconInfo *info)
{
  remove_icon_from_view (self, info);

  self->icons = g_list_remove (self->icons, info);
  gf_icon_info_free (info);

  if (self->placement == GF_PLACEMENT_AUTO_ARRANGE_ICONS)
    remove_and_readd_icons (self);
}

780
781
782
783
784
785
786
787
788
789
790
static void
file_deleted (GfIconView *self,
              GFile      *deleted_file)
{
  GfIconInfo *info;

  info = find_icon_info_by_file (self, deleted_file);

  if (info == NULL)
    return;

791
  remove_icon (self, info);
792
793
}

794
795
796
797
798
799
static void
file_created (GfIconView *self,
              GFile      *created_file)
{
  char *attributes;

800
  attributes = gf_icon_view_get_file_attributes (self);
801
802
803
804
805
806
807
808
809
810
811
812

  g_file_query_info_async (created_file,
                           attributes,
                           G_FILE_QUERY_INFO_NONE,
                           G_PRIORITY_LOW,
                           self->cancellable,
                           query_info_cb,
                           self);

  g_free (attributes);
}

813
814
815
816
817
static void
file_renamed (GfIconView *self,
              GFile      *old_file,
              GFile      *new_file)
{
818
819
  GfIconInfo *old_info;
  GfIconInfo *new_info;
820

821
822
  old_info = find_icon_info_by_file (self, old_file);
  new_info = find_icon_info_by_file (self, new_file);
823

824
825
826
827
828
829
830
831
832
833
834
835
836
  if (old_info != NULL && new_info != NULL)
    {
      gf_icon_set_file (GF_ICON (new_info->icon), new_file);
      remove_icon (self, old_info);
    }
  else if (old_info != NULL)
    {
      gf_icon_set_file (GF_ICON (old_info->icon), new_file);
    }
  else if (new_info != NULL)
    {
      gf_icon_set_file (GF_ICON (new_info->icon), new_file);
    }
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
static void
desktop_changed_cb (GFileMonitor      *monitor,
                    GFile             *file,
                    GFile             *other_file,
                    GFileMonitorEvent  event_type,
                    GfIconView        *self)
{
  switch (event_type)
    {
      case G_FILE_MONITOR_EVENT_CHANGED:
        break;

      case G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT:
        break;

      case G_FILE_MONITOR_EVENT_DELETED:
        file_deleted (self, file);
        break;

      case G_FILE_MONITOR_EVENT_CREATED:
        file_created (self, file);
        break;

      case G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED:
        break;

      case G_FILE_MONITOR_EVENT_PRE_UNMOUNT:
        break;

      case G_FILE_MONITOR_EVENT_UNMOUNTED:
        break;

      case G_FILE_MONITOR_EVENT_MOVED:
        break;

      case G_FILE_MONITOR_EVENT_RENAMED:
875
        file_renamed (self, file, other_file);
876
877
878
        break;

      case G_FILE_MONITOR_EVENT_MOVED_IN:
879
        file_created (self, file);
880
881
882
        break;

      case G_FILE_MONITOR_EVENT_MOVED_OUT:
883
        file_deleted (self, file);
884
885
886
887
888
889
890
        break;

      default:
        break;
    }
}

891
static void
892
893
894
895
create_folder_dialog_validate_cb (GfCreateFolderDialog *dialog,
                                  const char           *folder_name,
                                  GfIconView           *self)
{
896
897
  char *message;
  gboolean valid;
898

899
900
901
902
903
  message = NULL;
  valid = gf_icon_view_validate_new_name (self,
                                          G_FILE_TYPE_DIRECTORY,
                                          folder_name,
                                          &message);
904

905
906
  gf_create_folder_dialog_set_valid (dialog, valid, message);
  g_free (message);
907
908
}

909
static void
910
911
912
create_folder_cb (GObject      *object,
                  GAsyncResult *res,
                  gpointer      user_data)
913
914
915
916
{
  GError *error;

  error = NULL;
917
918
  gf_nautilus2_gen_call_create_folder_finish (GF_NAUTILUS2_GEN (object),
                                              res, &error);
919
920

  if (error != NULL)
921
    {
922
923
      if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
        g_warning ("Error creating new folder: %s", error->message);
924
925
926
927
      g_error_free (error);
    }
}

928
929
930
931
932
933
934
935
936
937
938
939
static void
create_folder_dialog_response_cb (GtkDialog  *dialog,
                                  gint        response_id,
                                  GfIconView *self)
{
  GfCreateFolderDialog *folder_dialog;

  if (response_id != GTK_RESPONSE_ACCEPT)
    return;

  folder_dialog = GF_CREATE_FOLDER_DIALOG (dialog);

940
941
942
943
944
  if (self->nautilus != NULL)
    {
      char *parent_uri;
      char *folder_name;
      guint32 timestamp;
945

946
947
948
      parent_uri = g_file_get_uri (self->desktop);
      folder_name = gf_create_folder_dialog_get_folder_name (folder_dialog);
      timestamp = gtk_get_current_event_time ();
949

950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
      gf_nautilus2_gen_call_create_folder (self->nautilus,
                                           parent_uri,
                                           folder_name,
                                           get_platform_data (self, timestamp),
                                           self->cancellable,
                                           create_folder_cb,
                                           NULL);

      g_free (parent_uri);
      g_free (folder_name);
    }
  else
    {
      g_assert_not_reached ();
    }
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

  gtk_widget_destroy (GTK_WIDGET (dialog));
}

static void
create_folder_dialog_destroy_cb (GtkWidget   *widget,
                                 GfIconView  *self)
{
  self->create_folder_dialog = NULL;
}

static void
new_folder_cb (GtkMenuItem *item,
               GfIconView  *self)
{
  GtkWidget *dialog;

  if (self->nautilus == NULL)
    return;

  if (self->create_folder_dialog != NULL)
    {
      gtk_window_present (GTK_WINDOW (self->create_folder_dialog));
      return;
    }

  dialog = gf_create_folder_dialog_new ();
  self->create_folder_dialog = dialog;

  g_signal_connect (dialog, "validate",
                    G_CALLBACK (create_folder_dialog_validate_cb),
                    self);

  g_signal_connect (dialog, "response",
                    G_CALLBACK (create_folder_dialog_response_cb),
                    self);

  g_signal_connect (dialog, "destroy",
                    G_CALLBACK (create_folder_dialog_destroy_cb),
                    self);

  gtk_window_present (GTK_WINDOW (dialog));
}

1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
static void
change_background_cb (GtkMenuItem *item,
                      GfIconView  *self)
{
  GError *error;

  error = NULL;
  if (!gf_launch_desktop_file ("gnome-background-panel.desktop", &error))
    {
      g_warning ("%s", error->message);
      g_error_free (error);
    }
}

1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
static void
display_settings_cb (GtkMenuItem *item,
                     GfIconView  *self)
{
  GError *error;

  error = NULL;
  if (!gf_launch_desktop_file ("gnome-display-panel.desktop", &error))
    {
      g_warning ("%s", error->message);
      g_error_free (error);
    }
}

1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
static void
open_terminal_cb (GtkMenuItem *item,
                  GfIconView  *self)
{
  GError *error;

  error = NULL;
  if (!gf_launch_desktop_file ("org.gnome.Terminal.desktop", &error))
    {
      g_warning ("%s", error->message);
      g_error_free (error);
    }
}

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
typedef struct
{
  GfIconView  *self;
  GfPlacement  placement;
} GfPlacementData;

static void
free_placement_data (gpointer  data,
                     GClosure *closure)
{
  g_free (data);
}

static void
placement_cb (GtkCheckMenuItem *item,
              GfPlacementData  *data)
{
  if (!gtk_check_menu_item_get_active (item))
    return;

  g_settings_set_enum (data->self->settings, "placement", data->placement);
}

static const char *
placement_to_string (GfPlacement placement)
{
  const char *string;

  string = NULL;

  switch (placement)
    {
      case GF_PLACEMENT_AUTO_ARRANGE_ICONS:
        string = _("Auto arrange icons");
        break;

      case GF_PLACEMENT_ALIGN_ICONS_TO_GRID:
        string = _("Align icons to grid");
        break;

      case GF_PLACEMENT_FREE:
1092
        string = C_("Free placement of icons", "Free");
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
        break;

      case GF_PLACEMENT_LAST:
      default:
        break;
    }

  g_assert (string != NULL);

  return string;
}

1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
static gboolean
is_placement_implemented (GfPlacement placement)
{
  switch (placement)
    {
      case GF_PLACEMENT_AUTO_ARRANGE_ICONS:
        return TRUE;

      case GF_PLACEMENT_ALIGN_ICONS_TO_GRID:
      case GF_PLACEMENT_FREE:
      case GF_PLACEMENT_LAST:
      default:
        break;
    }

  return FALSE;
}

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
static void
append_placement_submenu (GfIconView *self,
                          GtkWidget  *parent)
{
  GtkWidget *menu;
  GtkStyleContext *context;
  GSList *group;
  GfPlacement i;

  menu = gtk_menu_new ();
  gtk_menu_item_set_submenu (GTK_MENU_ITEM (parent), menu);

  context = gtk_widget_get_style_context (menu);
  gtk_style_context_add_class (context, GTK_STYLE_CLASS_CONTEXT_MENU);

  group = NULL;

  for (i = 0; i < GF_PLACEMENT_LAST; i++)
    {
      const char *label;
      GtkWidget *item;
      GfPlacementData *data;

      label = placement_to_string (i);

      item = gtk_radio_menu_item_new_with_label (group, label);
      group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (item));

      if (i == self->placement)
        gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (item), TRUE);

1154
1155
      gtk_widget_set_sensitive (item, is_placement_implemented (i));

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
      gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
      gtk_widget_show (item);

      data = g_new0 (GfPlacementData, 1);

      data->self = self;
      data->placement = i;

      g_signal_connect_data (item, "toggled",
                             G_CALLBACK (placement_cb),
                             data, free_placement_data,
                             0);
    }
}

typedef struct
{
  GfIconView *self;
  GfSortBy    sort_by;
} GfSortByData;

static void
free_sort_by_data (gpointer  data,
                   GClosure *closure)
{
  g_free (data);
}

static void
sort_by_toggled_cb (GtkCheckMenuItem *item,
                    GfSortByData     *data)
{
  if (!gtk_check_menu_item_get_active (item))
    return;

  g_settings_set_enum (data->self->settings, "sort-by", data->sort_by);
}

static void
sort_by_activate_cb (GtkMenuItem  *item,
                     GfSortByData *data)
{
  g_settings_set_enum (data->self->settings, "sort-by", data->sort_by);
1199
  resort_icons (data->self, FALSE);
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
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
}

static const char *
sort_by_to_string (GfSortBy sort_by)
{
  const char *string;

  string = NULL;

  switch (sort_by)
    {
      case GF_SORT_BY_NAME:
        string = _("Name");
        break;

      case GF_SORT_BY_DATE_MODIFIED:
        string = _("Date modified");
        break;

      case GF_SORT_BY_SIZE:
        string = _("Size");
        break;

      case GF_SORT_BY_LAST:
      default:
        break;
    }

  g_assert (string != NULL);

  return string;
}

static void
append_sort_by_submenu (GfIconView *self,
                        GtkWidget  *parent)
{
  GtkWidget *menu;
  GtkStyleContext *context;
  GSList *group;
  GfSortBy i;

  menu = gtk_menu_new ();
  gtk_menu_item_set_submenu (GTK_MENU_ITEM (parent), menu);

  context = gtk_widget_get_style_context (menu);
  gtk_style_context_add_class (context, GTK_STYLE_CLASS_CONTEXT_MENU);

  group = NULL;

  for (i = 0; i < GF_SORT_BY_LAST; i++)
    {
      const char *label;
      GtkWidget *item;
      GfSortByData *data;

      label = sort_by_to_string (i);

      if (self->placement == GF_PLACEMENT_AUTO_ARRANGE_ICONS)
        {
          item = gtk_radio_menu_item_new_with_label (group, label);
          group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (item));

          if (i == self->sort_by)
            gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (item), TRUE);
        }
      else
        {
          item = gtk_menu_item_new_with_label (label);
        }

      gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
      gtk_widget_show (item);

      data = g_new0 (GfSortByData, 1);

      data->self = self;
      data->sort_by = i;

      if (self->placement == GF_PLACEMENT_AUTO_ARRANGE_ICONS)
        {
          g_signal_connect_data (item, "toggled",
                                 G_CALLBACK (sort_by_toggled_cb),
                                 data, free_sort_by_data,
                                 0);
        }
      else
        {
          g_signal_connect_data (item, "activate",
                                 G_CALLBACK (sort_by_activate_cb),
                                 data, free_sort_by_data,
                                 0);
        }
    }
}

1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
static GtkWidget *
create_popup_menu (GfIconView *self)
{
  GtkWidget *popup_menu;
  GtkStyleContext *context;
  GtkWidget *item;

  popup_menu = gtk_menu_new ();

  context = gtk_widget_get_style_context (popup_menu);
  gtk_style_context_add_class (context, GTK_STYLE_CLASS_CONTEXT_MENU);

1308
  item = gtk_menu_item_new_with_label (_("New Folder"));
1309
1310
1311
1312
  gtk_menu_shell_append (GTK_MENU_SHELL (popup_menu), item);
  gtk_widget_show (item);

  g_signal_connect (item, "activate",
1313
                    G_CALLBACK (new_folder_cb),
1314
1315
1316
1317
1318
1319
                    self);

  item = gtk_separator_menu_item_new ();
  gtk_menu_shell_append (GTK_MENU_SHELL (popup_menu), item);
  gtk_widget_show (item);

1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
  item = gtk_menu_item_new_with_label (_("Placement"));
  gtk_menu_shell_append (GTK_MENU_SHELL (popup_menu), item);
  gtk_widget_show (item);

  append_placement_submenu (self, item);

  item = gtk_menu_item_new_with_label (_("Sort by"));
  gtk_menu_shell_append (GTK_MENU_SHELL (popup_menu), item);
  gtk_widget_show (item);

  append_sort_by_submenu (self, item);

  item = gtk_separator_menu_item_new ();
  gtk_menu_shell_append (GTK_MENU_SHELL (popup_menu), item);
  gtk_widget_show (item);

1336
1337
1338
1339
1340
1341
1342
1343
  item = gtk_menu_item_new_with_label (_("Change Background"));
  gtk_menu_shell_append (GTK_MENU_SHELL (popup_menu), item);
  gtk_widget_show (item);

  g_signal_connect (item, "activate",
                    G_CALLBACK (change_background_cb),
                    self);

1344
1345
1346
1347
1348
1349
1350
1351
  item = gtk_menu_item_new_with_label (_("Display Settings"));
  gtk_menu_shell_append (GTK_MENU_SHELL (popup_menu), item);
  gtk_widget_show (item);

  g_signal_connect (item, "activate",
                    G_CALLBACK (display_settings_cb),
                    self);

1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
  item = gtk_separator_menu_item_new ();
  gtk_menu_shell_append (GTK_MENU_SHELL (popup_menu), item);
  gtk_widget_show (item);

  item = gtk_menu_item_new_with_label (_("Open Terminal"));
  gtk_menu_shell_append (GTK_MENU_SHELL (popup_menu), item);
  gtk_widget_show (item);

  g_signal_connect (item, "activate",
                    G_CALLBACK (open_terminal_cb),
                    self);

1364
1365
1366
  return popup_menu;
}

1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
static void
multi_press_pressed_cb (GtkGestureMultiPress *gesture,
                        gint                  n_press,
                        gdouble               x,
                        gdouble               y,
                        GfIconView           *self)
{
  guint button;
  GdkEventSequence *sequence;
  const GdkEvent *event;

  button = gtk_gesture_single_get_current_button (GTK_GESTURE_SINGLE (gesture));
  sequence = gtk_gesture_single_get_current_sequence (GTK_GESTURE_SINGLE (gesture));
  event = gtk_gesture_get_last_event (GTK_GESTURE (gesture), sequence);

  if (event == NULL)
    return;

1385
1386
1387
  if (!gtk_widget_has_focus (GTK_WIDGET (self)))
    gtk_widget_grab_focus (GTK_WIDGET (self));

1388
1389
  if (button == GDK_BUTTON_PRIMARY)
    {
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
      GdkModifierType state;
      gboolean control_pressed;
      gboolean shift_pressed;

      gdk_event_get_state (event, &state);

      control_pressed = (state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK;
      shift_pressed = (state & GDK_SHIFT_MASK) == GDK_SHIFT_MASK;

      if (!control_pressed && !shift_pressed)
        {
          g_clear_pointer (&self->rubberband_icons, g_list_free);
          unselect_icons (self);
        }
1404
1405
1406
    }
  else if (button == GDK_BUTTON_SECONDARY)
    {
1407
1408
      GtkWidget *popup_menu;

1409
1410
      unselect_icons (self);

1411
1412
1413
1414
1415
      popup_menu = create_popup_menu (self);
      g_object_ref_sink (popup_menu);

      gtk_menu_popup_at_pointer (GTK_MENU (popup_menu), event);
      g_object_unref (popup_menu);
1416
1417
1418
    }
}

1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
static void
drag_begin_cb (GtkGestureDrag *gesture,
               gdouble         start_x,
               gdouble         start_y,
               GfIconView     *self)
{
  self->rubberband_rect = (GdkRectangle) { 0 };

  gtk_widget_queue_draw (GTK_WIDGET (self));
}

static void
drag_end_cb (GtkGestureDrag *gesture,
             gdouble         offset_x,
             gdouble         offset_y,
             GfIconView     *self)
{
  g_clear_pointer (&self->rubberband_icons, g_list_free);
  gtk_widget_queue_draw (GTK_WIDGET (self));
}

static void
drag_update_cb (GtkGestureDrag *gesture,
                gdouble         offset_x,
                gdouble         offset_y,
                GfIconView     *self)
{
  double start_x;
  double start_y;
  GdkRectangle old_rect;

  gtk_gesture_drag_get_start_point (GTK_GESTURE_DRAG (self->drag),
                                    &start_x, &start_y);

  old_rect = self->rubberband_rect;
  self->rubberband_rect = (GdkRectangle) {
    .x = start_x,
    .y = start_y,
    .width = ABS (offset_x),
    .height = ABS (offset_y)
  };

  if (offset_x < 0)
    self->rubberband_rect.x += offset_x;

  if (offset_y < 0)
    self->rubberband_rect.y += offset_y;

  if ((self->rubberband_rect.x > old_rect.x ||
       self->rubberband_rect.y > old_rect.y ||
       self->rubberband_rect.width < old_rect.width ||
       self->rubberband_rect.height < old_rect.height) &&
      self->rubberband_icons != NULL)
    {
      GList *rubberband_icons;
      GList *l;

      rubberband_icons = g_list_copy (self->rubberband_icons);

      for (l = rubberband_icons; l != NULL; l = l->next)
        {
          GfIcon *icon;
          GtkAllocation allocation;

          icon = l->data;

          gtk_widget_get_allocation (GTK_WIDGET (icon), &allocation);

          if (!gdk_rectangle_intersect (&self->rubberband_rect,
                                        &allocation,
                                        NULL))
            {
              if (gf_icon_get_selected (icon))
1492
                gf_icon_set_selected (icon, FALSE);
1493
              else
1494
                gf_icon_set_selected (icon, TRUE);
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553

              self->rubberband_icons = g_list_remove (self->rubberband_icons,
                                                      icon);
            }
        }

      g_list_free (rubberband_icons);
    }

  if (self->rubberband_rect.x < old_rect.x ||
      self->rubberband_rect.y < old_rect.y ||
      self->rubberband_rect.width > old_rect.width ||
      self->rubberband_rect.height > old_rect.height)
    {
      GList *rubberband_icons;
      GList *monitor_views;
      GList *l;

      rubberband_icons = NULL;
      monitor_views = get_monitor_views (self);

      for (l = monitor_views; l != NULL; l = l->next)
        {
          GfMonitorView *monitor_view;
          GtkAllocation allocation;
          GdkRectangle rect;

          monitor_view = l->data;

          gtk_widget_get_allocation (GTK_WIDGET (monitor_view), &allocation);

          if (gdk_rectangle_intersect (&self->rubberband_rect,
                                       &allocation,
                                       &rect))
            {
              GList *icons;

              icons = gf_monitor_view_get_icons (monitor_view, &rect);
              rubberband_icons = g_list_concat (rubberband_icons, icons);
            }
        }

      g_list_free (monitor_views);

      if (rubberband_icons != NULL)
        {
          for (l = rubberband_icons; l != NULL; l = l->next)
            {
              GfIcon *icon;

              icon = l->data;

              if (g_list_find (self->rubberband_icons, icon) != NULL)
                continue;

              self->rubberband_icons = g_list_prepend (self->rubberband_icons,
                                                       icon);

              if (gf_icon_get_selected (icon))
1554
                gf_icon_set_selected (icon, FALSE);
1555
              else
1556
                gf_icon_set_selected (icon, TRUE);
1557
1558
1559
1560
1561
1562
1563
1564
1565
            }

          g_list_free (rubberband_icons);
        }
    }

  gtk_widget_queue_draw (GTK_WIDGET (self));
}

1566
1567
1568
1569
static void
view_foreach_cb (GtkWidget *widget,
                 gpointer   user_data)
{
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
  GfIconView *self;
  GList *l;

  self = GF_ICON_VIEW (user_data);

  for (l = self->icons; l != NULL; l = l->next)
    {
      GfIconInfo *info;

      info = (GfIconInfo *) l->data;

      if (info->icon == widget)
        {
          gtk_container_remove (GTK_CONTAINER (info->view), widget);
          info->view = NULL;
          break;
        }
    }
1588
1589
1590
1591
1592
1593
}

static void
size_changed_cb (GtkWidget  *view,
                 GfIconView *self)
{
1594
  gtk_container_foreach (GTK_CONTAINER (view), view_foreach_cb, self);
1595
  add_icons (self);
1596
1597
}

1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
static void
append_home_icon (GfIconView *self)
{
  GError *error;
  GtkWidget *icon;

  if (self->home_info != NULL)
    return;

  error = NULL;
  icon = gf_home_icon_new (self, &error);

  if (error != NULL)
    {
      g_warning ("%s", error->message);
      g_error_free (error);
      return;
    }

  self->home_info = create_icon_info (self, icon);
  self->icons = g_list_prepend (self->icons, self->home_info);
}

static void
append_trash_icon (GfIconView *self)
{
  GError *error;
  GtkWidget *icon;

  if (self->trash_info != NULL)
    return;

  error = NULL;
  icon = gf_trash_icon_new (self, &error);

  if (error != NULL)
    {
      g_warning ("%s", error->message);
      g_error_free (error);
      return;
    }

  self->trash_info = create_icon_info (self, icon);
  self->icons = g_list_prepend (self->icons, self->trash_info);
}

1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
static void
next_files_cb (GObject      *object,
               GAsyncResult *res,
               gpointer      user_data)
{
  GFileEnumerator *enumerator;
  GList *files;
  GError *error;
  GfIconView *self;
  GList *l;

  enumerator = G_FILE_ENUMERATOR (object);

  error = NULL;
  files = g_file_enumerator_next_files_finish (enumerator, res, &error);

  if (error != NULL)
    {
      if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
        g_warning ("%s", error->message);

      g_error_free (error);
      return;
    }

  self = GF_ICON_VIEW (user_data);

  for (l = files; l != NULL; l = l->next)
    {
      GFileInfo *info;
      GFile *file;
1675
      GtkWidget *icon;
1676
      GfIconInfo *icon_info;
1677
1678
1679
1680

      info = l->data;
      file = g_file_enumerator_get_child (enumerator, info);

1681
      icon = gf_icon_new (self, file, info);
1682
1683
      g_object_unref (file);

1684
      icon_info = create_icon_info (self, icon);
1685
      self->icons = g_list_prepend (self->icons, icon_info);
1686
1687
    }

1688
1689
1690
1691
1692
1693
  if (g_settings_get_boolean (self->settings, "show-home"))
    append_home_icon (self);

  if (g_settings_get_boolean (self->settings, "show-trash"))
    append_trash_icon (self);

1694
  g_list_free_full (files, g_object_unref);
1695

1696
1697
1698
  if (self->placement == GF_PLACEMENT_AUTO_ARRANGE_ICONS)
    sort_icons (self);

1699
  add_icons (self);
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
}

static void
enumerate_children_cb (GObject      *object,
                       GAsyncResult *res,
                       gpointer      user_data)
{
  GFileEnumerator *enumerator;
  GError *error;
  GfIconView *self;

  error = NULL;
  enumerator = g_file_enumerate_children_finish (G_FILE (object), res, &error);

  if (error != NULL)
    {
      if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
        g_warning ("%s", error->message);

      g_error_free (error);
      return;
    }

  self = GF_ICON_VIEW (user_data);

  g_file_enumerator_next_files_async (enumerator,
                                      G_MAXINT32,
                                      G_PRIORITY_LOW,
                                      self->cancellable,
                                      next_files_cb,
                                      user_data);

  g_object_unref (enumerator);
}

static void
enumerate_desktop (GfIconView *self)
{
  char *attributes;

1740
  attributes = gf_icon_view_get_file_attributes (self);
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752

  g_file_enumerate_children_async (self->desktop,
                                   attributes,
                                   G_FILE_QUERY_INFO_NONE,
                                   G_PRIORITY_LOW,
                                   self->cancellable,
                                   enumerate_children_cb,
                                   self);

  g_free (attributes);
}

1753
static void
1754
nautilus_ready_cb (GObject      *object,
1755
1756
1757
1758
1759
                   GAsyncResult *res,
                   gpointer      user_data)

{
  GError *error;
1760
  GfNautilus2Gen *nautilus;
1761
1762
1763
  GfIconView *self;

  error = NULL;
1764
  nautilus = gf_nautilus2_gen_proxy_new_for_bus_finish (res, &error);
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778

  if (error != NULL)
    {
      if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
        g_warning ("%s", error->message);

      g_error_free (error);
      return;
    }

  self = GF_ICON_VIEW (user_data);
  self->nautilus = nautilus;
}

1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
static void
file_manager_ready_cb (GObject     *object,
                       GAsyncResult *res,
                       gpointer      user_data)

{
  GError *error;
  GfFileManagerGen *file_manager;
  GfIconView *self;

  error = NULL;
  file_manager = gf_file_manager_gen_proxy_new_for_bus_finish (res, &error);

  if (error != NULL)
    {
      if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
        g_warning ("%s", error->message);

      g_error_free (error);
      return;
    }

  self = GF_ICON_VIEW (user_data);
  self->file_manager = file_manager;
}

1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
static GtkWidget *
find_monitor_view_by_monitor (GfIconView *self,
                              GdkMonitor *monitor)
{
  GfMonitorView *view;
  GList *children;
  GList *l;

  children = gtk_container_get_children (GTK_CONTAINER (self->fixed));

  for (l = children; l != NULL; l = l->next)
    {
      view = GF_MONITOR_VIEW (l->data);

      if (gf_monitor_view_get_monitor (view) == monitor)
        break;

      view = NULL;
    }

  g_list_free (children);

  return GTK_WIDGET (view);
}

static void
1831
1832
workarea_watcher_changed_cb (GfWorkareaWatcher *workarea_watcher,
                             GfIconView        *self)
1833
{
1834
1835
  GList *children;
  GList *l;
1836

1837
  children = gtk_container_get_children (GTK_CONTAINER (self->fixed));
1838

1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
  for (l = children; l != NULL; l = l->next)
    {
      GfMonitorView *view;
      GdkMonitor *monitor;
      GdkRectangle workarea;

      view = GF_MONITOR_VIEW (l->data);

      monitor = gf_monitor_view_get_monitor (view);
      gdk_monitor_get_workarea (monitor, &workarea);

      gf_monitor_view_set_size (view, workarea.width, workarea.height);
      gtk_fixed_move (GTK_FIXED (self->fixed), l->data, workarea.x, workarea.y);
    }
1853

1854
  g_list_free (children);
1855
1856
1857
1858
1859
1860
}

static void
create_monitor_view (GfIconView *self,
                     GdkMonitor *monitor)
{
1861
1862
  guint column_spacing;
  guint row_spacing;
1863
1864
1865
  GdkRectangle workarea;
  GtkWidget *view;

1866
1867
  column_spacing = g_settings_get_uint (self->settings, "column-spacing");
  row_spacing = g_settings_get_uint (self->settings, "row-spacing");
1868