summaryrefslogtreecommitdiff
path: root/libpurple/purpleauthorizationrequest.c
blob: a7171c155188fffd68eeb65da88c62b29b1b437f (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
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
/*
 * Purple - Internet Messaging Library
 * Copyright (C) Pidgin Developers <devel@pidgin.im>
 *
 * 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 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 Lesser General Public
 * License along with this library; if not, see <https://www.gnu.org/licenses/>.
 */

#include "purpleauthorizationrequest.h"

struct _PurpleAuthorizationRequest {
	GObject parent;

	PurpleAccount *account;

	gchar *username;
	gchar *alias;
	gchar *message;
	gboolean add;

	/* This tracks whether _accept or _deny have been called. */
	gboolean handled;
};

enum {
	SIG_ACCEPTED,
	SIG_DENIED,
	N_SIGNALS
};
static guint signals[N_SIGNALS] = {0, };

enum {
	PROP_0,
	PROP_ACCOUNT,
	PROP_USERNAME,
	PROP_ALIAS,
	PROP_MESSAGE,
	PROP_ADD,
	N_PROPERTIES,
};
static GParamSpec *properties[N_PROPERTIES] = {NULL, };

G_DEFINE_TYPE(PurpleAuthorizationRequest, purple_authorization_request,
              G_TYPE_OBJECT)

/******************************************************************************
 * Helpers
 *****************************************************************************/
static void
purple_authorization_request_set_account(PurpleAuthorizationRequest *request,
                                         PurpleAccount *account)
{
	g_return_if_fail(PURPLE_IS_AUTHORIZATION_REQUEST(request));
	g_return_if_fail(PURPLE_IS_ACCOUNT(account));

	if(g_set_object(&request->account, account)) {
		g_object_notify_by_pspec(G_OBJECT(request), properties[PROP_ACCOUNT]);
	}
}

static void
purple_authorization_request_set_username(PurpleAuthorizationRequest *request,
                                          const gchar *username)
{
	g_return_if_fail(PURPLE_IS_AUTHORIZATION_REQUEST(request));

	g_free(request->username);
	request->username = g_strdup(username);

	g_object_notify_by_pspec(G_OBJECT(request), properties[PROP_USERNAME]);
}

/******************************************************************************
 * GObject Implementation
 *****************************************************************************/
static void
purple_authorization_request_get_property(GObject *obj, guint param_id,
                                          GValue *value, GParamSpec *pspec)
{
	PurpleAuthorizationRequest *request = PURPLE_AUTHORIZATION_REQUEST(obj);

	switch(param_id) {
		case PROP_ACCOUNT:
			g_value_set_object(value,
			                   purple_authorization_request_get_account(request));
			break;
		case PROP_USERNAME:
			g_value_set_string(value,
			                   purple_authorization_request_get_username(request));
			break;
		case PROP_ALIAS:
			g_value_set_string(value,
			                   purple_authorization_request_get_alias(request));
			break;
		case PROP_MESSAGE:
			g_value_set_string(value,
			                   purple_authorization_request_get_message(request));
			break;
		case PROP_ADD:
			g_value_set_boolean(value,
			                    purple_authorization_request_get_add(request));
			break;
		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
			break;
	}
}

static void
purple_authorization_request_set_property(GObject *obj, guint param_id,
                                          const GValue *value,
                                          GParamSpec *pspec)
{
	PurpleAuthorizationRequest *request = PURPLE_AUTHORIZATION_REQUEST(obj);

	switch(param_id) {
		case PROP_ACCOUNT:
			purple_authorization_request_set_account(request,
			                                         g_value_get_object(value));
			break;
		case PROP_USERNAME:
			purple_authorization_request_set_username(request,
			                                          g_value_get_string(value));
			break;
		case PROP_ALIAS:
			purple_authorization_request_set_alias(request,
			                                       g_value_get_string(value));
			break;
		case PROP_MESSAGE:
			purple_authorization_request_set_message(request,
			                                         g_value_get_string(value));
			break;
		case PROP_ADD:
			purple_authorization_request_set_add(request,
			                                     g_value_get_boolean(value));
			break;
		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
			break;
	}
}

static void
purple_authorization_request_dispose(GObject *obj) {
	PurpleAuthorizationRequest *request = PURPLE_AUTHORIZATION_REQUEST(obj);

	g_clear_object(&request->account);

	G_OBJECT_CLASS(purple_authorization_request_parent_class)->dispose(obj);
}

static void
purple_authorization_request_finalize(GObject *obj) {
	PurpleAuthorizationRequest *request = PURPLE_AUTHORIZATION_REQUEST(obj);

	g_clear_pointer(&request->username, g_free);
	g_clear_pointer(&request->alias, g_free);
	g_clear_pointer(&request->message, g_free);

	G_OBJECT_CLASS(purple_authorization_request_parent_class)->finalize(obj);
}

static void
purple_authorization_request_init(G_GNUC_UNUSED PurpleAuthorizationRequest *request)
{
}

static void
purple_authorization_request_class_init(PurpleAuthorizationRequestClass *klass)
{
	GObjectClass *obj_class = G_OBJECT_CLASS(klass);

	obj_class->get_property = purple_authorization_request_get_property;
	obj_class->set_property = purple_authorization_request_set_property;
	obj_class->dispose = purple_authorization_request_dispose;
	obj_class->finalize = purple_authorization_request_finalize;

	/**
	 * PurpleAuthorizationRequest::accepted:
	 * @request: The [class@AuthorizationRequest] instance.
	 *
	 * Emitted when the user has accepted @request. This is typically emitted
	 * by the user interface calling [method@AuthorizationRequest.accept].
	 *
	 * Since: 3.0.0
	 */
	signals[SIG_ACCEPTED] = g_signal_new_class_handler(
		"accepted",
		G_OBJECT_CLASS_TYPE(klass),
		G_SIGNAL_RUN_LAST,
		NULL,
		NULL,
		NULL,
		NULL,
		G_TYPE_NONE,
		0);

	/**
	 * PurpleAuthorizationRequest::denied:
	 * @request: The [class@AuthorizationRequest] instance.
	 * @message: (nullable): An optional denial message.
	 *
	 * Emitted when the user has denied @request. This is typically emitted
	 * by the user interface calling [method@AuthorizationRequest.deny].
	 *
	 * Since: 3.0.0
	 */
	signals[SIG_DENIED] = g_signal_new_class_handler(
		"denied",
		G_OBJECT_CLASS_TYPE(klass),
		G_SIGNAL_RUN_LAST,
		NULL,
		NULL,
		NULL,
		NULL,
		G_TYPE_NONE,
		1,
		G_TYPE_STRING);

	/**
	 * PurpleAuthorizationRequest:account:
	 *
	 * The account that this authorization request is for.
	 *
	 * Since: 3.0.0
	 */
	properties[PROP_ACCOUNT] = g_param_spec_object(
		"account", "account",
		"The account for this authorization request",
		PURPLE_TYPE_ACCOUNT,
		G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);

	/**
	 * PurpleAuthorizationRequest:username:
	 *
	 * The username of the remote user that is requesting authorization.
	 *
	 * Since: 3.0.0
	 */
	properties[PROP_USERNAME] = g_param_spec_string(
		"username", "username",
		"The username of the remote user that is requesting authorization",
		NULL,
		G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);

	/**
	 * PurpleAuthorizationRequest:alias:
	 *
	 * The alias of the remote user that is requesting authorization.
	 *
	 * Since: 3.0.0
	 */
	properties[PROP_ALIAS] = g_param_spec_string(
		"alias", "alias",
		"The alias of the remote user that is requesting authorization",
		NULL,
		G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

	/**
	 * PurpleAuthorizationRequest:message:
	 *
	 * The optional message sent from the remote user.
	 *
	 * Since: 3.0.0
	 */
	properties[PROP_MESSAGE] = g_param_spec_string(
		"message", "message",
		"The optional message sent by the remote user",
		NULL,
		G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

	/**
	 * PurpleAuthorizationRequest:add:
	 *
	 * Whether or not the user interface should ask the end user to add the
	 * remote user after accepting the end user's friend request.
	 *
	 * Since: 3.0.0
	 */
	properties[PROP_ADD] = g_param_spec_boolean(
		"add", "add",
		"Whether or not to add the remote user back",
		FALSE,
		G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

	g_object_class_install_properties(obj_class, N_PROPERTIES, properties);
}

/******************************************************************************
 * Public API
 *****************************************************************************/
PurpleAuthorizationRequest *
purple_authorization_request_new(PurpleAccount *account, const gchar *username)
{
	g_return_val_if_fail(PURPLE_IS_ACCOUNT(account), NULL);
	g_return_val_if_fail(username != NULL, NULL);

	return g_object_new(
		PURPLE_TYPE_AUTHORIZATION_REQUEST,
		"account", account,
		"username", username,
		NULL);
}

PurpleAccount *
purple_authorization_request_get_account(PurpleAuthorizationRequest *request) {
	g_return_val_if_fail(PURPLE_IS_AUTHORIZATION_REQUEST(request), NULL);

	return request->account;
}

const gchar *
purple_authorization_request_get_username(PurpleAuthorizationRequest *request)
{
	g_return_val_if_fail(PURPLE_IS_AUTHORIZATION_REQUEST(request), NULL);

	return request->username;
}

void
purple_authorization_request_set_alias(PurpleAuthorizationRequest *request,
                                       const gchar *alias)
{
	g_return_if_fail(PURPLE_IS_AUTHORIZATION_REQUEST(request));

	g_free(request->alias);
	request->alias = g_strdup(alias);

	g_object_notify_by_pspec(G_OBJECT(request), properties[PROP_ALIAS]);
}

const gchar *
purple_authorization_request_get_alias(PurpleAuthorizationRequest *request) {
	g_return_val_if_fail(PURPLE_IS_AUTHORIZATION_REQUEST(request), NULL);

	return request->alias;
}

void
purple_authorization_request_set_message(PurpleAuthorizationRequest *request,
                                         const gchar *message)
{
	g_return_if_fail(PURPLE_IS_AUTHORIZATION_REQUEST(request));

	g_free(request->message);
	request->message = g_strdup(message);

	g_object_notify_by_pspec(G_OBJECT(request), properties[PROP_MESSAGE]);
}

const gchar *
purple_authorization_request_get_message(PurpleAuthorizationRequest *request) {
	g_return_val_if_fail(PURPLE_IS_AUTHORIZATION_REQUEST(request), NULL);

	return request->message;
}

void
purple_authorization_request_set_add(PurpleAuthorizationRequest *request,
                                     gboolean add)
{
	g_return_if_fail(PURPLE_IS_AUTHORIZATION_REQUEST(request));

	request->add = add;

	g_object_notify_by_pspec(G_OBJECT(request), properties[PROP_ADD]);
}

gboolean
purple_authorization_request_get_add(PurpleAuthorizationRequest *request) {
	g_return_val_if_fail(PURPLE_IS_AUTHORIZATION_REQUEST(request), FALSE);

	return request->add;
}

void
purple_authorization_request_accept(PurpleAuthorizationRequest *request) {
	g_return_if_fail(PURPLE_IS_AUTHORIZATION_REQUEST(request));

	/* Calling this multiple times is a programming error. */
	g_return_if_fail(request->handled == FALSE);

	request->handled = TRUE;

	g_signal_emit(request, signals[SIG_ACCEPTED], 0);
}

void
purple_authorization_request_deny(PurpleAuthorizationRequest *request,
                                  const gchar *message)
{
	g_return_if_fail(PURPLE_IS_AUTHORIZATION_REQUEST(request));

	/* Calling this multiple times is a programming error. */
	g_return_if_fail(request->handled == FALSE);

	request->handled = TRUE;

	g_signal_emit(request, signals[SIG_DENIED], 0, message);
}