comparison libgaim/protocols/novell/nmuserrecord.c @ 14192:60b1bc8dbf37

[gaim-migrate @ 16863] Renamed 'core' to 'libgaim' committer: Tailor Script <tailor@pidgin.im>
author Evan Schoenberg <evan.s@dreskin.net>
date Sat, 19 Aug 2006 01:50:10 +0000
parents
children
comparison
equal deleted inserted replaced
14191:009db0b357b5 14192:60b1bc8dbf37
1 /*
2 * nmuserrecord.c
3 *
4 * Copyright (c) 2004 Novell, Inc. All Rights Reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
20
21 #include <glib.h>
22 #include <string.h>
23 #include "nmuserrecord.h"
24 #include "nmfield.h"
25 #include "nmuser.h"
26
27 struct _NMUserRecord
28 {
29 NMSTATUS_T status;
30 char *status_text;
31 char *dn;
32 char *cn;
33 char *display_id;
34 char *fname;
35 char *lname;
36 char *full_name;
37 NMField *fields;
38 gboolean auth_attr;
39 gpointer data;
40 int ref_count;
41 };
42
43 struct _NMProperty
44 {
45 char *tag;
46 char *value;
47 };
48
49 static int count = 0;
50
51 /* API functions */
52
53 NMUserRecord *
54 nm_create_user_record()
55 {
56 NMUserRecord *user_record = g_new0(NMUserRecord, 1);
57
58 user_record->ref_count = 1;
59
60 gaim_debug(GAIM_DEBUG_INFO, "novell", "Creating user_record, total=%d\n",
61 count++);
62
63 return user_record;
64 }
65
66 static char *
67 _get_attribute_value(NMField *field)
68 {
69 char *value = NULL;
70
71 if (field->ptr_value == NULL)
72 return NULL;
73
74 if (field->type == NMFIELD_TYPE_UTF8 || field->type == NMFIELD_TYPE_DN) {
75
76 value = (char *)field->ptr_value;
77
78 } else if (field->type == NMFIELD_TYPE_MV) {
79
80 /* Need to handle multi-valued returns, for now
81 * just pick the first value and return it
82 */
83 NMField *tmp = (NMField *)field->ptr_value;
84 if ((tmp != NULL) &&
85 ((tmp->type == NMFIELD_TYPE_UTF8) ||
86 (tmp->type == NMFIELD_TYPE_DN))) {
87
88 value = (char *)tmp->ptr_value;
89
90 } else {
91 return NULL;
92 }
93
94 } else {
95 return NULL;
96 }
97
98 return g_strdup(value);
99 }
100 /*
101 * This creates a user_record for the reference list the
102 * field array that is passed in should be a
103 * NM_A_FA_USER_DETAILS array.
104 */
105 NMUserRecord *
106 nm_create_user_record_from_fields(NMField * details)
107 {
108 NMUserRecord *user_record;
109 NMField *field, *fields = details;
110
111 if (details == NULL) {
112 return NULL;
113 }
114
115 if (details->type == NMFIELD_TYPE_ARRAY) {
116 if (details->ptr_value == NULL)
117 return NULL;
118 fields = (NMField *) details->ptr_value;
119 }
120
121 user_record = nm_create_user_record();
122
123 if ((field = nm_locate_field(NM_A_SZ_AUTH_ATTRIBUTE, fields))) {
124
125 if (field->ptr_value) {
126 user_record->display_id = _get_attribute_value(field);
127 user_record->auth_attr = TRUE;
128 }
129 }
130
131 if ((field = nm_locate_field(NM_A_SZ_DN, fields))) {
132
133 if (field->ptr_value) {
134 user_record->dn = _get_attribute_value(field);
135 }
136 }
137
138 if ((field = nm_locate_field("CN", fields))) {
139
140 if (field->ptr_value) {
141 user_record->cn = _get_attribute_value(field);
142 }
143 }
144
145 if ((field = nm_locate_field("Given Name", fields))) {
146
147 if (field->ptr_value) {
148 user_record->fname = _get_attribute_value(field);
149 }
150 }
151
152 if ((field = nm_locate_field("Surname", fields))) {
153
154 if (field->ptr_value) {
155 user_record->lname = _get_attribute_value(field);
156 }
157 }
158
159 if ((field = nm_locate_field("Full Name", fields))) {
160
161 if (field->ptr_value) {
162 user_record->full_name = _get_attribute_value(field);
163 }
164 }
165
166 if ((field = nm_locate_field(NM_A_SZ_STATUS, fields))) {
167
168 if (field->ptr_value)
169 user_record->status = atoi((char *) field->ptr_value);
170
171 }
172
173 if ((field = nm_locate_field(NM_A_SZ_MESSAGE_BODY, fields))) {
174
175 if (field->ptr_value)
176 user_record->status_text = g_strdup((char *) field->ptr_value);
177
178 }
179
180 user_record->fields = nm_copy_field_array(fields);
181
182 return user_record;
183 }
184
185 void
186 nm_user_record_copy(NMUserRecord * dest, NMUserRecord * src)
187 {
188 if (dest == NULL || src == NULL)
189 return;
190
191 dest->status = src->status;
192
193 /* Copy status text */
194 if (dest->status_text) {
195 g_free(dest->status_text);
196 dest->status_text = NULL;
197 }
198
199 if (src->status_text)
200 dest->status_text = g_strdup(src->status_text);
201
202 /* Copy DN */
203 if (dest->dn) {
204 g_free(dest->dn);
205 dest->dn = NULL;
206 }
207
208 if (src->dn)
209 dest->dn = g_strdup(src->dn);
210
211 /* Copy CN */
212 if (dest->cn) {
213 g_free(dest->cn);
214 dest->cn = NULL;
215 }
216
217 if (src->cn)
218 dest->cn = g_strdup(src->cn);
219
220 /* Copy display id */
221 if (dest->display_id) {
222 g_free(dest->display_id);
223 dest->display_id = NULL;
224 }
225
226 if (src->display_id)
227 dest->display_id = g_strdup(src->display_id);
228
229 /* Copy first name */
230 if (dest->fname) {
231 g_free(dest->fname);
232 dest->fname = NULL;
233 }
234
235 if (src->fname)
236 dest->fname = g_strdup(src->fname);
237
238 /* Copy last name */
239 if (dest->lname) {
240 g_free(dest->lname);
241 dest->lname = NULL;
242 }
243
244 if (src->lname)
245 dest->lname = g_strdup(src->lname);
246
247 /* Copy full name */
248 if (dest->full_name) {
249 g_free(dest->full_name);
250 dest->full_name = NULL;
251 }
252
253 if (src->full_name)
254 dest->full_name = g_strdup(src->full_name);
255
256 /* Copy fields */
257 if (src->fields) {
258
259 if (dest->fields) {
260 nm_free_fields(&dest->fields);
261 }
262
263 dest->fields = nm_copy_field_array(src->fields);
264 }
265
266 /* Copy data */
267 dest->data = src->data;
268 }
269
270 void
271 nm_user_record_add_ref(NMUserRecord * user_record)
272 {
273 if (user_record)
274 user_record->ref_count++;
275 }
276
277 void
278 nm_release_user_record(NMUserRecord * user_record)
279 {
280 if (--(user_record->ref_count) == 0) {
281
282 gaim_debug(GAIM_DEBUG_INFO, "novell",
283 "Releasing user_record, total=%d\n", --count);
284
285 if (user_record->dn) {
286 g_free(user_record->dn);
287 }
288
289 if (user_record->cn) {
290 g_free(user_record->cn);
291 }
292
293 if (user_record->display_id) {
294 g_free(user_record->display_id);
295 }
296
297 if (user_record->fname) {
298 g_free(user_record->fname);
299 }
300
301 if (user_record->lname) {
302 g_free(user_record->lname);
303 }
304
305 if (user_record->full_name) {
306 g_free(user_record->full_name);
307 }
308
309 if (user_record->status_text) {
310 g_free(user_record->status_text);
311 }
312
313 nm_free_fields(&user_record->fields);
314
315 g_free(user_record);
316 }
317 }
318
319 /* UserRecord API */
320
321 NMSTATUS_T
322 nm_user_record_get_status(NMUserRecord * user_record)
323 {
324 if (user_record == NULL)
325 return (NMSTATUS_T) - 1;
326
327 return user_record->status;
328
329 }
330
331 const char *
332 nm_user_record_get_status_text(NMUserRecord * user_record)
333 {
334 if (user_record == NULL)
335 return NULL;
336
337 return user_record->status_text;
338 }
339
340 void
341 nm_user_record_set_dn(NMUserRecord * user_record, const char *dn)
342 {
343 if (user_record != NULL && dn != NULL) {
344 if (user_record->dn)
345 g_free(user_record->dn);
346
347 user_record->dn = g_strdup(dn);
348 }
349 }
350
351 const char *
352 nm_user_record_get_dn(NMUserRecord * user_record)
353 {
354 if (user_record == NULL)
355 return NULL;
356
357 return user_record->dn;
358 }
359
360 void
361 nm_user_record_set_userid(NMUserRecord * user_record, const char *userid)
362 {
363 if (user_record != NULL && userid != NULL) {
364 if (user_record->cn)
365 g_free(user_record->cn);
366
367 user_record->cn = g_strdup(userid);
368 }
369 }
370
371 const char *
372 nm_user_record_get_userid(NMUserRecord * user_record)
373 {
374 if (user_record == NULL)
375 return NULL;
376
377 return user_record->cn;
378 }
379
380 void
381 nm_user_record_set_display_id(NMUserRecord * user_record, const char *display_id)
382 {
383 if (user_record != NULL && display_id != NULL) {
384 if (user_record->display_id)
385 g_free(user_record->display_id);
386
387 user_record->display_id = g_strdup(display_id);
388 }
389 }
390
391 const char *
392 nm_user_record_get_display_id(NMUserRecord * user_record)
393 {
394 if (user_record == NULL)
395 return NULL;
396
397 if (user_record->display_id == NULL) {
398 user_record->display_id = nm_typed_to_dotted(user_record->dn);
399 }
400
401 return user_record->display_id;
402 }
403
404 const char *
405 nm_user_record_get_full_name(NMUserRecord * user_record)
406 {
407 if (user_record == NULL)
408 return NULL;
409
410 if (user_record->full_name == NULL) {
411 if (user_record->fname && user_record->lname) {
412 user_record->full_name = g_strdup_printf("%s %s",
413 user_record->fname,
414 user_record->lname);
415
416 }
417 }
418
419 return user_record->full_name;
420 }
421
422 const char *
423 nm_user_record_get_first_name(NMUserRecord * user_record)
424 {
425 if (user_record == NULL)
426 return NULL;
427
428 return user_record->fname;
429
430 }
431
432 const char *
433 nm_user_record_get_last_name(NMUserRecord * user_record)
434 {
435 if (user_record == NULL)
436 return NULL;
437
438 return user_record->lname;
439 }
440
441 gpointer
442 nm_user_record_get_data(NMUserRecord * user_record)
443 {
444 if (user_record == NULL)
445 return NULL;
446
447 return user_record->data;
448 }
449
450 void
451 nm_user_record_set_data(NMUserRecord * user_record, gpointer data)
452 {
453 if (user_record == NULL)
454 return;
455
456 user_record->data = data;
457 }
458
459 void
460 nm_user_record_set_status(NMUserRecord * user_record,
461 int status, const char *text)
462 {
463 if (user_record == NULL)
464 return;
465
466 user_record->status = status;
467
468 if (user_record->status_text) {
469 g_free(user_record->status_text);
470 user_record->status_text = NULL;
471 }
472
473 if (text)
474 user_record->status_text = g_strdup(text);
475 }
476
477 gboolean
478 nm_user_record_get_auth_attr(NMUserRecord *user_record)
479 {
480 if (user_record == NULL)
481 return FALSE;
482
483 return user_record->auth_attr;
484 }
485
486 int
487 nm_user_record_get_property_count(NMUserRecord * user_record)
488 {
489 NMField *locate, *fields;
490
491 int count = 0;
492
493 if (user_record && user_record->fields) {
494 locate = nm_locate_field(NM_A_FA_INFO_DISPLAY_ARRAY,
495 (NMField *) user_record->fields);
496 if (locate && (fields = (NMField *) (locate->ptr_value))) {
497 count = (int) nm_count_fields(fields);
498 }
499 }
500 return count;
501 }
502
503 NMProperty *
504 nm_user_record_get_property(NMUserRecord * user_record, int index)
505 {
506 NMProperty *property = NULL;
507 NMField *field = NULL, *fields, *locate;
508
509 if (user_record && user_record->fields) {
510 locate = nm_locate_field(NM_A_FA_INFO_DISPLAY_ARRAY,
511 (NMField *) user_record->fields);
512 if (locate && (fields = (NMField *) (locate->ptr_value))) {
513 int max = nm_count_fields(fields);
514
515 if (index < max) {
516 if (user_record) {
517 field = &fields[index];
518 if (field && field->tag && field->ptr_value) {
519 property = g_new0(NMProperty, 1);
520 property->tag = g_strdup(field->tag);
521 property->value = _get_attribute_value(field);
522 }
523 }
524 }
525 }
526 }
527
528 return property;
529 }
530
531 void
532 nm_release_property(NMProperty * property)
533 {
534 if (property) {
535 if (property->tag)
536 g_free(property->tag);
537
538 if (property->value)
539 g_free(property->value);
540
541 g_free(property);
542 }
543 }
544
545 const char *
546 nm_property_get_tag(NMProperty * property)
547 {
548 if (property)
549 return property->tag;
550 else
551 return NULL;
552 }
553
554 const char *
555 nm_property_get_value(NMProperty * property)
556 {
557 if (property)
558 return property->value;
559 else
560 return NULL;
561 }