aboutsummaryrefslogtreecommitdiff
path: root/libqmenumodel/src/gtk/gtkactionobserveritem.c
blob: aebbcc3fd8b4dc50b09a4fe439216656cc64e2f1 (plain)
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

#include "gtkactionobserveritem.h"

typedef GObjectClass GtkActionObserverItemClass;

struct _GtkActionObserverItem
{
  GObject parent_instance;
  GtkActionObservable *observable;
  gchar* action_name;

  GtkActionAddedFunc action_added;
  GtkActionEnabledChangedFunc action_enabled_changed;
  GtkActionStateChangedFunc action_state_changed;
  GtkActionRemovedFunc action_removed;
};

static void gtk_action_observer_item_init_observer_iface (GtkActionObserverInterface *iface);
G_DEFINE_TYPE_WITH_CODE (GtkActionObserverItem, gtk_action_observer_item, G_TYPE_OBJECT,
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTION_OBSERVER, gtk_action_observer_item_init_observer_iface))

static void
gtk_action_observer_item_finalize (GObject *object)
{
  GtkActionObserverItem *self = GTK_ACTION_OBSERVER_ITEM (object);

  if (self->observable)
    g_object_unref (self->observable);

  if (self->action_name)
    g_free(self->action_name);

  G_OBJECT_CLASS (gtk_action_observer_item_parent_class)->finalize (object);
}

static void
gtk_action_observer_item_init (GtkActionObserverItem * self)
{
}

static void
gtk_action_observer_item_class_init (GtkActionObserverItemClass *class)
{
  class->finalize = gtk_action_observer_item_finalize;
}

static void
gtk_action_observer_item_action_added (GtkActionObserver   *observer,
                                    GtkActionObservable *observable,
                                    const gchar         *action_name,
                                    const GVariantType  *parameter_type,
                                    gboolean             enabled,
                                    GVariant            *state)
{
  g_return_if_fail (GTK_IS_ACTION_OBSERVER_ITEM (observer));

  GtkActionObserverItem* self;
  self = GTK_ACTION_OBSERVER_ITEM (observer);
  self->action_added(self, action_name, enabled, state);
}

static void
gtk_action_observer_item_action_enabled_changed (GtkActionObserver   *observer,
                                              GtkActionObservable *observable,
                                              const gchar         *action_name,
                                              gboolean             enabled)
{
  g_return_if_fail (GTK_IS_ACTION_OBSERVER_ITEM (observer));

  GtkActionObserverItem* self;
  self = GTK_ACTION_OBSERVER_ITEM (observer);
  self->action_enabled_changed(self, action_name, enabled);
}

static void
gtk_action_observer_item_action_state_changed (GtkActionObserver   *observer,
                                            GtkActionObservable *observable,
                                            const gchar         *action_name,
                                            GVariant            *state)
{
  g_return_if_fail (GTK_IS_ACTION_OBSERVER_ITEM (observer));

  GtkActionObserverItem* self;
  self = GTK_ACTION_OBSERVER_ITEM (observer);
  self->action_state_changed(self, action_name, state);
}

static void
gtk_action_observer_item_action_removed (GtkActionObserver   *observer,
                                      GtkActionObservable *observable,
                                      const gchar         *action_name)
{
  g_return_if_fail (GTK_IS_ACTION_OBSERVER_ITEM (observer));

  GtkActionObserverItem* self;
  self = GTK_ACTION_OBSERVER_ITEM (observer);
  self->action_removed(self, action_name);
}

static void
gtk_action_observer_item_init_observer_iface (GtkActionObserverInterface *iface)
{
  iface->action_added = gtk_action_observer_item_action_added;
  iface->action_enabled_changed = gtk_action_observer_item_action_enabled_changed;
  iface->action_state_changed = gtk_action_observer_item_action_state_changed;
  iface->action_removed = gtk_action_observer_item_action_removed;
}

GtkActionObserverItem*
gtk_action_observer_item_new (GtkActionObservable *observable,
                              GtkActionAddedFunc action_added,
                              GtkActionEnabledChangedFunc action_enabled_changed,
                              GtkActionStateChangedFunc action_state_changed,
                              GtkActionRemovedFunc action_removed)
{
  GtkActionObserverItem* self;
  self = g_object_new (GTK_TYPE_ACTION_OBSERVER_ITEM, NULL);
  self->observable =  g_object_ref (observable);
  self->action_name = NULL;

  self->action_added = action_added;
  self->action_enabled_changed = action_enabled_changed;
  self->action_state_changed = action_state_changed;
  self->action_removed = action_removed;

  return self;
}

void
gtk_action_observer_item_register_action (GtkActionObserverItem *self,
                                         const gchar* action_name)
{
  gtk_action_observer_item_unregister_action(self);

  if (action_name && g_strcmp0(action_name, "") != 0) {
    self->action_name = g_strdup (action_name);

    if (g_strcmp0(self->action_name, "") != 0) {
      gtk_action_observable_register_observer (self->observable, self->action_name, GTK_ACTION_OBSERVER (self));
    }
  }
}

void
gtk_action_observer_item_unregister_action (GtkActionObserverItem *self)
{
  if (self->action_name) {
    gtk_action_observable_unregister_observer(self->observable, self->action_name, GTK_ACTION_OBSERVER (self));
    g_free(self->action_name);
    self->action_name = NULL;
  }
}