Mercurial > pidgin
comparison finch/gntaccount.c @ 15817: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
15816:317e7613e581 | 15817:0e3a8505ebbe |
---|---|
1 /** | |
2 * @file gntaccount.c GNT Account 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 <gntcheckbox.h> | |
29 #include <gntcombobox.h> | |
30 #include <gntentry.h> | |
31 #include <gntlabel.h> | |
32 #include <gntline.h> | |
33 #include <gnttree.h> | |
34 | |
35 #include <account.h> | |
36 #include <accountopt.h> | |
37 #include <connection.h> | |
38 #include <notify.h> | |
39 #include <plugin.h> | |
40 #include <request.h> | |
41 | |
42 #include "gntaccount.h" | |
43 #include "gntgaim.h" | |
44 | |
45 #include <string.h> | |
46 | |
47 typedef struct | |
48 { | |
49 GntWidget *window; | |
50 GntWidget *tree; | |
51 } FinchAccountList; | |
52 | |
53 static FinchAccountList accounts; | |
54 | |
55 typedef struct | |
56 { | |
57 GaimAccount *account; /* NULL for a new account */ | |
58 | |
59 GntWidget *window; | |
60 | |
61 GntWidget *protocol; | |
62 GntWidget *screenname; | |
63 GntWidget *password; | |
64 GntWidget *alias; | |
65 | |
66 GntWidget *splits; | |
67 GList *split_entries; | |
68 | |
69 GList *prpl_entries; | |
70 GntWidget *prpls; | |
71 | |
72 GntWidget *newmail; | |
73 GntWidget *remember; | |
74 } AccountEditDialog; | |
75 | |
76 /* This is necessary to close an edit-dialog when an account is deleted */ | |
77 static GList *accountdialogs; | |
78 | |
79 static void | |
80 account_add(GaimAccount *account) | |
81 { | |
82 gnt_tree_add_choice(GNT_TREE(accounts.tree), account, | |
83 gnt_tree_create_row(GNT_TREE(accounts.tree), | |
84 gaim_account_get_username(account), | |
85 gaim_account_get_protocol_name(account)), | |
86 NULL, NULL); | |
87 gnt_tree_set_choice(GNT_TREE(accounts.tree), account, | |
88 gaim_account_get_enabled(account, GAIM_GNT_UI)); | |
89 } | |
90 | |
91 static void | |
92 edit_dialog_destroy(AccountEditDialog *dialog) | |
93 { | |
94 accountdialogs = g_list_remove(accountdialogs, dialog); | |
95 g_list_free(dialog->prpl_entries); | |
96 g_list_free(dialog->split_entries); | |
97 g_free(dialog); | |
98 } | |
99 | |
100 static void | |
101 save_account_cb(AccountEditDialog *dialog) | |
102 { | |
103 GaimAccount *account; | |
104 GaimPlugin *plugin; | |
105 GaimPluginProtocolInfo *prplinfo; | |
106 const char *value; | |
107 GString *username; | |
108 | |
109 /* XXX: Do some error checking first. */ | |
110 | |
111 plugin = gnt_combo_box_get_selected_data(GNT_COMBO_BOX(dialog->protocol)); | |
112 prplinfo = GAIM_PLUGIN_PROTOCOL_INFO(plugin); | |
113 | |
114 /* Screenname && user-splits */ | |
115 value = gnt_entry_get_text(GNT_ENTRY(dialog->screenname)); | |
116 | |
117 if (value == NULL || *value == '\0') | |
118 { | |
119 gaim_notify_error(NULL, _("Error"), _("Account was not added"), | |
120 _("Screenname of an account must be non-empty.")); | |
121 return; | |
122 } | |
123 | |
124 username = g_string_new(value); | |
125 | |
126 if (prplinfo != NULL) | |
127 { | |
128 GList *iter, *entries; | |
129 for (iter = prplinfo->user_splits, entries = dialog->split_entries; | |
130 iter && entries; iter = iter->next, entries = entries->next) | |
131 { | |
132 GaimAccountUserSplit *split = iter->data; | |
133 GntWidget *entry = entries->data; | |
134 | |
135 value = gnt_entry_get_text(GNT_ENTRY(entry)); | |
136 if (value == NULL || *value == '\0') | |
137 value = gaim_account_user_split_get_default_value(split); | |
138 g_string_append_printf(username, "%c%s", | |
139 gaim_account_user_split_get_separator(split), | |
140 value); | |
141 } | |
142 } | |
143 | |
144 if (dialog->account == NULL) | |
145 { | |
146 account = gaim_account_new(username->str, gaim_plugin_get_id(plugin)); | |
147 gaim_accounts_add(account); | |
148 } | |
149 else | |
150 { | |
151 account = dialog->account; | |
152 | |
153 /* Protocol */ | |
154 gaim_account_set_protocol_id(account, gaim_plugin_get_id(plugin)); | |
155 gaim_account_set_username(account, username->str); | |
156 } | |
157 g_string_free(username, TRUE); | |
158 | |
159 /* Alias */ | |
160 value = gnt_entry_get_text(GNT_ENTRY(dialog->alias)); | |
161 if (value && *value) | |
162 gaim_account_set_alias(account, value); | |
163 | |
164 /* Remember password and password */ | |
165 gaim_account_set_remember_password(account, | |
166 gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->remember))); | |
167 value = gnt_entry_get_text(GNT_ENTRY(dialog->password)); | |
168 if (value && *value && gaim_account_get_remember_password(account)) | |
169 gaim_account_set_password(account, value); | |
170 else | |
171 gaim_account_set_password(account, NULL); | |
172 | |
173 /* Mail notification */ | |
174 gaim_account_set_check_mail(account, | |
175 gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->newmail))); | |
176 | |
177 /* Protocol options */ | |
178 if (prplinfo) | |
179 { | |
180 GList *iter, *entries; | |
181 | |
182 for (iter = prplinfo->protocol_options, entries = dialog->prpl_entries; | |
183 iter && entries; iter = iter->next, entries = entries->next) | |
184 { | |
185 GaimAccountOption *option = iter->data; | |
186 GntWidget *entry = entries->data; | |
187 GaimPrefType type = gaim_account_option_get_type(option); | |
188 const char *setting = gaim_account_option_get_setting(option); | |
189 | |
190 if (type == GAIM_PREF_STRING) | |
191 { | |
192 const char *value = gnt_entry_get_text(GNT_ENTRY(entry)); | |
193 gaim_account_set_string(account, setting, value); | |
194 } | |
195 else if (type == GAIM_PREF_INT) | |
196 { | |
197 const char *str = gnt_entry_get_text(GNT_ENTRY(entry)); | |
198 int value = 0; | |
199 if (str) | |
200 value = atoi(str); | |
201 gaim_account_set_int(account, setting, value); | |
202 } | |
203 else if (type == GAIM_PREF_BOOLEAN) | |
204 { | |
205 gboolean value = gnt_check_box_get_checked(GNT_CHECK_BOX(entry)); | |
206 gaim_account_set_bool(account, setting, value); | |
207 } | |
208 else if (type == GAIM_PREF_STRING_LIST) | |
209 { | |
210 /* TODO: */ | |
211 } | |
212 else | |
213 { | |
214 g_assert_not_reached(); | |
215 } | |
216 } | |
217 } | |
218 | |
219 /* XXX: Proxy options */ | |
220 | |
221 gnt_widget_destroy(dialog->window); | |
222 } | |
223 | |
224 static void | |
225 update_user_splits(AccountEditDialog *dialog) | |
226 { | |
227 GntWidget *hbox; | |
228 GaimPlugin *plugin; | |
229 GaimPluginProtocolInfo *prplinfo; | |
230 GList *iter, *entries; | |
231 char *username = NULL; | |
232 | |
233 if (dialog->splits) | |
234 { | |
235 gnt_box_remove_all(GNT_BOX(dialog->splits)); | |
236 g_list_free(dialog->split_entries); | |
237 } | |
238 else | |
239 { | |
240 dialog->splits = gnt_vbox_new(FALSE); | |
241 gnt_box_set_pad(GNT_BOX(dialog->splits), 0); | |
242 gnt_box_set_fill(GNT_BOX(dialog->splits), TRUE); | |
243 } | |
244 | |
245 dialog->split_entries = NULL; | |
246 | |
247 plugin = gnt_combo_box_get_selected_data(GNT_COMBO_BOX(dialog->protocol)); | |
248 if (!plugin) | |
249 return; | |
250 prplinfo = GAIM_PLUGIN_PROTOCOL_INFO(plugin); | |
251 | |
252 username = dialog->account ? g_strdup(gaim_account_get_username(dialog->account)) : NULL; | |
253 | |
254 for (iter = prplinfo->user_splits; iter; iter = iter->next) | |
255 { | |
256 GaimAccountUserSplit *split = iter->data; | |
257 GntWidget *entry; | |
258 char *buf; | |
259 | |
260 hbox = gnt_hbox_new(TRUE); | |
261 gnt_box_add_widget(GNT_BOX(dialog->splits), hbox); | |
262 | |
263 buf = g_strdup_printf("%s:", gaim_account_user_split_get_text(split)); | |
264 gnt_box_add_widget(GNT_BOX(hbox), gnt_label_new(buf)); | |
265 | |
266 entry = gnt_entry_new(NULL); | |
267 gnt_box_add_widget(GNT_BOX(hbox), entry); | |
268 | |
269 dialog->split_entries = g_list_append(dialog->split_entries, entry); | |
270 g_free(buf); | |
271 } | |
272 | |
273 for (iter = g_list_last(prplinfo->user_splits), entries = g_list_last(dialog->split_entries); | |
274 iter && entries; iter = iter->prev, entries = entries->prev) | |
275 { | |
276 GntWidget *entry = entries->data; | |
277 GaimAccountUserSplit *split = iter->data; | |
278 const char *value = NULL; | |
279 char *s; | |
280 | |
281 if (dialog->account) | |
282 { | |
283 s = strrchr(username, gaim_account_user_split_get_separator(split)); | |
284 if (s != NULL) | |
285 { | |
286 *s = '\0'; | |
287 s++; | |
288 value = s; | |
289 } | |
290 } | |
291 if (value == NULL) | |
292 value = gaim_account_user_split_get_default_value(split); | |
293 | |
294 if (value != NULL) | |
295 gnt_entry_set_text(GNT_ENTRY(entry), value); | |
296 } | |
297 | |
298 if (username != NULL) | |
299 gnt_entry_set_text(GNT_ENTRY(dialog->screenname), username); | |
300 | |
301 g_free(username); | |
302 } | |
303 | |
304 static void | |
305 add_protocol_options(AccountEditDialog *dialog) | |
306 { | |
307 GaimPlugin *plugin; | |
308 GaimPluginProtocolInfo *prplinfo; | |
309 GList *iter; | |
310 GntWidget *vbox, *box; | |
311 GaimAccount *account; | |
312 | |
313 if (dialog->prpls) | |
314 gnt_box_remove_all(GNT_BOX(dialog->prpls)); | |
315 else | |
316 { | |
317 dialog->prpls = vbox = gnt_vbox_new(FALSE); | |
318 gnt_box_set_pad(GNT_BOX(vbox), 0); | |
319 gnt_box_set_alignment(GNT_BOX(vbox), GNT_ALIGN_LEFT); | |
320 gnt_box_set_fill(GNT_BOX(vbox), TRUE); | |
321 } | |
322 | |
323 if (dialog->prpl_entries) | |
324 { | |
325 g_list_free(dialog->prpl_entries); | |
326 dialog->prpl_entries = NULL; | |
327 } | |
328 | |
329 vbox = dialog->prpls; | |
330 | |
331 plugin = gnt_combo_box_get_selected_data(GNT_COMBO_BOX(dialog->protocol)); | |
332 if (!plugin) | |
333 return; | |
334 | |
335 prplinfo = GAIM_PLUGIN_PROTOCOL_INFO(plugin); | |
336 | |
337 account = dialog->account; | |
338 | |
339 for (iter = prplinfo->protocol_options; iter; iter = iter->next) | |
340 { | |
341 GaimAccountOption *option = iter->data; | |
342 GaimPrefType type = gaim_account_option_get_type(option); | |
343 | |
344 box = gnt_hbox_new(TRUE); | |
345 gnt_box_set_pad(GNT_BOX(box), 0); | |
346 gnt_box_add_widget(GNT_BOX(vbox), box); | |
347 | |
348 if (type == GAIM_PREF_BOOLEAN) | |
349 { | |
350 GntWidget *widget = gnt_check_box_new(gaim_account_option_get_text(option)); | |
351 gnt_box_add_widget(GNT_BOX(box), widget); | |
352 dialog->prpl_entries = g_list_append(dialog->prpl_entries, widget); | |
353 | |
354 if (account) | |
355 gnt_check_box_set_checked(GNT_CHECK_BOX(widget), | |
356 gaim_account_get_bool(account, | |
357 gaim_account_option_get_setting(option), | |
358 gaim_account_option_get_default_bool(option))); | |
359 else | |
360 gnt_check_box_set_checked(GNT_CHECK_BOX(widget), | |
361 gaim_account_option_get_default_bool(option)); | |
362 } | |
363 else | |
364 { | |
365 gnt_box_add_widget(GNT_BOX(box), | |
366 gnt_label_new(gaim_account_option_get_text(option))); | |
367 | |
368 if (type == GAIM_PREF_STRING_LIST) | |
369 { | |
370 /* TODO: Use a combobox */ | |
371 /* Don't forget to append the widget to prpl_entries */ | |
372 } | |
373 else | |
374 { | |
375 GntWidget *entry = gnt_entry_new(NULL); | |
376 gnt_box_add_widget(GNT_BOX(box), entry); | |
377 dialog->prpl_entries = g_list_append(dialog->prpl_entries, entry); | |
378 | |
379 if (type == GAIM_PREF_STRING) | |
380 { | |
381 const char *dv = gaim_account_option_get_default_string(option); | |
382 | |
383 if (account) | |
384 gnt_entry_set_text(GNT_ENTRY(entry), | |
385 gaim_account_get_string(account, | |
386 gaim_account_option_get_setting(option), dv)); | |
387 else | |
388 gnt_entry_set_text(GNT_ENTRY(entry), dv); | |
389 } | |
390 else if (type == GAIM_PREF_INT) | |
391 { | |
392 char str[32]; | |
393 int value = gaim_account_option_get_default_int(option); | |
394 if (account) | |
395 value = gaim_account_get_int(account, | |
396 gaim_account_option_get_setting(option), value); | |
397 snprintf(str, sizeof(str), "%d", value); | |
398 gnt_entry_set_flag(GNT_ENTRY(entry), GNT_ENTRY_FLAG_INT); | |
399 gnt_entry_set_text(GNT_ENTRY(entry), str); | |
400 } | |
401 else | |
402 { | |
403 g_assert_not_reached(); | |
404 } | |
405 } | |
406 } | |
407 } | |
408 } | |
409 | |
410 static void | |
411 update_user_options(AccountEditDialog *dialog) | |
412 { | |
413 GaimPlugin *plugin; | |
414 GaimPluginProtocolInfo *prplinfo; | |
415 | |
416 plugin = gnt_combo_box_get_selected_data(GNT_COMBO_BOX(dialog->protocol)); | |
417 if (!plugin) | |
418 return; | |
419 | |
420 prplinfo = GAIM_PLUGIN_PROTOCOL_INFO(plugin); | |
421 | |
422 if (dialog->newmail == NULL) | |
423 dialog->newmail = gnt_check_box_new(_("New mail notifications")); | |
424 if (dialog->account) | |
425 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->newmail), | |
426 gaim_account_get_check_mail(dialog->account)); | |
427 if (!prplinfo || !(prplinfo->options & OPT_PROTO_MAIL_CHECK)) | |
428 gnt_widget_set_visible(dialog->newmail, FALSE); | |
429 else | |
430 gnt_widget_set_visible(dialog->newmail, TRUE); | |
431 | |
432 if (dialog->remember == NULL) | |
433 dialog->remember = gnt_check_box_new(_("Remember password")); | |
434 if (dialog->account) | |
435 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->remember), | |
436 gaim_account_get_remember_password(dialog->account)); | |
437 } | |
438 | |
439 static void | |
440 prpl_changed_cb(GntWidget *combo, GaimPlugin *old, GaimPlugin *new, AccountEditDialog *dialog) | |
441 { | |
442 update_user_splits(dialog); | |
443 add_protocol_options(dialog); | |
444 update_user_options(dialog); /* This may not be necessary here */ | |
445 gnt_box_readjust(GNT_BOX(dialog->window)); | |
446 gnt_widget_draw(dialog->window); | |
447 } | |
448 | |
449 static void | |
450 edit_account(GaimAccount *account) | |
451 { | |
452 GntWidget *window, *hbox; | |
453 GntWidget *combo, *button, *entry; | |
454 GList *list, *iter; | |
455 AccountEditDialog *dialog; | |
456 | |
457 if (account) | |
458 { | |
459 GList *iter; | |
460 for (iter = accountdialogs; iter; iter = iter->next) | |
461 { | |
462 AccountEditDialog *dlg = iter->data; | |
463 if (dlg->account == account) | |
464 return; | |
465 } | |
466 } | |
467 | |
468 dialog = g_new0(AccountEditDialog, 1); | |
469 accountdialogs = g_list_prepend(accountdialogs, dialog); | |
470 | |
471 dialog->window = window = gnt_vbox_new(FALSE); | |
472 dialog->account = account; | |
473 gnt_box_set_toplevel(GNT_BOX(window), TRUE); | |
474 gnt_box_set_title(GNT_BOX(window), account ? _("Modify Account") : _("New Account")); | |
475 gnt_box_set_alignment(GNT_BOX(window), GNT_ALIGN_MID); | |
476 gnt_box_set_pad(GNT_BOX(window), 0); | |
477 gnt_widget_set_name(window, "edit-account"); | |
478 gnt_box_set_fill(GNT_BOX(window), TRUE); | |
479 | |
480 hbox = gnt_hbox_new(TRUE); | |
481 gnt_box_set_pad(GNT_BOX(hbox), 0); | |
482 gnt_box_add_widget(GNT_BOX(window), hbox); | |
483 | |
484 dialog->protocol = combo = gnt_combo_box_new(); | |
485 list = gaim_plugins_get_protocols(); | |
486 for (iter = list; iter; iter = iter->next) | |
487 { | |
488 gnt_combo_box_add_data(GNT_COMBO_BOX(combo), iter->data, | |
489 ((GaimPlugin*)iter->data)->info->name); | |
490 } | |
491 | |
492 if (account) | |
493 gnt_combo_box_set_selected(GNT_COMBO_BOX(combo), | |
494 gaim_plugins_find_with_id(gaim_account_get_protocol_id(account))); | |
495 else | |
496 gnt_combo_box_set_selected(GNT_COMBO_BOX(combo), list->data); | |
497 | |
498 g_signal_connect(G_OBJECT(combo), "selection-changed", G_CALLBACK(prpl_changed_cb), dialog); | |
499 | |
500 gnt_box_add_widget(GNT_BOX(hbox), gnt_label_new(_("Protocol:"))); | |
501 gnt_box_add_widget(GNT_BOX(hbox), combo); | |
502 | |
503 hbox = gnt_hbox_new(TRUE); | |
504 gnt_box_set_pad(GNT_BOX(hbox), 0); | |
505 gnt_box_add_widget(GNT_BOX(window), hbox); | |
506 | |
507 dialog->screenname = entry = gnt_entry_new(NULL); | |
508 gnt_box_add_widget(GNT_BOX(hbox), gnt_label_new(_("Screen name:"))); | |
509 gnt_box_add_widget(GNT_BOX(hbox), entry); | |
510 | |
511 /* User splits */ | |
512 update_user_splits(dialog); | |
513 gnt_box_add_widget(GNT_BOX(window), dialog->splits); | |
514 | |
515 hbox = gnt_hbox_new(TRUE); | |
516 gnt_box_set_pad(GNT_BOX(hbox), 0); | |
517 gnt_box_add_widget(GNT_BOX(window), hbox); | |
518 | |
519 dialog->password = entry = gnt_entry_new(NULL); | |
520 gnt_entry_set_masked(GNT_ENTRY(entry), TRUE); | |
521 gnt_box_add_widget(GNT_BOX(hbox), gnt_label_new(_("Password:"))); | |
522 gnt_box_add_widget(GNT_BOX(hbox), entry); | |
523 if (account) | |
524 gnt_entry_set_text(GNT_ENTRY(entry), gaim_account_get_password(account)); | |
525 | |
526 hbox = gnt_hbox_new(TRUE); | |
527 gnt_box_set_pad(GNT_BOX(hbox), 0); | |
528 gnt_box_add_widget(GNT_BOX(window), hbox); | |
529 | |
530 dialog->alias = entry = gnt_entry_new(NULL); | |
531 gnt_box_add_widget(GNT_BOX(hbox), gnt_label_new(_("Alias:"))); | |
532 gnt_box_add_widget(GNT_BOX(hbox), entry); | |
533 if (account) | |
534 gnt_entry_set_text(GNT_ENTRY(entry), gaim_account_get_alias(account)); | |
535 | |
536 /* User options */ | |
537 update_user_options(dialog); | |
538 gnt_box_add_widget(GNT_BOX(window), dialog->remember); | |
539 gnt_box_add_widget(GNT_BOX(window), dialog->newmail); | |
540 | |
541 gnt_box_add_widget(GNT_BOX(window), gnt_line_new(FALSE)); | |
542 | |
543 /* The advanced box */ | |
544 add_protocol_options(dialog); | |
545 gnt_box_add_widget(GNT_BOX(window), dialog->prpls); | |
546 | |
547 /* TODO: Add proxy options */ | |
548 | |
549 /* The button box */ | |
550 hbox = gnt_hbox_new(FALSE); | |
551 gnt_box_add_widget(GNT_BOX(window), hbox); | |
552 gnt_box_set_alignment(GNT_BOX(hbox), GNT_ALIGN_MID); | |
553 | |
554 button = gnt_button_new(_("Cancel")); | |
555 gnt_box_add_widget(GNT_BOX(hbox), button); | |
556 g_signal_connect_swapped(G_OBJECT(button), "activate", G_CALLBACK(gnt_widget_destroy), window); | |
557 | |
558 button = gnt_button_new(_("Save")); | |
559 gnt_box_add_widget(GNT_BOX(hbox), button); | |
560 g_signal_connect_swapped(G_OBJECT(button), "activate", G_CALLBACK(save_account_cb), dialog); | |
561 | |
562 g_signal_connect_swapped(G_OBJECT(window), "destroy", G_CALLBACK(edit_dialog_destroy), dialog); | |
563 | |
564 gnt_widget_show(window); | |
565 gnt_box_readjust(GNT_BOX(window)); | |
566 gnt_widget_draw(window); | |
567 } | |
568 | |
569 static void | |
570 add_account_cb(GntWidget *widget, gpointer null) | |
571 { | |
572 edit_account(NULL); | |
573 } | |
574 | |
575 static void | |
576 modify_account_cb(GntWidget *widget, GntTree *tree) | |
577 { | |
578 GaimAccount *account = gnt_tree_get_selection_data(tree); | |
579 if (!account) | |
580 return; | |
581 edit_account(account); | |
582 } | |
583 | |
584 static void | |
585 really_delete_account(GaimAccount *account) | |
586 { | |
587 GList *iter; | |
588 for (iter = accountdialogs; iter; iter = iter->next) | |
589 { | |
590 AccountEditDialog *dlg = iter->data; | |
591 if (dlg->account == account) | |
592 { | |
593 gnt_widget_destroy(dlg->window); | |
594 break; | |
595 } | |
596 } | |
597 gaim_request_close_with_handle(account); /* Close any other opened delete window */ | |
598 gaim_accounts_delete(account); | |
599 } | |
600 | |
601 static void | |
602 delete_account_cb(GntWidget *widget, GntTree *tree) | |
603 { | |
604 GaimAccount *account; | |
605 char *prompt; | |
606 | |
607 account = gnt_tree_get_selection_data(tree); | |
608 if (!account) | |
609 return; | |
610 | |
611 prompt = g_strdup_printf(_("Are you sure you want to delete %s?"), | |
612 gaim_account_get_username(account)); | |
613 | |
614 gaim_request_action(account, _("Delete Account"), prompt, NULL, 0, account, 2, | |
615 _("Delete"), really_delete_account, _("Cancel"), NULL); | |
616 g_free(prompt); | |
617 } | |
618 | |
619 static void | |
620 account_toggled(GntWidget *widget, void *key, gpointer null) | |
621 { | |
622 GaimAccount *account = key; | |
623 | |
624 gaim_account_set_enabled(account, GAIM_GNT_UI, gnt_tree_get_choice(GNT_TREE(widget), key)); | |
625 } | |
626 | |
627 static void | |
628 reset_accounts_win(GntWidget *widget, gpointer null) | |
629 { | |
630 accounts.window = NULL; | |
631 accounts.tree = NULL; | |
632 } | |
633 | |
634 void finch_accounts_show_all() | |
635 { | |
636 GList *iter; | |
637 GntWidget *box, *button; | |
638 | |
639 if (accounts.window) | |
640 return; | |
641 | |
642 accounts.window = gnt_vbox_new(FALSE); | |
643 gnt_box_set_toplevel(GNT_BOX(accounts.window), TRUE); | |
644 gnt_box_set_title(GNT_BOX(accounts.window), _("Accounts")); | |
645 gnt_box_set_pad(GNT_BOX(accounts.window), 0); | |
646 gnt_box_set_alignment(GNT_BOX(accounts.window), GNT_ALIGN_MID); | |
647 gnt_widget_set_name(accounts.window, "accounts"); | |
648 | |
649 gnt_box_add_widget(GNT_BOX(accounts.window), | |
650 gnt_label_new(_("You can enable/disable accounts from the following list."))); | |
651 | |
652 gnt_box_add_widget(GNT_BOX(accounts.window), gnt_line_new(FALSE)); | |
653 | |
654 accounts.tree = gnt_tree_new_with_columns(2); | |
655 GNT_WIDGET_SET_FLAGS(accounts.tree, GNT_WIDGET_NO_BORDER); | |
656 | |
657 for (iter = gaim_accounts_get_all(); iter; iter = iter->next) | |
658 { | |
659 GaimAccount *account = iter->data; | |
660 account_add(account); | |
661 } | |
662 | |
663 g_signal_connect(G_OBJECT(accounts.tree), "toggled", G_CALLBACK(account_toggled), NULL); | |
664 | |
665 gnt_tree_set_col_width(GNT_TREE(accounts.tree), 0, 40); | |
666 gnt_tree_set_col_width(GNT_TREE(accounts.tree), 1, 10); | |
667 gnt_box_add_widget(GNT_BOX(accounts.window), accounts.tree); | |
668 | |
669 gnt_box_add_widget(GNT_BOX(accounts.window), gnt_line_new(FALSE)); | |
670 | |
671 box = gnt_hbox_new(FALSE); | |
672 | |
673 button = gnt_button_new(_("Add")); | |
674 gnt_box_add_widget(GNT_BOX(box), button); | |
675 g_signal_connect(G_OBJECT(button), "activate", G_CALLBACK(add_account_cb), NULL); | |
676 | |
677 button = gnt_button_new(_("Modify")); | |
678 gnt_box_add_widget(GNT_BOX(box), button); | |
679 g_signal_connect(G_OBJECT(button), "activate", G_CALLBACK(modify_account_cb), accounts.tree); | |
680 | |
681 button = gnt_button_new(_("Delete")); | |
682 gnt_box_add_widget(GNT_BOX(box), button); | |
683 g_signal_connect(G_OBJECT(button), "activate", G_CALLBACK(delete_account_cb), accounts.tree); | |
684 | |
685 gnt_box_add_widget(GNT_BOX(accounts.window), box); | |
686 | |
687 g_signal_connect(G_OBJECT(accounts.window), "destroy", G_CALLBACK(reset_accounts_win), NULL); | |
688 | |
689 gnt_widget_show(accounts.window); | |
690 } | |
691 | |
692 static gpointer | |
693 finch_accounts_get_handle() | |
694 { | |
695 static int handle; | |
696 | |
697 return &handle; | |
698 } | |
699 | |
700 static void | |
701 account_added_callback(GaimAccount *account) | |
702 { | |
703 if (accounts.window == NULL) | |
704 return; | |
705 account_add(account); | |
706 gnt_widget_draw(accounts.tree); | |
707 } | |
708 | |
709 static void | |
710 account_removed_callback(GaimAccount *account) | |
711 { | |
712 if (accounts.window == NULL) | |
713 return; | |
714 | |
715 gnt_tree_remove(GNT_TREE(accounts.tree), account); | |
716 } | |
717 | |
718 void finch_accounts_init() | |
719 { | |
720 GList *iter; | |
721 | |
722 gaim_signal_connect(gaim_accounts_get_handle(), "account-added", | |
723 finch_accounts_get_handle(), GAIM_CALLBACK(account_added_callback), | |
724 NULL); | |
725 gaim_signal_connect(gaim_accounts_get_handle(), "account-removed", | |
726 finch_accounts_get_handle(), GAIM_CALLBACK(account_removed_callback), | |
727 NULL); | |
728 | |
729 for (iter = gaim_accounts_get_all(); iter; iter = iter->next) { | |
730 if (gaim_account_get_enabled(iter->data, GAIM_GNT_UI)) | |
731 break; | |
732 } | |
733 if (!iter) | |
734 finch_accounts_show_all(); | |
735 } | |
736 | |
737 void finch_accounts_uninit() | |
738 { | |
739 if (accounts.window) | |
740 gnt_widget_destroy(accounts.window); | |
741 } | |
742 | |
743 /* The following uiops stuff are copied from gtkaccount.c */ | |
744 typedef struct | |
745 { | |
746 GaimAccount *account; | |
747 char *username; | |
748 char *alias; | |
749 } AddUserData; | |
750 | |
751 static char * | |
752 make_info(GaimAccount *account, GaimConnection *gc, const char *remote_user, | |
753 const char *id, const char *alias, const char *msg) | |
754 { | |
755 if (msg != NULL && *msg == '\0') | |
756 msg = NULL; | |
757 | |
758 return g_strdup_printf(_("%s%s%s%s has made %s his or her buddy%s%s"), | |
759 remote_user, | |
760 (alias != NULL ? " (" : ""), | |
761 (alias != NULL ? alias : ""), | |
762 (alias != NULL ? ")" : ""), | |
763 (id != NULL | |
764 ? id | |
765 : (gaim_connection_get_display_name(gc) != NULL | |
766 ? gaim_connection_get_display_name(gc) | |
767 : gaim_account_get_username(account))), | |
768 (msg != NULL ? ": " : "."), | |
769 (msg != NULL ? msg : "")); | |
770 } | |
771 | |
772 static void | |
773 notify_added(GaimAccount *account, const char *remote_user, | |
774 const char *id, const char *alias, | |
775 const char *msg) | |
776 { | |
777 char *buffer; | |
778 GaimConnection *gc; | |
779 | |
780 gc = gaim_account_get_connection(account); | |
781 | |
782 buffer = make_info(account, gc, remote_user, id, alias, msg); | |
783 | |
784 gaim_notify_info(NULL, NULL, buffer, NULL); | |
785 | |
786 g_free(buffer); | |
787 } | |
788 | |
789 static void | |
790 free_add_user_data(AddUserData *data) | |
791 { | |
792 g_free(data->username); | |
793 | |
794 if (data->alias != NULL) | |
795 g_free(data->alias); | |
796 | |
797 g_free(data); | |
798 } | |
799 | |
800 static void | |
801 add_user_cb(AddUserData *data) | |
802 { | |
803 GaimConnection *gc = gaim_account_get_connection(data->account); | |
804 | |
805 if (g_list_find(gaim_connections_get_all(), gc)) | |
806 { | |
807 gaim_blist_request_add_buddy(data->account, data->username, | |
808 NULL, data->alias); | |
809 } | |
810 | |
811 free_add_user_data(data); | |
812 } | |
813 | |
814 static void | |
815 request_add(GaimAccount *account, const char *remote_user, | |
816 const char *id, const char *alias, | |
817 const char *msg) | |
818 { | |
819 char *buffer; | |
820 GaimConnection *gc; | |
821 AddUserData *data; | |
822 | |
823 gc = gaim_account_get_connection(account); | |
824 | |
825 data = g_new0(AddUserData, 1); | |
826 data->account = account; | |
827 data->username = g_strdup(remote_user); | |
828 data->alias = (alias != NULL ? g_strdup(alias) : NULL); | |
829 | |
830 buffer = make_info(account, gc, remote_user, id, alias, msg); | |
831 gaim_request_action(NULL, NULL, _("Add buddy to your list?"), | |
832 buffer, GAIM_DEFAULT_ACTION_NONE, data, 2, | |
833 _("Add"), G_CALLBACK(add_user_cb), | |
834 _("Cancel"), G_CALLBACK(free_add_user_data)); | |
835 g_free(buffer); | |
836 } | |
837 | |
838 /* Copied from gtkaccount.c */ | |
839 typedef struct { | |
840 GaimAccountRequestAuthorizationCb auth_cb; | |
841 GaimAccountRequestAuthorizationCb deny_cb; | |
842 void *data; | |
843 char *username; | |
844 char *alias; | |
845 GaimAccount *account; | |
846 } auth_and_add; | |
847 | |
848 static void | |
849 authorize_and_add_cb(auth_and_add *aa) | |
850 { | |
851 aa->auth_cb(aa->data); | |
852 gaim_blist_request_add_buddy(aa->account, aa->username, | |
853 NULL, aa->alias); | |
854 | |
855 g_free(aa->username); | |
856 g_free(aa->alias); | |
857 g_free(aa); | |
858 } | |
859 | |
860 static void | |
861 deny_no_add_cb(auth_and_add *aa) | |
862 { | |
863 aa->deny_cb(aa->data); | |
864 | |
865 g_free(aa->username); | |
866 g_free(aa->alias); | |
867 g_free(aa); | |
868 } | |
869 | |
870 static void * | |
871 finch_request_authorize(GaimAccount *account, const char *remote_user, | |
872 const char *id, const char *alias, const char *message, gboolean on_list, | |
873 GCallback auth_cb, GCallback deny_cb, void *user_data) | |
874 { | |
875 char *buffer; | |
876 GaimConnection *gc; | |
877 void *uihandle; | |
878 | |
879 gc = gaim_account_get_connection(account); | |
880 if (message != NULL && *message == '\0') | |
881 message = NULL; | |
882 | |
883 buffer = g_strdup_printf(_("%s%s%s%s wants to add %s to his or her buddy list%s%s"), | |
884 remote_user, | |
885 (alias != NULL ? " (" : ""), | |
886 (alias != NULL ? alias : ""), | |
887 (alias != NULL ? ")" : ""), | |
888 (id != NULL | |
889 ? id | |
890 : (gaim_connection_get_display_name(gc) != NULL | |
891 ? gaim_connection_get_display_name(gc) | |
892 : gaim_account_get_username(account))), | |
893 (message != NULL ? ": " : "."), | |
894 (message != NULL ? message : "")); | |
895 if (!on_list) { | |
896 auth_and_add *aa = g_new(auth_and_add, 1); | |
897 aa->auth_cb = (GaimAccountRequestAuthorizationCb)auth_cb; | |
898 aa->deny_cb = (GaimAccountRequestAuthorizationCb)deny_cb; | |
899 aa->data = user_data; | |
900 aa->username = g_strdup(remote_user); | |
901 aa->alias = g_strdup(alias); | |
902 aa->account = account; | |
903 uihandle = gaim_request_action(NULL, _("Authorize buddy?"), buffer, NULL, | |
904 GAIM_DEFAULT_ACTION_NONE, aa, 2, | |
905 _("Authorize"), authorize_and_add_cb, | |
906 _("Deny"), deny_no_add_cb); | |
907 } else { | |
908 uihandle = gaim_request_action(NULL, _("Authorize buddy?"), buffer, NULL, | |
909 GAIM_DEFAULT_ACTION_NONE, user_data, 2, | |
910 _("Authorize"), auth_cb, | |
911 _("Deny"), deny_cb); | |
912 } | |
913 g_free(buffer); | |
914 return uihandle; | |
915 } | |
916 | |
917 static void | |
918 finch_request_close(void *uihandle) | |
919 { | |
920 gaim_request_close(GAIM_REQUEST_ACTION, uihandle); | |
921 } | |
922 | |
923 static GaimAccountUiOps ui_ops = | |
924 { | |
925 .notify_added = notify_added, | |
926 .status_changed = NULL, | |
927 .request_add = request_add, | |
928 .request_authorize = finch_request_authorize, | |
929 .close_account_request = finch_request_close | |
930 }; | |
931 | |
932 GaimAccountUiOps *finch_accounts_get_ui_ops() | |
933 { | |
934 return &ui_ops; | |
935 } | |
936 |