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