* Copyright (c) 2004 Novell, Inc. All Rights Reserved. * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; version 2 of the License. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA NMUserRecord *user_record; static void _release_folder_contacts(NMFolder * folder); static void _release_folder_folders(NMFolder * folder); static void _add_contacts(NMUser * user, NMFolder * folder, NMField * fields); static void _add_folders(NMFolder * root, NMField * fields); /********************************************************************* *********************************************************************/ NMContact *contact = g_new0(NMContact, 1); purple_debug(PURPLE_DEBUG_INFO, "novell", "Creating contact, total=%d\n", * This creates a contact for the contact list. The * field array that is passed in should be a nm_create_contact_from_fields(NMField * fields) if ( fields == NULL || fields->tag == NULL || fields->ptr_value == 0 || strcmp(fields->tag, NM_A_FA_CONTACT) ) contact = nm_create_contact(); if ((field = nm_locate_field(NM_A_SZ_OBJECT_ID, (NMField *) fields->ptr_value))) { contact->id = atoi((char *) field->ptr_value); if ((field = nm_locate_field(NM_A_SZ_PARENT_ID, (NMField *) fields->ptr_value))) { contact->parent_id = atoi((char *) field->ptr_value); nm_locate_field(NM_A_SZ_SEQUENCE_NUMBER, (NMField *) fields->ptr_value))) { contact->seq = atoi((char *) field->ptr_value); nm_locate_field(NM_A_SZ_DISPLAY_NAME, (NMField *) fields->ptr_value))) { contact->display_name = g_strdup((char *) field->ptr_value); if ((field = nm_locate_field(NM_A_SZ_DN, (NMField *) fields->ptr_value))) { contact->dn = g_strdup((char *) field->ptr_value); nm_contact_update_list_properties(NMContact * contact, NMField * fields) if (contact == NULL || fields == NULL || fields->ptr_value == 0) if ((field = nm_locate_field(NM_A_SZ_OBJECT_ID, (NMField *) fields->ptr_value))) { contact->id = atoi((char *)field->ptr_value); if ((field = nm_locate_field(NM_A_SZ_PARENT_ID, (NMField *) fields->ptr_value))) { contact->parent_id = atoi((char *) field->ptr_value); nm_locate_field(NM_A_SZ_SEQUENCE_NUMBER, (NMField *) fields->ptr_value))) { contact->seq = atoi((char *) field->ptr_value); nm_locate_field(NM_A_SZ_DISPLAY_NAME, (NMField *) fields->ptr_value))) { if (contact->display_name) g_free(contact->display_name); contact->display_name = g_strdup((char *) field->ptr_value); if ((field = nm_locate_field(NM_A_SZ_DN, (NMField *) fields->ptr_value))) { contact->dn = g_strdup((char *) field->ptr_value); nm_contact_to_fields(NMContact * contact) fields = nm_field_add_pointer(fields, NM_A_SZ_OBJECT_ID, 0, NMFIELD_METHOD_VALID, 0, g_strdup_printf("%d", contact->id), NMFIELD_TYPE_UTF8); fields = nm_field_add_pointer(fields, NM_A_SZ_PARENT_ID, 0, NMFIELD_METHOD_VALID, 0, g_strdup_printf("%d", contact->parent_id), NMFIELD_TYPE_UTF8); fields = nm_field_add_pointer(fields, NM_A_SZ_SEQUENCE_NUMBER, 0, NMFIELD_METHOD_VALID, 0, g_strdup_printf("%d", contact->seq), NMFIELD_TYPE_UTF8); if (contact->display_name != NULL) { fields = nm_field_add_pointer(fields, NM_A_SZ_DISPLAY_NAME, 0, NMFIELD_METHOD_VALID, 0, g_strdup(contact->display_name), NMFIELD_TYPE_UTF8); if (contact->dn != NULL) { fields = nm_field_add_pointer(fields, NM_A_SZ_DN, 0, NMFIELD_METHOD_VALID, 0, g_strdup(contact->dn), NMFIELD_TYPE_UTF8); nm_contact_add_ref(NMContact * contact) nm_release_contact(NMContact * contact) if (--(contact->ref_count) == 0) { purple_debug(PURPLE_DEBUG_INFO, "novell", "Releasing contact, total=%d\n", --count); if (contact->display_name) { g_free(contact->display_name); if (contact->user_record) { nm_release_user_record(contact->user_record); nm_contact_get_display_name(NMContact * contact) if (contact->user_record != NULL && contact->display_name == NULL) { const char *full_name, *lname, *fname, *cn, *display_id; full_name = nm_user_record_get_full_name(contact->user_record); fname = nm_user_record_get_first_name(contact->user_record); lname = nm_user_record_get_last_name(contact->user_record); cn = nm_user_record_get_userid(contact->user_record); display_id = nm_user_record_get_display_id(contact->user_record); /* Try to build a display name. */ contact->display_name = g_strdup(full_name); } else if (fname && lname) { contact->display_name = g_strdup_printf("%s %s", fname, lname); /* If auth attribute is set use it */ if (nm_user_record_get_auth_attr(contact->user_record) && contact->display_name = g_strdup(display_id); /* Use CN or display id */ contact->display_name = g_strdup(cn); contact->display_name = g_strdup(display_id); return contact->display_name; nm_contact_set_display_name(NMContact * contact, const char *display_name) if (contact->display_name) { g_free(contact->display_name); contact->display_name = NULL; contact->display_name = g_strdup(display_name); nm_contact_set_dn(NMContact * contact, const char *dn) contact->dn = g_strdup(dn); nm_contact_get_dn(NMContact * contact) nm_contact_get_data(NMContact * contact) nm_contact_get_id(NMContact * contact) nm_contact_get_parent_id(NMContact * contact) return contact->parent_id; nm_contact_set_data(NMContact * contact, gpointer data) nm_contact_set_user_record(NMContact * contact, NMUserRecord * user_record) if (contact->user_record) { nm_release_user_record(contact->user_record); nm_user_record_add_ref(user_record); contact->user_record = user_record; nm_contact_get_user_record(NMContact * contact) return contact->user_record; nm_contact_get_userid(NMContact * contact) NMUserRecord *user_record; const char *userid = NULL; user_record = nm_contact_get_user_record(contact); userid = nm_user_record_get_userid(user_record); nm_contact_get_display_id(NMContact * contact) NMUserRecord *user_record; user_record = nm_contact_get_user_record(contact); id = nm_user_record_get_display_id(user_record); /********************************************************************* *********************************************************************/ nm_create_folder(const char *name) NMFolder *folder = g_new0(NMFolder, 1); folder->name = g_strdup(name); nm_create_folder_from_fields(NMField * fields) if (fields == NULL || fields->ptr_value == 0) folder = g_new0(NMFolder, 1); if ((field = nm_locate_field(NM_A_SZ_OBJECT_ID, (NMField *) fields->ptr_value))) { folder->id = atoi((char *) field->ptr_value); nm_locate_field(NM_A_SZ_SEQUENCE_NUMBER, (NMField *) fields->ptr_value))) { folder->seq = atoi((char *) field->ptr_value); nm_locate_field(NM_A_SZ_DISPLAY_NAME, (NMField *) fields->ptr_value))) { folder->name = g_strdup((char *) field->ptr_value); nm_folder_to_fields(NMFolder * folder) fields = nm_field_add_pointer(fields, NM_A_SZ_OBJECT_ID, 0, NMFIELD_METHOD_VALID, 0, g_strdup_printf("%d", folder->id), NMFIELD_TYPE_UTF8); fields = nm_field_add_pointer(fields, NM_A_SZ_PARENT_ID, 0, NMFIELD_METHOD_VALID, 0, g_strdup("0"), NMFIELD_TYPE_UTF8); fields = nm_field_add_pointer(fields, NM_A_SZ_TYPE, 0, NMFIELD_METHOD_VALID, 0, g_strdup("1"), NMFIELD_TYPE_UTF8); fields = nm_field_add_pointer(fields, NM_A_SZ_SEQUENCE_NUMBER, 0, NMFIELD_METHOD_VALID, 0, g_strdup_printf("%d", folder->seq), NMFIELD_TYPE_UTF8); if (folder->name != NULL) { fields = nm_field_add_pointer(fields, NM_A_SZ_DISPLAY_NAME, 0, NMFIELD_METHOD_VALID, 0, g_strdup(folder->name), NMFIELD_TYPE_UTF8); nm_folder_update_list_properties(NMFolder * folder, NMField * fields) if (folder == NULL || fields == NULL || fields->ptr_value == 0) if ((field = nm_locate_field(NM_A_SZ_OBJECT_ID, (NMField *) fields->ptr_value))) { folder->id = atoi((char *) field->ptr_value); nm_locate_field(NM_A_SZ_SEQUENCE_NUMBER, (NMField *) fields->ptr_value))) { folder->seq = atoi((char *) field->ptr_value); nm_locate_field(NM_A_SZ_DISPLAY_NAME, (NMField *) fields->ptr_value))) { folder->name = g_strdup((char *) field->ptr_value); nm_release_folder(NMFolder * folder) if (--(folder->ref_count) == 0) { _release_folder_folders(folder); _release_folder_contacts(folder); nm_folder_add_ref(NMFolder * folder) nm_folder_get_subfolder_count(NMFolder * folder) return g_slist_length(folder->folders); nm_folder_get_subfolder(NMFolder * folder, int index) return (NMFolder *) g_slist_nth_data(folder->folders, index); nm_folder_get_contact_count(NMFolder * folder) if (folder->contacts != NULL) return g_slist_length(folder->contacts); nm_folder_get_contact(NMFolder * folder, int index) return (NMContact *) g_slist_nth_data(folder->contacts, index); nm_folder_get_name(NMFolder * folder) nm_folder_set_name(NMFolder * folder, const char *name) if (folder == NULL || name == NULL) folder->name = g_strdup(name); nm_folder_get_id(NMFolder * folder) nm_folder_add_folder_to_list(NMFolder * root, NMFolder * folder) if (root == NULL || folder == NULL) if (folder->seq <= ((NMFolder *) node->data)->seq) { nm_folder_add_ref(folder); root->folders = g_slist_insert_before(root->folders, node, folder); node = g_slist_next(node); nm_folder_add_ref(folder); root->folders = g_slist_append(root->folders, folder); nm_folder_remove_contact(NMFolder * folder, NMContact * contact) if (folder == NULL || contact == NULL) if (contact->id == ((NMContact *) (node->data))->id) { folder->contacts = g_slist_remove(folder->contacts, node->data); nm_release_contact(contact); node = g_slist_next(node); nm_folder_add_contact_to_list(NMFolder * root_folder, NMContact * contact) NMFolder *folder = root_folder; if (folder == NULL || contact == NULL) /* Find folder to add contact to */ if (contact->parent_id != 0) { folder = (NMFolder *) node->data; if (contact->parent_id == folder->id) { node = g_slist_next(node); /* Add contact to list */ if (contact->seq <= ((NMContact *) (node->data))->seq) { nm_contact_add_ref(contact); g_slist_insert_before(folder->contacts, node, contact); node = g_slist_next(node); nm_contact_add_ref(contact); folder->contacts = g_slist_append(folder->contacts, contact); nm_folder_add_contacts_and_folders(NMUser * user, NMFolder * root, /* Add the contacts and folders from the field array */ if (user && root && fields) { _add_folders(root, fields); _add_contacts(user, root, fields); nm_folder_find_item_by_object_id(NMFolder * root_folder, int object_id) /* Check all contacts for the top level folder */ cnt = nm_folder_get_contact_count(root_folder); for (i = 0; i < cnt; i++) { contact = nm_folder_get_contact(root_folder, i); if (contact && (contact->id == object_id)) { /* If we haven't found the item yet, check the subfolders */ cnt = nm_folder_get_subfolder_count(root_folder); for (i = 0; (i < cnt) && (item == NULL); i++) { folder = nm_folder_get_subfolder(root_folder, i); /* Check the id of this folder */ if (folder && (folder->id == object_id)) { /* Check all contacts for this folder */ cnt2 = nm_folder_get_contact_count(folder); for (j = 0; j < cnt2; j++) { contact = nm_folder_get_contact(folder, j); if (contact && (contact->id == object_id)) { nm_folder_find_contact_by_userid(NMFolder * folder, const char *userid) NMContact *tmp, *contact = NULL; if (folder == NULL || userid == NULL) cnt = nm_folder_get_contact_count(folder); for (i = 0; i < cnt; i++) { tmp = nm_folder_get_contact(folder, i); if (tmp && nm_utf8_str_equal(nm_contact_get_userid(tmp), userid)) { nm_folder_find_contact_by_display_id(NMFolder * folder, const char *display_id) NMContact *tmp, *contact = NULL; if (folder == NULL || display_id == NULL) cnt = nm_folder_get_contact_count(folder); for (i = 0; i < cnt; i++) { tmp = nm_folder_get_contact(folder, i); if (tmp && nm_utf8_str_equal(nm_contact_get_display_id(tmp), display_id)) { nm_folder_find_contact(NMFolder * folder, const char *dn) NMContact *tmp, *contact = NULL; if (folder == NULL || dn == NULL) cnt = nm_folder_get_contact_count(folder); for (i = 0; i < cnt; i++) { tmp = nm_folder_get_contact(folder, i); if (tmp && nm_utf8_str_equal(nm_contact_get_dn(tmp), dn)) { /********************************************************************* *********************************************************************/ _release_folder_contacts(NMFolder * folder) for (cnode = folder->contacts; cnode; cnode = cnode->next) { nm_release_contact(contact); g_slist_free(folder->contacts); _release_folder_folders(NMFolder * folder) for (fnode = folder->folders; fnode; fnode = fnode->next) { nm_release_folder(subfolder); g_slist_free(folder->folders); _add_folders(NMFolder * root, NMField * fields) locate = nm_locate_field(NM_A_FA_FOLDER, fields); /* Create a new folder */ folder = nm_create_folder_from_fields(locate); /* Add subfolder to roots folder list */ nm_folder_add_folder_to_list(root, folder); /* Decrement the ref count */ nm_release_folder(folder); /* Find the next folder */ locate = nm_locate_field(NM_A_FA_FOLDER, locate+1); _add_contacts(NMUser * user, NMFolder * folder, NMField * fields) NMContact *contact = NULL; NMField *locate = NULL, *details; NMUserRecord *user_record = NULL; locate = nm_locate_field(NM_A_FA_CONTACT, fields); /* Create a new contact from the fields */ contact = nm_create_contact_from_fields(locate); /* Add it to our contact list */ nm_folder_add_contact_to_list(folder, contact); /* Update the contact cache */ nm_user_add_contact(user, contact); /* Update the user record cache */ if ((details = nm_locate_field(NM_A_FA_USER_DETAILS, (NMField *) locate->ptr_value))) { user_record = nm_find_user_record(user, nm_contact_get_dn(contact)); if (user_record == NULL) { user_record = nm_create_user_record_from_fields(details); nm_user_record_set_dn(user_record, nm_contact_get_dn(contact)); nm_user_add_user_record(user, user_record); nm_release_user_record(user_record); nm_contact_set_user_record(contact, user_record); nm_release_contact(contact); locate = nm_locate_field(NM_A_FA_CONTACT, locate+1);