Mercurial > pidgin.yaz
annotate finch/plugins/gntgf.c @ 17103:bf27ef68e81a
Add a missing semi-colon so this will compile.
author | Richard Laager <rlaager@wiktel.com> |
---|---|
date | Sat, 19 May 2007 06:34:27 +0000 |
parents | 30829e806dae |
children | e22968d33131 |
rev | line source |
---|---|
15818 | 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 <util.h> | |
51 | |
52 #include <gnt.h> | |
53 #include <gntbox.h> | |
54 #include <gntbutton.h> | |
55 #include <gntcheckbox.h> | |
56 #include <gntlabel.h> | |
57 #include <gnttree.h> | |
58 | |
59 #include <gntplugin.h> | |
60 | |
61 typedef struct | |
62 { | |
63 GntWidget *window; | |
64 int timer; | |
65 int column; | |
66 } GntToast; | |
67 | |
68 static GList *toasters; | |
69 static int gpsy[MAX_COLS]; | |
70 static int gpsw[MAX_COLS]; | |
71 | |
72 static void | |
73 destroy_toaster(GntToast *toast) | |
74 { | |
75 toasters = g_list_remove(toasters, toast); | |
76 gnt_widget_destroy(toast->window); | |
77 g_source_remove(toast->timer); | |
78 g_free(toast); | |
79 } | |
80 | |
81 static gboolean | |
82 remove_toaster(GntToast *toast) | |
83 { | |
84 GList *iter; | |
85 int h; | |
86 int col; | |
87 int nwin[MAX_COLS]; | |
88 | |
89 gnt_widget_get_size(toast->window, NULL, &h); | |
90 gpsy[toast->column] -= h; | |
91 col = toast->column; | |
92 | |
93 memset(&nwin, 0, sizeof(nwin)); | |
94 destroy_toaster(toast); | |
95 | |
96 for (iter = toasters; iter; iter = iter->next) | |
97 { | |
98 int x, y; | |
99 toast = iter->data; | |
100 nwin[toast->column]++; | |
101 if (toast->column != col) continue; | |
102 gnt_widget_get_position(toast->window, &x, &y); | |
103 y += h; | |
104 gnt_screen_move_widget(toast->window, x, y); | |
105 } | |
106 | |
107 if (nwin[col] == 0) | |
108 gpsw[col] = 0; | |
109 | |
110 return FALSE; | |
111 } | |
112 | |
113 #ifdef HAVE_X11 | |
16320
94843d812e69
Gracefully do nothing if the WINDOWID is invalid. And plug a leak, thanks to deryni.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15864
diff
changeset
|
114 static int |
94843d812e69
Gracefully do nothing if the WINDOWID is invalid. And plug a leak, thanks to deryni.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15864
diff
changeset
|
115 error_handler(Display *dpy, XErrorEvent *error) |
94843d812e69
Gracefully do nothing if the WINDOWID is invalid. And plug a leak, thanks to deryni.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15864
diff
changeset
|
116 { |
94843d812e69
Gracefully do nothing if the WINDOWID is invalid. And plug a leak, thanks to deryni.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15864
diff
changeset
|
117 char buffer[1024]; |
94843d812e69
Gracefully do nothing if the WINDOWID is invalid. And plug a leak, thanks to deryni.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15864
diff
changeset
|
118 XGetErrorText(dpy, error->error_code, buffer, sizeof(buffer)); |
94843d812e69
Gracefully do nothing if the WINDOWID is invalid. And plug a leak, thanks to deryni.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15864
diff
changeset
|
119 purple_debug_error("gntgf", "Could not set urgent to the window: %s.\n", buffer); |
94843d812e69
Gracefully do nothing if the WINDOWID is invalid. And plug a leak, thanks to deryni.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15864
diff
changeset
|
120 return 0; |
94843d812e69
Gracefully do nothing if the WINDOWID is invalid. And plug a leak, thanks to deryni.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15864
diff
changeset
|
121 } |
94843d812e69
Gracefully do nothing if the WINDOWID is invalid. And plug a leak, thanks to deryni.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15864
diff
changeset
|
122 |
15818 | 123 static void |
124 urgent() | |
125 { | |
126 /* This is from deryni/tuomov's urgent_test.c */ | |
127 Display *dpy; | |
128 Window id; | |
129 const char *ids; | |
130 XWMHints *hints; | |
131 | |
132 ids = getenv("WINDOWID"); | |
133 if (ids == NULL) | |
134 return; | |
135 | |
136 id = atoi(ids); | |
137 | |
138 dpy = XOpenDisplay(NULL); | |
139 if (dpy == NULL) | |
140 return; | |
141 | |
16320
94843d812e69
Gracefully do nothing if the WINDOWID is invalid. And plug a leak, thanks to deryni.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15864
diff
changeset
|
142 XSetErrorHandler(error_handler); |
15818 | 143 hints = XGetWMHints(dpy, id); |
16320
94843d812e69
Gracefully do nothing if the WINDOWID is invalid. And plug a leak, thanks to deryni.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15864
diff
changeset
|
144 if (hints) { |
94843d812e69
Gracefully do nothing if the WINDOWID is invalid. And plug a leak, thanks to deryni.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15864
diff
changeset
|
145 hints->flags|=XUrgencyHint; |
94843d812e69
Gracefully do nothing if the WINDOWID is invalid. And plug a leak, thanks to deryni.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15864
diff
changeset
|
146 XSetWMHints(dpy, id, hints); |
94843d812e69
Gracefully do nothing if the WINDOWID is invalid. And plug a leak, thanks to deryni.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15864
diff
changeset
|
147 XFree(hints); |
94843d812e69
Gracefully do nothing if the WINDOWID is invalid. And plug a leak, thanks to deryni.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15864
diff
changeset
|
148 } |
94843d812e69
Gracefully do nothing if the WINDOWID is invalid. And plug a leak, thanks to deryni.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15864
diff
changeset
|
149 XSetErrorHandler(NULL); |
15818 | 150 |
151 XFlush(dpy); | |
152 XCloseDisplay(dpy); | |
153 } | |
154 #endif | |
155 | |
156 static void | |
157 notify(const char *fmt, ...) | |
158 { | |
159 GntWidget *window; | |
160 GntToast *toast; | |
161 char *str; | |
162 int h, w, i; | |
163 va_list args; | |
164 | |
15823 | 165 if (purple_prefs_get_bool(PREFS_BEEP)) |
15818 | 166 beep(); |
167 #ifdef HAVE_X11 | |
15823 | 168 if (purple_prefs_get_bool(PREFS_URGENT)) |
15818 | 169 urgent(); |
170 #endif | |
171 | |
172 window = gnt_vbox_new(FALSE); | |
173 GNT_WIDGET_SET_FLAGS(window, GNT_WIDGET_TRANSIENT); | |
174 GNT_WIDGET_UNSET_FLAGS(window, GNT_WIDGET_NO_BORDER); | |
175 | |
176 va_start(args, fmt); | |
177 str = g_strdup_vprintf(fmt, args); | |
178 va_end(args); | |
179 | |
180 gnt_box_add_widget(GNT_BOX(window), | |
181 gnt_label_new_with_format(str, GNT_TEXT_FLAG_HIGHLIGHT)); | |
182 | |
183 g_free(str); | |
184 gnt_widget_size_request(window); | |
185 gnt_widget_get_size(window, &w, &h); | |
186 for (i = 0; i < MAX_COLS && gpsy[i] + h >= getmaxy(stdscr) ; ++i) | |
187 ; | |
188 if (i >= MAX_COLS) { | |
15823 | 189 purple_debug_warning("GntGf", "Dude, that's way too many popups\n"); |
15818 | 190 gnt_widget_destroy(window); |
191 return; | |
192 } | |
193 | |
194 toast = g_new0(GntToast, 1); | |
195 toast->window = window; | |
196 toast->column = i; | |
197 gpsy[i] += h; | |
198 if (w > gpsw[i]) { | |
199 if (i == 0) | |
200 gpsw[i] = w; | |
201 else | |
202 gpsw[i] = gpsw[i - 1] + w + 1; | |
203 } | |
204 | |
205 if (i == 0 || (w + gpsw[i - 1] >= getmaxx(stdscr))) { | |
206 /* if it's going to be too far left, overlap. */ | |
207 gnt_widget_set_position(window, getmaxx(stdscr) - w - 1, | |
208 getmaxy(stdscr) - gpsy[i] - 1); | |
209 } else { | |
210 gnt_widget_set_position(window, getmaxx(stdscr) - gpsw[i - 1] - w - 1, | |
211 getmaxy(stdscr) - gpsy[i] - 1); | |
212 } | |
213 gnt_widget_draw(window); | |
214 | |
215 toast->timer = g_timeout_add(4000, (GSourceFunc)remove_toaster, toast); | |
216 toasters = g_list_prepend(toasters, toast); | |
217 } | |
218 | |
219 static void | |
15823 | 220 buddy_signed_on(PurpleBuddy *buddy, gpointer null) |
15818 | 221 { |
15823 | 222 if (purple_prefs_get_bool(PREFS_EVENT_SIGNONF)) |
223 notify(_("%s just signed on"), purple_buddy_get_alias(buddy)); | |
15818 | 224 } |
225 | |
226 static void | |
15823 | 227 buddy_signed_off(PurpleBuddy *buddy, gpointer null) |
15818 | 228 { |
15823 | 229 if (purple_prefs_get_bool(PREFS_EVENT_SIGNONF)) |
230 notify(_("%s just signed off"), purple_buddy_get_alias(buddy)); | |
15818 | 231 } |
232 | |
233 static void | |
15823 | 234 received_im_msg(PurpleAccount *account, const char *sender, const char *msg, |
235 PurpleConversation *conv, PurpleMessageFlags flags, gpointer null) | |
15818 | 236 { |
15823 | 237 if (purple_prefs_get_bool(PREFS_EVENT_IM_MSG)) |
15818 | 238 notify(_("%s sent you a message"), sender); |
239 } | |
240 | |
241 static void | |
15823 | 242 received_chat_msg(PurpleAccount *account, const char *sender, const char *msg, |
243 PurpleConversation *conv, PurpleMessageFlags flags, gpointer null) | |
15818 | 244 { |
245 const char *nick; | |
246 | |
15823 | 247 if (flags & PURPLE_MESSAGE_WHISPER) |
15818 | 248 return; |
249 | |
15823 | 250 nick = PURPLE_CONV_CHAT(conv)->nick; |
15818 | 251 |
252 if (g_utf8_collate(sender, nick) == 0) | |
253 return; | |
254 | |
15823 | 255 if (purple_prefs_get_bool(PREFS_EVENT_CHAT_NICK) && |
256 (purple_utf8_has_word(msg, nick))) | |
257 notify(_("%s said your nick in %s"), sender, purple_conversation_get_name(conv)); | |
258 else if (purple_prefs_get_bool(PREFS_EVENT_CHAT_MSG)) | |
259 notify(_("%s sent a message in %s"), sender, purple_conversation_get_name(conv)); | |
15818 | 260 } |
261 | |
262 static gboolean | |
15823 | 263 plugin_load(PurplePlugin *plugin) |
15818 | 264 { |
15823 | 265 purple_signal_connect(purple_blist_get_handle(), "buddy-signed-on", plugin, |
266 PURPLE_CALLBACK(buddy_signed_on), NULL); | |
267 purple_signal_connect(purple_blist_get_handle(), "buddy-signed-off", plugin, | |
268 PURPLE_CALLBACK(buddy_signed_off), NULL); | |
269 purple_signal_connect(purple_conversations_get_handle(), "received-im-msg", plugin, | |
270 PURPLE_CALLBACK(received_im_msg), NULL); | |
271 purple_signal_connect(purple_conversations_get_handle(), "received-chat-msg", plugin, | |
272 PURPLE_CALLBACK(received_chat_msg), NULL); | |
15818 | 273 |
274 memset(&gpsy, 0, sizeof(gpsy)); | |
275 memset(&gpsw, 0, sizeof(gpsw)); | |
276 | |
277 return TRUE; | |
278 } | |
279 | |
280 static gboolean | |
15823 | 281 plugin_unload(PurplePlugin *plugin) |
15818 | 282 { |
283 while (toasters) | |
284 { | |
285 GntToast *toast = toasters->data; | |
286 destroy_toaster(toast); | |
287 } | |
288 return TRUE; | |
289 } | |
290 | |
291 static struct | |
292 { | |
293 char *pref; | |
294 char *display; | |
295 } prefs[] = | |
296 { | |
297 {PREFS_EVENT_SIGNONF, N_("Buddy signs on/off")}, | |
298 {PREFS_EVENT_IM_MSG, N_("You receive an IM")}, | |
299 {PREFS_EVENT_CHAT_MSG, N_("Someone speaks in a chat")}, | |
300 {PREFS_EVENT_CHAT_NICK, N_("Someone says your name in a chat")}, | |
301 {NULL, NULL} | |
302 }; | |
303 | |
304 static void | |
305 pref_toggled(GntTree *tree, char *key, gpointer null) | |
306 { | |
15823 | 307 purple_prefs_set_bool(key, gnt_tree_get_choice(tree, key)); |
15818 | 308 } |
309 | |
310 static void | |
311 toggle_option(GntCheckBox *check, gpointer str) | |
312 { | |
15823 | 313 purple_prefs_set_bool(str, gnt_check_box_get_checked(check)); |
15818 | 314 } |
315 | |
316 static GntWidget * | |
317 config_frame() | |
318 { | |
319 GntWidget *window, *tree, *check; | |
320 int i; | |
321 | |
322 window = gnt_vbox_new(FALSE); | |
323 gnt_box_set_pad(GNT_BOX(window), 0); | |
324 gnt_box_set_alignment(GNT_BOX(window), GNT_ALIGN_MID); | |
325 gnt_box_set_fill(GNT_BOX(window), TRUE); | |
326 | |
327 gnt_box_add_widget(GNT_BOX(window), | |
328 gnt_label_new(_("Notify with a toaster when"))); | |
329 | |
330 tree = gnt_tree_new(); | |
331 gnt_box_add_widget(GNT_BOX(window), tree); | |
332 | |
333 for (i = 0; prefs[i].pref; i++) | |
334 { | |
335 gnt_tree_add_choice(GNT_TREE(tree), prefs[i].pref, | |
336 gnt_tree_create_row(GNT_TREE(tree), prefs[i].display), NULL, NULL); | |
337 gnt_tree_set_choice(GNT_TREE(tree), prefs[i].pref, | |
15823 | 338 purple_prefs_get_bool(prefs[i].pref)); |
15818 | 339 } |
340 gnt_tree_set_col_width(GNT_TREE(tree), 0, 40); | |
341 g_signal_connect(G_OBJECT(tree), "toggled", G_CALLBACK(pref_toggled), NULL); | |
342 | |
343 check = gnt_check_box_new(_("Beep too!")); | |
15823 | 344 gnt_check_box_set_checked(GNT_CHECK_BOX(check), purple_prefs_get_bool(PREFS_BEEP)); |
15818 | 345 g_signal_connect(G_OBJECT(check), "toggled", G_CALLBACK(toggle_option), PREFS_BEEP); |
346 gnt_box_add_widget(GNT_BOX(window), check); | |
347 | |
348 #ifdef HAVE_X11 | |
349 check = gnt_check_box_new(_("Set URGENT for the terminal window.")); | |
15823 | 350 gnt_check_box_set_checked(GNT_CHECK_BOX(check), purple_prefs_get_bool(PREFS_URGENT)); |
15818 | 351 g_signal_connect(G_OBJECT(check), "toggled", G_CALLBACK(toggle_option), PREFS_URGENT); |
352 gnt_box_add_widget(GNT_BOX(window), check); | |
353 #endif | |
354 | |
355 return window; | |
356 } | |
357 | |
15823 | 358 static PurplePluginInfo info = |
15818 | 359 { |
15823 | 360 PURPLE_PLUGIN_MAGIC, |
361 PURPLE_MAJOR_VERSION, | |
362 PURPLE_MINOR_VERSION, | |
363 PURPLE_PLUGIN_STANDARD, | |
364 FINCH_PLUGIN_TYPE, | |
15818 | 365 0, |
366 NULL, | |
15823 | 367 PURPLE_PRIORITY_DEFAULT, |
15818 | 368 "gntgf", |
369 N_("GntGf"), | |
370 VERSION, | |
371 N_("Toaster plugin"), | |
372 N_("Toaster plugin"), | |
373 "Sadrul H Chowdhury <sadrul@users.sourceforge.net>", | |
15864
4c707efebc0c
Use PURPLE_WEBSITE instead of listing the website directly (which was wrong because of the sed).
Richard Laager <rlaager@wiktel.com>
parents:
15823
diff
changeset
|
374 PURPLE_WEBSITE, |
15818 | 375 plugin_load, |
376 plugin_unload, | |
377 NULL, | |
378 config_frame, | |
379 NULL, | |
380 NULL, | |
16677
30829e806dae
And finch is up to date
Gary Kramlich <grim@reaperworld.com>
parents:
16320
diff
changeset
|
381 NULL, |
30829e806dae
And finch is up to date
Gary Kramlich <grim@reaperworld.com>
parents:
16320
diff
changeset
|
382 |
30829e806dae
And finch is up to date
Gary Kramlich <grim@reaperworld.com>
parents:
16320
diff
changeset
|
383 /* padding */ |
30829e806dae
And finch is up to date
Gary Kramlich <grim@reaperworld.com>
parents:
16320
diff
changeset
|
384 NULL, |
30829e806dae
And finch is up to date
Gary Kramlich <grim@reaperworld.com>
parents:
16320
diff
changeset
|
385 NULL, |
30829e806dae
And finch is up to date
Gary Kramlich <grim@reaperworld.com>
parents:
16320
diff
changeset
|
386 NULL, |
15818 | 387 NULL |
388 }; | |
389 | |
390 static void | |
15823 | 391 init_plugin(PurplePlugin *plugin) |
15818 | 392 { |
15823 | 393 purple_prefs_add_none("/plugins"); |
394 purple_prefs_add_none("/plugins/gnt"); | |
15818 | 395 |
15823 | 396 purple_prefs_add_none("/plugins/gnt/gntgf"); |
397 purple_prefs_add_none(PREFS_EVENT); | |
15818 | 398 |
15823 | 399 purple_prefs_add_bool(PREFS_EVENT_SIGNONF, TRUE); |
400 purple_prefs_add_bool(PREFS_EVENT_IM_MSG, TRUE); | |
401 purple_prefs_add_bool(PREFS_EVENT_CHAT_MSG, TRUE); | |
402 purple_prefs_add_bool(PREFS_EVENT_CHAT_NICK, TRUE); | |
15818 | 403 |
15823 | 404 purple_prefs_add_bool(PREFS_BEEP, TRUE); |
15818 | 405 #ifdef HAVE_X11 |
15823 | 406 purple_prefs_add_bool(PREFS_URGENT, FALSE); |
15818 | 407 #endif |
408 } | |
409 | |
15823 | 410 PURPLE_INIT_PLUGIN(PLUGIN_STATIC_NAME, init_plugin, info) |