Mercurial > pidgin
annotate libgaim/account.c @ 15354:db11d9e416ac
[gaim-migrate @ 18147]
when auto-growing a conversation entry to fit text without scrollbars, reset to the
default size once the message is sent
committer: Tailor Script <tailor@pidgin.im>
author | Sean Egan <seanegan@gmail.com> |
---|---|
date | Wed, 17 Jan 2007 09:47:55 +0000 |
parents | d1014e749b4e |
children | a6aad36ca735 |
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 | |
700 ret = gaim_account_new(name, protocol_id); | |
701 g_free(name); | |
702 g_free(protocol_id); | |
703 | |
704 /* Read the password */ | |
705 child = xmlnode_get_child(node, "password"); | |
706 if ((child != NULL) && ((data = xmlnode_get_data(child)) != NULL)) | |
707 { | |
708 gaim_account_set_remember_password(ret, TRUE); | |
709 gaim_account_set_password(ret, data); | |
710 g_free(data); | |
711 } | |
712 | |
713 /* Read the alias */ | |
714 child = xmlnode_get_child(node, "alias"); | |
715 if ((child != NULL) && ((data = xmlnode_get_data(child)) != NULL)) | |
716 { | |
717 if (*data != '\0') | |
718 gaim_account_set_alias(ret, data); | |
719 g_free(data); | |
720 } | |
721 | |
722 /* Read the statuses */ | |
723 child = xmlnode_get_child(node, "statuses"); | |
724 if (child != NULL) | |
725 { | |
726 parse_statuses(child, ret); | |
727 } | |
728 | |
729 /* Read the userinfo */ | |
730 child = xmlnode_get_child(node, "userinfo"); | |
731 if ((child != NULL) && ((data = xmlnode_get_data(child)) != NULL)) | |
732 { | |
733 gaim_account_set_user_info(ret, data); | |
734 g_free(data); | |
735 } | |
736 | |
737 /* Read the buddyicon */ | |
738 child = xmlnode_get_child(node, "buddyicon"); | |
739 if ((child != NULL) && ((data = xmlnode_get_data(child)) != NULL)) | |
740 { | |
741 gaim_account_set_buddy_icon(ret, data); | |
742 g_free(data); | |
743 } | |
744 | |
745 /* Read settings (both core and UI) */ | |
746 for (child = xmlnode_get_child(node, "settings"); child != NULL; | |
747 child = xmlnode_get_next_twin(child)) | |
748 { | |
749 parse_settings(child, ret); | |
750 } | |
751 | |
752 /* Read proxy */ | |
753 child = xmlnode_get_child(node, "proxy"); | |
754 if (child != NULL) | |
755 { | |
756 parse_proxy_info(child, ret); | |
757 } | |
758 | |
759 return ret; | |
760 } | |
761 | |
762 static void | |
763 load_accounts(void) | |
764 { | |
765 xmlnode *node, *child; | |
766 | |
767 accounts_loaded = TRUE; | |
768 | |
769 node = gaim_util_read_xml_from_file("accounts.xml", _("accounts")); | |
770 | |
771 if (node == NULL) | |
772 return; | |
773 | |
774 for (child = xmlnode_get_child(node, "account"); child != NULL; | |
775 child = xmlnode_get_next_twin(child)) | |
776 { | |
777 GaimAccount *new_acct; | |
778 new_acct = parse_account(child); | |
779 gaim_accounts_add(new_acct); | |
780 } | |
781 | |
782 xmlnode_free(node); | |
783 } | |
784 | |
785 | |
786 static void | |
787 delete_setting(void *data) | |
788 { | |
789 GaimAccountSetting *setting = (GaimAccountSetting *)data; | |
790 | |
791 g_free(setting->ui); | |
792 | |
793 if (setting->type == GAIM_PREF_STRING) | |
794 g_free(setting->value.string); | |
795 | |
796 g_free(setting); | |
797 } | |
798 | |
799 GaimAccount * | |
800 gaim_account_new(const char *username, const char *protocol_id) | |
801 { | |
802 GaimAccount *account = NULL; | |
803 GaimPlugin *prpl = NULL; | |
804 GaimPluginProtocolInfo *prpl_info = NULL; | |
805 GaimStatusType *status_type; | |
806 | |
807 g_return_val_if_fail(username != NULL, NULL); | |
808 g_return_val_if_fail(protocol_id != NULL, NULL); | |
809 | |
810 account = gaim_accounts_find(username, protocol_id); | |
811 | |
812 if (account != NULL) | |
813 return account; | |
814 | |
815 account = g_new0(GaimAccount, 1); | |
816 GAIM_DBUS_REGISTER_POINTER(account, GaimAccount); | |
817 | |
818 gaim_account_set_username(account, username); | |
819 | |
820 gaim_account_set_protocol_id(account, protocol_id); | |
821 | |
822 account->settings = g_hash_table_new_full(g_str_hash, g_str_equal, | |
823 g_free, delete_setting); | |
824 account->ui_settings = g_hash_table_new_full(g_str_hash, g_str_equal, | |
825 g_free, (GDestroyNotify)g_hash_table_destroy); | |
826 account->system_log = NULL; | |
827 /* 0 is not a valid privacy setting */ | |
828 account->perm_deny = GAIM_PRIVACY_ALLOW_ALL; | |
829 | |
830 account->presence = gaim_presence_new_for_account(account); | |
831 | |
832 prpl = gaim_find_prpl(protocol_id); | |
833 | |
834 if (prpl == NULL) | |
835 return account; | |
836 | |
837 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(prpl); | |
838 if (prpl_info != NULL && prpl_info->status_types != NULL) | |
839 gaim_account_set_status_types(account, prpl_info->status_types(account)); | |
840 | |
841 status_type = gaim_account_get_status_type_with_primitive(account, GAIM_STATUS_AVAILABLE); | |
842 if (status_type != NULL) | |
843 gaim_presence_set_status_active(account->presence, | |
844 gaim_status_type_get_id(status_type), | |
845 TRUE); | |
846 else | |
847 gaim_presence_set_status_active(account->presence, | |
848 "offline", | |
849 TRUE); | |
850 | |
851 return account; | |
852 } | |
853 | |
854 void | |
855 gaim_account_destroy(GaimAccount *account) | |
856 { | |
857 GList *l; | |
858 | |
859 g_return_if_fail(account != NULL); | |
860 | |
861 gaim_debug_info("account", "Destroying account %p\n", account); | |
862 | |
863 for (l = gaim_get_conversations(); l != NULL; l = l->next) | |
864 { | |
865 GaimConversation *conv = (GaimConversation *)l->data; | |
866 | |
867 if (gaim_conversation_get_account(conv) == account) | |
868 gaim_conversation_set_account(conv, NULL); | |
869 } | |
870 | |
871 g_free(account->username); | |
872 g_free(account->alias); | |
873 g_free(account->password); | |
874 g_free(account->user_info); | |
15070 | 875 g_free(account->buddy_icon); |
876 g_free(account->buddy_icon_path); | |
14192 | 877 g_free(account->protocol_id); |
878 | |
879 g_hash_table_destroy(account->settings); | |
880 g_hash_table_destroy(account->ui_settings); | |
881 | |
882 gaim_account_set_status_types(account, NULL); | |
883 | |
884 gaim_presence_destroy(account->presence); | |
885 | |
886 if(account->system_log) | |
887 gaim_log_free(account->system_log); | |
888 | |
889 GAIM_DBUS_UNREGISTER_POINTER(account); | |
890 g_free(account); | |
891 } | |
892 | |
893 void | |
894 gaim_account_register(GaimAccount *account) | |
895 { | |
896 g_return_if_fail(account != NULL); | |
897 | |
898 gaim_debug_info("account", "Registering account %s\n", | |
899 gaim_account_get_username(account)); | |
900 | |
15151
d1014e749b4e
[gaim-migrate @ 17937]
Evan Schoenberg <evan.s@dreskin.net>
parents:
15135
diff
changeset
|
901 gaim_connection_new(account, TRUE, gaim_account_get_password(account)); |
14192 | 902 } |
903 | |
904 static void | |
905 request_password_ok_cb(GaimAccount *account, GaimRequestFields *fields) | |
906 { | |
907 const char *entry; | |
908 gboolean remember; | |
909 | |
910 entry = gaim_request_fields_get_string(fields, "password"); | |
911 remember = gaim_request_fields_get_bool(fields, "remember"); | |
912 | |
913 if (!entry || !*entry) | |
914 { | |
915 gaim_notify_error(account, NULL, _("Password is required to sign on."), NULL); | |
916 return; | |
917 } | |
918 | |
919 if(remember) | |
920 gaim_account_set_remember_password(account, TRUE); | |
921 | |
922 gaim_account_set_password(account, entry); | |
923 | |
924 gaim_connection_new(account, FALSE, entry); | |
925 } | |
926 | |
927 static void | |
928 request_password(GaimAccount *account) | |
929 { | |
930 gchar *primary; | |
931 const gchar *username; | |
932 GaimRequestFieldGroup *group; | |
933 GaimRequestField *field; | |
934 GaimRequestFields *fields; | |
935 | |
936 /* Close any previous password request windows */ | |
937 gaim_request_close_with_handle(account); | |
938 | |
939 username = gaim_account_get_username(account); | |
940 primary = g_strdup_printf(_("Enter password for %s (%s)"), username, | |
941 gaim_account_get_protocol_name(account)); | |
942 | |
943 fields = gaim_request_fields_new(); | |
944 group = gaim_request_field_group_new(NULL); | |
945 gaim_request_fields_add_group(fields, group); | |
946 | |
947 field = gaim_request_field_string_new("password", _("Enter Password"), NULL, FALSE); | |
948 gaim_request_field_string_set_masked(field, TRUE); | |
949 gaim_request_field_set_required(field, TRUE); | |
950 gaim_request_field_group_add_field(group, field); | |
951 | |
952 field = gaim_request_field_bool_new("remember", _("Save password"), FALSE); | |
953 gaim_request_field_group_add_field(group, field); | |
954 | |
955 gaim_request_fields(account, | |
956 NULL, | |
957 primary, | |
958 NULL, | |
959 fields, | |
960 _("OK"), G_CALLBACK(request_password_ok_cb), | |
961 _("Cancel"), NULL, | |
962 account); | |
963 g_free(primary); | |
964 } | |
965 | |
966 void | |
967 gaim_account_connect(GaimAccount *account) | |
968 { | |
969 GaimPlugin *prpl; | |
970 GaimPluginProtocolInfo *prpl_info; | |
971 const char *password; | |
972 | |
973 g_return_if_fail(account != NULL); | |
974 | |
975 gaim_debug_info("account", "Connecting to account %s\n", | |
976 gaim_account_get_username(account)); | |
977 | |
978 if (!gaim_account_get_enabled(account, gaim_core_get_ui())) | |
979 return; | |
980 | |
981 prpl = gaim_find_prpl(gaim_account_get_protocol_id(account)); | |
982 if (prpl == NULL) | |
983 { | |
984 gchar *message; | |
985 | |
986 message = g_strdup_printf(_("Missing protocol plugin for %s"), | |
987 gaim_account_get_username(account)); | |
988 gaim_notify_error(account, _("Connection Error"), message, NULL); | |
989 g_free(message); | |
990 return; | |
991 } | |
992 | |
993 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(prpl); | |
994 password = gaim_account_get_password(account); | |
995 if ((password == NULL) && | |
996 !(prpl_info->options & OPT_PROTO_NO_PASSWORD) && | |
997 !(prpl_info->options & OPT_PROTO_PASSWORD_OPTIONAL)) | |
998 request_password(account); | |
999 else | |
1000 gaim_connection_new(account, FALSE, password); | |
1001 } | |
1002 | |
1003 void | |
1004 gaim_account_disconnect(GaimAccount *account) | |
1005 { | |
1006 GaimConnection *gc; | |
1007 | |
1008 g_return_if_fail(account != NULL); | |
1009 g_return_if_fail(!gaim_account_is_disconnected(account)); | |
1010 | |
1011 gaim_debug_info("account", "Disconnecting account %p\n", account); | |
1012 | |
1013 account->disconnecting = TRUE; | |
1014 | |
1015 gc = gaim_account_get_connection(account); | |
1016 gaim_connection_destroy(gc); | |
1017 if (!gaim_account_get_remember_password(account)) | |
14979
c157efddc62a
[gaim-migrate @ 17758]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
14968
diff
changeset
|
1018 gaim_account_set_password(account, NULL); |
14192 | 1019 gaim_account_set_connection(account, NULL); |
1020 | |
1021 account->disconnecting = FALSE; | |
1022 } | |
1023 | |
1024 void | |
1025 gaim_account_notify_added(GaimAccount *account, const char *remote_user, | |
1026 const char *id, const char *alias, | |
1027 const char *message) | |
1028 { | |
1029 GaimAccountUiOps *ui_ops; | |
1030 | |
1031 g_return_if_fail(account != NULL); | |
1032 g_return_if_fail(remote_user != NULL); | |
1033 | |
1034 ui_ops = gaim_accounts_get_ui_ops(); | |
1035 | |
1036 if (ui_ops != NULL && ui_ops->notify_added != NULL) | |
1037 ui_ops->notify_added(account, remote_user, id, alias, message); | |
1038 } | |
1039 | |
1040 void | |
1041 gaim_account_request_add(GaimAccount *account, const char *remote_user, | |
1042 const char *id, const char *alias, | |
1043 const char *message) | |
1044 { | |
1045 GaimAccountUiOps *ui_ops; | |
1046 | |
1047 g_return_if_fail(account != NULL); | |
1048 g_return_if_fail(remote_user != NULL); | |
1049 | |
1050 ui_ops = gaim_accounts_get_ui_ops(); | |
1051 | |
1052 if (ui_ops != NULL && ui_ops->request_add != NULL) | |
1053 ui_ops->request_add(account, remote_user, id, alias, message); | |
1054 } | |
1055 | |
15057 | 1056 void |
1057 gaim_account_request_authorization(GaimAccount *account, const char *remote_user, | |
15135 | 1058 const char *id, const char *alias, const char *message, gboolean on_list, |
15057 | 1059 GCallback auth_cb, GCallback deny_cb, void *user_data) |
1060 { | |
1061 GaimAccountUiOps *ui_ops; | |
1062 | |
1063 g_return_if_fail(account != NULL); | |
1064 g_return_if_fail(remote_user != NULL); | |
1065 | |
1066 ui_ops = gaim_accounts_get_ui_ops(); | |
1067 | |
1068 if (ui_ops != NULL && ui_ops->request_authorize != NULL) | |
15135 | 1069 ui_ops->request_authorize(account, remote_user, id, alias, message, on_list, auth_cb, deny_cb, user_data); |
15057 | 1070 |
1071 } | |
1072 | |
14192 | 1073 static void |
1074 change_password_cb(GaimAccount *account, GaimRequestFields *fields) | |
1075 { | |
1076 const char *orig_pass, *new_pass_1, *new_pass_2; | |
1077 | |
1078 orig_pass = gaim_request_fields_get_string(fields, "password"); | |
1079 new_pass_1 = gaim_request_fields_get_string(fields, "new_password_1"); | |
1080 new_pass_2 = gaim_request_fields_get_string(fields, "new_password_2"); | |
1081 | |
1082 if (g_utf8_collate(new_pass_1, new_pass_2)) | |
1083 { | |
1084 gaim_notify_error(account, NULL, | |
1085 _("New passwords do not match."), NULL); | |
1086 | |
1087 return; | |
1088 } | |
1089 | |
1090 if (orig_pass == NULL || new_pass_1 == NULL || new_pass_2 == NULL || | |
1091 *orig_pass == '\0' || *new_pass_1 == '\0' || *new_pass_2 == '\0') | |
1092 { | |
1093 gaim_notify_error(account, NULL, | |
1094 _("Fill out all fields completely."), NULL); | |
1095 return; | |
1096 } | |
1097 | |
1098 gaim_account_change_password(account, orig_pass, new_pass_1); | |
1099 } | |
1100 | |
1101 void | |
1102 gaim_account_request_change_password(GaimAccount *account) | |
1103 { | |
1104 GaimRequestFields *fields; | |
1105 GaimRequestFieldGroup *group; | |
1106 GaimRequestField *field; | |
1107 char primary[256]; | |
1108 | |
1109 g_return_if_fail(account != NULL); | |
1110 g_return_if_fail(gaim_account_is_connected(account)); | |
1111 | |
1112 fields = gaim_request_fields_new(); | |
1113 | |
1114 group = gaim_request_field_group_new(NULL); | |
1115 gaim_request_fields_add_group(fields, group); | |
1116 | |
1117 field = gaim_request_field_string_new("password", _("Original password"), | |
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 field = gaim_request_field_string_new("new_password_1", | |
1124 _("New password"), | |
1125 NULL, FALSE); | |
1126 gaim_request_field_string_set_masked(field, TRUE); | |
1127 gaim_request_field_set_required(field, TRUE); | |
1128 gaim_request_field_group_add_field(group, field); | |
1129 | |
1130 field = gaim_request_field_string_new("new_password_2", | |
1131 _("New password (again)"), | |
1132 NULL, FALSE); | |
1133 gaim_request_field_string_set_masked(field, TRUE); | |
1134 gaim_request_field_set_required(field, TRUE); | |
1135 gaim_request_field_group_add_field(group, field); | |
1136 | |
1137 g_snprintf(primary, sizeof(primary), _("Change password for %s"), | |
1138 gaim_account_get_username(account)); | |
1139 | |
1140 /* I'm sticking this somewhere in the code: bologna */ | |
1141 | |
1142 gaim_request_fields(gaim_account_get_connection(account), | |
1143 NULL, | |
1144 primary, | |
1145 _("Please enter your current password and your " | |
1146 "new password."), | |
1147 fields, | |
1148 _("OK"), G_CALLBACK(change_password_cb), | |
1149 _("Cancel"), NULL, | |
1150 account); | |
1151 } | |
1152 | |
1153 static void | |
1154 set_user_info_cb(GaimAccount *account, const char *user_info) | |
1155 { | |
1156 GaimConnection *gc; | |
1157 | |
1158 gaim_account_set_user_info(account, user_info); | |
1159 gc = gaim_account_get_connection(account); | |
14607 | 1160 serv_set_info(gc, user_info); |
14192 | 1161 } |
1162 | |
1163 void | |
1164 gaim_account_request_change_user_info(GaimAccount *account) | |
1165 { | |
1166 GaimConnection *gc; | |
1167 char primary[256]; | |
1168 | |
1169 g_return_if_fail(account != NULL); | |
1170 g_return_if_fail(gaim_account_is_connected(account)); | |
1171 | |
1172 gc = gaim_account_get_connection(account); | |
1173 | |
1174 g_snprintf(primary, sizeof(primary), | |
1175 _("Change user information for %s"), | |
1176 gaim_account_get_username(account)); | |
1177 | |
1178 gaim_request_input(gc, _("Set User Info"), primary, NULL, | |
1179 gaim_account_get_user_info(account), | |
1180 TRUE, FALSE, ((gc != NULL) && | |
1181 (gc->flags & GAIM_CONNECTION_HTML) ? "html" : NULL), | |
1182 _("Save"), G_CALLBACK(set_user_info_cb), | |
1183 _("Cancel"), NULL, account); | |
1184 } | |
1185 | |
1186 void | |
1187 gaim_account_set_username(GaimAccount *account, const char *username) | |
1188 { | |
1189 g_return_if_fail(account != NULL); | |
1190 | |
1191 g_free(account->username); | |
1192 account->username = g_strdup(username); | |
1193 | |
1194 schedule_accounts_save(); | |
15101 | 1195 |
1196 /* if the name changes, we should re-write the buddy list | |
1197 * to disk with the new name */ | |
1198 gaim_blist_schedule_save(); | |
14192 | 1199 } |
1200 | |
1201 void | |
1202 gaim_account_set_password(GaimAccount *account, const char *password) | |
1203 { | |
1204 g_return_if_fail(account != NULL); | |
1205 | |
1206 g_free(account->password); | |
1207 account->password = g_strdup(password); | |
1208 | |
1209 schedule_accounts_save(); | |
1210 } | |
1211 | |
1212 void | |
1213 gaim_account_set_alias(GaimAccount *account, const char *alias) | |
1214 { | |
1215 g_return_if_fail(account != NULL); | |
1216 | |
1217 /* | |
1218 * Do nothing if alias and account->alias are both NULL. Or if | |
1219 * they're the exact same string. | |
1220 */ | |
1221 if (alias == account->alias) | |
1222 return; | |
1223 | |
1224 if ((!alias && account->alias) || (alias && !account->alias) || | |
1225 g_utf8_collate(account->alias, alias)) | |
1226 { | |
1227 char *old = account->alias; | |
1228 | |
1229 account->alias = g_strdup(alias); | |
1230 gaim_signal_emit(gaim_accounts_get_handle(), "account-alias-changed", | |
1231 account, old); | |
1232 g_free(old); | |
1233 | |
1234 schedule_accounts_save(); | |
1235 } | |
1236 } | |
1237 | |
1238 void | |
1239 gaim_account_set_user_info(GaimAccount *account, const char *user_info) | |
1240 { | |
1241 g_return_if_fail(account != NULL); | |
1242 | |
1243 g_free(account->user_info); | |
1244 account->user_info = g_strdup(user_info); | |
1245 | |
1246 schedule_accounts_save(); | |
1247 } | |
1248 | |
1249 void | |
1250 gaim_account_set_buddy_icon(GaimAccount *account, const char *icon) | |
1251 { | |
1252 g_return_if_fail(account != NULL); | |
1253 | |
1254 /* Delete an existing icon from the cache. */ | |
1255 if (account->buddy_icon != NULL && (icon == NULL || strcmp(account->buddy_icon, icon))) | |
1256 { | |
1257 const char *dirname = gaim_buddy_icons_get_cache_dir(); | |
15055 | 1258 |
1259 if (g_file_test(account->buddy_icon, G_FILE_TEST_IS_REGULAR)) | |
14192 | 1260 { |
1261 /* The file exists. This is a full path. */ | |
1262 | |
1263 /* XXX: This is a hack so we only delete the file if it's | |
1264 * in the cache dir. Otherwise, people who upgrade (who | |
1265 * may have buddy icon filenames set outside of the cache | |
1266 * dir) could lose files. */ | |
1267 if (!strncmp(dirname, account->buddy_icon, strlen(dirname))) | |
1268 g_unlink(account->buddy_icon); | |
1269 } | |
1270 else | |
1271 { | |
1272 char *filename = g_build_filename(dirname, account->buddy_icon, NULL); | |
1273 g_unlink(filename); | |
1274 g_free(filename); | |
1275 } | |
1276 } | |
1277 | |
1278 g_free(account->buddy_icon); | |
1279 account->buddy_icon = g_strdup(icon); | |
1280 if (gaim_account_is_connected(account)) | |
1281 { | |
14981 | 1282 GaimConnection *gc; |
1283 GaimPluginProtocolInfo *prpl_info; | |
1284 | |
1285 gc = gaim_account_get_connection(account); | |
1286 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(gc->prpl); | |
1287 | |
1288 if (prpl_info && prpl_info->set_buddy_icon) | |
1289 { | |
15070 | 1290 char *cached_path = gaim_buddy_icons_get_full_path(icon); |
1291 prpl_info->set_buddy_icon(gc, cached_path); | |
1292 g_free(cached_path); | |
14981 | 1293 } |
14192 | 1294 } |
1295 | |
1296 schedule_accounts_save(); | |
1297 } | |
1298 | |
15070 | 1299 void gaim_account_set_buddy_icon_path(GaimAccount *account, const char *path) |
1300 { | |
1301 g_return_if_fail(account != NULL); | |
1302 | |
1303 g_free(account->buddy_icon_path); | |
1304 account->buddy_icon_path = g_strdup(path); | |
1305 | |
1306 schedule_accounts_save(); | |
1307 } | |
1308 | |
14192 | 1309 void |
1310 gaim_account_set_protocol_id(GaimAccount *account, const char *protocol_id) | |
1311 { | |
1312 g_return_if_fail(account != NULL); | |
1313 g_return_if_fail(protocol_id != NULL); | |
1314 | |
1315 g_free(account->protocol_id); | |
1316 account->protocol_id = g_strdup(protocol_id); | |
1317 | |
1318 schedule_accounts_save(); | |
1319 } | |
1320 | |
1321 void | |
1322 gaim_account_set_connection(GaimAccount *account, GaimConnection *gc) | |
1323 { | |
1324 g_return_if_fail(account != NULL); | |
1325 | |
1326 account->gc = gc; | |
1327 } | |
1328 | |
1329 void | |
1330 gaim_account_set_remember_password(GaimAccount *account, gboolean value) | |
1331 { | |
1332 g_return_if_fail(account != NULL); | |
1333 | |
1334 account->remember_pass = value; | |
1335 | |
1336 schedule_accounts_save(); | |
1337 } | |
1338 | |
1339 void | |
1340 gaim_account_set_check_mail(GaimAccount *account, gboolean value) | |
1341 { | |
1342 g_return_if_fail(account != NULL); | |
1343 | |
1344 gaim_account_set_bool(account, "check-mail", value); | |
1345 } | |
1346 | |
1347 void | |
1348 gaim_account_set_enabled(GaimAccount *account, const char *ui, | |
1349 gboolean value) | |
1350 { | |
1351 GaimConnection *gc; | |
1352 gboolean was_enabled = FALSE; | |
1353 | |
1354 g_return_if_fail(account != NULL); | |
1355 g_return_if_fail(ui != NULL); | |
1356 | |
1357 was_enabled = gaim_account_get_enabled(account, ui); | |
1358 | |
1359 gaim_account_set_ui_bool(account, ui, "auto-login", value); | |
1360 gc = gaim_account_get_connection(account); | |
1361 | |
1362 if(was_enabled && !value) | |
1363 gaim_signal_emit(gaim_accounts_get_handle(), "account-disabled", account); | |
1364 else if(!was_enabled && value) | |
1365 gaim_signal_emit(gaim_accounts_get_handle(), "account-enabled", account); | |
1366 | |
1367 if ((gc != NULL) && (gc->wants_to_die == TRUE)) | |
1368 return; | |
1369 | |
1370 if (value && gaim_presence_is_online(account->presence)) | |
1371 gaim_account_connect(account); | |
1372 else if (!value && !gaim_account_is_disconnected(account)) | |
1373 gaim_account_disconnect(account); | |
1374 } | |
1375 | |
1376 void | |
1377 gaim_account_set_proxy_info(GaimAccount *account, GaimProxyInfo *info) | |
1378 { | |
1379 g_return_if_fail(account != NULL); | |
1380 | |
1381 if (account->proxy_info != NULL) | |
1382 gaim_proxy_info_destroy(account->proxy_info); | |
1383 | |
1384 account->proxy_info = info; | |
1385 | |
1386 schedule_accounts_save(); | |
1387 } | |
1388 | |
1389 void | |
1390 gaim_account_set_status_types(GaimAccount *account, GList *status_types) | |
1391 { | |
1392 g_return_if_fail(account != NULL); | |
1393 | |
14662
61a4a2dc491e
[gaim-migrate @ 17408]
Etan Reisner <pidgin@unreliablesource.net>
parents:
14643
diff
changeset
|
1394 /* Out with the old... */ |
14192 | 1395 if (account->status_types != NULL) |
1396 { | |
1397 g_list_foreach(account->status_types, (GFunc)gaim_status_type_destroy, NULL); | |
1398 g_list_free(account->status_types); | |
1399 } | |
1400 | |
1401 /* In with the new... */ | |
1402 account->status_types = status_types; | |
1403 } | |
1404 | |
1405 void | |
1406 gaim_account_set_status(GaimAccount *account, const char *status_id, | |
1407 gboolean active, ...) | |
1408 { | |
14643 | 1409 GList *attrs = NULL; |
1410 const gchar *id; | |
1411 gpointer data; | |
14192 | 1412 va_list args; |
1413 | |
1414 va_start(args, active); | |
14643 | 1415 while ((id = va_arg(args, const char *)) != NULL) |
14192 | 1416 { |
14643 | 1417 attrs = g_list_append(attrs, (char *)id); |
1418 data = va_arg(args, void *); | |
1419 attrs = g_list_append(attrs, data); | |
14192 | 1420 } |
1421 gaim_account_set_status_list(account, status_id, active, attrs); | |
1422 g_list_free(attrs); | |
14643 | 1423 va_end(args); |
14192 | 1424 } |
1425 | |
1426 void | |
1427 gaim_account_set_status_list(GaimAccount *account, const char *status_id, | |
1428 gboolean active, GList *attrs) | |
1429 { | |
1430 GaimStatus *status; | |
1431 | |
1432 g_return_if_fail(account != NULL); | |
1433 g_return_if_fail(status_id != NULL); | |
1434 | |
1435 status = gaim_account_get_status(account, status_id); | |
1436 if (status == NULL) | |
1437 { | |
1438 gaim_debug_error("account", | |
1439 "Invalid status ID %s for account %s (%s)\n", | |
1440 status_id, gaim_account_get_username(account), | |
1441 gaim_account_get_protocol_id(account)); | |
1442 return; | |
1443 } | |
1444 | |
1445 if (active || gaim_status_is_independent(status)) | |
1446 gaim_status_set_active_with_attrs_list(status, active, attrs); | |
1447 | |
1448 /* | |
1449 * Our current statuses are saved to accounts.xml (so that when we | |
1450 * reconnect, we go back to the previous status). | |
1451 */ | |
1452 schedule_accounts_save(); | |
1453 } | |
1454 | |
1455 void | |
1456 gaim_account_clear_settings(GaimAccount *account) | |
1457 { | |
1458 g_return_if_fail(account != NULL); | |
1459 | |
1460 g_hash_table_destroy(account->settings); | |
1461 | |
1462 account->settings = g_hash_table_new_full(g_str_hash, g_str_equal, | |
1463 g_free, delete_setting); | |
1464 } | |
1465 | |
1466 void | |
1467 gaim_account_set_int(GaimAccount *account, const char *name, int value) | |
1468 { | |
1469 GaimAccountSetting *setting; | |
1470 | |
1471 g_return_if_fail(account != NULL); | |
1472 g_return_if_fail(name != NULL); | |
1473 | |
1474 setting = g_new0(GaimAccountSetting, 1); | |
1475 | |
1476 setting->type = GAIM_PREF_INT; | |
1477 setting->value.integer = value; | |
1478 | |
1479 g_hash_table_insert(account->settings, g_strdup(name), setting); | |
1480 | |
1481 schedule_accounts_save(); | |
1482 } | |
1483 | |
1484 void | |
1485 gaim_account_set_string(GaimAccount *account, const char *name, | |
1486 const char *value) | |
1487 { | |
1488 GaimAccountSetting *setting; | |
1489 | |
1490 g_return_if_fail(account != NULL); | |
1491 g_return_if_fail(name != NULL); | |
1492 | |
1493 setting = g_new0(GaimAccountSetting, 1); | |
1494 | |
1495 setting->type = GAIM_PREF_STRING; | |
1496 setting->value.string = g_strdup(value); | |
1497 | |
1498 g_hash_table_insert(account->settings, g_strdup(name), setting); | |
1499 | |
1500 schedule_accounts_save(); | |
1501 } | |
1502 | |
1503 void | |
1504 gaim_account_set_bool(GaimAccount *account, const char *name, gboolean value) | |
1505 { | |
1506 GaimAccountSetting *setting; | |
1507 | |
1508 g_return_if_fail(account != NULL); | |
1509 g_return_if_fail(name != NULL); | |
1510 | |
1511 setting = g_new0(GaimAccountSetting, 1); | |
1512 | |
1513 setting->type = GAIM_PREF_BOOLEAN; | |
1514 setting->value.bool = value; | |
1515 | |
1516 g_hash_table_insert(account->settings, g_strdup(name), setting); | |
1517 | |
1518 schedule_accounts_save(); | |
1519 } | |
1520 | |
1521 static GHashTable * | |
1522 get_ui_settings_table(GaimAccount *account, const char *ui) | |
1523 { | |
1524 GHashTable *table; | |
1525 | |
1526 table = g_hash_table_lookup(account->ui_settings, ui); | |
1527 | |
1528 if (table == NULL) { | |
1529 table = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, | |
1530 delete_setting); | |
1531 g_hash_table_insert(account->ui_settings, g_strdup(ui), table); | |
1532 } | |
1533 | |
1534 return table; | |
1535 } | |
1536 | |
1537 void | |
1538 gaim_account_set_ui_int(GaimAccount *account, const char *ui, | |
1539 const char *name, int value) | |
1540 { | |
1541 GaimAccountSetting *setting; | |
1542 GHashTable *table; | |
1543 | |
1544 g_return_if_fail(account != NULL); | |
1545 g_return_if_fail(ui != NULL); | |
1546 g_return_if_fail(name != NULL); | |
1547 | |
1548 setting = g_new0(GaimAccountSetting, 1); | |
1549 | |
1550 setting->type = GAIM_PREF_INT; | |
1551 setting->ui = g_strdup(ui); | |
1552 setting->value.integer = value; | |
1553 | |
1554 table = get_ui_settings_table(account, ui); | |
1555 | |
1556 g_hash_table_insert(table, g_strdup(name), setting); | |
1557 | |
1558 schedule_accounts_save(); | |
1559 } | |
1560 | |
1561 void | |
1562 gaim_account_set_ui_string(GaimAccount *account, const char *ui, | |
1563 const char *name, const char *value) | |
1564 { | |
1565 GaimAccountSetting *setting; | |
1566 GHashTable *table; | |
1567 | |
1568 g_return_if_fail(account != NULL); | |
1569 g_return_if_fail(ui != NULL); | |
1570 g_return_if_fail(name != NULL); | |
1571 | |
1572 setting = g_new0(GaimAccountSetting, 1); | |
1573 | |
1574 setting->type = GAIM_PREF_STRING; | |
1575 setting->ui = g_strdup(ui); | |
1576 setting->value.string = g_strdup(value); | |
1577 | |
1578 table = get_ui_settings_table(account, ui); | |
1579 | |
1580 g_hash_table_insert(table, g_strdup(name), setting); | |
1581 | |
1582 schedule_accounts_save(); | |
1583 } | |
1584 | |
1585 void | |
1586 gaim_account_set_ui_bool(GaimAccount *account, const char *ui, | |
1587 const char *name, gboolean value) | |
1588 { | |
1589 GaimAccountSetting *setting; | |
1590 GHashTable *table; | |
1591 | |
1592 g_return_if_fail(account != NULL); | |
1593 g_return_if_fail(ui != NULL); | |
1594 g_return_if_fail(name != NULL); | |
1595 | |
1596 setting = g_new0(GaimAccountSetting, 1); | |
1597 | |
1598 setting->type = GAIM_PREF_BOOLEAN; | |
1599 setting->ui = g_strdup(ui); | |
1600 setting->value.bool = value; | |
1601 | |
1602 table = get_ui_settings_table(account, ui); | |
1603 | |
1604 g_hash_table_insert(table, g_strdup(name), setting); | |
1605 | |
1606 schedule_accounts_save(); | |
1607 } | |
1608 | |
1609 static GaimConnectionState | |
1610 gaim_account_get_state(const GaimAccount *account) | |
1611 { | |
1612 GaimConnection *gc; | |
1613 | |
1614 g_return_val_if_fail(account != NULL, GAIM_DISCONNECTED); | |
1615 | |
1616 gc = gaim_account_get_connection(account); | |
1617 if (!gc) | |
1618 return GAIM_DISCONNECTED; | |
1619 | |
1620 return gaim_connection_get_state(gc); | |
1621 } | |
1622 | |
1623 gboolean | |
1624 gaim_account_is_connected(const GaimAccount *account) | |
1625 { | |
1626 return (gaim_account_get_state(account) == GAIM_CONNECTED); | |
1627 } | |
1628 | |
1629 gboolean | |
1630 gaim_account_is_connecting(const GaimAccount *account) | |
1631 { | |
1632 return (gaim_account_get_state(account) == GAIM_CONNECTING); | |
1633 } | |
1634 | |
1635 gboolean | |
1636 gaim_account_is_disconnected(const GaimAccount *account) | |
1637 { | |
1638 return (gaim_account_get_state(account) == GAIM_DISCONNECTED); | |
1639 } | |
1640 | |
1641 const char * | |
1642 gaim_account_get_username(const GaimAccount *account) | |
1643 { | |
1644 g_return_val_if_fail(account != NULL, NULL); | |
1645 | |
1646 return account->username; | |
1647 } | |
1648 | |
1649 const char * | |
1650 gaim_account_get_password(const GaimAccount *account) | |
1651 { | |
1652 g_return_val_if_fail(account != NULL, NULL); | |
1653 | |
1654 return account->password; | |
1655 } | |
1656 | |
1657 const char * | |
1658 gaim_account_get_alias(const GaimAccount *account) | |
1659 { | |
1660 g_return_val_if_fail(account != NULL, NULL); | |
1661 | |
1662 return account->alias; | |
1663 } | |
1664 | |
1665 const char * | |
1666 gaim_account_get_user_info(const GaimAccount *account) | |
1667 { | |
1668 g_return_val_if_fail(account != NULL, NULL); | |
1669 | |
1670 return account->user_info; | |
1671 } | |
1672 | |
1673 const char * | |
1674 gaim_account_get_buddy_icon(const GaimAccount *account) | |
1675 { | |
1676 g_return_val_if_fail(account != NULL, NULL); | |
1677 | |
1678 return account->buddy_icon; | |
1679 } | |
1680 | |
1681 const char * | |
15070 | 1682 gaim_account_get_buddy_icon_path(const GaimAccount *account) |
1683 { | |
1684 g_return_val_if_fail(account != NULL, NULL); | |
1685 | |
1686 return account->buddy_icon_path; | |
1687 } | |
1688 | |
1689 const char * | |
14192 | 1690 gaim_account_get_protocol_id(const GaimAccount *account) |
1691 { | |
1692 g_return_val_if_fail(account != NULL, NULL); | |
14990 | 1693 /* |
1694 * HACK by Seanegan | |
1695 */ | |
1696 if (!strcmp(account->protocol_id, "prpl-oscar")) { | |
1697 if (isdigit(account->username[0])) | |
1698 return "prpl-icq"; | |
1699 else | |
1700 return "prpl-aim"; | |
1701 } | |
14192 | 1702 return account->protocol_id; |
1703 } | |
1704 | |
1705 const char * | |
1706 gaim_account_get_protocol_name(const GaimAccount *account) | |
1707 { | |
1708 GaimPlugin *p; | |
1709 | |
1710 g_return_val_if_fail(account != NULL, NULL); | |
1711 | |
1712 p = gaim_find_prpl(gaim_account_get_protocol_id(account)); | |
1713 | |
1714 return ((p && p->info->name) ? _(p->info->name) : _("Unknown")); | |
1715 } | |
1716 | |
1717 GaimConnection * | |
1718 gaim_account_get_connection(const GaimAccount *account) | |
1719 { | |
1720 g_return_val_if_fail(account != NULL, NULL); | |
1721 | |
1722 return account->gc; | |
1723 } | |
1724 | |
1725 gboolean | |
1726 gaim_account_get_remember_password(const GaimAccount *account) | |
1727 { | |
1728 g_return_val_if_fail(account != NULL, FALSE); | |
1729 | |
1730 return account->remember_pass; | |
1731 } | |
1732 | |
1733 gboolean | |
1734 gaim_account_get_check_mail(const GaimAccount *account) | |
1735 { | |
1736 g_return_val_if_fail(account != NULL, FALSE); | |
1737 | |
1738 return gaim_account_get_bool(account, "check-mail", FALSE); | |
1739 } | |
1740 | |
1741 gboolean | |
1742 gaim_account_get_enabled(const GaimAccount *account, const char *ui) | |
1743 { | |
1744 g_return_val_if_fail(account != NULL, FALSE); | |
1745 g_return_val_if_fail(ui != NULL, FALSE); | |
1746 | |
1747 return gaim_account_get_ui_bool(account, ui, "auto-login", FALSE); | |
1748 } | |
1749 | |
1750 GaimProxyInfo * | |
1751 gaim_account_get_proxy_info(const GaimAccount *account) | |
1752 { | |
1753 g_return_val_if_fail(account != NULL, NULL); | |
1754 | |
1755 return account->proxy_info; | |
1756 } | |
1757 | |
1758 GaimStatus * | |
1759 gaim_account_get_active_status(const GaimAccount *account) | |
1760 { | |
1761 g_return_val_if_fail(account != NULL, NULL); | |
1762 | |
1763 return gaim_presence_get_active_status(account->presence); | |
1764 } | |
1765 | |
1766 GaimStatus * | |
1767 gaim_account_get_status(const GaimAccount *account, const char *status_id) | |
1768 { | |
1769 g_return_val_if_fail(account != NULL, NULL); | |
1770 g_return_val_if_fail(status_id != NULL, NULL); | |
1771 | |
1772 return gaim_presence_get_status(account->presence, status_id); | |
1773 } | |
1774 | |
1775 GaimStatusType * | |
1776 gaim_account_get_status_type(const GaimAccount *account, const char *id) | |
1777 { | |
1778 const GList *l; | |
1779 | |
1780 g_return_val_if_fail(account != NULL, NULL); | |
1781 g_return_val_if_fail(id != NULL, NULL); | |
1782 | |
1783 for (l = gaim_account_get_status_types(account); l != NULL; l = l->next) | |
1784 { | |
1785 GaimStatusType *status_type = (GaimStatusType *)l->data; | |
1786 | |
1787 if (!strcmp(gaim_status_type_get_id(status_type), id)) | |
1788 return status_type; | |
1789 } | |
1790 | |
1791 return NULL; | |
1792 } | |
1793 | |
1794 GaimStatusType * | |
1795 gaim_account_get_status_type_with_primitive(const GaimAccount *account, GaimStatusPrimitive primitive) | |
1796 { | |
1797 const GList *l; | |
1798 | |
1799 g_return_val_if_fail(account != NULL, NULL); | |
1800 | |
1801 for (l = gaim_account_get_status_types(account); l != NULL; l = l->next) | |
1802 { | |
1803 GaimStatusType *status_type = (GaimStatusType *)l->data; | |
1804 | |
1805 if (gaim_status_type_get_primitive(status_type) == primitive) | |
1806 return status_type; | |
1807 } | |
1808 | |
1809 return NULL; | |
1810 } | |
1811 | |
1812 GaimPresence * | |
1813 gaim_account_get_presence(const GaimAccount *account) | |
1814 { | |
1815 g_return_val_if_fail(account != NULL, NULL); | |
1816 | |
1817 return account->presence; | |
1818 } | |
1819 | |
1820 gboolean | |
1821 gaim_account_is_status_active(const GaimAccount *account, | |
1822 const char *status_id) | |
1823 { | |
1824 g_return_val_if_fail(account != NULL, FALSE); | |
1825 g_return_val_if_fail(status_id != NULL, FALSE); | |
1826 | |
1827 return gaim_presence_is_status_active(account->presence, status_id); | |
1828 } | |
1829 | |
1830 const GList * | |
1831 gaim_account_get_status_types(const GaimAccount *account) | |
1832 { | |
1833 g_return_val_if_fail(account != NULL, NULL); | |
1834 | |
1835 return account->status_types; | |
1836 } | |
1837 | |
1838 int | |
1839 gaim_account_get_int(const GaimAccount *account, const char *name, | |
1840 int default_value) | |
1841 { | |
1842 GaimAccountSetting *setting; | |
1843 | |
1844 g_return_val_if_fail(account != NULL, default_value); | |
1845 g_return_val_if_fail(name != NULL, default_value); | |
1846 | |
1847 setting = g_hash_table_lookup(account->settings, name); | |
1848 | |
1849 if (setting == NULL) | |
1850 return default_value; | |
1851 | |
1852 g_return_val_if_fail(setting->type == GAIM_PREF_INT, default_value); | |
1853 | |
1854 return setting->value.integer; | |
1855 } | |
1856 | |
1857 const char * | |
1858 gaim_account_get_string(const GaimAccount *account, const char *name, | |
1859 const char *default_value) | |
1860 { | |
1861 GaimAccountSetting *setting; | |
1862 | |
1863 g_return_val_if_fail(account != NULL, default_value); | |
1864 g_return_val_if_fail(name != NULL, default_value); | |
1865 | |
1866 setting = g_hash_table_lookup(account->settings, name); | |
1867 | |
1868 if (setting == NULL) | |
1869 return default_value; | |
1870 | |
1871 g_return_val_if_fail(setting->type == GAIM_PREF_STRING, default_value); | |
1872 | |
1873 return setting->value.string; | |
1874 } | |
1875 | |
1876 gboolean | |
1877 gaim_account_get_bool(const GaimAccount *account, const char *name, | |
1878 gboolean default_value) | |
1879 { | |
1880 GaimAccountSetting *setting; | |
1881 | |
1882 g_return_val_if_fail(account != NULL, default_value); | |
1883 g_return_val_if_fail(name != NULL, default_value); | |
1884 | |
1885 setting = g_hash_table_lookup(account->settings, name); | |
1886 | |
1887 if (setting == NULL) | |
1888 return default_value; | |
1889 | |
1890 g_return_val_if_fail(setting->type == GAIM_PREF_BOOLEAN, default_value); | |
1891 | |
1892 return setting->value.bool; | |
1893 } | |
1894 | |
1895 int | |
1896 gaim_account_get_ui_int(const GaimAccount *account, const char *ui, | |
1897 const char *name, int 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_INT, default_value); | |
1913 | |
1914 return setting->value.integer; | |
1915 } | |
1916 | |
1917 const char * | |
1918 gaim_account_get_ui_string(const GaimAccount *account, const char *ui, | |
1919 const char *name, const char *default_value) | |
1920 { | |
1921 GaimAccountSetting *setting; | |
1922 GHashTable *table; | |
1923 | |
1924 g_return_val_if_fail(account != NULL, default_value); | |
1925 g_return_val_if_fail(ui != NULL, default_value); | |
1926 g_return_val_if_fail(name != NULL, default_value); | |
1927 | |
1928 if ((table = g_hash_table_lookup(account->ui_settings, ui)) == NULL) | |
1929 return default_value; | |
1930 | |
1931 if ((setting = g_hash_table_lookup(table, name)) == NULL) | |
1932 return default_value; | |
1933 | |
1934 g_return_val_if_fail(setting->type == GAIM_PREF_STRING, default_value); | |
1935 | |
1936 return setting->value.string; | |
1937 } | |
1938 | |
1939 gboolean | |
1940 gaim_account_get_ui_bool(const GaimAccount *account, const char *ui, | |
1941 const char *name, gboolean default_value) | |
1942 { | |
1943 GaimAccountSetting *setting; | |
1944 GHashTable *table; | |
1945 | |
1946 g_return_val_if_fail(account != NULL, default_value); | |
1947 g_return_val_if_fail(ui != NULL, default_value); | |
1948 g_return_val_if_fail(name != NULL, default_value); | |
1949 | |
1950 if ((table = g_hash_table_lookup(account->ui_settings, ui)) == NULL) | |
1951 return default_value; | |
1952 | |
1953 if ((setting = g_hash_table_lookup(table, name)) == NULL) | |
1954 return default_value; | |
1955 | |
1956 g_return_val_if_fail(setting->type == GAIM_PREF_BOOLEAN, default_value); | |
1957 | |
1958 return setting->value.bool; | |
1959 } | |
1960 | |
1961 GaimLog * | |
1962 gaim_account_get_log(GaimAccount *account, gboolean create) | |
1963 { | |
1964 g_return_val_if_fail(account != NULL, NULL); | |
1965 | |
1966 if(!account->system_log && create){ | |
1967 GaimPresence *presence; | |
1968 int login_time; | |
1969 | |
1970 presence = gaim_account_get_presence(account); | |
1971 login_time = gaim_presence_get_login_time(presence); | |
1972 | |
1973 account->system_log = gaim_log_new(GAIM_LOG_SYSTEM, | |
1974 gaim_account_get_username(account), account, NULL, | |
1975 (login_time != 0) ? login_time : time(NULL), NULL); | |
1976 } | |
1977 | |
1978 return account->system_log; | |
1979 } | |
1980 | |
1981 void | |
1982 gaim_account_destroy_log(GaimAccount *account) | |
1983 { | |
1984 g_return_if_fail(account != NULL); | |
1985 | |
1986 if(account->system_log){ | |
1987 gaim_log_free(account->system_log); | |
1988 account->system_log = NULL; | |
1989 } | |
1990 } | |
1991 | |
1992 void | |
1993 gaim_account_add_buddy(GaimAccount *account, GaimBuddy *buddy) | |
1994 { | |
1995 GaimPluginProtocolInfo *prpl_info = NULL; | |
1996 GaimConnection *gc = gaim_account_get_connection(account); | |
1997 | |
1998 if (gc != NULL && gc->prpl != NULL) | |
1999 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(gc->prpl); | |
2000 | |
14607 | 2001 if (prpl_info != NULL && prpl_info->add_buddy != NULL) |
14192 | 2002 prpl_info->add_buddy(gc, buddy, gaim_buddy_get_group(buddy)); |
2003 } | |
2004 | |
2005 void | |
2006 gaim_account_add_buddies(GaimAccount *account, GList *buddies) | |
2007 { | |
2008 GaimPluginProtocolInfo *prpl_info = NULL; | |
2009 GaimConnection *gc = gaim_account_get_connection(account); | |
2010 | |
2011 if (gc != NULL && gc->prpl != NULL) | |
2012 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(gc->prpl); | |
2013 | |
14607 | 2014 if (prpl_info) { |
14192 | 2015 GList *cur, *groups = NULL; |
2016 | |
2017 /* Make a list of what group each buddy is in */ | |
2018 for (cur = buddies; cur != NULL; cur = cur->next) { | |
2019 GaimBlistNode *node = cur->data; | |
2020 groups = g_list_append(groups, node->parent->parent); | |
2021 } | |
2022 | |
2023 if (prpl_info->add_buddies != NULL) | |
2024 prpl_info->add_buddies(gc, buddies, groups); | |
2025 else if (prpl_info->add_buddy != NULL) { | |
2026 GList *curb = buddies, *curg = groups; | |
2027 | |
2028 while ((curb != NULL) && (curg != NULL)) { | |
2029 prpl_info->add_buddy(gc, curb->data, curg->data); | |
2030 curb = curb->next; | |
2031 curg = curg->next; | |
2032 } | |
2033 } | |
2034 | |
2035 g_list_free(groups); | |
2036 } | |
2037 } | |
2038 | |
2039 void | |
2040 gaim_account_remove_buddy(GaimAccount *account, GaimBuddy *buddy, | |
2041 GaimGroup *group) | |
2042 { | |
2043 GaimPluginProtocolInfo *prpl_info = NULL; | |
2044 GaimConnection *gc = gaim_account_get_connection(account); | |
2045 | |
2046 if (gc != NULL && gc->prpl != NULL) | |
2047 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(gc->prpl); | |
2048 | |
14607 | 2049 if (prpl_info && prpl_info->remove_buddy) |
14192 | 2050 prpl_info->remove_buddy(gc, buddy, group); |
2051 } | |
2052 | |
2053 void | |
2054 gaim_account_remove_buddies(GaimAccount *account, GList *buddies, GList *groups) | |
2055 { | |
2056 GaimPluginProtocolInfo *prpl_info = NULL; | |
2057 GaimConnection *gc = gaim_account_get_connection(account); | |
2058 | |
2059 if (gc != NULL && gc->prpl != NULL) | |
2060 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(gc->prpl); | |
2061 | |
14607 | 2062 if (prpl_info) { |
14192 | 2063 if (prpl_info->remove_buddies) |
2064 prpl_info->remove_buddies(gc, buddies, groups); | |
2065 else { | |
2066 GList *curb = buddies; | |
2067 GList *curg = groups; | |
2068 while ((curb != NULL) && (curg != NULL)) { | |
2069 gaim_account_remove_buddy(account, curb->data, curg->data); | |
2070 curb = curb->next; | |
2071 curg = curg->next; | |
2072 } | |
2073 } | |
2074 } | |
2075 } | |
2076 | |
2077 void | |
2078 gaim_account_remove_group(GaimAccount *account, GaimGroup *group) | |
2079 { | |
2080 GaimPluginProtocolInfo *prpl_info = NULL; | |
2081 GaimConnection *gc = gaim_account_get_connection(account); | |
2082 | |
2083 if (gc != NULL && gc->prpl != NULL) | |
2084 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(gc->prpl); | |
2085 | |
14607 | 2086 if (prpl_info && prpl_info->remove_group) |
14192 | 2087 prpl_info->remove_group(gc, group); |
2088 } | |
2089 | |
2090 void | |
2091 gaim_account_change_password(GaimAccount *account, const char *orig_pw, | |
2092 const char *new_pw) | |
2093 { | |
2094 GaimPluginProtocolInfo *prpl_info = NULL; | |
2095 GaimConnection *gc = gaim_account_get_connection(account); | |
2096 | |
2097 gaim_account_set_password(account, new_pw); | |
2098 | |
2099 if (gc != NULL && gc->prpl != NULL) | |
2100 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(gc->prpl); | |
2101 | |
14607 | 2102 if (prpl_info && prpl_info->change_passwd) |
14192 | 2103 prpl_info->change_passwd(gc, orig_pw, new_pw); |
2104 } | |
2105 | |
2106 gboolean gaim_account_supports_offline_message(GaimAccount *account, GaimBuddy *buddy) | |
2107 { | |
2108 GaimConnection *gc; | |
2109 GaimPluginProtocolInfo *prpl_info; | |
2110 | |
2111 g_return_val_if_fail(account, FALSE); | |
2112 g_return_val_if_fail(buddy, FALSE); | |
2113 | |
2114 gc = gaim_account_get_connection(account); | |
2115 if (gc == NULL) | |
2116 return FALSE; | |
2117 | |
2118 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(gc->prpl); | |
2119 | |
2120 if (!prpl_info || !prpl_info->offline_message) | |
2121 return FALSE; | |
2122 return prpl_info->offline_message(buddy); | |
2123 } | |
2124 | |
2125 void | |
2126 gaim_accounts_add(GaimAccount *account) | |
2127 { | |
2128 g_return_if_fail(account != NULL); | |
2129 | |
2130 if (g_list_find(accounts, account) != NULL) | |
2131 return; | |
2132 | |
2133 accounts = g_list_append(accounts, account); | |
2134 | |
2135 schedule_accounts_save(); | |
2136 | |
2137 gaim_signal_emit(gaim_accounts_get_handle(), "account-added", account); | |
2138 } | |
2139 | |
2140 void | |
2141 gaim_accounts_remove(GaimAccount *account) | |
2142 { | |
2143 g_return_if_fail(account != NULL); | |
2144 | |
2145 accounts = g_list_remove(accounts, account); | |
2146 | |
2147 schedule_accounts_save(); | |
2148 | |
2149 gaim_signal_emit(gaim_accounts_get_handle(), "account-removed", account); | |
2150 } | |
2151 | |
2152 void | |
2153 gaim_accounts_delete(GaimAccount *account) | |
2154 { | |
2155 GaimBlistNode *gnode, *cnode, *bnode; | |
2156 | |
2157 g_return_if_fail(account != NULL); | |
2158 | |
14909 | 2159 /* |
2160 * Disable the account before blowing it out of the water. | |
2161 * Conceptually it probably makes more sense to disable the | |
2162 * account for all UIs rather than the just the current UI, | |
2163 * but it doesn't really matter. | |
2164 */ | |
2165 gaim_account_set_enabled(account, gaim_core_get_ui(), FALSE); | |
2166 | |
14192 | 2167 gaim_notify_close_with_handle(account); |
2168 gaim_request_close_with_handle(account); | |
2169 | |
2170 gaim_accounts_remove(account); | |
2171 | |
2172 /* Remove this account's buddies */ | |
2173 for (gnode = gaim_get_blist()->root; gnode != NULL; gnode = gnode->next) { | |
2174 if (!GAIM_BLIST_NODE_IS_GROUP(gnode)) | |
2175 continue; | |
2176 | |
2177 cnode = gnode->child; | |
2178 while (cnode) { | |
2179 GaimBlistNode *cnode_next = cnode->next; | |
2180 | |
2181 if(GAIM_BLIST_NODE_IS_CONTACT(cnode)) { | |
2182 bnode = cnode->child; | |
2183 while (bnode) { | |
2184 GaimBlistNode *bnode_next = bnode->next; | |
2185 | |
2186 if (GAIM_BLIST_NODE_IS_BUDDY(bnode)) { | |
2187 GaimBuddy *b = (GaimBuddy *)bnode; | |
2188 | |
2189 if (b->account == account) | |
2190 gaim_blist_remove_buddy(b); | |
2191 } | |
2192 bnode = bnode_next; | |
2193 } | |
2194 } else if (GAIM_BLIST_NODE_IS_CHAT(cnode)) { | |
2195 GaimChat *c = (GaimChat *)cnode; | |
2196 | |
2197 if (c->account == account) | |
2198 gaim_blist_remove_chat(c); | |
2199 } | |
2200 cnode = cnode_next; | |
2201 } | |
2202 } | |
2203 | |
2204 /* Remove this account's pounces */ | |
2205 gaim_pounce_destroy_all_by_account(account); | |
2206 | |
2207 /* This will cause the deletion of an old buddy icon. */ | |
2208 gaim_account_set_buddy_icon(account, NULL); | |
2209 | |
2210 gaim_account_destroy(account); | |
2211 } | |
2212 | |
2213 void | |
2214 gaim_accounts_reorder(GaimAccount *account, gint new_index) | |
2215 { | |
2216 gint index; | |
2217 GList *l; | |
2218 | |
2219 g_return_if_fail(account != NULL); | |
2220 g_return_if_fail(new_index <= g_list_length(accounts)); | |
2221 | |
2222 index = g_list_index(accounts, account); | |
2223 | |
2224 if (index == -1) { | |
2225 gaim_debug_error("account", | |
2226 "Unregistered account (%s) discovered during reorder!\n", | |
2227 gaim_account_get_username(account)); | |
2228 return; | |
2229 } | |
2230 | |
2231 l = g_list_nth(accounts, index); | |
2232 | |
2233 if (new_index > index) | |
2234 new_index--; | |
2235 | |
2236 /* Remove the old one. */ | |
2237 accounts = g_list_delete_link(accounts, l); | |
2238 | |
2239 /* Insert it where it should go. */ | |
2240 accounts = g_list_insert(accounts, account, new_index); | |
2241 | |
2242 schedule_accounts_save(); | |
2243 } | |
2244 | |
2245 GList * | |
2246 gaim_accounts_get_all(void) | |
2247 { | |
2248 return accounts; | |
2249 } | |
2250 | |
2251 GList * | |
2252 gaim_accounts_get_all_active(void) | |
2253 { | |
2254 GList *list = NULL; | |
2255 GList *all = gaim_accounts_get_all(); | |
2256 | |
2257 while (all != NULL) { | |
2258 GaimAccount *account = all->data; | |
2259 | |
2260 if (gaim_account_get_enabled(account, gaim_core_get_ui())) | |
2261 list = g_list_append(list, account); | |
2262 | |
2263 all = all->next; | |
2264 } | |
2265 | |
2266 return list; | |
2267 } | |
2268 | |
2269 GaimAccount * | |
2270 gaim_accounts_find(const char *name, const char *protocol_id) | |
2271 { | |
2272 GaimAccount *account = NULL; | |
2273 GList *l; | |
2274 char *who; | |
2275 | |
2276 g_return_val_if_fail(name != NULL, NULL); | |
2277 | |
2278 who = g_strdup(gaim_normalize(NULL, name)); | |
2279 | |
2280 for (l = gaim_accounts_get_all(); l != NULL; l = l->next) { | |
2281 account = (GaimAccount *)l->data; | |
2282 | |
2283 if (!strcmp(gaim_normalize(NULL, gaim_account_get_username(account)), who) && | |
2284 (!protocol_id || !strcmp(account->protocol_id, protocol_id))) { | |
2285 | |
2286 break; | |
2287 } | |
2288 | |
2289 account = NULL; | |
2290 } | |
2291 | |
2292 g_free(who); | |
2293 | |
2294 return account; | |
2295 } | |
2296 | |
2297 void | |
2298 gaim_accounts_restore_current_statuses() | |
2299 { | |
2300 GList *l; | |
2301 GaimAccount *account; | |
2302 | |
14696 | 2303 /* If we're not connected to the Internet right now, we bail on this */ |
2304 if (!gaim_network_is_available()) | |
2305 { | |
14705 | 2306 gaim_debug_info("account", "Network not connected; skipping reconnect\n"); |
14696 | 2307 return; |
2308 } | |
2309 | |
14192 | 2310 for (l = gaim_accounts_get_all(); l != NULL; l = l->next) |
2311 { | |
2312 account = (GaimAccount *)l->data; | |
2313 if (gaim_account_get_enabled(account, gaim_core_get_ui()) && | |
2314 (gaim_presence_is_online(account->presence))) | |
2315 { | |
2316 gaim_account_connect(account); | |
2317 } | |
2318 } | |
2319 } | |
2320 | |
2321 void | |
2322 gaim_accounts_set_ui_ops(GaimAccountUiOps *ops) | |
2323 { | |
2324 account_ui_ops = ops; | |
2325 } | |
2326 | |
2327 GaimAccountUiOps * | |
2328 gaim_accounts_get_ui_ops(void) | |
2329 { | |
2330 return account_ui_ops; | |
2331 } | |
2332 | |
2333 void * | |
2334 gaim_accounts_get_handle(void) | |
2335 { | |
2336 static int handle; | |
2337 | |
2338 return &handle; | |
2339 } | |
2340 | |
2341 void | |
2342 gaim_accounts_init(void) | |
2343 { | |
2344 void *handle = gaim_accounts_get_handle(); | |
2345 | |
2346 gaim_signal_register(handle, "account-connecting", | |
2347 gaim_marshal_VOID__POINTER, NULL, 1, | |
2348 gaim_value_new(GAIM_TYPE_SUBTYPE, | |
2349 GAIM_SUBTYPE_ACCOUNT)); | |
2350 | |
2351 gaim_signal_register(handle, "account-disabled", | |
2352 gaim_marshal_VOID__POINTER, NULL, 1, | |
2353 gaim_value_new(GAIM_TYPE_SUBTYPE, | |
2354 GAIM_SUBTYPE_ACCOUNT)); | |
2355 | |
2356 gaim_signal_register(handle, "account-enabled", | |
2357 gaim_marshal_VOID__POINTER, NULL, 1, | |
2358 gaim_value_new(GAIM_TYPE_SUBTYPE, | |
2359 GAIM_SUBTYPE_ACCOUNT)); | |
2360 | |
2361 gaim_signal_register(handle, "account-setting-info", | |
2362 gaim_marshal_VOID__POINTER_POINTER, NULL, 2, | |
2363 gaim_value_new(GAIM_TYPE_SUBTYPE, | |
2364 GAIM_SUBTYPE_ACCOUNT), | |
2365 gaim_value_new(GAIM_TYPE_STRING)); | |
2366 | |
2367 gaim_signal_register(handle, "account-set-info", | |
2368 gaim_marshal_VOID__POINTER_POINTER, NULL, 2, | |
2369 gaim_value_new(GAIM_TYPE_SUBTYPE, | |
2370 GAIM_SUBTYPE_ACCOUNT), | |
2371 gaim_value_new(GAIM_TYPE_STRING)); | |
2372 | |
2373 gaim_signal_register(handle, "account-added", | |
2374 gaim_marshal_VOID__POINTER, NULL, 1, | |
2375 gaim_value_new(GAIM_TYPE_SUBTYPE, GAIM_SUBTYPE_ACCOUNT)); | |
2376 | |
2377 gaim_signal_register(handle, "account-removed", | |
2378 gaim_marshal_VOID__POINTER, NULL, 1, | |
2379 gaim_value_new(GAIM_TYPE_SUBTYPE, GAIM_SUBTYPE_ACCOUNT)); | |
2380 | |
2381 gaim_signal_register(handle, "account-status-changed", | |
2382 gaim_marshal_VOID__POINTER_POINTER_POINTER, NULL, 3, | |
2383 gaim_value_new(GAIM_TYPE_SUBTYPE, | |
2384 GAIM_SUBTYPE_ACCOUNT), | |
2385 gaim_value_new(GAIM_TYPE_SUBTYPE, | |
2386 GAIM_SUBTYPE_STATUS), | |
2387 gaim_value_new(GAIM_TYPE_SUBTYPE, | |
2388 GAIM_SUBTYPE_STATUS)); | |
2389 | |
2390 gaim_signal_register(handle, "account-alias-changed", | |
2391 gaim_marshal_VOID__POINTER_POINTER, NULL, 2, | |
2392 gaim_value_new(GAIM_TYPE_SUBTYPE, | |
2393 GAIM_SUBTYPE_ACCOUNT), | |
2394 gaim_value_new(GAIM_TYPE_STRING)); | |
2395 | |
2396 load_accounts(); | |
2397 | |
2398 } | |
2399 | |
2400 void | |
2401 gaim_accounts_uninit(void) | |
2402 { | |
2403 if (save_timer != 0) | |
2404 { | |
2405 gaim_timeout_remove(save_timer); | |
2406 save_timer = 0; | |
2407 sync_accounts(); | |
2408 } | |
2409 | |
2410 gaim_signals_unregister_by_instance(gaim_accounts_get_handle()); | |
2411 } |