~geballin/ical-tcl/ical-tcl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
/* Copyright (c) 1993 by Sanjay Ghemawat */
#include <assert.h>
#include <stdlib.h>
#include <string.h>

#include "WeekDay.h"

#include "basic.h"
#include "arrays.h"
#include "cal_tcl.h"
#include "calfile.h"
#include "calendar.h"
#include "ical.h"
#include "item_tcl.h"
#include "dispatch.h"

/*
 * Item* -> Item_Tcl* map.
 */
static Tcl_HashTable itemMap;
static int itemMapInited = 0;

static int check_permission(Tcl_Interp* tcl, Item_Tcl* item);
// effects      If "item" can be modified legally, then mark
//              containing calendar as modified and return true.
//              Else store an error message in "tcl->result"
//              and return false.

static void trigger_item(Tcl_Interp* tcl, Item_Tcl* item,
                         char const* = "change");
// effects      If "item" belongs to a calendar, generate a trigger message
//              for it.  The generate trigger is of type "change" by default,
//              but that can be overridden by providing a different value for
//              the optional last argument.

Item_Tcl::Item_Tcl(Tcl_Interp* tcl, Item* i, CalFile* c)
    : Object(tcl, "Item")
{
    item = i;
    cal = c;

    if (! itemMapInited) {
        itemMapInited = 1;
        Tcl_InitHashTable(&itemMap, TCL_ONE_WORD_KEYS);
    }
    int newentry;
    Tcl_HashEntry* entry = Tcl_CreateHashEntry(&itemMap,(char*)item,&newentry);
    assert(newentry);
    Tcl_SetHashValue(entry, (ClientData)this);
}

Item_Tcl::~Item_Tcl() {
    Tcl_HashEntry* entry = Tcl_FindHashEntry(&itemMap, (char*)item);
    assert((entry != 0) && (Tcl_GetHashValue(entry) == ((ClientData) this)));
    Tcl_DeleteHashEntry(entry);
}

Item_Tcl* Item_Tcl::find(Item* item) {
    Tcl_HashEntry* entry = Tcl_FindHashEntry(&itemMap, (char*)item);
    return ((entry == 0) ? 0 : ((Item_Tcl*) Tcl_GetHashValue(entry)));
}

/*
 * Forward declaration of handler procedures.
 */
static int item_length    (ClientData, Tcl_Interp*, int, const char**);
static int item_starttime (ClientData, Tcl_Interp*, int, const char**);
static int item_timezone  (ClientData, Tcl_Interp*, int, const char**);
static int item_clone     (ClientData, Tcl_Interp*, int, const char**);
static int item_is        (ClientData, Tcl_Interp*, int, const char**);
static int item_delete    (ClientData, Tcl_Interp*, int, const char**);
static int item_cal       (ClientData, Tcl_Interp*, int, const char**);
static int item_text      (ClientData, Tcl_Interp*, int, const char**);
static int item_uid       (ClientData, Tcl_Interp*, int, const char**);
static int item_early     (ClientData, Tcl_Interp*, int, const char**);
static int item_owner     (ClientData, Tcl_Interp*, int, const char**);
static int item_owned     (ClientData, Tcl_Interp*, int, const char**);
static int item_own       (ClientData, Tcl_Interp*, int, const char**);
static int item_hilite    (ClientData, Tcl_Interp*, int, const char**);
static int item_todo      (ClientData, Tcl_Interp*, int, const char**);
static int item_is_done   (ClientData, Tcl_Interp*, int, const char**);
static int item_done      (ClientData, Tcl_Interp*, int, const char**);
static int item_alarms    (ClientData, Tcl_Interp*, int, const char**);
static int item_option    (ClientData, Tcl_Interp*, int, const char**);
static int item_doption   (ClientData, Tcl_Interp*, int, const char**);
static int item_empty     (ClientData, Tcl_Interp*, int, const char**);
static int item_repeat    (ClientData, Tcl_Interp*, int, const char**);
static int item_first     (ClientData, Tcl_Interp*, int, const char**);
static int item_type      (ClientData, Tcl_Interp*, int, const char**);
static int item_desc      (ClientData, Tcl_Interp*, int, const char**);
static int item_cont      (ClientData, Tcl_Interp*, int, const char**);
static int item_next      (ClientData, Tcl_Interp*, int, const char**);
static int item_range     (ClientData, Tcl_Interp*, int, const char**);
static int item_date      (ClientData, Tcl_Interp*, int, const char**);
static int item_start     (ClientData, Tcl_Interp*, int, const char**);
static int item_finish    (ClientData, Tcl_Interp*, int, const char**);
static int item_ondel     (ClientData, Tcl_Interp*, int, const char**);
static int item_dayr      (ClientData, Tcl_Interp*, int, const char**);
static int item_wdays     (ClientData, Tcl_Interp*, int, const char**);

static int item_monthr    (ClientData, Tcl_Interp*, int, const char**);
static int item_mday      (ClientData, Tcl_Interp*, int, const char**);
static int item_mlday     (ClientData, Tcl_Interp*, int, const char**);
static int item_mworkday  (ClientData, Tcl_Interp*, int, const char**);
static int item_mlworkday (ClientData, Tcl_Interp*, int, const char**);
static int item_mweekday  (ClientData, Tcl_Interp*, int, const char**);
static int item_mlweekday (ClientData, Tcl_Interp*, int, const char**);

static Dispatch_Entry item_dispatch[] = {
    { "delete",                 0, 0, item_delete       },
    { "clone",                  0, 0, item_clone        },

    { "length",                 0, 1, item_length       },
    { "starttime",              1, 2, item_starttime    },
    { "timezone",               0, 2, item_timezone     },
    { "alarms",                 0, 1, item_alarms       },
    { "option",                 1, 2, item_option       },
    { "delete_option",          1, 1, item_doption      },

    { "is",                     1, 1, item_is           },
    { "calendar",               0, 0, item_cal          },
    { "text",                   0, 1, item_text         },
    { "uid",                    0, 0, item_uid          },
    { "earlywarning",           0, 1, item_early        },
    { "owner",                  0, 1, item_owner        },
    { "owned",                  0, 0, item_owned        },
    { "own",                    0, 0, item_own          },
    { "hilite",                 0, 1, item_hilite       },
    { "todo",                   0, 1, item_todo         },
    { "is_done",                0, 0, item_is_done      },
    { "done",                   1, 1, item_done         },

    { "contains",               1, 1, item_cont         },
    { "empty",                  0, 0, item_empty        },
    { "repeats",                0, 0, item_repeat       },
    { "first",                  0, 0, item_first        },
    { "next",                   1, 1, item_next         },
    { "range",                  2, 3, item_range        },
    { "type",                   0, 0, item_type         },
    { "describe_repeat",        0, 1, item_desc         },

    { "date",                   1, 1, item_date         },
    { "dayrepeat",              2, 2, item_dayr         },

    { "monthrepeat",            2, 2, item_monthr       },
    { "month_day",              1, 3, item_mday         },
    { "month_last_day",         1, 3, item_mlday        },
    { "month_work_day",         1, 3, item_mworkday     },
    { "month_last_work_day",    1, 3, item_mlworkday    },
    { "month_week_day",         2, 4, item_mweekday     },
    { "month_last_week_day",    2, 4, item_mlweekday    },

    { "weekdays",               0, -1, item_wdays       },
    { "start",                  1,  2, item_start       },
    { "finish",                 1,  2, item_finish      },
    { "deleteon",               1,  1, item_ondel       },

    { 0,                        0, 0, 0                 }
};

int Item_Tcl::method(int argc, const char** argv) {
    return Dispatch(item_dispatch, (ClientData)this, tcl(), argc, argv);
}

/*
 * Handler procedures.
 */

static int item_length(ClientData c, Tcl_Interp* tcl, int argc, const char** argv) {
    Item_Tcl* item = (Item_Tcl*) c;

    Appointment* appt = item->value()->AsAppointment();
    if (appt != 0) {
        if (argc == 0) {
            char buffer[100];
            sprintf(buffer, "%d", appt->GetLength());
            Tcl_SetResult(tcl, buffer, TCL_VOLATILE);
            return TCL_OK;
        }

        int length;
        if ((Tcl_GetInt(tcl, argv[0], &length) != TCL_OK) ||
            (length < 1) ||
            (length > 24*60)) {
            TCL_Error(tcl, "invalid appointment length");
        }
        if (! check_permission(tcl, item)) return TCL_ERROR;

        appt->SetLength(length);
        trigger_item(tcl, item);

        TCL_Return(tcl, "");
    }

    TCL_Error(tcl, "unknown command");
}

static int item_starttime(ClientData c, Tcl_Interp* tcl, int argc, const char** argv) {
    Item_Tcl* item = (Item_Tcl*) c;
    int date;

    Appointment* appt = item->value()->AsAppointment();
    if (appt == 0) {
        TCL_Error(tcl, "unknown command");
    }

    if (strcmp(argv[0], "native") == 0) {
        date = -1;
    } else {
        if (Tcl_GetInt(tcl, argv[0], &date) != TCL_OK || date < 0) {
            TCL_Error(tcl, "invalid starttime's date");
        }
    }

    if (argc == 1) {
        char buffer[100];
        sprintf(buffer, "%d", appt->GetStart(date));
        Tcl_SetResult(tcl, buffer, TCL_VOLATILE);
        return TCL_OK;
    }

    int start;
    if ((Tcl_GetInt(tcl, argv[1], &start) != TCL_OK) ||
        (start < 0) || (start >= (24*60))) {
        TCL_Error(tcl, "invalid appointment start");
    }
    if (! check_permission(tcl, item)) return TCL_ERROR;

    appt->SetStart(date, start);
    trigger_item(tcl, item);

    TCL_Return(tcl, "");
}

static int item_timezone(ClientData c, Tcl_Interp* tcl, int argc, const char** argv) {
    Item_Tcl* item = (Item_Tcl*) c;
    bool convert_remote=false;

    Appointment* appt = item->value()->AsAppointment();
    if (appt == 0) {
        TCL_Error(tcl, "unknown command");
    }

    if (argc == 0) {
        TCL_Return(tcl, (char*) appt->GetTimezone());
    }

    if (argc > 1 && strcmp(argv[0], "-convert") == 0) {
        convert_remote=true;
        argc--;
        argv++;
    }

    if (! check_permission(tcl, item)) return TCL_ERROR;
    appt->SetTimezone(argv[0], convert_remote);
    trigger_item(tcl, item);

    TCL_Return(tcl, "");
}

static int item_clone(ClientData c, Tcl_Interp* tcl, int argc, const char** argv) {
    Item_Tcl* item = (Item_Tcl*) c;
    Item_Tcl* clone = new Item_Tcl(tcl, item->value()->Clone(), 0);
    TCL_Return(tcl, (char*) clone->handle());
}

static int item_is(ClientData c, Tcl_Interp* tcl, int argc, const char** argv) {
    Item_Tcl* item = (Item_Tcl*) c;

    if ((strcmp(argv[0], "note") == 0) && (item->value()->AsNotice() != 0)) {
        TCL_Return(tcl, "1");
    }

    if ((strcmp(argv[0], "appt") == 0) && (item->value()->AsAppointment()!=0)){
        TCL_Return(tcl, "1");
    }

    TCL_Return(tcl, "0");
}

static int item_delete(ClientData c, Tcl_Interp* tcl, int argc, const char** argv) {
    Item_Tcl* item = ((Item_Tcl*) c);
    if (! check_permission(tcl, item)) return TCL_ERROR;

    CalFile* file = item->calendar();
    if (file != 0) {
        item->set_calendar(0);
        file->GetCalendar()->Remove(item->value());
        file->Modified();

        // XXX Only send triggers when deleting an item from a calendar???
        trigger(tcl, "delete", item->handle());
    }

    delete item->value();
    delete item;

    TCL_Return(tcl, "");
}

static int item_cal(ClientData c, Tcl_Interp* tcl, int argc, const char** argv) {
    Item_Tcl* item = (Item_Tcl*) c;

    CalFile* cal = item->calendar();
    if (cal == 0) {
        TCL_Error(tcl, "item not in calendar");
    }
    TCL_Return(tcl, (char*)(cal->GetName()));
}

static int item_text(ClientData c, Tcl_Interp* tcl, int argc, const char** argv) {
    Item_Tcl* item = (Item_Tcl*) c;

    if (argc == 0) {
        TCL_Return(tcl, (char*) item->value()->GetText());
    }

    if (! check_permission(tcl, item)) return TCL_ERROR;
    item->value()->SetText(argv[0]);
    trigger_item(tcl, item, "text");

    TCL_Return(tcl, "");
}

static int item_uid(ClientData c, Tcl_Interp* tcl, int argc, const char** argv) {
    Item_Tcl* item = (Item_Tcl*) c;
    TCL_Return(tcl, (char*) item->value()->GetUid());
}

static int item_early(ClientData c, Tcl_Interp* tcl, int argc, const char** argv) {
    Item_Tcl* item = (Item_Tcl*) c;

    if (argc == 0) {
        char buffer[100];

        sprintf(buffer, "%d", item->value()->GetRemindStart());
        Tcl_SetResult(tcl, buffer, TCL_VOLATILE);
        return TCL_OK;
    }

    int warn;
    if (Tcl_GetInt(tcl, argv[0], &warn) != TCL_OK) {
        TCL_Error(tcl, "invalid early warning option");
    }
    if (! check_permission(tcl, item)) return TCL_ERROR;
    item->value()->SetRemindStart(warn);
    trigger_item(tcl, item);

    TCL_Return(tcl, "");
}

static int item_owner(ClientData c, Tcl_Interp* tcl, int argc, const char** argv) {
    Item_Tcl* item = (Item_Tcl*) c;

    if (argc == 0) {
        TCL_Return(tcl, (char*) item->value()->GetOwner());
    }

    if (! check_permission(tcl, item)) return TCL_ERROR;
    item->value()->SetOwner(argv[0]);
    trigger_item(tcl, item);

    TCL_Return(tcl, "");
}

static int item_owned(ClientData c, Tcl_Interp* tcl, int argc, const char** argv) {
    Item_Tcl* item = (Item_Tcl*) c;
    TCL_Return(tcl, (item->value()->IsMine() ? "1" : "0"));
}

static int item_own(ClientData c, Tcl_Interp* tcl, int argc, const char** argv) {
    Item_Tcl* item = (Item_Tcl*) c;
    if (! check_permission(tcl, item)) return TCL_ERROR;
    item->value()->MakeOwner();
    trigger_item(tcl, item);

    TCL_Return(tcl, "");
}

static int item_hilite(ClientData c, Tcl_Interp* tcl, int argc, const char** argv) {
    Item_Tcl* item = (Item_Tcl*) c;

    if (argc == 0) {
        Tcl_SetResult(tcl, (char*) (item->value()->Hilite()), TCL_VOLATILE);
        return TCL_OK;
    }

    if (! check_permission(tcl, item)) return TCL_ERROR;
    item->value()->Hilite(argv[0]);
    trigger_item(tcl, item);

    TCL_Return(tcl, "");
}

static int item_todo(ClientData c, Tcl_Interp* tcl, int argc, const char** argv) {
    Item_Tcl* item = (Item_Tcl*) c;

    if (argc == 0) {
        TCL_Return(tcl, (char*)(item->value()->IsTodo() ? "1" : "0"));
    }

    int todo;
    if (Tcl_GetBoolean(tcl, argv[0], &todo) != TCL_OK) {
        TCL_Error(tcl, "invalid value for todo flag");
    }
    if (! check_permission(tcl, item)) return TCL_ERROR;
    item->value()->SetTodo(todo);
    trigger_item(tcl, item);

    TCL_Return(tcl, "");
}

static int item_is_done(ClientData c, Tcl_Interp* tcl,int argc,const char* argv[]) {
    Item_Tcl* item = (Item_Tcl*) c;
    TCL_Return(tcl, (char*)(item->value()->IsDone() ? "1" : "0"));
}

static int item_done(ClientData c, Tcl_Interp* tcl, int argc, const char** argv) {
    Item_Tcl* item = (Item_Tcl*) c;
    if (! check_permission(tcl, item)) return TCL_ERROR;

    int done;
    if (Tcl_GetBoolean(tcl, argv[0], &done) != TCL_OK) {
        TCL_Error(tcl, "invalid value for done flag");
    }

    item->value()->SetDone(done);
    trigger_item(tcl, item);

    TCL_Return(tcl, "");
}

static int item_alarms(ClientData c, Tcl_Interp* tcl, int argc, const char** argv) {
    Item_Tcl* item = (Item_Tcl*) c;

    Appointment* appt = item->value()->AsAppointment();
    if (appt == 0) {
        TCL_Error(tcl, "unknown command");
    }

    if (argc == 0) {
        intArray* alarms = appt->GetAlarms();
        if (alarms == 0) {
            TCL_Error(tcl, "no alarms");
        }

        // Make Tcl string out of integers
        // (We allocate an array one larger than necessary to avoid
        // zero length allocation).

        int i;
        char** str = new char*[alarms->size()+1];
        for (i = 0; i < alarms->size(); i++) {
            int x = alarms->slot(i);

            str[i] = new char[100];
            sprintf(str[i], "%d", x);
        }
        char* list = Tcl_Merge(alarms->size(), str);
        for (i = 0; i < alarms->size(); i++) {
            delete [] str[i];
        }
        delete [] str;

        Tcl_SetResult(tcl, list, TCL_DYNAMIC);
        return TCL_OK;
    }

    int count;
    const char** list;
    if (Tcl_SplitList(tcl, argv[0], &count, &list) != TCL_OK) {
        TCL_Error(tcl, "invalid alarm list");
    }

    intArray* alarms = new intArray(count);
    for (int i = 0; i < count; i++) {
        int x;
        if ((Tcl_GetInt(tcl, list[i], &x) != TCL_OK) || (x < 0) || (x > 60)) {
            Tcl_Free((char*) list);
            delete alarms;
            TCL_Error(tcl, "invalid alarm time");
        }
        alarms->append(x);
    }
    Tcl_Free((char*) list);
            
    if (! check_permission(tcl, item)) {
        delete alarms;
        return TCL_ERROR;
    }

    appt->SetAlarms(alarms);
    delete alarms;
    trigger_item(tcl, item);

    TCL_Return(tcl, "");
}

static int item_option(ClientData c, Tcl_Interp* tcl, int argc, const char** argv) {
    Item_Tcl* item = (Item_Tcl*) c;

    if (argc == 1) {
        char const* val = item->value()->GetOption(argv[0]);
        if (val == 0) TCL_Error(tcl, "unknown item option");
        Tcl_SetResult(tcl, (char*) val, TCL_VOLATILE);
        return TCL_OK;
    }

    if (! check_permission(tcl, item)) return TCL_ERROR;
    item->value()->SetOption(argv[0], argv[1]);
    trigger_item(tcl, item);
    TCL_Return(tcl, "");
}

static int item_doption(ClientData c, Tcl_Interp* tcl, int argc,const char* argv[]) {
    Item_Tcl* item = (Item_Tcl*) c;

    if (! check_permission(tcl, item)) return TCL_ERROR;

    // Check whether option exists
    char const* val = item->value()->GetOption(argv[0]);
    if (val == 0) TCL_Error(tcl, "unknown item option");

    item->value()->RemoveOption(argv[0]);
    trigger_item(tcl, item);
    TCL_Return(tcl, "");
}

static int item_empty(ClientData c, Tcl_Interp* tcl, int argc, const char** argv) {
    Item_Tcl* item = (Item_Tcl*) c;
    TCL_Return(tcl, (item->value()->empty()?"1":"0"));
}

static int item_repeat(ClientData c, Tcl_Interp* tcl, int argc, const char** argv) {
    Item_Tcl* item = (Item_Tcl*) c;
    TCL_Return(tcl,(item->value()->repeats()?"1":"0"));
}

static int item_first(ClientData c, Tcl_Interp* tcl, int argc, const char** argv) {
    Item_Tcl* item = (Item_Tcl*) c;
    Date d;
    if (! item->value()->first(d)) {
        TCL_Error(tcl, "item does not occur");
    }

    char buffer[100];
    sprintf(buffer, "%d", d.EpochDays());
    Tcl_SetResult(tcl, buffer, TCL_VOLATILE);
    return TCL_OK;
}

static int item_type(ClientData c, Tcl_Interp* tcl, int argc, const char** argv) {
    Item_Tcl* item = (Item_Tcl*) c;
    const char* result;
    switch (item->value()->repeat_type()) {
      case DateSet::None:
        result = "";
        break;
      case DateSet::Daily:
        result = "Daily";
        break;
      case DateSet::Weekly:
        result = "Weekly";
        break;
      case DateSet::BiWeekly:
        result = "Every Two Weeks";
        break;
      case DateSet::ThreeWeekly:
        result = "Every Three Weeks";
        break;
      case DateSet::FourWeekly:
        result = "Every Four Weeks";
        break;
      case DateSet::Monthly:
        result = "Monthly";
        break;
      case DateSet::TwoMonthly:
        result = "Every Two Months";
        break;
      case DateSet::ThreeMonthly:
        result = "Every Three Months";
        break;
      case DateSet::FourMonthly:
        result = "Every Four Months";
        break;
      case DateSet::SixMonthly:
        result = "Every Six Months";
        break;
      case DateSet::Annual:
        result = "Annual";
        break;
      default:
        result = "Complex";
        break;
    }
    TCL_Return(tcl, result);
}

static int item_desc(ClientData c, Tcl_Interp* tcl, int argc, const char** argv) {
    Item_Tcl* item = (Item_Tcl*) c;
    charArray buffer;

    if (argc == 0)
        item->value()->describe(&buffer);
    else
      if (argc == 1 && strcmp(argv[0], "-terse") == 0)
        item->value()->describe_terse(&buffer);
      else
        TCL_Error(tcl, "invalid argument(s)");
    buffer.append('\0');
    Tcl_SetResult(tcl, buffer.as_pointer(), TCL_VOLATILE);
    return TCL_OK;
}

static int item_cont(ClientData c, Tcl_Interp* tcl, int argc, const char** argv) {
    Item_Tcl* item = (Item_Tcl*) c;
    int dateDays;
    if (Tcl_GetInt(tcl, argv[0], &dateDays) != TCL_OK) {
        TCL_Error(tcl, "invalid date");
    }
    Date date(dateDays);
    TCL_Return(tcl, (item->value()->contains(date)?"1":"0"));
}

static int item_next(ClientData c, Tcl_Interp* tcl, int argc, const char** argv) {
    Item_Tcl* item = (Item_Tcl*) c;
    int dateDays;
    if (Tcl_GetInt(tcl, argv[0], &dateDays) != TCL_OK) {
        TCL_Error(tcl, "invalid date");
    }
    Date date(dateDays);
    Date next;
    if (! item->value()->next(date, next)) {
        TCL_Error(tcl, "no next occurrence for item");
    }

    char buffer[100];
    sprintf(buffer, "%d", next.EpochDays());
    Tcl_SetResult(tcl, buffer, TCL_VOLATILE);
    return TCL_OK;
}

static int item_range(ClientData c, Tcl_Interp* tcl, int argc, const char** argv) {
    Item_Tcl* item = (Item_Tcl*) c;

    Date s, f;
    if (item->value()->range(s, f)) {
        char buffer[100];
        sprintf(buffer, "%d", s.EpochDays());
        if (Tcl_SetVar(tcl, argv[0], buffer, 0) == NULL)
            TCL_Error(tcl, "could not set range start variable");
        sprintf(buffer, "%d", f.EpochDays());
        if (Tcl_SetVar(tcl, argv[1], buffer, 0) == NULL)
            TCL_Error(tcl, "could not set range finish variable");
        TCL_Return(tcl, "1");
    }
    else {
        TCL_Return(tcl, "0");
    }
}

static int item_date(ClientData c, Tcl_Interp* tcl, int argc, const char** argv) {
    Item_Tcl* item = (Item_Tcl*) c;
    int dateDays;
    if (Tcl_GetInt(tcl, argv[0], &dateDays) != TCL_OK) {
        TCL_Error(tcl, "invalid date");
    }
    Date date(dateDays);
    if (! check_permission(tcl, item)) return TCL_ERROR;
    item->value()->set_date(date);
    trigger_item(tcl, item);

    TCL_Return(tcl, "");
}

static int item_start(ClientData c, Tcl_Interp* tcl, int argc, const char** argv) {
    Item_Tcl* item = (Item_Tcl*) c;
    int dateDays;

    if (Tcl_GetInt(tcl, argv[0], &dateDays) != TCL_OK) {
        TCL_Error(tcl, "invalid date");
    }

    Date date(dateDays);
    if (! check_permission(tcl, item)) return TCL_ERROR;
    item->value()->set_start(date);
    trigger_item(tcl, item);

    TCL_Return(tcl, "");
}

static int item_finish(ClientData c, Tcl_Interp* tcl, int argc, const char** argv) {
    Item_Tcl* item = (Item_Tcl*) c;
    int dateDays;

    if (Tcl_GetInt(tcl, argv[0], &dateDays) != TCL_OK) {
        TCL_Error(tcl, "invalid date");
    }

    Date date(dateDays);
    if (! check_permission(tcl, item)) return TCL_ERROR;
    item->value()->set_finish(date);
    trigger_item(tcl, item);

    TCL_Return(tcl, "");
}

static int item_ondel(ClientData c, Tcl_Interp* tcl, int argc, const char** argv) {
    Item_Tcl* item = (Item_Tcl*) c;
    int dateDays;
    if (Tcl_GetInt(tcl, argv[0], &dateDays) != TCL_OK) {
        TCL_Error(tcl, "invalid date");
    }
    Date date(dateDays);
    if (! check_permission(tcl, item)) return TCL_ERROR;
    item->value()->delete_occurrence(date);
    trigger_item(tcl, item);

    TCL_Return(tcl, "");
}

static int item_dayr(ClientData c, Tcl_Interp* tcl, int argc, const char** argv) {
    Item_Tcl* item = (Item_Tcl*) c;

    int interval;
    if ((Tcl_GetInt(tcl, argv[0], &interval) != TCL_OK) || (interval < 1)) {
        TCL_Error(tcl, "invalid interval");
    }

    int anchorDays;
    if (Tcl_GetInt(tcl, argv[1], &anchorDays) != TCL_OK) {
        TCL_Error(tcl, "invalid anchor date");
    }
    Date anchor(anchorDays);

    if (! check_permission(tcl, item)) return TCL_ERROR;
    item->value()->set_day_based_repeat(interval, anchor);
    trigger_item(tcl, item);

    TCL_Return(tcl, "");
}

static int item_monthr(ClientData c, Tcl_Interp* tcl, int argc, const char** argv) {
    Item_Tcl* item = (Item_Tcl*) c;

    int interval;
    if ((Tcl_GetInt(tcl, argv[0], &interval) != TCL_OK) || (interval < 1)) {
        TCL_Error(tcl, "invalid interval");
    }

    int anchorDays;
    if (Tcl_GetInt(tcl, argv[1], &anchorDays) != TCL_OK) {
        TCL_Error(tcl, "invalid anchor date");
    }
    Date anchor(anchorDays);

    if (! check_permission(tcl, item)) return TCL_ERROR;
    item->value()->set_month_based_repeat(interval, anchor);
    trigger_item(tcl, item);

    TCL_Return(tcl, "");
}

// Helper routine for "set_monthly" interfaces.
// modifies     "n", "anchor", "interval".
// effects      Parses "n", "anchor" and "interval" from the command
//              arguments.  Returns TCL_OK iff successful.  If "anchor"
//              and "interval" are not specified in the command arguments,
//              then "anchor" is set to today and "interval" is set to "1".
static int parse_month_args(Tcl_Interp* tcl, int argc, const char* argv[],
                            int& n, Date& anchor, int& interval)
{
    if ((argc != 1) && (argc != 3)) TCL_Error(tcl,"wrong number of arguments");
    if ((Tcl_GetInt(tcl, argv[0], &n) != TCL_OK) || (n < 1))
        TCL_Error(tcl, "invalid count");

    // Get anchor and interval
    if (argc == 1) {
        anchor = Date::Today();
        interval = 1;
    }
    else {
        int anchorDays;
        if (Tcl_GetInt(tcl, argv[1], &anchorDays) != TCL_OK)
            TCL_Error(tcl, "invalid anchor date");
        anchor = Date(anchorDays);

        if ((Tcl_GetInt(tcl, argv[2], &interval) != TCL_OK) || (interval < 1))
            TCL_Error(tcl, "invalid interval");
    }

    return TCL_OK;
}

static int item_mday(ClientData c, Tcl_Interp* tcl, int argc, const char** argv) {
    Item_Tcl* item = (Item_Tcl*) c;

    int n, interval;
    Date anchor;
    if (parse_month_args(tcl, argc, argv, n, anchor, interval) != TCL_OK)
        return TCL_ERROR;

    if (! check_permission(tcl, item)) return TCL_ERROR;
    item->value()->set_monthly_by_days(n, interval, anchor, 0);
    trigger_item(tcl, item);

    TCL_Return(tcl, "");
}

static int item_mlday(ClientData c, Tcl_Interp* tcl, int argc, const char** argv) {
    Item_Tcl* item = (Item_Tcl*) c;

    int n, interval;
    Date anchor;
    if (parse_month_args(tcl, argc, argv, n, anchor, interval) != TCL_OK)
        return TCL_ERROR;

    if (! check_permission(tcl, item)) return TCL_ERROR;
    item->value()->set_monthly_by_days(n, interval, anchor, 1);
    trigger_item(tcl, item);

    TCL_Return(tcl, "");
}

static int item_mworkday(ClientData c, Tcl_Interp* tcl,
                         int argc, const char** argv) {
    Item_Tcl* item = (Item_Tcl*) c;

    int n, interval;
    Date anchor;
    if (parse_month_args(tcl, argc, argv, n, anchor, interval) != TCL_OK)
        return TCL_ERROR;

    if (! check_permission(tcl, item)) return TCL_ERROR;
    item->value()->set_monthly_by_workdays(n, interval, anchor, 0);
    trigger_item(tcl, item);

    TCL_Return(tcl, "");
}

static int item_mlworkday(ClientData c, Tcl_Interp* tcl,
                          int argc, const char** argv) {
    Item_Tcl* item = (Item_Tcl*) c;

    int n, interval;
    Date anchor;
    if (parse_month_args(tcl, argc, argv, n, anchor, interval) != TCL_OK)
        return TCL_ERROR;

    if (! check_permission(tcl, item)) return TCL_ERROR;
    item->value()->set_monthly_by_workdays(n, interval, anchor, 1);
    trigger_item(tcl, item);

    TCL_Return(tcl, "");
}

static int item_mweekday(ClientData c, Tcl_Interp* tcl,
                         int argc, const char** argv) {
    Item_Tcl* item = (Item_Tcl*) c;

    int wday;
    if ((Tcl_GetInt(tcl,argv[0],&wday) != TCL_OK) || (wday < 1) || (wday > 7))
        TCL_Error(tcl, "invalid weekday");
    WeekDay w = WeekDay::First() + (wday - 1);

    int n, interval;
    Date anchor;
    if (parse_month_args(tcl, argc-1, argv+1, n, anchor, interval) != TCL_OK)
        return TCL_ERROR;

    if (! check_permission(tcl, item)) return TCL_ERROR;
    item->value()->set_monthly_by_weeks(n, w, interval, anchor, 0);
    trigger_item(tcl, item);

    TCL_Return(tcl, "");
}

static int item_mlweekday(ClientData c, Tcl_Interp* tcl,
                          int argc, const char** argv) {
    Item_Tcl* item = (Item_Tcl*) c;

    int wday;
    if ((Tcl_GetInt(tcl,argv[0],&wday) != TCL_OK) || (wday < 1) || (wday > 7))
        TCL_Error(tcl, "invalid weekday");
    WeekDay w = WeekDay::First() + (wday - 1);

    int n, interval;
    Date anchor;
    if (parse_month_args(tcl, argc-1, argv+1, n, anchor, interval) != TCL_OK)
        return TCL_ERROR;

    if (! check_permission(tcl, item)) return TCL_ERROR;
    item->value()->set_monthly_by_weeks(n, w, interval, anchor, 1);
    trigger_item(tcl, item);

    TCL_Return(tcl, "");
}

static int item_wdays(ClientData c, Tcl_Interp* tcl, int argc, const char** argv) {
    Item_Tcl* item = (Item_Tcl*) c;

    /* Collect weekdays */
    int i;
    SmallIntSet set;
    set.Clear();
    for (i = 0; i < argc; i++) {
        int weekday;
        if ((Tcl_GetInt(tcl, argv[i], &weekday) != TCL_OK) ||
            (weekday < 1) ||
            (weekday > 7)) {
            TCL_Error(tcl, "invalid weekday");
        }
        set.Insert(weekday);
    }

    if (! check_permission(tcl, item)) return TCL_ERROR;

    /* Repeat every month */
    SmallIntSet months;
    months.Clear();
    for (i = 1; i <= 12; i++) {
        months.Insert(i);
    }

    item->value()->set_week_set(set, months);
    trigger_item(tcl, item);

    TCL_Return(tcl, "");
}

static int check_permission(Tcl_Interp* tcl, Item_Tcl* item) {
    CalFile* file = item->calendar();
    if (file == 0) return 1;

    if (file->GetCalendar()->ReadOnly()) {
        Tcl_SetResult(tcl, const_cast<char*>("item is in readonly calendar"),
                TCL_STATIC);
        return 0;
    }

    file->Modified();
    return 1;
}

static void trigger_item(Tcl_Interp* tcl, Item_Tcl* item, char const* t) {
    if (item->calendar() != 0)
        trigger(tcl, t, item->handle());
}