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