summaryrefslogtreecommitdiff
path: root/pidgin/pidginaccountstore.c
blob: 0e2c2546c18c5c6c7489b878018dea175d82a789 (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
/*
 * Pidgin - Internet Messenger
 * Copyright (C) Pidgin Developers <devel@pidgin.im>
 *
 * 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, see <https://www.gnu.org/licenses/>.
 */

#include <glib/gi18n-lib.h>

#include "pidgin/pidginaccountstore.h"

#include "gtkutils.h"

struct _PidginAccountStore {
	GtkListStore parent;
};

/******************************************************************************
 * Helpers
 *****************************************************************************/
static void
pidgin_account_store_add_account(PidginAccountStore *store,
                                 PurpleAccount *account)
{
	PurpleProtocol *protocol = NULL;
	GtkTreeIter iter;
	GdkPixbuf *pixbuf = NULL;
	gchar *markup = NULL;
	const gchar *alias = NULL, *icon_name = NULL;

	protocol = purple_account_get_protocol(account);
	icon_name = purple_protocol_get_icon_name(protocol);

	alias = purple_account_get_private_alias(account);
	if(alias != NULL) {
		markup = g_strdup_printf(_("%s (%s) (%s)"),
		                         purple_account_get_username(account),
		                         alias,
		                         purple_account_get_protocol_name(account));
	} else {
		markup = g_strdup_printf(_("%s (%s)"),
		                         purple_account_get_username(account),
		                         purple_account_get_protocol_name(account));
	}

	gtk_list_store_append(GTK_LIST_STORE(store), &iter);
	gtk_list_store_set(
		GTK_LIST_STORE(store),
		&iter,
		PIDGIN_ACCOUNT_STORE_COLUMN_ACCOUNT, account,
		PIDGIN_ACCOUNT_STORE_COLUMN_MARKUP, markup,
		PIDGIN_ACCOUNT_STORE_COLUMN_ICON_NAME, icon_name,
		-1
	);

	g_free(markup);
}

static void
pidgin_account_store_add_account_helper(PurpleAccount *account, gpointer data) {
	if(PURPLE_IS_ACCOUNT(account)) {
		pidgin_account_store_add_account(PIDGIN_ACCOUNT_STORE(data),
		                                 PURPLE_ACCOUNT(account));
	}
}

static void
pidgin_account_store_add_accounts(PidginAccountStore *store) {
	PurpleAccountManager *manager = NULL;

	manager = purple_account_manager_get_default();
	purple_account_manager_foreach(manager,
	                               pidgin_account_store_add_account_helper,
	                               store);
}

static void
pidgin_account_store_remove_account(PidginAccountStore *store,
                                    PurpleAccount *account)
{
	GtkTreeIter iter;

	if(gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter) == FALSE) {
		purple_debug_warning("account-store",
		                     "account %s was removed but not in the store",
		                     purple_account_get_username(account));
		return;
	}

	/* walk through the store and look for the account to remove */
	do {
		PurpleAccount *found = NULL;

		gtk_tree_model_get(
			GTK_TREE_MODEL(store),
			&iter,
			PIDGIN_ACCOUNT_STORE_COLUMN_ACCOUNT, &found,
			-1
		);

		if(found == account) {
			g_object_unref(G_OBJECT(found));

			gtk_list_store_remove(GTK_LIST_STORE(store), &iter);

			return;
		}
	} while(gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter));
}

/******************************************************************************
 * Callbacks
 *****************************************************************************/
static void
pidgin_account_store_account_added_cb(PurpleAccount *account,
                                      gpointer data)
{
	pidgin_account_store_add_account(PIDGIN_ACCOUNT_STORE(data), account);
}

static void
pidgin_account_store_account_removed_cb(PurpleAccount *account,
                                        gpointer data)
{
	pidgin_account_store_remove_account(PIDGIN_ACCOUNT_STORE(data), account);
}

/******************************************************************************
 * GObject Implementation
 *****************************************************************************/
G_DEFINE_TYPE(PidginAccountStore, pidgin_account_store, GTK_TYPE_LIST_STORE)

static void
pidgin_account_store_init(PidginAccountStore *store) {
	gpointer accounts_handle = NULL;
	GType types[PIDGIN_ACCOUNT_STORE_N_COLUMNS] = {
		PURPLE_TYPE_ACCOUNT,
		G_TYPE_STRING,
		G_TYPE_STRING,
	};

	gtk_list_store_set_column_types(
		GTK_LIST_STORE(store),
		PIDGIN_ACCOUNT_STORE_N_COLUMNS,
		types
	);

	/* add the known accounts */
	pidgin_account_store_add_accounts(store);

	/* add the signal handlers to dynamically add/remove accounts */
	accounts_handle = purple_accounts_get_handle();
	purple_signal_connect(accounts_handle, "account-added", store,
	                      G_CALLBACK(pidgin_account_store_account_added_cb),
	                      store);
	purple_signal_connect(accounts_handle, "account-removed", store,
	                      G_CALLBACK(pidgin_account_store_account_removed_cb),
	                      store);
}

static void
pidgin_account_store_finalize(GObject *obj) {
	purple_signals_disconnect_by_handle(obj);

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

static void
pidgin_account_store_class_init(PidginAccountStoreClass *klass) {
	GObjectClass *obj_class = G_OBJECT_CLASS(klass);

	obj_class->finalize = pidgin_account_store_finalize;
}

/******************************************************************************
 * API
 *****************************************************************************/
GtkListStore *
pidgin_account_store_new(void) {
	return GTK_LIST_STORE(g_object_new(PIDGIN_TYPE_ACCOUNT_STORE, NULL));
}