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
23 #include "nmuserrecord.h"
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",
67 _get_attribute_value(NMField
*field
)
71 if (field
->ptr_value
== 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
;
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.
102 nm_create_user_record_from_fields(NMField
* details
)
104 NMUserRecord
*user_record
;
105 NMField
*field
, *fields
= details
;
107 if (details
== NULL
) {
111 if (details
->type
== NMFIELD_TYPE_ARRAY
) {
112 if (details
->ptr_value
== 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
);
182 nm_user_record_copy(NMUserRecord
* dest
, NMUserRecord
* src
)
184 if (dest
== NULL
|| src
== NULL
)
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
);
201 dest
->dn
= g_strdup(src
->dn
);
208 dest
->cn
= g_strdup(src
->cn
);
210 /* Copy display id */
211 g_free(dest
->display_id
);
212 dest
->display_id
= NULL
;
215 dest
->display_id
= g_strdup(src
->display_id
);
217 /* Copy first name */
222 dest
->fname
= g_strdup(src
->fname
);
229 dest
->lname
= g_strdup(src
->lname
);
232 g_free(dest
->full_name
);
233 dest
->full_name
= NULL
;
236 dest
->full_name
= g_strdup(src
->full_name
);
242 nm_free_fields(&dest
->fields
);
245 dest
->fields
= nm_copy_field_array(src
->fields
);
249 dest
->data
= src
->data
;
253 nm_user_record_add_ref(NMUserRecord
* user_record
)
256 user_record
->ref_count
++;
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
);
284 nm_user_record_get_status(NMUserRecord
* user_record
)
286 if (user_record
== NULL
)
287 return (NMSTATUS_T
) - 1;
289 return user_record
->status
;
294 nm_user_record_get_status_text(NMUserRecord
* user_record
)
296 if (user_record
== NULL
)
299 return user_record
->status_text
;
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
);
313 nm_user_record_get_dn(NMUserRecord
* user_record
)
315 if (user_record
== NULL
)
318 return user_record
->dn
;
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
);
332 nm_user_record_get_userid(NMUserRecord
* user_record
)
334 if (user_record
== NULL
)
337 return user_record
->cn
;
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
);
351 nm_user_record_get_display_id(NMUserRecord
* user_record
)
353 if (user_record
== 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
;
364 nm_user_record_get_full_name(NMUserRecord
* user_record
)
366 if (user_record
== 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",
378 return user_record
->full_name
;
382 nm_user_record_get_first_name(NMUserRecord
* user_record
)
384 if (user_record
== NULL
)
387 return user_record
->fname
;
392 nm_user_record_get_last_name(NMUserRecord
* user_record
)
394 if (user_record
== NULL
)
397 return user_record
->lname
;
401 nm_user_record_get_data(NMUserRecord
* user_record
)
403 if (user_record
== NULL
)
406 return user_record
->data
;
410 nm_user_record_set_data(NMUserRecord
* user_record
, gpointer data
)
412 if (user_record
== NULL
)
415 user_record
->data
= data
;
419 nm_user_record_set_status(NMUserRecord
* user_record
,
420 int status
, const char *text
)
422 if (user_record
== NULL
)
425 user_record
->status
= status
;
427 g_free(user_record
->status_text
);
428 user_record
->status_text
= NULL
;
431 user_record
->status_text
= g_strdup(text
);
435 nm_user_record_get_auth_attr(NMUserRecord
*user_record
)
437 if (user_record
== NULL
)
440 return user_record
->auth_attr
;
444 nm_user_record_get_property_count(NMUserRecord
* user_record
)
446 NMField
*locate
, *fields
;
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
);
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
);
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
);
487 nm_release_property(NMProperty
* property
)
490 g_free(property
->tag
);
492 g_free(property
->value
);
499 nm_property_get_tag(NMProperty
* property
)
502 return property
->tag
;
508 nm_property_get_value(NMProperty
* property
)
511 return property
->value
;