1 /* $XConsortium: entry.c /main/1 1996/04/21 19:23:10 drk $ */
3 * (c) Copyright 1993, 1994 Hewlett-Packard Company
4 * (c) Copyright 1993, 1994 International Business Machines Corp.
5 * (c) Copyright 1993, 1994 Novell, Inc.
6 * (c) Copyright 1993, 1994 Sun Microsystems, Inc.
10 * Functions that manage the entry data structures.
13 #include <EUSCompat.h>
21 #include "convert4-5.h"
22 #include "convert5-4.h"
29 #include "updateattrs.h"
31 /******************************************************************************
32 * forward declaration of static functions used within the file
33 ******************************************************************************/
34 static CSA_return_code _CmsentryToLibentry(_DtCmNameTable **tbl, cms_entry *e,
35 _DtCm_libentry **entry_r);
37 /*****************************************************************************
38 * extern functions used in the library
39 *****************************************************************************/
42 * Given the entry handle, return the internal entry data structure.
44 extern _DtCm_libentry *
45 _DtCm_get_libentry(CSA_entry_handle entryhandle)
47 _DtCm_libentry *entry = (_DtCm_libentry *)entryhandle;
49 if (entry == NULL || entry->handle != (void *)entry)
55 extern CSA_return_code
56 _DtCm_make_libentry(cms_entry *e, _DtCm_libentry **entry_r)
59 CSA_return_code stat = CSA_SUCCESS;
62 return (CSA_E_INVALID_PARAMETER);
64 if ((ptr = (_DtCm_libentry *)calloc(1, sizeof(_DtCm_libentry)))
66 return (CSA_E_INSUFFICIENT_MEMORY);
69 if ((ptr->e = _DtCm_make_cms_entry(_DtCm_entry_name_tbl))
71 stat = CSA_E_INSUFFICIENT_MEMORY;
73 stat = _DtCm_copy_cms_entry(e, &ptr->e);
75 if (stat == CSA_SUCCESS) {
76 ptr->handle = (void *)ptr;
85 * Get entry data from the server.
87 extern CSA_return_code
88 _DtCm_get_entry_detail(_DtCm_libentry *entry)
90 CSA_return_code stat = CSA_SUCCESS;
92 if (entry->filled == B_FALSE)
93 return (_DtCm_rpc_lookup_entry_by_id(entry->cal, entry));
99 * return attribute names of all attributes.
100 * ** how to deal with
101 * - predefined attributes with null values
102 * - custom attributes
104 extern CSA_return_code
105 _DtCm_get_entry_attr_names(
106 _DtCm_libentry *entry,
107 CSA_uint32 *num_names_r,
116 if ((names = _DtCm_alloc_character_pointers(entry->e->num_attrs))
118 return (CSA_E_INSUFFICIENT_MEMORY);
120 /* first element in attr array is not used */
121 for (i = 1, j = 0; i <= entry->e->num_attrs; i++) {
122 /* there should not be any NULL names in the attr array */
123 if (entry->e->attrs[i].value != NULL) {
124 if ((names[j] = strdup(entry->e->attrs[i].name.name))
127 return (CSA_E_INSUFFICIENT_MEMORY);
136 return (CSA_SUCCESS);
139 extern CSA_return_code
140 _DtCm_get_all_entry_attrs(
141 _DtCm_libentry *entry,
142 CSA_uint32 *num_attrs,
143 CSA_attribute **attrs)
146 CSA_return_code stat = CSA_SUCCESS;
147 CSA_attribute *attrs_r;
149 if (num_attrs == NULL || attrs == NULL)
150 return (CSA_E_INVALID_PARAMETER);
152 if ((attrs_r = _DtCm_alloc_attributes(entry->e->num_attrs)) == NULL)
153 return (CSA_E_INSUFFICIENT_MEMORY);
155 /* first element in attr array is not used */
156 for (i = 1, j = 0; i <= entry->e->num_attrs; i++) {
157 if (entry->e->attrs[i].value != NULL) {
159 if ((stat = _DtCm_cms2csa_attribute(entry->e->attrs[i],
160 &attrs_r[j])) != CSA_SUCCESS) {
171 return (CSA_SUCCESS);
175 * Search the attribute list for the given attribute names.
176 * If it's not found, the attribute value
179 extern CSA_return_code
180 _DtCm_get_entry_attrs_by_name(
181 _DtCm_libentry *entry,
182 CSA_uint32 num_names,
183 CSA_attribute_reference *names,
184 CSA_uint32 *num_attrs,
185 CSA_attribute **attrs)
188 CSA_return_code stat = CSA_SUCCESS;
189 CSA_attribute *attrs_r;
191 if (num_attrs == NULL || attrs == NULL)
192 return (CSA_E_INVALID_PARAMETER);
194 if ((attrs_r = _DtCm_alloc_attributes(num_names)) == NULL)
195 return (CSA_E_INSUFFICIENT_MEMORY);
198 for (i = 0, j = 0; i < num_names; i++) {
199 if (names[i] != NULL) {
200 index = _DtCm_get_index_from_table(
201 entry->cal->entry_tbl, names[i]);
203 if (index >= 0 && entry->e->attrs[index].value) {
204 if (attrs_r[j].name =
205 strdup(entry->e->attrs[index].name.name))
207 stat = _DtCm_cms2csa_attrval(
208 entry->e->attrs[index].value,
211 stat = CSA_E_INSUFFICIENT_MEMORY;
213 if (stat != CSA_SUCCESS) {
225 return (CSA_SUCCESS);
229 * convert the linked list of entry structures to
230 * an array of entry handles.
232 extern CSA_return_code
233 _DtCm_libentry_to_entryh(
234 _DtCm_libentry *elist,
236 CSA_entry_handle **entries_r)
238 CSA_return_code stat = CSA_SUCCESS;
239 CSA_entry_handle *eh;
243 if (elist == NULL || size == NULL || entries_r == NULL)
244 return (CSA_E_INVALID_PARAMETER);
246 for (i = 0, ptr = elist; ptr != NULL; ptr = ptr->next)
249 if ((eh = _DtCm_alloc_entry_handles(i)) == NULL)
250 return (CSA_E_INSUFFICIENT_MEMORY);
252 for (j = 0; j < i; j++, elist = elist->next)
253 eh[j] = (CSA_entry_handle)elist;
258 return (CSA_SUCCESS);
261 extern CSA_return_code
262 _DtCmCmsentriesToLibentries(
263 _DtCmNameTable **tbl,
265 _DtCm_libentry **libentries)
267 CSA_return_code stat = CSA_SUCCESS;
268 _DtCm_libentry *entry, *head, *prev;
270 if (libentries == NULL)
271 return(CSA_E_INVALID_PARAMETER);
274 while (entries != NULL) {
276 if ((stat = _CmsentryToLibentry(tbl, entries, &entry))
289 entries = entries->next;
292 if (stat != CSA_SUCCESS) {
293 _DtCm_free_libentries(head);
301 extern CSA_return_code
302 _DtCm_appt4_to_libentries(
305 _DtCm_libentry **libentries)
307 CSA_return_code stat = CSA_SUCCESS;
308 _DtCm_libentry *entry, *head, *prev;
310 if (libentries == NULL)
311 return(CSA_E_INVALID_PARAMETER);
314 while (appt4 != NULL) {
316 if ((stat = _DtCm_make_libentry(NULL, &entry)) != CSA_SUCCESS)
319 if ((stat = _DtCm_appt4_to_attrs(calname, appt4,
320 entry->e->num_attrs, entry->e->attrs, B_FALSE))
324 entry->e->key.time = appt4->appt_id.tick;
325 entry->e->key.id = appt4->appt_id.key;
326 entry->filled = B_TRUE;
340 if (stat != CSA_SUCCESS) {
341 _DtCm_free_libentries(head);
349 extern CSA_return_code
350 _DtCm_libentries_to_appt4(_DtCm_libentry *entries, Appt_4 **appt4)
352 CSA_return_code stat = CSA_SUCCESS;
353 Appt_4 *a4, *head, *prev;
356 return(CSA_E_INVALID_PARAMETER);
359 while (entries != NULL) {
361 if ((stat = _DtCm_cms_entry_to_appt4(entries->e, &a4)) != CSA_SUCCESS)
372 entries = entries->next;
375 if (stat != CSA_SUCCESS) {
376 _DtCm_free_appt4(head);
384 extern CSA_return_code
385 _DtCm_reminder4_to_csareminder(
387 CSA_uint32 *num_rems,
388 CSA_reminder_reference **rems)
390 CSA_return_code stat = CSA_SUCCESS;
391 _DtCm_libentry *entry;
392 CSA_reminder_reference *rem_r;
394 Reminder_4 *rptr = r4;
395 char isotime[BUFSIZ];
397 if (num_rems == NULL || rems == NULL)
398 return(CSA_E_INVALID_PARAMETER);
403 return (CSA_SUCCESS);
406 for (count = 0, rptr = r4; rptr != NULL; count++, rptr = rptr->next)
409 if ((rem_r = _DtCm_alloc_reminder_references(count)) == NULL) {
410 return (CSA_E_INSUFFICIENT_MEMORY);
414 while (r4 != NULL && r4->attr.attr != NULL) {
416 (void)_csa_tick_to_iso8601(r4->tick, isotime);
417 if ((rem_r[i].run_time = strdup(isotime)) == NULL) {
418 stat = CSA_E_INSUFFICIENT_MEMORY;
422 if ((rem_r[i].attribute_name = strdup(
423 _DtCm_old_reminder_name_to_name(r4->attr.attr))) == NULL) {
424 stat = CSA_E_INSUFFICIENT_MEMORY;
428 if ((stat = _DtCm_make_libentry(NULL, &entry)) == CSA_SUCCESS) {
429 entry->e->key.id = r4->appt_id.key;
430 entry->e->key.time = r4->appt_id.tick;
431 rem_r[i].entry = (CSA_entry_handle)entry;
439 if (stat == CSA_SUCCESS) {
449 extern CSA_return_code
450 _DtCm_cms2csa_reminder_ref(
451 cms_reminder_ref *cmsrems,
452 CSA_uint32 *num_rems,
453 CSA_reminder_reference **csarems)
455 CSA_return_code stat = CSA_SUCCESS;
456 _DtCm_libentry *entry;
457 CSA_reminder_reference *rem_r;
458 cms_reminder_ref *rptr;
460 char isotime[BUFSIZ];
464 if (num_rems == NULL || csarems == NULL)
465 return(CSA_E_INVALID_PARAMETER);
467 if (cmsrems == NULL) {
470 return (CSA_SUCCESS);
473 for (count = 0, rptr = cmsrems; rptr != NULL; rptr = rptr->next)
476 if ((rem_r = _DtCm_alloc_reminder_references(count)) == NULL) {
477 return (CSA_E_INSUFFICIENT_MEMORY);
481 while (cmsrems != NULL && stat == CSA_SUCCESS) {
483 (void)_csa_tick_to_iso8601(cmsrems->runtime, isotime);
484 if ((rem_r[i].run_time = strdup(isotime)) == NULL) {
485 stat = CSA_E_INSUFFICIENT_MEMORY;
489 if ((rem_r[i].attribute_name = strdup(cmsrems->reminder_name))
491 stat = CSA_E_INSUFFICIENT_MEMORY;
495 if ((stat = _DtCm_make_libentry(NULL, &entry)) == CSA_SUCCESS) {
496 opq.size = strlen(cmsrems->entryid);
497 opq.data = (unsigned char *)cmsrems->entryid;
499 /* put reference id in entry */
500 stat = _DtCm_set_opaque_attrval(&opq,
502 [CSA_ENTRY_ATTR_REFERENCE_IDENTIFIER_I].value);
504 entry->e->key.id = cmsrems->key.id;
505 entry->e->key.time = cmsrems->key.time;
507 rem_r[i].entry = (CSA_entry_handle)entry;
511 cmsrems = cmsrems->next;
515 if (stat == CSA_SUCCESS) {
526 * This routine convert the entry to an entry structure with
527 * a header so that it can be freed with csa_free
528 * Memory occupied by the orginal entry will be destroyed.
530 extern _DtCm_libentry *
531 _DtCm_convert_entry_wheader(_DtCm_libentry *entry)
533 _DtCm_libentry *pentry;
535 if ((pentry = (_DtCm_libentry *)_DtCm_alloc_entry(
536 sizeof(_DtCm_libentry))) == NULL) {
537 _DtCm_free_libentries(entry);
541 pentry->handle = (void *)pentry;
542 pentry->filled = entry->filled;
543 pentry->e = entry->e;
551 * this is invoked from csa_free indirectly
555 _DtCm_free_entry_content(uint dummy, _DtCm_libentry *entry)
557 _DtCm_remove_from_entry_list(entry->cal, (caddr_t)entry, (caddr_t)entry);
558 if (entry->e) _DtCm_free_cms_entry(entry->e);
559 memset((void *)entry, NULL, sizeof(_DtCm_libentry));
563 * free a linked list of entries
564 * It is first removed from the list and then freed.
567 _DtCm_free_libentries_from_list(_DtCm_libentry *head, _DtCm_libentry *tail)
569 if (head == NULL || tail == NULL)
572 _DtCm_remove_from_entry_list(head->cal, (caddr_t)head, (caddr_t)tail);
574 _DtCm_free_libentries(head);
578 * free a linked list of appointments
579 * All the memory pointed to by the entry are freed,
580 * except for the attribute array.
581 * The entry structures are returned to the free list.
584 _DtCm_free_libentries(_DtCm_libentry *entry)
586 _DtCm_libentry *nptr;
588 while (entry != NULL) {
591 if (entry->e) _DtCm_free_cms_entry(entry->e);
592 memset((void *)entry, NULL, sizeof(_DtCm_libentry));
601 * Free the reminder linked list.
602 * The entry objects pointed to by the list are freed as well.
605 _DtCm_free_reminder_references(uint num_rems, CSA_reminder_reference *rems)
607 CSA_reminder_reference *nptr;
608 _DtCm_libentry *entry, *head, *cptr;
612 for (i = 0; i < num_rems; i++) {
614 entry = (_DtCm_libentry *)rems[i].entry;
615 if (entry && entry->handle == (void *)entry) {
618 } else if (cptr->next == entry) {
621 _DtCm_free_libentries_from_list(head, cptr);
626 if (rems[i].run_time)
627 free(rems[i].run_time);
629 if (rems[i].snooze_time)
630 free(rems[i].snooze_time);
632 if (rems[i].attribute_name)
633 free(rems[i].attribute_name);
637 _DtCm_free_libentries_from_list(head, cptr);
641 _DtCm_free_entry_handles(uint num_entries, CSA_entry_handle *entries)
644 _DtCm_libentry *entry, *head, *cptr;
646 DP(("api.c: _DtCm_free_entry_handles\n"));
649 for (i = 0, head = cptr = NULL; i < num_entries; i++) {
651 /* in case it is a bad appointment handle */
652 if ((entry = _DtCm_get_libentry(entries[i])) != NULL) {
654 * rather than freeing one appointment at a time,
655 * check to see if the appointments are linked to
656 * each other and free each consecutive chunk together
661 } else if (cptr->next == entry) {
664 _DtCm_free_libentries_from_list(head, cptr);
670 _DtCm_free_libentries_from_list(head, cptr);
673 /******************************************************************************
674 * static functions used within in the file
675 ******************************************************************************/
677 static CSA_return_code
679 _DtCmNameTable **tbl,
681 _DtCm_libentry **entry_r)
683 _DtCm_libentry *entry;
684 CSA_return_code stat;
686 if ((stat = _DtCm_make_libentry(NULL, &entry)) != CSA_SUCCESS)
689 if ((stat = _DtCmUpdateAttributes(e->num_attrs, e->attrs,
690 &entry->e->num_attrs, &entry->e->attrs, tbl, B_FALSE,
691 NULL, B_FALSE)) != CSA_SUCCESS) {
692 _DtCm_free_libentries(entry);
694 entry->e->key = e->key;
695 entry->filled = B_TRUE;