summaryrefslogtreecommitdiff
path: root/finch/gntroomlist.c
blob: 54572caff8f4da0e1af30e4f997434183585e762 (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
/*
 * finch
 *
 * Finch 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
 */

#include <glib/gi18n-lib.h>

#include <purple.h>

#include <gnt.h>

#include "gntrequest.h"
#include "gntroomlist.h"

#define PREF_ROOT "/finch/roomlist"


/* Yes, just one roomlist at a time. Let's not get greedy. Aight? */
struct _FinchRoomlist
{
	GntWidget *window;

	GntWidget *accounts;
	GntWidget *tree;
	GntWidget *details;

	GntWidget *getlist;
	GntWidget *add;
	GntWidget *join;
	GntWidget *stop;
	GntWidget *close;

	PurpleAccount *account;
	PurpleRoomlist *roomlist;
} froomlist;

typedef struct _FinchRoomlist FinchRoomlist;

static void
unset_roomlist(G_GNUC_UNUSED gpointer data)
{
	froomlist.window = NULL;
	g_clear_object(&froomlist.roomlist);
	froomlist.account = NULL;
	froomlist.tree = NULL;
}

static void
fl_stop(G_GNUC_UNUSED GntWidget *button, G_GNUC_UNUSED gpointer data) {
	if (froomlist.roomlist &&
			purple_roomlist_get_in_progress(froomlist.roomlist))
		purple_roomlist_cancel_get_list(froomlist.roomlist);
}

static void
fl_get_list(G_GNUC_UNUSED GntWidget *button, G_GNUC_UNUSED gpointer data) {
	PurpleAccount *account = gnt_combo_box_get_selected_data(GNT_COMBO_BOX(froomlist.accounts));
	PurpleConnection *gc = purple_account_get_connection(account);

	if (!gc)
		return;

	g_clear_object(&froomlist.roomlist);
	froomlist.roomlist = purple_roomlist_get_list(gc);
	gnt_box_give_focus_to_child(GNT_BOX(froomlist.window), froomlist.tree);
}

static void
fl_add_chat(G_GNUC_UNUSED GntWidget *button, G_GNUC_UNUSED gpointer data) {
	char *name;
	PurpleRoomlistRoom *room = gnt_tree_get_selection_data(GNT_TREE(froomlist.tree));
	PurpleConnection *gc = purple_account_get_connection(froomlist.account);
	PurpleProtocol *protocol = NULL;

	if (gc == NULL || room == NULL)
		return;

	protocol = purple_connection_get_protocol(gc);

	if (PURPLE_PROTOCOL_IMPLEMENTS(protocol, ROOMLIST, room_serialize)) {
		name = purple_protocol_roomlist_room_serialize(PURPLE_PROTOCOL_ROOMLIST(protocol), room);
	} else {
		name = g_strdup(purple_roomlist_room_get_name(room));
	}

	purple_blist_request_add_chat(froomlist.account, NULL, NULL, name);

	g_free(name);
}

static void
fl_close(G_GNUC_UNUSED GntWidget *button, G_GNUC_UNUSED gpointer data)
{
	gnt_widget_destroy(froomlist.window);
}

static void
roomlist_activated(GntWidget *widget)
{
	PurpleRoomlistRoom *room = gnt_tree_get_selection_data(GNT_TREE(widget));
	if (!room)
		return;

	purple_roomlist_join_room(froomlist.roomlist, room);
}

static void
roomlist_selection_changed(G_GNUC_UNUSED GntWidget *widget,
                           G_GNUC_UNUSED gpointer old, gpointer current,
                           G_GNUC_UNUSED gpointer data)
{
	PurpleRoomlistRoom *room = current;
	GntTextView *tv = GNT_TEXT_VIEW(froomlist.details);

	gnt_text_view_clear(tv);

	if (!room)
		return;

	gnt_text_view_append_text_with_flags(tv,
	                                     purple_roomlist_room_get_name(room),
	                                     GNT_TEXT_FLAG_BOLD);
}

static void
roomlist_account_changed(G_GNUC_UNUSED GntWidget *widget,
                         G_GNUC_UNUSED gpointer old, gpointer current,
                         G_GNUC_UNUSED gpointer data)
{
	if (froomlist.account == current) {
		return;
	}

	froomlist.account = current;
	if (froomlist.roomlist) {
		if (purple_roomlist_get_in_progress(froomlist.roomlist))
			purple_roomlist_cancel_get_list(froomlist.roomlist);
		g_clear_object(&froomlist.roomlist);
	}

	gnt_tree_remove_all(GNT_TREE(froomlist.tree));
	gnt_widget_draw(froomlist.tree);
}

static void
reset_account_list(G_GNUC_UNUSED PurpleAccount *account)
{
	GList *list;
	GntComboBox *accounts = GNT_COMBO_BOX(froomlist.accounts);
	gnt_combo_box_remove_all(accounts);
	for (list = purple_connections_get_all(); list; list = list->next) {
		PurpleProtocol *protocol = NULL;
		PurpleConnection *gc = list->data;

		protocol = purple_connection_get_protocol(gc);
		if(PURPLE_CONNECTION_IS_CONNECTED(gc) &&
		   PURPLE_PROTOCOL_IMPLEMENTS(protocol, ROOMLIST, get_list))
		{
			PurpleAccount *account = purple_connection_get_account(gc);
			PurpleContactInfo *info = PURPLE_CONTACT_INFO(account);
			char *text = NULL;

			text = g_strdup_printf("%s (%s)",
			                       purple_contact_info_get_username(info),
			                       purple_account_get_protocol_name(account));
			gnt_combo_box_add_data(accounts, account, text);
			g_free(text);
		}
	}
}

static void
size_changed_cb(GntWidget *widget, G_GNUC_UNUSED int oldw,
                G_GNUC_UNUSED int oldh)
{
	int w, h;
	gnt_widget_get_size(widget, &w, &h);
	purple_prefs_set_int(PREF_ROOT "/size/width", w);
	purple_prefs_set_int(PREF_ROOT "/size/height", h);
}

static void
setup_roomlist(PurpleAccount *account)
{
	GntWidget *window, *tree, *hbox, *accounts;
	int iter;
	struct {
		const char *label;
		GCallback callback;
		GntWidget **widget;
	} buttons[] = {
		{_("Stop"), G_CALLBACK(fl_stop), &froomlist.stop},
		{_("Get"), G_CALLBACK(fl_get_list), &froomlist.getlist},
		{_("Add"), G_CALLBACK(fl_add_chat), &froomlist.add},
		{_("Close"), G_CALLBACK(fl_close), &froomlist.close},
		{NULL, NULL, NULL}
	};

	if (froomlist.window)
		return;

	froomlist.window = window = gnt_window_new();
	g_object_set(G_OBJECT(window), "vertical", TRUE, NULL);
	gnt_box_set_pad(GNT_BOX(window), 0);
	gnt_box_set_title(GNT_BOX(window), _("Room List"));
	gnt_box_set_alignment(GNT_BOX(window), GNT_ALIGN_MID);

	froomlist.accounts = accounts = gnt_combo_box_new();
	reset_account_list(account);
	gnt_box_add_widget(GNT_BOX(window), accounts);
	g_signal_connect(G_OBJECT(accounts), "selection-changed",
			G_CALLBACK(roomlist_account_changed), NULL);
	froomlist.account = gnt_combo_box_get_selected_data(GNT_COMBO_BOX(accounts));

	froomlist.tree = tree = gnt_tree_new_with_columns(2);
	gnt_tree_set_show_title(GNT_TREE(tree), TRUE);
	g_signal_connect(G_OBJECT(tree), "activate", G_CALLBACK(roomlist_activated), NULL);
	gnt_tree_set_column_titles(GNT_TREE(tree), _("Name"), "");
	gnt_tree_set_show_separator(GNT_TREE(tree), FALSE);
	gnt_tree_set_col_width(GNT_TREE(tree), 1, 1);
	gnt_tree_set_column_resizable(GNT_TREE(tree), 1, FALSE);
	gnt_tree_set_search_column(GNT_TREE(tree), 0);

	gnt_box_add_widget(GNT_BOX(window), tree);

	froomlist.details = gnt_text_view_new();
	gnt_text_view_set_flag(GNT_TEXT_VIEW(froomlist.details), GNT_TEXT_VIEW_TOP_ALIGN);
	gnt_box_add_widget(GNT_BOX(window), froomlist.details);
	gnt_widget_set_size(froomlist.details, -1, 8);

	hbox = gnt_hbox_new(FALSE);
	gnt_box_add_widget(GNT_BOX(window), hbox);

	for (iter = 0; buttons[iter].label; iter++) {
		GntWidget *button = gnt_button_new(buttons[iter].label);
		gnt_box_add_widget(GNT_BOX(hbox), button);
		g_signal_connect(G_OBJECT(button), "activate", buttons[iter].callback, NULL);
		*buttons[iter].widget = button;
		gnt_text_view_attach_scroll_widget(GNT_TEXT_VIEW(froomlist.details), button);
	}

	g_signal_connect(G_OBJECT(tree), "selection-changed", G_CALLBACK(roomlist_selection_changed), NULL);

	g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(unset_roomlist), NULL);
}

static void
fl_show_with_account(PurpleAccount *account)
{
	setup_roomlist(account);
	g_signal_handlers_disconnect_matched(G_OBJECT(froomlist.window), G_SIGNAL_MATCH_FUNC,
			0, 0, NULL, G_CALLBACK(size_changed_cb), NULL);
	gnt_widget_show(froomlist.window);
	gnt_screen_resize_widget(froomlist.window,
			purple_prefs_get_int(PREF_ROOT "/size/width"),
			purple_prefs_get_int(PREF_ROOT "/size/height"));
	g_signal_connect(G_OBJECT(froomlist.window), "size_changed", G_CALLBACK(size_changed_cb), NULL);
	gnt_window_present(froomlist.window);
}

static void
fl_destroy(G_GNUC_UNUSED gpointer data, GObject *list)
{
	if (!froomlist.window) {
		return;
	}

	if (G_OBJECT(froomlist.roomlist) == list) {
		froomlist.roomlist = NULL;
		gnt_tree_remove_all(GNT_TREE(froomlist.tree));
		gnt_widget_draw(froomlist.tree);
	}
}

static void
fl_create(PurpleRoomlist *list)
{
	g_object_set_data(G_OBJECT(list), "finch-ui", &froomlist);
	g_object_weak_ref(G_OBJECT(list), (GWeakNotify)fl_destroy, NULL);
	setup_roomlist(NULL);
	g_set_object(&froomlist.roomlist, list);
}

static void
fl_set_fields(G_GNUC_UNUSED PurpleRoomlist *list, G_GNUC_UNUSED GList *fields)
{
}

static void
fl_add_room(PurpleRoomlist *roomlist, PurpleRoomlistRoom *room)
{
	gchar *category = NULL;
	if (froomlist.roomlist != roomlist)
		return;

	gnt_tree_remove(GNT_TREE(froomlist.tree), room);
	gnt_tree_add_row_after(GNT_TREE(froomlist.tree), room,
			gnt_tree_create_row(GNT_TREE(froomlist.tree),
				purple_roomlist_room_get_name(room), ""),
		NULL, NULL);
	gnt_tree_set_expanded(GNT_TREE(froomlist.tree), room, category == NULL);
}

static PurpleRoomlistUiOps ui_ops = {
	.show_with_account = fl_show_with_account,
	.create = fl_create,
	.set_fields = fl_set_fields,
	.add_room = fl_add_room,
};

PurpleRoomlistUiOps *finch_roomlist_get_ui_ops(void)
{
	return &ui_ops;
}

void finch_roomlist_show_all(void)
{
	purple_roomlist_show_with_account(NULL);
}

void finch_roomlist_init(void)
{
	purple_prefs_add_none(PREF_ROOT);
	purple_prefs_add_none(PREF_ROOT "/size");
	purple_prefs_add_int(PREF_ROOT "/size/width", 60);
	purple_prefs_add_int(PREF_ROOT "/size/height", 15);
}

void finch_roomlist_uninit(void)
{
}