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