comparison console/plugins/gntgf.c @ 14139:44ec6c7cbc76

[gaim-migrate @ 16781] Allow setting the preferences for gnt-plugins. Add a guifications-like plugin for gntgaim. You can set its preferences. The preferences for core plugins are still not accessible. The makefile-foo will require changes once the split is complete. I am now just committing whatever works for me. committer: Tailor Script <tailor@pidgin.im>
author Sadrul Habib Chowdhury <imadil@gmail.com>
date Wed, 16 Aug 2006 05:12:48 +0000
parents
children 55e3db9db9f6
comparison
equal deleted inserted replaced
14138:7f276f375789 14139:44ec6c7cbc76
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 #define GAIM_PLUGINS
22
23 #define PLUGIN_STATIC_NAME "GntGf"
24
25 #define PREFS_EVENT "/plugins/gnt/gntgf/events"
26 #define PREFS_EVENT_SIGNONF PREFS_EVENT "/signonf"
27 #define PREFS_EVENT_IM_MSG PREFS_EVENT "/immsg"
28 #define PREFS_EVENT_CHAT_MSG PREFS_EVENT "/chatmsg"
29 #define PREFS_EVENT_CHAT_NICK PREFS_EVENT "/chatnick"
30
31 #include <glib.h>
32
33 #include <plugin.h>
34 #include <version.h>
35 #include <blist.h>
36 #include <conversation.h>
37
38 #include <gnt.h>
39 #include <gntbox.h>
40 #include <gntbutton.h>
41 #include <gntlabel.h>
42 #include <gnttree.h>
43
44 #include <gntplugin.h>
45
46 #define _(X) X
47
48 typedef struct
49 {
50 GntWidget *window;
51 int timer;
52 } GntToast;
53
54 static GList *toasters;
55 static int gpsy;
56
57 static void
58 destroy_toaster(GntToast *toast)
59 {
60 toasters = g_list_remove(toasters, toast);
61 gnt_widget_destroy(toast->window);
62 g_source_remove(toast->timer);
63 g_free(toast);
64 }
65
66 static gboolean
67 remove_toaster(GntToast *toast)
68 {
69 GList *iter;
70 int h;
71
72 gnt_widget_get_size(toast->window, NULL, &h);
73 gpsy -= h;
74
75 destroy_toaster(toast);
76
77 for (iter = toasters; iter; iter = iter->next)
78 {
79 int x, y;
80 toast = iter->data;
81 gnt_widget_get_position(toast->window, &x, &y);
82 y += h;
83 gnt_screen_move_widget(toast->window, x, y);
84 }
85
86 return FALSE;
87 }
88
89 static void
90 notify(const char *fmt, ...)
91 {
92 GntWidget *window;
93 GntToast *toast = g_new0(GntToast, 1);
94 char *str;
95 int h, w;
96 va_list args;
97
98 toast->window = window = gnt_vbox_new(FALSE);
99 GNT_WIDGET_SET_FLAGS(window, GNT_WIDGET_TRANSIENT);
100 GNT_WIDGET_UNSET_FLAGS(window, GNT_WIDGET_NO_BORDER);
101
102 va_start(args, fmt);
103 str = g_strdup_vprintf(fmt, args);
104 va_end(args);
105
106 gnt_box_add_widget(GNT_BOX(window),
107 gnt_label_new_with_format(str, GNT_TEXT_FLAG_HIGHLIGHT));
108
109 g_free(str);
110 gnt_widget_size_request(window);
111 gnt_widget_get_size(window, &w, &h);
112 gpsy += h;
113 gnt_widget_set_position(window, getmaxx(stdscr) - w - 1,
114 getmaxy(stdscr) - gpsy - 1);
115 gnt_widget_draw(window);
116
117 toast->timer = g_timeout_add(4000, (GSourceFunc)remove_toaster, toast);
118 toasters = g_list_prepend(toasters, toast);
119 }
120
121 static void
122 buddy_signed_on(GaimBuddy *buddy, gpointer null)
123 {
124 if (gaim_prefs_get_bool(PREFS_EVENT_SIGNONF))
125 notify(_("%s just signed on"), gaim_buddy_get_alias(buddy));
126 }
127
128 static void
129 buddy_signed_off(GaimBuddy *buddy, gpointer null)
130 {
131 if (gaim_prefs_get_bool(PREFS_EVENT_SIGNONF))
132 notify(_("%s just signed off"), gaim_buddy_get_alias(buddy));
133 }
134
135 static void
136 received_im_msg(GaimAccount *account, const char *sender, const char *msg,
137 GaimConversation *conv, GaimMessageFlags flags, gpointer null)
138 {
139 if (gaim_prefs_get_bool(PREFS_EVENT_IM_MSG))
140 notify(_("%s sent you a message"), sender);
141 }
142
143 static void
144 received_chat_msg(GaimAccount *account, const char *sender, const char *msg,
145 GaimConversation *conv, GaimMessageFlags flags, gpointer null)
146 {
147 if (gaim_prefs_get_bool(PREFS_EVENT_CHAT_NICK) && (flags & GAIM_MESSAGE_NICK))
148 notify(_("%s said your nick in %s"), sender, gaim_conversation_get_name(conv));
149 else if (gaim_prefs_get_bool(PREFS_EVENT_CHAT_MSG))
150 notify(_("%s sent a message in %s"), sender, gaim_conversation_get_name(conv));
151 }
152
153 static gboolean
154 plugin_load(GaimPlugin *plugin)
155 {
156 gaim_signal_connect(gaim_blist_get_handle(), "buddy-signed-on", plugin,
157 GAIM_CALLBACK(buddy_signed_on), NULL);
158 gaim_signal_connect(gaim_blist_get_handle(), "buddy-signed-off", plugin,
159 GAIM_CALLBACK(buddy_signed_off), NULL);
160 gaim_signal_connect(gaim_conversations_get_handle(), "received-im-msg", plugin,
161 GAIM_CALLBACK(received_im_msg), NULL);
162 gaim_signal_connect(gaim_conversations_get_handle(), "received-chat-msg", plugin,
163 GAIM_CALLBACK(received_chat_msg), NULL);
164
165 gpsy = 0;
166
167 return TRUE;
168 }
169
170 static gboolean
171 plugin_unload(GaimPlugin *plugin)
172 {
173 while (toasters)
174 {
175 GntToast *toast = toasters->data;
176 destroy_toaster(toast);
177 }
178 return TRUE;
179 }
180
181 static struct
182 {
183 char *pref;
184 char *display;
185 } prefs[] =
186 {
187 {PREFS_EVENT_SIGNONF, _("Buddy signs on/off")},
188 {PREFS_EVENT_IM_MSG, _("You receive an IMs")},
189 {PREFS_EVENT_CHAT_MSG, _("Someone speaks in a chat")},
190 {PREFS_EVENT_CHAT_NICK, _("Someone says your name in a chat")},
191 {NULL, NULL}
192 };
193
194 static void
195 pref_toggled(GntTree *tree, char *key, gpointer null)
196 {
197 gaim_prefs_set_bool(key, gnt_tree_get_choice(tree, key));
198 }
199
200 static GntWidget *
201 config_frame()
202 {
203 GntWidget *window, *tree;
204 int i;
205
206 window = gnt_vbox_new(FALSE);
207 gnt_box_set_pad(GNT_BOX(window), 0);
208 gnt_box_set_alignment(GNT_BOX(window), GNT_ALIGN_MID);
209 gnt_box_set_fill(GNT_BOX(window), TRUE);
210
211 gnt_box_add_widget(GNT_BOX(window),
212 gnt_label_new(_("Notify with a toaster when")));
213
214 tree = gnt_tree_new();
215 gnt_box_add_widget(GNT_BOX(window), tree);
216
217 for (i = 0; prefs[i].pref; i++)
218 {
219 gnt_tree_add_choice(GNT_TREE(tree), prefs[i].pref,
220 gnt_tree_create_row(GNT_TREE(tree), prefs[i].display), NULL, NULL);
221 gnt_tree_set_choice(GNT_TREE(tree), prefs[i].pref,
222 gaim_prefs_get_bool(prefs[i].pref));
223 }
224 gnt_tree_set_col_width(GNT_TREE(tree), 0, 40);
225 g_signal_connect(G_OBJECT(tree), "toggled", G_CALLBACK(pref_toggled), NULL);
226
227 return window;
228 }
229
230 static GaimPluginInfo info =
231 {
232 GAIM_PLUGIN_MAGIC,
233 GAIM_MAJOR_VERSION,
234 GAIM_MINOR_VERSION,
235 GAIM_PLUGIN_STANDARD,
236 GAIM_GNT_PLUGIN_TYPE,
237 0,
238 NULL,
239 GAIM_PRIORITY_DEFAULT,
240 "gntgf",
241 N_("GntGf"),
242 VERSION,
243 N_("Toaster plugin for GntGaim."),
244 N_("Toaster plugin for GntGaim."),
245 "Sadrul H Chowdhury <sadrul@users.sourceforge.net>",
246 "http://gaim.sourceforge.net",
247 plugin_load,
248 plugin_unload,
249 NULL,
250 config_frame,
251 NULL,
252 NULL,
253 NULL
254 };
255
256 static void
257 init_plugin(GaimPlugin *plugin)
258 {
259 gaim_prefs_add_none("/plugins");
260 gaim_prefs_add_none("/plugins/gnt");
261
262 gaim_prefs_add_none("/plugins/gnt/gntgf");
263 gaim_prefs_add_none(PREFS_EVENT);
264
265 gaim_prefs_add_bool(PREFS_EVENT_SIGNONF, TRUE);
266 gaim_prefs_add_bool(PREFS_EVENT_IM_MSG, TRUE);
267 gaim_prefs_add_bool(PREFS_EVENT_CHAT_MSG, TRUE);
268 gaim_prefs_add_bool(PREFS_EVENT_CHAT_NICK, TRUE);
269 }
270
271 GAIM_INIT_PLUGIN(PLUGIN_STATIC_NAME, init_plugin, info)