Replace functions which called once with their bodies
[pidgin-git.git] / finch / plugins / gntgf.c
blob141d64bc91dd264450b8f029c54c25dfca0fd8a5
1 /**
2 * Copyright (C) 2006 Sadrul Habib Chowdhury <sadrul@users.sourceforge.net>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
19 #include "internal.h"
21 #include NCURSES_HEADER
23 #define PLUGIN_STATIC_NAME GntGf
25 #define PREFS_PREFIX "/plugins/gnt/gntgf"
26 #define PREFS_EVENT PREFS_PREFIX "/events"
27 #define PREFS_EVENT_SIGNONF PREFS_EVENT "/signonf"
28 #define PREFS_EVENT_IM_MSG PREFS_EVENT "/immsg"
29 #define PREFS_EVENT_CHAT_MSG PREFS_EVENT "/chatmsg"
30 #define PREFS_EVENT_CHAT_NICK PREFS_EVENT "/chatnick"
31 #define PREFS_BEEP PREFS_PREFIX "/beep"
33 #define MAX_COLS 3
35 #ifdef HAVE_X11
36 #define PREFS_URGENT PREFS_PREFIX "/urgent"
38 #include <X11/Xlib.h>
39 #include <X11/Xutil.h>
40 #endif
42 #include <glib.h>
44 #include <plugins.h>
45 #include <version.h>
46 #include <buddylist.h>
47 #include <conversation.h>
48 #include <debug.h>
49 #include <eventloop.h>
50 #include <util.h>
52 #include <gnt.h>
53 #include <gntbox.h>
54 #include <gntbutton.h>
55 #include <gntcheckbox.h>
56 #include <gntlabel.h>
57 #include <gnttree.h>
59 #include "gntplugin.h"
60 #include "gntconv.h"
62 typedef struct
64 GntWidget *window;
65 int timer;
66 int column;
67 } GntToast;
69 static GList *toasters;
70 static int gpsy[MAX_COLS];
71 static int gpsw[MAX_COLS];
73 static void
74 destroy_toaster(GntToast *toast)
76 toasters = g_list_remove(toasters, toast);
77 gnt_widget_destroy(toast->window);
78 g_source_remove(toast->timer);
79 g_free(toast);
82 static gboolean
83 remove_toaster(GntToast *toast)
85 GList *iter;
86 int h;
87 int col;
88 int nwin[MAX_COLS];
90 gnt_widget_get_size(toast->window, NULL, &h);
91 gpsy[toast->column] -= h;
92 col = toast->column;
94 memset(&nwin, 0, sizeof(nwin));
95 destroy_toaster(toast);
97 for (iter = toasters; iter; iter = iter->next)
99 int x, y;
100 toast = iter->data;
101 nwin[toast->column]++;
102 if (toast->column != col) continue;
103 gnt_widget_get_position(toast->window, &x, &y);
104 y += h;
105 gnt_screen_move_widget(toast->window, x, y);
108 if (nwin[col] == 0)
109 gpsw[col] = 0;
111 return FALSE;
114 #ifdef HAVE_X11
115 static int
116 error_handler(Display *dpy, XErrorEvent *error)
118 char buffer[1024];
119 XGetErrorText(dpy, error->error_code, buffer, sizeof(buffer));
120 purple_debug_error("gntgf", "Could not set urgent to the window: %s.\n", buffer);
121 return 0;
124 static void
125 urgent(void)
127 /* This is from deryni/tuomov's urgent_test.c */
128 Display *dpy;
129 Window id;
130 const char *ids;
131 XWMHints *hints;
133 ids = getenv("WINDOWID");
134 if (ids == NULL)
135 return;
137 id = atoi(ids);
139 dpy = XOpenDisplay(NULL);
140 if (dpy == NULL)
141 return;
143 XSetErrorHandler(error_handler);
144 hints = XGetWMHints(dpy, id);
145 if (hints) {
146 hints->flags|=XUrgencyHint;
147 XSetWMHints(dpy, id, hints);
148 XFree(hints);
150 XSetErrorHandler(NULL);
152 XFlush(dpy);
153 XCloseDisplay(dpy);
155 #endif
157 static void
158 notify(PurpleConversation *conv, const char *fmt, ...)
160 GntWidget *window;
161 GntToast *toast;
162 char *str;
163 int h, w, i;
164 va_list args;
166 if (purple_prefs_get_bool(PREFS_BEEP))
167 beep();
169 if (conv != NULL) {
170 FinchConv *fc = FINCH_CONV(conv);
171 if (gnt_widget_has_focus(fc->window))
172 return;
175 #ifdef HAVE_X11
176 if (purple_prefs_get_bool(PREFS_URGENT))
177 urgent();
178 #endif
180 window = gnt_vbox_new(FALSE);
181 gnt_widget_set_transient(window, TRUE);
182 gnt_widget_set_has_border(window, TRUE);
184 va_start(args, fmt);
185 str = g_strdup_vprintf(fmt, args);
186 va_end(args);
188 gnt_box_add_widget(GNT_BOX(window),
189 gnt_label_new_with_format(str, GNT_TEXT_FLAG_HIGHLIGHT));
191 g_free(str);
192 gnt_widget_size_request(window);
193 gnt_widget_get_size(window, &w, &h);
194 for (i = 0; i < MAX_COLS && gpsy[i] + h >= getmaxy(stdscr) ; ++i)
196 if (i >= MAX_COLS) {
197 purple_debug_warning("GntGf", "Dude, that's way too many popups\n");
198 gnt_widget_destroy(window);
199 return;
202 toast = g_new0(GntToast, 1);
203 toast->window = window;
204 toast->column = i;
205 gpsy[i] += h;
206 if (w > gpsw[i]) {
207 if (i == 0)
208 gpsw[i] = w;
209 else
210 gpsw[i] = gpsw[i - 1] + w + 1;
213 if (i == 0 || (w + gpsw[i - 1] >= getmaxx(stdscr))) {
214 /* if it's going to be too far left, overlap. */
215 gnt_widget_set_position(window, getmaxx(stdscr) - w - 1,
216 getmaxy(stdscr) - gpsy[i] - 1);
217 } else {
218 gnt_widget_set_position(window, getmaxx(stdscr) - gpsw[i - 1] - w - 1,
219 getmaxy(stdscr) - gpsy[i] - 1);
221 gnt_widget_draw(window);
223 toast->timer = g_timeout_add_seconds(4, (GSourceFunc)remove_toaster, toast);
224 toasters = g_list_prepend(toasters, toast);
227 static void
228 buddy_signed_on(PurpleBuddy *buddy, gpointer null)
230 if (purple_prefs_get_bool(PREFS_EVENT_SIGNONF))
231 notify(NULL, _("%s just signed on"), purple_buddy_get_alias(buddy));
234 static void
235 buddy_signed_off(PurpleBuddy *buddy, gpointer null)
237 if (purple_prefs_get_bool(PREFS_EVENT_SIGNONF))
238 notify(NULL, _("%s just signed off"), purple_buddy_get_alias(buddy));
241 static void
242 received_im_msg(PurpleAccount *account, const char *sender, const char *msg,
243 PurpleIMConversation *im, PurpleMessageFlags flags, gpointer null)
245 if (purple_prefs_get_bool(PREFS_EVENT_IM_MSG))
246 notify(PURPLE_CONVERSATION(im), _("%s sent you a message"), sender);
249 static void
250 received_chat_msg(PurpleAccount *account, const char *sender, const char *msg,
251 PurpleChatConversation *chat, PurpleMessageFlags flags, gpointer null)
253 const char *nick;
254 PurpleConversation *conv = PURPLE_CONVERSATION(chat);
256 nick = purple_chat_conversation_get_nick(chat);
258 if (g_utf8_collate(sender, nick) == 0)
259 return;
261 if (purple_prefs_get_bool(PREFS_EVENT_CHAT_NICK) &&
262 (purple_utf8_has_word(msg, nick)))
263 notify(conv, _("%s said your nick in %s"), sender, purple_conversation_get_name(conv));
264 else if (purple_prefs_get_bool(PREFS_EVENT_CHAT_MSG))
265 notify(conv, _("%s sent a message in %s"), sender, purple_conversation_get_name(conv));
268 static struct
270 char *pref;
271 char *display;
272 } prefs[] =
274 {PREFS_EVENT_SIGNONF, N_("Buddy signs on/off")},
275 {PREFS_EVENT_IM_MSG, N_("You receive an IM")},
276 {PREFS_EVENT_CHAT_MSG, N_("Someone speaks in a chat")},
277 {PREFS_EVENT_CHAT_NICK, N_("Someone says your name in a chat")},
278 {NULL, NULL}
281 static void
282 pref_toggled(GntTree *tree, char *key, gpointer null)
284 purple_prefs_set_bool(key, gnt_tree_get_choice(tree, key));
287 static void
288 toggle_option(GntCheckBox *check, gpointer str)
290 purple_prefs_set_bool(str, gnt_check_box_get_checked(check));
293 static GntWidget *
294 config_frame(void)
296 GntWidget *window, *tree, *check;
297 int i;
299 window = gnt_vbox_new(FALSE);
300 gnt_box_set_pad(GNT_BOX(window), 0);
301 gnt_box_set_alignment(GNT_BOX(window), GNT_ALIGN_MID);
302 gnt_box_set_fill(GNT_BOX(window), TRUE);
304 gnt_box_add_widget(GNT_BOX(window),
305 /* Translators: "toaster" here means "pop-up". */
306 gnt_label_new(_("Notify with a toaster when")));
308 tree = gnt_tree_new();
309 gnt_box_add_widget(GNT_BOX(window), tree);
311 for (i = 0; prefs[i].pref; i++)
313 gnt_tree_add_choice(GNT_TREE(tree), prefs[i].pref,
314 gnt_tree_create_row(GNT_TREE(tree), prefs[i].display), NULL, NULL);
315 gnt_tree_set_choice(GNT_TREE(tree), prefs[i].pref,
316 purple_prefs_get_bool(prefs[i].pref));
318 gnt_tree_set_col_width(GNT_TREE(tree), 0, 40);
319 g_signal_connect(G_OBJECT(tree), "toggled", G_CALLBACK(pref_toggled), NULL);
321 check = gnt_check_box_new(_("Beep too!"));
322 gnt_check_box_set_checked(GNT_CHECK_BOX(check), purple_prefs_get_bool(PREFS_BEEP));
323 g_signal_connect(G_OBJECT(check), "toggled", G_CALLBACK(toggle_option), PREFS_BEEP);
324 gnt_box_add_widget(GNT_BOX(window), check);
326 #ifdef HAVE_X11
327 check = gnt_check_box_new(_("Set URGENT for the terminal window."));
328 gnt_check_box_set_checked(GNT_CHECK_BOX(check), purple_prefs_get_bool(PREFS_URGENT));
329 g_signal_connect(G_OBJECT(check), "toggled", G_CALLBACK(toggle_option), PREFS_URGENT);
330 gnt_box_add_widget(GNT_BOX(window), check);
331 #endif
333 return window;
336 static FinchPluginInfo *
337 plugin_query(GError **error)
339 const gchar * const authors[] = {
340 "Sadrul H Chowdhury <sadrul@users.sourceforge.net>",
341 NULL
344 return finch_plugin_info_new(
345 "id", "gntgf",
346 "name", N_("GntGf"),
347 "version", DISPLAY_VERSION,
348 "category", N_("Notification"),
349 "summary", N_("Toaster plugin"),
350 "description", N_("Toaster plugin"),
351 "authors", authors,
352 "website", PURPLE_WEBSITE,
353 "abi-version", PURPLE_ABI_VERSION,
354 "gnt-pref-frame-cb", config_frame,
355 NULL
359 static gboolean
360 plugin_load(PurplePlugin *plugin, GError **error)
362 purple_prefs_add_none("/plugins");
363 purple_prefs_add_none("/plugins/gnt");
365 purple_prefs_add_none("/plugins/gnt/gntgf");
366 purple_prefs_add_none(PREFS_EVENT);
368 purple_prefs_add_bool(PREFS_EVENT_SIGNONF, TRUE);
369 purple_prefs_add_bool(PREFS_EVENT_IM_MSG, TRUE);
370 purple_prefs_add_bool(PREFS_EVENT_CHAT_MSG, TRUE);
371 purple_prefs_add_bool(PREFS_EVENT_CHAT_NICK, TRUE);
373 purple_prefs_add_bool(PREFS_BEEP, TRUE);
374 #ifdef HAVE_X11
375 purple_prefs_add_bool(PREFS_URGENT, FALSE);
376 #endif
378 purple_signal_connect(purple_blist_get_handle(), "buddy-signed-on", plugin,
379 PURPLE_CALLBACK(buddy_signed_on), NULL);
380 purple_signal_connect(purple_blist_get_handle(), "buddy-signed-off", plugin,
381 PURPLE_CALLBACK(buddy_signed_off), NULL);
382 purple_signal_connect(purple_conversations_get_handle(), "received-im-msg", plugin,
383 PURPLE_CALLBACK(received_im_msg), NULL);
384 purple_signal_connect(purple_conversations_get_handle(), "received-chat-msg", plugin,
385 PURPLE_CALLBACK(received_chat_msg), NULL);
387 memset(&gpsy, 0, sizeof(gpsy));
388 memset(&gpsw, 0, sizeof(gpsw));
390 return TRUE;
393 static gboolean
394 plugin_unload(PurplePlugin *plugin, GError **error)
396 while (toasters)
398 GntToast *toast = toasters->data;
399 destroy_toaster(toast);
401 return TRUE;
404 PURPLE_PLUGIN_INIT(PLUGIN_STATIC_NAME, plugin_query, plugin_load, plugin_unload);