Mercurial > pidgin.yaz
annotate libgaim/account.c @ 14694:8538993dbb00
[gaim-migrate @ 17446]
Patch #1571853 from Richard Nelson (wabz):
"This patch removes the contact and all buddies when
removing a contact, rather than just removing it from
the blist (and having all the buddies show up next time
gaim-text is opened)"
committer: Tailor Script <tailor@pidgin.im>
author | Sadrul Habib Chowdhury <imadil@gmail.com> |
---|---|
date | Sun, 08 Oct 2006 04:21:20 +0000 |
parents | 61a4a2dc491e |
children | 34083fe39891 |
rev | line source |
---|---|
14192 | 1 /** |
2 * @file account.c Account API | |
3 * @ingroup core | |
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 "internal.h" | |
26 #include "account.h" | |
27 #include "core.h" | |
28 #include "dbus-maybe.h" | |
29 #include "debug.h" | |
30 #include "notify.h" | |
31 #include "pounce.h" | |
32 #include "prefs.h" | |
33 #include "privacy.h" | |
34 #include "prpl.h" | |
35 #include "request.h" | |
36 #include "server.h" | |
37 #include "signals.h" | |
38 #include "status.h" | |
39 #include "util.h" | |
40 #include "xmlnode.h" | |
41 | |
42 /* TODO: Should use GaimValue instead of this? What about "ui"? */ | |
43 typedef struct | |
44 { | |
45 GaimPrefType type; | |
46 | |
47 char *ui; | |
48 | |
49 union | |
50 { | |
51 int integer; | |
52 char *string; | |
53 gboolean bool; | |
54 | |
55 } value; | |
56 | |
57 } GaimAccountSetting; | |
58 | |
59 | |
60 static GaimAccountUiOps *account_ui_ops = NULL; | |
61 | |
62 static GList *accounts = NULL; | |
63 static guint save_timer = 0; | |
64 static gboolean accounts_loaded = FALSE; | |
65 | |
66 | |
67 /********************************************************************* | |
68 * Writing to disk * | |
69 *********************************************************************/ | |
70 | |
71 static void | |
72 setting_to_xmlnode(gpointer key, gpointer value, gpointer user_data) | |
73 { | |
74 const char *name; | |
75 GaimAccountSetting *setting; | |
76 xmlnode *node, *child; | |
77 char buf[20]; | |
78 | |
79 name = (const char *)key; | |
80 setting = (GaimAccountSetting *)value; | |
81 node = (xmlnode *)user_data; | |
82 | |
83 child = xmlnode_new_child(node, "setting"); | |
84 xmlnode_set_attrib(child, "name", name); | |
85 | |
86 if (setting->type == GAIM_PREF_INT) { | |
87 xmlnode_set_attrib(child, "type", "int"); | |
88 snprintf(buf, sizeof(buf), "%d", setting->value.integer); | |
89 xmlnode_insert_data(child, buf, -1); | |
90 } | |
91 else if (setting->type == GAIM_PREF_STRING && setting->value.string != NULL) { | |
92 xmlnode_set_attrib(child, "type", "string"); | |
93 xmlnode_insert_data(child, setting->value.string, -1); | |
94 } | |
95 else if (setting->type == GAIM_PREF_BOOLEAN) { | |
96 xmlnode_set_attrib(child, "type", "bool"); | |
97 snprintf(buf, sizeof(buf), "%d", setting->value.bool); | |
98 xmlnode_insert_data(child, buf, -1); | |
99 } | |
100 } | |
101 | |
102 static void | |
103 ui_setting_to_xmlnode(gpointer key, gpointer value, gpointer user_data) | |
104 { | |
105 const char *ui; | |
106 GHashTable *table; | |
107 xmlnode *node, *child; | |
108 | |
109 ui = (const char *)key; | |
110 table = (GHashTable *)value; | |
111 node = (xmlnode *)user_data; | |
112 | |
113 if (g_hash_table_size(table) > 0) | |
114 { | |
115 child = xmlnode_new_child(node, "settings"); | |
116 xmlnode_set_attrib(child, "ui", ui); | |
117 g_hash_table_foreach(table, setting_to_xmlnode, child); | |
118 } | |
119 } | |
120 | |
121 static xmlnode * | |
122 status_attr_to_xmlnode(const GaimStatus *status, const GaimStatusType *type, const GaimStatusAttr *attr) | |
123 { | |
124 xmlnode *node; | |
125 const char *id; | |
126 char *value = NULL; | |
127 GaimStatusAttr *default_attr; | |
128 GaimValue *default_value; | |
129 GaimType attr_type; | |
130 GaimValue *attr_value; | |
131 | |
132 id = gaim_status_attr_get_id(attr); | |
133 g_return_val_if_fail(id, NULL); | |
134 | |
135 attr_value = gaim_status_get_attr_value(status, id); | |
136 g_return_val_if_fail(attr_value, NULL); | |
137 attr_type = gaim_value_get_type(attr_value); | |
138 | |
139 /* | |
140 * If attr_value is a different type than it should be | |
141 * then don't write it to the file. | |
142 */ | |
143 default_attr = gaim_status_type_get_attr(type, id); | |
144 default_value = gaim_status_attr_get_value(default_attr); | |
145 if (attr_type != gaim_value_get_type(default_value)) | |
146 return NULL; | |
147 | |
148 /* | |
149 * If attr_value is the same as the default for this status | |
150 * then there is no need to write it to the file. | |
151 */ | |
152 if (attr_type == GAIM_TYPE_STRING) | |
153 { | |
154 const char *string_value = gaim_value_get_string(attr_value); | |
155 const char *default_string_value = gaim_value_get_string(default_value); | |
156 if (((string_value == NULL) && (default_string_value == NULL)) || | |
157 ((string_value != NULL) && (default_string_value != NULL) && | |
158 !strcmp(string_value, default_string_value))) | |
159 return NULL; | |
160 value = g_strdup(gaim_value_get_string(attr_value)); | |
161 } | |
162 else if (attr_type == GAIM_TYPE_INT) | |
163 { | |
164 int int_value = gaim_value_get_int(attr_value); | |
165 if (int_value == gaim_value_get_int(default_value)) | |
166 return NULL; | |
167 value = g_strdup_printf("%d", int_value); | |
168 } | |
169 else if (attr_type == GAIM_TYPE_BOOLEAN) | |
170 { | |
171 gboolean boolean_value = gaim_value_get_boolean(attr_value); | |
172 if (boolean_value == gaim_value_get_boolean(default_value)) | |
173 return NULL; | |
174 value = g_strdup(boolean_value ? | |
175 "true" : "false"); | |
176 } | |
177 else | |
178 { | |
179 return NULL; | |
180 } | |
181 | |
182 g_return_val_if_fail(value, NULL); | |
183 | |
184 node = xmlnode_new("attribute"); | |
185 | |
186 xmlnode_set_attrib(node, "id", id); | |
187 xmlnode_set_attrib(node, "value", value); | |
188 | |
189 g_free(value); | |
190 | |
191 return node; | |
192 } | |
193 | |
194 static xmlnode * | |
195 status_attrs_to_xmlnode(const GaimStatus *status) | |
196 { | |
197 GaimStatusType *type = gaim_status_get_type(status); | |
198 xmlnode *node, *child; | |
199 const GList *attrs, *attr; | |
200 | |
201 node = xmlnode_new("attributes"); | |
202 | |
203 attrs = gaim_status_type_get_attrs(type); | |
204 for (attr = attrs; attr != NULL; attr = attr->next) | |
205 { | |
206 child = status_attr_to_xmlnode(status, type, (const GaimStatusAttr *)attr->data); | |
207 if (child) | |
208 xmlnode_insert_child(node, child); | |
209 } | |
210 | |
211 return node; | |
212 } | |
213 | |
214 static xmlnode * | |
215 status_to_xmlnode(const GaimStatus *status) | |
216 { | |
217 xmlnode *node, *child; | |
218 | |
219 node = xmlnode_new("status"); | |
220 xmlnode_set_attrib(node, "type", gaim_status_get_id(status)); | |
221 if (gaim_status_get_name(status) != NULL) | |
222 xmlnode_set_attrib(node, "name", gaim_status_get_name(status)); | |
223 xmlnode_set_attrib(node, "active", gaim_status_is_active(status) ? "true" : "false"); | |
224 | |
225 child = status_attrs_to_xmlnode(status); | |
226 xmlnode_insert_child(node, child); | |
227 | |
228 return node; | |
229 } | |
230 | |
231 static xmlnode * | |
232 statuses_to_xmlnode(const GaimPresence *presence) | |
233 { | |
234 xmlnode *node, *child; | |
235 const GList *statuses, *status; | |
236 | |
237 node = xmlnode_new("statuses"); | |
238 | |
239 statuses = gaim_presence_get_statuses(presence); | |
240 for (status = statuses; status != NULL; status = status->next) | |
241 { | |
242 child = status_to_xmlnode((GaimStatus *)status->data); | |
243 xmlnode_insert_child(node, child); | |
244 } | |
245 | |
246 return node; | |
247 } | |
248 | |
249 static xmlnode * | |
250 proxy_settings_to_xmlnode(GaimProxyInfo *proxy_info) | |
251 { | |
252 xmlnode *node, *child; | |
253 GaimProxyType proxy_type; | |
254 const char *value; | |
255 int int_value; | |
256 char buf[20]; | |
257 | |
258 proxy_type = gaim_proxy_info_get_type(proxy_info); | |
259 | |
260 node = xmlnode_new("proxy"); | |
261 | |
262 child = xmlnode_new_child(node, "type"); | |
263 xmlnode_insert_data(child, | |
264 (proxy_type == GAIM_PROXY_USE_GLOBAL ? "global" : | |
265 proxy_type == GAIM_PROXY_NONE ? "none" : | |
266 proxy_type == GAIM_PROXY_HTTP ? "http" : | |
267 proxy_type == GAIM_PROXY_SOCKS4 ? "socks4" : | |
268 proxy_type == GAIM_PROXY_SOCKS5 ? "socks5" : | |
269 proxy_type == GAIM_PROXY_USE_ENVVAR ? "envvar" : "unknown"), -1); | |
270 | |
271 if (proxy_type != GAIM_PROXY_USE_GLOBAL && | |
272 proxy_type != GAIM_PROXY_NONE && | |
273 proxy_type != GAIM_PROXY_USE_ENVVAR) | |
274 { | |
275 if ((value = gaim_proxy_info_get_host(proxy_info)) != NULL) | |
276 { | |
277 child = xmlnode_new_child(node, "host"); | |
278 xmlnode_insert_data(child, value, -1); | |
279 } | |
280 | |
281 if ((int_value = gaim_proxy_info_get_port(proxy_info)) != 0) | |
282 { | |
283 snprintf(buf, sizeof(buf), "%d", int_value); | |
284 child = xmlnode_new_child(node, "port"); | |
285 xmlnode_insert_data(child, buf, -1); | |
286 } | |
287 | |
288 if ((value = gaim_proxy_info_get_username(proxy_info)) != NULL) | |
289 { | |
290 child = xmlnode_new_child(node, "username"); | |
291 xmlnode_insert_data(child, value, -1); | |
292 } | |
293 | |
294 if ((value = gaim_proxy_info_get_password(proxy_info)) != NULL) | |
295 { | |
296 child = xmlnode_new_child(node, "password"); | |
297 xmlnode_insert_data(child, value, -1); | |
298 } | |
299 } | |
300 | |
301 return node; | |
302 } | |
303 | |
304 static xmlnode * | |
305 account_to_xmlnode(GaimAccount *account) | |
306 { | |
307 xmlnode *node, *child; | |
308 const char *tmp; | |
309 GaimPresence *presence; | |
310 GaimProxyInfo *proxy_info; | |
311 | |
312 node = xmlnode_new("account"); | |
313 | |
314 child = xmlnode_new_child(node, "protocol"); | |
315 xmlnode_insert_data(child, gaim_account_get_protocol_id(account), -1); | |
316 | |
317 child = xmlnode_new_child(node, "name"); | |
318 xmlnode_insert_data(child, gaim_account_get_username(account), -1); | |
319 | |
320 if (gaim_account_get_remember_password(account) && | |
321 ((tmp = gaim_account_get_password(account)) != NULL)) | |
322 { | |
323 child = xmlnode_new_child(node, "password"); | |
324 xmlnode_insert_data(child, tmp, -1); | |
325 } | |
326 | |
327 if ((tmp = gaim_account_get_alias(account)) != NULL) | |
328 { | |
329 child = xmlnode_new_child(node, "alias"); | |
330 xmlnode_insert_data(child, tmp, -1); | |
331 } | |
332 | |
333 if ((presence = gaim_account_get_presence(account)) != NULL) | |
334 { | |
335 child = statuses_to_xmlnode(presence); | |
336 xmlnode_insert_child(node, child); | |
337 } | |
338 | |
339 if ((tmp = gaim_account_get_user_info(account)) != NULL) | |
340 { | |
341 /* TODO: Do we need to call gaim_str_strip_char(tmp, '\r') here? */ | |
342 child = xmlnode_new_child(node, "userinfo"); | |
343 xmlnode_insert_data(child, tmp, -1); | |
344 } | |
345 | |
346 if ((tmp = gaim_account_get_buddy_icon(account)) != NULL) | |
347 { | |
348 child = xmlnode_new_child(node, "buddyicon"); | |
349 xmlnode_insert_data(child, tmp, -1); | |
350 } | |
351 | |
352 if (g_hash_table_size(account->settings) > 0) | |
353 { | |
354 child = xmlnode_new_child(node, "settings"); | |
355 g_hash_table_foreach(account->settings, setting_to_xmlnode, child); | |
356 } | |
357 | |
358 if (g_hash_table_size(account->ui_settings) > 0) | |
359 { | |
360 g_hash_table_foreach(account->ui_settings, ui_setting_to_xmlnode, node); | |
361 } | |
362 | |
363 if ((proxy_info = gaim_account_get_proxy_info(account)) != NULL) | |
364 { | |
365 child = proxy_settings_to_xmlnode(proxy_info); | |
366 xmlnode_insert_child(node, child); | |
367 } | |
368 | |
369 return node; | |
370 } | |
371 | |
372 static xmlnode * | |
373 accounts_to_xmlnode(void) | |
374 { | |
375 xmlnode *node, *child; | |
376 GList *cur; | |
377 | |
378 node = xmlnode_new("account"); | |
379 xmlnode_set_attrib(node, "version", "1.0"); | |
380 | |
381 for (cur = gaim_accounts_get_all(); cur != NULL; cur = cur->next) | |
382 { | |
383 child = account_to_xmlnode(cur->data); | |
384 xmlnode_insert_child(node, child); | |
385 } | |
386 | |
387 return node; | |
388 } | |
389 | |
390 static void | |
391 sync_accounts(void) | |
392 { | |
393 xmlnode *node; | |
394 char *data; | |
395 | |
396 if (!accounts_loaded) | |
397 { | |
398 gaim_debug_error("account", "Attempted to save accounts before " | |
399 "they were read!\n"); | |
400 return; | |
401 } | |
402 | |
403 node = accounts_to_xmlnode(); | |
404 data = xmlnode_to_formatted_str(node, NULL); | |
405 gaim_util_write_data_to_file("accounts.xml", data, -1); | |
406 g_free(data); | |
407 xmlnode_free(node); | |
408 } | |
409 | |
410 static gboolean | |
411 save_cb(gpointer data) | |
412 { | |
413 sync_accounts(); | |
414 save_timer = 0; | |
415 return FALSE; | |
416 } | |
417 | |
418 static void | |
419 schedule_accounts_save() | |
420 { | |
421 if (save_timer == 0) | |
422 save_timer = gaim_timeout_add(5000, save_cb, NULL); | |
423 } | |
424 | |
425 | |
426 /********************************************************************* | |
427 * Reading from disk * | |
428 *********************************************************************/ | |
429 | |
430 static void | |
431 parse_settings(xmlnode *node, GaimAccount *account) | |
432 { | |
433 const char *ui; | |
434 xmlnode *child; | |
435 | |
436 /* Get the UI string, if these are UI settings */ | |
437 ui = xmlnode_get_attrib(node, "ui"); | |
438 | |
439 /* Read settings, one by one */ | |
440 for (child = xmlnode_get_child(node, "setting"); child != NULL; | |
441 child = xmlnode_get_next_twin(child)) | |
442 { | |
443 const char *name, *str_type; | |
444 GaimPrefType type; | |
445 char *data; | |
446 | |
447 name = xmlnode_get_attrib(child, "name"); | |
448 if (name == NULL) | |
449 /* Ignore this setting */ | |
450 continue; | |
451 | |
452 str_type = xmlnode_get_attrib(child, "type"); | |
453 if (str_type == NULL) | |
454 /* Ignore this setting */ | |
455 continue; | |
456 | |
457 if (!strcmp(str_type, "string")) | |
458 type = GAIM_PREF_STRING; | |
459 else if (!strcmp(str_type, "int")) | |
460 type = GAIM_PREF_INT; | |
461 else if (!strcmp(str_type, "bool")) | |
462 type = GAIM_PREF_BOOLEAN; | |
463 else | |
464 /* Ignore this setting */ | |
465 continue; | |
466 | |
467 data = xmlnode_get_data(child); | |
468 if (data == NULL) | |
469 /* Ignore this setting */ | |
470 continue; | |
471 | |
472 if (ui == NULL) | |
473 { | |
474 if (type == GAIM_PREF_STRING) | |
475 gaim_account_set_string(account, name, data); | |
476 else if (type == GAIM_PREF_INT) | |
477 gaim_account_set_int(account, name, atoi(data)); | |
478 else if (type == GAIM_PREF_BOOLEAN) | |
479 gaim_account_set_bool(account, name, | |
480 (*data == '0' ? FALSE : TRUE)); | |
481 } else { | |
482 if (type == GAIM_PREF_STRING) | |
483 gaim_account_set_ui_string(account, ui, name, data); | |
484 else if (type == GAIM_PREF_INT) | |
485 gaim_account_set_ui_int(account, ui, name, atoi(data)); | |
486 else if (type == GAIM_PREF_BOOLEAN) | |
487 gaim_account_set_ui_bool(account, ui, name, | |
488 (*data == '0' ? FALSE : TRUE)); | |
489 } | |
490 | |
491 g_free(data); | |
492 } | |
493 } | |
494 | |
495 static GList * | |
496 parse_status_attrs(xmlnode *node, GaimStatus *status) | |
497 { | |
498 GList *list = NULL; | |
499 xmlnode *child; | |
500 GaimValue *attr_value; | |
501 | |
502 for (child = xmlnode_get_child(node, "attribute"); child != NULL; | |
503 child = xmlnode_get_next_twin(child)) | |
504 { | |
505 const char *id = xmlnode_get_attrib(child, "id"); | |
506 const char *value = xmlnode_get_attrib(child, "value"); | |
507 | |
508 if (!id || !*id || !value || !*value) | |
509 continue; | |
510 | |
511 attr_value = gaim_status_get_attr_value(status, id); | |
512 if (!attr_value) | |
513 continue; | |
514 | |
515 list = g_list_append(list, (char *)id); | |
516 | |
517 switch (gaim_value_get_type(attr_value)) | |
518 { | |
519 case GAIM_TYPE_STRING: | |
520 list = g_list_append(list, (char *)value); | |
521 break; | |
522 case GAIM_TYPE_INT: | |
523 case GAIM_TYPE_BOOLEAN: | |
524 { | |
525 int v; | |
526 if (sscanf(value, "%d", &v) == 1) | |
527 list = g_list_append(list, GINT_TO_POINTER(v)); | |
528 else | |
529 list = g_list_remove(list, id); | |
530 break; | |
531 } | |
532 default: | |
533 break; | |
534 } | |
535 } | |
536 | |
537 return list; | |
538 } | |
539 | |
540 static void | |
541 parse_status(xmlnode *node, GaimAccount *account) | |
542 { | |
543 gboolean active = FALSE; | |
544 const char *data; | |
545 const char *type; | |
546 xmlnode *child; | |
547 GList *attrs = NULL; | |
548 | |
549 /* Get the active/inactive state */ | |
550 data = xmlnode_get_attrib(node, "active"); | |
551 if (data == NULL) | |
552 return; | |
553 if (strcasecmp(data, "true") == 0) | |
554 active = TRUE; | |
555 else if (strcasecmp(data, "false") == 0) | |
556 active = FALSE; | |
557 else | |
558 return; | |
559 | |
560 /* Get the type of the status */ | |
561 type = xmlnode_get_attrib(node, "type"); | |
562 if (type == NULL) | |
563 return; | |
564 | |
565 /* Read attributes into a GList */ | |
566 child = xmlnode_get_child(node, "attributes"); | |
567 if (child != NULL) | |
568 { | |
569 attrs = parse_status_attrs(child, | |
570 gaim_account_get_status(account, type)); | |
571 } | |
572 | |
573 gaim_account_set_status_list(account, type, active, attrs); | |
574 | |
575 g_list_free(attrs); | |
576 } | |
577 | |
578 static void | |
579 parse_statuses(xmlnode *node, GaimAccount *account) | |
580 { | |
581 xmlnode *child; | |
582 | |
583 for (child = xmlnode_get_child(node, "status"); child != NULL; | |
584 child = xmlnode_get_next_twin(child)) | |
585 { | |
586 parse_status(child, account); | |
587 } | |
588 } | |
589 | |
590 static void | |
591 parse_proxy_info(xmlnode *node, GaimAccount *account) | |
592 { | |
593 GaimProxyInfo *proxy_info; | |
594 xmlnode *child; | |
595 char *data; | |
596 | |
597 proxy_info = gaim_proxy_info_new(); | |
598 | |
599 /* Use the global proxy settings, by default */ | |
600 gaim_proxy_info_set_type(proxy_info, GAIM_PROXY_USE_GLOBAL); | |
601 | |
602 /* Read proxy type */ | |
603 child = xmlnode_get_child(node, "type"); | |
604 if ((child != NULL) && ((data = xmlnode_get_data(child)) != NULL)) | |
605 { | |
606 if (!strcmp(data, "global")) | |
607 gaim_proxy_info_set_type(proxy_info, GAIM_PROXY_USE_GLOBAL); | |
608 else if (!strcmp(data, "none")) | |
609 gaim_proxy_info_set_type(proxy_info, GAIM_PROXY_NONE); | |
610 else if (!strcmp(data, "http")) | |
611 gaim_proxy_info_set_type(proxy_info, GAIM_PROXY_HTTP); | |
612 else if (!strcmp(data, "socks4")) | |
613 gaim_proxy_info_set_type(proxy_info, GAIM_PROXY_SOCKS4); | |
614 else if (!strcmp(data, "socks5")) | |
615 gaim_proxy_info_set_type(proxy_info, GAIM_PROXY_SOCKS5); | |
616 else if (!strcmp(data, "envvar")) | |
617 gaim_proxy_info_set_type(proxy_info, GAIM_PROXY_USE_ENVVAR); | |
618 else | |
619 { | |
620 gaim_debug_error("account", "Invalid proxy type found when " | |
621 "loading account information for %s\n", | |
622 gaim_account_get_username(account)); | |
623 } | |
624 g_free(data); | |
625 } | |
626 | |
627 /* Read proxy host */ | |
628 child = xmlnode_get_child(node, "host"); | |
629 if ((child != NULL) && ((data = xmlnode_get_data(child)) != NULL)) | |
630 { | |
631 gaim_proxy_info_set_host(proxy_info, data); | |
632 g_free(data); | |
633 } | |
634 | |
635 /* Read proxy port */ | |
636 child = xmlnode_get_child(node, "port"); | |
637 if ((child != NULL) && ((data = xmlnode_get_data(child)) != NULL)) | |
638 { | |
639 gaim_proxy_info_set_port(proxy_info, atoi(data)); | |
640 g_free(data); | |
641 } | |
642 | |
643 /* Read proxy username */ | |
644 child = xmlnode_get_child(node, "username"); | |
645 if ((child != NULL) && ((data = xmlnode_get_data(child)) != NULL)) | |
646 { | |
647 gaim_proxy_info_set_username(proxy_info, data); | |
648 g_free(data); | |
649 } | |
650 | |
651 /* Read proxy password */ | |
652 child = xmlnode_get_child(node, "password"); | |
653 if ((child != NULL) && ((data = xmlnode_get_data(child)) != NULL)) | |
654 { | |
655 gaim_proxy_info_set_password(proxy_info, data); | |
656 g_free(data); | |
657 } | |
658 | |
659 /* If there are no values set then proxy_infourn NULL */ | |
660 if ((gaim_proxy_info_get_type(proxy_info) == GAIM_PROXY_USE_GLOBAL) && | |
661 (gaim_proxy_info_get_host(proxy_info) == NULL) && | |
662 (gaim_proxy_info_get_port(proxy_info) == 0) && | |
663 (gaim_proxy_info_get_username(proxy_info) == NULL) && | |
664 (gaim_proxy_info_get_password(proxy_info) == NULL)) | |
665 { | |
666 gaim_proxy_info_destroy(proxy_info); | |
667 return; | |
668 } | |
669 | |
670 gaim_account_set_proxy_info(account, proxy_info); | |
671 } | |
672 | |
673 static GaimAccount * | |
674 parse_account(xmlnode *node) | |
675 { | |
676 GaimAccount *ret; | |
677 xmlnode *child; | |
678 char *protocol_id = NULL; | |
679 char *name = NULL; | |
680 char *data; | |
681 | |
682 child = xmlnode_get_child(node, "protocol"); | |
683 if (child != NULL) | |
684 protocol_id = xmlnode_get_data(child); | |
685 | |
686 child = xmlnode_get_child(node, "name"); | |
687 if (child != NULL) | |
688 name = xmlnode_get_data(child); | |
689 if (name == NULL) | |
690 { | |
691 /* Do we really need to do this? */ | |
692 child = xmlnode_get_child(node, "username"); | |
693 if (child != NULL) | |
694 name = xmlnode_get_data(child); | |
695 } | |
696 | |
697 if ((protocol_id == NULL) || (name == NULL)) | |
698 { | |
699 g_free(protocol_id); | |
700 g_free(name); | |
701 return NULL; | |
702 } | |
703 | |
704 ret = gaim_account_new(name, protocol_id); | |
705 g_free(name); | |
706 g_free(protocol_id); | |
707 | |
708 /* Read the password */ | |
709 child = xmlnode_get_child(node, "password"); | |
710 if ((child != NULL) && ((data = xmlnode_get_data(child)) != NULL)) | |
711 { | |
712 gaim_account_set_remember_password(ret, TRUE); | |
713 gaim_account_set_password(ret, data); | |
714 g_free(data); | |
715 } | |
716 | |
717 /* Read the alias */ | |
718 child = xmlnode_get_child(node, "alias"); | |
719 if ((child != NULL) && ((data = xmlnode_get_data(child)) != NULL)) | |
720 { | |
721 if (*data != '\0') | |
722 gaim_account_set_alias(ret, data); | |
723 g_free(data); | |
724 } | |
725 | |
726 /* Read the statuses */ | |
727 child = xmlnode_get_child(node, "statuses"); | |
728 if (child != NULL) | |
729 { | |
730 parse_statuses(child, ret); | |
731 } | |
732 | |
733 /* Read the userinfo */ | |
734 child = xmlnode_get_child(node, "userinfo"); | |
735 if ((child != NULL) && ((data = xmlnode_get_data(child)) != NULL)) | |
736 { | |
737 gaim_account_set_user_info(ret, data); | |
738 g_free(data); | |
739 } | |
740 | |
741 /* Read the buddyicon */ | |
742 child = xmlnode_get_child(node, "buddyicon"); | |
743 if ((child != NULL) && ((data = xmlnode_get_data(child)) != NULL)) | |
744 { | |
745 gaim_account_set_buddy_icon(ret, data); | |
746 g_free(data); | |
747 } | |
748 | |
749 /* Read settings (both core and UI) */ | |
750 for (child = xmlnode_get_child(node, "settings"); child != NULL; | |
751 child = xmlnode_get_next_twin(child)) | |
752 { | |
753 parse_settings(child, ret); | |
754 } | |
755 | |
756 /* Read proxy */ | |
757 child = xmlnode_get_child(node, "proxy"); | |
758 if (child != NULL) | |
759 { | |
760 parse_proxy_info(child, ret); | |
761 } | |
762 | |
763 return ret; | |
764 } | |
765 | |
766 static void | |
767 load_accounts(void) | |
768 { | |
769 xmlnode *node, *child; | |
770 | |
771 accounts_loaded = TRUE; | |
772 | |
773 node = gaim_util_read_xml_from_file("accounts.xml", _("accounts")); | |
774 | |
775 if (node == NULL) | |
776 return; | |
777 | |
778 for (child = xmlnode_get_child(node, "account"); child != NULL; | |
779 child = xmlnode_get_next_twin(child)) | |
780 { | |
781 GaimAccount *new_acct; | |
782 new_acct = parse_account(child); | |
783 gaim_accounts_add(new_acct); | |
784 } | |
785 | |
786 xmlnode_free(node); | |
787 } | |
788 | |
789 | |
790 static void | |
791 delete_setting(void *data) | |
792 { | |
793 GaimAccountSetting *setting = (GaimAccountSetting *)data; | |
794 | |
795 g_free(setting->ui); | |
796 | |
797 if (setting->type == GAIM_PREF_STRING) | |
798 g_free(setting->value.string); | |
799 | |
800 g_free(setting); | |
801 } | |
802 | |
803 GaimAccount * | |
804 gaim_account_new(const char *username, const char *protocol_id) | |
805 { | |
806 GaimAccount *account = NULL; | |
807 GaimPlugin *prpl = NULL; | |
808 GaimPluginProtocolInfo *prpl_info = NULL; | |
809 GaimStatusType *status_type; | |
810 | |
811 g_return_val_if_fail(username != NULL, NULL); | |
812 g_return_val_if_fail(protocol_id != NULL, NULL); | |
813 | |
814 account = gaim_accounts_find(username, protocol_id); | |
815 | |
816 if (account != NULL) | |
817 return account; | |
818 | |
819 account = g_new0(GaimAccount, 1); | |
820 GAIM_DBUS_REGISTER_POINTER(account, GaimAccount); | |
821 | |
822 gaim_account_set_username(account, username); | |
823 | |
824 gaim_account_set_protocol_id(account, protocol_id); | |
825 | |
826 account->settings = g_hash_table_new_full(g_str_hash, g_str_equal, | |
827 g_free, delete_setting); | |
828 account->ui_settings = g_hash_table_new_full(g_str_hash, g_str_equal, | |
829 g_free, (GDestroyNotify)g_hash_table_destroy); | |
830 account->system_log = NULL; | |
831 /* 0 is not a valid privacy setting */ | |
832 account->perm_deny = GAIM_PRIVACY_ALLOW_ALL; | |
833 | |
834 account->presence = gaim_presence_new_for_account(account); | |
835 | |
836 prpl = gaim_find_prpl(protocol_id); | |
837 | |
838 if (prpl == NULL) | |
839 return account; | |
840 | |
841 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(prpl); | |
842 if (prpl_info != NULL && prpl_info->status_types != NULL) | |
843 gaim_account_set_status_types(account, prpl_info->status_types(account)); | |
844 | |
845 status_type = gaim_account_get_status_type_with_primitive(account, GAIM_STATUS_AVAILABLE); | |
846 if (status_type != NULL) | |
847 gaim_presence_set_status_active(account->presence, | |
848 gaim_status_type_get_id(status_type), | |
849 TRUE); | |
850 else | |
851 gaim_presence_set_status_active(account->presence, | |
852 "offline", | |
853 TRUE); | |
854 | |
855 return account; | |
856 } | |
857 | |
858 void | |
859 gaim_account_destroy(GaimAccount *account) | |
860 { | |
861 GList *l; | |
862 | |
863 g_return_if_fail(account != NULL); | |
864 | |
865 gaim_debug_info("account", "Destroying account %p\n", account); | |
866 | |
867 for (l = gaim_get_conversations(); l != NULL; l = l->next) | |
868 { | |
869 GaimConversation *conv = (GaimConversation *)l->data; | |
870 | |
871 if (gaim_conversation_get_account(conv) == account) | |
872 gaim_conversation_set_account(conv, NULL); | |
873 } | |
874 | |
875 g_free(account->username); | |
876 g_free(account->alias); | |
877 g_free(account->password); | |
878 g_free(account->user_info); | |
879 g_free(account->protocol_id); | |
880 | |
881 g_hash_table_destroy(account->settings); | |
882 g_hash_table_destroy(account->ui_settings); | |
883 | |
884 gaim_account_set_status_types(account, NULL); | |
885 | |
886 gaim_presence_destroy(account->presence); | |
887 | |
888 if(account->system_log) | |
889 gaim_log_free(account->system_log); | |
890 | |
891 GAIM_DBUS_UNREGISTER_POINTER(account); | |
892 g_free(account); | |
893 } | |
894 | |
895 void | |
896 gaim_account_register(GaimAccount *account) | |
897 { | |
898 g_return_if_fail(account != NULL); | |
899 | |
900 gaim_debug_info("account", "Registering account %s\n", | |
901 gaim_account_get_username(account)); | |
902 | |
903 gaim_connection_new(account, TRUE, NULL); | |
904 } | |
905 | |
906 static void | |
907 request_password_ok_cb(GaimAccount *account, GaimRequestFields *fields) | |
908 { | |
909 const char *entry; | |
910 gboolean remember; | |
911 | |
912 entry = gaim_request_fields_get_string(fields, "password"); | |
913 remember = gaim_request_fields_get_bool(fields, "remember"); | |
914 | |
915 if (!entry || !*entry) | |
916 { | |
917 gaim_notify_error(account, NULL, _("Password is required to sign on."), NULL); | |
918 return; | |
919 } | |
920 | |
921 if(remember) | |
922 gaim_account_set_remember_password(account, TRUE); | |
923 | |
924 gaim_account_set_password(account, entry); | |
925 | |
926 gaim_connection_new(account, FALSE, entry); | |
927 } | |
928 | |
929 static void | |
930 request_password(GaimAccount *account) | |
931 { | |
932 gchar *primary; | |
933 const gchar *username; | |
934 GaimRequestFieldGroup *group; | |
935 GaimRequestField *field; | |
936 GaimRequestFields *fields; | |
937 | |
938 /* Close any previous password request windows */ | |
939 gaim_request_close_with_handle(account); | |
940 | |
941 username = gaim_account_get_username(account); | |
942 primary = g_strdup_printf(_("Enter password for %s (%s)"), username, | |
943 gaim_account_get_protocol_name(account)); | |
944 | |
945 fields = gaim_request_fields_new(); | |
946 group = gaim_request_field_group_new(NULL); | |
947 gaim_request_fields_add_group(fields, group); | |
948 | |
949 field = gaim_request_field_string_new("password", _("Enter Password"), NULL, FALSE); | |
950 gaim_request_field_string_set_masked(field, TRUE); | |
951 gaim_request_field_set_required(field, TRUE); | |
952 gaim_request_field_group_add_field(group, field); | |
953 | |
954 field = gaim_request_field_bool_new("remember", _("Save password"), FALSE); | |
955 gaim_request_field_group_add_field(group, field); | |
956 | |
957 gaim_request_fields(account, | |
958 NULL, | |
959 primary, | |
960 NULL, | |
961 fields, | |
962 _("OK"), G_CALLBACK(request_password_ok_cb), | |
963 _("Cancel"), NULL, | |
964 account); | |
965 g_free(primary); | |
966 } | |
967 | |
968 void | |
969 gaim_account_connect(GaimAccount *account) | |
970 { | |
971 GaimPlugin *prpl; | |
972 GaimPluginProtocolInfo *prpl_info; | |
973 const char *password; | |
974 | |
975 g_return_if_fail(account != NULL); | |
976 | |
977 gaim_debug_info("account", "Connecting to account %s\n", | |
978 gaim_account_get_username(account)); | |
979 | |
980 if (!gaim_account_get_enabled(account, gaim_core_get_ui())) | |
981 return; | |
982 | |
983 prpl = gaim_find_prpl(gaim_account_get_protocol_id(account)); | |
984 if (prpl == NULL) | |
985 { | |
986 gchar *message; | |
987 | |
988 message = g_strdup_printf(_("Missing protocol plugin for %s"), | |
989 gaim_account_get_username(account)); | |
990 gaim_notify_error(account, _("Connection Error"), message, NULL); | |
991 g_free(message); | |
992 return; | |
993 } | |
994 | |
995 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(prpl); | |
996 password = gaim_account_get_password(account); | |
997 if ((password == NULL) && | |
998 !(prpl_info->options & OPT_PROTO_NO_PASSWORD) && | |
999 !(prpl_info->options & OPT_PROTO_PASSWORD_OPTIONAL)) | |
1000 request_password(account); | |
1001 else | |
1002 gaim_connection_new(account, FALSE, password); | |
1003 } | |
1004 | |
1005 void | |
1006 gaim_account_disconnect(GaimAccount *account) | |
1007 { | |
1008 GaimConnection *gc; | |
1009 | |
1010 g_return_if_fail(account != NULL); | |
1011 g_return_if_fail(!gaim_account_is_disconnected(account)); | |
1012 | |
1013 gaim_debug_info("account", "Disconnecting account %p\n", account); | |
1014 | |
1015 account->disconnecting = TRUE; | |
1016 | |
1017 gc = gaim_account_get_connection(account); | |
1018 gaim_connection_destroy(gc); | |
1019 if (!gaim_account_get_remember_password(account)) | |
1020 gaim_account_set_password(account, NULL); | |
1021 gaim_account_set_connection(account, NULL); | |
1022 | |
1023 account->disconnecting = FALSE; | |
1024 } | |
1025 | |
1026 void | |
1027 gaim_account_notify_added(GaimAccount *account, const char *remote_user, | |
1028 const char *id, const char *alias, | |
1029 const char *message) | |
1030 { | |
1031 GaimAccountUiOps *ui_ops; | |
1032 | |
1033 g_return_if_fail(account != NULL); | |
1034 g_return_if_fail(remote_user != NULL); | |
1035 | |
1036 ui_ops = gaim_accounts_get_ui_ops(); | |
1037 | |
1038 if (ui_ops != NULL && ui_ops->notify_added != NULL) | |
1039 ui_ops->notify_added(account, remote_user, id, alias, message); | |
1040 } | |
1041 | |
1042 void | |
1043 gaim_account_request_add(GaimAccount *account, const char *remote_user, | |
1044 const char *id, const char *alias, | |
1045 const char *message) | |
1046 { | |
1047 GaimAccountUiOps *ui_ops; | |
1048 | |
1049 g_return_if_fail(account != NULL); | |
1050 g_return_if_fail(remote_user != NULL); | |
1051 | |
1052 ui_ops = gaim_accounts_get_ui_ops(); | |
1053 | |
1054 if (ui_ops != NULL && ui_ops->request_add != NULL) | |
1055 ui_ops->request_add(account, remote_user, id, alias, message); | |
1056 } | |
1057 | |
1058 static void | |
1059 change_password_cb(GaimAccount *account, GaimRequestFields *fields) | |
1060 { | |
1061 const char *orig_pass, *new_pass_1, *new_pass_2; | |
1062 | |
1063 orig_pass = gaim_request_fields_get_string(fields, "password"); | |
1064 new_pass_1 = gaim_request_fields_get_string(fields, "new_password_1"); | |
1065 new_pass_2 = gaim_request_fields_get_string(fields, "new_password_2"); | |
1066 | |
1067 if (g_utf8_collate(new_pass_1, new_pass_2)) | |
1068 { | |
1069 gaim_notify_error(account, NULL, | |
1070 _("New passwords do not match."), NULL); | |
1071 | |
1072 return; | |
1073 } | |
1074 | |
1075 if (orig_pass == NULL || new_pass_1 == NULL || new_pass_2 == NULL || | |
1076 *orig_pass == '\0' || *new_pass_1 == '\0' || *new_pass_2 == '\0') | |
1077 { | |
1078 gaim_notify_error(account, NULL, | |
1079 _("Fill out all fields completely."), NULL); | |
1080 return; | |
1081 } | |
1082 | |
1083 gaim_account_change_password(account, orig_pass, new_pass_1); | |
1084 } | |
1085 | |
1086 void | |
1087 gaim_account_request_change_password(GaimAccount *account) | |
1088 { | |
1089 GaimRequestFields *fields; | |
1090 GaimRequestFieldGroup *group; | |
1091 GaimRequestField *field; | |
1092 char primary[256]; | |
1093 | |
1094 g_return_if_fail(account != NULL); | |
1095 g_return_if_fail(gaim_account_is_connected(account)); | |
1096 | |
1097 fields = gaim_request_fields_new(); | |
1098 | |
1099 group = gaim_request_field_group_new(NULL); | |
1100 gaim_request_fields_add_group(fields, group); | |
1101 | |
1102 field = gaim_request_field_string_new("password", _("Original password"), | |
1103 NULL, FALSE); | |
1104 gaim_request_field_string_set_masked(field, TRUE); | |
1105 gaim_request_field_set_required(field, TRUE); | |
1106 gaim_request_field_group_add_field(group, field); | |
1107 | |
1108 field = gaim_request_field_string_new("new_password_1", | |
1109 _("New password"), | |
1110 NULL, FALSE); | |
1111 gaim_request_field_string_set_masked(field, TRUE); | |
1112 gaim_request_field_set_required(field, TRUE); | |
1113 gaim_request_field_group_add_field(group, field); | |
1114 | |
1115 field = gaim_request_field_string_new("new_password_2", | |
1116 _("New password (again)"), | |
1117 NULL, FALSE); | |
1118 gaim_request_field_string_set_masked(field, TRUE); | |
1119 gaim_request_field_set_required(field, TRUE); | |
1120 gaim_request_field_group_add_field(group, field); | |
1121 | |
1122 g_snprintf(primary, sizeof(primary), _("Change password for %s"), | |
1123 gaim_account_get_username(account)); | |
1124 | |
1125 /* I'm sticking this somewhere in the code: bologna */ | |
1126 | |
1127 gaim_request_fields(gaim_account_get_connection(account), | |
1128 NULL, | |
1129 primary, | |
1130 _("Please enter your current password and your " | |
1131 "new password."), | |
1132 fields, | |
1133 _("OK"), G_CALLBACK(change_password_cb), | |
1134 _("Cancel"), NULL, | |
1135 account); | |
1136 } | |
1137 | |
1138 static void | |
1139 set_user_info_cb(GaimAccount *account, const char *user_info) | |
1140 { | |
1141 GaimConnection *gc; | |
1142 | |
1143 gaim_account_set_user_info(account, user_info); | |
1144 gc = gaim_account_get_connection(account); | |
14607 | 1145 serv_set_info(gc, user_info); |
14192 | 1146 } |
1147 | |
1148 void | |
1149 gaim_account_request_change_user_info(GaimAccount *account) | |
1150 { | |
1151 GaimConnection *gc; | |
1152 char primary[256]; | |
1153 | |
1154 g_return_if_fail(account != NULL); | |
1155 g_return_if_fail(gaim_account_is_connected(account)); | |
1156 | |
1157 gc = gaim_account_get_connection(account); | |
1158 | |
1159 g_snprintf(primary, sizeof(primary), | |
1160 _("Change user information for %s"), | |
1161 gaim_account_get_username(account)); | |
1162 | |
1163 gaim_request_input(gc, _("Set User Info"), primary, NULL, | |
1164 gaim_account_get_user_info(account), | |
1165 TRUE, FALSE, ((gc != NULL) && | |
1166 (gc->flags & GAIM_CONNECTION_HTML) ? "html" : NULL), | |
1167 _("Save"), G_CALLBACK(set_user_info_cb), | |
1168 _("Cancel"), NULL, account); | |
1169 } | |
1170 | |
1171 void | |
1172 gaim_account_set_username(GaimAccount *account, const char *username) | |
1173 { | |
1174 g_return_if_fail(account != NULL); | |
1175 | |
1176 g_free(account->username); | |
1177 account->username = g_strdup(username); | |
1178 | |
1179 schedule_accounts_save(); | |
1180 } | |
1181 | |
1182 void | |
1183 gaim_account_set_password(GaimAccount *account, const char *password) | |
1184 { | |
1185 g_return_if_fail(account != NULL); | |
1186 | |
1187 g_free(account->password); | |
1188 account->password = g_strdup(password); | |
1189 | |
1190 schedule_accounts_save(); | |
1191 } | |
1192 | |
1193 void | |
1194 gaim_account_set_alias(GaimAccount *account, const char *alias) | |
1195 { | |
1196 g_return_if_fail(account != NULL); | |
1197 | |
1198 /* | |
1199 * Do nothing if alias and account->alias are both NULL. Or if | |
1200 * they're the exact same string. | |
1201 */ | |
1202 if (alias == account->alias) | |
1203 return; | |
1204 | |
1205 if ((!alias && account->alias) || (alias && !account->alias) || | |
1206 g_utf8_collate(account->alias, alias)) | |
1207 { | |
1208 char *old = account->alias; | |
1209 | |
1210 account->alias = g_strdup(alias); | |
1211 gaim_signal_emit(gaim_accounts_get_handle(), "account-alias-changed", | |
1212 account, old); | |
1213 g_free(old); | |
1214 | |
1215 schedule_accounts_save(); | |
1216 } | |
1217 } | |
1218 | |
1219 void | |
1220 gaim_account_set_user_info(GaimAccount *account, const char *user_info) | |
1221 { | |
1222 g_return_if_fail(account != NULL); | |
1223 | |
1224 g_free(account->user_info); | |
1225 account->user_info = g_strdup(user_info); | |
1226 | |
1227 schedule_accounts_save(); | |
1228 } | |
1229 | |
1230 void | |
1231 gaim_account_set_buddy_icon(GaimAccount *account, const char *icon) | |
1232 { | |
1233 g_return_if_fail(account != NULL); | |
1234 | |
1235 /* Delete an existing icon from the cache. */ | |
1236 if (account->buddy_icon != NULL && (icon == NULL || strcmp(account->buddy_icon, icon))) | |
1237 { | |
1238 const char *dirname = gaim_buddy_icons_get_cache_dir(); | |
1239 struct stat st; | |
1240 | |
1241 if (g_stat(account->buddy_icon, &st) == 0) | |
1242 { | |
1243 /* The file exists. This is a full path. */ | |
1244 | |
1245 /* XXX: This is a hack so we only delete the file if it's | |
1246 * in the cache dir. Otherwise, people who upgrade (who | |
1247 * may have buddy icon filenames set outside of the cache | |
1248 * dir) could lose files. */ | |
1249 if (!strncmp(dirname, account->buddy_icon, strlen(dirname))) | |
1250 g_unlink(account->buddy_icon); | |
1251 } | |
1252 else | |
1253 { | |
1254 char *filename = g_build_filename(dirname, account->buddy_icon, NULL); | |
1255 g_unlink(filename); | |
1256 g_free(filename); | |
1257 } | |
1258 } | |
1259 | |
1260 g_free(account->buddy_icon); | |
1261 account->buddy_icon = g_strdup(icon); | |
1262 if (gaim_account_is_connected(account)) | |
1263 { | |
1264 char *filename = gaim_buddy_icons_get_full_path(icon); | |
1265 serv_set_buddyicon(gaim_account_get_connection(account), filename); | |
1266 g_free(filename); | |
1267 } | |
1268 | |
1269 schedule_accounts_save(); | |
1270 } | |
1271 | |
1272 void | |
1273 gaim_account_set_protocol_id(GaimAccount *account, const char *protocol_id) | |
1274 { | |
1275 g_return_if_fail(account != NULL); | |
1276 g_return_if_fail(protocol_id != NULL); | |
1277 | |
1278 g_free(account->protocol_id); | |
1279 account->protocol_id = g_strdup(protocol_id); | |
1280 | |
1281 schedule_accounts_save(); | |
1282 } | |
1283 | |
1284 void | |
1285 gaim_account_set_connection(GaimAccount *account, GaimConnection *gc) | |
1286 { | |
1287 g_return_if_fail(account != NULL); | |
1288 | |
1289 account->gc = gc; | |
1290 } | |
1291 | |
1292 void | |
1293 gaim_account_set_remember_password(GaimAccount *account, gboolean value) | |
1294 { | |
1295 g_return_if_fail(account != NULL); | |
1296 | |
1297 account->remember_pass = value; | |
1298 | |
1299 schedule_accounts_save(); | |
1300 } | |
1301 | |
1302 void | |
1303 gaim_account_set_check_mail(GaimAccount *account, gboolean value) | |
1304 { | |
1305 g_return_if_fail(account != NULL); | |
1306 | |
1307 gaim_account_set_bool(account, "check-mail", value); | |
1308 } | |
1309 | |
1310 void | |
1311 gaim_account_set_enabled(GaimAccount *account, const char *ui, | |
1312 gboolean value) | |
1313 { | |
1314 GaimConnection *gc; | |
1315 gboolean was_enabled = FALSE; | |
1316 | |
1317 g_return_if_fail(account != NULL); | |
1318 g_return_if_fail(ui != NULL); | |
1319 | |
1320 was_enabled = gaim_account_get_enabled(account, ui); | |
1321 | |
1322 gaim_account_set_ui_bool(account, ui, "auto-login", value); | |
1323 gc = gaim_account_get_connection(account); | |
1324 | |
1325 if(was_enabled && !value) | |
1326 gaim_signal_emit(gaim_accounts_get_handle(), "account-disabled", account); | |
1327 else if(!was_enabled && value) | |
1328 gaim_signal_emit(gaim_accounts_get_handle(), "account-enabled", account); | |
1329 | |
1330 if ((gc != NULL) && (gc->wants_to_die == TRUE)) | |
1331 return; | |
1332 | |
1333 if (value && gaim_presence_is_online(account->presence)) | |
1334 gaim_account_connect(account); | |
1335 else if (!value && !gaim_account_is_disconnected(account)) | |
1336 gaim_account_disconnect(account); | |
1337 } | |
1338 | |
1339 void | |
1340 gaim_account_set_proxy_info(GaimAccount *account, GaimProxyInfo *info) | |
1341 { | |
1342 g_return_if_fail(account != NULL); | |
1343 | |
1344 if (account->proxy_info != NULL) | |
1345 gaim_proxy_info_destroy(account->proxy_info); | |
1346 | |
1347 account->proxy_info = info; | |
1348 | |
1349 schedule_accounts_save(); | |
1350 } | |
1351 | |
1352 void | |
1353 gaim_account_set_status_types(GaimAccount *account, GList *status_types) | |
1354 { | |
1355 g_return_if_fail(account != NULL); | |
1356 | |
14662
61a4a2dc491e
[gaim-migrate @ 17408]
Etan Reisner <pidgin@unreliablesource.net>
parents:
14643
diff
changeset
|
1357 /* Out with the old... */ |
14192 | 1358 if (account->status_types != NULL) |
1359 { | |
1360 g_list_foreach(account->status_types, (GFunc)gaim_status_type_destroy, NULL); | |
1361 g_list_free(account->status_types); | |
1362 } | |
1363 | |
1364 /* In with the new... */ | |
1365 account->status_types = status_types; | |
1366 } | |
1367 | |
1368 void | |
1369 gaim_account_set_status(GaimAccount *account, const char *status_id, | |
1370 gboolean active, ...) | |
1371 { | |
14643 | 1372 GList *attrs = NULL; |
1373 const gchar *id; | |
1374 gpointer data; | |
14192 | 1375 va_list args; |
1376 | |
1377 va_start(args, active); | |
14643 | 1378 while ((id = va_arg(args, const char *)) != NULL) |
14192 | 1379 { |
14643 | 1380 attrs = g_list_append(attrs, (char *)id); |
1381 data = va_arg(args, void *); | |
1382 attrs = g_list_append(attrs, data); | |
14192 | 1383 } |
1384 gaim_account_set_status_list(account, status_id, active, attrs); | |
1385 g_list_free(attrs); | |
14643 | 1386 va_end(args); |
14192 | 1387 } |
1388 | |
1389 void | |
1390 gaim_account_set_status_list(GaimAccount *account, const char *status_id, | |
1391 gboolean active, GList *attrs) | |
1392 { | |
1393 GaimStatus *status; | |
1394 | |
1395 g_return_if_fail(account != NULL); | |
1396 g_return_if_fail(status_id != NULL); | |
1397 | |
1398 status = gaim_account_get_status(account, status_id); | |
1399 if (status == NULL) | |
1400 { | |
1401 gaim_debug_error("account", | |
1402 "Invalid status ID %s for account %s (%s)\n", | |
1403 status_id, gaim_account_get_username(account), | |
1404 gaim_account_get_protocol_id(account)); | |
1405 return; | |
1406 } | |
1407 | |
1408 if (active || gaim_status_is_independent(status)) | |
1409 gaim_status_set_active_with_attrs_list(status, active, attrs); | |
1410 | |
1411 /* | |
1412 * Our current statuses are saved to accounts.xml (so that when we | |
1413 * reconnect, we go back to the previous status). | |
1414 */ | |
1415 schedule_accounts_save(); | |
1416 } | |
1417 | |
1418 void | |
1419 gaim_account_clear_settings(GaimAccount *account) | |
1420 { | |
1421 g_return_if_fail(account != NULL); | |
1422 | |
1423 g_hash_table_destroy(account->settings); | |
1424 | |
1425 account->settings = g_hash_table_new_full(g_str_hash, g_str_equal, | |
1426 g_free, delete_setting); | |
1427 } | |
1428 | |
1429 void | |
1430 gaim_account_set_int(GaimAccount *account, const char *name, int value) | |
1431 { | |
1432 GaimAccountSetting *setting; | |
1433 | |
1434 g_return_if_fail(account != NULL); | |
1435 g_return_if_fail(name != NULL); | |
1436 | |
1437 setting = g_new0(GaimAccountSetting, 1); | |
1438 | |
1439 setting->type = GAIM_PREF_INT; | |
1440 setting->value.integer = value; | |
1441 | |
1442 g_hash_table_insert(account->settings, g_strdup(name), setting); | |
1443 | |
1444 schedule_accounts_save(); | |
1445 } | |
1446 | |
1447 void | |
1448 gaim_account_set_string(GaimAccount *account, const char *name, | |
1449 const char *value) | |
1450 { | |
1451 GaimAccountSetting *setting; | |
1452 | |
1453 g_return_if_fail(account != NULL); | |
1454 g_return_if_fail(name != NULL); | |
1455 | |
1456 setting = g_new0(GaimAccountSetting, 1); | |
1457 | |
1458 setting->type = GAIM_PREF_STRING; | |
1459 setting->value.string = g_strdup(value); | |
1460 | |
1461 g_hash_table_insert(account->settings, g_strdup(name), setting); | |
1462 | |
1463 schedule_accounts_save(); | |
1464 } | |
1465 | |
1466 void | |
1467 gaim_account_set_bool(GaimAccount *account, const char *name, gboolean value) | |
1468 { | |
1469 GaimAccountSetting *setting; | |
1470 | |
1471 g_return_if_fail(account != NULL); | |
1472 g_return_if_fail(name != NULL); | |
1473 | |
1474 setting = g_new0(GaimAccountSetting, 1); | |
1475 | |
1476 setting->type = GAIM_PREF_BOOLEAN; | |
1477 setting->value.bool = value; | |
1478 | |
1479 g_hash_table_insert(account->settings, g_strdup(name), setting); | |
1480 | |
1481 schedule_accounts_save(); | |
1482 } | |
1483 | |
1484 static GHashTable * | |
1485 get_ui_settings_table(GaimAccount *account, const char *ui) | |
1486 { | |
1487 GHashTable *table; | |
1488 | |
1489 table = g_hash_table_lookup(account->ui_settings, ui); | |
1490 | |
1491 if (table == NULL) { | |
1492 table = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, | |
1493 delete_setting); | |
1494 g_hash_table_insert(account->ui_settings, g_strdup(ui), table); | |
1495 } | |
1496 | |
1497 return table; | |
1498 } | |
1499 | |
1500 void | |
1501 gaim_account_set_ui_int(GaimAccount *account, const char *ui, | |
1502 const char *name, int value) | |
1503 { | |
1504 GaimAccountSetting *setting; | |
1505 GHashTable *table; | |
1506 | |
1507 g_return_if_fail(account != NULL); | |
1508 g_return_if_fail(ui != NULL); | |
1509 g_return_if_fail(name != NULL); | |
1510 | |
1511 setting = g_new0(GaimAccountSetting, 1); | |
1512 | |
1513 setting->type = GAIM_PREF_INT; | |
1514 setting->ui = g_strdup(ui); | |
1515 setting->value.integer = value; | |
1516 | |
1517 table = get_ui_settings_table(account, ui); | |
1518 | |
1519 g_hash_table_insert(table, g_strdup(name), setting); | |
1520 | |
1521 schedule_accounts_save(); | |
1522 } | |
1523 | |
1524 void | |
1525 gaim_account_set_ui_string(GaimAccount *account, const char *ui, | |
1526 const char *name, const char *value) | |
1527 { | |
1528 GaimAccountSetting *setting; | |
1529 GHashTable *table; | |
1530 | |
1531 g_return_if_fail(account != NULL); | |
1532 g_return_if_fail(ui != NULL); | |
1533 g_return_if_fail(name != NULL); | |
1534 | |
1535 setting = g_new0(GaimAccountSetting, 1); | |
1536 | |
1537 setting->type = GAIM_PREF_STRING; | |
1538 setting->ui = g_strdup(ui); | |
1539 setting->value.string = g_strdup(value); | |
1540 | |
1541 table = get_ui_settings_table(account, ui); | |
1542 | |
1543 g_hash_table_insert(table, g_strdup(name), setting); | |
1544 | |
1545 schedule_accounts_save(); | |
1546 } | |
1547 | |
1548 void | |
1549 gaim_account_set_ui_bool(GaimAccount *account, const char *ui, | |
1550 const char *name, gboolean value) | |
1551 { | |
1552 GaimAccountSetting *setting; | |
1553 GHashTable *table; | |
1554 | |
1555 g_return_if_fail(account != NULL); | |
1556 g_return_if_fail(ui != NULL); | |
1557 g_return_if_fail(name != NULL); | |
1558 | |
1559 setting = g_new0(GaimAccountSetting, 1); | |
1560 | |
1561 setting->type = GAIM_PREF_BOOLEAN; | |
1562 setting->ui = g_strdup(ui); | |
1563 setting->value.bool = value; | |
1564 | |
1565 table = get_ui_settings_table(account, ui); | |
1566 | |
1567 g_hash_table_insert(table, g_strdup(name), setting); | |
1568 | |
1569 schedule_accounts_save(); | |
1570 } | |
1571 | |
1572 static GaimConnectionState | |
1573 gaim_account_get_state(const GaimAccount *account) | |
1574 { | |
1575 GaimConnection *gc; | |
1576 | |
1577 g_return_val_if_fail(account != NULL, GAIM_DISCONNECTED); | |
1578 | |
1579 gc = gaim_account_get_connection(account); | |
1580 if (!gc) | |
1581 return GAIM_DISCONNECTED; | |
1582 | |
1583 return gaim_connection_get_state(gc); | |
1584 } | |
1585 | |
1586 gboolean | |
1587 gaim_account_is_connected(const GaimAccount *account) | |
1588 { | |
1589 return (gaim_account_get_state(account) == GAIM_CONNECTED); | |
1590 } | |
1591 | |
1592 gboolean | |
1593 gaim_account_is_connecting(const GaimAccount *account) | |
1594 { | |
1595 return (gaim_account_get_state(account) == GAIM_CONNECTING); | |
1596 } | |
1597 | |
1598 gboolean | |
1599 gaim_account_is_disconnected(const GaimAccount *account) | |
1600 { | |
1601 return (gaim_account_get_state(account) == GAIM_DISCONNECTED); | |
1602 } | |
1603 | |
1604 const char * | |
1605 gaim_account_get_username(const GaimAccount *account) | |
1606 { | |
1607 g_return_val_if_fail(account != NULL, NULL); | |
1608 | |
1609 return account->username; | |
1610 } | |
1611 | |
1612 const char * | |
1613 gaim_account_get_password(const GaimAccount *account) | |
1614 { | |
1615 g_return_val_if_fail(account != NULL, NULL); | |
1616 | |
1617 return account->password; | |
1618 } | |
1619 | |
1620 const char * | |
1621 gaim_account_get_alias(const GaimAccount *account) | |
1622 { | |
1623 g_return_val_if_fail(account != NULL, NULL); | |
1624 | |
1625 return account->alias; | |
1626 } | |
1627 | |
1628 const char * | |
1629 gaim_account_get_user_info(const GaimAccount *account) | |
1630 { | |
1631 g_return_val_if_fail(account != NULL, NULL); | |
1632 | |
1633 return account->user_info; | |
1634 } | |
1635 | |
1636 const char * | |
1637 gaim_account_get_buddy_icon(const GaimAccount *account) | |
1638 { | |
1639 g_return_val_if_fail(account != NULL, NULL); | |
1640 | |
1641 return account->buddy_icon; | |
1642 } | |
1643 | |
1644 const char * | |
1645 gaim_account_get_protocol_id(const GaimAccount *account) | |
1646 { | |
1647 g_return_val_if_fail(account != NULL, NULL); | |
1648 | |
1649 return account->protocol_id; | |
1650 } | |
1651 | |
1652 const char * | |
1653 gaim_account_get_protocol_name(const GaimAccount *account) | |
1654 { | |
1655 GaimPlugin *p; | |
1656 | |
1657 g_return_val_if_fail(account != NULL, NULL); | |
1658 | |
1659 p = gaim_find_prpl(gaim_account_get_protocol_id(account)); | |
1660 | |
1661 return ((p && p->info->name) ? _(p->info->name) : _("Unknown")); | |
1662 } | |
1663 | |
1664 GaimConnection * | |
1665 gaim_account_get_connection(const GaimAccount *account) | |
1666 { | |
1667 g_return_val_if_fail(account != NULL, NULL); | |
1668 | |
1669 return account->gc; | |
1670 } | |
1671 | |
1672 gboolean | |
1673 gaim_account_get_remember_password(const GaimAccount *account) | |
1674 { | |
1675 g_return_val_if_fail(account != NULL, FALSE); | |
1676 | |
1677 return account->remember_pass; | |
1678 } | |
1679 | |
1680 gboolean | |
1681 gaim_account_get_check_mail(const GaimAccount *account) | |
1682 { | |
1683 g_return_val_if_fail(account != NULL, FALSE); | |
1684 | |
1685 return gaim_account_get_bool(account, "check-mail", FALSE); | |
1686 } | |
1687 | |
1688 gboolean | |
1689 gaim_account_get_enabled(const GaimAccount *account, const char *ui) | |
1690 { | |
1691 g_return_val_if_fail(account != NULL, FALSE); | |
1692 g_return_val_if_fail(ui != NULL, FALSE); | |
1693 | |
1694 return gaim_account_get_ui_bool(account, ui, "auto-login", FALSE); | |
1695 } | |
1696 | |
1697 GaimProxyInfo * | |
1698 gaim_account_get_proxy_info(const GaimAccount *account) | |
1699 { | |
1700 g_return_val_if_fail(account != NULL, NULL); | |
1701 | |
1702 return account->proxy_info; | |
1703 } | |
1704 | |
1705 GaimStatus * | |
1706 gaim_account_get_active_status(const GaimAccount *account) | |
1707 { | |
1708 g_return_val_if_fail(account != NULL, NULL); | |
1709 | |
1710 return gaim_presence_get_active_status(account->presence); | |
1711 } | |
1712 | |
1713 GaimStatus * | |
1714 gaim_account_get_status(const GaimAccount *account, const char *status_id) | |
1715 { | |
1716 g_return_val_if_fail(account != NULL, NULL); | |
1717 g_return_val_if_fail(status_id != NULL, NULL); | |
1718 | |
1719 return gaim_presence_get_status(account->presence, status_id); | |
1720 } | |
1721 | |
1722 GaimStatusType * | |
1723 gaim_account_get_status_type(const GaimAccount *account, const char *id) | |
1724 { | |
1725 const GList *l; | |
1726 | |
1727 g_return_val_if_fail(account != NULL, NULL); | |
1728 g_return_val_if_fail(id != NULL, NULL); | |
1729 | |
1730 for (l = gaim_account_get_status_types(account); l != NULL; l = l->next) | |
1731 { | |
1732 GaimStatusType *status_type = (GaimStatusType *)l->data; | |
1733 | |
1734 if (!strcmp(gaim_status_type_get_id(status_type), id)) | |
1735 return status_type; | |
1736 } | |
1737 | |
1738 return NULL; | |
1739 } | |
1740 | |
1741 GaimStatusType * | |
1742 gaim_account_get_status_type_with_primitive(const GaimAccount *account, GaimStatusPrimitive primitive) | |
1743 { | |
1744 const GList *l; | |
1745 | |
1746 g_return_val_if_fail(account != NULL, NULL); | |
1747 | |
1748 for (l = gaim_account_get_status_types(account); l != NULL; l = l->next) | |
1749 { | |
1750 GaimStatusType *status_type = (GaimStatusType *)l->data; | |
1751 | |
1752 if (gaim_status_type_get_primitive(status_type) == primitive) | |
1753 return status_type; | |
1754 } | |
1755 | |
1756 return NULL; | |
1757 } | |
1758 | |
1759 GaimPresence * | |
1760 gaim_account_get_presence(const GaimAccount *account) | |
1761 { | |
1762 g_return_val_if_fail(account != NULL, NULL); | |
1763 | |
1764 return account->presence; | |
1765 } | |
1766 | |
1767 gboolean | |
1768 gaim_account_is_status_active(const GaimAccount *account, | |
1769 const char *status_id) | |
1770 { | |
1771 g_return_val_if_fail(account != NULL, FALSE); | |
1772 g_return_val_if_fail(status_id != NULL, FALSE); | |
1773 | |
1774 return gaim_presence_is_status_active(account->presence, status_id); | |
1775 } | |
1776 | |
1777 const GList * | |
1778 gaim_account_get_status_types(const GaimAccount *account) | |
1779 { | |
1780 g_return_val_if_fail(account != NULL, NULL); | |
1781 | |
1782 return account->status_types; | |
1783 } | |
1784 | |
1785 int | |
1786 gaim_account_get_int(const GaimAccount *account, const char *name, | |
1787 int default_value) | |
1788 { | |
1789 GaimAccountSetting *setting; | |
1790 | |
1791 g_return_val_if_fail(account != NULL, default_value); | |
1792 g_return_val_if_fail(name != NULL, default_value); | |
1793 | |
1794 setting = g_hash_table_lookup(account->settings, name); | |
1795 | |
1796 if (setting == NULL) | |
1797 return default_value; | |
1798 | |
1799 g_return_val_if_fail(setting->type == GAIM_PREF_INT, default_value); | |
1800 | |
1801 return setting->value.integer; | |
1802 } | |
1803 | |
1804 const char * | |
1805 gaim_account_get_string(const GaimAccount *account, const char *name, | |
1806 const char *default_value) | |
1807 { | |
1808 GaimAccountSetting *setting; | |
1809 | |
1810 g_return_val_if_fail(account != NULL, default_value); | |
1811 g_return_val_if_fail(name != NULL, default_value); | |
1812 | |
1813 setting = g_hash_table_lookup(account->settings, name); | |
1814 | |
1815 if (setting == NULL) | |
1816 return default_value; | |
1817 | |
1818 g_return_val_if_fail(setting->type == GAIM_PREF_STRING, default_value); | |
1819 | |
1820 return setting->value.string; | |
1821 } | |
1822 | |
1823 gboolean | |
1824 gaim_account_get_bool(const GaimAccount *account, const char *name, | |
1825 gboolean default_value) | |
1826 { | |
1827 GaimAccountSetting *setting; | |
1828 | |
1829 g_return_val_if_fail(account != NULL, default_value); | |
1830 g_return_val_if_fail(name != NULL, default_value); | |
1831 | |
1832 setting = g_hash_table_lookup(account->settings, name); | |
1833 | |
1834 if (setting == NULL) | |
1835 return default_value; | |
1836 | |
1837 g_return_val_if_fail(setting->type == GAIM_PREF_BOOLEAN, default_value); | |
1838 | |
1839 return setting->value.bool; | |
1840 } | |
1841 | |
1842 int | |
1843 gaim_account_get_ui_int(const GaimAccount *account, const char *ui, | |
1844 const char *name, int default_value) | |
1845 { | |
1846 GaimAccountSetting *setting; | |
1847 GHashTable *table; | |
1848 | |
1849 g_return_val_if_fail(account != NULL, default_value); | |
1850 g_return_val_if_fail(ui != NULL, default_value); | |
1851 g_return_val_if_fail(name != NULL, default_value); | |
1852 | |
1853 if ((table = g_hash_table_lookup(account->ui_settings, ui)) == NULL) | |
1854 return default_value; | |
1855 | |
1856 if ((setting = g_hash_table_lookup(table, name)) == NULL) | |
1857 return default_value; | |
1858 | |
1859 g_return_val_if_fail(setting->type == GAIM_PREF_INT, default_value); | |
1860 | |
1861 return setting->value.integer; | |
1862 } | |
1863 | |
1864 const char * | |
1865 gaim_account_get_ui_string(const GaimAccount *account, const char *ui, | |
1866 const char *name, const char *default_value) | |
1867 { | |
1868 GaimAccountSetting *setting; | |
1869 GHashTable *table; | |
1870 | |
1871 g_return_val_if_fail(account != NULL, default_value); | |
1872 g_return_val_if_fail(ui != NULL, default_value); | |
1873 g_return_val_if_fail(name != NULL, default_value); | |
1874 | |
1875 if ((table = g_hash_table_lookup(account->ui_settings, ui)) == NULL) | |
1876 return default_value; | |
1877 | |
1878 if ((setting = g_hash_table_lookup(table, name)) == NULL) | |
1879 return default_value; | |
1880 | |
1881 g_return_val_if_fail(setting->type == GAIM_PREF_STRING, default_value); | |
1882 | |
1883 return setting->value.string; | |
1884 } | |
1885 | |
1886 gboolean | |
1887 gaim_account_get_ui_bool(const GaimAccount *account, const char *ui, | |
1888 const char *name, gboolean default_value) | |
1889 { | |
1890 GaimAccountSetting *setting; | |
1891 GHashTable *table; | |
1892 | |
1893 g_return_val_if_fail(account != NULL, default_value); | |
1894 g_return_val_if_fail(ui != NULL, default_value); | |
1895 g_return_val_if_fail(name != NULL, default_value); | |
1896 | |
1897 if ((table = g_hash_table_lookup(account->ui_settings, ui)) == NULL) | |
1898 return default_value; | |
1899 | |
1900 if ((setting = g_hash_table_lookup(table, name)) == NULL) | |
1901 return default_value; | |
1902 | |
1903 g_return_val_if_fail(setting->type == GAIM_PREF_BOOLEAN, default_value); | |
1904 | |
1905 return setting->value.bool; | |
1906 } | |
1907 | |
1908 GaimLog * | |
1909 gaim_account_get_log(GaimAccount *account, gboolean create) | |
1910 { | |
1911 g_return_val_if_fail(account != NULL, NULL); | |
1912 | |
1913 if(!account->system_log && create){ | |
1914 GaimPresence *presence; | |
1915 int login_time; | |
1916 | |
1917 presence = gaim_account_get_presence(account); | |
1918 login_time = gaim_presence_get_login_time(presence); | |
1919 | |
1920 account->system_log = gaim_log_new(GAIM_LOG_SYSTEM, | |
1921 gaim_account_get_username(account), account, NULL, | |
1922 (login_time != 0) ? login_time : time(NULL), NULL); | |
1923 } | |
1924 | |
1925 return account->system_log; | |
1926 } | |
1927 | |
1928 void | |
1929 gaim_account_destroy_log(GaimAccount *account) | |
1930 { | |
1931 g_return_if_fail(account != NULL); | |
1932 | |
1933 if(account->system_log){ | |
1934 gaim_log_free(account->system_log); | |
1935 account->system_log = NULL; | |
1936 } | |
1937 } | |
1938 | |
1939 void | |
1940 gaim_account_add_buddy(GaimAccount *account, GaimBuddy *buddy) | |
1941 { | |
1942 GaimPluginProtocolInfo *prpl_info = NULL; | |
1943 GaimConnection *gc = gaim_account_get_connection(account); | |
1944 | |
1945 if (gc != NULL && gc->prpl != NULL) | |
1946 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(gc->prpl); | |
1947 | |
14607 | 1948 if (prpl_info != NULL && prpl_info->add_buddy != NULL) |
14192 | 1949 prpl_info->add_buddy(gc, buddy, gaim_buddy_get_group(buddy)); |
1950 } | |
1951 | |
1952 void | |
1953 gaim_account_add_buddies(GaimAccount *account, GList *buddies) | |
1954 { | |
1955 GaimPluginProtocolInfo *prpl_info = NULL; | |
1956 GaimConnection *gc = gaim_account_get_connection(account); | |
1957 | |
1958 if (gc != NULL && gc->prpl != NULL) | |
1959 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(gc->prpl); | |
1960 | |
14607 | 1961 if (prpl_info) { |
14192 | 1962 GList *cur, *groups = NULL; |
1963 | |
1964 /* Make a list of what group each buddy is in */ | |
1965 for (cur = buddies; cur != NULL; cur = cur->next) { | |
1966 GaimBlistNode *node = cur->data; | |
1967 groups = g_list_append(groups, node->parent->parent); | |
1968 } | |
1969 | |
1970 if (prpl_info->add_buddies != NULL) | |
1971 prpl_info->add_buddies(gc, buddies, groups); | |
1972 else if (prpl_info->add_buddy != NULL) { | |
1973 GList *curb = buddies, *curg = groups; | |
1974 | |
1975 while ((curb != NULL) && (curg != NULL)) { | |
1976 prpl_info->add_buddy(gc, curb->data, curg->data); | |
1977 curb = curb->next; | |
1978 curg = curg->next; | |
1979 } | |
1980 } | |
1981 | |
1982 g_list_free(groups); | |
1983 } | |
1984 } | |
1985 | |
1986 void | |
1987 gaim_account_remove_buddy(GaimAccount *account, GaimBuddy *buddy, | |
1988 GaimGroup *group) | |
1989 { | |
1990 GaimPluginProtocolInfo *prpl_info = NULL; | |
1991 GaimConnection *gc = gaim_account_get_connection(account); | |
1992 | |
1993 if (gc != NULL && gc->prpl != NULL) | |
1994 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(gc->prpl); | |
1995 | |
14607 | 1996 if (prpl_info && prpl_info->remove_buddy) |
14192 | 1997 prpl_info->remove_buddy(gc, buddy, group); |
1998 } | |
1999 | |
2000 void | |
2001 gaim_account_remove_buddies(GaimAccount *account, GList *buddies, GList *groups) | |
2002 { | |
2003 GaimPluginProtocolInfo *prpl_info = NULL; | |
2004 GaimConnection *gc = gaim_account_get_connection(account); | |
2005 | |
2006 if (gc != NULL && gc->prpl != NULL) | |
2007 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(gc->prpl); | |
2008 | |
14607 | 2009 if (prpl_info) { |
14192 | 2010 if (prpl_info->remove_buddies) |
2011 prpl_info->remove_buddies(gc, buddies, groups); | |
2012 else { | |
2013 GList *curb = buddies; | |
2014 GList *curg = groups; | |
2015 while ((curb != NULL) && (curg != NULL)) { | |
2016 gaim_account_remove_buddy(account, curb->data, curg->data); | |
2017 curb = curb->next; | |
2018 curg = curg->next; | |
2019 } | |
2020 } | |
2021 } | |
2022 } | |
2023 | |
2024 void | |
2025 gaim_account_remove_group(GaimAccount *account, GaimGroup *group) | |
2026 { | |
2027 GaimPluginProtocolInfo *prpl_info = NULL; | |
2028 GaimConnection *gc = gaim_account_get_connection(account); | |
2029 | |
2030 if (gc != NULL && gc->prpl != NULL) | |
2031 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(gc->prpl); | |
2032 | |
14607 | 2033 if (prpl_info && prpl_info->remove_group) |
14192 | 2034 prpl_info->remove_group(gc, group); |
2035 } | |
2036 | |
2037 void | |
2038 gaim_account_change_password(GaimAccount *account, const char *orig_pw, | |
2039 const char *new_pw) | |
2040 { | |
2041 GaimPluginProtocolInfo *prpl_info = NULL; | |
2042 GaimConnection *gc = gaim_account_get_connection(account); | |
2043 | |
2044 gaim_account_set_password(account, new_pw); | |
2045 | |
2046 if (gc != NULL && gc->prpl != NULL) | |
2047 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(gc->prpl); | |
2048 | |
14607 | 2049 if (prpl_info && prpl_info->change_passwd) |
14192 | 2050 prpl_info->change_passwd(gc, orig_pw, new_pw); |
2051 } | |
2052 | |
2053 gboolean gaim_account_supports_offline_message(GaimAccount *account, GaimBuddy *buddy) | |
2054 { | |
2055 GaimConnection *gc; | |
2056 GaimPluginProtocolInfo *prpl_info; | |
2057 | |
2058 g_return_val_if_fail(account, FALSE); | |
2059 g_return_val_if_fail(buddy, FALSE); | |
2060 | |
2061 gc = gaim_account_get_connection(account); | |
2062 if (gc == NULL) | |
2063 return FALSE; | |
2064 | |
2065 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(gc->prpl); | |
2066 | |
2067 if (!prpl_info || !prpl_info->offline_message) | |
2068 return FALSE; | |
2069 return prpl_info->offline_message(buddy); | |
2070 } | |
2071 | |
2072 void | |
2073 gaim_accounts_add(GaimAccount *account) | |
2074 { | |
2075 g_return_if_fail(account != NULL); | |
2076 | |
2077 if (g_list_find(accounts, account) != NULL) | |
2078 return; | |
2079 | |
2080 accounts = g_list_append(accounts, account); | |
2081 | |
2082 schedule_accounts_save(); | |
2083 | |
2084 gaim_signal_emit(gaim_accounts_get_handle(), "account-added", account); | |
2085 } | |
2086 | |
2087 void | |
2088 gaim_accounts_remove(GaimAccount *account) | |
2089 { | |
2090 g_return_if_fail(account != NULL); | |
2091 | |
2092 accounts = g_list_remove(accounts, account); | |
2093 | |
2094 schedule_accounts_save(); | |
2095 | |
2096 gaim_signal_emit(gaim_accounts_get_handle(), "account-removed", account); | |
2097 } | |
2098 | |
2099 void | |
2100 gaim_accounts_delete(GaimAccount *account) | |
2101 { | |
2102 GaimBlistNode *gnode, *cnode, *bnode; | |
2103 | |
2104 g_return_if_fail(account != NULL); | |
2105 | |
2106 if (gaim_account_is_connected(account)) | |
2107 gaim_account_disconnect(account); | |
2108 | |
2109 gaim_notify_close_with_handle(account); | |
2110 gaim_request_close_with_handle(account); | |
2111 | |
2112 gaim_accounts_remove(account); | |
2113 | |
2114 /* Remove this account's buddies */ | |
2115 for (gnode = gaim_get_blist()->root; gnode != NULL; gnode = gnode->next) { | |
2116 if (!GAIM_BLIST_NODE_IS_GROUP(gnode)) | |
2117 continue; | |
2118 | |
2119 cnode = gnode->child; | |
2120 while (cnode) { | |
2121 GaimBlistNode *cnode_next = cnode->next; | |
2122 | |
2123 if(GAIM_BLIST_NODE_IS_CONTACT(cnode)) { | |
2124 bnode = cnode->child; | |
2125 while (bnode) { | |
2126 GaimBlistNode *bnode_next = bnode->next; | |
2127 | |
2128 if (GAIM_BLIST_NODE_IS_BUDDY(bnode)) { | |
2129 GaimBuddy *b = (GaimBuddy *)bnode; | |
2130 | |
2131 if (b->account == account) | |
2132 gaim_blist_remove_buddy(b); | |
2133 } | |
2134 bnode = bnode_next; | |
2135 } | |
2136 } else if (GAIM_BLIST_NODE_IS_CHAT(cnode)) { | |
2137 GaimChat *c = (GaimChat *)cnode; | |
2138 | |
2139 if (c->account == account) | |
2140 gaim_blist_remove_chat(c); | |
2141 } | |
2142 cnode = cnode_next; | |
2143 } | |
2144 } | |
2145 | |
2146 /* Remove this account's pounces */ | |
2147 gaim_pounce_destroy_all_by_account(account); | |
2148 | |
2149 /* This will cause the deletion of an old buddy icon. */ | |
2150 gaim_account_set_buddy_icon(account, NULL); | |
2151 | |
2152 gaim_account_destroy(account); | |
2153 } | |
2154 | |
2155 void | |
2156 gaim_accounts_reorder(GaimAccount *account, gint new_index) | |
2157 { | |
2158 gint index; | |
2159 GList *l; | |
2160 | |
2161 g_return_if_fail(account != NULL); | |
2162 g_return_if_fail(new_index <= g_list_length(accounts)); | |
2163 | |
2164 index = g_list_index(accounts, account); | |
2165 | |
2166 if (index == -1) { | |
2167 gaim_debug_error("account", | |
2168 "Unregistered account (%s) discovered during reorder!\n", | |
2169 gaim_account_get_username(account)); | |
2170 return; | |
2171 } | |
2172 | |
2173 l = g_list_nth(accounts, index); | |
2174 | |
2175 if (new_index > index) | |
2176 new_index--; | |
2177 | |
2178 /* Remove the old one. */ | |
2179 accounts = g_list_delete_link(accounts, l); | |
2180 | |
2181 /* Insert it where it should go. */ | |
2182 accounts = g_list_insert(accounts, account, new_index); | |
2183 | |
2184 schedule_accounts_save(); | |
2185 } | |
2186 | |
2187 GList * | |
2188 gaim_accounts_get_all(void) | |
2189 { | |
2190 return accounts; | |
2191 } | |
2192 | |
2193 GList * | |
2194 gaim_accounts_get_all_active(void) | |
2195 { | |
2196 GList *list = NULL; | |
2197 GList *all = gaim_accounts_get_all(); | |
2198 | |
2199 while (all != NULL) { | |
2200 GaimAccount *account = all->data; | |
2201 | |
2202 if (gaim_account_get_enabled(account, gaim_core_get_ui())) | |
2203 list = g_list_append(list, account); | |
2204 | |
2205 all = all->next; | |
2206 } | |
2207 | |
2208 return list; | |
2209 } | |
2210 | |
2211 GaimAccount * | |
2212 gaim_accounts_find(const char *name, const char *protocol_id) | |
2213 { | |
2214 GaimAccount *account = NULL; | |
2215 GList *l; | |
2216 char *who; | |
2217 | |
2218 g_return_val_if_fail(name != NULL, NULL); | |
2219 | |
2220 who = g_strdup(gaim_normalize(NULL, name)); | |
2221 | |
2222 for (l = gaim_accounts_get_all(); l != NULL; l = l->next) { | |
2223 account = (GaimAccount *)l->data; | |
2224 | |
2225 if (!strcmp(gaim_normalize(NULL, gaim_account_get_username(account)), who) && | |
2226 (!protocol_id || !strcmp(account->protocol_id, protocol_id))) { | |
2227 | |
2228 break; | |
2229 } | |
2230 | |
2231 account = NULL; | |
2232 } | |
2233 | |
2234 g_free(who); | |
2235 | |
2236 return account; | |
2237 } | |
2238 | |
2239 void | |
2240 gaim_accounts_restore_current_statuses() | |
2241 { | |
2242 GList *l; | |
2243 GaimAccount *account; | |
2244 | |
2245 for (l = gaim_accounts_get_all(); l != NULL; l = l->next) | |
2246 { | |
2247 account = (GaimAccount *)l->data; | |
2248 if (gaim_account_get_enabled(account, gaim_core_get_ui()) && | |
2249 (gaim_presence_is_online(account->presence))) | |
2250 { | |
2251 gaim_account_connect(account); | |
2252 } | |
2253 } | |
2254 } | |
2255 | |
2256 void | |
2257 gaim_accounts_set_ui_ops(GaimAccountUiOps *ops) | |
2258 { | |
2259 account_ui_ops = ops; | |
2260 } | |
2261 | |
2262 GaimAccountUiOps * | |
2263 gaim_accounts_get_ui_ops(void) | |
2264 { | |
2265 return account_ui_ops; | |
2266 } | |
2267 | |
2268 void * | |
2269 gaim_accounts_get_handle(void) | |
2270 { | |
2271 static int handle; | |
2272 | |
2273 return &handle; | |
2274 } | |
2275 | |
2276 void | |
2277 gaim_accounts_init(void) | |
2278 { | |
2279 void *handle = gaim_accounts_get_handle(); | |
2280 | |
2281 gaim_signal_register(handle, "account-connecting", | |
2282 gaim_marshal_VOID__POINTER, NULL, 1, | |
2283 gaim_value_new(GAIM_TYPE_SUBTYPE, | |
2284 GAIM_SUBTYPE_ACCOUNT)); | |
2285 | |
2286 gaim_signal_register(handle, "account-disabled", | |
2287 gaim_marshal_VOID__POINTER, NULL, 1, | |
2288 gaim_value_new(GAIM_TYPE_SUBTYPE, | |
2289 GAIM_SUBTYPE_ACCOUNT)); | |
2290 | |
2291 gaim_signal_register(handle, "account-enabled", | |
2292 gaim_marshal_VOID__POINTER, NULL, 1, | |
2293 gaim_value_new(GAIM_TYPE_SUBTYPE, | |
2294 GAIM_SUBTYPE_ACCOUNT)); | |
2295 | |
2296 gaim_signal_register(handle, "account-setting-info", | |
2297 gaim_marshal_VOID__POINTER_POINTER, NULL, 2, | |
2298 gaim_value_new(GAIM_TYPE_SUBTYPE, | |
2299 GAIM_SUBTYPE_ACCOUNT), | |
2300 gaim_value_new(GAIM_TYPE_STRING)); | |
2301 | |
2302 gaim_signal_register(handle, "account-set-info", | |
2303 gaim_marshal_VOID__POINTER_POINTER, NULL, 2, | |
2304 gaim_value_new(GAIM_TYPE_SUBTYPE, | |
2305 GAIM_SUBTYPE_ACCOUNT), | |
2306 gaim_value_new(GAIM_TYPE_STRING)); | |
2307 | |
2308 gaim_signal_register(handle, "account-added", | |
2309 gaim_marshal_VOID__POINTER, NULL, 1, | |
2310 gaim_value_new(GAIM_TYPE_SUBTYPE, GAIM_SUBTYPE_ACCOUNT)); | |
2311 | |
2312 gaim_signal_register(handle, "account-removed", | |
2313 gaim_marshal_VOID__POINTER, NULL, 1, | |
2314 gaim_value_new(GAIM_TYPE_SUBTYPE, GAIM_SUBTYPE_ACCOUNT)); | |
2315 | |
2316 gaim_signal_register(handle, "account-status-changed", | |
2317 gaim_marshal_VOID__POINTER_POINTER_POINTER, NULL, 3, | |
2318 gaim_value_new(GAIM_TYPE_SUBTYPE, | |
2319 GAIM_SUBTYPE_ACCOUNT), | |
2320 gaim_value_new(GAIM_TYPE_SUBTYPE, | |
2321 GAIM_SUBTYPE_STATUS), | |
2322 gaim_value_new(GAIM_TYPE_SUBTYPE, | |
2323 GAIM_SUBTYPE_STATUS)); | |
2324 | |
2325 gaim_signal_register(handle, "account-alias-changed", | |
2326 gaim_marshal_VOID__POINTER_POINTER, NULL, 2, | |
2327 gaim_value_new(GAIM_TYPE_SUBTYPE, | |
2328 GAIM_SUBTYPE_ACCOUNT), | |
2329 gaim_value_new(GAIM_TYPE_STRING)); | |
2330 | |
2331 load_accounts(); | |
2332 | |
2333 } | |
2334 | |
2335 void | |
2336 gaim_accounts_uninit(void) | |
2337 { | |
2338 if (save_timer != 0) | |
2339 { | |
2340 gaim_timeout_remove(save_timer); | |
2341 save_timer = 0; | |
2342 sync_accounts(); | |
2343 } | |
2344 | |
2345 gaim_signals_unregister_by_instance(gaim_accounts_get_handle()); | |
2346 } |