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