Mercurial > pidgin.yaz
annotate plugins/notify.c @ 3385:7931eb8bba19
[gaim-migrate @ 3404]
a compile fix from Ben Miller
committer: Tailor Script <tailor@pidgin.im>
author | Sean Egan <seanegan@gmail.com> |
---|---|
date | Thu, 08 Aug 2002 15:31:08 +0000 |
parents | 3cd2fbddf95a |
children | 5a5df7968b6e |
rev | line source |
---|---|
3374 | 1 /* Reworked by Etan Reisner |
2 * | |
3 * Added config dialog | |
4 * Added control over notification method | |
5 * Added control over when to release notification | |
6 */ | |
7 | |
191 | 8 #define GAIM_PLUGINS |
9 #include "gaim.h" | |
10 | |
11 #include <gtk/gtk.h> | |
192
e9ca9146ebf1
[gaim-migrate @ 202]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
191
diff
changeset
|
12 #include <string.h> |
3385 | 13 #include <X11/Xlib.h> |
3374 | 14 #include <X11/Xutil.h> |
15 #include <X11/Xatom.h> | |
16 #include <gdk/gdkx.h> | |
17 | |
18 guint choice; | |
19 #define REMOVE_FOCUS 0x00000001 | |
20 #define REMOVE_TYPE 0x00000002 | |
21 | |
22 guint method; | |
23 #define METHOD_STRING 0x00000001 | |
24 #define METHOD_QUOTE 0x00000002 | |
25 #define METHOD_URGENT 0x00000004 | |
191 | 26 |
27 void *handle; | |
3374 | 28 GtkWidget *Dialog = NULL; |
29 GtkWidget *Click, *Focus, *Type; | |
30 GtkWidget *String, *Quote, *Urgent, *Entry; | |
31 gchar *title_string; | |
191 | 32 |
3374 | 33 /* predefine some functions, less warnings */ |
34 void options(GtkWidget *widget, gpointer data); | |
35 void un_star(GtkWidget *widget, gpointer data); | |
36 void un_star_window(GtkWidget *widget, gpointer data); | |
37 void string_remove(GtkWidget *widget); | |
38 void quote_remove(GtkWidget *widget); | |
39 void urgent_remove(struct conversation *c); | |
40 | |
41 int received_im(struct gaim_connection *gc, char **who, char **what, void *m) { | |
191 | 42 char buf[256]; |
43 struct conversation *cnv = find_conversation(*who); | |
44 GtkWindow *win; | |
1047
ece2d1543b20
[gaim-migrate @ 1057]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1000
diff
changeset
|
45 char *me = g_strdup(normalize(gc->username)); |
3374 | 46 int revert_to_return; |
47 Window focus_return; | |
1047
ece2d1543b20
[gaim-migrate @ 1057]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1000
diff
changeset
|
48 |
ece2d1543b20
[gaim-migrate @ 1057]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1000
diff
changeset
|
49 if (!strcmp(me, normalize(*who))) { |
ece2d1543b20
[gaim-migrate @ 1057]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1000
diff
changeset
|
50 g_free(me); |
3374 | 51 return 0; |
1047
ece2d1543b20
[gaim-migrate @ 1057]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1000
diff
changeset
|
52 } |
ece2d1543b20
[gaim-migrate @ 1057]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1000
diff
changeset
|
53 g_free(me); |
191 | 54 |
55 if (cnv == NULL) | |
3374 | 56 { |
57 if (away_options & OPT_AWAY_QUEUE) | |
58 return 0; | |
1835 | 59 |
3374 | 60 cnv = new_conversation(*who); |
61 } | |
191 | 62 |
63 win = (GtkWindow *)cnv->window; | |
64 | |
3374 | 65 XGetInputFocus(GDK_WINDOW_XDISPLAY(cnv->window->window), &focus_return, &revert_to_return); |
66 | |
67 if (focus_return != GDK_WINDOW_XWINDOW(cnv->window->window)) { | |
68 if (method & METHOD_STRING) { | |
69 g_snprintf(buf, sizeof(buf), "%s", win->title); | |
70 if (!strstr(buf, title_string)) { | |
71 g_snprintf(buf, sizeof(buf), "%s %s", title_string, win->title); | |
72 gtk_window_set_title(win, buf); | |
73 } | |
74 } | |
75 if (method & METHOD_QUOTE) { | |
76 g_snprintf(buf, sizeof(buf), "%s", win->title); | |
77 if (g_strncasecmp(buf, "\"", 1)) { | |
78 g_snprintf(buf, sizeof(buf), "\"%s\"", win->title); | |
79 gtk_window_set_title(win, buf); | |
80 } | |
81 } | |
82 if (method & METHOD_URGENT) { | |
83 /* do it the gdk way for windows compatibility(?) if I can figure it out */ | |
84 /* gdk_property_change(win->window, WM_HINTS, WM_HINTS, 32, GDK_PROP_MODE_REPLACE, XUrgencyHint, 1); */ | |
85 XWMHints *hints = XGetWMHints(GDK_WINDOW_XDISPLAY(cnv->window->window), GDK_WINDOW_XWINDOW(cnv->window->window)); | |
86 hints->flags |= XUrgencyHint; | |
87 XSetWMHints(GDK_WINDOW_XDISPLAY(cnv->window->window), GDK_WINDOW_XWINDOW(cnv->window->window), hints); | |
88 } | |
89 } | |
90 | |
91 return 0; | |
92 } | |
93 | |
94 int sent_im(struct gaim_connection *gc, char *who, char **what, void *m) { | |
95 char buf[256]; | |
96 struct conversation *c = find_conversation(who); | |
97 | |
98 if (method & METHOD_QUOTE) | |
99 string_remove(c->window); | |
100 if (method & METHOD_STRING) | |
101 quote_remove(c->window); | |
102 if (method & METHOD_URGENT) | |
103 urgent_remove(c); | |
104 return 0; | |
105 } | |
106 | |
107 int new_conv(char *who) { | |
108 struct conversation *c = find_conversation(who); | |
109 | |
110 if (choice & REMOVE_FOCUS) { | |
111 gtk_signal_connect(GTK_OBJECT(c->window), "focus-in-event", GTK_SIGNAL_FUNC(un_star), NULL); | |
112 gtk_object_set_user_data(GTK_OBJECT(c->window), c); | |
113 } | |
114 else { | |
115 gtk_signal_connect(GTK_OBJECT(c->window), "button_press_event", GTK_SIGNAL_FUNC(un_star), NULL); | |
116 gtk_object_set_user_data(GTK_OBJECT(c->window), c); | |
117 gtk_signal_connect(GTK_OBJECT(c->text), "button_press_event", GTK_SIGNAL_FUNC(un_star_window), NULL); | |
118 gtk_object_set_user_data(GTK_OBJECT(c->text), c); | |
119 gtk_signal_connect(GTK_OBJECT(c->entry), "button_press_event", GTK_SIGNAL_FUNC(un_star_window), NULL); | |
120 gtk_object_set_user_data(GTK_OBJECT(c->entry), c); | |
121 /* gtk_signal_connect(GTK_OBJECT(c->text), "button_press_event", GTK_SIGNAL_FUNC(un_star_window), c); */ | |
122 /* gtk_signal_connect(GTK_OBJECT(c->entry), "button_press_event", GTK_SIGNAL_FUNC(un_star_window), c); */ | |
123 } | |
124 | |
125 if (choice & REMOVE_TYPE) { | |
126 gtk_signal_connect(GTK_OBJECT(c->entry), "key-press-event", GTK_SIGNAL_FUNC(un_star_window), NULL); | |
127 gtk_object_set_user_data(GTK_OBJECT(c->entry), (gpointer) c); | |
128 /* gtk_signal_connect(GTK_OBJECT(c->entry), "key-press-event", GTK_SIGNAL_FUNC(un_star_window), c); */ | |
191 | 129 } |
130 } | |
131 | |
3374 | 132 void un_star(GtkWidget *widget, gpointer data) { |
133 struct conversation *c = gtk_object_get_user_data(GTK_OBJECT(widget)); | |
134 /* struct conversation *c = data; */ | |
135 | |
136 if (method & METHOD_QUOTE) | |
137 quote_remove(widget); | |
138 if (method & METHOD_STRING) | |
139 string_remove(widget); | |
140 if (method & METHOD_URGENT) | |
141 urgent_remove(c); | |
142 return; | |
143 } | |
144 | |
145 void un_star_window(GtkWidget *widget, gpointer data) { | |
146 GtkWidget *parent = gtk_widget_get_ancestor(widget, GTK_TYPE_WINDOW); | |
147 gtk_object_set_user_data(GTK_OBJECT(parent), gtk_object_get_user_data(GTK_OBJECT(widget))); | |
148 un_star(parent, data); | |
149 } | |
150 | |
151 void string_remove(GtkWidget *widget) { | |
191 | 152 char buf[256]; |
3374 | 153 GtkWindow *win = GTK_WINDOW(widget); |
154 | |
155 g_snprintf(buf, sizeof(buf), "%s", win->title); | |
156 if (strstr(buf, title_string)) { | |
157 g_snprintf(buf, sizeof(buf), "%s", &win->title[strlen(title_string)]); | |
158 gtk_window_set_title(win, buf); | |
159 } | |
160 return; | |
161 } | |
162 | |
163 void quote_remove(GtkWidget *widget) { | |
164 char buf[256]; | |
165 GtkWindow *win = GTK_WINDOW(widget); | |
191 | 166 |
167 g_snprintf(buf, sizeof(buf), "%s", win->title); | |
3374 | 168 if (!g_strncasecmp(buf, "\"", 1)) { |
169 g_snprintf(buf, strlen(buf) - 1, "%s", &win->title[1]); | |
191 | 170 gtk_window_set_title(win, buf); |
171 } | |
3374 | 172 return; |
173 } | |
174 | |
175 void urgent_remove(struct conversation *c) { | |
176 char buf[256]; | |
177 GdkWindow *win = c->window->window; | |
178 | |
179 XWMHints *hints = XGetWMHints(GDK_WINDOW_XDISPLAY(c->window->window), GDK_WINDOW_XWINDOW(c->window->window)); | |
180 hints->flags &= ~XUrgencyHint; | |
181 XSetWMHints(GDK_WINDOW_XDISPLAY(c->window->window), GDK_WINDOW_XWINDOW(c->window->window), hints); | |
182 return; | |
183 } | |
184 | |
185 void save_notify_prefs() { | |
186 char *buf; | |
187 FILE *fp; | |
188 | |
189 buf = malloc(1000); | |
190 snprintf(buf, 1000, "%s/.gaim/.notify", getenv("HOME")); | |
191 if (!(fp = fopen(buf, "w"))) { | |
192 do_error_dialog(_("Unable to write to config file"), _("Notify plugin")); | |
193 return; | |
194 } | |
195 free(buf); | |
196 | |
197 fprintf(fp, "CHOICE=%d\n", choice); | |
198 fprintf(fp, "METHOD=%d\n", method); | |
199 fprintf(fp, "STRING=%s\n", title_string); | |
200 fclose(fp); | |
201 } | |
202 | |
203 void load_notify_prefs() { | |
204 gchar buf[1000]; | |
205 gchar **parsed; | |
206 FILE *fp; | |
207 | |
208 g_snprintf(buf, sizeof(buf), "%s/.gaim/.notify", getenv("HOME")); | |
209 if (!(fp = fopen(buf, "r"))) | |
210 return; | |
211 | |
212 while (fgets(buf, 1000, fp) != NULL) { | |
213 parsed = g_strsplit(g_strchomp(buf), "=", 2); | |
214 if (parsed[0] && parsed[1]) { | |
215 if (!strcmp(parsed[0], "CHOICE")) | |
216 choice = atoi(parsed[1]); | |
217 if (!strcmp(parsed[0], "METHOD")) | |
218 method = atoi(parsed[1]); | |
219 if (!strcmp(parsed[0], "STRING")) | |
220 title_string = parsed[1]; | |
221 } | |
222 } | |
223 fclose(fp); | |
224 return; | |
225 } | |
226 | |
227 void options(GtkWidget *widget, gpointer data) { | |
228 gint option = GPOINTER_TO_INT(data); | |
229 | |
230 if (option == 0) | |
231 choice ^= REMOVE_FOCUS; | |
232 else if (option == 1) | |
233 choice ^= REMOVE_TYPE; | |
234 else if (option == 2) { | |
235 method ^= METHOD_STRING; | |
236 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget))) | |
237 gtk_widget_set_sensitive(Entry, TRUE); | |
238 else | |
239 gtk_widget_set_sensitive(Entry, FALSE); | |
240 } | |
241 else if (option == 3) | |
242 method ^= METHOD_QUOTE; | |
243 else if (option == 4) | |
244 method ^= METHOD_URGENT; | |
245 } | |
246 | |
247 void setup_buttons() { | |
248 if (choice & REMOVE_FOCUS) | |
249 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(Focus), TRUE); | |
250 else | |
251 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(Click), TRUE); | |
252 if (choice & REMOVE_TYPE) | |
253 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(Type), TRUE); | |
254 else | |
255 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(Type), FALSE); | |
256 | |
257 if (method & METHOD_STRING) | |
258 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(String), TRUE); | |
259 else | |
260 gtk_widget_set_sensitive(Entry, FALSE); | |
261 | |
262 if (method & METHOD_QUOTE) | |
263 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(Quote), TRUE); | |
264 | |
265 if (method & METHOD_URGENT) | |
266 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(Urgent), TRUE); | |
267 | |
268 return; | |
269 } | |
270 | |
271 void close_dialog(GtkWidget *widget, gpointer data) { | |
272 gint option = GPOINTER_TO_INT(data); | |
273 | |
274 if (option > 0) { | |
275 title_string = g_strdup(gtk_entry_get_text(GTK_ENTRY(Entry))); | |
276 save_notify_prefs(); | |
277 } | |
278 else if (option < 0) | |
279 load_notify_prefs(); | |
280 | |
281 if (Dialog) | |
282 gtk_widget_destroy(Dialog); | |
283 Dialog = NULL; | |
191 | 284 } |
285 | |
1047
ece2d1543b20
[gaim-migrate @ 1057]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1000
diff
changeset
|
286 char *gaim_plugin_init(GModule *hndl) { |
191 | 287 handle = hndl; |
288 | |
3374 | 289 load_notify_prefs(); |
290 | |
191 | 291 gaim_signal_connect(handle, event_im_recv, received_im, NULL); |
292 gaim_signal_connect(handle, event_im_send, sent_im, NULL); | |
3374 | 293 gaim_signal_connect(handle, event_new_conversation, new_conv, NULL); |
1052
25f121faa75e
[gaim-migrate @ 1062]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1047
diff
changeset
|
294 |
25f121faa75e
[gaim-migrate @ 1062]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1047
diff
changeset
|
295 return NULL; |
191 | 296 } |
297 | |
298 char *name() { | |
299 return "Visual Notification"; | |
300 } | |
301 | |
302 char *description() { | |
303 return "Puts an asterisk in the title bar of all conversations" | |
304 " where you have not responded to a message yet."; | |
305 } | |
3374 | 306 |
307 void gaim_plugin_config() { | |
308 GtkWidget *dialog_vbox; | |
309 GtkWidget *button, *label; | |
310 GtkWidget *box, *box2, *box3, *frame; | |
311 | |
312 if (Dialog) | |
313 return; | |
314 | |
315 /* main config dialog */ | |
316 Dialog = gtk_dialog_new(); | |
317 /* gtk_widget_set_usize(Dialog, 275, -1); */ | |
318 gtk_window_set_title(GTK_WINDOW(Dialog), "Notify plugin configuration"); | |
319 gtk_window_set_policy(GTK_WINDOW(Dialog), FALSE, FALSE, TRUE); | |
320 gtk_signal_connect(GTK_OBJECT(Dialog), "destroy", GTK_SIGNAL_FUNC(close_dialog), GINT_TO_POINTER(-1)); | |
321 | |
322 dialog_vbox = GTK_DIALOG(Dialog)->vbox; | |
323 | |
324 /* Ok and Cancel buttons */ | |
325 box = gtk_hbox_new(FALSE, 8); | |
326 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(Dialog)->action_area), box); | |
327 | |
328 button = gtk_button_new_with_label(_("Cancel")); | |
329 gtk_box_pack_end(GTK_BOX(box), button, FALSE, FALSE, 0); | |
330 gtk_widget_set_usize(button, 80, -2); | |
331 gtk_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(close_dialog), GINT_TO_POINTER(0)); | |
332 | |
333 button = gtk_button_new_with_label(_("Ok")); | |
334 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 0); | |
335 gtk_widget_set_usize(button, 80, -2); | |
336 gtk_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(close_dialog), GINT_TO_POINTER(1)); | |
337 | |
338 /* main hbox */ | |
339 box = gtk_hbox_new(FALSE, 0); | |
340 gtk_box_pack_start(GTK_BOX(dialog_vbox), box, FALSE, FALSE, 0); | |
341 | |
342 /* un-notify choices */ | |
343 frame = gtk_frame_new(_("Remove notification when:")); | |
344 gtk_box_pack_start(GTK_BOX(box), frame, FALSE, FALSE, 0); | |
345 | |
346 box2 = gtk_vbox_new(FALSE, 0); | |
347 gtk_container_add(GTK_CONTAINER(frame), box2); | |
348 | |
349 Focus = gtk_radio_button_new_with_label(NULL, _("Conversation window gains focus.")); | |
350 gtk_box_pack_start(GTK_BOX(box2), Focus, FALSE, FALSE, 2); | |
351 | |
352 Click = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(Focus), _("Conversation window gets clicked.")); | |
353 gtk_box_pack_start(GTK_BOX(box2), Click, FALSE, FALSE, 2); | |
354 | |
355 Type = gtk_check_button_new_with_label(_("Type in conversation window")); | |
356 gtk_box_pack_start(GTK_BOX(box2), Type, FALSE, FALSE, 2); | |
357 | |
358 /* notification method choices */ | |
359 /* do I need/want any other notification methods? */ | |
360 frame = gtk_frame_new(_("Notification method:")); | |
361 gtk_box_pack_start(GTK_BOX(box), frame, FALSE, FALSE, 0); | |
362 | |
363 box2 = gtk_vbox_new(FALSE, 0); | |
364 gtk_container_add(GTK_CONTAINER(frame), box2); | |
365 | |
366 box3 = gtk_hbox_new(FALSE, 0); | |
367 gtk_box_pack_start(GTK_BOX(box2), box3, FALSE, FALSE, 0); | |
368 | |
369 String = gtk_check_button_new_with_label(_("Insert string into window title:")); | |
370 gtk_box_pack_start(GTK_BOX(box3), String, FALSE, FALSE, 0); | |
371 | |
372 Entry = gtk_entry_new_with_max_length(7); | |
373 gtk_box_pack_start(GTK_BOX(box3), Entry, FALSE, FALSE, 0); | |
374 if (!title_string) | |
375 gtk_entry_set_text(GTK_ENTRY(Entry), "(*) "); | |
376 else | |
377 gtk_entry_set_text(GTK_ENTRY(Entry), title_string); | |
378 | |
379 Quote = gtk_check_button_new_with_label(_("Quote window title.")); | |
380 gtk_box_pack_start(GTK_BOX(box2), Quote, FALSE, FALSE, 0); | |
381 | |
382 Urgent = gtk_check_button_new_with_label(_("Send URGENT to window manager.")); | |
383 gtk_box_pack_start(GTK_BOX(box2), Urgent, FALSE, FALSE, 0); | |
384 | |
385 /* setup buttons, then attach signals */ | |
386 setup_buttons(); | |
387 gtk_signal_connect(GTK_OBJECT(Focus), "toggled", GTK_SIGNAL_FUNC(options), GINT_TO_POINTER(0)); | |
388 gtk_signal_connect(GTK_OBJECT(Type), "toggled", GTK_SIGNAL_FUNC(options), GINT_TO_POINTER(1)); | |
389 gtk_signal_connect(GTK_OBJECT(String), "toggled", GTK_SIGNAL_FUNC(options), GINT_TO_POINTER(2)); | |
390 gtk_signal_connect(GTK_OBJECT(Quote), "toggled", GTK_SIGNAL_FUNC(options), GINT_TO_POINTER(3)); | |
391 gtk_signal_connect(GTK_OBJECT(Urgent), "toggled", GTK_SIGNAL_FUNC(options), GINT_TO_POINTER(4)); | |
392 | |
393 gtk_widget_show_all(Dialog); | |
394 } |