File: ical.C

package info (click to toggle)
ical 2.1-4
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 1,680 kB
  • ctags: 1,650
  • sloc: cpp: 12,639; tcl: 6,726; makefile: 584; sh: 521; perl: 60; ansic: 27
file content (146 lines) | stat: -rw-r--r-- 3,443 bytes parent folder | download | duplicates (3)
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
/* Copyright (c) 1994 by Sanjay Ghemawat */
#include <stdio.h>
#include <string.h>

#include "collect.h"
#include "ical.h"
#include "cal_tcl.h"
#include "item_tcl.h"

#include "arrays.h"
#include "calfile.h"
#include "calendar.h"
#include "dateset.h"
#include "item.h"

implementArray(ItemList,Item_Tcl*)
implementArray(Occurrences,Occurrence)

void collect_all(Calendar_Tcl* cal, ItemList& list) {
    Calendar* mainCalendar = cal->main->GetCalendar();

    for (int i = 0; i <= cal->includes->size(); i++) {
	Calendar* calendar = ((i >= cal->includes->size())
			      ? mainCalendar
			      : cal->includes->slot(i)->GetCalendar()
			      );
	collect_calendar(cal, calendar, list);
    }
}

void collect_calendar(Calendar_Tcl* cal, Calendar* calendar, ItemList& list) {
    Calendar* mainCalendar = cal->main->GetCalendar();

    for (int i = 0; i < calendar->Size(); i++) {
	Item_Tcl* item = Item_Tcl::find(calendar->Get(i));
	if (item == 0) continue;

	// Ignore hidden items
	if (mainCalendar->Hidden(item->value()->GetUid())) continue;

	list.append(item);
    }
}


void collect_occurrences(Calendar_Tcl* cal, ItemList const& items,
			 Occurrences& list,
			 Date s, Date f, int e)
{
    list.clear();
    for (int i = 0; i < items.size(); i++) {
	Item_Tcl* item = items[i];

	Date d = s - 1;
	Date limit = f;
	if (e) limit += item->value()->GetRemindStart();

	while (item->value()->next(d, d) && (d <= limit)) {
	    Occurrence o;
	    o.item = item;
	    o.date = d;
	    list.append(o);
	}
    }
}

static int occurs_before(Occurrence const& xo, Occurrence const& yo) {
    // Compare by date
    if (xo.date < yo.date) return 1;
    if (xo.date > yo.date) return 0;

    Item* x = xo.item->value();
    Item* y = yo.item->value();

    // Compare by appt time
    Appointment* xa = x->AsAppointment();
    Appointment* ya = y->AsAppointment();
    int xs = (xa == 0) ? -1 : xa->GetStart();
    int ys = (ya == 0) ? -1 : ya->GetStart();
    if (xs < ys) return 1;
    if (xs > ys) return 0;

    // Done items occur before todo items
    if (x->IsDone() && !y->IsDone()) return 1;
    if (y->IsDone() && !x->IsDone()) return 0;

    // Todo items occur before non-todo items
    if (x->IsTodo() && !y->IsTodo()) return 1;
    if (y->IsTodo() && !x->IsTodo()) return 0;

    return (strcmp(x->GetText(), y->GetText()) < 0);
}

void sort_occurrences(Occurrences& list) {
    int i = 0;
    while (i < list.size()) {
	/* Find min element in list[i..] */
	int minIndex = i;

	for (int j = i+1; j < list.size(); j++) {
	    if (occurs_before(list[j], list[minIndex])) {
		minIndex = j;
	    }
	}

	Occurrence temp;
	temp = list[i];
	list[i] = list[minIndex];
	list[minIndex] = temp;

	i++;
    }
}

void reverse(Occurrences& list) {
    // Scan from front and back of list while swapping elements
    int first = 0;
    int last = list.size() - 1;
    while (first < last) {
	Occurrence tmp = list[first];
	list[first] = list[last];
	list[last] = tmp;
	first++;
	last--;
    }
}

void trigger(Tcl_Interp* tcl, char const* ttype, char const* id) {
    charArray buffer;
    char const* cmd = "trigger fire ";

    buffer.concat(cmd, strlen(cmd));
    buffer.concat(ttype, strlen(ttype));

    if (id != 0) {
	buffer.append(' ');
	buffer.concat(id, strlen(id));
    }

    buffer.append('\0');

    if (Tcl_Eval(tcl, buffer.as_pointer()) == TCL_ERROR)
	fprintf(stderr, "ical: trigger error: %s\n", tcl->result);

    buffer.clear();
}