Mercurial > pidgin
annotate finch/gntnotify.c @ 16750:d9377d6b9e7c
I believe this fixes #439.
author | Sadrul Habib Chowdhury <imadil@gmail.com> |
---|---|
date | Tue, 01 May 2007 19:32:22 +0000 |
parents | 0f0832c13fcb |
children | 9a568611fa4a |
rev | line source |
---|---|
15817 | 1 /** |
2 * @file gntnotify.c GNT Notify API | |
16194
0f0832c13fcb
Rename the Doxygen group from gntui to finch and define the finch group
Richard Laager <rlaager@wiktel.com>
parents:
15870
diff
changeset
|
3 * @ingroup finch |
15817 | 4 * |
15870
66dff3dfdea6
Re-sed the copyright notices so they don't all talk about Purple.
Richard Laager <rlaager@wiktel.com>
parents:
15822
diff
changeset
|
5 * finch |
15817 | 6 * |
15870
66dff3dfdea6
Re-sed the copyright notices so they don't all talk about Purple.
Richard Laager <rlaager@wiktel.com>
parents:
15822
diff
changeset
|
7 * Finch is the legal property of its developers, whose names are too numerous |
15817 | 8 * to list here. Please refer to the COPYRIGHT file distributed with this |
9 * source distribution. | |
10 * | |
11 * This program is free software; you can redistribute it and/or modify | |
12 * it under the terms of the GNU General Public License as published by | |
13 * the Free Software Foundation; either version 2 of the License, or | |
14 * (at your option) any later version. | |
15 * | |
16 * This program is distributed in the hope that it will be useful, | |
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 * GNU General Public License for more details. | |
20 * | |
21 * You should have received a copy of the GNU General Public License | |
22 * along with this program; if not, write to the Free Software | |
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
24 */ | |
25 #include <gnt.h> | |
26 #include <gntbox.h> | |
27 #include <gntbutton.h> | |
28 #include <gntlabel.h> | |
29 #include <gnttree.h> | |
30 | |
31 #include <util.h> | |
32 | |
33 #include "gntnotify.h" | |
15822 | 34 #include "finch.h" |
15817 | 35 |
36 static struct | |
37 { | |
38 GntWidget *window; | |
39 GntWidget *tree; | |
40 } emaildialog; | |
41 | |
42 static void | |
15822 | 43 notify_msg_window_destroy_cb(GntWidget *window, PurpleNotifyMsgType type) |
15817 | 44 { |
15822 | 45 purple_notify_close(type, window); |
15817 | 46 } |
47 | |
48 static void * | |
15822 | 49 finch_notify_message(PurpleNotifyMsgType type, const char *title, |
15817 | 50 const char *primary, const char *secondary) |
51 { | |
52 GntWidget *window, *button; | |
53 GntTextFormatFlags pf = 0, sf = 0; | |
54 | |
55 switch (type) | |
56 { | |
15822 | 57 case PURPLE_NOTIFY_MSG_ERROR: |
15817 | 58 sf |= GNT_TEXT_FLAG_BOLD; |
15822 | 59 case PURPLE_NOTIFY_MSG_WARNING: |
15817 | 60 pf |= GNT_TEXT_FLAG_UNDERLINE; |
15822 | 61 case PURPLE_NOTIFY_MSG_INFO: |
15817 | 62 pf |= GNT_TEXT_FLAG_BOLD; |
63 break; | |
64 } | |
65 | |
66 window = gnt_box_new(FALSE, TRUE); | |
67 gnt_box_set_toplevel(GNT_BOX(window), TRUE); | |
68 gnt_box_set_title(GNT_BOX(window), title); | |
69 gnt_box_set_fill(GNT_BOX(window), FALSE); | |
70 gnt_box_set_alignment(GNT_BOX(window), GNT_ALIGN_MID); | |
71 | |
72 if (primary) | |
73 gnt_box_add_widget(GNT_BOX(window), | |
74 gnt_label_new_with_format(primary, pf)); | |
75 if (secondary) | |
76 gnt_box_add_widget(GNT_BOX(window), | |
77 gnt_label_new_with_format(secondary, sf)); | |
78 | |
79 button = gnt_button_new(_("OK")); | |
80 gnt_box_add_widget(GNT_BOX(window), button); | |
81 g_signal_connect_swapped(G_OBJECT(button), "activate", | |
82 G_CALLBACK(gnt_widget_destroy), window); | |
83 g_signal_connect(G_OBJECT(window), "destroy", | |
84 G_CALLBACK(notify_msg_window_destroy_cb), GINT_TO_POINTER(type)); | |
85 | |
86 gnt_widget_show(window); | |
87 return window; | |
88 } | |
89 | |
90 /* handle is, in all/most occasions, a GntWidget * */ | |
15822 | 91 static void finch_close_notify(PurpleNotifyType type, void *handle) |
15817 | 92 { |
93 GntWidget *widget = handle; | |
94 | |
95 if (!widget) | |
96 return; | |
97 | |
98 while (widget->parent) | |
99 widget = widget->parent; | |
100 | |
15822 | 101 if (type == PURPLE_NOTIFY_SEARCHRESULTS) |
102 purple_notify_searchresults_free(g_object_get_data(handle, "notify-results")); | |
15817 | 103 #if 1 |
104 /* This did not seem to be necessary */ | |
105 g_signal_handlers_disconnect_by_func(G_OBJECT(widget), | |
106 G_CALLBACK(notify_msg_window_destroy_cb), GINT_TO_POINTER(type)); | |
107 #endif | |
108 gnt_widget_destroy(widget); | |
109 } | |
110 | |
111 static void *finch_notify_formatted(const char *title, const char *primary, | |
112 const char *secondary, const char *text) | |
113 { | |
114 /* XXX: For now, simply strip the html and use _notify_message. For future use, | |
115 * there should be some way of parsing the makrups from GntTextView */ | |
15822 | 116 char *unformat = purple_markup_strip_html(text); |
15817 | 117 char *t = g_strdup_printf("%s%s%s", |
118 secondary ? secondary : "", | |
119 secondary ? "\n" : "", | |
120 unformat ? unformat : ""); | |
121 | |
15822 | 122 void *ret = finch_notify_message(PURPLE_NOTIFY_FORMATTED, title, primary, t); |
15817 | 123 |
124 g_free(t); | |
125 g_free(unformat); | |
126 | |
127 return ret; | |
128 } | |
129 | |
130 static void | |
131 reset_email_dialog() | |
132 { | |
133 emaildialog.window = NULL; | |
134 emaildialog.tree = NULL; | |
135 } | |
136 | |
137 static void | |
138 setup_email_dialog() | |
139 { | |
140 GntWidget *box, *tree, *button; | |
141 if (emaildialog.window) | |
142 return; | |
143 | |
144 emaildialog.window = box = gnt_vbox_new(FALSE); | |
145 gnt_box_set_toplevel(GNT_BOX(box), TRUE); | |
146 gnt_box_set_title(GNT_BOX(box), _("Emails")); | |
147 gnt_box_set_fill(GNT_BOX(box), FALSE); | |
148 gnt_box_set_alignment(GNT_BOX(box), GNT_ALIGN_MID); | |
149 gnt_box_set_pad(GNT_BOX(box), 0); | |
150 | |
151 gnt_box_add_widget(GNT_BOX(box), | |
152 gnt_label_new_with_format(_("You have mail!"), GNT_TEXT_FLAG_BOLD)); | |
153 | |
154 emaildialog.tree = tree = gnt_tree_new_with_columns(3); | |
155 gnt_tree_set_column_titles(GNT_TREE(tree), _("Account"), _("From"), _("Subject")); | |
156 gnt_tree_set_show_title(GNT_TREE(tree), TRUE); | |
157 gnt_tree_set_col_width(GNT_TREE(tree), 0, 15); | |
158 gnt_tree_set_col_width(GNT_TREE(tree), 1, 25); | |
159 gnt_tree_set_col_width(GNT_TREE(tree), 2, 25); | |
160 | |
161 gnt_box_add_widget(GNT_BOX(box), tree); | |
162 | |
163 button = gnt_button_new(_("Close")); | |
164 gnt_box_add_widget(GNT_BOX(box), button); | |
165 | |
166 g_signal_connect_swapped(G_OBJECT(button), "activate", G_CALLBACK(gnt_widget_destroy), box); | |
167 g_signal_connect(G_OBJECT(box), "destroy", G_CALLBACK(reset_email_dialog), NULL); | |
168 } | |
169 | |
170 static void * | |
15822 | 171 finch_notify_emails(PurpleConnection *gc, size_t count, gboolean detailed, |
15817 | 172 const char **subjects, const char **froms, const char **tos, |
173 const char **urls) | |
174 { | |
15822 | 175 PurpleAccount *account = purple_connection_get_account(gc); |
15817 | 176 GString *message = g_string_new(NULL); |
177 void *ret; | |
178 | |
179 if (!detailed) | |
180 { | |
181 g_string_append_printf(message, | |
182 ngettext("%s (%s) has %d new message.", | |
183 "%s (%s) has %d new messages.", | |
184 (int)count), | |
15822 | 185 tos ? *tos : purple_account_get_username(account), |
186 purple_account_get_protocol_name(account), (int)count); | |
15817 | 187 } |
188 else | |
189 { | |
190 char *to; | |
191 | |
192 setup_email_dialog(); | |
193 | |
15822 | 194 to = g_strdup_printf("%s (%s)", tos ? *tos : purple_account_get_username(account), |
195 purple_account_get_protocol_name(account)); | |
15817 | 196 gnt_tree_add_row_after(GNT_TREE(emaildialog.tree), GINT_TO_POINTER(time(NULL)), |
197 gnt_tree_create_row(GNT_TREE(emaildialog.tree), to, | |
198 froms ? *froms : "[Unknown sender]", | |
199 *subjects), | |
200 NULL, NULL); | |
201 g_free(to); | |
202 gnt_widget_show(emaildialog.window); | |
203 return NULL; | |
204 } | |
205 | |
15822 | 206 ret = finch_notify_message(PURPLE_NOTIFY_EMAIL, _("New Mail"), _("You have mail!"), message->str); |
15817 | 207 g_string_free(message, TRUE); |
208 return ret; | |
209 } | |
210 | |
211 static void * | |
15822 | 212 finch_notify_email(PurpleConnection *gc, const char *subject, const char *from, |
15817 | 213 const char *to, const char *url) |
214 { | |
215 return finch_notify_emails(gc, 1, subject != NULL, | |
216 subject ? &subject : NULL, | |
217 from ? &from : NULL, | |
218 to ? &to : NULL, | |
219 url ? &url : NULL); | |
220 } | |
221 | |
222 static void * | |
15822 | 223 finch_notify_userinfo(PurpleConnection *gc, const char *who, PurpleNotifyUserInfo *user_info) |
15817 | 224 { |
225 /* Xeroxed from gtknotify.c */ | |
226 char *primary; | |
227 char *info; | |
228 void *ui_handle; | |
229 | |
230 primary = g_strdup_printf(_("Info for %s"), who); | |
15822 | 231 info = purple_notify_user_info_get_text_with_newline(user_info, "<BR>"); |
15817 | 232 ui_handle = finch_notify_formatted(_("Buddy Information"), primary, NULL, info); |
233 g_free(info); | |
234 g_free(primary); | |
235 return ui_handle; | |
236 } | |
237 | |
238 static void | |
15822 | 239 notify_button_activated(GntWidget *widget, PurpleNotifySearchButton *b) |
15817 | 240 { |
241 GList *list = NULL; | |
15822 | 242 PurpleAccount *account = g_object_get_data(G_OBJECT(widget), "notify-account"); |
15817 | 243 gpointer data = g_object_get_data(G_OBJECT(widget), "notify-data"); |
244 | |
245 list = gnt_tree_get_selection_text_list(GNT_TREE(widget)); | |
246 | |
15822 | 247 b->callback(purple_account_get_connection(account), list, data); |
15817 | 248 g_list_foreach(list, (GFunc)g_free, NULL); |
249 g_list_free(list); | |
250 } | |
251 | |
252 static void | |
15822 | 253 finch_notify_sr_new_rows(PurpleConnection *gc, |
254 PurpleNotifySearchResults *results, void *data) | |
15817 | 255 { |
256 GntTree *tree = GNT_TREE(data); | |
257 GList *o; | |
258 | |
259 /* XXX: Do I need to empty the tree here? */ | |
260 | |
261 for (o = results->rows; o; o = o->next) | |
262 { | |
263 gnt_tree_add_row_after(GNT_TREE(tree), o->data, | |
264 gnt_tree_create_row_from_list(GNT_TREE(tree), o->data), | |
265 NULL, NULL); | |
266 } | |
267 } | |
268 | |
269 static void * | |
15822 | 270 finch_notify_searchresults(PurpleConnection *gc, const char *title, |
15817 | 271 const char *primary, const char *secondary, |
15822 | 272 PurpleNotifySearchResults *results, gpointer data) |
15817 | 273 { |
274 GntWidget *window, *tree, *box, *button; | |
275 GList *iter; | |
276 | |
277 window = gnt_vbox_new(FALSE); | |
278 gnt_box_set_toplevel(GNT_BOX(window), TRUE); | |
279 gnt_box_set_title(GNT_BOX(window), title); | |
280 gnt_box_set_fill(GNT_BOX(window), FALSE); | |
281 gnt_box_set_pad(GNT_BOX(window), 0); | |
282 gnt_box_set_alignment(GNT_BOX(window), GNT_ALIGN_MID); | |
283 | |
284 gnt_box_add_widget(GNT_BOX(window), | |
285 gnt_label_new_with_format(primary, GNT_TEXT_FLAG_BOLD)); | |
286 gnt_box_add_widget(GNT_BOX(window), | |
287 gnt_label_new_with_format(secondary, GNT_TEXT_FLAG_NORMAL)); | |
288 | |
289 tree = gnt_tree_new_with_columns(g_list_length(results->columns)); | |
290 gnt_tree_set_show_title(GNT_TREE(tree), TRUE); | |
291 gnt_box_add_widget(GNT_BOX(window), tree); | |
292 | |
293 box = gnt_hbox_new(TRUE); | |
294 | |
295 for (iter = results->buttons; iter; iter = iter->next) | |
296 { | |
15822 | 297 PurpleNotifySearchButton *b = iter->data; |
15817 | 298 const char *text; |
299 | |
300 switch (b->type) | |
301 { | |
15822 | 302 case PURPLE_NOTIFY_BUTTON_LABELED: |
15817 | 303 text = b->label; |
304 break; | |
15822 | 305 case PURPLE_NOTIFY_BUTTON_CONTINUE: |
15817 | 306 text = _("Continue"); |
307 break; | |
15822 | 308 case PURPLE_NOTIFY_BUTTON_ADD: |
15817 | 309 text = _("Add"); |
310 break; | |
15822 | 311 case PURPLE_NOTIFY_BUTTON_INFO: |
15817 | 312 text = _("Info"); |
313 break; | |
15822 | 314 case PURPLE_NOTIFY_BUTTON_IM: |
15817 | 315 text = _("IM"); |
316 break; | |
15822 | 317 case PURPLE_NOTIFY_BUTTON_JOIN: |
15817 | 318 text = _("Join"); |
319 break; | |
15822 | 320 case PURPLE_NOTIFY_BUTTON_INVITE: |
15817 | 321 text = _("Invite"); |
322 break; | |
323 default: | |
324 text = _("(none)"); | |
325 } | |
326 | |
327 button = gnt_button_new(text); | |
15822 | 328 g_object_set_data(G_OBJECT(button), "notify-account", purple_connection_get_account(gc)); |
15817 | 329 g_object_set_data(G_OBJECT(button), "notify-data", data); |
330 g_signal_connect_swapped(G_OBJECT(button), "activate", | |
331 G_CALLBACK(notify_button_activated), b); | |
332 | |
333 gnt_box_add_widget(GNT_BOX(box), button); | |
334 } | |
335 | |
336 gnt_box_add_widget(GNT_BOX(window), box); | |
337 | |
338 finch_notify_sr_new_rows(gc, results, tree); | |
339 | |
340 gnt_widget_show(window); | |
341 g_object_set_data(G_OBJECT(window), "notify-results", results); | |
342 | |
343 return tree; | |
344 } | |
345 | |
15822 | 346 static PurpleNotifyUiOps ops = |
15817 | 347 { |
348 .notify_message = finch_notify_message, | |
349 .close_notify = finch_close_notify, /* The rest of the notify-uiops return a GntWidget. | |
350 These widgets should be destroyed from here. */ | |
351 .notify_formatted = finch_notify_formatted, | |
352 .notify_email = finch_notify_email, | |
353 .notify_emails = finch_notify_emails, | |
354 .notify_userinfo = finch_notify_userinfo, | |
355 | |
356 .notify_searchresults = finch_notify_searchresults, | |
357 .notify_searchresults_new_rows = finch_notify_sr_new_rows, | |
358 .notify_uri = NULL /* This is of low-priority to me */ | |
359 }; | |
360 | |
15822 | 361 PurpleNotifyUiOps *finch_notify_get_ui_ops() |
15817 | 362 { |
363 return &ops; | |
364 } | |
365 | |
366 void finch_notify_init() | |
367 { | |
368 } | |
369 | |
370 void finch_notify_uninit() | |
371 { | |
372 } | |
373 | |
374 |