1 /**
2 * @file gntgf.c Minimal toaster plugin in Gnt.
3 *
4 * Copyright (C) 2006 Sadrul Habib Chowdhury <sadrul@users.sourceforge.net>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
19 */
20
21
22 #include "internal.h"
23
24 #define PLUGIN_STATIC_NAME GntGf
25
26 #define PREFS_PREFIX "/plugins/gnt/gntgf"
27 #define PREFS_EVENT PREFS_PREFIX "/events"
28 #define PREFS_EVENT_SIGNONF PREFS_EVENT "/signonf"
29 #define PREFS_EVENT_IM_MSG PREFS_EVENT "/immsg"
30 #define PREFS_EVENT_CHAT_MSG PREFS_EVENT "/chatmsg"
31 #define PREFS_EVENT_CHAT_NICK PREFS_EVENT "/chatnick"
32 #define PREFS_BEEP PREFS_PREFIX "/beep"
33
34 #define MAX_COLS 3
35
36 #ifdef HAVE_X11
37 #define PREFS_URGENT PREFS_PREFIX "/urgent"
38
39 #include <X11/Xlib.h>
40 #include <X11/Xutil.h>
41 #endif
42
43 #include <glib.h>
44
45 #include <plugin.h>
46 #include <version.h>
47 #include <blist.h>
48 #include <conversation.h>
49 #include <debug.h>
50 #include <eventloop.h>
51 #include <util.h>
52
53 #include <gnt.h>
54 #include <gntbox.h>
55 #include <gntbutton.h>
56 #include <gntcheckbox.h>
57 #include <gntlabel.h>
58 #include <gnttree.h>
59
60 #include "gntplugin.h"
61 #include "gntconv.h"
62
63 typedef struct
64 {
65 GntWidget *window;
66 int timer;
67 int column;
68 } GntToast;
69
70 static GList *toasters;
71 static int gpsy[MAX_COLS];
72 static int gpsw[MAX_COLS];
73
74 static void
destroy_toaster(GntToast * toast)75 destroy_toaster(GntToast *toast)
76 {
77 toasters = g_list_remove(toasters, toast);
78 gnt_widget_destroy(toast->window);
79 purple_timeout_remove(toast->timer);
80 g_free(toast);
81 }
82
83 static gboolean
remove_toaster(GntToast * toast)84 remove_toaster(GntToast *toast)
85 {
86 GList *iter;
87 int h;
88 int col;
89 int nwin[MAX_COLS];
90
91 gnt_widget_get_size(toast->window, NULL, &h);
92 gpsy[toast->column] -= h;
93 col = toast->column;
94
95 memset(&nwin, 0, sizeof(nwin));
96 destroy_toaster(toast);
97
98 for (iter = toasters; iter; iter = iter->next)
99 {
100 int x, y;
101 toast = iter->data;
102 nwin[toast->column]++;
103 if (toast->column != col) continue;
104 gnt_widget_get_position(toast->window, &x, &y);
105 y += h;
106 gnt_screen_move_widget(toast->window, x, y);
107 }
108
109 if (nwin[col] == 0)
110 gpsw[col] = 0;
111
112 return FALSE;
113 }
114
115 #ifdef HAVE_X11
116 static int
error_handler(Display * dpy,XErrorEvent * error)117 error_handler(Display *dpy, XErrorEvent *error)
118 {
119 char buffer[1024];
120 XGetErrorText(dpy, error->error_code, buffer, sizeof(buffer));
121 purple_debug_error("gntgf", "Could not set urgent to the window: %s.\n", buffer);
122 return 0;
123 }
124
125 static void
urgent(void)126 urgent(void)
127 {
128 /* This is from deryni/tuomov's urgent_test.c */
129 Display *dpy;
130 Window id;
131 const char *ids;
132 XWMHints *hints;
133
134 ids = getenv("WINDOWID");
135 if (ids == NULL)
136 return;
137
138 id = atoi(ids);
139
140 dpy = XOpenDisplay(NULL);
141 if (dpy == NULL)
142 return;
143
144 XSetErrorHandler(error_handler);
145 hints = XGetWMHints(dpy, id);
146 if (hints) {
147 hints->flags|=XUrgencyHint;
148 XSetWMHints(dpy, id, hints);
149 XFree(hints);
150 }
151 XSetErrorHandler(NULL);
152
153 XFlush(dpy);
154 XCloseDisplay(dpy);
155 }
156 #endif
157
158 static void
notify(PurpleConversation * conv,const char * fmt,...)159 notify(PurpleConversation *conv, const char *fmt, ...)
160 {
161 GntWidget *window;
162 GntToast *toast;
163 char *str;
164 int h, w, i;
165 va_list args;
166
167 if (purple_prefs_get_bool(PREFS_BEEP))
168 beep();
169
170 if (conv != NULL) {
171 FinchConv *fc = conv->ui_data;
172 if (gnt_widget_has_focus(fc->window))
173 return;
174 }
175
176 #ifdef HAVE_X11
177 if (purple_prefs_get_bool(PREFS_URGENT))
178 urgent();
179 #endif
180
181 window = gnt_vbox_new(FALSE);
182 gnt_widget_set_transient(window, TRUE);
183 gnt_widget_set_has_border(window, TRUE);
184
185 va_start(args, fmt);
186 str = g_strdup_vprintf(fmt, args);
187 va_end(args);
188
189 gnt_box_add_widget(GNT_BOX(window),
190 gnt_label_new_with_format(str, GNT_TEXT_FLAG_HIGHLIGHT));
191
192 g_free(str);
193 gnt_widget_size_request(window);
194 gnt_widget_get_size(window, &w, &h);
195 for (i = 0; i < MAX_COLS && gpsy[i] + h >= getmaxy(stdscr) ; ++i)
196 ;
197 if (i >= MAX_COLS) {
198 purple_debug_warning("GntGf", "Dude, that's way too many popups\n");
199 gnt_widget_destroy(window);
200 return;
201 }
202
203 toast = g_new0(GntToast, 1);
204 toast->window = window;
205 toast->column = i;
206 gpsy[i] += h;
207 if (w > gpsw[i]) {
208 if (i == 0)
209 gpsw[i] = w;
210 else
211 gpsw[i] = gpsw[i - 1] + w + 1;
212 }
213
214 if (i == 0 || (w + gpsw[i - 1] >= getmaxx(stdscr))) {
215 /* if it's going to be too far left, overlap. */
216 gnt_widget_set_position(window, getmaxx(stdscr) - w - 1,
217 getmaxy(stdscr) - gpsy[i] - 1);
218 } else {
219 gnt_widget_set_position(window, getmaxx(stdscr) - gpsw[i - 1] - w - 1,
220 getmaxy(stdscr) - gpsy[i] - 1);
221 }
222 gnt_widget_draw(window);
223
224 toast->timer = purple_timeout_add_seconds(4, (GSourceFunc)remove_toaster, toast);
225 toasters = g_list_prepend(toasters, toast);
226 }
227
228 static void
buddy_signed_on(PurpleBuddy * buddy,gpointer null)229 buddy_signed_on(PurpleBuddy *buddy, gpointer null)
230 {
231 if (purple_prefs_get_bool(PREFS_EVENT_SIGNONF))
232 notify(NULL, _("%s just signed on"), purple_buddy_get_alias(buddy));
233 }
234
235 static void
buddy_signed_off(PurpleBuddy * buddy,gpointer null)236 buddy_signed_off(PurpleBuddy *buddy, gpointer null)
237 {
238 if (purple_prefs_get_bool(PREFS_EVENT_SIGNONF))
239 notify(NULL, _("%s just signed off"), purple_buddy_get_alias(buddy));
240 }
241
242 static void
received_im_msg(PurpleAccount * account,const char * sender,const char * msg,PurpleConversation * conv,PurpleMessageFlags flags,gpointer null)243 received_im_msg(PurpleAccount *account, const char *sender, const char *msg,
244 PurpleConversation *conv, PurpleMessageFlags flags, gpointer null)
245 {
246 if (purple_prefs_get_bool(PREFS_EVENT_IM_MSG))
247 notify(conv, _("%s sent you a message"), sender);
248 }
249
250 static void
received_chat_msg(PurpleAccount * account,const char * sender,const char * msg,PurpleConversation * conv,PurpleMessageFlags flags,gpointer null)251 received_chat_msg(PurpleAccount *account, const char *sender, const char *msg,
252 PurpleConversation *conv, PurpleMessageFlags flags, gpointer null)
253 {
254 const char *nick;
255
256 if (flags & PURPLE_MESSAGE_WHISPER)
257 return;
258
259 nick = PURPLE_CONV_CHAT(conv)->nick;
260
261 if (g_utf8_collate(sender, nick) == 0)
262 return;
263
264 if (purple_prefs_get_bool(PREFS_EVENT_CHAT_NICK) &&
265 (purple_utf8_has_word(msg, nick)))
266 notify(conv, _("%s said your nick in %s"), sender, purple_conversation_get_name(conv));
267 else if (purple_prefs_get_bool(PREFS_EVENT_CHAT_MSG))
268 notify(conv, _("%s sent a message in %s"), sender, purple_conversation_get_name(conv));
269 }
270
271 static gboolean
plugin_load(PurplePlugin * plugin)272 plugin_load(PurplePlugin *plugin)
273 {
274 purple_signal_connect(purple_blist_get_handle(), "buddy-signed-on", plugin,
275 PURPLE_CALLBACK(buddy_signed_on), NULL);
276 purple_signal_connect(purple_blist_get_handle(), "buddy-signed-off", plugin,
277 PURPLE_CALLBACK(buddy_signed_off), NULL);
278 purple_signal_connect(purple_conversations_get_handle(), "received-im-msg", plugin,
279 PURPLE_CALLBACK(received_im_msg), NULL);
280 purple_signal_connect(purple_conversations_get_handle(), "received-chat-msg", plugin,
281 PURPLE_CALLBACK(received_chat_msg), NULL);
282
283 memset(&gpsy, 0, sizeof(gpsy));
284 memset(&gpsw, 0, sizeof(gpsw));
285
286 return TRUE;
287 }
288
289 static gboolean
plugin_unload(PurplePlugin * plugin)290 plugin_unload(PurplePlugin *plugin)
291 {
292 while (toasters)
293 {
294 GntToast *toast = toasters->data;
295 destroy_toaster(toast);
296 }
297 return TRUE;
298 }
299
300 static struct
301 {
302 char *pref;
303 char *display;
304 } prefs[] =
305 {
306 {PREFS_EVENT_SIGNONF, N_("Buddy signs on/off")},
307 {PREFS_EVENT_IM_MSG, N_("You receive an IM")},
308 {PREFS_EVENT_CHAT_MSG, N_("Someone speaks in a chat")},
309 {PREFS_EVENT_CHAT_NICK, N_("Someone says your name in a chat")},
310 {NULL, NULL}
311 };
312
313 static void
pref_toggled(GntTree * tree,char * key,gpointer null)314 pref_toggled(GntTree *tree, char *key, gpointer null)
315 {
316 purple_prefs_set_bool(key, gnt_tree_get_choice(tree, key));
317 }
318
319 static void
toggle_option(GntCheckBox * check,gpointer str)320 toggle_option(GntCheckBox *check, gpointer str)
321 {
322 purple_prefs_set_bool(str, gnt_check_box_get_checked(check));
323 }
324
325 static GntWidget *
config_frame(void)326 config_frame(void)
327 {
328 GntWidget *window, *tree, *check;
329 int i;
330
331 window = gnt_vbox_new(FALSE);
332 gnt_box_set_pad(GNT_BOX(window), 0);
333 gnt_box_set_alignment(GNT_BOX(window), GNT_ALIGN_MID);
334 gnt_box_set_fill(GNT_BOX(window), TRUE);
335
336 gnt_box_add_widget(GNT_BOX(window),
337 /* Translators: "toaster" here means "pop-up". */
338 gnt_label_new(_("Notify with a toaster when")));
339
340 tree = gnt_tree_new();
341 gnt_box_add_widget(GNT_BOX(window), tree);
342
343 for (i = 0; prefs[i].pref; i++)
344 {
345 gnt_tree_add_choice(GNT_TREE(tree), prefs[i].pref,
346 gnt_tree_create_row(GNT_TREE(tree), prefs[i].display), NULL, NULL);
347 gnt_tree_set_choice(GNT_TREE(tree), prefs[i].pref,
348 purple_prefs_get_bool(prefs[i].pref));
349 }
350 gnt_tree_set_col_width(GNT_TREE(tree), 0, 40);
351 g_signal_connect(G_OBJECT(tree), "toggled", G_CALLBACK(pref_toggled), NULL);
352
353 check = gnt_check_box_new(_("Beep too!"));
354 gnt_check_box_set_checked(GNT_CHECK_BOX(check), purple_prefs_get_bool(PREFS_BEEP));
355 g_signal_connect(G_OBJECT(check), "toggled", G_CALLBACK(toggle_option), PREFS_BEEP);
356 gnt_box_add_widget(GNT_BOX(window), check);
357
358 #ifdef HAVE_X11
359 check = gnt_check_box_new(_("Set URGENT for the terminal window."));
360 gnt_check_box_set_checked(GNT_CHECK_BOX(check), purple_prefs_get_bool(PREFS_URGENT));
361 g_signal_connect(G_OBJECT(check), "toggled", G_CALLBACK(toggle_option), PREFS_URGENT);
362 gnt_box_add_widget(GNT_BOX(window), check);
363 #endif
364
365 return window;
366 }
367
368 static PurplePluginInfo info =
369 {
370 PURPLE_PLUGIN_MAGIC,
371 PURPLE_MAJOR_VERSION,
372 PURPLE_MINOR_VERSION,
373 PURPLE_PLUGIN_STANDARD,
374 FINCH_PLUGIN_TYPE,
375 0,
376 NULL,
377 PURPLE_PRIORITY_DEFAULT,
378 "gntgf",
379 N_("GntGf"),
380 DISPLAY_VERSION,
381 /* Translators: "toaster" here means "pop-up". */
382 N_("Toaster plugin"),
383 /* Translators: "toaster" here means "pop-up". */
384 N_("Toaster plugin"),
385 "Sadrul H Chowdhury <sadrul@users.sourceforge.net>",
386 PURPLE_WEBSITE,
387 plugin_load,
388 plugin_unload,
389 NULL,
390 config_frame,
391 NULL,
392 NULL,
393 NULL,
394
395 /* padding */
396 NULL,
397 NULL,
398 NULL,
399 NULL
400 };
401
402 static void
init_plugin(PurplePlugin * plugin)403 init_plugin(PurplePlugin *plugin)
404 {
405 purple_prefs_add_none("/plugins");
406 purple_prefs_add_none("/plugins/gnt");
407
408 purple_prefs_add_none("/plugins/gnt/gntgf");
409 purple_prefs_add_none(PREFS_EVENT);
410
411 purple_prefs_add_bool(PREFS_EVENT_SIGNONF, TRUE);
412 purple_prefs_add_bool(PREFS_EVENT_IM_MSG, TRUE);
413 purple_prefs_add_bool(PREFS_EVENT_CHAT_MSG, TRUE);
414 purple_prefs_add_bool(PREFS_EVENT_CHAT_NICK, TRUE);
415
416 purple_prefs_add_bool(PREFS_BEEP, TRUE);
417 #ifdef HAVE_X11
418 purple_prefs_add_bool(PREFS_URGENT, FALSE);
419 #endif
420 }
421
422 PURPLE_INIT_PLUGIN(PLUGIN_STATIC_NAME, init_plugin, info)
423