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