Minor changelog updates
[pidgin-git.git] / finch / gntstatus.c
blob7bb115edbce7ac5f325a11ebb55342efcaae7f47
1 /**
2 * @file gntstatus.c GNT Status API
3 * @ingroup finch
4 */
6 /* finch
8 * Finch is the legal property of its developers, whose names are too numerous
9 * to list here. Please refer to the COPYRIGHT file distributed with this
10 * source distribution.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
26 #include <gnt.h>
27 #include <gntbox.h>
28 #include <gntbutton.h>
29 #include <gntcombobox.h>
30 #include <gntentry.h>
31 #include <gntlabel.h>
32 #include <gntline.h>
33 #include <gnttree.h>
34 #include <gntutils.h>
36 #include "finch.h"
38 #include <notify.h>
39 #include <request.h>
41 #include "gntstatus.h"
43 static struct
45 GntWidget *window;
46 GntWidget *tree;
47 } statuses;
49 typedef struct
51 PurpleSavedStatus *saved;
52 GntWidget *window;
53 GntWidget *title;
54 GntWidget *type;
55 GntWidget *message;
56 GntWidget *tree;
57 GHashTable *hash; /* list of windows for substatuses */
58 } EditStatus;
60 typedef struct
62 PurpleAccount *account;
63 const PurpleStatusType *type;
64 char *message;
65 } RowInfo;
67 typedef struct
69 GntWidget *window;
70 GntWidget *type;
71 GntWidget *message;
73 EditStatus *parent;
74 RowInfo *key;
75 } EditSubStatus;
77 static GList *edits; /* List of opened edit-status dialogs */
79 static void
80 reset_status_window(GntWidget *widget, gpointer null)
82 statuses.window = NULL;
83 statuses.tree = NULL;
86 static void
87 populate_statuses(GntTree *tree)
89 GList *list;
91 for (list = purple_savedstatuses_get_all(); list; list = list->next)
93 PurpleSavedStatus *saved = list->data;
94 const char *title, *type, *message;
96 if (purple_savedstatus_is_transient(saved))
97 continue;
99 title = purple_savedstatus_get_title(saved);
100 type = purple_primitive_get_name_from_type(purple_savedstatus_get_type(saved));
101 message = purple_savedstatus_get_message(saved); /* XXX: Strip possible markups */
103 gnt_tree_add_row_last(tree, saved,
104 gnt_tree_create_row(tree, title, type, message), NULL);
108 static void
109 really_delete_status(PurpleSavedStatus *saved)
111 GList *iter;
113 for (iter = edits; iter; iter = iter->next)
115 EditStatus *edit = iter->data;
116 if (edit->saved == saved)
118 gnt_widget_destroy(edit->window);
119 break;
123 if (statuses.tree)
124 gnt_tree_remove(GNT_TREE(statuses.tree), saved);
126 purple_savedstatus_delete(purple_savedstatus_get_title(saved));
129 static void
130 ask_before_delete(GntWidget *button, gpointer null)
132 char *ask;
133 PurpleSavedStatus *saved;
135 g_return_if_fail(statuses.tree != NULL);
137 saved = gnt_tree_get_selection_data(GNT_TREE(statuses.tree));
138 ask = g_strdup_printf(_("Are you sure you want to delete \"%s\""),
139 purple_savedstatus_get_title(saved));
141 purple_request_action(saved, _("Delete Status"), ask, NULL, 0,
142 NULL, NULL, NULL,
143 saved, 2,
144 _("Delete"), really_delete_status,
145 _("Cancel"), NULL);
146 g_free(ask);
149 static void
150 use_savedstatus_cb(GntWidget *widget, gpointer null)
152 g_return_if_fail(statuses.tree != NULL);
154 purple_savedstatus_activate(gnt_tree_get_selection_data(GNT_TREE(statuses.tree)));
157 static void
158 edit_savedstatus_cb(GntWidget *widget, gpointer null)
160 g_return_if_fail(statuses.tree != NULL);
162 finch_savedstatus_edit(gnt_tree_get_selection_data(GNT_TREE(statuses.tree)));
165 void finch_savedstatus_show_all()
167 GntWidget *window, *tree, *box, *button;
168 int widths[] = {25, 12, 35};
169 if (statuses.window) {
170 gnt_window_present(statuses.window);
171 return;
174 statuses.window = window = gnt_vbox_new(FALSE);
175 gnt_box_set_toplevel(GNT_BOX(window), TRUE);
176 gnt_box_set_title(GNT_BOX(window), _("Saved Statuses"));
177 gnt_box_set_fill(GNT_BOX(window), FALSE);
178 gnt_box_set_alignment(GNT_BOX(window), GNT_ALIGN_MID);
179 gnt_box_set_pad(GNT_BOX(window), 0);
181 /* XXX: Add some sorting function to sort alphabetically, perhaps */
182 statuses.tree = tree = gnt_tree_new_with_columns(3);
183 gnt_tree_set_column_titles(GNT_TREE(tree), _("Title"), _("Type"), _("Message"));
184 gnt_tree_set_show_title(GNT_TREE(tree), TRUE);
185 gnt_tree_set_column_width_ratio(GNT_TREE(tree), widths);
186 gnt_widget_set_size(tree, 72, 0);
187 gnt_box_add_widget(GNT_BOX(window), tree);
189 populate_statuses(GNT_TREE(tree));
191 box = gnt_hbox_new(FALSE);
192 gnt_box_add_widget(GNT_BOX(window), box);
194 button = gnt_button_new(_("Use"));
195 gnt_box_add_widget(GNT_BOX(box), button);
196 g_signal_connect(G_OBJECT(button), "activate",
197 G_CALLBACK(use_savedstatus_cb), NULL);
199 button = gnt_button_new(_("Add"));
200 gnt_box_add_widget(GNT_BOX(box), button);
201 gnt_util_set_trigger_widget(tree, GNT_KEY_INS, button);
202 g_signal_connect_swapped(G_OBJECT(button), "activate",
203 G_CALLBACK(finch_savedstatus_edit), NULL);
205 button = gnt_button_new(_("Edit"));
206 gnt_box_add_widget(GNT_BOX(box), button);
207 g_signal_connect(G_OBJECT(button), "activate",
208 G_CALLBACK(edit_savedstatus_cb), NULL);
210 button = gnt_button_new(_("Delete"));
211 gnt_box_add_widget(GNT_BOX(box), button);
212 gnt_util_set_trigger_widget(tree, GNT_KEY_DEL, button);
213 g_signal_connect(G_OBJECT(button), "activate",
214 G_CALLBACK(ask_before_delete), NULL);
216 button = gnt_button_new(_("Close"));
217 gnt_box_add_widget(GNT_BOX(box), button);
218 g_signal_connect_swapped(G_OBJECT(button), "activate",
219 G_CALLBACK(gnt_widget_destroy), window);
221 g_signal_connect(G_OBJECT(window), "destroy",
222 G_CALLBACK(reset_status_window), NULL);
223 gnt_widget_show(window);
226 static void
227 destroy_substatus_win(PurpleAccount *account, EditSubStatus *sub, gpointer null)
229 gnt_widget_destroy(sub->window); /* the "destroy" callback will remove entry from the hashtable */
232 static void
233 free_key(gpointer key, gpointer n)
235 RowInfo *row = key;
236 g_free(row->message);
237 g_free(key);
241 static void
242 update_edit_list(GntWidget *widget, EditStatus *edit)
244 edits = g_list_remove(edits, edit);
245 purple_notify_close_with_handle(edit);
246 g_hash_table_foreach(edit->hash, (GHFunc)destroy_substatus_win, NULL);
247 g_list_foreach((GList*)gnt_tree_get_rows(GNT_TREE(edit->tree)), free_key, NULL);
248 g_free(edit);
251 static void
252 set_substatuses(EditStatus *edit)
254 GList *iter;
255 for (iter = gnt_tree_get_rows(GNT_TREE(edit->tree)); iter; iter = iter->next) {
256 RowInfo *key = iter->data;
257 if (gnt_tree_get_choice(GNT_TREE(edit->tree), key)) {
258 purple_savedstatus_set_substatus(edit->saved, key->account, key->type, key->message);
264 static void
265 use_trans_status_cb(GntWidget *button, EditStatus *edit)
267 const char *message;
268 PurpleStatusPrimitive prim;
269 PurpleSavedStatus *saved;
271 message = gnt_entry_get_text(GNT_ENTRY(edit->message));
272 prim = GPOINTER_TO_INT(gnt_combo_box_get_selected_data(GNT_COMBO_BOX(edit->type)));
274 saved = purple_savedstatus_find_transient_by_type_and_message(prim, message);
275 if (saved == NULL) {
276 saved = purple_savedstatus_new(NULL, prim);
277 edit->saved = saved;
278 set_substatuses(edit);
280 purple_savedstatus_set_message(saved, message);
281 purple_savedstatus_activate(saved);
282 gnt_widget_destroy(edit->window);
285 static void
286 save_savedstatus_cb(GntWidget *button, EditStatus *edit)
288 const char *title, *message;
289 PurpleStatusPrimitive prim;
290 PurpleSavedStatus *find;
292 title = gnt_entry_get_text(GNT_ENTRY(edit->title));
293 message = gnt_entry_get_text(GNT_ENTRY(edit->message));
294 if (!message || !*message)
295 message = NULL;
297 prim = GPOINTER_TO_INT(gnt_combo_box_get_selected_data(GNT_COMBO_BOX(edit->type)));
299 if (!title || !*title)
301 purple_notify_error(edit, _("Error"), _("Invalid title"),
302 _("Please enter a non-empty title for the status."));
303 gnt_box_give_focus_to_child(GNT_BOX(edit->window), edit->title);
304 return;
307 find = purple_savedstatus_find(title);
308 if (find && find != edit->saved)
310 purple_notify_error(edit, _("Error"), _("Duplicate title"),
311 _("Please enter a different title for the status."));
312 gnt_box_give_focus_to_child(GNT_BOX(edit->window), edit->title);
313 return;
316 if (edit->saved == NULL)
318 edit->saved = purple_savedstatus_new(title, prim);
319 purple_savedstatus_set_message(edit->saved, message);
320 set_substatuses(edit);
321 if (statuses.tree)
322 gnt_tree_add_row_last(GNT_TREE(statuses.tree), edit->saved,
323 gnt_tree_create_row(GNT_TREE(statuses.tree), title,
324 purple_primitive_get_name_from_type(prim), message), NULL);
326 else
328 purple_savedstatus_set_title(edit->saved, title);
329 purple_savedstatus_set_type(edit->saved, prim);
330 purple_savedstatus_set_message(edit->saved, message);
331 if (statuses.tree)
333 gnt_tree_change_text(GNT_TREE(statuses.tree), edit->saved, 0, title);
334 gnt_tree_change_text(GNT_TREE(statuses.tree), edit->saved, 1,
335 purple_primitive_get_name_from_type(prim));
336 gnt_tree_change_text(GNT_TREE(statuses.tree), edit->saved, 2, message);
340 if (g_object_get_data(G_OBJECT(button), "use"))
341 purple_savedstatus_activate(edit->saved);
343 gnt_widget_destroy(edit->window);
346 static void
347 add_substatus(EditStatus *edit, PurpleAccount *account)
349 char *name;
350 const char *type = NULL, *message = NULL;
351 PurpleSavedStatusSub *sub = NULL;
352 RowInfo *key;
354 if (!edit || !edit->tree)
355 return;
357 if (edit->saved)
358 sub = purple_savedstatus_get_substatus(edit->saved, account);
360 key = g_new0(RowInfo, 1);
361 key->account = account;
363 if (sub)
365 key->type = purple_savedstatus_substatus_get_type(sub);
366 type = purple_status_type_get_name(key->type);
367 message = purple_savedstatus_substatus_get_message(sub);
368 key->message = g_strdup(message);
371 name = g_strdup_printf("%s (%s)", purple_account_get_username(account),
372 purple_account_get_protocol_name(account));
373 gnt_tree_add_choice(GNT_TREE(edit->tree), key,
374 gnt_tree_create_row(GNT_TREE(edit->tree),
375 name, type ? type : "", message ? message : ""), NULL, NULL);
377 if (sub)
378 gnt_tree_set_choice(GNT_TREE(edit->tree), key, TRUE);
379 g_free(name);
382 static void
383 substatus_window_destroy_cb(GntWidget *window, EditSubStatus *sub)
385 g_hash_table_remove(sub->parent->hash, sub->key->account);
386 g_free(sub);
389 static void
390 save_substatus_cb(GntWidget *widget, EditSubStatus *sub)
392 PurpleSavedStatus *saved = sub->parent->saved;
393 RowInfo *row = sub->key;
394 const char *message;
395 PurpleStatusType *type;
397 type = gnt_combo_box_get_selected_data(GNT_COMBO_BOX(sub->type));
398 message = gnt_entry_get_text(GNT_ENTRY(sub->message));
400 row->type = type;
401 row->message = g_strdup(message);
403 if (saved) /* Save the substatus if the savedstatus actually exists. */
404 purple_savedstatus_set_substatus(saved, row->account, type, message);
406 gnt_tree_set_choice(GNT_TREE(sub->parent->tree), row, TRUE);
407 gnt_tree_change_text(GNT_TREE(sub->parent->tree), row, 1,
408 purple_status_type_get_name(type));
409 gnt_tree_change_text(GNT_TREE(sub->parent->tree), row, 2, message);
411 gnt_widget_destroy(sub->window);
414 static gboolean
415 popup_substatus(GntTree *tree, const char *key, EditStatus *edit)
417 if (key[0] == ' ' && key[1] == 0)
419 EditSubStatus *sub;
420 GntWidget *window, *combo, *entry, *box, *button, *l;
421 PurpleSavedStatusSub *substatus = NULL;
422 GList *iter;
423 char *name;
424 RowInfo *selected = gnt_tree_get_selection_data(tree);
425 PurpleAccount *account = selected->account;
427 if (gnt_tree_get_choice(tree, selected))
429 /* There was a savedstatus for this account. Now remove it. */
430 g_free(selected->message);
431 selected->type = NULL;
432 selected->message = NULL;
433 /* XXX: should we really be saving it right now? */
434 purple_savedstatus_unset_substatus(edit->saved, account);
435 gnt_tree_change_text(tree, account, 1, NULL);
436 gnt_tree_change_text(tree, account, 2, NULL);
437 return FALSE;
440 if (g_hash_table_lookup(edit->hash, account))
441 return TRUE;
443 if (edit->saved)
444 substatus = purple_savedstatus_get_substatus(edit->saved, account);
446 sub = g_new0(EditSubStatus, 1);
447 sub->parent = edit;
448 sub->key = selected;
450 sub->window = window = gnt_vbox_new(FALSE);
451 gnt_box_set_toplevel(GNT_BOX(window), TRUE);
452 gnt_box_set_title(GNT_BOX(window), _("Substatus")); /* XXX: a better title */
453 gnt_box_set_alignment(GNT_BOX(window), GNT_ALIGN_MID);
455 box = gnt_hbox_new(FALSE);
456 gnt_box_add_widget(GNT_BOX(box), gnt_label_new(_("Account:")));
457 name = g_strdup_printf("%s (%s)", purple_account_get_username(account),
458 purple_account_get_protocol_name(account));
459 gnt_box_add_widget(GNT_BOX(box), gnt_label_new(name));
460 g_free(name);
461 gnt_box_add_widget(GNT_BOX(window), box);
463 box = gnt_hbox_new(FALSE);
464 gnt_box_add_widget(GNT_BOX(box), (l = gnt_label_new(_("Status:"))));
465 gnt_widget_set_size(l, 0, 1); /* I don't like having to do this */
466 sub->type = combo = gnt_combo_box_new();
467 gnt_box_add_widget(GNT_BOX(box), combo);
468 gnt_box_add_widget(GNT_BOX(window), box);
470 for (iter = purple_account_get_status_types(account); iter; iter = iter->next)
472 PurpleStatusType *type = iter->data;
473 if (!purple_status_type_is_user_settable(type))
474 continue;
475 gnt_combo_box_add_data(GNT_COMBO_BOX(combo), type, purple_status_type_get_name(type));
478 box = gnt_hbox_new(FALSE);
479 gnt_box_add_widget(GNT_BOX(box), gnt_label_new(_("Message:")));
480 sub->message = entry = gnt_entry_new(substatus ? purple_savedstatus_substatus_get_message(substatus) : NULL);
481 gnt_box_add_widget(GNT_BOX(box), entry);
482 gnt_box_add_widget(GNT_BOX(window), box);
484 box = gnt_hbox_new(FALSE);
485 button = gnt_button_new(_("Cancel"));
486 g_signal_connect_swapped(G_OBJECT(button), "activate", G_CALLBACK(gnt_widget_destroy), window);
487 gnt_box_add_widget(GNT_BOX(box), button);
488 button = gnt_button_new(_("Save"));
489 g_signal_connect(G_OBJECT(button), "activate", G_CALLBACK(save_substatus_cb), sub);
490 gnt_box_add_widget(GNT_BOX(box), button);
491 gnt_box_add_widget(GNT_BOX(window), box);
493 gnt_widget_show(window);
495 g_hash_table_insert(edit->hash, account, sub);
497 g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(substatus_window_destroy_cb), sub);
499 return TRUE;
501 return FALSE;
504 void finch_savedstatus_edit(PurpleSavedStatus *saved)
506 EditStatus *edit;
507 GntWidget *window, *box, *button, *entry, *combo, *label, *tree;
508 PurpleStatusPrimitive prims[] = {PURPLE_STATUS_AVAILABLE, PURPLE_STATUS_AWAY,
509 PURPLE_STATUS_INVISIBLE, PURPLE_STATUS_OFFLINE, PURPLE_STATUS_UNSET}, current;
510 GList *iter;
511 int i;
513 if (saved)
515 GList *iter;
516 for (iter = edits; iter; iter = iter->next)
518 edit = iter->data;
519 if (edit->saved == saved)
520 return;
524 edit = g_new0(EditStatus, 1);
525 edit->saved = saved;
526 edit->window = window = gnt_vbox_new(FALSE);
527 gnt_box_set_toplevel(GNT_BOX(window), TRUE);
528 gnt_box_set_title(GNT_BOX(window), _("Edit Status"));
529 gnt_box_set_fill(GNT_BOX(window), TRUE);
530 gnt_box_set_alignment(GNT_BOX(window), GNT_ALIGN_MID);
531 gnt_box_set_pad(GNT_BOX(window), 0);
533 edits = g_list_append(edits, edit);
535 /* Title */
536 box = gnt_hbox_new(FALSE);
537 gnt_box_set_alignment(GNT_BOX(box), GNT_ALIGN_LEFT);
538 gnt_box_add_widget(GNT_BOX(window), box);
539 gnt_box_add_widget(GNT_BOX(box), gnt_label_new(_("Title")));
541 edit->title = entry = gnt_entry_new(saved ? purple_savedstatus_get_title(saved) : NULL);
542 gnt_box_add_widget(GNT_BOX(box), entry);
544 /* Type */
545 box = gnt_hbox_new(FALSE);
546 gnt_box_add_widget(GNT_BOX(window), box);
547 gnt_box_add_widget(GNT_BOX(box), label = gnt_label_new(_("Status")));
548 gnt_widget_set_size(label, 0, 1);
550 edit->type = combo = gnt_combo_box_new();
551 gnt_box_add_widget(GNT_BOX(box), combo);
552 current = saved ? purple_savedstatus_get_type(saved) : PURPLE_STATUS_UNSET;
553 for (i = 0; prims[i] != PURPLE_STATUS_UNSET; i++)
555 gnt_combo_box_add_data(GNT_COMBO_BOX(combo), GINT_TO_POINTER(prims[i]),
556 purple_primitive_get_name_from_type(prims[i]));
557 if (prims[i] == current)
558 gnt_combo_box_set_selected(GNT_COMBO_BOX(combo), GINT_TO_POINTER(current));
561 /* Message */
562 box = gnt_hbox_new(FALSE);
563 gnt_box_add_widget(GNT_BOX(window), box);
564 gnt_box_add_widget(GNT_BOX(box), gnt_label_new(_("Message")));
566 edit->message = entry = gnt_entry_new(saved ? purple_savedstatus_get_message(saved) : NULL);
567 gnt_box_add_widget(GNT_BOX(window), entry);
569 gnt_box_add_widget(GNT_BOX(window), gnt_hline_new());
570 gnt_box_add_widget(GNT_BOX(window), gnt_label_new(_("Use different status for following accounts")));
572 edit->hash = g_hash_table_new(g_direct_hash, g_direct_equal);
573 edit->tree = tree = gnt_tree_new_with_columns(3);
574 gnt_box_add_widget(GNT_BOX(window), tree);
575 gnt_tree_set_show_title(GNT_TREE(tree), TRUE);
576 gnt_tree_set_column_titles(GNT_TREE(tree), _("Account"), _("Status"), _("Message"));
577 gnt_tree_set_col_width(GNT_TREE(tree), 0, 30);
578 gnt_tree_set_col_width(GNT_TREE(tree), 1, 10);
579 gnt_tree_set_col_width(GNT_TREE(tree), 2, 30);
581 for (iter = purple_accounts_get_all(); iter; iter = iter->next)
583 add_substatus(edit, iter->data);
586 g_signal_connect(G_OBJECT(tree), "key_pressed", G_CALLBACK(popup_substatus), edit);
588 /* The buttons */
589 box = gnt_hbox_new(FALSE);
590 gnt_box_add_widget(GNT_BOX(window), box);
592 /* Use */
593 button = gnt_button_new(_("Use"));
594 gnt_box_add_widget(GNT_BOX(box), button);
595 g_signal_connect(G_OBJECT(button), "activate", G_CALLBACK(use_trans_status_cb), edit);
597 /* Save */
598 button = gnt_button_new(_("Save"));
599 gnt_box_add_widget(GNT_BOX(box), button);
600 g_object_set_data(G_OBJECT(button), "use", NULL);
601 g_signal_connect(G_OBJECT(button), "activate", G_CALLBACK(save_savedstatus_cb), edit);
603 /* Save & Use */
604 button = gnt_button_new(_("Save & Use"));
605 gnt_box_add_widget(GNT_BOX(box), button);
606 g_object_set_data(G_OBJECT(button), "use", GINT_TO_POINTER(TRUE));
607 g_signal_connect(G_OBJECT(button), "activate", G_CALLBACK(save_savedstatus_cb), edit);
609 /* Cancel */
610 button = gnt_button_new(_("Cancel"));
611 gnt_box_add_widget(GNT_BOX(box), button);
612 g_signal_connect_swapped(G_OBJECT(button), "activate",
613 G_CALLBACK(gnt_widget_destroy), window);
615 g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(update_edit_list), edit);
617 gnt_widget_show(window);