aboutsummaryrefslogtreecommitdiff
path: root/libqmenumodel/src/gtk/gtksimpleactionobserver.c
blob: 054b82f6a5935a9a8ebef3856f3f60bb134cf743 (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
153
154
155
156
157
158
159
160
/*
 * Copyright © 2013 Canonical Limited
 *
 * This library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * licence or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors: Nick Dedekind <nick.dedekind@canonical.com
 */

#include "gtksimpleactionobserver.h"

typedef GObjectClass GtkSimpleActionObserverClass;

struct _GtkSimpleActionObserver
{
  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_simple_action_observer_init_observer_iface (GtkActionObserverInterface *iface);
G_DEFINE_TYPE_WITH_CODE (GtkSimpleActionObserver, gtk_simple_action_observer, G_TYPE_OBJECT,
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTION_OBSERVER, gtk_simple_action_observer_init_observer_iface))

static void
gtk_simple_action_observer_finalize (GObject *object)
{
  GtkSimpleActionObserver *self = GTK_SIMPLE_ACTION_OBSERVER (object);

  g_clear_object (&self->observable);

  g_free(self->action_name);

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

static void
gtk_simple_action_observer_init (GtkSimpleActionObserver * self)
{
}

static void
gtk_simple_action_observer_class_init (GtkSimpleActionObserverClass *class)
{
  class->finalize = gtk_simple_action_observer_finalize;
}

static void
gtk_simple_action_observer_action_added (GtkActionObserver   *observer,
                                         GtkActionObservable *observable,
                                         const gchar         *action_name,
                                         const GVariantType  *parameter_type,
                                         gboolean             enabled,
                                         GVariant            *state)
{
  GtkSimpleActionObserver* self;
  self = GTK_SIMPLE_ACTION_OBSERVER (observer);
  self->action_added(self, action_name, enabled, state);
}

static void
gtk_simple_action_observer_action_enabled_changed (GtkActionObserver   *observer,
                                                   GtkActionObservable *observable,
                                                   const gchar         *action_name,
                                                   gboolean             enabled)
{
  GtkSimpleActionObserver* self;
  self = GTK_SIMPLE_ACTION_OBSERVER (observer);
  self->action_enabled_changed(self, action_name, enabled);
}

static void
gtk_simple_action_observer_action_state_changed (GtkActionObserver   *observer,
                                                 GtkActionObservable *observable,
                                                 const gchar         *action_name,
                                                 GVariant            *state)
{
  GtkSimpleActionObserver* self;
  self = GTK_SIMPLE_ACTION_OBSERVER (observer);
  self->action_state_changed(self, action_name, state);
}

static void
gtk_simple_action_observer_action_removed (GtkActionObserver   *observer,
                                           GtkActionObservable *observable,
                                           const gchar         *action_name)
{
  GtkSimpleActionObserver* self;
  self = GTK_SIMPLE_ACTION_OBSERVER (observer);
  self->action_removed(self, action_name);
}

static void
gtk_simple_action_observer_init_observer_iface (GtkActionObserverInterface *iface)
{
  iface->action_added = gtk_simple_action_observer_action_added;
  iface->action_enabled_changed = gtk_simple_action_observer_action_enabled_changed;
  iface->action_state_changed = gtk_simple_action_observer_action_state_changed;
  iface->action_removed = gtk_simple_action_observer_action_removed;
}

GtkSimpleActionObserver*
gtk_simple_action_observer_new (GtkActionObservable         *observable,
                                GtkActionAddedFunc           action_added,
                                GtkActionEnabledChangedFunc  action_enabled_changed,
                                GtkActionStateChangedFunc    action_state_changed,
                                GtkActionRemovedFunc         action_removed)
{
  GtkSimpleActionObserver* self;
  self = g_object_new (GTK_TYPE_SIMPLE_ACTION_OBSERVER, 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_simple_action_observer_register_action (GtkSimpleActionObserver *self,
                                            const gchar             *action_name)
{
  gtk_simple_action_observer_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_simple_action_observer_unregister_action (GtkSimpleActionObserver *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;
  }
}