summaryrefslogtreecommitdiff
path: root/finch/plugins/gntgf/gntgf.c
blob: a4d0b3e87ceebd729f1ddd1d29d65e23825af484 (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
/**
 * Copyright (C) 2006 Sadrul Habib Chowdhury <sadrul@users.sourceforge.net>
 *
 * 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 NCURSES_HEADER

#define PREFS_SCHEMA "im.pidgin.Finch.plugin.GntGf"
#define PREFS_EVENT_SIGN_ON_OFF "event-sign-on-off"
#define PREFS_EVENT_IM_MSG "event-im-message"
#define PREFS_EVENT_CHAT_MSG "event-chat-message"
#define PREFS_EVENT_CHAT_NICK "event-chat-nick"
#define PREFS_BEEP "beep"

#define MAX_COLS	3

#ifdef HAVE_X11
#define PREFS_URGENT "urgent"

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#endif

#include <glib.h>
#include <glib/gi18n-lib.h>


#include <purple.h>

#include <gnt.h>

#include <finch.h>

typedef struct
{
	GntWidget *window;
	int timer;
	int column;
} GntToast;

static GList *toasters;
static int gpsy[MAX_COLS];
static int gpsw[MAX_COLS];

static void
destroy_toaster(GntToast *toast)
{
	toasters = g_list_remove(toasters, toast);
	gnt_widget_destroy(toast->window);
	g_source_remove(toast->timer);
	g_free(toast);
}

static gboolean
remove_toaster(GntToast *toast)
{
	GList *iter;
	int h;
	int col;
	int nwin[MAX_COLS];

	gnt_widget_get_size(toast->window, NULL, &h);
	gpsy[toast->column] -= h;
	col = toast->column;

	memset(&nwin, 0, sizeof(nwin));
	destroy_toaster(toast);

	for (iter = toasters; iter; iter = iter->next)
	{
		int x, y;
		toast = iter->data;
		nwin[toast->column]++;
		if (toast->column != col) continue;
		gnt_widget_get_position(toast->window, &x, &y);
		y += h;
		gnt_screen_move_widget(toast->window, x, y);
	}

	if (nwin[col] == 0)
		gpsw[col] = 0;

	return FALSE;
}

#ifdef HAVE_X11
static int
error_handler(Display *dpy, XErrorEvent *error)
{
	char buffer[1024];
	XGetErrorText(dpy, error->error_code, buffer, sizeof(buffer));
	purple_debug_error("gntgf", "Could not set urgent to the window: %s.\n", buffer);
	return 0;
}

static void
urgent(void)
{
	/* This is from deryni/tuomov's urgent_test.c */
	Display *dpy;
	Window id;
	const char *ids;
	XWMHints *hints;

	ids = getenv("WINDOWID");
	if (ids == NULL)
		return;

	id = atoi(ids);

	dpy = XOpenDisplay(NULL);
	if (dpy == NULL)
		return;

	XSetErrorHandler(error_handler);
	hints = XGetWMHints(dpy, id);
	if (hints) {
		hints->flags|=XUrgencyHint;
		XSetWMHints(dpy, id, hints);
		XFree(hints);
	}
	XSetErrorHandler(NULL);

	XFlush(dpy);
	XCloseDisplay(dpy);
}
#endif

static void
notify(PurpleConversation *conv, const char *fmt, ...)
{
	GntWidget *window;
	GntToast *toast;
	char *str;
	int h, w, i;
	va_list args;
	GSettings *settings = NULL;

	settings = g_settings_new_with_backend(PREFS_SCHEMA,
	                                       purple_core_get_settings_backend());

	if(g_settings_get_boolean(settings, PREFS_BEEP)) {
		beep();
	}

	if (conv != NULL) {
		FinchConv *fc = FINCH_CONV(conv);
		if (gnt_widget_has_focus(fc->window)) {
			g_object_unref(settings);
			return;
		}
	}

#ifdef HAVE_X11
	if(g_settings_get_boolean(settings, PREFS_URGENT)) {
		urgent();
	}
#endif

	g_clear_object(&settings);

	window = gnt_vbox_new(FALSE);
	gnt_widget_set_transient(window, TRUE);
	gnt_widget_set_has_border(window, TRUE);

	va_start(args, fmt);
	str = g_strdup_vprintf(fmt, args);
	va_end(args);

	gnt_box_add_widget(GNT_BOX(window),
			gnt_label_new_with_format(str, GNT_TEXT_FLAG_HIGHLIGHT));

	g_free(str);
	gnt_widget_size_request(window);
	gnt_widget_get_size(window, &w, &h);
	for (i = 0; i < MAX_COLS && gpsy[i] + h >= getmaxy(stdscr) ; ++i)
		;
	if (i >= MAX_COLS) {
		purple_debug_warning("GntGf", "Dude, that's way too many popups\n");
		gnt_widget_destroy(window);
		return;
	}

	toast = g_new0(GntToast, 1);
	toast->window = window;
	toast->column = i;
	gpsy[i] += h;
	if (w > gpsw[i]) {
		if (i == 0)
			gpsw[i] = w;
		else
			gpsw[i] = gpsw[i - 1] + w + 1;
	}

	if (i == 0 || (w + gpsw[i - 1] >= getmaxx(stdscr))) {
		/* if it's going to be too far left, overlap. */
		gnt_widget_set_position(window, getmaxx(stdscr) - w - 1,
				getmaxy(stdscr) - gpsy[i] - 1);
	} else {
		gnt_widget_set_position(window, getmaxx(stdscr) - gpsw[i - 1] - w - 1,
				getmaxy(stdscr) - gpsy[i] - 1);
	}
	gnt_widget_draw(window);

	toast->timer = g_timeout_add_seconds(4, (GSourceFunc)remove_toaster, toast);
	toasters = g_list_prepend(toasters, toast);
}

static void
buddy_signed_on(PurpleBuddy *buddy, G_GNUC_UNUSED gpointer data)
{
	GSettings *settings = NULL;

	settings = g_settings_new_with_backend(PREFS_SCHEMA,
	                                       purple_core_get_settings_backend());

	if(g_settings_get_boolean(settings, PREFS_EVENT_SIGN_ON_OFF)) {
		notify(NULL, _("%s just signed on"), purple_buddy_get_alias(buddy));
	}

	g_object_unref(settings);
}

static void
buddy_signed_off(PurpleBuddy *buddy, G_GNUC_UNUSED gpointer data)
{
	GSettings *settings = NULL;

	settings = g_settings_new_with_backend(PREFS_SCHEMA,
	                                       purple_core_get_settings_backend());

	if(g_settings_get_boolean(settings, PREFS_EVENT_SIGN_ON_OFF)) {
		notify(NULL, _("%s just signed off"), purple_buddy_get_alias(buddy));
	}

	g_object_unref(settings);
}

static void
received_im_msg(G_GNUC_UNUSED PurpleAccount *account, const char *sender,
                G_GNUC_UNUSED const char *msg, PurpleIMConversation *im,
                G_GNUC_UNUSED PurpleMessageFlags flags,
                G_GNUC_UNUSED gpointer data)
{
	GSettings *settings = NULL;

	settings = g_settings_new_with_backend(PREFS_SCHEMA,
	                                       purple_core_get_settings_backend());

	if(g_settings_get_boolean(settings, PREFS_EVENT_IM_MSG)) {
		notify(PURPLE_CONVERSATION(im), _("%s sent you a message"), sender);
	}

	g_object_unref(settings);
}

static void
received_chat_msg(G_GNUC_UNUSED PurpleAccount *account, const char *sender,
                  const char *msg, PurpleChatConversation *chat,
                  G_GNUC_UNUSED PurpleMessageFlags flags,
                  G_GNUC_UNUSED gpointer data)
{
	const char *nick;
	PurpleConversation *conv = PURPLE_CONVERSATION(chat);
	GSettings *settings = NULL;

	nick = purple_chat_conversation_get_nick(chat);

	if (g_utf8_collate(sender, nick) == 0) {
		return;
	}

	settings = g_settings_new_with_backend(PREFS_SCHEMA,
	                                       purple_core_get_settings_backend());

	if(g_settings_get_boolean(settings, PREFS_EVENT_CHAT_NICK) &&
	   purple_utf8_has_word(msg, nick))
	{
		notify(conv, _("%s said your nick in %s"), sender,
		       purple_conversation_get_name(conv));
	} else if(g_settings_get_boolean(settings, PREFS_EVENT_CHAT_MSG)) {
		notify(conv, _("%s sent a message in %s"), sender,
		       purple_conversation_get_name(conv));
	}

	g_object_unref(settings);
}

static struct
{
	char *pref;
	char *display;
} prefs[] =
{
	{PREFS_EVENT_SIGN_ON_OFF, N_("Buddy signs on/off")},
	{PREFS_EVENT_IM_MSG, N_("You receive an IM")},
	{PREFS_EVENT_CHAT_MSG, N_("Someone speaks in a chat")},
	{PREFS_EVENT_CHAT_NICK, N_("Someone says your name in a chat")},
	{NULL, NULL}
};

static void
pref_toggled(GntTree *tree, char *key, G_GNUC_UNUSED gpointer data) {
	purple_prefs_set_bool(key, gnt_tree_get_choice(tree, key));
}

static void
toggle_option(GntCheckBox *check, gpointer str)
{
	purple_prefs_set_bool(str, gnt_check_box_get_checked(check));
}

static GntWidget *
config_frame(void)
{
	GntWidget *window, *tree, *check;
	int i;

	window = gnt_vbox_new(FALSE);
	gnt_box_set_pad(GNT_BOX(window), 0);
	gnt_box_set_alignment(GNT_BOX(window), GNT_ALIGN_MID);
	gnt_box_set_fill(GNT_BOX(window), TRUE);

	gnt_box_add_widget(GNT_BOX(window),
			/* Translators: "toaster" here means "pop-up". */
			gnt_label_new(_("Notify with a toaster when")));

	tree = gnt_tree_new();
	gnt_box_add_widget(GNT_BOX(window), tree);

	for (i = 0; prefs[i].pref; i++)
	{
		gnt_tree_add_choice(GNT_TREE(tree), prefs[i].pref,
				gnt_tree_create_row(GNT_TREE(tree), prefs[i].display), NULL, NULL);
		gnt_tree_set_choice(GNT_TREE(tree), prefs[i].pref,
				purple_prefs_get_bool(prefs[i].pref));
	}
	gnt_tree_set_col_width(GNT_TREE(tree), 0, 40);
	g_signal_connect(G_OBJECT(tree), "toggled", G_CALLBACK(pref_toggled), NULL);

	check = gnt_check_box_new(_("Beep too!"));
	gnt_check_box_set_checked(GNT_CHECK_BOX(check), purple_prefs_get_bool(PREFS_BEEP));
	g_signal_connect(G_OBJECT(check), "toggled", G_CALLBACK(toggle_option), PREFS_BEEP);
	gnt_box_add_widget(GNT_BOX(window), check);

#ifdef HAVE_X11
	check = gnt_check_box_new(_("Set URGENT for the terminal window."));
	gnt_check_box_set_checked(GNT_CHECK_BOX(check), purple_prefs_get_bool(PREFS_URGENT));
	g_signal_connect(G_OBJECT(check), "toggled", G_CALLBACK(toggle_option), PREFS_URGENT);
	gnt_box_add_widget(GNT_BOX(window), check);
#endif

	return window;
}

static GPluginPluginInfo *
gnt_gf_query(G_GNUC_UNUSED GError **error) {
	const gchar * const authors[] = {
		"Sadrul H Chowdhury <sadrul@users.sourceforge.net>",
		NULL
	};

	return finch_plugin_info_new(
		"id",                 "gntgf",
		"name",               N_("GntGf"),
		"version",            DISPLAY_VERSION,
		"category",           N_("Notification"),
		"summary",            N_("Toaster plugin"),
		"description",        N_("Toaster plugin"),
		"authors",            authors,
		"website",            PURPLE_WEBSITE,
		"abi-version",        PURPLE_ABI_VERSION,
		"gnt-pref-frame-cb",  config_frame,
		NULL
	);
}

static gboolean
gnt_gf_load(GPluginPlugin *plugin, G_GNUC_UNUSED GError **error) {
	purple_signal_connect(purple_blist_get_handle(), "buddy-signed-on", plugin,
			G_CALLBACK(buddy_signed_on), NULL);
	purple_signal_connect(purple_blist_get_handle(), "buddy-signed-off", plugin,
			G_CALLBACK(buddy_signed_off), NULL);
	purple_signal_connect(purple_conversations_get_handle(), "received-im-msg", plugin,
			G_CALLBACK(received_im_msg), NULL);
	purple_signal_connect(purple_conversations_get_handle(), "received-chat-msg", plugin,
			G_CALLBACK(received_chat_msg), NULL);

	memset(&gpsy, 0, sizeof(gpsy));
	memset(&gpsw, 0, sizeof(gpsw));

	return TRUE;
}

static gboolean
gnt_gf_unload(G_GNUC_UNUSED GPluginPlugin *plugin,
              G_GNUC_UNUSED gboolean shutdown,
              G_GNUC_UNUSED GError **error)
{
	while (toasters)
	{
		GntToast *toast = toasters->data;
		destroy_toaster(toast);
	}
	return TRUE;
}

GPLUGIN_NATIVE_PLUGIN_DECLARE(gnt_gf)