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