Mercurial > pidgin
annotate src/protocols/novell/nmcontact.c @ 8769:0bde91a2ea4d
[gaim-migrate @ 9530]
Ughhh... C99
committer: Tailor Script <tailor@pidgin.im>
author | Christian Hammond <chipx86@chipx86.com> |
---|---|
date | Fri, 23 Apr 2004 06:14:15 +0000 |
parents | 046dd8ef2920 |
children | 7df86465ec82 |
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 { | |
8769
0bde91a2ea4d
[gaim-migrate @ 9530]
Christian Hammond <chipx86@chipx86.com>
parents:
8684
diff
changeset
|
704 GSList *node = folder->contacts; |
0bde91a2ea4d
[gaim-migrate @ 9530]
Christian Hammond <chipx86@chipx86.com>
parents:
8684
diff
changeset
|
705 |
8675 | 706 if (folder == NULL || contact == NULL) |
707 return; | |
708 | |
709 while (node) { | |
710 if (contact->id == ((NMContact *) (node->data))->id) { | |
711 folder->contacts = g_slist_remove(folder->contacts, node->data); | |
712 nm_release_contact(contact); | |
713 break; | |
714 } | |
715 node = g_slist_next(node); | |
716 } | |
717 } | |
718 | |
719 void | |
720 nm_folder_add_contact_to_list(NMFolder * root_folder, NMContact * contact) | |
721 { | |
722 GSList *node = NULL; | |
723 NMFolder *folder = root_folder; | |
8684
046dd8ef2920
[gaim-migrate @ 9437]
Christian Hammond <chipx86@chipx86.com>
parents:
8675
diff
changeset
|
724 |
8675 | 725 if (folder == NULL || contact == NULL) |
726 return; | |
727 | |
728 /* Find folder to add contact to */ | |
729 if (contact->parent_id != 0) { | |
730 node = folder->folders; | |
731 while (node) { | |
732 folder = (NMFolder *) node->data; | |
733 if (contact->parent_id == folder->id) { | |
734 break; | |
735 } | |
736 folder = NULL; | |
737 node = g_slist_next(node); | |
738 } | |
739 } | |
740 | |
741 /* Add contact to list */ | |
742 if (folder) { | |
743 node = folder->contacts; | |
744 while (node) { | |
745 if (contact->seq <= ((NMContact *) (node->data))->seq) { | |
746 nm_contact_add_ref(contact); | |
747 folder->contacts = | |
748 g_slist_insert_before(folder->contacts, node, contact); | |
749 break; | |
750 } | |
751 node = g_slist_next(node); | |
752 } | |
753 | |
754 if (node == NULL) { | |
755 nm_contact_add_ref(contact); | |
756 folder->contacts = g_slist_append(folder->contacts, contact); | |
757 } | |
758 } | |
759 } | |
760 | |
761 void | |
762 nm_folder_add_contacts_and_folders(NMUser * user, NMFolder * root, | |
763 NMField * fields) | |
764 { | |
765 /* Add the contacts and folders from the field array */ | |
766 if (user && root && fields) { | |
767 _add_folders(root, fields); | |
768 _add_contacts(user, root, fields); | |
769 } | |
770 } | |
771 | |
772 gpointer | |
773 nm_folder_find_item_by_object_id(NMFolder * root_folder, int object_id) | |
774 { | |
775 int cnt, cnt2, i, j; | |
776 gpointer item = NULL; | |
777 NMFolder *folder; | |
778 NMContact *contact; | |
779 | |
780 if (root_folder == NULL) | |
781 return NULL; | |
782 | |
783 /* Check all contacts for the top level folder */ | |
784 cnt = nm_folder_get_contact_count(root_folder); | |
785 for (i = 0; i < cnt; i++) { | |
786 contact = nm_folder_get_contact(root_folder, i); | |
787 if (contact && (contact->id == object_id)) { | |
788 item = contact; | |
789 break; | |
790 } | |
791 } | |
792 | |
793 /* If we haven't found the item yet, check the subfolders */ | |
794 if (item == NULL) { | |
795 cnt = nm_folder_get_subfolder_count(root_folder); | |
796 for (i = 0; (i < cnt) && (item == NULL); i++) { | |
797 folder = nm_folder_get_subfolder(root_folder, i); | |
798 | |
799 /* Check the id of this folder */ | |
800 if (folder && (folder->id == object_id)) { | |
801 item = folder; | |
802 break; | |
803 } | |
804 | |
805 /* Check all contacts for this folder */ | |
806 cnt2 = nm_folder_get_contact_count(folder); | |
807 for (j = 0; j < cnt2; j++) { | |
808 contact = nm_folder_get_contact(folder, j); | |
809 if (contact && (contact->id == object_id)) { | |
810 item = contact; | |
811 break; | |
812 } | |
813 } | |
814 } | |
815 } | |
816 | |
817 return item; | |
818 } | |
819 | |
820 NMContact * | |
821 nm_folder_find_contact_by_userid(NMFolder * folder, const char *userid) | |
822 { | |
823 int cnt, i; | |
824 NMContact *tmp, *contact = NULL; | |
825 | |
826 if (folder == NULL || userid == NULL) | |
827 return NULL; | |
828 | |
829 cnt = nm_folder_get_contact_count(folder); | |
830 for (i = 0; i < cnt; i++) { | |
831 tmp = nm_folder_get_contact(folder, i); | |
832 if (tmp && nm_utf8_str_equal(nm_contact_get_userid(tmp), userid)) { | |
833 contact = tmp; | |
834 break; | |
835 } | |
836 } | |
837 | |
838 return contact; | |
839 } | |
840 | |
841 NMContact * | |
842 nm_folder_find_contact(NMFolder * folder, const char *dn) | |
843 { | |
844 int cnt, i; | |
845 NMContact *tmp, *contact = NULL; | |
8684
046dd8ef2920
[gaim-migrate @ 9437]
Christian Hammond <chipx86@chipx86.com>
parents:
8675
diff
changeset
|
846 |
8675 | 847 if (folder == NULL || dn == NULL) |
848 return NULL; | |
849 | |
850 cnt = nm_folder_get_contact_count(folder); | |
851 for (i = 0; i < cnt; i++) { | |
852 tmp = nm_folder_get_contact(folder, i); | |
853 if (tmp && nm_utf8_str_equal(nm_contact_get_dn(tmp), dn)) { | |
854 contact = tmp; | |
855 break; | |
856 } | |
857 } | |
858 | |
859 return contact; | |
860 } | |
861 | |
862 | |
863 /********************************************************************* | |
864 * Utility functions | |
865 *********************************************************************/ | |
866 | |
867 static void | |
868 _release_folder_contacts(NMFolder * folder) | |
869 { | |
870 GSList *cnode; | |
871 NMContact *contact; | |
872 | |
873 for (cnode = folder->contacts; cnode; cnode = cnode->next) { | |
874 contact = cnode->data; | |
875 cnode->data = NULL; | |
876 nm_release_contact(contact); | |
877 } | |
878 | |
879 g_slist_free(folder->contacts); | |
880 folder->contacts = NULL; | |
881 } | |
882 | |
883 static void | |
884 _release_folder_folders(NMFolder * folder) | |
885 { | |
886 GSList *fnode; | |
887 NMFolder *subfolder; | |
8684
046dd8ef2920
[gaim-migrate @ 9437]
Christian Hammond <chipx86@chipx86.com>
parents:
8675
diff
changeset
|
888 |
8675 | 889 if (folder == NULL) |
890 return; | |
891 | |
892 for (fnode = folder->folders; fnode; fnode = fnode->next) { | |
893 subfolder = fnode->data; | |
894 fnode->data = NULL; | |
895 nm_release_folder(subfolder); | |
896 } | |
897 | |
898 g_slist_free(folder->folders); | |
899 folder->folders = NULL; | |
900 } | |
901 | |
902 static void | |
903 _add_folders(NMFolder * root, NMField * fields) | |
904 { | |
905 NMFolder *folder = NULL; | |
906 NMField *locate = NULL; | |
907 | |
908 locate = nm_locate_field(NM_A_FA_FOLDER, fields); | |
909 while (locate != NULL) { | |
910 | |
911 /* Create a new folder */ | |
912 folder = nm_create_folder_from_fields(locate); | |
913 | |
914 /* Add subfolder to roots folder list */ | |
915 nm_folder_add_folder_to_list(root, folder); | |
916 | |
917 /* Decrement the ref count */ | |
918 nm_release_folder(folder); | |
919 | |
920 /* Find the next folder */ | |
921 locate = nm_locate_field(NM_A_FA_FOLDER, locate+1); | |
922 | |
923 } | |
924 } | |
925 | |
926 static void | |
927 _add_contacts(NMUser * user, NMFolder * folder, NMField * fields) | |
928 { | |
929 NMContact *contact = NULL; | |
930 NMField *locate = NULL, *details; | |
931 NMUserRecord *user_record = NULL; | |
932 | |
933 locate = nm_locate_field(NM_A_FA_CONTACT, fields); | |
934 while (locate != NULL) { | |
935 | |
936 /* Create a new contact from the fields */ | |
937 contact = nm_create_contact_from_fields(locate); | |
938 | |
939 /* Add it to our contact list */ | |
940 nm_folder_add_contact_to_list(folder, contact); | |
941 | |
942 /* Update the contact cache */ | |
943 nm_user_add_contact(user, contact); | |
944 | |
945 /* Update the user record cache */ | |
946 if ((details = nm_locate_field(NM_A_FA_USER_DETAILS, | |
947 (NMField *) locate->value))) { | |
948 user_record = nm_find_user_record(user, nm_contact_get_dn(contact)); | |
949 if (user_record == NULL) { | |
950 user_record = nm_create_user_record_from_fields(details); | |
951 nm_user_record_set_dn(user_record, nm_contact_get_dn(contact)); | |
952 nm_user_add_user_record(user, user_record); | |
953 nm_release_user_record(user_record); | |
954 } | |
955 nm_contact_set_user_record(contact, user_record); | |
956 } | |
957 | |
958 nm_release_contact(contact); | |
959 | |
960 locate = nm_locate_field(NM_A_FA_CONTACT, locate+1); | |
961 } | |
962 } |