Mercurial > pidgin
annotate src/protocols/novell/nmcontact.c @ 8770:7df86465ec82
[gaim-migrate @ 9531]
using a pointer before checking it isn't exactly a good thing. nosnilmot
caught this.
committer: Tailor Script <tailor@pidgin.im>
author | Luke Schierer <lschiere@pidgin.im> |
---|---|
date | Fri, 23 Apr 2004 15:02:39 +0000 |
parents | 0bde91a2ea4d |
children | 6663ad2386d9 |
rev | line source |
---|---|
8675 | 1 /* |
2 * nmcontact.c | |
3 * | |
4 * Copyright © 2004 Unpublished Work of Novell, Inc. All Rights Reserved. | |
5 * | |
6 * THIS WORK IS AN UNPUBLISHED WORK OF NOVELL, INC. NO PART OF THIS WORK MAY BE | |
7 * USED, PRACTICED, PERFORMED, COPIED, DISTRIBUTED, REVISED, MODIFIED, | |
8 * TRANSLATED, ABRIDGED, CONDENSED, EXPANDED, COLLECTED, COMPILED, LINKED, | |
9 * RECAST, TRANSFORMED OR ADAPTED WITHOUT THE PRIOR WRITTEN CONSENT OF NOVELL, | |
10 * INC. ANY USE OR EXPLOITATION OF THIS WORK WITHOUT AUTHORIZATION COULD SUBJECT | |
11 * THE PERPETRATOR TO CRIMINAL AND CIVIL LIABILITY. | |
8684
046dd8ef2920
[gaim-migrate @ 9437]
Christian Hammond <chipx86@chipx86.com>
parents:
8675
diff
changeset
|
12 * |
8675 | 13 * AS BETWEEN [GAIM] AND NOVELL, NOVELL GRANTS [GAIM] THE RIGHT TO REPUBLISH |
14 * THIS WORK UNDER THE GPL (GNU GENERAL PUBLIC LICENSE) WITH ALL RIGHTS AND | |
15 * LICENSES THEREUNDER. IF YOU HAVE RECEIVED THIS WORK DIRECTLY OR INDIRECTLY | |
16 * FROM [GAIM] AS PART OF SUCH A REPUBLICATION, YOU HAVE ALL RIGHTS AND LICENSES | |
17 * GRANTED BY [GAIM] UNDER THE GPL. IN CONNECTION WITH SUCH A REPUBLICATION, IF | |
18 * ANYTHING IN THIS NOTICE CONFLICTS WITH THE TERMS OF THE GPL, SUCH TERMS | |
19 * PREVAIL. | |
20 * | |
21 */ | |
22 | |
23 #include <glib.h> | |
24 #include <string.h> | |
25 #include "nmcontact.h" | |
26 #include "nmfield.h" | |
27 #include "nmuser.h" | |
28 | |
29 struct _NMContact | |
30 { | |
31 int id; | |
32 int parent_id; | |
33 int seq; | |
34 char *dn; | |
35 char *display_name; | |
36 NMUserRecord *user_record; | |
37 gpointer data; | |
38 int ref_count; | |
39 }; | |
40 | |
41 struct _NMFolder | |
42 { | |
43 int id; | |
44 int seq; | |
45 char *name; | |
46 GSList *folders; | |
47 GSList *contacts; | |
48 int ref_count; | |
49 }; | |
50 | |
51 static int count = 0; | |
52 | |
53 static void _release_folder_contacts(NMFolder * folder); | |
54 static void _release_folder_folders(NMFolder * folder); | |
55 static void _add_contacts(NMUser * user, NMFolder * folder, NMField * fields); | |
56 static void _add_folders(NMFolder * root, NMField * fields); | |
57 | |
58 /********************************************************************* | |
8684
046dd8ef2920
[gaim-migrate @ 9437]
Christian Hammond <chipx86@chipx86.com>
parents:
8675
diff
changeset
|
59 * Contact API |
8675 | 60 *********************************************************************/ |
61 | |
62 NMContact * | |
63 nm_create_contact() | |
64 { | |
65 NMContact *contact = g_new0(NMContact, 1); | |
66 | |
67 contact->ref_count = 1; | |
68 | |
69 gaim_debug(GAIM_DEBUG_INFO, "novell", "Creating contact, total=%d\n", | |
70 count++); | |
71 | |
72 return contact; | |
73 } | |
74 | |
75 /* | |
8684
046dd8ef2920
[gaim-migrate @ 9437]
Christian Hammond <chipx86@chipx86.com>
parents:
8675
diff
changeset
|
76 * This creates a contact for the contact list. The |
046dd8ef2920
[gaim-migrate @ 9437]
Christian Hammond <chipx86@chipx86.com>
parents:
8675
diff
changeset
|
77 * field array that is passed in should be a |
8675 | 78 * NM_A_FA_CONTACT array. |
79 * | |
80 */ | |
81 NMContact * | |
82 nm_create_contact_from_fields(NMField * fields) | |
83 { | |
84 NMContact *contact; | |
85 NMField *field; | |
86 | |
87 if ( fields == NULL || fields->tag == NULL || fields->value == 0 || | |
88 strcmp(fields->tag, NM_A_FA_CONTACT) ) | |
89 { | |
90 return NULL; | |
91 } | |
92 | |
93 contact = nm_create_contact(); | |
94 | |
95 if ((field = nm_locate_field(NM_A_SZ_OBJECT_ID, (NMField *) fields->value))) { | |
96 | |
97 if (field->value) | |
98 contact->id = atoi((char *) field->value); | |
99 | |
100 } | |
101 | |
102 if ((field = nm_locate_field(NM_A_SZ_PARENT_ID, (NMField *) fields->value))) { | |
103 | |
104 if (field->value) | |
105 contact->parent_id = atoi((char *) field->value); | |
106 | |
107 } | |
108 | |
109 if ((field = | |
110 nm_locate_field(NM_A_SZ_SEQUENCE_NUMBER, (NMField *) fields->value))) { | |
8684
046dd8ef2920
[gaim-migrate @ 9437]
Christian Hammond <chipx86@chipx86.com>
parents:
8675
diff
changeset
|
111 |
8675 | 112 if (field->value) |
113 contact->seq = atoi((char *) field->value); | |
114 | |
115 } | |
116 | |
117 if ((field = | |
118 nm_locate_field(NM_A_SZ_DISPLAY_NAME, (NMField *) fields->value))) { | |
8684
046dd8ef2920
[gaim-migrate @ 9437]
Christian Hammond <chipx86@chipx86.com>
parents:
8675
diff
changeset
|
119 |
8675 | 120 if (field->value) |
121 contact->display_name = g_strdup((char *) field->value); | |
122 | |
123 } | |
124 | |
125 if ((field = nm_locate_field(NM_A_SZ_DN, (NMField *) fields->value))) { | |
126 | |
127 if (field->value) | |
128 contact->dn = g_strdup((char *) field->value); | |
129 | |
130 } | |
131 | |
132 return contact; | |
133 } | |
134 | |
135 void | |
136 nm_contact_update_list_properties(NMContact * contact, NMField * fields) | |
137 { | |
138 NMField *field; | |
139 | |
140 if (contact == NULL || fields == NULL || fields->value == 0) | |
141 return; | |
142 | |
143 if ((field = nm_locate_field(NM_A_SZ_OBJECT_ID, (NMField *) fields->value))) { | |
8684
046dd8ef2920
[gaim-migrate @ 9437]
Christian Hammond <chipx86@chipx86.com>
parents:
8675
diff
changeset
|
144 |
8675 | 145 if (field->value) |
146 contact->id = atoi((char *)field->value); | |
147 | |
148 } | |
149 | |
150 if ((field = nm_locate_field(NM_A_SZ_PARENT_ID, (NMField *) fields->value))) { | |
8684
046dd8ef2920
[gaim-migrate @ 9437]
Christian Hammond <chipx86@chipx86.com>
parents:
8675
diff
changeset
|
151 |
8675 | 152 if (field->value) |
153 contact->parent_id = atoi((char *) field->value); | |
154 | |
155 } | |
156 | |
157 if ((field = | |
158 nm_locate_field(NM_A_SZ_SEQUENCE_NUMBER, (NMField *) fields->value))) { | |
159 | |
160 if (field->value) | |
161 contact->seq = atoi((char *) field->value); | |
162 | |
163 } | |
164 | |
165 if ((field = | |
166 nm_locate_field(NM_A_SZ_DISPLAY_NAME, (NMField *) fields->value))) { | |
8684
046dd8ef2920
[gaim-migrate @ 9437]
Christian Hammond <chipx86@chipx86.com>
parents:
8675
diff
changeset
|
167 |
8675 | 168 if (field->value) { |
169 if (contact->display_name) | |
170 g_free(contact->display_name); | |
171 | |
172 contact->display_name = g_strdup((char *) field->value); | |
173 } | |
174 | |
175 } | |
176 | |
177 if ((field = nm_locate_field(NM_A_SZ_DN, (NMField *) fields->value))) { | |
178 | |
179 if (field->value) { | |
180 if (contact->dn) | |
181 g_free(contact->dn); | |
8684
046dd8ef2920
[gaim-migrate @ 9437]
Christian Hammond <chipx86@chipx86.com>
parents:
8675
diff
changeset
|
182 |
8675 | 183 contact->dn = g_strdup((char *) field->value); |
184 } | |
185 | |
186 } | |
187 } | |
188 | |
189 NMField * | |
190 nm_contact_to_fields(NMContact * contact) | |
191 { | |
192 NMField *fields = NULL; | |
193 | |
194 if (contact == NULL) | |
195 return NULL; | |
196 | |
197 fields = nm_add_field(fields, NM_A_SZ_OBJECT_ID, 0, NMFIELD_METHOD_VALID, 0, | |
198 (guint32) g_strdup_printf("%d", contact->id), | |
199 NMFIELD_TYPE_UTF8); | |
200 | |
201 fields = nm_add_field(fields, NM_A_SZ_PARENT_ID, 0, NMFIELD_METHOD_VALID, 0, | |
202 (guint32) g_strdup_printf("%d", contact->parent_id), | |
203 NMFIELD_TYPE_UTF8); | |
204 | |
205 fields = | |
206 nm_add_field(fields, NM_A_SZ_SEQUENCE_NUMBER, 0, NMFIELD_METHOD_VALID, 0, | |
207 (guint32) g_strdup_printf("%d", contact->seq), | |
208 NMFIELD_TYPE_UTF8); | |
209 | |
210 if (contact->display_name != NULL) { | |
211 fields = | |
212 nm_add_field(fields, NM_A_SZ_DISPLAY_NAME, 0, NMFIELD_METHOD_VALID, 0, | |
213 (guint32) g_strdup(contact->display_name), | |
214 NMFIELD_TYPE_UTF8); | |
215 } | |
216 | |
217 if (contact->dn != NULL) { | |
218 fields = nm_add_field(fields, NM_A_SZ_DN, 0, NMFIELD_METHOD_VALID, 0, | |
219 (guint32) g_strdup(contact->dn), NMFIELD_TYPE_UTF8); | |
220 } | |
221 | |
222 return fields; | |
223 } | |
224 | |
225 void | |
226 nm_contact_add_ref(NMContact * contact) | |
227 { | |
228 if (contact) | |
229 contact->ref_count++; | |
230 } | |
231 | |
232 void | |
233 nm_release_contact(NMContact * contact) | |
234 { | |
235 if (contact == NULL) | |
236 return; | |
237 | |
238 if (--(contact->ref_count) == 0) { | |
239 | |
240 gaim_debug(GAIM_DEBUG_INFO, "novell", | |
241 "Releasing contact, total=%d\n", --count); | |
242 | |
243 if (contact->display_name) { | |
244 g_free(contact->display_name); | |
245 } | |
246 | |
247 if (contact->dn) { | |
248 g_free(contact->dn); | |
249 } | |
250 | |
251 if (contact->user_record) { | |
252 nm_release_user_record(contact->user_record); | |
253 } | |
254 | |
255 g_free(contact); | |
256 } | |
257 | |
258 } | |
259 | |
260 const char * | |
261 nm_contact_get_display_name(NMContact * contact) | |
262 { | |
263 if (contact == NULL) | |
264 return NULL; | |
265 | |
266 if (contact->user_record != NULL && contact->display_name == NULL) { | |
267 const char *full_name, *lname, *fname, *cn, *display_id; | |
268 | |
269 full_name = nm_user_record_get_full_name(contact->user_record); | |
270 fname = nm_user_record_get_first_name(contact->user_record); | |
271 lname = nm_user_record_get_last_name(contact->user_record); | |
272 cn = nm_user_record_get_userid(contact->user_record); | |
273 display_id = nm_user_record_get_display_id(contact->user_record); | |
274 | |
275 /* Try to build a display name. */ | |
276 if (full_name) { | |
277 | |
278 contact->display_name = g_strdup(full_name); | |
279 | |
280 } else if (fname && lname) { | |
281 | |
282 contact->display_name = g_strdup_printf("%s %s", fname, lname); | |
283 | |
284 } else { | |
285 | |
286 /* If auth attribute is set use it */ | |
8684
046dd8ef2920
[gaim-migrate @ 9437]
Christian Hammond <chipx86@chipx86.com>
parents:
8675
diff
changeset
|
287 if (nm_user_record_get_auth_attr(contact->user_record) && |
8675 | 288 display_id != NULL) { |
289 | |
290 contact->display_name = g_strdup(display_id); | |
291 | |
292 } else { | |
293 | |
294 /* Use CN or display id */ | |
295 if (cn) { | |
296 | |
297 contact->display_name = g_strdup(cn); | |
298 | |
299 } else if (display_id) { | |
300 | |
301 contact->display_name = g_strdup(display_id); | |
302 | |
303 } | |
304 | |
305 } | |
306 | |
307 } | |
308 } | |
309 | |
310 return contact->display_name; | |
311 } | |
312 | |
313 void | |
314 nm_contact_set_display_name(NMContact * contact, const char *display_name) | |
315 { | |
316 if (contact == NULL) | |
317 return; | |
318 | |
319 if (contact->display_name) { | |
320 g_free(contact->display_name); | |
321 contact->display_name = NULL; | |
322 } | |
8684
046dd8ef2920
[gaim-migrate @ 9437]
Christian Hammond <chipx86@chipx86.com>
parents:
8675
diff
changeset
|
323 |
8675 | 324 if (display_name) |
325 contact->display_name = g_strdup(display_name); | |
326 } | |
327 | |
328 void | |
329 nm_contact_set_dn(NMContact * contact, const char *dn) | |
330 { | |
331 if (contact == NULL) | |
332 return; | |
333 | |
334 if (contact->dn) { | |
335 g_free(contact->dn); | |
336 contact->dn = NULL; | |
337 } | |
338 | |
339 if (dn) | |
340 contact->dn = g_strdup(dn); | |
341 } | |
342 | |
343 const char * | |
344 nm_contact_get_dn(NMContact * contact) | |
345 { | |
346 if (contact == NULL) | |
347 return NULL; | |
348 | |
349 return contact->dn; | |
350 } | |
351 | |
352 gpointer | |
353 nm_contact_get_data(NMContact * contact) | |
354 { | |
355 if (contact == NULL) | |
356 return NULL; | |
357 | |
358 return contact->data; | |
359 } | |
360 | |
361 int | |
362 nm_contact_get_id(NMContact * contact) | |
363 { | |
364 if (contact == NULL) | |
365 return -1; | |
366 | |
367 return contact->id; | |
368 } | |
369 | |
370 int | |
371 nm_contact_get_parent_id(NMContact * contact) | |
372 { | |
373 if (contact == NULL) | |
374 return -1; | |
375 | |
376 return contact->parent_id; | |
377 } | |
378 | |
379 void | |
380 nm_contact_set_data(NMContact * contact, gpointer data) | |
381 { | |
382 if (contact == NULL) | |
383 return; | |
384 | |
385 contact->data = data; | |
386 } | |
387 | |
388 void | |
389 nm_contact_set_user_record(NMContact * contact, NMUserRecord * user_record) | |
390 { | |
391 if (contact == NULL) | |
392 return; | |
393 | |
394 if (contact->user_record) { | |
395 nm_release_user_record(contact->user_record); | |
396 } | |
397 | |
398 nm_user_record_add_ref(user_record); | |
399 contact->user_record = user_record; | |
400 } | |
401 | |
402 NMUserRecord * | |
403 nm_contact_get_user_record(NMContact * contact) | |
404 { | |
405 if (contact == NULL) | |
406 return NULL; | |
407 | |
408 return contact->user_record; | |
409 } | |
410 | |
411 const char * | |
412 nm_contact_get_userid(NMContact * contact) | |
413 { | |
414 NMUserRecord *user_record; | |
415 const char *userid = NULL; | |
416 | |
417 if (contact == NULL) | |
418 return NULL; | |
419 | |
420 user_record = nm_contact_get_user_record(contact); | |
421 if (user_record) { | |
422 userid = nm_user_record_get_userid(user_record); | |
423 } | |
424 | |
425 return userid; | |
426 } | |
427 | |
428 const char * | |
429 nm_contact_get_display_id(NMContact * contact) | |
430 { | |
431 NMUserRecord *user_record; | |
432 const char *id = NULL; | |
433 | |
434 if (contact == NULL) | |
435 return NULL; | |
436 | |
437 user_record = nm_contact_get_user_record(contact); | |
438 if (user_record) { | |
439 id = nm_user_record_get_display_id(user_record); | |
440 } | |
441 | |
442 return id; | |
443 } | |
444 | |
445 | |
446 /********************************************************************* | |
8684
046dd8ef2920
[gaim-migrate @ 9437]
Christian Hammond <chipx86@chipx86.com>
parents:
8675
diff
changeset
|
447 * Folder API |
8675 | 448 *********************************************************************/ |
449 | |
450 NMFolder * | |
451 nm_create_folder(const char *name) | |
452 { | |
453 NMFolder *folder = g_new0(NMFolder, 1); | |
454 | |
455 if (name) | |
456 folder->name = g_strdup(name); | |
457 | |
458 folder->ref_count = 1; | |
459 | |
460 return folder; | |
461 } | |
462 | |
463 NMFolder * | |
464 nm_create_folder_from_fields(NMField * fields) | |
465 { | |
466 NMField *field; | |
467 NMFolder *folder; | |
468 | |
469 if (fields == NULL || fields->value == 0) | |
470 return NULL; | |
471 | |
472 folder = g_new0(NMFolder, 1); | |
473 | |
474 if ((field = nm_locate_field(NM_A_SZ_OBJECT_ID, (NMField *) fields->value))) { | |
8684
046dd8ef2920
[gaim-migrate @ 9437]
Christian Hammond <chipx86@chipx86.com>
parents:
8675
diff
changeset
|
475 |
8675 | 476 if (field->value) |
477 folder->id = atoi((char *) field->value); | |
478 } | |
479 | |
480 if ((field = | |
481 nm_locate_field(NM_A_SZ_SEQUENCE_NUMBER, (NMField *) fields->value))) { | |
8684
046dd8ef2920
[gaim-migrate @ 9437]
Christian Hammond <chipx86@chipx86.com>
parents:
8675
diff
changeset
|
482 |
8675 | 483 if (field->value) |
484 folder->seq = atoi((char *) field->value); | |
485 } | |
486 | |
487 if ((field = | |
488 nm_locate_field(NM_A_SZ_DISPLAY_NAME, (NMField *) fields->value))) { | |
489 | |
490 if (field->value) | |
491 folder->name = g_strdup((char *) field->value); | |
492 } | |
493 | |
494 folder->ref_count = 1; | |
495 return folder; | |
496 } | |
497 | |
498 NMField * | |
499 nm_folder_to_fields(NMFolder * folder) | |
500 { | |
501 NMField *fields = NULL; | |
502 | |
503 if (folder == NULL) | |
504 return NULL; | |
505 | |
506 fields = nm_add_field(fields, NM_A_SZ_OBJECT_ID, 0, NMFIELD_METHOD_VALID, 0, | |
507 (guint32) g_strdup_printf("%d", folder->id), | |
508 NMFIELD_TYPE_UTF8); | |
509 | |
510 fields = nm_add_field(fields, NM_A_SZ_PARENT_ID, 0, NMFIELD_METHOD_VALID, 0, | |
511 (guint32) g_strdup("0"), NMFIELD_TYPE_UTF8); | |
512 | |
513 fields = nm_add_field(fields, NM_A_SZ_TYPE, 0, NMFIELD_METHOD_VALID, 0, | |
514 (guint32) g_strdup("1"), NMFIELD_TYPE_UTF8); | |
515 | |
516 fields = | |
517 nm_add_field(fields, NM_A_SZ_SEQUENCE_NUMBER, 0, NMFIELD_METHOD_VALID, 0, | |
518 (guint32) g_strdup_printf("%d", folder->seq), | |
519 NMFIELD_TYPE_UTF8); | |
520 | |
521 if (folder->name != NULL) { | |
522 fields = | |
523 nm_add_field(fields, NM_A_SZ_DISPLAY_NAME, 0, NMFIELD_METHOD_VALID, 0, | |
524 (guint32) g_strdup(folder->name), NMFIELD_TYPE_UTF8); | |
525 } | |
526 | |
527 | |
528 return fields; | |
529 } | |
530 | |
531 void | |
532 nm_folder_update_list_properties(NMFolder * folder, NMField * fields) | |
533 { | |
534 NMField *field; | |
535 | |
536 if (folder == NULL || fields == NULL || fields->value == 0) | |
537 return; | |
538 | |
539 if ((field = nm_locate_field(NM_A_SZ_OBJECT_ID, (NMField *) fields->value))) { | |
8684
046dd8ef2920
[gaim-migrate @ 9437]
Christian Hammond <chipx86@chipx86.com>
parents:
8675
diff
changeset
|
540 |
8675 | 541 if (field->value) |
542 folder->id = atoi((char *) field->value); | |
543 | |
544 } | |
545 | |
546 if ((field = | |
547 nm_locate_field(NM_A_SZ_SEQUENCE_NUMBER, (NMField *) fields->value))) { | |
8684
046dd8ef2920
[gaim-migrate @ 9437]
Christian Hammond <chipx86@chipx86.com>
parents:
8675
diff
changeset
|
548 |
8675 | 549 if (field->value) |
550 folder->seq = atoi((char *) field->value); | |
551 | |
552 } | |
553 | |
554 if ((field = | |
555 nm_locate_field(NM_A_SZ_DISPLAY_NAME, (NMField *) fields->value))) { | |
556 | |
557 if (field->value) { | |
558 if (folder->name) | |
559 g_free(folder->name); | |
8684
046dd8ef2920
[gaim-migrate @ 9437]
Christian Hammond <chipx86@chipx86.com>
parents:
8675
diff
changeset
|
560 |
8675 | 561 folder->name = g_strdup((char *) field->value); |
562 } | |
563 | |
564 } | |
565 | |
566 } | |
567 | |
568 void | |
569 nm_release_folder(NMFolder * folder) | |
570 { | |
571 if (folder == NULL) | |
572 return; | |
573 | |
574 if (--(folder->ref_count) == 0) { | |
575 if (folder->name) { | |
576 g_free(folder->name); | |
577 } | |
578 | |
579 if (folder->folders) { | |
580 _release_folder_folders(folder); | |
581 } | |
582 | |
583 if (folder->contacts) { | |
584 _release_folder_contacts(folder); | |
585 } | |
586 | |
587 g_free(folder); | |
588 } | |
589 } | |
590 | |
591 | |
592 void | |
593 nm_folder_add_ref(NMFolder * folder) | |
594 { | |
595 if (folder) | |
596 folder->ref_count++; | |
597 } | |
598 | |
599 int | |
600 nm_folder_get_subfolder_count(NMFolder * folder) | |
601 { | |
602 if (folder == NULL) | |
603 return 0; | |
604 | |
605 if (folder->folders) | |
606 return g_slist_length(folder->folders); | |
607 else | |
608 return 0; | |
609 } | |
610 | |
611 NMFolder * | |
612 nm_folder_get_subfolder(NMFolder * folder, int index) | |
613 { | |
614 if (folder == NULL) | |
615 return NULL; | |
616 | |
617 if (folder->folders) | |
618 return (NMFolder *) g_slist_nth_data(folder->folders, index); | |
619 else | |
620 return NULL; | |
621 } | |
622 | |
623 int | |
624 nm_folder_get_contact_count(NMFolder * folder) | |
625 { | |
626 if (folder == NULL) | |
627 return 0; | |
628 | |
629 if (folder->contacts != NULL) | |
630 return g_slist_length(folder->contacts); | |
631 else | |
632 return 0; | |
633 } | |
634 | |
635 NMContact * | |
636 nm_folder_get_contact(NMFolder * folder, int index) | |
637 { | |
638 if (folder == NULL) | |
639 return NULL; | |
640 | |
641 if (folder->contacts) | |
642 return (NMContact *) g_slist_nth_data(folder->contacts, index); | |
643 else | |
644 return NULL; | |
645 } | |
646 | |
647 const char * | |
648 nm_folder_get_name(NMFolder * folder) | |
649 { | |
650 if (folder == NULL) | |
651 return NULL; | |
652 | |
653 return folder->name; | |
654 } | |
655 | |
656 void | |
657 nm_folder_set_name(NMFolder * folder, const char *name) | |
658 { | |
659 if (folder == NULL || name == NULL) | |
660 return; | |
661 | |
662 if (folder->name) | |
663 g_free(folder->name); | |
664 | |
665 folder->name = g_strdup(name); | |
666 } | |
667 | |
668 int | |
669 nm_folder_get_id(NMFolder * folder) | |
670 { | |
671 if (folder == NULL) { | |
672 return -1; | |
673 } | |
674 | |
675 return folder->id; | |
676 } | |
677 | |
678 void | |
679 nm_folder_add_folder_to_list(NMFolder * root, NMFolder * folder) | |
680 { | |
681 GSList *node; | |
682 | |
683 if (root == NULL || folder == NULL) | |
684 return; | |
685 | |
686 node = root->folders; | |
687 while (node) { | |
688 if (folder->seq <= ((NMFolder *) node->data)->seq) { | |
689 nm_folder_add_ref(folder); | |
690 root->folders = g_slist_insert_before(root->folders, node, folder); | |
691 break; | |
692 } | |
693 node = g_slist_next(node); | |
694 } | |
695 if (node == NULL) { | |
696 nm_folder_add_ref(folder); | |
697 root->folders = g_slist_append(root->folders, folder); | |
698 } | |
699 } | |
700 | |
701 void | |
702 nm_folder_remove_contact(NMFolder * folder, NMContact * contact) | |
703 { | |
8770 | 704 GSList *node; |
8769
0bde91a2ea4d
[gaim-migrate @ 9530]
Christian Hammond <chipx86@chipx86.com>
parents:
8684
diff
changeset
|
705 |
8675 | 706 if (folder == NULL || contact == NULL) |
707 return; | |
708 | |
8770 | 709 node = folder->contacts; |
710 | |
8675 | 711 while (node) { |
712 if (contact->id == ((NMContact *) (node->data))->id) { | |
713 folder->contacts = g_slist_remove(folder->contacts, node->data); | |
714 nm_release_contact(contact); | |
715 break; | |
716 } | |
717 node = g_slist_next(node); | |
718 } | |
719 } | |
720 | |
721 void | |
722 nm_folder_add_contact_to_list(NMFolder * root_folder, NMContact * contact) | |
723 { | |
724 GSList *node = NULL; | |
725 NMFolder *folder = root_folder; | |
8684
046dd8ef2920
[gaim-migrate @ 9437]
Christian Hammond <chipx86@chipx86.com>
parents:
8675
diff
changeset
|
726 |
8675 | 727 if (folder == NULL || contact == NULL) |
728 return; | |
729 | |
730 /* Find folder to add contact to */ | |
731 if (contact->parent_id != 0) { | |
732 node = folder->folders; | |
733 while (node) { | |
734 folder = (NMFolder *) node->data; | |
735 if (contact->parent_id == folder->id) { | |
736 break; | |
737 } | |
738 folder = NULL; | |
739 node = g_slist_next(node); | |
740 } | |
741 } | |
742 | |
743 /* Add contact to list */ | |
744 if (folder) { | |
745 node = folder->contacts; | |
746 while (node) { | |
747 if (contact->seq <= ((NMContact *) (node->data))->seq) { | |
748 nm_contact_add_ref(contact); | |
749 folder->contacts = | |
750 g_slist_insert_before(folder->contacts, node, contact); | |
751 break; | |
752 } | |
753 node = g_slist_next(node); | |
754 } | |
755 | |
756 if (node == NULL) { | |
757 nm_contact_add_ref(contact); | |
758 folder->contacts = g_slist_append(folder->contacts, contact); | |
759 } | |
760 } | |
761 } | |
762 | |
763 void | |
764 nm_folder_add_contacts_and_folders(NMUser * user, NMFolder * root, | |
765 NMField * fields) | |
766 { | |
767 /* Add the contacts and folders from the field array */ | |
768 if (user && root && fields) { | |
769 _add_folders(root, fields); | |
770 _add_contacts(user, root, fields); | |
771 } | |
772 } | |
773 | |
774 gpointer | |
775 nm_folder_find_item_by_object_id(NMFolder * root_folder, int object_id) | |
776 { | |
777 int cnt, cnt2, i, j; | |
778 gpointer item = NULL; | |
779 NMFolder *folder; | |
780 NMContact *contact; | |
781 | |
782 if (root_folder == NULL) | |
783 return NULL; | |
784 | |
785 /* Check all contacts for the top level folder */ | |
786 cnt = nm_folder_get_contact_count(root_folder); | |
787 for (i = 0; i < cnt; i++) { | |
788 contact = nm_folder_get_contact(root_folder, i); | |
789 if (contact && (contact->id == object_id)) { | |
790 item = contact; | |
791 break; | |
792 } | |
793 } | |
794 | |
795 /* If we haven't found the item yet, check the subfolders */ | |
796 if (item == NULL) { | |
797 cnt = nm_folder_get_subfolder_count(root_folder); | |
798 for (i = 0; (i < cnt) && (item == NULL); i++) { | |
799 folder = nm_folder_get_subfolder(root_folder, i); | |
800 | |
801 /* Check the id of this folder */ | |
802 if (folder && (folder->id == object_id)) { | |
803 item = folder; | |
804 break; | |
805 } | |
806 | |
807 /* Check all contacts for this folder */ | |
808 cnt2 = nm_folder_get_contact_count(folder); | |
809 for (j = 0; j < cnt2; j++) { | |
810 contact = nm_folder_get_contact(folder, j); | |
811 if (contact && (contact->id == object_id)) { | |
812 item = contact; | |
813 break; | |
814 } | |
815 } | |
816 } | |
817 } | |
818 | |
819 return item; | |
820 } | |
821 | |
822 NMContact * | |
823 nm_folder_find_contact_by_userid(NMFolder * folder, const char *userid) | |
824 { | |
825 int cnt, i; | |
826 NMContact *tmp, *contact = NULL; | |
827 | |
828 if (folder == NULL || userid == NULL) | |
829 return NULL; | |
830 | |
831 cnt = nm_folder_get_contact_count(folder); | |
832 for (i = 0; i < cnt; i++) { | |
833 tmp = nm_folder_get_contact(folder, i); | |
834 if (tmp && nm_utf8_str_equal(nm_contact_get_userid(tmp), userid)) { | |
835 contact = tmp; | |
836 break; | |
837 } | |
838 } | |
839 | |
840 return contact; | |
841 } | |
842 | |
843 NMContact * | |
844 nm_folder_find_contact(NMFolder * folder, const char *dn) | |
845 { | |
846 int cnt, i; | |
847 NMContact *tmp, *contact = NULL; | |
8684
046dd8ef2920
[gaim-migrate @ 9437]
Christian Hammond <chipx86@chipx86.com>
parents:
8675
diff
changeset
|
848 |
8675 | 849 if (folder == NULL || dn == NULL) |
850 return NULL; | |
851 | |
852 cnt = nm_folder_get_contact_count(folder); | |
853 for (i = 0; i < cnt; i++) { | |
854 tmp = nm_folder_get_contact(folder, i); | |
855 if (tmp && nm_utf8_str_equal(nm_contact_get_dn(tmp), dn)) { | |
856 contact = tmp; | |
857 break; | |
858 } | |
859 } | |
860 | |
861 return contact; | |
862 } | |
863 | |
864 | |
865 /********************************************************************* | |
866 * Utility functions | |
867 *********************************************************************/ | |
868 | |
869 static void | |
870 _release_folder_contacts(NMFolder * folder) | |
871 { | |
872 GSList *cnode; | |
873 NMContact *contact; | |
874 | |
875 for (cnode = folder->contacts; cnode; cnode = cnode->next) { | |
876 contact = cnode->data; | |
877 cnode->data = NULL; | |
878 nm_release_contact(contact); | |
879 } | |
880 | |
881 g_slist_free(folder->contacts); | |
882 folder->contacts = NULL; | |
883 } | |
884 | |
885 static void | |
886 _release_folder_folders(NMFolder * folder) | |
887 { | |
888 GSList *fnode; | |
889 NMFolder *subfolder; | |
8684
046dd8ef2920
[gaim-migrate @ 9437]
Christian Hammond <chipx86@chipx86.com>
parents:
8675
diff
changeset
|
890 |
8675 | 891 if (folder == NULL) |
892 return; | |
893 | |
894 for (fnode = folder->folders; fnode; fnode = fnode->next) { | |
895 subfolder = fnode->data; | |
896 fnode->data = NULL; | |
897 nm_release_folder(subfolder); | |
898 } | |
899 | |
900 g_slist_free(folder->folders); | |
901 folder->folders = NULL; | |
902 } | |
903 | |
904 static void | |
905 _add_folders(NMFolder * root, NMField * fields) | |
906 { | |
907 NMFolder *folder = NULL; | |
908 NMField *locate = NULL; | |
909 | |
910 locate = nm_locate_field(NM_A_FA_FOLDER, fields); | |
911 while (locate != NULL) { | |
912 | |
913 /* Create a new folder */ | |
914 folder = nm_create_folder_from_fields(locate); | |
915 | |
916 /* Add subfolder to roots folder list */ | |
917 nm_folder_add_folder_to_list(root, folder); | |
918 | |
919 /* Decrement the ref count */ | |
920 nm_release_folder(folder); | |
921 | |
922 /* Find the next folder */ | |
923 locate = nm_locate_field(NM_A_FA_FOLDER, locate+1); | |
924 | |
925 } | |
926 } | |
927 | |
928 static void | |
929 _add_contacts(NMUser * user, NMFolder * folder, NMField * fields) | |
930 { | |
931 NMContact *contact = NULL; | |
932 NMField *locate = NULL, *details; | |
933 NMUserRecord *user_record = NULL; | |
934 | |
935 locate = nm_locate_field(NM_A_FA_CONTACT, fields); | |
936 while (locate != NULL) { | |
937 | |
938 /* Create a new contact from the fields */ | |
939 contact = nm_create_contact_from_fields(locate); | |
940 | |
941 /* Add it to our contact list */ | |
942 nm_folder_add_contact_to_list(folder, contact); | |
943 | |
944 /* Update the contact cache */ | |
945 nm_user_add_contact(user, contact); | |
946 | |
947 /* Update the user record cache */ | |
948 if ((details = nm_locate_field(NM_A_FA_USER_DETAILS, | |
949 (NMField *) locate->value))) { | |
950 user_record = nm_find_user_record(user, nm_contact_get_dn(contact)); | |
951 if (user_record == NULL) { | |
952 user_record = nm_create_user_record_from_fields(details); | |
953 nm_user_record_set_dn(user_record, nm_contact_get_dn(contact)); | |
954 nm_user_add_user_record(user, user_record); | |
955 nm_release_user_record(user_record); | |
956 } | |
957 nm_contact_set_user_record(contact, user_record); | |
958 } | |
959 | |
960 nm_release_contact(contact); | |
961 | |
962 locate = nm_locate_field(NM_A_FA_CONTACT, locate+1); | |
963 } | |
964 } |