Replace functions which called once with their bodies
[pidgin-git.git] / libpurple / protocols / novell / nmuserrecord.c
blob0c3ada4cd55faee245ac652131103cf3f53271ac
1 /*
2 * nmuserrecord.c
4 * Copyright (c) 2004 Novell, Inc. All Rights Reserved.
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
21 #include <glib.h>
22 #include <string.h>
23 #include "nmuserrecord.h"
24 #include "nmfield.h"
25 #include "nmuser.h"
27 struct _NMUserRecord
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;
43 struct _NMProperty
45 char *tag;
46 char *value;
49 static int count = 0;
51 /* API functions */
53 NMUserRecord *
54 nm_create_user_record()
56 NMUserRecord *user_record = g_new0(NMUserRecord, 1);
58 user_record->ref_count = 1;
60 purple_debug(PURPLE_DEBUG_INFO, "novell", "Creating user_record, total=%d\n",
61 count++);
63 return user_record;
66 static char *
67 _get_attribute_value(NMField *field)
69 char *value = NULL;
71 if (field->ptr_value == NULL)
72 return NULL;
74 if (field->type == NMFIELD_TYPE_UTF8 || field->type == NMFIELD_TYPE_DN) {
76 value = (char *)field->ptr_value;
78 } else if (field->type == NMFIELD_TYPE_MV) {
80 /* Need to handle multi-valued returns, for now
81 * just pick the first value and return it
83 NMField *tmp = (NMField *)field->ptr_value;
84 if (tmp->type == NMFIELD_TYPE_UTF8 || tmp->type == NMFIELD_TYPE_DN) {
85 value = (char *)tmp->ptr_value;
86 } else {
87 return NULL;
90 } else {
91 return NULL;
94 return g_strdup(value);
97 * This creates a user_record for the reference list the
98 * field array that is passed in should be a
99 * NM_A_FA_USER_DETAILS array.
101 NMUserRecord *
102 nm_create_user_record_from_fields(NMField * details)
104 NMUserRecord *user_record;
105 NMField *field, *fields = details;
107 if (details == NULL) {
108 return NULL;
111 if (details->type == NMFIELD_TYPE_ARRAY) {
112 if (details->ptr_value == NULL)
113 return NULL;
114 fields = (NMField *) details->ptr_value;
117 user_record = nm_create_user_record();
119 if ((field = nm_locate_field(NM_A_SZ_AUTH_ATTRIBUTE, fields))) {
121 if (field->ptr_value) {
122 user_record->display_id = _get_attribute_value(field);
123 user_record->auth_attr = TRUE;
127 if ((field = nm_locate_field(NM_A_SZ_DN, fields))) {
129 if (field->ptr_value) {
130 user_record->dn = _get_attribute_value(field);
134 if ((field = nm_locate_field("CN", fields))) {
136 if (field->ptr_value) {
137 user_record->cn = _get_attribute_value(field);
141 if ((field = nm_locate_field("Given Name", fields))) {
143 if (field->ptr_value) {
144 user_record->fname = _get_attribute_value(field);
148 if ((field = nm_locate_field("Surname", fields))) {
150 if (field->ptr_value) {
151 user_record->lname = _get_attribute_value(field);
155 if ((field = nm_locate_field("Full Name", fields))) {
157 if (field->ptr_value) {
158 user_record->full_name = _get_attribute_value(field);
162 if ((field = nm_locate_field(NM_A_SZ_STATUS, fields))) {
164 if (field->ptr_value)
165 user_record->status = atoi((char *) field->ptr_value);
169 if ((field = nm_locate_field(NM_A_SZ_MESSAGE_BODY, fields))) {
171 if (field->ptr_value)
172 user_record->status_text = g_strdup((char *) field->ptr_value);
176 user_record->fields = nm_copy_field_array(fields);
178 return user_record;
181 void
182 nm_user_record_copy(NMUserRecord * dest, NMUserRecord * src)
184 if (dest == NULL || src == NULL)
185 return;
187 dest->status = src->status;
189 /* Copy status text */
190 g_free(dest->status_text);
191 dest->status_text = NULL;
193 if (src->status_text)
194 dest->status_text = g_strdup(src->status_text);
196 /* Copy DN */
197 g_free(dest->dn);
198 dest->dn = NULL;
200 if (src->dn)
201 dest->dn = g_strdup(src->dn);
203 /* Copy CN */
204 g_free(dest->cn);
205 dest->cn = NULL;
207 if (src->cn)
208 dest->cn = g_strdup(src->cn);
210 /* Copy display id */
211 g_free(dest->display_id);
212 dest->display_id = NULL;
214 if (src->display_id)
215 dest->display_id = g_strdup(src->display_id);
217 /* Copy first name */
218 g_free(dest->fname);
219 dest->fname = NULL;
221 if (src->fname)
222 dest->fname = g_strdup(src->fname);
224 /* Copy last name */
225 g_free(dest->lname);
226 dest->lname = NULL;
228 if (src->lname)
229 dest->lname = g_strdup(src->lname);
231 /* Copy full name */
232 g_free(dest->full_name);
233 dest->full_name = NULL;
235 if (src->full_name)
236 dest->full_name = g_strdup(src->full_name);
238 /* Copy fields */
239 if (src->fields) {
241 if (dest->fields) {
242 nm_free_fields(&dest->fields);
245 dest->fields = nm_copy_field_array(src->fields);
248 /* Copy data */
249 dest->data = src->data;
252 void
253 nm_user_record_add_ref(NMUserRecord * user_record)
255 if (user_record)
256 user_record->ref_count++;
259 void
260 nm_release_user_record(NMUserRecord * user_record)
262 if (--(user_record->ref_count) == 0) {
264 purple_debug(PURPLE_DEBUG_INFO, "novell",
265 "Releasing user_record, total=%d\n", --count);
267 g_free(user_record->dn);
268 g_free(user_record->cn);
269 g_free(user_record->display_id);
270 g_free(user_record->fname);
271 g_free(user_record->lname);
272 g_free(user_record->full_name);
273 g_free(user_record->status_text);
275 nm_free_fields(&user_record->fields);
277 g_free(user_record);
281 /* UserRecord API */
283 NMSTATUS_T
284 nm_user_record_get_status(NMUserRecord * user_record)
286 if (user_record == NULL)
287 return (NMSTATUS_T) - 1;
289 return user_record->status;
293 const char *
294 nm_user_record_get_status_text(NMUserRecord * user_record)
296 if (user_record == NULL)
297 return NULL;
299 return user_record->status_text;
302 void
303 nm_user_record_set_dn(NMUserRecord * user_record, const char *dn)
305 if (user_record != NULL && dn != NULL) {
306 g_free(user_record->dn);
308 user_record->dn = g_strdup(dn);
312 const char *
313 nm_user_record_get_dn(NMUserRecord * user_record)
315 if (user_record == NULL)
316 return NULL;
318 return user_record->dn;
321 void
322 nm_user_record_set_userid(NMUserRecord * user_record, const char *userid)
324 if (user_record != NULL && userid != NULL) {
325 g_free(user_record->cn);
327 user_record->cn = g_strdup(userid);
331 const char *
332 nm_user_record_get_userid(NMUserRecord * user_record)
334 if (user_record == NULL)
335 return NULL;
337 return user_record->cn;
340 void
341 nm_user_record_set_display_id(NMUserRecord * user_record, const char *display_id)
343 if (user_record != NULL && display_id != NULL) {
344 g_free(user_record->display_id);
346 user_record->display_id = g_strdup(display_id);
350 const char *
351 nm_user_record_get_display_id(NMUserRecord * user_record)
353 if (user_record == NULL)
354 return NULL;
356 if (user_record->display_id == NULL) {
357 user_record->display_id = nm_typed_to_dotted(user_record->dn);
360 return user_record->display_id;
363 const char *
364 nm_user_record_get_full_name(NMUserRecord * user_record)
366 if (user_record == NULL)
367 return NULL;
369 if (user_record->full_name == NULL) {
370 if (user_record->fname && user_record->lname) {
371 user_record->full_name = g_strdup_printf("%s %s",
372 user_record->fname,
373 user_record->lname);
378 return user_record->full_name;
381 const char *
382 nm_user_record_get_first_name(NMUserRecord * user_record)
384 if (user_record == NULL)
385 return NULL;
387 return user_record->fname;
391 const char *
392 nm_user_record_get_last_name(NMUserRecord * user_record)
394 if (user_record == NULL)
395 return NULL;
397 return user_record->lname;
400 gpointer
401 nm_user_record_get_data(NMUserRecord * user_record)
403 if (user_record == NULL)
404 return NULL;
406 return user_record->data;
409 void
410 nm_user_record_set_data(NMUserRecord * user_record, gpointer data)
412 if (user_record == NULL)
413 return;
415 user_record->data = data;
418 void
419 nm_user_record_set_status(NMUserRecord * user_record,
420 int status, const char *text)
422 if (user_record == NULL)
423 return;
425 user_record->status = status;
427 g_free(user_record->status_text);
428 user_record->status_text = NULL;
430 if (text)
431 user_record->status_text = g_strdup(text);
434 gboolean
435 nm_user_record_get_auth_attr(NMUserRecord *user_record)
437 if (user_record == NULL)
438 return FALSE;
440 return user_record->auth_attr;
444 nm_user_record_get_property_count(NMUserRecord * user_record)
446 NMField *locate, *fields;
448 int count = 0;
450 if (user_record && user_record->fields) {
451 locate = nm_locate_field(NM_A_FA_INFO_DISPLAY_ARRAY,
452 (NMField *) user_record->fields);
453 if (locate && (fields = (NMField *) (locate->ptr_value))) {
454 count = (int) nm_count_fields(fields);
457 return count;
460 NMProperty *
461 nm_user_record_get_property(NMUserRecord * user_record, int index)
463 NMProperty *property = NULL;
464 NMField *field = NULL, *fields, *locate;
466 if (user_record && user_record->fields) {
467 locate = nm_locate_field(NM_A_FA_INFO_DISPLAY_ARRAY,
468 (NMField *) user_record->fields);
469 if (locate && (fields = (NMField *) (locate->ptr_value))) {
470 int max = nm_count_fields(fields);
472 if (index < max) {
473 field = &fields[index];
474 if (field && field->tag && field->ptr_value) {
475 property = g_new0(NMProperty, 1);
476 property->tag = g_strdup(field->tag);
477 property->value = _get_attribute_value(field);
483 return property;
486 void
487 nm_release_property(NMProperty * property)
489 if (property) {
490 g_free(property->tag);
492 g_free(property->value);
494 g_free(property);
498 const char *
499 nm_property_get_tag(NMProperty * property)
501 if (property)
502 return property->tag;
503 else
504 return NULL;
507 const char *
508 nm_property_get_value(NMProperty * property)
510 if (property)
511 return property->value;
512 else
513 return NULL;