comparison finch/gntnotify.c @ 15818:0e3a8505ebbe

renamed gaim-text to finch
author Sean Egan <seanegan@gmail.com>
date Sun, 18 Mar 2007 19:38:15 +0000
parents
children 32c366eeeb99
comparison
equal deleted inserted replaced
15817:317e7613e581 15818:0e3a8505ebbe
1 /**
2 * @file gntnotify.c GNT Notify API
3 * @ingroup gntui
4 *
5 * gaim
6 *
7 * Gaim is the legal property of its developers, whose names are too numerous
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"
34 #include "gntgaim.h"
35
36 static struct
37 {
38 GntWidget *window;
39 GntWidget *tree;
40 } emaildialog;
41
42 static void
43 notify_msg_window_destroy_cb(GntWidget *window, GaimNotifyMsgType type)
44 {
45 gaim_notify_close(type, window);
46 }
47
48 static void *
49 finch_notify_message(GaimNotifyMsgType type, const char *title,
50 const char *primary, const char *secondary)
51 {
52 GntWidget *window, *button;
53 GntTextFormatFlags pf = 0, sf = 0;
54
55 switch (type)
56 {
57 case GAIM_NOTIFY_MSG_ERROR:
58 sf |= GNT_TEXT_FLAG_BOLD;
59 case GAIM_NOTIFY_MSG_WARNING:
60 pf |= GNT_TEXT_FLAG_UNDERLINE;
61 case GAIM_NOTIFY_MSG_INFO:
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 * */
91 static void finch_close_notify(GaimNotifyType type, void *handle)
92 {
93 GntWidget *widget = handle;
94
95 if (!widget)
96 return;
97
98 while (widget->parent)
99 widget = widget->parent;
100
101 if (type == GAIM_NOTIFY_SEARCHRESULTS)
102 gaim_notify_searchresults_free(g_object_get_data(handle, "notify-results"));
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 */
116 char *unformat = gaim_markup_strip_html(text);
117 char *t = g_strdup_printf("%s%s%s",
118 secondary ? secondary : "",
119 secondary ? "\n" : "",
120 unformat ? unformat : "");
121
122 void *ret = finch_notify_message(GAIM_NOTIFY_FORMATTED, title, primary, t);
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 *
171 finch_notify_emails(GaimConnection *gc, size_t count, gboolean detailed,
172 const char **subjects, const char **froms, const char **tos,
173 const char **urls)
174 {
175 GaimAccount *account = gaim_connection_get_account(gc);
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),
185 tos ? *tos : gaim_account_get_username(account),
186 gaim_account_get_protocol_name(account), (int)count);
187 }
188 else
189 {
190 char *to;
191
192 setup_email_dialog();
193
194 to = g_strdup_printf("%s (%s)", tos ? *tos : gaim_account_get_username(account),
195 gaim_account_get_protocol_name(account));
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
206 ret = finch_notify_message(GAIM_NOTIFY_EMAIL, _("New Mail"), _("You have mail!"), message->str);
207 g_string_free(message, TRUE);
208 return ret;
209 }
210
211 static void *
212 finch_notify_email(GaimConnection *gc, const char *subject, const char *from,
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 *
223 finch_notify_userinfo(GaimConnection *gc, const char *who, GaimNotifyUserInfo *user_info)
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);
231 info = gaim_notify_user_info_get_text_with_newline(user_info, "<BR>");
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
239 notify_button_activated(GntWidget *widget, GaimNotifySearchButton *b)
240 {
241 GList *list = NULL;
242 GaimAccount *account = g_object_get_data(G_OBJECT(widget), "notify-account");
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
247 b->callback(gaim_account_get_connection(account), list, data);
248 g_list_foreach(list, (GFunc)g_free, NULL);
249 g_list_free(list);
250 }
251
252 static void
253 finch_notify_sr_new_rows(GaimConnection *gc,
254 GaimNotifySearchResults *results, void *data)
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 *
270 finch_notify_searchresults(GaimConnection *gc, const char *title,
271 const char *primary, const char *secondary,
272 GaimNotifySearchResults *results, gpointer data)
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 {
297 GaimNotifySearchButton *b = iter->data;
298 const char *text;
299
300 switch (b->type)
301 {
302 case GAIM_NOTIFY_BUTTON_LABELED:
303 text = b->label;
304 break;
305 case GAIM_NOTIFY_BUTTON_CONTINUE:
306 text = _("Continue");
307 break;
308 case GAIM_NOTIFY_BUTTON_ADD:
309 text = _("Add");
310 break;
311 case GAIM_NOTIFY_BUTTON_INFO:
312 text = _("Info");
313 break;
314 case GAIM_NOTIFY_BUTTON_IM:
315 text = _("IM");
316 break;
317 case GAIM_NOTIFY_BUTTON_JOIN:
318 text = _("Join");
319 break;
320 case GAIM_NOTIFY_BUTTON_INVITE:
321 text = _("Invite");
322 break;
323 default:
324 text = _("(none)");
325 }
326
327 button = gnt_button_new(text);
328 g_object_set_data(G_OBJECT(button), "notify-account", gaim_connection_get_account(gc));
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
346 static GaimNotifyUiOps ops =
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
361 GaimNotifyUiOps *finch_notify_get_ui_ops()
362 {
363 return &ops;
364 }
365
366 void finch_notify_init()
367 {
368 }
369
370 void finch_notify_uninit()
371 {
372 }
373
374