📄 nmcontact.c
字号:
}NMField *nm_folder_to_fields(NMFolder * folder){ NMField *fields = NULL; if (folder == NULL) return NULL; 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); } return fields;}voidnm_folder_update_list_properties(NMFolder * folder, NMField * fields){ NMField *field; if (folder == NULL || fields == NULL || fields->ptr_value == 0) return; if ((field = nm_locate_field(NM_A_SZ_OBJECT_ID, (NMField *) fields->ptr_value))) { if (field->ptr_value) folder->id = atoi((char *) field->ptr_value); } if ((field = nm_locate_field(NM_A_SZ_SEQUENCE_NUMBER, (NMField *) fields->ptr_value))) { if (field->ptr_value) folder->seq = atoi((char *) field->ptr_value); } if ((field = nm_locate_field(NM_A_SZ_DISPLAY_NAME, (NMField *) fields->ptr_value))) { if (field->ptr_value) { if (folder->name) g_free(folder->name); folder->name = g_strdup((char *) field->ptr_value); } }}voidnm_release_folder(NMFolder * folder){ if (folder == NULL) return; if (--(folder->ref_count) == 0) { if (folder->name) { g_free(folder->name); } if (folder->folders) { _release_folder_folders(folder); } if (folder->contacts) { _release_folder_contacts(folder); } g_free(folder); }}voidnm_folder_add_ref(NMFolder * folder){ if (folder) folder->ref_count++;}intnm_folder_get_subfolder_count(NMFolder * folder){ if (folder == NULL) return 0; if (folder->folders) return g_slist_length(folder->folders); else return 0;}NMFolder *nm_folder_get_subfolder(NMFolder * folder, int index){ if (folder == NULL) return NULL; if (folder->folders) return (NMFolder *) g_slist_nth_data(folder->folders, index); else return NULL;}intnm_folder_get_contact_count(NMFolder * folder){ if (folder == NULL) return 0; if (folder->contacts != NULL) return g_slist_length(folder->contacts); else return 0;}NMContact *nm_folder_get_contact(NMFolder * folder, int index){ if (folder == NULL) return NULL; if (folder->contacts) return (NMContact *) g_slist_nth_data(folder->contacts, index); else return NULL;}const char *nm_folder_get_name(NMFolder * folder){ if (folder == NULL) return NULL; return folder->name;}voidnm_folder_set_name(NMFolder * folder, const char *name){ if (folder == NULL || name == NULL) return; if (folder->name) g_free(folder->name); folder->name = g_strdup(name);}intnm_folder_get_id(NMFolder * folder){ if (folder == NULL) { return -1; } return folder->id;}voidnm_folder_add_folder_to_list(NMFolder * root, NMFolder * folder){ GSList *node; if (root == NULL || folder == NULL) return; node = root->folders; while (node) { if (folder->seq <= ((NMFolder *) node->data)->seq) { nm_folder_add_ref(folder); root->folders = g_slist_insert_before(root->folders, node, folder); break; } node = g_slist_next(node); } if (node == NULL) { nm_folder_add_ref(folder); root->folders = g_slist_append(root->folders, folder); }}voidnm_folder_remove_contact(NMFolder * folder, NMContact * contact){ GSList *node; if (folder == NULL || contact == NULL) return; node = folder->contacts; while (node) { if (contact->id == ((NMContact *) (node->data))->id) { folder->contacts = g_slist_remove(folder->contacts, node->data); nm_release_contact(contact); break; } node = g_slist_next(node); }}voidnm_folder_add_contact_to_list(NMFolder * root_folder, NMContact * contact){ GSList *node = NULL; NMFolder *folder = root_folder; if (folder == NULL || contact == NULL) return; /* Find folder to add contact to */ if (contact->parent_id != 0) { node = folder->folders; while (node) { folder = (NMFolder *) node->data; if (contact->parent_id == folder->id) { break; } folder = NULL; node = g_slist_next(node); } } /* Add contact to list */ if (folder) { node = folder->contacts; while (node) { if (contact->seq <= ((NMContact *) (node->data))->seq) { nm_contact_add_ref(contact); folder->contacts = g_slist_insert_before(folder->contacts, node, contact); break; } node = g_slist_next(node); } if (node == NULL) { nm_contact_add_ref(contact); folder->contacts = g_slist_append(folder->contacts, contact); } }}voidnm_folder_add_contacts_and_folders(NMUser * user, NMFolder * root, NMField * fields){ /* Add the contacts and folders from the field array */ if (user && root && fields) { _add_folders(root, fields); _add_contacts(user, root, fields); }}gpointernm_folder_find_item_by_object_id(NMFolder * root_folder, int object_id){ int cnt, cnt2, i, j; gpointer item = NULL; NMFolder *folder; NMContact *contact; if (root_folder == NULL) return NULL; /* 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)) { item = contact; break; } } /* If we haven't found the item yet, check the subfolders */ if (item == NULL) { 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)) { item = folder; break; } /* 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)) { item = contact; break; } } } } return item;}NMContact *nm_folder_find_contact_by_userid(NMFolder * folder, const char *userid){ int cnt, i; NMContact *tmp, *contact = NULL; if (folder == NULL || userid == NULL) return 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)) { contact = tmp; break; } } return contact;}NMContact *nm_folder_find_contact_by_display_id(NMFolder * folder, const char *display_id){ int cnt, i; NMContact *tmp, *contact = NULL; if (folder == NULL || display_id == NULL) return 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)) { contact = tmp; break; } } return contact;}NMContact *nm_folder_find_contact(NMFolder * folder, const char *dn){ int cnt, i; NMContact *tmp, *contact = NULL; if (folder == NULL || dn == NULL) return 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)) { contact = tmp; break; } } return contact;}/********************************************************************* * Utility functions *********************************************************************/static void_release_folder_contacts(NMFolder * folder){ GSList *cnode; NMContact *contact; for (cnode = folder->contacts; cnode; cnode = cnode->next) { contact = cnode->data; cnode->data = NULL; nm_release_contact(contact); } g_slist_free(folder->contacts); folder->contacts = NULL;}static void_release_folder_folders(NMFolder * folder){ GSList *fnode; NMFolder *subfolder; if (folder == NULL) return; for (fnode = folder->folders; fnode; fnode = fnode->next) { subfolder = fnode->data; fnode->data = NULL; nm_release_folder(subfolder); } g_slist_free(folder->folders); folder->folders = NULL;}static void_add_folders(NMFolder * root, NMField * fields){ NMFolder *folder = NULL; NMField *locate = NULL; locate = nm_locate_field(NM_A_FA_FOLDER, fields); while (locate != NULL) { /* 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); }}static void_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); while (locate != NULL) { /* 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); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -