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