File: egg-action-group.h

package info (click to toggle)
libspelling 0.4.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 900 kB
  • sloc: ansic: 7,367; javascript: 33; sh: 21; makefile: 14
file content (223 lines) | stat: -rw-r--r-- 17,168 bytes parent folder | download | duplicates (2)
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
/* egg-action-group.h
 *
 * Copyright 2017-2023 Christian Hergert <chergert@redhat.com>
 *
 * 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.1 of the
 * License, 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 General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#pragma once

#include <gio/gio.h>

G_BEGIN_DECLS

#define EGG_DEFINE_ACTION_GROUP(Type, prefix, ...)                                \
struct _##Type##ActionEntry {                                                     \
  const gchar *name;                                                              \
  void (*activate) (Type *self, GVariant *param);                                 \
  const gchar *parameter_type;                                                    \
  const gchar *state;                                                             \
  void (*change_state) (Type *self, GVariant *state);                             \
} prefix##_actions[] = __VA_ARGS__;                                               \
                                                                                  \
typedef struct {                                                                  \
  GVariant *state;                                                                \
  GVariant *state_hint;                                                           \
  guint enabled : 1;                                                              \
} Type##ActionInfo;                                                               \
                                                                                  \
static gboolean                                                                   \
_##prefix##_has_action (GActionGroup *group,                                      \
                        const gchar *name)                                        \
{                                                                                 \
  for (guint i = 0; i < G_N_ELEMENTS(prefix##_actions); i++)                      \
    {                                                                             \
      if (g_strcmp0 (name, prefix##_actions[i].name) == 0)                        \
        return TRUE;                                                              \
    }                                                                             \
  return FALSE;                                                                   \
}                                                                                 \
                                                                                  \
static gchar **                                                                   \
_##prefix##_list_actions (GActionGroup *group)                                    \
{                                                                                 \
  GPtrArray *ar = g_ptr_array_new ();                                             \
                                                                                  \
  for (guint i = 0; i < G_N_ELEMENTS(prefix##_actions); i++)                      \
    g_ptr_array_add (ar, g_strdup (prefix##_actions[i].name));                    \
  g_ptr_array_add (ar, NULL);                                                     \
                                                                                  \
  return (gchar **)g_ptr_array_free (ar, FALSE);                                  \
}                                                                                 \
                                                                                  \
static void                                                                       \
_##prefix##_action_info_free (gpointer data)                                      \
{                                                                                 \
  Type##ActionInfo *info = data;                                                  \
  g_clear_pointer (&info->state, g_variant_unref);                                \
  g_clear_pointer (&info->state_hint, g_variant_unref);                           \
  g_slice_free (Type##ActionInfo, info);                                          \
}                                                                                 \
                                                                                  \
static Type##ActionInfo *                                                         \
_##prefix##_get_action_info (GActionGroup *group,                                 \
                             const gchar *name)                                   \
{                                                                                 \
  g_autofree gchar *fullname = g_strdup_printf ("ACTION-INFO:%s", name);          \
  Type##ActionInfo *info = g_object_get_data (G_OBJECT (group), fullname);        \
  if (info == NULL)                                                               \
    {                                                                             \
      info = g_slice_new0 (Type##ActionInfo);                                     \
      info->enabled = TRUE;                                                       \
      for (guint i = 0; i < G_N_ELEMENTS(prefix##_actions); i++)                  \
        {                                                                         \
          if (g_strcmp0 (prefix##_actions[i].name, name) == 0)                    \
            {                                                                     \
              if (prefix##_actions[i].state != NULL)                              \
                info->state = g_variant_parse (                                   \
                  NULL, prefix##_actions[i].state, NULL, NULL, NULL);             \
              break;                                                              \
            }                                                                     \
        }                                                                         \
      g_object_set_data_full (G_OBJECT (group), fullname, info,                   \
                              _##prefix##_action_info_free);                      \
    }                                                                             \
  return info;                                                                    \
}                                                                                 \
                                                                                  \
G_GNUC_UNUSED static inline GVariant *                                            \
prefix##_get_action_state (Type *self,                                            \
                           const gchar *name)                                     \
{                                                                                 \
  Type##ActionInfo *info = _##prefix##_get_action_info (G_ACTION_GROUP (self),    \
                                                        name);                    \
  return info->state;                                                             \
}                                                                                 \
                                                                                  \
G_GNUC_UNUSED static inline void                                                  \
prefix##_set_action_state (Type *self,                                            \
                           const gchar *name,                                     \
                           GVariant *state)                                       \
{                                                                                 \
  Type##ActionInfo *info = _##prefix##_get_action_info (G_ACTION_GROUP (self),    \
                                                        name);                    \
  if (state != info->state)                                                       \
    {                                                                             \
      g_clear_pointer (&info->state, g_variant_unref);                            \
      info->state = state ? g_variant_ref_sink (state) : NULL;                    \
      g_action_group_action_state_changed (G_ACTION_GROUP (self), name, state);   \
    }                                                                             \
}                                                                                 \
                                                                                  \
G_GNUC_UNUSED static inline void                                                  \
prefix##_set_action_enabled (Type *self,                                          \
                             const gchar *name,                                   \
                             gboolean enabled)                                    \
{                                                                                 \
  Type##ActionInfo *info = _##prefix##_get_action_info (G_ACTION_GROUP (self),    \
                                                        name);                    \
  if (enabled != info->enabled)                                                   \
    {                                                                             \
      info->enabled = !!enabled;                                                  \
      g_action_group_action_enabled_changed (G_ACTION_GROUP (self),               \
                                             name, enabled);                      \
    }                                                                             \
}                                                                                 \
                                                                                  \
static void                                                                       \
_##prefix##_change_action_state (GActionGroup *group,                             \
                                 const gchar *name,                               \
                                 GVariant *state)                                 \
{                                                                                 \
  for (guint i = 0; i < G_N_ELEMENTS(prefix##_actions); i++)                      \
    {                                                                             \
      if (g_strcmp0 (name, prefix##_actions[i].name) == 0)                        \
        {                                                                         \
          if (prefix##_actions[i].change_state)                                   \
            prefix##_actions[i].change_state ((Type*)group, state);               \
          return;                                                                 \
        }                                                                         \
    }                                                                             \
}                                                                                 \
                                                                                  \
static void                                                                       \
_##prefix##_activate_action (GActionGroup *group,                                 \
                             const gchar *name,                                   \
                             GVariant *param)                                     \
{                                                                                 \
  for (guint i = 0; i < G_N_ELEMENTS(prefix##_actions); i++)                      \
    {                                                                             \
      if (g_strcmp0 (name, prefix##_actions[i].name) == 0)                        \
        {                                                                         \
          if (prefix##_actions[i].activate)                                       \
            prefix##_actions[i].activate ((Type*)group, param);                   \
          return;                                                                 \
        }                                                                         \
    }                                                                             \
}                                                                                 \
                                                                                  \
static gboolean                                                                   \
_##prefix##_query_action (GActionGroup *group,                                    \
                          const gchar *name,                                      \
                          gboolean *enabled,                                      \
                          const GVariantType **parameter_type,                    \
                          const GVariantType **state_type,                        \
                          GVariant **state_hint,                                  \
                          GVariant **state)                                       \
{                                                                                 \
  if (enabled) *enabled = FALSE;                                                  \
  if (parameter_type) *parameter_type = NULL ;                                    \
  if (state_type) *state_type = NULL ;                                            \
  if (state_hint) *state_hint = NULL ;                                            \
  if (state) *state = NULL ;                                                      \
  for (guint i = 0; i < G_N_ELEMENTS(prefix##_actions); i++)                      \
    {                                                                             \
      if (g_strcmp0 (name, prefix##_actions[i].name) == 0)                        \
        {                                                                         \
          Type##ActionInfo *info = _##prefix##_get_action_info(group, name);      \
          if (prefix##_actions[i].change_state && state_type)                     \
            *state_type = prefix##_actions[i].parameter_type ?                    \
                          G_VARIANT_TYPE(prefix##_actions[i].parameter_type) :    \
                          NULL;                                                   \
          else if (prefix##_actions[i].activate && parameter_type)                \
            *parameter_type = prefix##_actions[i].parameter_type ?                \
                              G_VARIANT_TYPE(prefix##_actions[i].parameter_type) :\
                              NULL;                                               \
          if (state_hint)                                                         \
            *state_hint = info->state_hint != NULL ?                              \
                          g_variant_ref (info->state_hint) : NULL;                \
          if (state)                                                              \
            *state = info->state != NULL ?                                        \
                     g_variant_ref (info->state) : NULL;                          \
          if (enabled)                                                            \
            *enabled = info->enabled;                                             \
          return TRUE;                                                            \
        }                                                                         \
    }                                                                             \
  return FALSE;                                                                   \
}                                                                                 \
                                                                                  \
static void                                                                       \
prefix##_init_action_group (GActionGroupInterface *iface)                         \
{                                                                                 \
  iface->has_action = _##prefix##_has_action;                                     \
  iface->list_actions = _##prefix##_list_actions;                                 \
  iface->change_action_state = _##prefix##_change_action_state;                   \
  iface->activate_action = _##prefix##_activate_action;                           \
  iface->query_action = _##prefix##_query_action;                                 \
}

G_END_DECLS