Mercurial > pidgin.yaz
annotate src/blist.c @ 5283:1872e8efba87
[gaim-migrate @ 5655]
Morten Brix Pedersen (mbrix) updated the danish translation :-)
committer: Tailor Script <tailor@pidgin.im>
author | Luke Schierer <lschiere@pidgin.im> |
---|---|
date | Fri, 02 May 2003 17:25:49 +0000 |
parents | 1dcdc2504849 |
children | abd6d3abbe37 |
rev | line source |
---|---|
5228 | 1 /* |
2 * gaim | |
3 * | |
4 * Copyright (C) 2003, Sean Egan <sean.egan@binghamton.edu> | |
5 * Copyright (C) 1998-1999, Mark Spencer <markster@marko.net> | |
6 * | |
7 * This program is free software; you can redistribute it and/or modify | |
8 * it under the terms of the GNU General Public License as published by | |
9 * the Free Software Foundation; either version 2 of the License, or | |
10 * (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, | |
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 * GNU General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU General Public License | |
18 * along with this program; if not, write to the Free Software | |
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
20 * | |
21 */ | |
22 | |
23 #ifdef HAVE_CONFIG_H | |
24 #include <config.h> | |
25 #endif | |
26 #include <string.h> | |
27 #include <stdlib.h> | |
28 #include <sys/types.h> | |
29 #include <sys/stat.h> | |
30 #ifndef _WIN32 | |
31 #include <unistd.h> | |
32 #else | |
33 #include <direct.h> | |
34 #endif | |
35 #include <ctype.h> | |
36 #include "gaim.h" | |
37 #include "prpl.h" | |
38 #include "blist.h" | |
39 | |
40 #ifdef _WIN32 | |
41 #include "win32dep.h" | |
42 #endif | |
43 | |
44 #define PATHSIZE 1024 | |
45 | |
46 struct gaim_buddy_list *gaimbuddylist = NULL; | |
47 static struct gaim_blist_ui_ops *blist_ui_ops = NULL; | |
48 | |
49 /***************************************************************************** | |
50 * Private Utility functions * | |
51 *****************************************************************************/ | |
52 static GaimBlistNode *gaim_blist_get_last_sibling(GaimBlistNode *node) | |
53 { | |
54 GaimBlistNode *n = node; | |
55 if (!n) | |
56 return NULL; | |
57 while (n->next) | |
58 n = n->next; | |
59 return n; | |
60 } | |
61 static GaimBlistNode *gaim_blist_get_last_child(GaimBlistNode *node) | |
62 { | |
63 if (!node) | |
64 return NULL; | |
65 return gaim_blist_get_last_sibling(node->child); | |
66 } | |
67 | |
5247 | 68 struct _gaim_hbuddy { |
69 char *name; | |
70 struct gaim_account *account; | |
71 }; | |
72 | |
73 static guint _gaim_blist_hbuddy_hash (struct _gaim_hbuddy *hb) | |
74 { | |
75 return g_str_hash(hb->name); | |
76 } | |
77 | |
78 static guint _gaim_blist_hbuddy_equal (struct _gaim_hbuddy *hb1, struct _gaim_hbuddy *hb2) | |
79 { | |
80 return ((!strcmp(hb1->name, hb2->name)) && hb1->account == hb2->account); | |
81 } | |
82 | |
5228 | 83 /***************************************************************************** |
84 * Public API functions * | |
85 *****************************************************************************/ | |
86 | |
87 struct gaim_buddy_list *gaim_blist_new() | |
88 { | |
89 struct gaim_buddy_list *gbl = g_new0(struct gaim_buddy_list, 1); | |
90 | |
91 gbl->ui_ops = gaim_get_blist_ui_ops(); | |
92 | |
5247 | 93 gbl->buddies = g_hash_table_new ((GHashFunc)_gaim_blist_hbuddy_hash, |
94 (GEqualFunc)_gaim_blist_hbuddy_equal); | |
95 | |
5228 | 96 if (gbl->ui_ops != NULL && gbl->ui_ops->new_list != NULL) |
97 gbl->ui_ops->new_list(gbl); | |
98 | |
99 return gbl; | |
100 } | |
101 | |
102 void | |
103 gaim_set_blist(struct gaim_buddy_list *list) | |
104 { | |
105 gaimbuddylist = list; | |
106 } | |
107 | |
108 struct gaim_buddy_list * | |
109 gaim_get_blist(void) | |
110 { | |
111 return gaimbuddylist; | |
112 } | |
113 | |
114 void gaim_blist_show () | |
115 { | |
116 struct gaim_blist_ui_ops *ops = gaimbuddylist->ui_ops; | |
117 if (ops) | |
118 ops->show(gaimbuddylist); | |
119 } | |
120 | |
121 void gaim_blist_destroy() | |
122 { | |
123 struct gaim_blist_ui_ops *ops = gaimbuddylist->ui_ops; | |
124 if (ops) | |
125 ops->destroy(gaimbuddylist); | |
126 } | |
127 | |
128 void gaim_blist_set_visible (gboolean show) | |
129 { | |
130 struct gaim_blist_ui_ops *ops = gaimbuddylist->ui_ops; | |
131 if (ops) | |
132 ops->set_visible(gaimbuddylist, show); | |
133 } | |
134 | |
135 void gaim_blist_update_buddy_status (struct buddy *buddy, int status) | |
136 { | |
5266
b3a03b86b09b
[gaim-migrate @ 5638]
Christian Hammond <chipx86@chipx86.com>
parents:
5259
diff
changeset
|
137 struct gaim_blist_ui_ops *ops; |
b3a03b86b09b
[gaim-migrate @ 5638]
Christian Hammond <chipx86@chipx86.com>
parents:
5259
diff
changeset
|
138 |
b3a03b86b09b
[gaim-migrate @ 5638]
Christian Hammond <chipx86@chipx86.com>
parents:
5259
diff
changeset
|
139 if (buddy->uc == status) |
b3a03b86b09b
[gaim-migrate @ 5638]
Christian Hammond <chipx86@chipx86.com>
parents:
5259
diff
changeset
|
140 return; |
b3a03b86b09b
[gaim-migrate @ 5638]
Christian Hammond <chipx86@chipx86.com>
parents:
5259
diff
changeset
|
141 |
b3a03b86b09b
[gaim-migrate @ 5638]
Christian Hammond <chipx86@chipx86.com>
parents:
5259
diff
changeset
|
142 ops = gaimbuddylist->ui_ops; |
5228 | 143 buddy->uc = status; |
144 | |
145 if(!(status & UC_UNAVAILABLE)) | |
146 gaim_event_broadcast(event_buddy_back, buddy->account->gc, buddy->name); | |
147 else | |
148 gaim_event_broadcast(event_buddy_away, buddy->account->gc, buddy->name); | |
149 | |
150 if (ops) | |
151 ops->update(gaimbuddylist, (GaimBlistNode*)buddy); | |
152 } | |
153 | |
154 static gboolean presence_update_timeout_cb(struct buddy *buddy) { | |
155 struct gaim_blist_ui_ops *ops = gaimbuddylist->ui_ops; | |
156 | |
157 if(buddy->present == GAIM_BUDDY_SIGNING_ON) { | |
158 buddy->present = GAIM_BUDDY_ONLINE; | |
159 gaim_event_broadcast(event_buddy_signon, buddy->account->gc, buddy->name); | |
160 } else if(buddy->present == GAIM_BUDDY_SIGNING_OFF) { | |
161 buddy->present = GAIM_BUDDY_OFFLINE; | |
162 } | |
163 | |
164 buddy->timer = 0; | |
165 | |
166 if (ops) | |
167 ops->update(gaimbuddylist, (GaimBlistNode*)buddy); | |
168 | |
169 return FALSE; | |
170 } | |
171 | |
172 void gaim_blist_update_buddy_presence(struct buddy *buddy, int presence) { | |
173 struct gaim_blist_ui_ops *ops = gaimbuddylist->ui_ops; | |
174 gboolean do_timer = FALSE; | |
175 | |
176 if (!GAIM_BUDDY_IS_ONLINE(buddy) && presence) { | |
177 buddy->present = GAIM_BUDDY_SIGNING_ON; | |
178 gaim_event_broadcast(event_buddy_signon, buddy->account->gc, buddy->name); | |
179 do_timer = TRUE; | |
5277 | 180 ((struct group *)((GaimBlistNode *)buddy)->parent)->online++; |
5228 | 181 } else if(GAIM_BUDDY_IS_ONLINE(buddy) && !presence) { |
182 buddy->present = GAIM_BUDDY_SIGNING_OFF; | |
183 gaim_event_broadcast(event_buddy_signoff, buddy->account->gc, buddy->name); | |
184 do_timer = TRUE; | |
5277 | 185 ((struct group *)((GaimBlistNode *)buddy)->parent)->online--; |
5228 | 186 } |
187 | |
188 if(do_timer) { | |
189 if(buddy->timer > 0) | |
190 g_source_remove(buddy->timer); | |
191 buddy->timer = g_timeout_add(10000, (GSourceFunc)presence_update_timeout_cb, buddy); | |
192 } | |
193 | |
194 if (ops) | |
195 ops->update(gaimbuddylist, (GaimBlistNode*)buddy); | |
196 } | |
197 | |
198 | |
199 void gaim_blist_update_buddy_idle (struct buddy *buddy, int idle) | |
200 { | |
201 struct gaim_blist_ui_ops *ops = gaimbuddylist->ui_ops; | |
202 buddy->idle = idle; | |
203 if (ops) | |
204 ops->update(gaimbuddylist, (GaimBlistNode*)buddy); | |
205 } | |
206 void gaim_blist_update_buddy_evil (struct buddy *buddy, int warning) | |
207 { | |
208 struct gaim_blist_ui_ops *ops = gaimbuddylist->ui_ops; | |
209 buddy->evil = warning; | |
210 if (ops) | |
211 ops->update(gaimbuddylist,(GaimBlistNode*)buddy); | |
212 } | |
213 void gaim_blist_update_buddy_icon(struct buddy *buddy) { | |
214 struct gaim_blist_ui_ops *ops = gaimbuddylist->ui_ops; | |
215 if(ops) | |
216 ops->update(gaimbuddylist, (GaimBlistNode*)buddy); | |
217 } | |
218 void gaim_blist_rename_buddy (struct buddy *buddy, const char *name) | |
219 { | |
220 struct gaim_blist_ui_ops *ops = gaimbuddylist->ui_ops; | |
221 g_free(buddy->name); | |
222 buddy->name = g_strdup(name); | |
223 if (ops) | |
224 ops->update(gaimbuddylist, (GaimBlistNode*)buddy); | |
225 } | |
5234 | 226 |
227 void gaim_blist_alias_chat(struct chat *chat, const char *alias) | |
228 { | |
229 struct gaim_blist_ui_ops *ops = gaimbuddylist->ui_ops; | |
230 | |
5237 | 231 g_free(chat->alias); |
5234 | 232 |
5237 | 233 if(alias && strlen(alias)) |
234 chat->alias = g_strdup(alias); | |
235 else | |
236 chat->alias = NULL; | |
237 | |
5234 | 238 if(ops) |
239 ops->update(gaimbuddylist, (GaimBlistNode*)chat); | |
240 } | |
241 | |
5228 | 242 void gaim_blist_alias_buddy (struct buddy *buddy, const char *alias) |
243 { | |
244 struct gaim_blist_ui_ops *ops = gaimbuddylist->ui_ops; | |
245 struct gaim_conversation *conv; | |
246 | |
247 g_free(buddy->alias); | |
248 | |
249 if(alias && strlen(alias)) | |
250 buddy->alias = g_strdup(alias); | |
251 else | |
252 buddy->alias = NULL; | |
253 | |
254 if (ops) | |
255 ops->update(gaimbuddylist, (GaimBlistNode*)buddy); | |
256 | |
257 conv = gaim_find_conversation_with_account(buddy->name, buddy->account); | |
258 | |
259 if (conv) | |
260 gaim_conversation_autoset_title(conv); | |
261 } | |
262 | |
263 void gaim_blist_rename_group(struct group *group, const char *name) | |
264 { | |
265 struct gaim_blist_ui_ops *ops = gaimbuddylist->ui_ops; | |
266 g_free(group->name); | |
267 group->name = g_strdup(name); | |
268 if (ops) | |
269 ops->update(gaimbuddylist, (GaimBlistNode*)group); | |
270 } | |
5234 | 271 |
272 struct chat *gaim_chat_new(struct gaim_account *account, const char *alias, GHashTable *components) | |
273 { | |
274 struct chat *chat; | |
275 struct gaim_blist_ui_ops *ops; | |
276 | |
5237 | 277 if(!components) |
5234 | 278 return NULL; |
279 | |
280 chat = g_new0(struct chat, 1); | |
281 chat->account = account; | |
5237 | 282 if(alias && strlen(alias)) |
283 chat->alias = g_strdup(alias); | |
5234 | 284 chat->components = components; |
285 | |
286 ((GaimBlistNode*)chat)->type = GAIM_BLIST_CHAT_NODE; | |
287 | |
288 ops = gaim_get_blist_ui_ops(); | |
289 | |
290 if (ops != NULL && ops->new_node != NULL) | |
291 ops->new_node((GaimBlistNode *)chat); | |
292 | |
293 return chat; | |
294 } | |
295 | |
5228 | 296 struct buddy *gaim_buddy_new(struct gaim_account *account, const char *screenname, const char *alias) |
297 { | |
298 struct buddy *b; | |
299 struct gaim_blist_ui_ops *ops; | |
300 | |
301 b = g_new0(struct buddy, 1); | |
302 b->account = account; | |
303 b->name = g_strdup(screenname); | |
304 b->alias = g_strdup(alias); | |
305 b->settings = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); | |
306 ((GaimBlistNode*)b)->type = GAIM_BLIST_BUDDY_NODE; | |
307 | |
308 ops = gaim_get_blist_ui_ops(); | |
309 | |
310 if (ops != NULL && ops->new_node != NULL) | |
311 ops->new_node((GaimBlistNode *)b); | |
312 | |
313 return b; | |
314 } | |
5234 | 315 void gaim_blist_add_chat(struct chat *chat, struct group *group, GaimBlistNode *node) |
316 { | |
317 GaimBlistNode *n = node, *cnode = (GaimBlistNode*)chat; | |
318 struct group *g = group; | |
319 struct gaim_blist_ui_ops *ops = gaimbuddylist->ui_ops; | |
320 gboolean save = FALSE; | |
321 | |
322 if (!n) { | |
323 if (!g) { | |
324 g = gaim_group_new(_("Chats")); | |
325 gaim_blist_add_group(g, NULL); | |
326 } | |
327 n = gaim_blist_get_last_child((GaimBlistNode*)g); | |
328 } else { | |
329 g = (struct group*)n->parent; | |
330 } | |
331 | |
332 /* if we're moving to overtop of ourselves, do nothing */ | |
333 if(cnode == n) | |
334 return; | |
335 | |
336 if (cnode->parent) { | |
337 /* This chat was already in the list and is | |
338 * being moved. | |
339 */ | |
5277 | 340 ((struct group *)cnode->parent)->totalsize--; |
341 if (chat->account->gc) | |
342 ((struct group *)cnode->parent)->currentsize--; | |
5234 | 343 if(cnode->next) |
344 cnode->next->prev = cnode->prev; | |
345 if(cnode->prev) | |
346 cnode->prev->next = cnode->next; | |
347 if(cnode->parent->child == cnode) | |
348 cnode->parent->child = cnode->next; | |
349 | |
5278 | 350 ((struct group *)((GaimBlistNode *)chat)->parent)->online--; |
351 | |
5234 | 352 ops->remove(gaimbuddylist, cnode); |
353 | |
354 save = TRUE; | |
355 } | |
356 | |
357 if (n) { | |
358 if(n->next) | |
359 n->next->prev = cnode; | |
360 cnode->next = n->next; | |
361 cnode->prev = n; | |
362 cnode->parent = n->parent; | |
363 n->next = cnode; | |
5277 | 364 ((struct group *)n->parent)->totalsize++; |
365 if (chat->account->gc) | |
366 ((struct group *)n->parent)->currentsize++; | |
5234 | 367 } else { |
368 ((GaimBlistNode*)g)->child = cnode; | |
369 cnode->next = cnode->prev = NULL; | |
370 cnode->parent = (GaimBlistNode*)g; | |
5277 | 371 g->totalsize++; |
372 if (chat->account->gc) | |
373 g->currentsize++; | |
5234 | 374 } |
375 | |
5278 | 376 ((struct group *)((GaimBlistNode *)chat)->parent)->online++; |
377 | |
5234 | 378 if (ops) |
379 ops->update(gaimbuddylist, (GaimBlistNode*)cnode); | |
380 if (save) | |
381 gaim_blist_save(); | |
382 } | |
383 | |
5228 | 384 void gaim_blist_add_buddy (struct buddy *buddy, struct group *group, GaimBlistNode *node) |
385 { | |
386 GaimBlistNode *n = node, *bnode = (GaimBlistNode*)buddy; | |
387 struct group *g = group; | |
388 struct gaim_blist_ui_ops *ops = gaimbuddylist->ui_ops; | |
5247 | 389 struct _gaim_hbuddy *hb; |
5228 | 390 gboolean save = FALSE; |
391 | |
392 if (!n) { | |
393 if (!g) { | |
394 g = gaim_group_new(_("Buddies")); | |
395 gaim_blist_add_group(g, NULL); | |
396 } | |
397 n = gaim_blist_get_last_child((GaimBlistNode*)g); | |
398 } else { | |
399 g = (struct group*)n->parent; | |
400 } | |
401 | |
402 /* if we're moving to overtop of ourselves, do nothing */ | |
403 if(bnode == n) | |
404 return; | |
405 | |
406 if (bnode->parent) { | |
407 /* This buddy was already in the list and is | |
408 * being moved. | |
409 */ | |
5277 | 410 ((struct group *)bnode->parent)->totalsize--; |
411 if (buddy->account->gc) | |
412 ((struct group *)bnode->parent)->currentsize--; | |
413 | |
5228 | 414 if(bnode->next) |
415 bnode->next->prev = bnode->prev; | |
416 if(bnode->prev) | |
417 bnode->prev->next = bnode->next; | |
418 if(bnode->parent->child == bnode) | |
419 bnode->parent->child = bnode->next; | |
420 | |
421 ops->remove(gaimbuddylist, bnode); | |
422 | |
5277 | 423 if (bnode->parent != ((GaimBlistNode*)g)) { |
5228 | 424 serv_move_buddy(buddy, (struct group*)bnode->parent, g); |
5277 | 425 } |
5228 | 426 save = TRUE; |
427 } | |
428 | |
429 if (n) { | |
430 if(n->next) | |
431 n->next->prev = (GaimBlistNode*)buddy; | |
432 ((GaimBlistNode*)buddy)->next = n->next; | |
433 ((GaimBlistNode*)buddy)->prev = n; | |
434 ((GaimBlistNode*)buddy)->parent = n->parent; | |
435 n->next = (GaimBlistNode*)buddy; | |
5277 | 436 ((struct group *)n->parent)->totalsize++; |
437 if (buddy->account->gc) | |
438 ((struct group *)n->parent)->currentsize++; | |
5228 | 439 } else { |
440 ((GaimBlistNode*)g)->child = (GaimBlistNode*)buddy; | |
441 ((GaimBlistNode*)buddy)->next = NULL; | |
442 ((GaimBlistNode*)buddy)->prev = NULL; | |
443 ((GaimBlistNode*)buddy)->parent = (GaimBlistNode*)g; | |
5277 | 444 g->totalsize++; |
445 if (buddy->account->gc) | |
446 g->currentsize++; | |
5228 | 447 } |
448 | |
5247 | 449 hb = g_malloc(sizeof(struct _gaim_hbuddy)); |
450 hb->name = g_strdup(normalize(buddy->name)); | |
451 hb->account = buddy->account; | |
452 | |
453 if (g_hash_table_lookup(gaimbuddylist->buddies, (gpointer)hb)) { | |
454 /* This guy already exists */ | |
455 g_free(hb->name); | |
456 g_free(hb); | |
457 } else { | |
458 g_hash_table_insert(gaimbuddylist->buddies, (gpointer)hb, (gpointer)buddy); | |
459 } | |
460 | |
5228 | 461 if (ops) |
462 ops->update(gaimbuddylist, (GaimBlistNode*)buddy); | |
463 if (save) | |
464 gaim_blist_save(); | |
465 } | |
466 | |
467 struct group *gaim_group_new(const char *name) | |
468 { | |
469 struct group *g = gaim_find_group(name); | |
470 | |
471 if (!g) { | |
472 struct gaim_blist_ui_ops *ops; | |
473 g= g_new0(struct group, 1); | |
474 g->name = g_strdup(name); | |
5277 | 475 g->totalsize = 0; |
476 g->currentsize = 0; | |
477 g->online = 0; | |
5228 | 478 g->settings = g_hash_table_new_full(g_str_hash, g_str_equal, |
479 g_free, g_free); | |
480 ((GaimBlistNode*)g)->type = GAIM_BLIST_GROUP_NODE; | |
481 | |
482 ops = gaim_get_blist_ui_ops(); | |
483 | |
484 if (ops != NULL && ops->new_node != NULL) | |
485 ops->new_node((GaimBlistNode *)g); | |
486 | |
487 } | |
488 return g; | |
489 } | |
490 | |
491 void gaim_blist_add_group (struct group *group, GaimBlistNode *node) | |
492 { | |
493 struct gaim_blist_ui_ops *ops; | |
494 GaimBlistNode *gnode = (GaimBlistNode*)group; | |
495 gboolean save = FALSE; | |
496 | |
497 if (!gaimbuddylist) | |
498 gaimbuddylist = gaim_blist_new(); | |
499 ops = gaimbuddylist->ui_ops; | |
500 | |
501 if (!gaimbuddylist->root) { | |
502 gaimbuddylist->root = gnode; | |
503 return; | |
504 } | |
505 | |
506 if (!node) | |
507 node = gaim_blist_get_last_sibling(gaimbuddylist->root); | |
508 | |
509 /* if we're moving to overtop of ourselves, do nothing */ | |
510 if(gnode == node) | |
511 return; | |
512 | |
513 if (gaim_find_group(group->name)) { | |
514 /* This is just being moved */ | |
515 | |
516 ops->remove(gaimbuddylist, (GaimBlistNode*)group); | |
517 | |
518 if(gnode == gaimbuddylist->root) | |
519 gaimbuddylist->root = gnode->next; | |
520 if(gnode->prev) | |
521 gnode->prev->next = gnode->next; | |
522 if(gnode->next) | |
523 gnode->next->prev = gnode->prev; | |
524 | |
525 save = TRUE; | |
526 } | |
527 | |
528 gnode->next = node->next; | |
529 gnode->prev = node; | |
530 if(node->next) | |
531 node->next->prev = gnode; | |
532 node->next = gnode; | |
533 | |
534 if (ops) { | |
535 ops->update(gaimbuddylist, gnode); | |
536 for(node = gnode->child; node; node = node->next) | |
537 ops->update(gaimbuddylist, node); | |
538 } | |
539 if (save) | |
540 gaim_blist_save(); | |
541 } | |
542 | |
543 void gaim_blist_remove_buddy (struct buddy *buddy) | |
544 { | |
545 struct gaim_blist_ui_ops *ops = gaimbuddylist->ui_ops; | |
546 | |
547 GaimBlistNode *gnode, *node = (GaimBlistNode*)buddy; | |
548 struct group *group; | |
5247 | 549 struct _gaim_hbuddy hb, *key; |
550 struct buddy *val; | |
5228 | 551 |
552 gnode = node->parent; | |
553 group = (struct group *)gnode; | |
554 | |
555 if(gnode->child == node) | |
556 gnode->child = node->next; | |
557 if (node->prev) | |
558 node->prev->next = node->next; | |
559 if (node->next) | |
560 node->next->prev = node->prev; | |
5277 | 561 group->totalsize--; |
562 if (buddy->account->gc) | |
563 group->currentsize--; | |
5228 | 564 |
5247 | 565 hb.name = normalize(buddy->name); |
566 hb.account = buddy->account; | |
567 if (g_hash_table_lookup_extended(gaimbuddylist->buddies, &hb, (gpointer *)&key, (gpointer *)&val)) { | |
568 g_hash_table_remove(gaimbuddylist->buddies, &hb); | |
569 g_free(key->name); | |
570 g_free(key); | |
571 } | |
572 | |
5228 | 573 ops->remove(gaimbuddylist, node); |
574 g_hash_table_destroy(buddy->settings); | |
575 g_free(buddy->name); | |
576 g_free(buddy->alias); | |
577 g_free(buddy); | |
578 } | |
579 | |
5234 | 580 void gaim_blist_remove_chat (struct chat *chat) |
581 { | |
582 struct gaim_blist_ui_ops *ops = gaimbuddylist->ui_ops; | |
583 | |
584 GaimBlistNode *gnode, *node = (GaimBlistNode*)chat; | |
585 struct group *group; | |
586 | |
587 gnode = node->parent; | |
588 group = (struct group *)gnode; | |
589 | |
590 if(gnode->child == node) | |
591 gnode->child = node->next; | |
592 if (node->prev) | |
593 node->prev->next = node->next; | |
594 if (node->next) | |
595 node->next->prev = node->prev; | |
5277 | 596 group->totalsize--; |
597 if (chat->account->gc) | |
598 group->currentsize--; | |
5234 | 599 |
600 ops->remove(gaimbuddylist, node); | |
601 g_hash_table_destroy(chat->components); | |
602 g_free(chat->alias); | |
603 g_free(chat); | |
604 } | |
605 | |
5228 | 606 void gaim_blist_remove_group (struct group *group) |
607 { | |
608 struct gaim_blist_ui_ops *ops = gaimbuddylist->ui_ops; | |
609 GaimBlistNode *node = (GaimBlistNode*)group; | |
610 | |
611 if(node->child) { | |
612 char *buf; | |
613 int count = 0; | |
614 GaimBlistNode *child = node->child; | |
615 | |
616 while(child) { | |
617 count++; | |
618 child = child->next; | |
619 } | |
620 | |
621 buf = g_strdup_printf(_("%d buddies from group %s were not " | |
622 "removed because their accounts were not logged in. These " | |
623 "buddies, and the group were not removed.\n"), | |
624 count, group->name); | |
625 do_error_dialog(_("Group Not Removed"), buf, GAIM_ERROR); | |
626 g_free(buf); | |
627 return; | |
628 } | |
629 | |
630 if(gaimbuddylist->root == node) | |
631 gaimbuddylist->root = node->next; | |
632 if (node->prev) | |
633 node->prev->next = node->next; | |
634 if (node->next) | |
635 node->next->prev = node->prev; | |
636 | |
637 ops->remove(gaimbuddylist, node); | |
638 g_free(group->name); | |
639 g_free(group); | |
640 } | |
641 | |
642 char *gaim_get_buddy_alias_only(struct buddy *b) { | |
643 if(!b) | |
644 return NULL; | |
645 if(b->alias && b->alias[0]) | |
646 return b->alias; | |
647 else if((misc_options & OPT_MISC_USE_SERVER_ALIAS) && b->server_alias) | |
648 return b->server_alias; | |
649 return NULL; | |
650 } | |
651 | |
652 char * gaim_get_buddy_alias (struct buddy *buddy) | |
653 { | |
654 char *ret = gaim_get_buddy_alias_only(buddy); | |
655 if(!ret) | |
656 return buddy ? buddy->name : _("Unknown"); | |
657 return ret; | |
658 | |
659 } | |
660 | |
661 struct buddy *gaim_find_buddy(struct gaim_account *account, const char *name) | |
662 { | |
5247 | 663 struct buddy *buddy; |
664 struct _gaim_hbuddy hb; | |
5228 | 665 |
666 if (!gaimbuddylist) | |
667 return NULL; | |
668 | |
5247 | 669 hb.name = normalize(name); |
670 hb.account = account; | |
671 buddy = g_hash_table_lookup(gaimbuddylist->buddies, &hb); | |
672 | |
673 return buddy; | |
5228 | 674 } |
675 | |
676 struct group *gaim_find_group(const char *name) | |
677 { | |
678 GaimBlistNode *node; | |
679 if (!gaimbuddylist) | |
680 return NULL; | |
681 node = gaimbuddylist->root; | |
682 while(node) { | |
683 if (!strcmp(((struct group*)node)->name, name)) | |
684 return (struct group*)node; | |
685 node = node->next; | |
686 } | |
687 return NULL; | |
688 } | |
689 struct group *gaim_find_buddys_group(struct buddy *buddy) | |
690 { | |
691 if (!buddy) | |
692 return NULL; | |
693 return (struct group*)(((GaimBlistNode*)buddy)->parent); | |
694 } | |
695 | |
696 GSList *gaim_group_get_accounts(struct group *g) | |
697 { | |
698 GSList *l = NULL; | |
699 GaimBlistNode *child = ((GaimBlistNode *)g)->child; | |
700 | |
701 while (child) { | |
702 if (!g_slist_find(l, ((struct buddy*)child)->account)) | |
703 l = g_slist_append(l, ((struct buddy*)child)->account); | |
704 child = child->next; | |
705 } | |
706 return l; | |
707 } | |
708 | |
5234 | 709 void gaim_blist_add_account(struct gaim_account *account) |
710 { | |
711 struct gaim_blist_ui_ops *ops = gaimbuddylist->ui_ops; | |
712 GaimBlistNode *group, *buddy; | |
713 | |
714 if(!gaimbuddylist) | |
715 return; | |
716 | |
717 for(group = gaimbuddylist->root; group; group = group->next) { | |
718 if(!GAIM_BLIST_NODE_IS_GROUP(group)) | |
719 continue; | |
720 for(buddy = group->child; buddy; buddy = buddy->next) { | |
721 if(GAIM_BLIST_NODE_IS_BUDDY(buddy)) { | |
722 if (account == ((struct buddy*)buddy)->account) { | |
5277 | 723 ((struct group *)group)->currentsize++; |
5234 | 724 if(ops) |
725 ops->update(gaimbuddylist, buddy); | |
726 } | |
727 } else if(GAIM_BLIST_NODE_IS_CHAT(buddy)) { | |
728 if (account == ((struct chat*)buddy)->account) { | |
5277 | 729 ((struct group *)group)->currentsize++; |
5234 | 730 if(ops) |
731 ops->update(gaimbuddylist, buddy); | |
732 } | |
733 } | |
734 } | |
735 } | |
736 } | |
737 | |
5228 | 738 void gaim_blist_remove_account(struct gaim_account *account) |
739 { | |
740 struct gaim_blist_ui_ops *ops = gaimbuddylist->ui_ops; | |
5234 | 741 GaimBlistNode *group, *buddy; |
742 | |
5228 | 743 if (!gaimbuddylist) |
744 return; | |
5234 | 745 |
746 for(group = gaimbuddylist->root; group; group = group->next) { | |
747 if(!GAIM_BLIST_NODE_IS_GROUP(group)) | |
748 continue; | |
749 for(buddy = group->child; buddy; buddy = buddy->next) { | |
750 if(GAIM_BLIST_NODE_IS_BUDDY(buddy)) { | |
751 if (account == ((struct buddy*)buddy)->account) { | |
5277 | 752 if (((struct buddy *)buddy)->present) |
753 ((struct group *)group)->online--; | |
5234 | 754 ((struct buddy*)buddy)->present = GAIM_BUDDY_OFFLINE; |
5277 | 755 ((struct group *)group)->currentsize--; |
5234 | 756 if(ops) |
757 ops->remove(gaimbuddylist, buddy); | |
758 } | |
759 } else if(GAIM_BLIST_NODE_IS_CHAT(buddy)) { | |
760 if (account == ((struct chat*)buddy)->account) { | |
5277 | 761 ((struct group *)group)->online--; |
762 ((struct group *)group)->currentsize--; | |
5234 | 763 if(ops) |
764 ops->remove(gaimbuddylist, buddy); | |
765 } | |
5228 | 766 } |
767 } | |
768 } | |
769 } | |
770 | |
771 void parse_toc_buddy_list(struct gaim_account *account, char *config) | |
772 { | |
773 char *c; | |
774 char current[256]; | |
775 GList *bud = NULL; | |
776 | |
777 | |
778 if (config != NULL) { | |
779 | |
780 /* skip "CONFIG:" (if it exists) */ | |
781 c = strncmp(config + 6 /* sizeof(struct sflap_hdr) */ , "CONFIG:", strlen("CONFIG:")) ? | |
782 strtok(config, "\n") : | |
783 strtok(config + 6 /* sizeof(struct sflap_hdr) */ + strlen("CONFIG:"), "\n"); | |
784 do { | |
785 if (c == NULL) | |
786 break; | |
787 if (*c == 'g') { | |
788 char *utf8 = NULL; | |
789 utf8 = gaim_try_conv_to_utf8(c + 2); | |
790 if (utf8 == NULL) { | |
791 g_strlcpy(current, _("Invalid Groupname"), sizeof(current)); | |
792 } else { | |
793 g_strlcpy(current, utf8, sizeof(current)); | |
794 g_free(utf8); | |
795 } | |
796 if (!gaim_find_group(current)) { | |
797 struct group *g = gaim_group_new(current); | |
798 gaim_blist_add_group(g, NULL); | |
799 } | |
800 } else if (*c == 'b') { /*&& !gaim_find_buddy(user, c + 2)) {*/ | |
801 char nm[80], sw[388], *a, *utf8 = NULL; | |
802 | |
803 if ((a = strchr(c + 2, ':')) != NULL) { | |
804 *a++ = '\0'; /* nul the : */ | |
805 } | |
806 | |
807 g_strlcpy(nm, c + 2, sizeof(nm)); | |
808 if (a) { | |
809 utf8 = gaim_try_conv_to_utf8(a); | |
810 if (utf8 == NULL) { | |
811 gaim_debug(GAIM_DEBUG_ERROR, "toc blist", | |
812 "Failed to convert alias for " | |
813 "'%s' to UTF-8\n", nm); | |
814 } | |
815 } | |
816 if (utf8 == NULL) { | |
817 sw[0] = '\0'; | |
818 } else { | |
819 /* This can leave a partial sequence at the end, | |
820 * but who cares? */ | |
821 g_strlcpy(sw, utf8, sizeof(sw)); | |
822 g_free(utf8); | |
823 } | |
824 | |
825 if (!gaim_find_buddy(account, nm)) { | |
826 struct buddy *b = gaim_buddy_new(account, nm, sw); | |
827 struct group *g = gaim_find_group(current); | |
828 gaim_blist_add_buddy(b, g, NULL); | |
829 bud = g_list_append(bud, g_strdup(nm)); | |
830 } | |
831 } else if (*c == 'p') { | |
832 gaim_privacy_permit_add(account, c + 2); | |
833 } else if (*c == 'd') { | |
834 gaim_privacy_deny_add(account, c + 2); | |
835 } else if (!strncmp("toc", c, 3)) { | |
836 sscanf(c + strlen(c) - 1, "%d", &account->permdeny); | |
837 gaim_debug(GAIM_DEBUG_MISC, "toc blist", | |
838 "permdeny: %d\n", account->permdeny); | |
839 if (account->permdeny == 0) | |
840 account->permdeny = 1; | |
841 } else if (*c == 'm') { | |
842 sscanf(c + 2, "%d", &account->permdeny); | |
843 gaim_debug(GAIM_DEBUG_MISC, "toc blist", | |
844 "permdeny: %d\n", account->permdeny); | |
845 if (account->permdeny == 0) | |
846 account->permdeny = 1; | |
847 } | |
848 } while ((c = strtok(NULL, "\n"))); | |
849 | |
850 if(account->gc) { | |
851 if(bud) { | |
852 GList *node = bud; | |
853 serv_add_buddies(account->gc, bud); | |
854 while(node) { | |
855 g_free(node->data); | |
856 node = node->next; | |
857 } | |
858 } | |
859 serv_set_permit_deny(account->gc); | |
860 } | |
861 g_list_free(bud); | |
862 } | |
863 } | |
864 | |
865 #if 0 | |
866 /* translate an AIM 3 buddylist (*.lst) to a Gaim buddylist */ | |
867 static GString *translate_lst(FILE *src_fp) | |
868 { | |
869 char line[BUF_LEN], *line2; | |
870 char *name; | |
871 int i; | |
872 | |
873 GString *dest = g_string_new("m 1\n"); | |
874 | |
875 while (fgets(line, BUF_LEN, src_fp)) { | |
876 line2 = g_strchug(line); | |
877 if (strstr(line2, "group") == line2) { | |
878 name = strpbrk(line2, " \t\n\r\f") + 1; | |
879 dest = g_string_append(dest, "g "); | |
880 for (i = 0; i < strcspn(name, "\n\r"); i++) | |
881 if (name[i] != '\"') | |
882 dest = g_string_append_c(dest, name[i]); | |
883 dest = g_string_append_c(dest, '\n'); | |
884 } | |
885 if (strstr(line2, "buddy") == line2) { | |
886 name = strpbrk(line2, " \t\n\r\f") + 1; | |
887 dest = g_string_append(dest, "b "); | |
888 for (i = 0; i < strcspn(name, "\n\r"); i++) | |
889 if (name[i] != '\"') | |
890 dest = g_string_append_c(dest, name[i]); | |
891 dest = g_string_append_c(dest, '\n'); | |
892 } | |
893 } | |
894 | |
895 return dest; | |
896 } | |
897 | |
898 | |
899 /* translate an AIM 4 buddylist (*.blt) to Gaim format */ | |
900 static GString *translate_blt(FILE *src_fp) | |
901 { | |
902 int i; | |
903 char line[BUF_LEN]; | |
904 char *buddy; | |
905 | |
906 GString *dest = g_string_new("m 1\n"); | |
907 | |
908 while (strstr(fgets(line, BUF_LEN, src_fp), "Buddy") == NULL); | |
909 while (strstr(fgets(line, BUF_LEN, src_fp), "list") == NULL); | |
910 | |
911 while (1) { | |
912 fgets(line, BUF_LEN, src_fp); g_strchomp(line); | |
913 if (strchr(line, '}') != NULL) | |
914 break; | |
915 | |
916 if (strchr(line, '{') != NULL) { | |
917 /* Syntax starting with "<group> {" */ | |
918 | |
919 dest = g_string_append(dest, "g "); | |
920 buddy = g_strchug(strtok(line, "{")); | |
921 for (i = 0; i < strlen(buddy); i++) | |
922 if (buddy[i] != '\"') | |
923 dest = g_string_append_c(dest, buddy[i]); | |
924 dest = g_string_append_c(dest, '\n'); | |
925 while (strchr(fgets(line, BUF_LEN, src_fp), '}') == NULL) { | |
926 gboolean pounce = FALSE; | |
927 char *e; | |
928 g_strchomp(line); | |
929 buddy = g_strchug(line); | |
930 gaim_debug(GAIM_DEBUG_MISC, "AIM 4 blt import", | |
931 "buddy: \"%s\"\n", buddy); | |
932 dest = g_string_append(dest, "b "); | |
933 if (strchr(buddy, '{') != NULL) { | |
934 /* buddy pounce, etc */ | |
935 char *pos = strchr(buddy, '{') - 1; | |
936 *pos = 0; | |
937 pounce = TRUE; | |
938 } | |
939 if ((e = strchr(buddy, '\"')) != NULL) { | |
940 *e = '\0'; | |
941 buddy++; | |
942 } | |
943 dest = g_string_append(dest, buddy); | |
944 dest = g_string_append_c(dest, '\n'); | |
945 if (pounce) | |
946 do | |
947 fgets(line, BUF_LEN, src_fp); | |
948 while (!strchr(line, '}')); | |
949 } | |
950 } else { | |
951 | |
952 /* Syntax "group buddy buddy ..." */ | |
953 buddy = g_strchug(strtok(line, " \n")); | |
954 dest = g_string_append(dest, "g "); | |
955 if (strchr(buddy, '\"') != NULL) { | |
956 dest = g_string_append(dest, &buddy[1]); | |
957 dest = g_string_append_c(dest, ' '); | |
958 buddy = g_strchug(strtok(NULL, " \n")); | |
959 while (strchr(buddy, '\"') == NULL) { | |
960 dest = g_string_append(dest, buddy); | |
961 dest = g_string_append_c(dest, ' '); | |
962 buddy = g_strchug(strtok(NULL, " \n")); | |
963 } | |
964 buddy[strlen(buddy) - 1] = '\0'; | |
965 dest = g_string_append(dest, buddy); | |
966 } else { | |
967 dest = g_string_append(dest, buddy); | |
968 } | |
969 dest = g_string_append_c(dest, '\n'); | |
970 while ((buddy = g_strchug(strtok(NULL, " \n"))) != NULL) { | |
971 dest = g_string_append(dest, "b "); | |
972 if (strchr(buddy, '\"') != NULL) { | |
973 dest = g_string_append(dest, &buddy[1]); | |
974 dest = g_string_append_c(dest, ' '); | |
975 buddy = g_strchug(strtok(NULL, " \n")); | |
976 while (strchr(buddy, '\"') == NULL) { | |
977 dest = g_string_append(dest, buddy); | |
978 dest = g_string_append_c(dest, ' '); | |
979 buddy = g_strchug(strtok(NULL, " \n")); | |
980 } | |
981 buddy[strlen(buddy) - 1] = '\0'; | |
982 dest = g_string_append(dest, buddy); | |
983 } else { | |
984 dest = g_string_append(dest, buddy); | |
985 } | |
986 dest = g_string_append_c(dest, '\n'); | |
987 } | |
988 } | |
989 } | |
990 | |
991 return dest; | |
992 } | |
993 | |
994 static GString *translate_gnomeicu(FILE *src_fp) | |
995 { | |
996 char line[BUF_LEN]; | |
997 GString *dest = g_string_new("m 1\ng Buddies\n"); | |
998 | |
999 while (strstr(fgets(line, BUF_LEN, src_fp), "NewContacts") == NULL); | |
1000 | |
1001 while (fgets(line, BUF_LEN, src_fp)) { | |
1002 char *eq; | |
1003 g_strchomp(line); | |
1004 if (line[0] == '\n' || line[0] == '[') | |
1005 break; | |
1006 eq = strchr(line, '='); | |
1007 if (!eq) | |
1008 break; | |
1009 *eq = ':'; | |
1010 eq = strchr(eq, ','); | |
1011 if (eq) | |
1012 *eq = '\0'; | |
1013 dest = g_string_append(dest, "b "); | |
1014 dest = g_string_append(dest, line); | |
1015 dest = g_string_append_c(dest, '\n'); | |
1016 } | |
1017 | |
1018 return dest; | |
1019 } | |
1020 #endif | |
1021 | |
1022 static gchar *get_screenname_filename(const char *name) | |
1023 { | |
1024 gchar **split; | |
1025 gchar *good; | |
1026 gchar *ret; | |
1027 | |
1028 split = g_strsplit(name, G_DIR_SEPARATOR_S, -1); | |
1029 good = g_strjoinv(NULL, split); | |
1030 g_strfreev(split); | |
1031 | |
1032 ret = g_utf8_strup(good, -1); | |
1033 | |
1034 g_free(good); | |
1035 | |
1036 return ret; | |
1037 } | |
1038 | |
1039 static gboolean gaim_blist_read(const char *filename); | |
1040 | |
1041 | |
1042 static void do_import(struct gaim_account *account, const char *filename) | |
1043 { | |
1044 GString *buf = NULL; | |
1045 char first[64]; | |
1046 char path[PATHSIZE]; | |
1047 int len; | |
1048 FILE *f; | |
1049 struct stat st; | |
1050 | |
1051 if (filename) { | |
1052 g_snprintf(path, sizeof(path), "%s", filename); | |
1053 } else { | |
1054 char *g_screenname = get_screenname_filename(account->username); | |
1055 char *file = gaim_user_dir(); | |
1056 int protocol = (account->protocol == GAIM_PROTO_OSCAR) ? (isalpha(account->username[0]) ? GAIM_PROTO_TOC : GAIM_PROTO_ICQ): account->protocol; | |
1057 | |
1058 if (file != (char *)NULL) { | |
1059 sprintf(path, "%s" G_DIR_SEPARATOR_S "%s.%d.blist", file, g_screenname, protocol); | |
1060 g_free(g_screenname); | |
1061 } else { | |
1062 g_free(g_screenname); | |
1063 return; | |
1064 } | |
1065 } | |
1066 | |
1067 if (stat(path, &st)) { | |
1068 gaim_debug(GAIM_DEBUG_ERROR, "blist import", "Unable to stat %s.\n", | |
1069 path); | |
1070 return; | |
1071 } | |
1072 | |
1073 if (!(f = fopen(path, "r"))) { | |
1074 gaim_debug(GAIM_DEBUG_ERROR, "blist import", "Unable to open %s.\n", | |
1075 path); | |
1076 return; | |
1077 } | |
1078 | |
1079 fgets(first, 64, f); | |
1080 | |
1081 if ((first[0] == '\n') || (first[0] == '\r' && first[1] == '\n')) | |
1082 fgets(first, 64, f); | |
1083 | |
1084 #if 0 | |
1085 if (!g_strncasecmp(first, "<xml", strlen("<xml"))) { | |
1086 /* new gaim XML buddy list */ | |
1087 gaim_blist_read(path); | |
1088 | |
1089 /* We really don't need to bother doing stuf like translating AIM 3 buddy lists anymore */ | |
1090 | |
1091 } else if (!g_strncasecmp(first, "Config {", strlen("Config {"))) { | |
1092 /* AIM 4 buddy list */ | |
1093 gaim_debug(GAIM_DEBUG_MISC, "blist import", "aim 4\n"); | |
1094 rewind(f); | |
1095 buf = translate_blt(f); | |
1096 } else if (strstr(first, "group") != NULL) { | |
1097 /* AIM 3 buddy list */ | |
1098 gaim_debug(GAIM_DEBUG_MISC, "blist import", "aim 3\n"); | |
1099 rewind(f); | |
1100 buf = translate_lst(f); | |
1101 } else if (!g_strncasecmp(first, "[User]", strlen("[User]"))) { | |
1102 /* GnomeICU (hopefully) */ | |
1103 gaim_debug(GAIM_DEBUG_MISC, "blist import", "gnomeicu\n"); | |
1104 rewind(f); | |
1105 buf = translate_gnomeicu(f); | |
1106 | |
1107 } else | |
1108 #endif | |
1109 if (first[0] == 'm') { | |
1110 /* Gaim buddy list - no translation */ | |
1111 char buf2[BUF_LONG * 2]; | |
1112 buf = g_string_new(""); | |
1113 rewind(f); | |
1114 while (1) { | |
1115 len = fread(buf2, 1, BUF_LONG * 2 - 1, f); | |
1116 if (len <= 0) | |
1117 break; | |
1118 buf2[len] = '\0'; | |
1119 buf = g_string_append(buf, buf2); | |
1120 if (len != BUF_LONG * 2 - 1) | |
1121 break; | |
1122 } | |
1123 } | |
1124 | |
1125 fclose(f); | |
1126 | |
1127 if (buf) { | |
1128 buf = g_string_prepend(buf, "toc_set_config {"); | |
1129 buf = g_string_append(buf, "}\n"); | |
1130 parse_toc_buddy_list(account, buf->str); | |
1131 g_string_free(buf, TRUE); | |
1132 } | |
1133 } | |
1134 | |
1135 gboolean gaim_group_on_account(struct group *g, struct gaim_account *account) { | |
1136 GaimBlistNode *bnode; | |
1137 for(bnode = g->node.child; bnode; bnode = bnode->next) { | |
1138 struct buddy *b = (struct buddy *)bnode; | |
1139 if(!GAIM_BLIST_NODE_IS_BUDDY(bnode)) | |
1140 continue; | |
1141 if((!account && b->account->gc) || b->account == account) | |
1142 return TRUE; | |
1143 } | |
1144 return FALSE; | |
1145 } | |
1146 | |
1147 static gboolean blist_safe_to_write = FALSE; | |
1148 | |
1149 static char *blist_parser_group_name = NULL; | |
1150 static char *blist_parser_person_name = NULL; | |
1151 static char *blist_parser_account_name = NULL; | |
1152 static int blist_parser_account_protocol = 0; | |
5234 | 1153 static char *blist_parser_chat_alias = NULL; |
1154 static char *blist_parser_component_name = NULL; | |
1155 static char *blist_parser_component_value = NULL; | |
5228 | 1156 static char *blist_parser_buddy_name = NULL; |
1157 static char *blist_parser_buddy_alias = NULL; | |
1158 static char *blist_parser_setting_name = NULL; | |
1159 static char *blist_parser_setting_value = NULL; | |
1160 static GHashTable *blist_parser_buddy_settings = NULL; | |
1161 static GHashTable *blist_parser_group_settings = NULL; | |
5234 | 1162 static GHashTable *blist_parser_chat_components = NULL; |
5228 | 1163 static int blist_parser_privacy_mode = 0; |
1164 static GList *tag_stack = NULL; | |
1165 enum { | |
1166 BLIST_TAG_GAIM, | |
1167 BLIST_TAG_BLIST, | |
1168 BLIST_TAG_GROUP, | |
5234 | 1169 BLIST_TAG_CHAT, |
1170 BLIST_TAG_COMPONENT, | |
5228 | 1171 BLIST_TAG_PERSON, |
1172 BLIST_TAG_BUDDY, | |
1173 BLIST_TAG_NAME, | |
1174 BLIST_TAG_ALIAS, | |
1175 BLIST_TAG_SETTING, | |
1176 BLIST_TAG_PRIVACY, | |
1177 BLIST_TAG_ACCOUNT, | |
1178 BLIST_TAG_PERMIT, | |
1179 BLIST_TAG_BLOCK, | |
1180 BLIST_TAG_IGNORE | |
1181 }; | |
1182 static gboolean blist_parser_error_occurred = FALSE; | |
1183 | |
1184 static void blist_start_element_handler (GMarkupParseContext *context, | |
1185 const gchar *element_name, | |
1186 const gchar **attribute_names, | |
1187 const gchar **attribute_values, | |
1188 gpointer user_data, | |
1189 GError **error) { | |
1190 int i; | |
1191 | |
1192 if(!strcmp(element_name, "gaim")) { | |
1193 tag_stack = g_list_prepend(tag_stack, GINT_TO_POINTER(BLIST_TAG_GAIM)); | |
1194 } else if(!strcmp(element_name, "blist")) { | |
1195 tag_stack = g_list_prepend(tag_stack, GINT_TO_POINTER(BLIST_TAG_BLIST)); | |
1196 } else if(!strcmp(element_name, "group")) { | |
1197 tag_stack = g_list_prepend(tag_stack, GINT_TO_POINTER(BLIST_TAG_GROUP)); | |
1198 for(i=0; attribute_names[i]; i++) { | |
1199 if(!strcmp(attribute_names[i], "name")) { | |
1200 g_free(blist_parser_group_name); | |
1201 blist_parser_group_name = g_strdup(attribute_values[i]); | |
1202 } | |
1203 } | |
1204 if(blist_parser_group_name) { | |
1205 struct group *g = gaim_group_new(blist_parser_group_name); | |
1206 gaim_blist_add_group(g,NULL); | |
1207 } | |
5234 | 1208 } else if(!strcmp(element_name, "chat")) { |
1209 tag_stack = g_list_prepend(tag_stack, GINT_TO_POINTER(BLIST_TAG_CHAT)); | |
1210 for(i=0; attribute_names[i]; i++) { | |
1211 if(!strcmp(attribute_names[i], "account")) { | |
1212 g_free(blist_parser_account_name); | |
1213 blist_parser_account_name = g_strdup(attribute_values[i]); | |
1214 } else if(!strcmp(attribute_names[i], "protocol")) { | |
1215 blist_parser_account_protocol = atoi(attribute_values[i]); | |
1216 } | |
1217 } | |
5228 | 1218 } else if(!strcmp(element_name, "person")) { |
1219 tag_stack = g_list_prepend(tag_stack, GINT_TO_POINTER(BLIST_TAG_PERSON)); | |
1220 for(i=0; attribute_names[i]; i++) { | |
1221 if(!strcmp(attribute_names[i], "name")) { | |
1222 g_free(blist_parser_person_name); | |
1223 blist_parser_person_name = g_strdup(attribute_values[i]); | |
1224 } | |
1225 } | |
1226 } else if(!strcmp(element_name, "buddy")) { | |
1227 tag_stack = g_list_prepend(tag_stack, GINT_TO_POINTER(BLIST_TAG_BUDDY)); | |
1228 for(i=0; attribute_names[i]; i++) { | |
1229 if(!strcmp(attribute_names[i], "account")) { | |
1230 g_free(blist_parser_account_name); | |
1231 blist_parser_account_name = g_strdup(attribute_values[i]); | |
1232 } else if(!strcmp(attribute_names[i], "protocol")) { | |
1233 blist_parser_account_protocol = atoi(attribute_values[i]); | |
1234 } | |
1235 } | |
1236 } else if(!strcmp(element_name, "name")) { | |
1237 tag_stack = g_list_prepend(tag_stack, GINT_TO_POINTER(BLIST_TAG_NAME)); | |
1238 } else if(!strcmp(element_name, "alias")) { | |
1239 tag_stack = g_list_prepend(tag_stack, GINT_TO_POINTER(BLIST_TAG_ALIAS)); | |
1240 } else if(!strcmp(element_name, "setting")) { | |
1241 tag_stack = g_list_prepend(tag_stack, GINT_TO_POINTER(BLIST_TAG_SETTING)); | |
1242 for(i=0; attribute_names[i]; i++) { | |
1243 if(!strcmp(attribute_names[i], "name")) { | |
1244 g_free(blist_parser_setting_name); | |
1245 blist_parser_setting_name = g_strdup(attribute_values[i]); | |
1246 } | |
1247 } | |
5234 | 1248 } else if(!strcmp(element_name, "component")) { |
1249 tag_stack = g_list_prepend(tag_stack, GINT_TO_POINTER(BLIST_TAG_COMPONENT)); | |
1250 for(i=0; attribute_names[i]; i++) { | |
1251 if(!strcmp(attribute_names[i], "name")) { | |
1252 g_free(blist_parser_component_name); | |
1253 blist_parser_component_name = g_strdup(attribute_values[i]); | |
1254 } | |
1255 } | |
1256 | |
5228 | 1257 } else if(!strcmp(element_name, "privacy")) { |
1258 tag_stack = g_list_prepend(tag_stack, GINT_TO_POINTER(BLIST_TAG_PRIVACY)); | |
1259 } else if(!strcmp(element_name, "account")) { | |
1260 tag_stack = g_list_prepend(tag_stack, GINT_TO_POINTER(BLIST_TAG_ACCOUNT)); | |
1261 for(i=0; attribute_names[i]; i++) { | |
1262 if(!strcmp(attribute_names[i], "protocol")) | |
1263 blist_parser_account_protocol = atoi(attribute_values[i]); | |
1264 else if(!strcmp(attribute_names[i], "mode")) | |
1265 blist_parser_privacy_mode = atoi(attribute_values[i]); | |
1266 else if(!strcmp(attribute_names[i], "name")) { | |
1267 g_free(blist_parser_account_name); | |
1268 blist_parser_account_name = g_strdup(attribute_values[i]); | |
1269 } | |
1270 } | |
1271 } else if(!strcmp(element_name, "permit")) { | |
1272 tag_stack = g_list_prepend(tag_stack, GINT_TO_POINTER(BLIST_TAG_PERMIT)); | |
1273 } else if(!strcmp(element_name, "block")) { | |
1274 tag_stack = g_list_prepend(tag_stack, GINT_TO_POINTER(BLIST_TAG_BLOCK)); | |
1275 } else if(!strcmp(element_name, "ignore")) { | |
1276 tag_stack = g_list_prepend(tag_stack, GINT_TO_POINTER(BLIST_TAG_IGNORE)); | |
1277 } | |
1278 } | |
1279 | |
1280 static void blist_end_element_handler(GMarkupParseContext *context, | |
1281 const gchar *element_name, gpointer user_data, GError **error) { | |
1282 if(!strcmp(element_name, "gaim")) { | |
1283 tag_stack = g_list_delete_link(tag_stack, tag_stack); | |
1284 } else if(!strcmp(element_name, "blist")) { | |
1285 tag_stack = g_list_delete_link(tag_stack, tag_stack); | |
1286 } else if(!strcmp(element_name, "group")) { | |
1287 if(blist_parser_group_settings) { | |
1288 struct group *g = gaim_find_group(blist_parser_group_name); | |
1289 g_hash_table_destroy(g->settings); | |
1290 g->settings = blist_parser_group_settings; | |
1291 } | |
1292 tag_stack = g_list_delete_link(tag_stack, tag_stack); | |
1293 blist_parser_group_settings = NULL; | |
5234 | 1294 } else if(!strcmp(element_name, "chat")) { |
1295 struct gaim_account *account = gaim_account_find(blist_parser_account_name, | |
1296 blist_parser_account_protocol); | |
5237 | 1297 if(account) { |
5234 | 1298 struct chat *chat = gaim_chat_new(account, blist_parser_chat_alias, blist_parser_chat_components); |
1299 struct group *g = gaim_find_group(blist_parser_group_name); | |
1300 gaim_blist_add_chat(chat,g,NULL); | |
1301 } | |
1302 g_free(blist_parser_chat_alias); | |
1303 blist_parser_chat_alias = NULL; | |
1304 g_free(blist_parser_account_name); | |
1305 blist_parser_account_name = NULL; | |
1306 blist_parser_chat_components = NULL; | |
1307 tag_stack = g_list_delete_link(tag_stack, tag_stack); | |
5228 | 1308 } else if(!strcmp(element_name, "person")) { |
1309 g_free(blist_parser_person_name); | |
1310 blist_parser_person_name = NULL; | |
1311 tag_stack = g_list_delete_link(tag_stack, tag_stack); | |
1312 } else if(!strcmp(element_name, "buddy")) { | |
1313 struct gaim_account *account = gaim_account_find(blist_parser_account_name, | |
1314 blist_parser_account_protocol); | |
1315 if(account) { | |
1316 struct buddy *b = gaim_buddy_new(account, blist_parser_buddy_name, blist_parser_buddy_alias); | |
1317 struct group *g = gaim_find_group(blist_parser_group_name); | |
1318 gaim_blist_add_buddy(b,g,NULL); | |
1319 if(blist_parser_buddy_settings) { | |
1320 g_hash_table_destroy(b->settings); | |
1321 b->settings = blist_parser_buddy_settings; | |
1322 } | |
1323 } | |
1324 g_free(blist_parser_buddy_name); | |
1325 blist_parser_buddy_name = NULL; | |
1326 g_free(blist_parser_buddy_alias); | |
1327 blist_parser_buddy_alias = NULL; | |
1328 g_free(blist_parser_account_name); | |
1329 blist_parser_account_name = NULL; | |
1330 blist_parser_buddy_settings = NULL; | |
1331 tag_stack = g_list_delete_link(tag_stack, tag_stack); | |
1332 } else if(!strcmp(element_name, "name")) { | |
1333 tag_stack = g_list_delete_link(tag_stack, tag_stack); | |
1334 } else if(!strcmp(element_name, "alias")) { | |
1335 tag_stack = g_list_delete_link(tag_stack, tag_stack); | |
5234 | 1336 } else if(!strcmp(element_name, "component")) { |
1337 if(!blist_parser_chat_components) | |
1338 blist_parser_chat_components = g_hash_table_new_full(g_str_hash, | |
1339 g_str_equal, g_free, g_free); | |
1340 if(blist_parser_component_name && blist_parser_component_value) { | |
1341 g_hash_table_replace(blist_parser_chat_components, | |
1342 g_strdup(blist_parser_component_name), | |
1343 g_strdup(blist_parser_component_value)); | |
1344 } | |
1345 g_free(blist_parser_component_name); | |
1346 g_free(blist_parser_component_value); | |
1347 blist_parser_component_name = NULL; | |
1348 blist_parser_component_value = NULL; | |
1349 tag_stack = g_list_delete_link(tag_stack, tag_stack); | |
5228 | 1350 } else if(!strcmp(element_name, "setting")) { |
1351 if(GPOINTER_TO_INT(tag_stack->next->data) == BLIST_TAG_BUDDY) { | |
1352 if(!blist_parser_buddy_settings) | |
1353 blist_parser_buddy_settings = g_hash_table_new_full(g_str_hash, | |
1354 g_str_equal, g_free, g_free); | |
1355 if(blist_parser_setting_name && blist_parser_setting_value) { | |
1356 g_hash_table_replace(blist_parser_buddy_settings, | |
1357 g_strdup(blist_parser_setting_name), | |
1358 g_strdup(blist_parser_setting_value)); | |
1359 } | |
1360 } else if(GPOINTER_TO_INT(tag_stack->next->data) == BLIST_TAG_GROUP) { | |
1361 if(!blist_parser_group_settings) | |
1362 blist_parser_group_settings = g_hash_table_new_full(g_str_hash, | |
1363 g_str_equal, g_free, g_free); | |
1364 if(blist_parser_setting_name && blist_parser_setting_value) { | |
1365 g_hash_table_replace(blist_parser_group_settings, | |
1366 g_strdup(blist_parser_setting_name), | |
1367 g_strdup(blist_parser_setting_value)); | |
1368 } | |
1369 } | |
1370 g_free(blist_parser_setting_name); | |
1371 g_free(blist_parser_setting_value); | |
1372 blist_parser_setting_name = NULL; | |
1373 blist_parser_setting_value = NULL; | |
1374 tag_stack = g_list_delete_link(tag_stack, tag_stack); | |
1375 } else if(!strcmp(element_name, "privacy")) { | |
1376 tag_stack = g_list_delete_link(tag_stack, tag_stack); | |
1377 } else if(!strcmp(element_name, "account")) { | |
1378 struct gaim_account *account = gaim_account_find(blist_parser_account_name, | |
1379 blist_parser_account_protocol); | |
1380 if(account) { | |
1381 account->permdeny = blist_parser_privacy_mode; | |
1382 } | |
1383 g_free(blist_parser_account_name); | |
1384 blist_parser_account_name = NULL; | |
1385 tag_stack = g_list_delete_link(tag_stack, tag_stack); | |
1386 } else if(!strcmp(element_name, "permit")) { | |
1387 struct gaim_account *account = gaim_account_find(blist_parser_account_name, | |
1388 blist_parser_account_protocol); | |
1389 if(account) { | |
1390 gaim_privacy_permit_add(account, blist_parser_buddy_name); | |
1391 } | |
1392 g_free(blist_parser_buddy_name); | |
1393 blist_parser_buddy_name = NULL; | |
1394 tag_stack = g_list_delete_link(tag_stack, tag_stack); | |
1395 } else if(!strcmp(element_name, "block")) { | |
1396 struct gaim_account *account = gaim_account_find(blist_parser_account_name, | |
1397 blist_parser_account_protocol); | |
1398 if(account) { | |
1399 gaim_privacy_deny_add(account, blist_parser_buddy_name); | |
1400 } | |
1401 g_free(blist_parser_buddy_name); | |
1402 blist_parser_buddy_name = NULL; | |
1403 tag_stack = g_list_delete_link(tag_stack, tag_stack); | |
1404 } else if(!strcmp(element_name, "ignore")) { | |
1405 /* we'll apparently do something with this later */ | |
1406 tag_stack = g_list_delete_link(tag_stack, tag_stack); | |
1407 } | |
1408 } | |
1409 | |
1410 static void blist_text_handler(GMarkupParseContext *context, const gchar *text, | |
1411 gsize text_len, gpointer user_data, GError **error) { | |
1412 switch(GPOINTER_TO_INT(tag_stack->data)) { | |
1413 case BLIST_TAG_NAME: | |
1414 blist_parser_buddy_name = g_strndup(text, text_len); | |
1415 break; | |
1416 case BLIST_TAG_ALIAS: | |
5234 | 1417 if(tag_stack->next && |
1418 GPOINTER_TO_INT(tag_stack->next->data) == BLIST_TAG_BUDDY) | |
1419 blist_parser_buddy_alias = g_strndup(text, text_len); | |
1420 else if(tag_stack->next && | |
1421 GPOINTER_TO_INT(tag_stack->next->data) == BLIST_TAG_CHAT) | |
1422 blist_parser_chat_alias = g_strndup(text, text_len); | |
5228 | 1423 break; |
1424 case BLIST_TAG_PERMIT: | |
1425 case BLIST_TAG_BLOCK: | |
1426 case BLIST_TAG_IGNORE: | |
1427 blist_parser_buddy_name = g_strndup(text, text_len); | |
1428 break; | |
5234 | 1429 case BLIST_TAG_COMPONENT: |
1430 blist_parser_component_value = g_strndup(text, text_len); | |
1431 break; | |
5228 | 1432 case BLIST_TAG_SETTING: |
1433 blist_parser_setting_value = g_strndup(text, text_len); | |
1434 break; | |
1435 default: | |
1436 break; | |
1437 } | |
1438 } | |
1439 | |
1440 static void blist_error_handler(GMarkupParseContext *context, GError *error, | |
1441 gpointer user_data) { | |
1442 blist_parser_error_occurred = TRUE; | |
1443 gaim_debug(GAIM_DEBUG_ERROR, "blist import", | |
1444 "Error parsing blist.xml: %s\n", error->message); | |
1445 } | |
1446 | |
1447 static GMarkupParser blist_parser = { | |
1448 blist_start_element_handler, | |
1449 blist_end_element_handler, | |
1450 blist_text_handler, | |
1451 NULL, | |
1452 blist_error_handler | |
1453 }; | |
1454 | |
1455 static gboolean gaim_blist_read(const char *filename) { | |
1456 gchar *contents = NULL; | |
1457 gsize length; | |
1458 GMarkupParseContext *context; | |
1459 GError *error = NULL; | |
1460 | |
1461 gaim_debug(GAIM_DEBUG_INFO, "blist import", | |
1462 "Reading %s\n", filename); | |
1463 if(!g_file_get_contents(filename, &contents, &length, &error)) { | |
1464 gaim_debug(GAIM_DEBUG_ERROR, "blist import", | |
1465 "Error reading blist: %s\n", error->message); | |
1466 g_error_free(error); | |
1467 return FALSE; | |
1468 } | |
1469 | |
1470 context = g_markup_parse_context_new(&blist_parser, 0, NULL, NULL); | |
1471 | |
1472 if(!g_markup_parse_context_parse(context, contents, length, NULL)) { | |
1473 g_markup_parse_context_free(context); | |
1474 g_free(contents); | |
1475 return FALSE; | |
1476 } | |
1477 | |
1478 if(!g_markup_parse_context_end_parse(context, NULL)) { | |
1479 gaim_debug(GAIM_DEBUG_ERROR, "blist import", | |
1480 "Error parsing %s\n", filename); | |
1481 g_markup_parse_context_free(context); | |
1482 g_free(contents); | |
1483 return FALSE; | |
1484 } | |
1485 | |
1486 g_markup_parse_context_free(context); | |
1487 g_free(contents); | |
1488 | |
1489 if(blist_parser_error_occurred) | |
1490 return FALSE; | |
1491 | |
1492 gaim_debug(GAIM_DEBUG_INFO, "blist import", "Finished reading %s\n", | |
1493 filename); | |
1494 | |
1495 return TRUE; | |
1496 } | |
1497 | |
1498 void gaim_blist_load() { | |
1499 GSList *accts; | |
1500 char *user_dir = gaim_user_dir(); | |
1501 char *filename; | |
1502 char *msg; | |
1503 | |
1504 blist_safe_to_write = TRUE; | |
1505 | |
1506 if(!user_dir) | |
1507 return; | |
1508 | |
1509 filename = g_build_filename(user_dir, "blist.xml", NULL); | |
1510 | |
1511 if(g_file_test(filename, G_FILE_TEST_EXISTS)) { | |
1512 if(!gaim_blist_read(filename)) { | |
1513 msg = g_strdup_printf(_("An error was encountered parsing your " | |
1514 "buddy list. It has not been loaded.")); | |
1515 do_error_dialog(_("Buddy List Error"), msg, GAIM_ERROR); | |
1516 g_free(msg); | |
1517 } | |
1518 } else if(g_slist_length(gaim_accounts)) { | |
1519 /* rob wants to inform the user that their buddy lists are | |
1520 * being converted */ | |
1521 msg = g_strdup_printf(_("Gaim is converting your old buddy lists " | |
1522 "to a new format, which will now be located at %s"), | |
1523 filename); | |
1524 do_error_dialog(_("Converting Buddy List"), msg, GAIM_INFO); | |
1525 g_free(msg); | |
1526 | |
1527 /* now, let gtk actually display the dialog before we start anything */ | |
1528 while(gtk_events_pending()) | |
1529 gtk_main_iteration(); | |
1530 | |
1531 /* read in the old lists, then save to the new format */ | |
1532 for(accts = gaim_accounts; accts; accts = accts->next) { | |
1533 do_import(accts->data, NULL); | |
1534 } | |
1535 gaim_blist_save(); | |
1536 } | |
1537 | |
1538 g_free(filename); | |
1539 } | |
1540 | |
1541 static void blist_print_group_settings(gpointer key, gpointer data, | |
1542 gpointer user_data) { | |
1543 char *key_val; | |
1544 char *data_val; | |
1545 FILE *file = user_data; | |
1546 | |
1547 if(!key || !data) | |
1548 return; | |
1549 | |
1550 key_val = g_markup_escape_text(key, -1); | |
1551 data_val = g_markup_escape_text(data, -1); | |
1552 | |
1553 fprintf(file, "\t\t\t<setting name=\"%s\">%s</setting>\n", key_val, | |
1554 data_val); | |
1555 g_free(key_val); | |
1556 g_free(data_val); | |
1557 } | |
1558 | |
1559 static void blist_print_buddy_settings(gpointer key, gpointer data, | |
1560 gpointer user_data) { | |
1561 char *key_val; | |
1562 char *data_val; | |
1563 FILE *file = user_data; | |
1564 | |
1565 if(!key || !data) | |
1566 return; | |
1567 | |
1568 key_val = g_markup_escape_text(key, -1); | |
1569 data_val = g_markup_escape_text(data, -1); | |
1570 | |
1571 fprintf(file, "\t\t\t\t\t<setting name=\"%s\">%s</setting>\n", key_val, | |
1572 data_val); | |
1573 g_free(key_val); | |
1574 g_free(data_val); | |
1575 } | |
1576 | |
5234 | 1577 static void blist_print_chat_components(gpointer key, gpointer data, |
1578 gpointer user_data) { | |
1579 char *key_val; | |
1580 char *data_val; | |
1581 FILE *file = user_data; | |
1582 | |
1583 if(!key || !data) | |
1584 return; | |
1585 | |
1586 key_val = g_markup_escape_text(key, -1); | |
1587 data_val = g_markup_escape_text(data, -1); | |
1588 | |
1589 fprintf(file, "\t\t\t\t<component name=\"%s\">%s</component>\n", key_val, | |
1590 data_val); | |
1591 g_free(key_val); | |
1592 g_free(data_val); | |
1593 } | |
1594 | |
5228 | 1595 static void gaim_blist_write(FILE *file, struct gaim_account *exp_acct) { |
1596 GSList *accounts, *buds; | |
1597 GaimBlistNode *gnode,*bnode; | |
1598 struct group *group; | |
1599 struct buddy *bud; | |
1600 fprintf(file, "<?xml version='1.0' encoding='UTF-8' ?>\n"); | |
1601 fprintf(file, "<gaim version=\"1\">\n"); | |
1602 fprintf(file, "\t<blist>\n"); | |
1603 | |
1604 for(gnode = gaimbuddylist->root; gnode; gnode = gnode->next) { | |
1605 if(!GAIM_BLIST_NODE_IS_GROUP(gnode)) | |
1606 continue; | |
1607 group = (struct group *)gnode; | |
1608 if(!exp_acct || gaim_group_on_account(group, exp_acct)) { | |
1609 char *group_name = g_markup_escape_text(group->name, -1); | |
1610 fprintf(file, "\t\t<group name=\"%s\">\n", group_name); | |
1611 g_hash_table_foreach(group->settings, blist_print_group_settings, file); | |
1612 for(bnode = gnode->child; bnode; bnode = bnode->next) { | |
5234 | 1613 if(GAIM_BLIST_NODE_IS_BUDDY(bnode)) { |
1614 bud = (struct buddy *)bnode; | |
1615 if(!exp_acct || bud->account == exp_acct) { | |
1616 char *bud_name = g_markup_escape_text(bud->name, -1); | |
1617 char *bud_alias = NULL; | |
1618 char *acct_name = g_markup_escape_text(bud->account->username, -1); | |
1619 if(bud->alias) | |
1620 bud_alias= g_markup_escape_text(bud->alias, -1); | |
1621 fprintf(file, "\t\t\t<person name=\"%s\">\n", | |
1622 bud_alias ? bud_alias : bud_name); | |
1623 fprintf(file, "\t\t\t\t<buddy protocol=\"%d\" " | |
1624 "account=\"%s\">\n", bud->account->protocol, | |
1625 acct_name); | |
1626 fprintf(file, "\t\t\t\t\t<name>%s</name>\n", bud_name); | |
1627 if(bud_alias) { | |
1628 fprintf(file, "\t\t\t\t\t<alias>%s</alias>\n", | |
1629 bud_alias); | |
1630 } | |
1631 g_hash_table_foreach(bud->settings, | |
1632 blist_print_buddy_settings, file); | |
1633 fprintf(file, "\t\t\t\t</buddy>\n"); | |
1634 fprintf(file, "\t\t\t</person>\n"); | |
1635 g_free(bud_name); | |
1636 g_free(bud_alias); | |
1637 g_free(acct_name); | |
5228 | 1638 } |
5234 | 1639 } else if(GAIM_BLIST_NODE_IS_CHAT(bnode)) { |
1640 struct chat *chat = (struct chat *)bnode; | |
1641 if(!exp_acct || chat->account == exp_acct) { | |
1642 char *acct_name = g_markup_escape_text(chat->account->username, -1); | |
1643 fprintf(file, "\t\t\t<chat protocol=\"%d\" account=\"%s\">\n", chat->account->protocol, acct_name); | |
5237 | 1644 if(chat->alias) { |
1645 char *chat_alias = g_markup_escape_text(chat->alias, -1); | |
1646 fprintf(file, "\t\t\t\t<alias>%s</alias>\n", chat_alias); | |
1647 g_free(chat_alias); | |
1648 } | |
5234 | 1649 g_hash_table_foreach(chat->components, |
1650 blist_print_chat_components, file); | |
1651 fprintf(file, "\t\t\t</chat>\n"); | |
5237 | 1652 g_free(acct_name); |
5234 | 1653 } |
5228 | 1654 } |
1655 } | |
1656 fprintf(file, "\t\t</group>\n"); | |
1657 g_free(group_name); | |
1658 } | |
1659 } | |
1660 | |
1661 fprintf(file, "\t</blist>\n"); | |
1662 fprintf(file, "\t<privacy>\n"); | |
1663 | |
1664 for(accounts = gaim_accounts; accounts; accounts = accounts->next) { | |
1665 struct gaim_account *account = accounts->data; | |
1666 char *acct_name = g_markup_escape_text(account->username, -1); | |
1667 if(!exp_acct || account == exp_acct) { | |
1668 fprintf(file, "\t\t<account protocol=\"%d\" name=\"%s\" " | |
1669 "mode=\"%d\">\n", account->protocol, acct_name, account->permdeny); | |
1670 for(buds = account->permit; buds; buds = buds->next) { | |
1671 char *bud_name = g_markup_escape_text(buds->data, -1); | |
1672 fprintf(file, "\t\t\t<permit>%s</permit>\n", bud_name); | |
1673 g_free(bud_name); | |
1674 } | |
1675 for(buds = account->deny; buds; buds = buds->next) { | |
1676 char *bud_name = g_markup_escape_text(buds->data, -1); | |
1677 fprintf(file, "\t\t\t<block>%s</block>\n", bud_name); | |
1678 g_free(bud_name); | |
1679 } | |
1680 fprintf(file, "\t\t</account>\n"); | |
1681 } | |
1682 g_free(acct_name); | |
1683 } | |
1684 | |
1685 fprintf(file, "\t</privacy>\n"); | |
1686 fprintf(file, "</gaim>\n"); | |
1687 } | |
1688 | |
1689 void gaim_blist_save() { | |
1690 FILE *file; | |
1691 char *user_dir = gaim_user_dir(); | |
1692 char *filename; | |
1693 char *filename_real; | |
1694 | |
1695 if(!user_dir) | |
1696 return; | |
1697 if(!blist_safe_to_write) { | |
1698 gaim_debug(GAIM_DEBUG_WARNING, "blist save", | |
1699 "AHH!! Tried to write the blist before we read it!\n"); | |
1700 return; | |
1701 } | |
1702 | |
1703 file = fopen(user_dir, "r"); | |
1704 if(!file) | |
1705 mkdir(user_dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
1706 else | |
1707 fclose(file); | |
1708 | |
1709 filename = g_build_filename(user_dir, "blist.xml.save", NULL); | |
1710 | |
1711 if((file = fopen(filename, "w"))) { | |
1712 gaim_blist_write(file, NULL); | |
1713 fclose(file); | |
1714 chmod(filename, S_IRUSR | S_IWUSR); | |
1715 } else { | |
1716 gaim_debug(GAIM_DEBUG_ERROR, "blist save", "Unable to write %s\n", | |
1717 filename); | |
1718 } | |
1719 | |
1720 filename_real = g_build_filename(user_dir, "blist.xml", NULL); | |
1721 | |
1722 if(rename(filename, filename_real) < 0) | |
1723 gaim_debug(GAIM_DEBUG_ERROR, "blist save", | |
1724 "Error renaming %s to %s\n", filename, filename_real); | |
1725 | |
1726 | |
1727 g_free(filename); | |
1728 g_free(filename_real); | |
1729 } | |
1730 | |
1731 gboolean gaim_privacy_permit_add(struct gaim_account *account, const char *who) { | |
1732 GSList *d = account->permit; | |
1733 char *n = g_strdup(normalize(who)); | |
1734 while(d) { | |
1735 if(!gaim_utf8_strcasecmp(n, normalize(d->data))) | |
1736 break; | |
1737 d = d->next; | |
1738 } | |
1739 g_free(n); | |
1740 if(!d) { | |
1741 account->permit = g_slist_append(account->permit, g_strdup(who)); | |
1742 return TRUE; | |
1743 } | |
1744 | |
1745 return FALSE; | |
1746 } | |
1747 | |
1748 gboolean gaim_privacy_permit_remove(struct gaim_account *account, const char *who) { | |
1749 GSList *d = account->permit; | |
1750 char *n = g_strdup(normalize(who)); | |
1751 while(d) { | |
1752 if(!gaim_utf8_strcasecmp(n, normalize(d->data))) | |
1753 break; | |
1754 d = d->next; | |
1755 } | |
1756 g_free(n); | |
1757 if(d) { | |
1758 account->permit = g_slist_remove(account->permit, d->data); | |
1759 g_free(d->data); | |
1760 return TRUE; | |
1761 } | |
1762 return FALSE; | |
1763 } | |
1764 | |
1765 gboolean gaim_privacy_deny_add(struct gaim_account *account, const char *who) { | |
1766 GSList *d = account->deny; | |
1767 char *n = g_strdup(normalize(who)); | |
1768 while(d) { | |
1769 if(!gaim_utf8_strcasecmp(n, normalize(d->data))) | |
1770 break; | |
1771 d = d->next; | |
1772 } | |
1773 g_free(n); | |
1774 if(!d) { | |
1775 account->deny = g_slist_append(account->deny, g_strdup(who)); | |
1776 return TRUE; | |
1777 } | |
1778 | |
1779 return FALSE; | |
1780 } | |
1781 | |
1782 gboolean gaim_privacy_deny_remove(struct gaim_account *account, const char *who) { | |
1783 GSList *d = account->deny; | |
1784 char *n = g_strdup(normalize(who)); | |
1785 while(d) { | |
1786 if(!gaim_utf8_strcasecmp(n, normalize(d->data))) | |
1787 break; | |
1788 d = d->next; | |
1789 } | |
1790 g_free(n); | |
1791 if(d) { | |
1792 account->deny = g_slist_remove(account->deny, d->data); | |
1793 g_free(d->data); | |
1794 return TRUE; | |
1795 } | |
1796 return FALSE; | |
1797 } | |
1798 | |
1799 void gaim_group_set_setting(struct group *g, const char *key, | |
1800 const char *value) { | |
1801 if(!g) | |
1802 return; | |
1803 g_hash_table_replace(g->settings, g_strdup(key), g_strdup(value)); | |
1804 } | |
1805 | |
1806 char *gaim_group_get_setting(struct group *g, const char *key) { | |
1807 if(!g) | |
1808 return NULL; | |
1809 return g_strdup(g_hash_table_lookup(g->settings, key)); | |
1810 } | |
1811 | |
1812 void gaim_buddy_set_setting(struct buddy *b, const char *key, | |
1813 const char *value) { | |
1814 if(!b) | |
1815 return; | |
1816 g_hash_table_replace(b->settings, g_strdup(key), g_strdup(value)); | |
1817 } | |
1818 | |
1819 char *gaim_buddy_get_setting(struct buddy *b, const char *key) { | |
1820 if(!b) | |
1821 return NULL; | |
1822 return g_strdup(g_hash_table_lookup(b->settings, key)); | |
1823 } | |
1824 | |
1825 void gaim_set_blist_ui_ops(struct gaim_blist_ui_ops *ops) | |
1826 { | |
1827 blist_ui_ops = ops; | |
1828 } | |
1829 | |
1830 struct gaim_blist_ui_ops * | |
1831 gaim_get_blist_ui_ops(void) | |
1832 { | |
1833 return blist_ui_ops; | |
1834 } | |
1835 | |
1836 int gaim_blist_get_group_size(struct group *group, gboolean offline) { | |
1837 if(!group) | |
1838 return 0; | |
1839 | |
5277 | 1840 return offline ? group->totalsize : group->currentsize; |
5228 | 1841 } |
1842 | |
1843 int gaim_blist_get_group_online_count(struct group *group) { | |
1844 if(!group) | |
1845 return 0; | |
1846 | |
5277 | 1847 return group->online; |
5228 | 1848 } |
1849 | |
1850 |