Initial import of the CDE 2.1.30 sources from the Open Group.
[oweals/cde.git] / cde / lib / csa / convert5-4.c
1 /* $XConsortium: convert5-4.c /main/2 1996/10/07 15:57:11 drk $ */
2 /*
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.
7  */
8
9 #include <EUSCompat.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
13 #include "convert4-5.h"
14 #include "convert5-4.h"
15 #include "attr.h"
16 #include "appt4.h"
17 #include "iso8601.h"
18
19 /*
20  * Routines to convert data types used in version 5 to version 4.
21  */
22
23 /*****************************************************************************
24  * extern functions
25  *****************************************************************************/
26
27 /*
28  * attributes are checked before calling this routine
29  * so all attributes should be supported in v4
30  */
31 extern CSA_return_code
32 _DtCm_cmsattrs_to_apptdata(uint size, cms_attribute *attrs, Appt_4 *appt)
33 {
34         CSA_return_code stat = CSA_SUCCESS;
35         char            *string, *ptr;
36         time_t          endtick;
37         cms_attribute   *aptr;
38         char            buf[BUFSIZ];
39
40         /*
41          * this routine is called to convert valid v2 to v4 attributes
42          * so no checking of CSA_E_INVALID_ATTRIBUTE and
43          * CSA_E_UNSUPPORTED_ATTRIBUTE is necessary
44          */
45
46         /* assuming all appt data is available */
47
48         /* start time */
49         if (_csa_iso8601_to_tick(
50             attrs[CSA_ENTRY_ATTR_START_DATE_I].value->item.date_time_value,
51             &appt->appt_id.tick))
52                 return (CSA_E_INVALID_DATE_TIME);
53
54         /* duration */
55         aptr = &attrs[CSA_ENTRY_ATTR_END_DATE_I];
56         if (aptr->value && aptr->value->item.date_time_value &&
57                 *(aptr->value->item.date_time_value))
58         {
59                 if (_csa_iso8601_to_tick(aptr->value->item.date_time_value,
60                     &endtick))
61                         return (CSA_E_INVALID_DATE_TIME);
62                 else
63                         appt->duration = endtick - appt->appt_id.tick;
64         }
65
66         /* id */
67         aptr = &attrs[CSA_ENTRY_ATTR_REFERENCE_IDENTIFIER_I];
68         if (aptr->value && aptr->value->item.opaque_data_value &&
69                 aptr->value->item.opaque_data_value->size > 0)
70         {
71                 strncpy(buf, (char *)aptr->value->item.opaque_data_value->data,
72                         aptr->value->item.opaque_data_value->size);
73                 buf[aptr->value->item.opaque_data_value->size] = NULL;
74
75                 ptr = strchr(buf, ':');
76                 if (ptr != NULL)
77                         *ptr = NULL;
78
79                 appt->appt_id.key = atol(buf);
80
81         } else
82                 return (CSA_E_INVALID_ATTRIBUTE_VALUE);
83
84         /* _DtCm_old_attr_type */
85         if ((stat = _DtCm_attrs_to_eventtype4(
86             attrs[CSA_ENTRY_ATTR_TYPE_I].value,
87             attrs[CSA_ENTRY_ATTR_SUBTYPE_I].value, appt->tag)) != CSA_SUCCESS)
88                 return (stat);
89
90         /* _DtCm_old_attr_what */
91         aptr = &attrs[CSA_ENTRY_ATTR_SUMMARY_I];
92         if (aptr->value->item.string_value) {
93                 if ((string = strdup(aptr->value->item.string_value)) == NULL)
94                         return (CSA_E_INSUFFICIENT_MEMORY);
95                 else {
96                         if (appt->what)
97                                 free(appt->what);
98                         appt->what = string;
99                 }
100         }
101
102         /* _DtCm_old_attr_author */
103         aptr = &attrs[CSA_ENTRY_ATTR_ORGANIZER_I];
104         if (aptr->value && aptr->value->item.calendar_user_value) {
105                 if ((string = strdup(aptr->value->item.calendar_user_value))
106                     == NULL)
107                         return (CSA_E_INSUFFICIENT_MEMORY);
108                 else {
109                         if (appt->author)
110                                 free(appt->author);
111                         appt->author = string;
112                 }
113         }
114
115         /* _DtCm_old_attr_beep_reminder */
116         aptr = &attrs[CSA_ENTRY_ATTR_AUDIO_REMINDER_I];
117         if (aptr->value && aptr->value->item.reminder_value) {
118                 if ((stat = _DtCm_add_reminder(_DtCM_OLD_ATTR_BEEP_REMINDER,
119                     aptr->value->item.reminder_value, appt)) != CSA_SUCCESS)
120                         return (stat);
121         }
122
123         /* _DtCm_old_attr_flash_reminder */
124         aptr = &attrs[CSA_ENTRY_ATTR_FLASHING_REMINDER_I];
125         if (aptr->value && aptr->value->item.reminder_value) {
126                 if ((stat = _DtCm_add_reminder(_DtCM_OLD_ATTR_FLASH_REMINDER,
127                     aptr->value->item.reminder_value, appt)) != CSA_SUCCESS)
128                         return (stat);
129         }
130
131         /* _DtCm_old_attr_mail_reminder */
132         aptr = &attrs[CSA_ENTRY_ATTR_MAIL_REMINDER_I];
133         if (aptr->value && aptr->value->item.reminder_value) {
134                 if ((stat = _DtCm_add_reminder(_DtCM_OLD_ATTR_MAIL_REMINDER,
135                     aptr->value->item.reminder_value, appt)) != CSA_SUCCESS)
136                         return (stat);
137         }
138
139         /* _DtCm_old_attr_popup_reminder */
140         aptr = &attrs[CSA_ENTRY_ATTR_POPUP_REMINDER_I];
141         if (aptr->value && aptr->value->item.reminder_value) {
142                 if ((stat = _DtCm_add_reminder(_DtCM_OLD_ATTR_POPUP_REMINDER,
143                     aptr->value->item.reminder_value, appt)) != CSA_SUCCESS)
144                         return (stat);
145         }
146
147         /* _DtCm_old_attr_repeat_type */
148         if (_DtCm_rtype_to_interval4(
149             attrs[CSA_X_DT_ENTRY_ATTR_REPEAT_TYPE_I].value->item.sint32_value,
150             &appt->period.period))
151                 return (CSA_E_INVALID_ATTRIBUTE_VALUE);
152
153         /* _DtCm_old_attr_repeat_times */
154         aptr = &attrs[CSA_X_DT_ENTRY_ATTR_REPEAT_TIMES_I];
155         if (aptr->value)
156                 appt->ntimes = aptr->value->item.uint32_value;
157
158         if (appt->period.period != single_4 &&
159             appt->ntimes == CSA_X_DT_DT_REPEAT_FOREVER) {
160                 appt->ntimes = _DtCM_OLD_REPEAT_FOREVER;
161         }
162
163         /* _DtCm_old_attr_showtime */
164         aptr = &attrs[CSA_X_DT_ENTRY_ATTR_SHOWTIME_I];
165         if (aptr->value)
166                 appt->tag->showtime = aptr->value->item.sint32_value;
167
168         /* _DtCm_old_attr_status */
169         if (_DtCm_status_to_apptstatus4(
170             attrs[CSA_ENTRY_ATTR_STATUS_I].value->item.uint32_value,
171             &appt->appt_status))
172                 return (CSA_E_INVALID_ATTRIBUTE_VALUE);
173
174         /* _DtCm_old_attr_privacy */
175         if (_DtCm_classification_to_privacy4(
176             attrs[CSA_ENTRY_ATTR_CLASSIFICATION_I].value->item.uint32_value,
177             &appt->privacy))
178                 return (CSA_E_INVALID_ATTRIBUTE_VALUE);
179
180         /* _DtCm_old_attr_end_date */
181         aptr = &attrs[CSA_X_DT_ENTRY_ATTR_SEQUENCE_END_DATE_I];
182         if (aptr->value && aptr->value->item.date_time_value &&
183                 *(aptr->value->item.date_time_value))
184         {
185                 if (_csa_iso8601_to_tick(aptr->value->item.date_time_value,
186                     &appt->period.enddate))
187                         return (CSA_E_INVALID_DATE_TIME);
188         }
189
190         switch (appt->period.period) {
191         case nthWeekday_4:
192                 /* _DtCm_old_attr_repeat_nth_weeknum */
193                 aptr = &attrs[CSA_X_DT_ENTRY_ATTR_REPEAT_OCCURRENCE_NUM_I];
194                 if (aptr->value)
195                         appt->period.nth = aptr->value->item.sint32_value;
196                 break;
197         case everyNthDay_4:
198         case everyNthWeek_4:
199         case everyNthMonth_4:
200                 /* _DtCm_old_attr_repeat_nth_interval */
201                 aptr = &attrs[CSA_X_DT_ENTRY_ATTR_REPEAT_INTERVAL_I];
202                 if (aptr->value)
203                         appt->period.nth = aptr->value->item.uint32_value;
204                 break;
205         default:
206                 appt->period.nth = 0;
207         }
208
209         return (stat);
210 }
211
212 /*
213  * ** need to check whether value is valid
214  */
215 extern CSA_return_code
216 _DtCm_attrs_to_apptdata(uint size, CSA_attribute *attrs, Appt_4 *appt)
217 {
218         CSA_return_code stat = CSA_SUCCESS;
219         _DtCm_old_attrs oldattr;
220         char            *string, *ptr;
221         int             interval = 0, week_num = 0, i;
222         boolean_t       set_interval = B_FALSE, set_weeknum = B_FALSE;
223         time_t          endtick = -1;
224         int             tindex = 0, stindex = 0;
225         CSA_attribute_value     otype;
226
227         for (i = 0; i < size && stat == CSA_SUCCESS; i++) {
228
229                 if (attrs[i].name == NULL)
230                         continue;
231
232                 if ((stat = _DtCm_get_old_attr_by_name(attrs[i].name, &oldattr))
233                     != CSA_SUCCESS)
234                         break;
235
236                 switch (oldattr) {
237                 case _DtCm_old_attr_id:
238                         stat = CSA_E_READONLY;
239                         break;
240
241                 case _DtCm_old_attr_time:
242                         if (attrs[i].value == NULL ||
243                             attrs[i].value->item.date_time_value == NULL ||
244                             _csa_iso8601_to_tick(
245                             attrs[i].value->item.date_time_value,
246                             &appt->appt_id.tick))
247                                 stat = CSA_E_INVALID_DATE_TIME;
248                         break;
249                 case _DtCm_old_attr_type:
250                         if (attrs[i].value == NULL)
251                                 stat = CSA_E_INVALID_ATTRIBUTE_VALUE;
252                         else
253                                 tindex = i;
254                         break;
255                 case _DtCm_old_attr_type2:
256                         stindex = i;
257                         break;
258                 case _DtCm_old_attr_duration:
259                         if (attrs[i].value == NULL)
260                                 appt->duration = 0;
261                         else if (attrs[i].value->item.date_time_value
262                             == NULL || _csa_iso8601_to_tick(
263                             attrs[i].value->item.date_time_value,
264                             &endtick))
265                                 stat = CSA_E_INVALID_DATE_TIME;
266                         break;
267                 case _DtCm_old_attr_what:
268                         if (attrs[i].value && attrs[i].value->item.string_value)
269                         {
270                                 if ((string = strdup(
271                                     attrs[i].value->item.string_value))
272                                     == NULL)
273                                         stat = CSA_E_INSUFFICIENT_MEMORY;
274                                 else {
275                                         if (appt->what)
276                                                 free(appt->what);
277                                         appt->what = string;
278                                 }
279                         }
280                         break;
281                 case _DtCm_old_attr_author:
282                         stat = CSA_E_READONLY;
283                         break;
284
285                 case _DtCm_old_attr_beep_reminder:
286                         if (attrs[i].value == NULL)
287                                 stat = _DtCm_remove_reminder(
288                                         _DtCM_OLD_ATTR_BEEP_REMINDER, appt);
289                         else if (attrs[i].value->item.reminder_value == NULL)
290                                 stat = CSA_E_INVALID_ATTRIBUTE_VALUE;
291                         else
292                                 stat = _DtCm_add_reminder(
293                                         _DtCM_OLD_ATTR_BEEP_REMINDER,
294                                         attrs[i].value->item.reminder_value,
295                                         appt);
296                         break;
297
298                 case _DtCm_old_attr_flash_reminder:
299                         if (attrs[i].value == NULL)
300                                 stat = _DtCm_remove_reminder(
301                                         _DtCM_OLD_ATTR_FLASH_REMINDER, appt);
302                         else if (attrs[i].value->item.reminder_value == NULL)
303                                 stat = CSA_E_INVALID_ATTRIBUTE_VALUE;
304                         else
305                                 stat = _DtCm_add_reminder(
306                                         _DtCM_OLD_ATTR_FLASH_REMINDER,
307                                         attrs[i].value->item.reminder_value,
308                                         appt);
309                         break;
310
311                 case _DtCm_old_attr_mail_reminder:
312                         if (attrs[i].value == NULL)
313                                 stat = _DtCm_remove_reminder(
314                                         _DtCM_OLD_ATTR_MAIL_REMINDER, appt);
315                         else if (attrs[i].value->item.reminder_value == NULL)
316                                 stat = CSA_E_INVALID_ATTRIBUTE_VALUE;
317                         else
318                                 stat = _DtCm_add_reminder(
319                                         _DtCM_OLD_ATTR_MAIL_REMINDER,
320                                         attrs[i].value->item.reminder_value,
321                                         appt);
322                         break;
323
324                 case _DtCm_old_attr_popup_reminder:
325                         if (attrs[i].value == NULL)
326                                 stat = _DtCm_remove_reminder(
327                                         _DtCM_OLD_ATTR_POPUP_REMINDER, appt);
328                         else if (attrs[i].value->item.reminder_value == NULL)
329                                 stat = CSA_E_INVALID_ATTRIBUTE_VALUE;
330                         else
331                                 stat = _DtCm_add_reminder(
332                                         _DtCM_OLD_ATTR_POPUP_REMINDER,
333                                         attrs[i].value->item.reminder_value,
334                                         appt);
335                         break;
336
337                 case _DtCm_old_attr_repeat_type:
338                         if (attrs[i].value == NULL)
339                                 appt->period.period = single_4;
340                         else if (_DtCm_rtype_to_interval4(
341                             attrs[i].value->item.sint32_value,
342                             &appt->period.period))
343                                 stat = CSA_E_INVALID_ATTRIBUTE_VALUE;
344                         break;
345                 case _DtCm_old_attr_repeat_times:
346                         if (attrs[i].value == NULL)
347                                 appt->ntimes = 1;
348                         else 
349                                 appt->ntimes =
350                                         attrs[i].value->item.uint32_value;
351                         break;
352                 case _DtCm_old_attr_showtime:
353                         if (attrs[i].value == NULL)
354                                 stat = CSA_E_INVALID_ATTRIBUTE_VALUE;
355                         else
356                                 appt->tag->showtime =
357                                         attrs[i].value->item.sint32_value;
358                         break;
359                 case _DtCm_old_attr_status:
360                         if (attrs[i].value == NULL)
361                                 stat = CSA_E_INVALID_ATTRIBUTE_VALUE;
362                         else if (_DtCm_status_to_apptstatus4(
363                             attrs[i].value->item.sint32_value,
364                             &appt->appt_status))
365                                 stat = CSA_E_INVALID_ATTRIBUTE_VALUE;
366                         break;
367                 case _DtCm_old_attr_privacy:
368                         if (attrs[i].value == NULL)
369                                 stat = CSA_E_INVALID_ATTRIBUTE_VALUE;
370                         else if (_DtCm_classification_to_privacy4(
371                             attrs[i].value->item.sint32_value,
372                             &appt->privacy))
373                                 stat = CSA_E_INVALID_ATTRIBUTE_VALUE;
374                         break;
375                 case _DtCm_old_attr_repeat_nth_interval:
376                         if (attrs[i].value != NULL) {
377                                 set_interval = B_TRUE;
378                                 interval = attrs[i].value->item.uint32_value;
379                         }
380                         break;
381                 case _DtCm_old_attr_repeat_nth_weeknum:
382                         if (attrs[i].value != NULL) {
383                                 set_weeknum = B_TRUE;
384                                 week_num = attrs[i].value->item.sint32_value;
385                         }
386                         break;
387                 case _DtCm_old_attr_end_date:
388                         if (attrs[i].value == NULL)
389                                 appt->period.enddate = 0;
390                         else if (attrs[i].value->item.date_time_value == NULL ||
391                             _csa_iso8601_to_tick(
392                             attrs[i].value->item.date_time_value,
393                             &appt->period.enddate))
394                                 stat = CSA_E_INVALID_DATE_TIME;
395                         break;
396                 default:
397                         stat = CSA_E_INVALID_PARAMETER;
398                 }
399         }
400
401         if (stat == CSA_SUCCESS) {
402
403                 if (tindex || stindex) {
404                         if (tindex == 0) {
405                                 otype.type = CSA_VALUE_SINT32;
406                                 switch (appt->tag->tag) {
407                                 case appointment_4:
408                                 case holiday_4:
409                                         otype.item.sint32_value =
410                                                 CSA_TYPE_EVENT;
411                                         break;
412                                 case toDo_4:
413                                         otype.item.sint32_value = CSA_TYPE_TODO;
414                                         break;
415                                 case reminder_4:
416                                 case otherTag_4:
417                                         otype.item.sint32_value =
418                                                 CSA_X_DT_TYPE_OTHER; 
419                                 }
420                         }
421
422                         stat = _DtCm_attrs_to_eventtype4(
423                                 (cms_attribute_value *)
424                                 (tindex ? attrs[tindex].value : &otype),
425                                 (cms_attribute_value *)
426                                 (stindex ? attrs[stindex].value : NULL),
427                                 appt->tag);
428                 }
429
430                 if (stat == CSA_SUCCESS) {
431                         if (endtick >= 0) {
432                                 appt->duration = endtick - appt->appt_id.tick;
433                                 /* make sure duration is positive */
434                                 if (appt->duration < 0)
435                                         stat = CSA_E_INVALID_ATTRIBUTE_VALUE;
436                         }
437
438                         if (appt->period.period != single_4 &&
439                             appt->ntimes == CSA_X_DT_DT_REPEAT_FOREVER) {
440                                 appt->ntimes = _DtCM_OLD_REPEAT_FOREVER;
441                         }
442
443                         switch (appt->period.period) {
444                         case nthWeekday_4:
445                                 if (set_weeknum == B_TRUE)
446                                         appt->period.nth = week_num;
447                                 break;
448                         case everyNthDay_4:
449                         case everyNthWeek_4:
450                         case everyNthMonth_4:
451                                 if (set_interval == B_TRUE)
452                                         appt->period.nth = interval;
453                                 break;
454                         default:
455                                 appt->period.nth = 0;
456                         }
457                 }
458         }
459
460         return (stat);
461 }
462
463 extern CSA_return_code
464 _DtCm_cms_entry_to_appt4(cms_entry *entry, Appt_4 **appt4_r)
465 {
466         CSA_return_code stat;
467         Appt_4          *appt;
468
469         if (entry == NULL || appt4_r == NULL)
470                 return (CSA_E_INVALID_PARAMETER);
471
472         if ((appt = _DtCm_make_appt4(B_TRUE)) == NULL)
473                 return (CSA_E_INSUFFICIENT_MEMORY);
474
475         if ((stat = _DtCm_cmsattrs_to_apptdata(entry->num_attrs, entry->attrs,
476             appt)) != CSA_SUCCESS) {
477                 _DtCm_free_appt4(appt);
478                 return (stat);
479         } else {
480                 *appt4_r = appt;
481                 return (CSA_SUCCESS);
482         }
483 }
484
485 extern CSA_return_code
486 _DtCm_attrs_to_appt4(uint num_attrs, CSA_attribute *attrs, Appt_4 **appt4_r)
487 {
488         CSA_return_code stat;
489         Appt_4          *appt;
490
491         if (appt4_r == NULL)
492                 return (CSA_E_INVALID_PARAMETER);
493
494         if ((appt = _DtCm_make_appt4(B_TRUE)) == NULL)
495                 return (CSA_E_INSUFFICIENT_MEMORY);
496
497         if ((stat = _DtCm_attrs_to_apptdata(num_attrs, attrs, appt)) != CSA_SUCCESS) {
498                 _DtCm_free_appt4(appt);
499                 return (stat);
500         } else {
501                 *appt4_r = appt;
502                 return (CSA_SUCCESS);
503         }
504 }
505
506 extern CSA_return_code
507 _DtCm_scope_to_options4(CSA_enum scope, Options_4 *opt)
508 {
509         if (opt == NULL)
510                 return (CSA_E_INVALID_PARAMETER);
511
512         switch (scope) {
513         case CSA_SCOPE_ONE:
514                 *opt = do_one_4;
515                 break;
516         case CSA_SCOPE_ALL:
517                 *opt = do_all_4;
518                 break;
519         case CSA_SCOPE_FORWARD:
520                 *opt = do_forward_4;
521                 break;
522         default:
523                 return (CSA_E_INVALID_ENUM);
524         }
525
526         return (CSA_SUCCESS);
527 }
528
529 extern CSA_return_code
530 _DtCm_csaaccesslist_toaccessentry4(CSA_access_list alist, Access_Entry_4 **a4)
531 {
532         CSA_return_code stat = CSA_SUCCESS;
533         Access_Entry_4  *to, *prev, *head;
534
535         head = prev = NULL;
536         while (alist != NULL) {
537                 if ((to = (Access_Entry_4 *)calloc(1, sizeof(Access_Entry_4)))
538                     == NULL) {
539                         stat = CSA_E_INSUFFICIENT_MEMORY;
540                         break;
541                 }
542
543                 if ((to->access_type = _DtCmAccessRightToV4AccessType(
544                     alist->rights)) < 0) {
545                         stat = CSA_E_INVALID_FLAG;
546                         break;
547                 }
548
549                 if (alist->user == NULL || alist->user->user_name == NULL) {
550                         stat = CSA_E_INVALID_ATTRIBUTE_VALUE;
551                         break;
552                 }
553
554                 if ((to->who = strdup(alist->user->user_name)) == NULL) {
555                         free(to);
556                         stat = CSA_E_INSUFFICIENT_MEMORY;
557                         break;
558                 }
559
560                 if (head == NULL)
561                         head = to;
562                 else
563                         prev->next = to;
564
565                 prev = to;
566
567                 alist = alist->next;
568         }
569
570         if (stat != CSA_SUCCESS) {
571                 _DtCm_free_access_list4(head);
572                 head = NULL;
573         }
574
575         *a4 = head;
576         return (stat);
577 }
578
579 extern int
580 _DtCm_rtype_to_interval4(int val, Interval_4 *period)
581 {
582         switch (val) {
583         case CSA_X_DT_REPEAT_ONETIME:
584                 *period = single_4;
585                 break;
586         case CSA_X_DT_REPEAT_WEEKLY:
587                 *period = weekly_4;
588                 break;
589         case CSA_X_DT_REPEAT_DAILY:
590                 *period = daily_4;
591                 break;
592         case CSA_X_DT_REPEAT_BIWEEKLY:
593                 *period = biweekly_4;
594                 break;
595         case CSA_X_DT_REPEAT_MONTHLY_BY_DATE:
596                 *period = monthly_4;
597                 break;
598         case CSA_X_DT_REPEAT_YEARLY:
599                 *period = yearly_4;
600                 break;
601         case CSA_X_DT_REPEAT_MONTHLY_BY_WEEKDAY:
602                 *period = nthWeekday_4;
603                 break;
604         case CSA_X_DT_REPEAT_EVERY_NDAY:
605                 *period = everyNthDay_4;
606                 break;
607         case CSA_X_DT_REPEAT_EVERY_NWEEK:
608                 *period = everyNthWeek_4;
609                 break;
610         case CSA_X_DT_REPEAT_EVERY_NMONTH:
611                 *period = everyNthMonth_4;
612                 break;
613         case CSA_X_DT_REPEAT_MON_TO_FRI:
614                 *period = monThruFri_4;
615                 break;
616         case CSA_X_DT_REPEAT_MONWEDFRI:
617                 *period = monWedFri_4;
618                 break;
619         case CSA_X_DT_REPEAT_TUETHUR:
620                 *period = tueThur_4;
621                 break;
622         case CSA_X_DT_REPEAT_WEEKDAYCOMBO:
623                 *period = daysOfWeek_4;
624                 break;
625         case CSA_X_DT_REPEAT_OTHER:
626                 *period = otherPeriod_4;
627                 break;
628         default:
629                 return (-1);
630         }
631         return (0);
632 }
633
634 extern int
635 _DtCm_status_to_apptstatus4(int val, Appt_Status_4 *astat)
636 {
637         switch (val) {
638         case CSA_X_DT_STATUS_ACTIVE:
639                 *astat = active_4;
640                 break;
641         case CSA_STATUS_COMPLETED:
642                 *astat = completed_4;
643                 break;
644         case CSA_X_DT_STATUS_CANCELLED:
645                 *astat = cancelled_4;
646                 break;
647         case CSA_X_DT_STATUS_COMMITTED:
648                 *astat = committed_4;
649                 break;
650         case CSA_X_DT_STATUS_ADD_PENDING:
651                 *astat = pendingAdd_4;
652                 break;
653         case CSA_X_DT_STATUS_DELETE_PENDING:
654                 *astat = pendingDelete_4;
655                 break;
656         default:
657                 return (-1);
658         }
659
660         return (0);
661 }
662
663 extern int
664 _DtCm_classification_to_privacy4(int val, Privacy_Level_4 *privacy)
665 {
666         switch (val) {
667         case CSA_CLASS_PUBLIC:
668                 *privacy = public_4;
669                 break;
670         case CSA_CLASS_CONFIDENTIAL:
671                 *privacy = semiprivate_4;
672                 break;
673         case CSA_CLASS_PRIVATE:
674                 *privacy = private_4;
675                 break;
676         default:
677                 return (-1);
678         }
679
680         return (0);
681 }
682
683 extern CSA_return_code
684 _DtCm_attrs_to_eventtype4(
685         cms_attribute_value     *type,
686         cms_attribute_value     *stype,
687         Tag_4                   *tag)
688 {
689         CSA_return_code stat = CSA_SUCCESS;
690
691         if (type) {
692                 switch (type->item.uint32_value) {
693                 case CSA_TYPE_EVENT:
694
695                         if (stype && stype->item.string_value) {
696                                 if (strcmp(stype->item.string_value,
697                                     CSA_SUBTYPE_APPOINTMENT) == 0)
698                                         tag->tag = appointment_4;
699                                 else if (strcmp(stype->item.string_value,
700                                     CSA_SUBTYPE_HOLIDAY) == 0)
701                                         tag->tag = holiday_4;
702                                 else
703                                         stat = CSA_E_INVALID_ATTRIBUTE_VALUE;
704                         } else
705                                 stat = CSA_E_INVALID_ATTRIBUTE_VALUE;
706                         break;
707
708                 case CSA_TYPE_TODO:
709
710                         if (stype)
711                                 stat = CSA_E_INVALID_ATTRIBUTE_VALUE;
712                         else
713                                 tag->tag = toDo_4;
714                         break;
715
716                 case CSA_X_DT_TYPE_OTHER:
717
718                         if (stype)
719                                 stat = CSA_E_INVALID_ATTRIBUTE_VALUE;
720                         else
721                                 tag->tag = otherTag_4;
722                         break;
723
724                 default:
725                         stat = CSA_E_INVALID_ATTRIBUTE_VALUE;
726                 }
727         } else
728                 stat = CSA_E_INVALID_ATTRIBUTE_VALUE;
729
730         return (stat);
731 }
732
733 /*
734  * Get rid of the given reminder in the appt.
735  */
736 extern CSA_return_code
737 _DtCm_remove_reminder(char *rem, Appt_4 *appt)
738 {
739         Attribute_4     *attr, *prev;
740
741         for (attr = prev = appt->attr; attr != NULL; attr = attr->next) { 
742                 if (strcmp(attr->attr, rem) == 0) {
743                         if (prev == attr)
744                                 appt->attr = attr->next;
745                         else
746                                 prev->next = attr->next;
747
748                         attr->next = NULL;
749                         _DtCm_free_attr4(attr);
750                 } else
751                         prev = attr;
752         }
753
754         return (CSA_SUCCESS);
755 }
756
757 /*
758  * If the corresponding attribute exists already, replace it.
759  */
760 extern CSA_return_code
761 _DtCm_add_reminder(char *rem, CSA_reminder * val, Appt_4 *appt)
762 {
763         Attribute_4     *newattr, *attrs, *prev = NULL;
764         char            adv[80];
765         char            *str;
766         time_t          ltime = 0;
767
768 /*
769         if ((val->snooze_time && val->snooze_time[0] != NULL)
770             || val->repeat_count > 1)
771                 return (CSA_E_INVALID_ATTRIBUTE_VALUE);
772 */
773
774         if ((newattr = calloc(1, sizeof(Attribute_4))) == NULL)
775                 return (CSA_E_INSUFFICIENT_MEMORY);
776
777         if ((newattr->attr = strdup(rem)) == NULL) {
778                 free(newattr);
779                 return (CSA_E_INSUFFICIENT_MEMORY);
780         }
781
782         /* convert lead_time to advance value */
783         if (val->lead_time)
784                 _csa_iso8601_to_duration(val->lead_time, &ltime);
785         sprintf(adv, "%d", ltime);
786
787         if ((newattr->value = strdup(adv)) == NULL) {
788                 _DtCm_free_attr4(newattr);
789                 return (CSA_E_INSUFFICIENT_MEMORY);
790         }
791
792         if (val->reminder_data.size > 0) {
793                 if ((newattr->clientdata = malloc(val->reminder_data.size + 1))
794                     == NULL) {
795                         _DtCm_free_attr4(newattr);
796                         return (CSA_E_INSUFFICIENT_MEMORY);
797                 }
798                 memcpy(newattr->clientdata, val->reminder_data.data,
799                         val->reminder_data.size);
800                 newattr->clientdata[val->reminder_data.size] = NULL;
801
802         } else if ((newattr->clientdata = calloc(1,1)) == NULL) {
803                 _DtCm_free_attr4(newattr);
804                 return (CSA_E_INSUFFICIENT_MEMORY);
805         }
806
807         for (attrs = appt->attr; attrs != NULL; attrs = attrs->next) {
808                 if (strcmp(attrs->attr, rem) == 0) {
809
810                         newattr->next = attrs->next;
811                         if (prev == NULL)
812                                 appt->attr = newattr;
813                         else
814                                 prev->next = newattr;
815
816                         attrs->next = NULL;
817                         _DtCm_free_attr4(attrs);
818
819                         break;
820                 } else
821                         prev = attrs;
822         }
823
824         if (attrs == NULL) {
825                 newattr->next = appt->attr;
826                 appt->attr = newattr;
827         }
828
829         return (CSA_SUCCESS);
830 }
831
832 extern int
833 _DtCmAccessRightToV4AccessType(unsigned int access)
834 {
835         int     newaccess = access_none_4;
836
837         if (access < 0 ||
838             access & ~(CSA_X_DT_BROWSE_ACCESS | CSA_X_DT_INSERT_ACCESS |
839             CSA_X_DT_DELETE_ACCESS))
840                 return (-1);
841
842         if (access & CSA_X_DT_BROWSE_ACCESS)
843                 newaccess |= access_read_4;
844         if (access & CSA_X_DT_INSERT_ACCESS)
845                 newaccess |= access_write_4;
846         if (access & CSA_X_DT_DELETE_ACCESS)
847                 newaccess |= access_delete_4;
848
849         return (newaccess);
850 }
851