summaryrefslogtreecommitdiff
path: root/pidgin/gtkdialogs.c
blob: b1e47dc2a2fc816992e7e33d5f56e991de4c9074 (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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/* pidgin
 *
 * Pidgin is the legal property of its developers, whose names are too numerous
 * to list here.  Please refer to the COPYRIGHT file distributed with this
 * source distribution.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
 */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <glib/gi18n-lib.h>

#include <gst/gst.h>

#include <talkatu.h>

#include "package_revision.h"
#ifdef HAVE_MESON_CONFIG
#include "meson-config.h"
#endif

#include <purple.h>

#include "gtkblist.h"
#include "gtkdialogs.h"
#include "gtkutils.h"
#include "pidgincore.h"

struct _PidginGroupMergeObject {
	PurpleGroup* parent;
	char *new_name;
};

static void
pidgin_dialogs_im_cb(G_GNUC_UNUSED gpointer data, PurpleRequestPage *page) {
	PurpleAccount *account;
	const char *username;

	account = purple_request_page_get_account(page, "account");
	username = purple_request_page_get_string(page,  "screenname");

	pidgin_dialogs_im_with_user(account, username);
}

static gboolean
pidgin_dialogs_im_name_validator(PurpleRequestField *field, char **errmsg,
                                 gpointer data)
{
	PurpleRequestFieldAccount *account_field = data;
	PurpleAccount *account;
	PurpleProtocol *protocol;
	const char *username;
	gboolean valid = FALSE;

	account = purple_request_field_account_get_value(account_field);
	protocol = purple_account_get_protocol(account);
	username = purple_request_field_string_get_value(PURPLE_REQUEST_FIELD_STRING(field));

	if (username) {
		valid = purple_validate(protocol, username);
	}

	if (errmsg && !valid)
		*errmsg = g_strdup(_("Invalid username"));

	return valid;
}

void
pidgin_dialogs_im(void)
{
	PurpleRequestPage *page;
	PurpleRequestGroup *group;
	PurpleRequestField *username_field = NULL;
	PurpleRequestField *account_field = NULL;

	page = purple_request_page_new();

	group = purple_request_group_new(NULL);
	purple_request_page_add_group(page, group);

	username_field = purple_request_field_string_new("screenname", _("_Name"),
	                                                 NULL, FALSE);
	purple_request_field_set_type_hint(username_field, "screenname");
	purple_request_field_set_required(username_field, TRUE);
	purple_request_group_add_field(group, username_field);

	account_field = purple_request_field_account_new("account", _("_Account"),
	                                                 NULL);
	purple_request_field_set_type_hint(account_field, "account");
	purple_request_field_set_visible(account_field,
		(purple_connections_get_all() != NULL &&
		 purple_connections_get_all()->next != NULL));
	purple_request_field_set_required(account_field, TRUE);
	purple_request_group_add_field(group, account_field);

	purple_request_field_set_validator(username_field,
	                                   pidgin_dialogs_im_name_validator,
	                                   account_field, NULL);

	purple_request_fields(
	        purple_blist_get_default(), _("New Instant Message"), NULL,
	        _("Please enter the username or alias of the person "
	          "you would like to IM."),
	        page, _("OK"), G_CALLBACK(pidgin_dialogs_im_cb), _("Cancel"),
	        NULL, NULL, NULL);
}

void
pidgin_dialogs_im_with_user(PurpleAccount *account, const char *username)
{
	PurpleConversation *im;
	PurpleConversationManager *manager;

	g_return_if_fail(account != NULL);
	g_return_if_fail(username != NULL);

	manager = purple_conversation_manager_get_default();
	im = purple_conversation_manager_find_im(manager, account, username);

	if(!PURPLE_IS_IM_CONVERSATION(im)) {
		im = purple_im_conversation_new(account, username);
	}

	pidgin_conv_attach_to_conversation(im);
	purple_conversation_present(im);
}

static void
pidgin_dialogs_info_cb(G_GNUC_UNUSED gpointer data, PurpleRequestPage *page) {
	char *username;
	PurpleAccount *account;
	const gchar *screenname = NULL;

	account = purple_request_page_get_account(page, "account");

	screenname = purple_request_page_get_string(page, "screenname");
	username = g_strdup(purple_normalize(account, screenname));

	if(username != NULL && *username != '\0' && account != NULL) {
		pidgin_retrieve_user_info(purple_account_get_connection(account),
		                          username);
	}

	g_free(username);
}

void
pidgin_dialogs_info(void)
{
	PurpleRequestPage *page;
	PurpleRequestGroup *group;
	PurpleRequestField *field;

	page = purple_request_page_new();

	group = purple_request_group_new(NULL);
	purple_request_page_add_group(page, group);

	field = purple_request_field_string_new("screenname", _("_Name"), NULL, FALSE);
	purple_request_field_set_type_hint(field, "screenname");
	purple_request_field_set_required(field, TRUE);
	purple_request_group_add_field(group, field);

	field = purple_request_field_account_new("account", _("_Account"), NULL);
	purple_request_field_set_type_hint(field, "account");
	purple_request_field_set_visible(field,
		(purple_connections_get_all() != NULL &&
		 purple_connections_get_all()->next != NULL));
	purple_request_field_set_required(field, TRUE);
	purple_request_group_add_field(group, field);

	purple_request_fields(
	        purple_blist_get_default(), _("Get User Info"), NULL,
	        _("Please enter the username or alias of the person "
	          "whose info you would like to view."),
	        page, _("OK"), G_CALLBACK(pidgin_dialogs_info_cb),
	        _("Cancel"), NULL, NULL, NULL);
}

static void
pidgin_dialogs_alias_buddy_cb(PurpleBuddy *buddy, const char *new_alias)
{
	purple_buddy_set_local_alias(buddy, new_alias);
	purple_serv_alias_buddy(buddy);
}

void
pidgin_dialogs_alias_buddy(PurpleBuddy *buddy)
{
	gchar *secondary;

	g_return_if_fail(buddy != NULL);

	secondary = g_strdup_printf(_("Enter an alias for %s."), purple_buddy_get_name(buddy));

	purple_request_input(NULL, _("Alias Buddy"), NULL,
					   secondary, purple_buddy_get_local_alias(buddy), FALSE, FALSE, NULL,
					   _("Alias"), G_CALLBACK(pidgin_dialogs_alias_buddy_cb),
					   _("Cancel"), NULL,
					   purple_request_cpar_from_account(purple_buddy_get_account(buddy)),
					   buddy);

	g_free(secondary);
}

static void
pidgin_dialogs_alias_chat_cb(PurpleChat *chat, const char *new_alias)
{
	purple_chat_set_alias(chat, new_alias);
}

void
pidgin_dialogs_alias_chat(PurpleChat *chat)
{
	gchar *alias;

	g_return_if_fail(chat != NULL);

	g_object_get(chat, "alias", &alias, NULL);

	purple_request_input(NULL, _("Alias Chat"), NULL,
					   _("Enter an alias for this chat."),
					   alias, FALSE, FALSE, NULL,
					   _("Alias"), G_CALLBACK(pidgin_dialogs_alias_chat_cb),
					   _("Cancel"), NULL,
					   purple_request_cpar_from_account(purple_chat_get_account(chat)),
					   chat);

	g_free(alias);
}

static void
pidgin_dialogs_remove_contact_cb(PurpleMetaContact *contact)
{
	PurpleBlistNode *bnode, *cnode;
	PurpleGroup *group;

	cnode = (PurpleBlistNode *)contact;
	group = (PurpleGroup*)cnode->parent;
	for (bnode = cnode->child; bnode; bnode = bnode->next) {
		PurpleBuddy *buddy = (PurpleBuddy*)bnode;
		if (purple_account_is_connected(purple_buddy_get_account(buddy)))
			purple_account_remove_buddy(purple_buddy_get_account(buddy), buddy, group);
	}
	purple_blist_remove_contact(contact);
}

void
pidgin_dialogs_remove_contact(PurpleMetaContact *contact)
{
	PurpleBuddy *buddy = purple_meta_contact_get_priority_buddy(contact);

	g_return_if_fail(contact != NULL);
	g_return_if_fail(buddy != NULL);

	if (PURPLE_BLIST_NODE(contact)->child == PURPLE_BLIST_NODE(buddy) &&
	    PURPLE_BLIST_NODE(buddy)->next == NULL) {
		pidgin_dialogs_remove_buddy(buddy);
	} else {
		gchar *text;
		int contact_size = purple_counting_node_get_total_size(PURPLE_COUNTING_NODE(contact));
		text = g_strdup_printf(
					ngettext(
						"You are about to remove the contact containing %s "
						"and %d other buddy from your buddy list.  Do you "
						"want to continue?",
						"You are about to remove the contact containing %s "
						"and %d other buddies from your buddy list.  Do you "
						"want to continue?", contact_size - 1),
					purple_buddy_get_name(buddy), contact_size - 1);

		purple_request_action(contact, NULL, _("Remove Contact"), text, 0,
				NULL,
				contact, 2,
				_("_Remove Contact"), G_CALLBACK(pidgin_dialogs_remove_contact_cb),
				_("Cancel"),
				NULL);

		g_free(text);
	}
}

static void free_ggmo(struct _PidginGroupMergeObject *ggp)
{
	g_free(ggp->new_name);
	g_free(ggp);
}

static void
pidgin_dialogs_merge_groups_cb(struct _PidginGroupMergeObject *GGP)
{
	purple_group_set_name(GGP->parent, GGP->new_name);
	free_ggmo(GGP);
}

void
pidgin_dialogs_merge_groups(PurpleGroup *source, const char *new_name)
{
	gchar *text;
	struct _PidginGroupMergeObject *ggp;

	g_return_if_fail(source != NULL);
	g_return_if_fail(new_name != NULL);

	text = g_strdup_printf(
				_("You are about to merge the group called %s into the group "
				"called %s. Do you want to continue?"), purple_group_get_name(source), new_name);

	ggp = g_new(struct _PidginGroupMergeObject, 1);
	ggp->parent = source;
	ggp->new_name = g_strdup(new_name);

	purple_request_action(source, NULL, _("Merge Groups"), text, 0,
			NULL,
			ggp, 2,
			_("_Merge Groups"), G_CALLBACK(pidgin_dialogs_merge_groups_cb),
			_("Cancel"), G_CALLBACK(free_ggmo));

	g_free(text);
}

static void
pidgin_dialogs_remove_group_cb(PurpleGroup *group)
{
	PurpleBlistNode *cnode, *bnode;

	cnode = ((PurpleBlistNode*)group)->child;

	while (cnode) {
		if (PURPLE_IS_META_CONTACT(cnode)) {
			bnode = cnode->child;
			cnode = cnode->next;
			while (bnode) {
				PurpleBuddy *buddy;
				if (PURPLE_IS_BUDDY(bnode)) {
					buddy = (PurpleBuddy*)bnode;
					bnode = bnode->next;
					if (purple_account_is_connected(purple_buddy_get_account(buddy))) {
						purple_account_remove_buddy(purple_buddy_get_account(buddy), buddy, group);
						purple_blist_remove_buddy(buddy);
					}
				} else {
					bnode = bnode->next;
				}
			}
		} else if (PURPLE_IS_CHAT(cnode)) {
			PurpleChat *chat = (PurpleChat *)cnode;
			cnode = cnode->next;
			if (purple_account_is_connected(purple_chat_get_account(chat)))
				purple_blist_remove_chat(chat);
		} else {
			cnode = cnode->next;
		}
	}

	purple_blist_remove_group(group);
}

void
pidgin_dialogs_remove_group(PurpleGroup *group)
{
	gchar *text;

	g_return_if_fail(group != NULL);

	text = g_strdup_printf(_("You are about to remove the group %s and all its members from your buddy list.  Do you want to continue?"),
						   purple_group_get_name(group));

	purple_request_action(group, NULL, _("Remove Group"), text, 0,
						NULL,
						group, 2,
						_("_Remove Group"), G_CALLBACK(pidgin_dialogs_remove_group_cb),
						_("Cancel"), NULL);

	g_free(text);
}

/* XXX - Some of this should be moved into the core, methinks. */
static void
pidgin_dialogs_remove_buddy_cb(PurpleBuddy *buddy)
{
	PurpleGroup *group;
	gchar *name;
	PurpleAccount *account;

	group = purple_buddy_get_group(buddy);
	name = g_strdup(purple_buddy_get_name(buddy)); /* purple_buddy_get_name() is a crasher after remove_buddy */
	account = purple_buddy_get_account(buddy);

	purple_debug_info("blist", "Removing '%s' from buddy list.\n", purple_buddy_get_name(buddy));
	/* TODO - Should remove from blist first... then call purple_account_remove_buddy()? */
	purple_account_remove_buddy(account, buddy, group);
	purple_blist_remove_buddy(buddy);

	g_free(name);
}

void
pidgin_dialogs_remove_buddy(PurpleBuddy *buddy)
{
	gchar *text;

	g_return_if_fail(buddy != NULL);

	text = g_strdup_printf(_("You are about to remove %s from your buddy list.  Do you want to continue?"),
						   purple_buddy_get_name(buddy));

	purple_request_action(buddy, NULL, _("Remove Buddy"), text, 0,
		purple_request_cpar_from_account(
			purple_buddy_get_account(buddy)),
		buddy, 2, _("_Remove Buddy"),
		G_CALLBACK(pidgin_dialogs_remove_buddy_cb), _("Cancel"), NULL);

	g_free(text);
}

static void
pidgin_dialogs_remove_chat_cb(PurpleChat *chat)
{
	purple_blist_remove_chat(chat);
}

void
pidgin_dialogs_remove_chat(PurpleChat *chat)
{
	const gchar *name;
	gchar *text;

	g_return_if_fail(chat != NULL);

	name = purple_chat_get_name(chat);
	text = g_strdup_printf(_("You are about to remove the chat %s from your buddy list.  Do you want to continue?"),
			name ? name : "");

	purple_request_action(chat, NULL, _("Remove Chat"), text, 0,
		purple_request_cpar_from_account(purple_chat_get_account(chat)),
		chat, 2, _("_Remove Chat"),
		G_CALLBACK(pidgin_dialogs_remove_chat_cb), _("Cancel"), NULL);

	g_free(text);
}