-
Notifications
You must be signed in to change notification settings - Fork 0
/
account-wizard-context.c
341 lines (287 loc) · 8.82 KB
/
account-wizard-context.c
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
/*
* account-wizard-context.c
*
* Copyright (C) 2022 Ivaylo Dimitrov <ivo.g.dimitrov.75@gmail.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.
*
* 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 <https://www.gnu.org/licenses/>.
*
*/
/**
* SECTION:account-wizard-context
* @short_description: interface to provide wizard-like account editing.
*
* The #AccountWizardContext interface specifies a small set of methods and
* properties that an #AccountEditContext subclass can implement in order to
* provide a wizard-like editing for #AccountItem objects.
*
* The most important functions in this interface are
* account_wizard_context_next() and account_wizard_context_back(): they serve
* to respectively retrieve the next and the previous page of the wizard, in
* form of a #GtkWidget. The user interface application is supposed to provide
* the Cancel, Back, Next and Finish buttons (which therefore must not be
* present in the page returned by the #AccountWizardContext) and listen to the
* #AccountWizardContext:can-next, #AccountWizardContext:can-back and
* #AccountWizardContext:can-finish properties to determine if the buttons
* should be enabled or disabled.
*
* In any case, regardless of the buttons' state, the above mentioned methods
* should still be called by the UI when the user presses the buttons and it's
* perfectly valid for these methods to return a %NULL page and set the #GError
* accordingly (see #AccountErrorCode for a list of possible error codes).
*
* The error code %ACCOUNT_ERROR_PAGE_ASYNC has a special meaning: it is used
* when retrieving the next/previous page is an asyncronous operation (and
* possibly lengthy). In such cases, the page (or an error code) will be
* delivered through the #AccountWizardContext::page-async signal.
*
* To terminate the editing the UI should call either
* account_wizard_context_cancel() to abort the account creation/editing or
* account_wizard_context_finish() to complete the editing process.
*/
#include "config.h"
#include "account-wizard-context.h"
#include "account-marshal.h"
typedef AccountWizardContextIface AccountWizardContextInterface;
G_DEFINE_INTERFACE(
AccountWizardContext,
account_wizard_context,
G_TYPE_OBJECT
)
enum
{
PAGE_ASYNC,
LAST_SIGNAL
};
static guint signals[LAST_SIGNAL] = { 0 };
#define NO_IMPL(fun) \
g_warning("AccountWizardContext::" # fun "not implemented for `%s'", \
g_type_name(G_TYPE_FROM_INSTANCE(context)));
static void
account_wizard_context_default_init(AccountWizardContextIface *iface)
{
static gboolean initialized = FALSE;
if (initialized)
return;
g_object_interface_install_property(
iface,
g_param_spec_boolean(
"can-next",
"Can next",
"Can go next page",
FALSE,
G_PARAM_READWRITE));
g_object_interface_install_property(
iface,
g_param_spec_boolean(
"can-back",
"Can back",
"Can go back page",
FALSE,
G_PARAM_READWRITE));
g_object_interface_install_property(
iface,
g_param_spec_boolean(
"can-finish",
"Can finish",
"Can finish editing",
FALSE,
G_PARAM_READWRITE));
signals[PAGE_ASYNC] =
g_signal_new(
"page-async", G_TYPE_FROM_CLASS(iface), G_SIGNAL_RUN_FIRST, 0, NULL,
NULL, account_marshal_VOID__OBJECT_POINTER,
G_TYPE_NONE, 2, GTK_TYPE_WIDGET, G_TYPE_POINTER);
initialized = TRUE;
}
/**
* account_wizard_context_get_can_next:
* @context:the #AccountWizardContext.
*
* Whether the "next" button should be enabled.
*
* Returns: %TRUE if "next" should be enabled.
*/
gboolean
account_wizard_context_get_can_next(AccountWizardContext *context)
{
gboolean can_next;
g_object_get(context, "can-next", &can_next, NULL);
return can_next;
}
/**
* account_wizard_context_get_can_back:
* @context:the #AccountWizardContext.
*
* Whether the "back" button should be enabled.
*
* Returns: %TRUE if "back" should be enabled.
*/
gboolean
account_wizard_context_get_can_back(AccountWizardContext *context)
{
gboolean can_back;
g_object_get(context, "can-back", &can_back, NULL);
return can_back;
}
/**
* account_wizard_context_get_can_finish:
* @context:the #AccountWizardContext.
*
* Whether the "finish" button should be enabled.
*
* Returns: %TRUE if "finish" should be enabled.
*/
gboolean
account_wizard_context_get_can_finish(AccountWizardContext *context)
{
gboolean can_finish;
g_object_get(context, "can-finish", &can_finish, NULL);
return can_finish;
}
/**
* account_wizard_context_set_can_next:
* @context:the #AccountWizardContext.
* @can_next: %TRUE if "next" should be enabled.
*
* Whether the "next" button should be enabled.
*/
void
account_wizard_context_set_can_next(AccountWizardContext *context,
gboolean can_next)
{
g_object_set(context, "can-next", can_next, NULL);
}
/**
* account_wizard_context_set_can_back:
* @context:the #AccountWizardContext.
* @can_back: %TRUE if "back" should be enabled.
*
* Whether the "back" button should be enabled.
*/
void
account_wizard_context_set_can_back(AccountWizardContext *context,
gboolean can_back)
{
g_object_set(context, "can-back", can_back, NULL);
}
/**
* account_wizard_context_set_can_finish:
* @context:the #AccountWizardContext.
* @can_finish: %TRUE if "finish" should be enabled.
*
* Whether the "finish" button should be enabled.
*/
void
account_wizard_context_set_can_finish(AccountWizardContext *context,
gboolean can_finish)
{
g_object_set(context, "can-finish", can_finish, NULL);
}
/**
* account_wizard_context_next:
* @context: the #AccountWizardContext.
* @error: a pointer to a #GError.
*
* Proceeds to the next page of the wizard.
*
* Returns:(transfer none): the #GtkWidget to be displayed on the next page.
*/
GtkWidget *
account_wizard_context_next(AccountWizardContext *context, GError **error)
{
AccountWizardContextIface *iface;
g_return_val_if_fail(ACCOUNT_IS_WIZARD_CONTEXT(context), NULL);
iface = ACCOUNT_WIZARD_CONTEXT_GET_IFACE(context);
if (iface->next)
return iface->next(context, error);
NO_IMPL(next);
return NULL;
}
/**
* account_wizard_context_back:
* @context: the #AccountWizardContext.
* @error: a pointer to a #GError.
*
* Goes back to the previous page of the wizard.
*
* Returns:(transfer none): the #GtkWidget to be displayed on the previous page.
*/
GtkWidget *
account_wizard_context_back(AccountWizardContext *context, GError **error)
{
AccountWizardContextIface *iface;
g_return_val_if_fail(ACCOUNT_IS_WIZARD_CONTEXT(context), NULL);
iface = ACCOUNT_WIZARD_CONTEXT_GET_IFACE(context);
if (iface->back)
return iface->back(context, error);
NO_IMPL(back);
return NULL;
}
/**
* account_wizard_context_get_page_title:
* @context: the #AccountWizardContext.
*
* Gets the title of the current page.
*
* Returns:(transfer none): a string representing the title.
*/
const gchar *
account_wizard_context_get_page_title(AccountWizardContext *context)
{
AccountWizardContextIface *iface;
g_return_val_if_fail(ACCOUNT_IS_WIZARD_CONTEXT(context), NULL);
iface = ACCOUNT_WIZARD_CONTEXT_GET_IFACE(context);
if (iface->get_page_title)
return iface->get_page_title(context);
NO_IMPL(back);
return NULL;
}
/**
* account_wizard_context_finish:
* @context: the #AccountWizardContext.
* @error: a pointer to a #GError.
*
* Completes the account creation. If this call succeeds, the context can be
* destroyed and the newly created #AccountItem will be added into the
* #AccountsList.
*
* Returns: %TRUE if account was created successfully, %FALSE otherwise.
*/
gboolean
account_wizard_context_finish(AccountWizardContext *context, GError **error)
{
AccountWizardContextIface *iface;
g_return_val_if_fail(ACCOUNT_IS_WIZARD_CONTEXT(context), FALSE);
iface = ACCOUNT_WIZARD_CONTEXT_GET_IFACE(context);
if (iface->finish)
return iface->finish(context, error);
NO_IMPL(finish);
return FALSE;
}
/**
* account_wizard_context_cancel:
* @context: the #AccountWizardContext.
*
* Cancel the account creation. The context can then be destroyed.
*/
void
account_wizard_context_cancel(AccountWizardContext *context)
{
AccountWizardContextIface *iface;
g_return_if_fail(ACCOUNT_IS_WIZARD_CONTEXT(context));
iface = ACCOUNT_WIZARD_CONTEXT_GET_IFACE(context);
if (iface->cancel)
iface->cancel(context);
else
NO_IMPL(finish);
}