File: bamf-view.c

package info (click to toggle)
bamf 0.5.4-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,628 kB
  • sloc: ansic: 14,656; sh: 4,270; makefile: 423; xml: 249; python: 46
file content (1057 lines) | stat: -rw-r--r-- 29,268 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
/*
 * Copyright 2010-2012 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of either or both of the following licenses:
 *
 * 1) the GNU Lesser General Public License version 3, as published by the
 * Free Software Foundation; and/or
 * 2) the GNU Lesser General Public License version 2.1, as published by
 * the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the applicable version of the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of both the GNU Lesser General Public
 * License version 3 and version 2.1 along with this program.  If not, see
 * <http://www.gnu.org/licenses/>
 *
 * Authored by: Jason Smith <jason.smith@canonical.com>
 *              Neil Jagdish Patel <neil.patel@canonical.com>
 *              Marco Trevisan (TreviƱo) <3v1n0@ubuntu.com>
 *
 */
/**
 * SECTION:bamf-view
 * @short_description: The base class for all views
 *
 * #BamfView is the base class that all views need to derive from.
 */

#if HAVE_CONFIG_H
#include <config.h>
#endif

#include <libbamf-private/bamf-private.h>

#include "bamf-view.h"
#include "bamf-view-private.h"
#include "bamf-factory.h"
#include "bamf-tab.h"
#include "bamf-window.h"

G_DEFINE_TYPE (BamfView, bamf_view, G_TYPE_INITIALLY_UNOWNED);

#define BAMF_VIEW_GET_PRIVATE(o) \
  (G_TYPE_INSTANCE_GET_PRIVATE ((o), BAMF_TYPE_VIEW, BamfViewPrivate))

enum
{
  ACTIVE_CHANGED,
  CLOSED,
  CHILD_ADDED,
  CHILD_REMOVED,
  CHILD_MOVED,
  STARTING_CHANGED,
  RUNNING_CHANGED,
  URGENT_CHANGED,
  VISIBLE_CHANGED,
  NAME_CHANGED,
  ICON_CHANGED,
  LAST_SIGNAL
};

enum
{
  PROP_0,

  PROP_PATH,
  PROP_STARTING,
  PROP_RUNNING,
  PROP_ACTIVE,
  PROP_USER_VISIBLE,
  PROP_URGENT,
  PROP_LAST
};

static guint view_signals[LAST_SIGNAL] = { 0 };
static GParamSpec *properties[PROP_LAST] = { 0 };

struct _BamfViewPrivate
{
  BamfDBusItemView *proxy;
  GCancellable     *cancellable;
  gchar            *type;
  gchar            *cached_name;
  gchar            *cached_icon;
  GList            *cached_children;
  gboolean          reload_children;
  gboolean          is_closed;
  gboolean          sticky;
};

static void bamf_view_unset_proxy (BamfView *self);

/**
 * bamf_view_get_children:
 * @view: a #BamfView
 *
 * Note: Makes sever dbus calls the first time this is called on a view. Dbus messaging is reduced afterwards.
 *
 * Returns: (element-type Bamf.View) (transfer container): Returns a list of #BamfView which must be
 *           freed after usage. Elements of the list are owned by bamf and should not be unreffed.
 */
GList *
bamf_view_get_children (BamfView *view)
{
  g_return_val_if_fail (BAMF_IS_VIEW (view), NULL);

  if (BAMF_VIEW_GET_CLASS (view)->get_children)
    return BAMF_VIEW_GET_CLASS (view)->get_children (view);

  return g_list_copy (bamf_view_peek_children (view));
}

/**
 * bamf_view_peek_children:
 * @view: a #BamfView
 *
 * Note: Makes sever dbus calls the first time this is called on a view.
 * Dbus messaging is reduced afterwards.
 * Since: 0.5.2
 * Returns: (element-type Bamf.View) (transfer none): Returns a list of #BamfView which
 *           is owned by the #BamfView and should not freed or modified after usage.
 */
GList *
bamf_view_peek_children (BamfView *view)
{
  char ** children;
  int i, len;
  GList *results = NULL;
  GError *error = NULL;
  BamfViewPrivate *priv;
  BamfView *child;

  g_return_val_if_fail (BAMF_IS_VIEW (view), NULL);

  if (!_bamf_view_remote_ready (view))
    return NULL;

  priv = view->priv;

  if (priv->cached_children || !priv->reload_children)
    return priv->cached_children;

  if (!_bamf_dbus_item_view_call_children_sync (priv->proxy, &children, CANCELLABLE (view), &error))
    {
      g_warning ("Unable to fetch children: %s\n", error ? error->message : "");
      g_error_free (error);
      return NULL;
    }

  if (!children)
    return NULL;

  len = g_strv_length (children);

  for (i = len-1; i >= 0; --i)
    {
      child = _bamf_factory_view_for_path (_bamf_factory_get_default (), children[i]);

      if (BAMF_IS_VIEW (child))
        {
          results = g_list_prepend (results, g_object_ref (child));
        }
    }

  if (priv->cached_children)
    g_list_free_full (priv->cached_children, g_object_unref);

  priv->reload_children = FALSE;
  priv->cached_children = results;

  return priv->cached_children;
}

/**
 * bamf_view_has_child:
 * @view: a #BamfView
 *
 * Returns: %TRUE whether the #BamfView @view has the specified @child.
 */
gboolean
bamf_view_has_child (BamfView *view, BamfView *child)
{
  GList *l;

  g_return_val_if_fail (BAMF_IS_VIEW (view), FALSE);
  g_return_val_if_fail (BAMF_IS_VIEW (child), FALSE);

  for (l = bamf_view_peek_children (view); l; l = l->next)
    {
      if (l->data == child)
        return TRUE;
    }

  return FALSE;
}

/**
 * bamf_view_is_closed:
 * @view: a #BamfView
 *
 * Determines if the view is closed or not.
 */
gboolean
bamf_view_is_closed (BamfView *view)
{
  g_return_val_if_fail (BAMF_IS_VIEW (view), TRUE);

  return view->priv->is_closed;
}

/**
 * bamf_view_is_active:
 * @view: a #BamfView
 *
 * Determines if the view is currently active and focused by the user. Useful for an active window indicator.
 */
gboolean
bamf_view_is_active (BamfView *view)
{
  g_return_val_if_fail (BAMF_IS_VIEW (view), FALSE);

  if (BAMF_VIEW_GET_CLASS (view)->is_active)
    return BAMF_VIEW_GET_CLASS (view)->is_active (view);

  if (!_bamf_view_remote_ready (view))
    return FALSE;

  return _bamf_dbus_item_view_get_active (view->priv->proxy);
}

/**
 * bamf_view_is_user_visible:
 * @view: a #BamfView
 *
 * Returns: a boolean useful for determining if a particular view is "user visible". User visible
 * is a concept relating to whether or not a window should be shown in a launcher tasklist.
 *
 * Since: 0.4.0
 */
gboolean
bamf_view_is_user_visible (BamfView *self)
{
  g_return_val_if_fail (BAMF_IS_VIEW (self), FALSE);

  if (BAMF_VIEW_GET_CLASS (self)->is_user_visible)
    return BAMF_VIEW_GET_CLASS (self)->is_user_visible (self);

  if (!_bamf_view_remote_ready (self))
    return FALSE;

  return _bamf_dbus_item_view_get_user_visible (self->priv->proxy);
}

/**
 * bamf_view_user_visible: (skip)
 * @view: a #BamfView
 *
 * Returns: a boolean useful for determining if a particular view is "user visible". User visible
 * is a concept relating to whether or not a window should be shown in a launcher tasklist.
 *
 * Deprecated: 0.4.0
 */
gboolean
bamf_view_user_visible (BamfView *self)
{
  return bamf_view_is_user_visible (self);
}

/**
 * bamf_view_is_starting:
 * @view: a #BamfView
 *
 * Determines if the view is currently starting. Useful for the startup animation.
 */
gboolean
bamf_view_is_starting (BamfView *self)
{
  g_return_val_if_fail (BAMF_IS_VIEW (self), FALSE);

  if (BAMF_VIEW_GET_CLASS (self)->is_starting)
    return BAMF_VIEW_GET_CLASS (self)->is_starting (self);

  if (!_bamf_view_remote_ready (self))
    return FALSE;

  return _bamf_dbus_item_view_get_starting (self->priv->proxy);
}

/**
 * bamf_view_is_running:
 * @view: a #BamfView
 *
 * Determines if the view is currently running. Useful for a running window indicator.
 */
gboolean
bamf_view_is_running (BamfView *self)
{
  g_return_val_if_fail (BAMF_IS_VIEW (self), FALSE);

  if (BAMF_VIEW_GET_CLASS (self)->is_running)
    return BAMF_VIEW_GET_CLASS (self)->is_running (self);

  if (!_bamf_view_remote_ready (self))
    return FALSE;

  return _bamf_dbus_item_view_get_running (self->priv->proxy);
}

/**
 * bamf_view_is_urgent:
 * @view: a #BamfView
 *
 * Determines if the view is currently requiring attention. Useful for a running window indicator.
 */
gboolean
bamf_view_is_urgent (BamfView *self)
{
  g_return_val_if_fail (BAMF_IS_VIEW (self), FALSE);

  if (BAMF_VIEW_GET_CLASS (self)->is_urgent)
    return BAMF_VIEW_GET_CLASS (self)->is_urgent (self);

  if (!_bamf_view_remote_ready (self))
    return FALSE;

  return _bamf_dbus_item_view_get_urgent (self->priv->proxy);
}

void
_bamf_view_set_cached_name (BamfView *view, const char *name)
{
  g_return_if_fail (BAMF_IS_VIEW (view));

  if (!name || g_strcmp0 (name, view->priv->cached_name) == 0)
    return;

  g_free (view->priv->cached_name);
  view->priv->cached_name = NULL;

  if (name && name[0] != '\0')
    {
      view->priv->cached_name = g_strdup (name);
    }
}

void
_bamf_view_set_cached_icon (BamfView *view, const char *icon)
{
  g_return_if_fail (BAMF_IS_VIEW (view));

  if (!icon || g_strcmp0 (icon, view->priv->cached_icon) == 0)
    return;

  g_free (view->priv->cached_icon);
  view->priv->cached_icon = NULL;

  if (icon && icon[0] != '\0')
    {
      view->priv->cached_icon = g_strdup (icon);
    }
}

gboolean
bamf_view_is_sticky (BamfView *view)
{
  g_return_val_if_fail (BAMF_IS_VIEW (view), FALSE);

  return view->priv->sticky;
}

void
bamf_view_set_sticky (BamfView *view, gboolean sticky)
{
  g_return_if_fail (BAMF_IS_VIEW (view));

  if (view->priv->sticky == sticky)
    return;

  view->priv->sticky = sticky;

  if (sticky)
    {
      g_object_ref_sink (view);
    }
  else
    {
      g_object_unref (view);
    }

  if (BAMF_VIEW_GET_CLASS (view)->set_sticky)
    return BAMF_VIEW_GET_CLASS (view)->set_sticky (view, sticky);
}

/**
 * bamf_view_get_icon:
 * @view: a #BamfView
 *
 * Gets the icon of a view. This icon is used to visually represent the view.
 */
gchar *
bamf_view_get_icon (BamfView *self)
{
  BamfViewPrivate *priv;

  g_return_val_if_fail (BAMF_IS_VIEW (self), NULL);
  priv = self->priv;

  if (BAMF_VIEW_GET_CLASS (self)->get_icon)
    return BAMF_VIEW_GET_CLASS (self)->get_icon (self);

  if (!_bamf_view_remote_ready (self))
    return g_strdup (priv->cached_icon);

  return _bamf_dbus_item_view_dup_icon (priv->proxy);
}

/**
 * bamf_view_get_name:
 * @view: a #BamfView
 *
 * Gets the name of a view. This name is a short name best used to represent the view with text.
 */
gchar *
bamf_view_get_name (BamfView *self)
{
  BamfViewPrivate *priv;

  g_return_val_if_fail (BAMF_IS_VIEW (self), NULL);
  priv = self->priv;

  if (BAMF_VIEW_GET_CLASS (self)->get_name)
    return BAMF_VIEW_GET_CLASS (self)->get_name (self);

  if (!_bamf_view_remote_ready (self))
    return g_strdup (priv->cached_name);

  return _bamf_dbus_item_view_dup_name (priv->proxy);
}

gboolean
_bamf_view_remote_ready (BamfView *self)
{
  if (BAMF_IS_VIEW (self) && G_IS_DBUS_PROXY (self->priv->proxy))
    return !self->priv->is_closed;

  return FALSE;
}

/**
 * bamf_view_get_view_type: (virtual view_type)
 * @view: a #BamfView
 *
 * The view type of a window is a short string used to represent all views of the same class. These
 * descriptions should not be used to do casting as they are not considered stable.
 */
const gchar *
bamf_view_get_view_type (BamfView *self)
{
  BamfViewPrivate *priv;
  char *type = NULL;
  GError *error = NULL;

  g_return_val_if_fail (BAMF_IS_VIEW (self), NULL);
  priv = self->priv;

  if (BAMF_VIEW_GET_CLASS (self)->view_type)
    return BAMF_VIEW_GET_CLASS (self)->view_type (self);

  if (priv->type)
    return priv->type;

  if (!_bamf_view_remote_ready (self))
    return NULL;

  if (!_bamf_dbus_item_view_call_view_type_sync (priv->proxy, &type, CANCELLABLE (self), &error))
    {
      const gchar *path = g_dbus_proxy_get_object_path (G_DBUS_PROXY (priv->proxy));
      g_warning ("Failed to fetch view type at %s: %s", path, error ? error->message : "");
      g_error_free (error);
      return NULL;
    }

  priv->type = type;
  return type;
}

BamfClickBehavior
bamf_view_get_click_suggestion (BamfView *self)
{
  g_return_val_if_fail (BAMF_IS_VIEW (self), BAMF_CLICK_BEHAVIOR_NONE);

  if (BAMF_VIEW_GET_CLASS (self)->click_behavior)
    return BAMF_VIEW_GET_CLASS (self)->click_behavior (self);

  return BAMF_CLICK_BEHAVIOR_NONE;
}

static void
bamf_view_child_xid_changed (GObject *object, GParamSpec *pspec, gpointer user_data)
{
  BamfView *self;

  self = (BamfView *)user_data;

  g_signal_emit (G_OBJECT (self), view_signals[CHILD_MOVED], 0, BAMF_VIEW (object));
  g_signal_emit (G_OBJECT (self), view_signals[VISIBLE_CHANGED], 0);
}

static void
bamf_view_on_child_added (BamfDBusItemView *proxy, const char *path, BamfView *self)
{
  BamfView *view;
  BamfViewPrivate *priv;

  view = _bamf_factory_view_for_path (_bamf_factory_get_default (), path);
  priv = self->priv;

  g_return_if_fail (BAMF_IS_VIEW (view));

  if (BAMF_IS_TAB (view))
    {
      g_signal_connect (view, "notify::xid",
                        G_CALLBACK (bamf_view_child_xid_changed), self);
    }

  if (!g_list_find (priv->cached_children, view))
    {
      g_object_ref (view);
      priv->cached_children = g_list_prepend (priv->cached_children, view);
    }

  g_signal_emit (G_OBJECT (self), view_signals[CHILD_ADDED], 0, view);
}

static void
bamf_view_on_child_removed (BamfDBusItemView *proxy, char *path, BamfView *self)
{
  BamfView *view;
  BamfViewPrivate *priv;
  gboolean was_cached = FALSE;

  view = _bamf_factory_view_for_path (_bamf_factory_get_default (), path);
  priv = self->priv;

  g_return_if_fail (BAMF_IS_VIEW (view));

  if (BAMF_IS_TAB (view))
    {
      g_signal_handlers_disconnect_by_func (view, bamf_view_child_xid_changed, self);
    }

  if (priv->cached_children)
    {
      GList *l = g_list_find (priv->cached_children, view);

      if (l)
        {
          priv->cached_children = g_list_delete_link (priv->cached_children, l);
          was_cached = TRUE;
        }
    }

  g_signal_emit (G_OBJECT (self), view_signals[CHILD_REMOVED], 0, view);

  if (was_cached)
    g_object_unref (view);
}

static void
bamf_view_on_name_owner_changed (BamfDBusItemView *proxy, GParamSpec *param, BamfView *self)
{
  /* This is called when the bamfdaemon is killed / started */
  gchar *name_owner = g_dbus_proxy_get_name_owner (G_DBUS_PROXY (proxy));

  if (!name_owner)
    {
      if (self->priv->cached_children)
        {
          g_list_free_full (self->priv->cached_children, g_object_unref);
          self->priv->reload_children = TRUE;
          self->priv->cached_children = NULL;
        }

      if (self->priv->cached_name)
        {
          const char *cached_name = self->priv->cached_name;
          g_signal_emit (G_OBJECT (self), view_signals[NAME_CHANGED], 0, NULL, cached_name);
        }

      if (self->priv->cached_icon)
        {
          const char *cached_icon = self->priv->cached_icon;
          g_signal_emit (G_OBJECT (self), view_signals[ICON_CHANGED], 0, cached_icon);
        }

      _bamf_view_set_closed (self, TRUE);
      g_signal_emit (G_OBJECT (self), view_signals[CLOSED], 0);
    }

  g_free (name_owner);
}

static void
bamf_view_on_active_changed (BamfDBusItemView *proxy, GParamSpec *param, BamfView *self)
{
  gboolean active = _bamf_dbus_item_view_get_active (proxy);
  g_signal_emit (G_OBJECT (self), view_signals[ACTIVE_CHANGED], 0, active);
  g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_ACTIVE]);
}

static void
bamf_view_on_name_changed (BamfDBusItemView *proxy, GParamSpec *param, BamfView *self)
{
  const char *new_name = _bamf_dbus_item_view_get_name (proxy);
  const char *old_name = self->priv->cached_name;
  g_signal_emit (self, view_signals[NAME_CHANGED], 0, old_name, new_name);
  _bamf_view_set_cached_name (self, new_name);
}

static void
bamf_view_on_icon_changed (BamfDBusItemView *proxy, GParamSpec *param, BamfView *self)
{
  const char *icon = _bamf_dbus_item_view_get_icon (proxy);
  g_signal_emit (self, view_signals[ICON_CHANGED], 0, icon);
  _bamf_view_set_cached_icon (self, icon);
}

static void
bamf_view_on_starting_changed (BamfDBusItemView *proxy, GParamSpec *param, BamfView *self)
{
  gboolean starting = _bamf_dbus_item_view_get_starting (proxy);
  g_signal_emit (G_OBJECT (self), view_signals[STARTING_CHANGED], 0, starting);
  g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_STARTING]);
}


static void
bamf_view_on_running_changed (BamfDBusItemView *proxy, GParamSpec *param, BamfView *self)
{
  gboolean running = _bamf_dbus_item_view_get_running (proxy);
  g_signal_emit (G_OBJECT (self), view_signals[RUNNING_CHANGED], 0, running);
  g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_RUNNING]);
}

static void
bamf_view_on_urgent_changed (BamfDBusItemView *proxy, GParamSpec *param, BamfView *self)
{
  gboolean urgent = _bamf_dbus_item_view_get_urgent (proxy);
  g_signal_emit (G_OBJECT (self), view_signals[URGENT_CHANGED], 0, urgent);
  g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_URGENT]);
}

static void
bamf_view_on_user_visible_changed (BamfDBusItemView *proxy, GParamSpec *param, BamfView *self)
{
  gboolean user_visible = _bamf_dbus_item_view_get_user_visible (proxy);
  g_signal_emit (G_OBJECT (self), view_signals[VISIBLE_CHANGED], 0, user_visible);
  g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_USER_VISIBLE]);
}

GCancellable *
_bamf_view_get_cancellable (BamfView *view)
{
  g_return_val_if_fail (BAMF_IS_VIEW (view), NULL);

  return view->priv->cancellable;
}

void
_bamf_view_set_closed (BamfView *self, gboolean closed)
{
  BamfViewPrivate *priv;
  g_return_if_fail (BAMF_IS_VIEW (self));

  priv = self->priv;

  if (priv->is_closed != closed)
    {
      priv->is_closed = closed;

      if (closed)
        {
          g_cancellable_cancel (priv->cancellable);
          g_list_free_full (priv->cached_children, g_object_unref);
          priv->cached_children = NULL;
        }
      else
        {
          g_cancellable_reset (priv->cancellable);
        }
    }
}

static void
bamf_view_on_closed (BamfDBusItemView *proxy, BamfView *self)
{
  _bamf_view_set_closed (self, TRUE);
  g_signal_emit (G_OBJECT (self), view_signals[CLOSED], 0);
}

static void
bamf_view_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec)
{
  switch (property_id)
    {
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    }
}

static void
bamf_view_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec)
{
  BamfView *self;

  self = BAMF_VIEW (object);

  switch (property_id)
    {
      case PROP_PATH:
        g_value_set_string (value, bamf_view_is_closed (self) ? NULL : _bamf_view_get_path (self));
        break;

      case PROP_ACTIVE:
        g_value_set_boolean (value, bamf_view_is_active (self));
        break;

      case PROP_STARTING:
        g_value_set_boolean (value, bamf_view_is_starting (self));
        break;

      case PROP_RUNNING:
        g_value_set_boolean (value, bamf_view_is_running (self));
        break;

      case PROP_URGENT:
        g_value_set_boolean (value, bamf_view_is_urgent (self));
        break;

      case PROP_USER_VISIBLE:
        g_value_set_boolean (value, bamf_view_is_user_visible (self));
        break;

      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    }
}

static void
bamf_view_unset_proxy (BamfView *self)
{
  BamfViewPrivate *priv;

  g_return_if_fail (BAMF_IS_VIEW (self));
  priv = self->priv;

  if (!priv->proxy)
    return;

  g_signal_handlers_disconnect_by_data (priv->proxy, self);

  g_object_unref (priv->proxy);
  priv->proxy = NULL;
}

static void
bamf_view_dispose (GObject *object)
{
  BamfView *view;
  BamfViewPrivate *priv;

  view = BAMF_VIEW (object);

  priv = view->priv;

  if (priv->cancellable)
    {
      g_cancellable_cancel (priv->cancellable);
      g_object_unref (priv->cancellable);
      priv->cancellable = NULL;
    }

  if (priv->type)
    {
      g_free (priv->type);
      priv->type = NULL;
    }

  if (priv->cached_icon)
    {
      g_free (priv->cached_icon);
      priv->cached_icon = NULL;
    }

  if (priv->cached_name)
    {
      g_free (priv->cached_name);
      priv->cached_name = NULL;
    }

  if (priv->cached_children)
    {
      g_list_free_full (priv->cached_children, g_object_unref);
      priv->cached_children = NULL;
    }

  bamf_view_unset_proxy (view);

  G_OBJECT_CLASS (bamf_view_parent_class)->dispose (object);
}

const char *
_bamf_view_get_path (BamfView *view)
{
  g_return_val_if_fail (BAMF_IS_VIEW (view), NULL);

  if (G_IS_DBUS_PROXY (view->priv->proxy))
    return g_dbus_proxy_get_object_path (G_DBUS_PROXY (view->priv->proxy));

  return NULL;
}

void
_bamf_view_reset_flags (BamfView *view)
{
  g_return_if_fail (BAMF_IS_VIEW (view));

  /* Notifying proxy properties makes the view to emit proper signals */
  g_object_notify (G_OBJECT (view->priv->proxy), "user-visible");
  g_object_notify (G_OBJECT (view->priv->proxy), "active");
  g_object_notify (G_OBJECT (view->priv->proxy), "starting");
  g_object_notify (G_OBJECT (view->priv->proxy), "running");
  g_object_notify (G_OBJECT (view->priv->proxy), "urgent");
  g_object_notify (G_OBJECT (view->priv->proxy), "name");
}

void
_bamf_view_set_path (BamfView *view, const char *path)
{
  BamfViewPrivate *priv;
  GError *error = NULL;

  g_return_if_fail (BAMF_IS_VIEW (view));
  g_return_if_fail (path);

  _bamf_view_set_closed (view, FALSE);

  if (g_strcmp0 (_bamf_view_get_path (view), path) == 0)
    {
      // The proxy path has not been changed, no need to unset and re-set it again
      _bamf_view_reset_flags (view);
      return;
    }

  bamf_view_unset_proxy (view);

  priv = view->priv;
  priv->reload_children = TRUE;

  priv->proxy = _bamf_dbus_item_view_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
                                                             G_DBUS_PROXY_FLAGS_NONE,
                                                             BAMF_DBUS_SERVICE_NAME,
                                                             path, CANCELLABLE (view),
                                                             &error);
  if (!G_IS_DBUS_PROXY (priv->proxy))
    {
      g_critical ("Unable to get %s view: %s", BAMF_DBUS_SERVICE_NAME, error ? error ? error->message : "" : "");
      g_error_free (error);
      return;
    }

  g_dbus_proxy_set_default_timeout (G_DBUS_PROXY (priv->proxy), BAMF_DBUS_DEFAULT_TIMEOUT);
  g_object_notify_by_pspec (G_OBJECT (view), properties[PROP_PATH]);

  g_signal_connect (priv->proxy, "notify::g-name-owner",
                    G_CALLBACK (bamf_view_on_name_owner_changed), view);

  g_signal_connect (priv->proxy, "notify::active",
                    G_CALLBACK (bamf_view_on_active_changed), view);

  g_signal_connect (priv->proxy, "notify::starting",
                    G_CALLBACK (bamf_view_on_starting_changed), view);

  g_signal_connect (priv->proxy, "notify::running",
                    G_CALLBACK (bamf_view_on_running_changed), view);

  g_signal_connect (priv->proxy, "notify::urgent",
                    G_CALLBACK (bamf_view_on_urgent_changed), view);

  g_signal_connect (priv->proxy, "notify::user-visible",
                    G_CALLBACK (bamf_view_on_user_visible_changed), view);

  g_signal_connect (priv->proxy, "notify::name",
                    G_CALLBACK (bamf_view_on_name_changed), view);

  g_signal_connect (priv->proxy, "notify::icon",
                    G_CALLBACK (bamf_view_on_icon_changed), view);

  g_signal_connect (priv->proxy, "child-added",
                    G_CALLBACK (bamf_view_on_child_added), view);

  g_signal_connect (priv->proxy, "child-removed",
                    G_CALLBACK (bamf_view_on_child_removed), view);

  g_signal_connect (priv->proxy, "closed",
                    G_CALLBACK (bamf_view_on_closed), view);

  _bamf_view_reset_flags (view);

  if (BAMF_VIEW_GET_CLASS (view)->set_path)
    BAMF_VIEW_GET_CLASS (view)->set_path (view, path);
}

static void
bamf_view_class_init (BamfViewClass *klass)
{
  GObjectClass *obj_class = G_OBJECT_CLASS (klass);

  obj_class->dispose      = bamf_view_dispose;
  obj_class->get_property = bamf_view_get_property;
  obj_class->set_property = bamf_view_set_property;

  properties[PROP_PATH] = g_param_spec_string ("path", "path", "path", NULL, G_PARAM_READABLE);
  g_object_class_install_property (obj_class, PROP_PATH, properties[PROP_PATH]);

  properties[PROP_ACTIVE] = g_param_spec_boolean ("active", "active", "active", FALSE, G_PARAM_READABLE);
  g_object_class_install_property (obj_class, PROP_ACTIVE, properties[PROP_ACTIVE]);

  properties[PROP_URGENT] = g_param_spec_boolean ("urgent", "urgent", "urgent", FALSE, G_PARAM_READABLE);
  g_object_class_install_property (obj_class, PROP_URGENT, properties[PROP_URGENT]);

  properties[PROP_STARTING] = g_param_spec_boolean ("starting", "starting", "starting", FALSE, G_PARAM_READABLE);
  g_object_class_install_property (obj_class, PROP_STARTING, properties[PROP_STARTING]);

  properties[PROP_RUNNING] = g_param_spec_boolean ("running", "running", "running", FALSE, G_PARAM_READABLE);
  g_object_class_install_property (obj_class, PROP_RUNNING, properties[PROP_RUNNING]);

  properties[PROP_USER_VISIBLE] = g_param_spec_boolean ("user-visible", "user-visible", "user-visible", FALSE, G_PARAM_READABLE);
  g_object_class_install_property (obj_class, PROP_USER_VISIBLE, properties[PROP_USER_VISIBLE]);

  g_type_class_add_private (obj_class, sizeof (BamfViewPrivate));

  view_signals [ACTIVE_CHANGED] =
    g_signal_new (BAMF_VIEW_SIGNAL_ACTIVE_CHANGED,
                  G_OBJECT_CLASS_TYPE (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (BamfViewClass, active_changed),
                  NULL, NULL, NULL,
                  G_TYPE_NONE, 1,
                  G_TYPE_BOOLEAN);

  view_signals [CLOSED] =
    g_signal_new (BAMF_VIEW_SIGNAL_CLOSED,
                  G_OBJECT_CLASS_TYPE (klass),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (BamfViewClass, closed),
                  NULL, NULL, NULL,
                  G_TYPE_NONE, 0);

  view_signals [CHILD_ADDED] =
    g_signal_new (BAMF_VIEW_SIGNAL_CHILD_ADDED,
                  G_OBJECT_CLASS_TYPE (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (BamfViewClass, child_added),
                  NULL, NULL, NULL,
                  G_TYPE_NONE, 1,
                  BAMF_TYPE_VIEW);

  view_signals [CHILD_REMOVED] =
    g_signal_new (BAMF_VIEW_SIGNAL_CHILD_REMOVED,
                  G_OBJECT_CLASS_TYPE (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (BamfViewClass, child_removed),
                  NULL, NULL, NULL,
                  G_TYPE_NONE, 1,
                  BAMF_TYPE_VIEW);

  view_signals [CHILD_MOVED] =
    g_signal_new (BAMF_VIEW_SIGNAL_CHILD_MOVED,
                  G_OBJECT_CLASS_TYPE (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (BamfViewClass, child_moved),
                  NULL, NULL, NULL,
                  G_TYPE_NONE, 1,
                  BAMF_TYPE_VIEW);

  view_signals [STARTING_CHANGED] =
    g_signal_new (BAMF_VIEW_SIGNAL_STARTING_CHANGED,
                  G_OBJECT_CLASS_TYPE (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (BamfViewClass, starting_changed),
                  NULL, NULL, NULL,
                  G_TYPE_NONE, 1,
                  G_TYPE_BOOLEAN);

  view_signals [RUNNING_CHANGED] =
    g_signal_new (BAMF_VIEW_SIGNAL_RUNNING_CHANGED,
                  G_OBJECT_CLASS_TYPE (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (BamfViewClass, running_changed),
                  NULL, NULL, NULL,
                  G_TYPE_NONE, 1,
                  G_TYPE_BOOLEAN);

  view_signals [URGENT_CHANGED] =
    g_signal_new (BAMF_VIEW_SIGNAL_URGENT_CHANGED,
                  G_OBJECT_CLASS_TYPE (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (BamfViewClass, urgent_changed),
                  NULL, NULL, NULL,
                  G_TYPE_NONE, 1,
                  G_TYPE_BOOLEAN);

  view_signals [VISIBLE_CHANGED] =
    g_signal_new (BAMF_VIEW_SIGNAL_USER_VISIBLE_CHANGED,
                  G_OBJECT_CLASS_TYPE (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (BamfViewClass, user_visible_changed),
                  NULL, NULL, NULL,
                  G_TYPE_NONE, 1,
                  G_TYPE_BOOLEAN);

  view_signals [NAME_CHANGED] =
    g_signal_new (BAMF_VIEW_SIGNAL_NAME_CHANGED,
                  G_OBJECT_CLASS_TYPE (klass),
                  0,
                  G_STRUCT_OFFSET (BamfViewClass, name_changed),
                  NULL, NULL, NULL,
                  G_TYPE_NONE, 2,
                  G_TYPE_STRING,
                  G_TYPE_STRING);

  view_signals [ICON_CHANGED] =
    g_signal_new (BAMF_VIEW_SIGNAL_ICON_CHANGED,
                  G_OBJECT_CLASS_TYPE (klass),
                  0,
                  G_STRUCT_OFFSET (BamfViewClass, icon_changed),
                  NULL, NULL, NULL,
                  G_TYPE_NONE, 1,
                  G_TYPE_STRING);
}

static void
bamf_view_init (BamfView *self)
{
  BamfViewPrivate *priv;

  priv = self->priv = BAMF_VIEW_GET_PRIVATE (self);
  priv->cancellable = g_cancellable_new ();
  priv->is_closed = TRUE;
  priv->reload_children = TRUE;
}