summaryrefslogtreecommitdiff
path: root/src/recur.c
blob: d0629aa53f7ab292f15f63c1b43726f634612390 (plain)
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
/*	$calcurse: recur.c,v 1.1 2006/07/31 21:00:03 culot Exp $	*/

/*
 * Calcurse - text-based organizer
 * Copyright (c) 2004-2006 Frederic Culot
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Send your feedback or comments to : calcurse@culot.org
 * Calcurse home page : http://culot.org/calcurse
 *
 */

#include <ncurses.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>

#include "i18n.h"
#include "utils.h"
#include "apoint.h"
#include "event.h"
#include "recur.h"

struct recur_apoint_s *recur_alist;
struct recur_event_s *recur_elist;

/* Insert a new recursive appointment in the general linked list */
struct recur_apoint_s *recur_apoint_new(char *mesg, long start, long dur, 
	int type, int freq, long until)
{
	struct recur_apoint_s *o, **i;
	o = (struct recur_apoint_s *) malloc(sizeof(struct recur_apoint_s));
	o->rpt = (struct rpt_s *) malloc(sizeof(struct rpt_s));
	o->exc = (struct days_s *) malloc(sizeof(struct days_s));
	o->mesg = (char *) malloc(strlen(mesg) + 1);
	strcpy(o->mesg, mesg);
	o->start = start;
	o->dur = dur;
	o->rpt->type = type;
	o->rpt->freq = freq;
	o->rpt->until = until;
	i = &recur_alist;
	for (;;) {
		if (*i == 0 || (*i)->start > start) {
			o->next = *i;
			*i = o;
			break;
		}
		i = &(*i)->next;
	}
	return o;
}

/* Insert a new recursive event in the general linked list */
struct recur_event_s *recur_event_new(char *mesg, long day, int id, 
	int type, int freq, long until)
{
	struct recur_event_s *o, **i;
	o = (struct recur_event_s *) malloc(sizeof(struct recur_event_s));
	o->rpt = (struct rpt_s *) malloc(sizeof(struct rpt_s));
	o->exc = (struct days_s *) malloc(sizeof(struct days_s));
	o->mesg = (char *) malloc(strlen(mesg) + 1);
	strcpy(o->mesg, mesg);
	o->day = day;
	o->id = id;
	o->rpt->type = type;
	o->rpt->freq = freq;
	o->rpt->until = until;
	i = &recur_elist;
	for (;;) {
		if (*i == 0 || (*i)->day > day) {
			o->next = *i;
			*i = o;
			break;
		}
		i = &(*i)->next;
	}
	return o;
}

/* 
 * Correspondance between the defines on recursive type, 
 * and the letter to be written in file. 
 */
char recur_def2char(int define){
	char recur_char;

	switch (define) {
	case 1:
		recur_char = 'D';
		break;	
	case 2:
		recur_char = 'W';
		break;
	case 3:
		recur_char = 'M';
		break;
	case 4:
		recur_char = 'Y';
		break;
	}
	return recur_char;
}

/* 
 * Correspondance between the letters written in file and the defines
 * concerning the recursive type.
 */
int recur_char2def(char type){
	int recur_def;

	switch (type) {
	case 'D':
		recur_def = RECUR_DAILY;
		break;	
	case 'W':
		recur_def = RECUR_WEEKLY;
		break;
	case 'M':
		recur_def = RECUR_MONTHLY;
		break;
	case 'Y':
		recur_def = RECUR_YEARLY;
		break;
	}
	return recur_def;
}

/* Writting of a recursive appointment into file. */
void recur_apoint_write(struct recur_apoint_s *o, FILE *f)
{
	struct tm *lt;
	time_t t;

	t = o->start;
	lt = localtime(&t);
	fprintf(f, "%02u/%02u/%04u @ %02u:%02u",
		lt->tm_mon + 1, lt->tm_mday, 1900 + lt->tm_year,
		lt->tm_hour, lt->tm_min);

	t = o->start + o->dur;
	lt = localtime(&t);
	fprintf(f, " -> %02u/%02u/%04u @ %02u:%02u",
		lt->tm_mon + 1, lt->tm_mday, 1900 + lt->tm_year,
		lt->tm_hour, lt->tm_min);

	t = o->rpt->until;
	if (t == 0) { /* We have an endless recurrent appointment. */
		fprintf(f, " {%d%c} |%s\n", o->rpt->freq, 
			recur_def2char(o->rpt->type), o->mesg);
	} else {
		lt = localtime(&t);
		fprintf(f, " {%d%c -> %02u/%02u/%04u} |%s\n",
			o->rpt->freq, recur_def2char(o->rpt->type),
			lt->tm_mon + 1, lt->tm_mday, 1900 + lt->tm_year,
			o->mesg);
	}
}

/* Writting of a recursive event into file. */
void recur_event_write(struct recur_event_s *o, FILE *f)
{
	struct tm *lt;
	time_t t;
	int st_mon, st_day, st_year;
	int end_mon, end_day, end_year;

	t = o->day;
	lt = localtime(&t);
	st_mon = lt->tm_mon + 1;
	st_day = lt->tm_mday;
	st_year = lt->tm_year + 1900;
	t = o->rpt->until;
	if (t == 0) { /* We have an endless recurrent event. */
		fprintf(f, "%02u/%02u/%04u [%d] {%d%c} %s\n",
			st_mon, st_day, st_year, o->id, o->rpt->freq,
			recur_def2char(o->rpt->type), o->mesg);
	} else {
		lt = localtime(&t);
		end_mon = lt->tm_mon + 1;
		end_day = lt->tm_mday;
		end_year = lt->tm_year + 1900;
		fprintf(f, "%02u/%02u/%04u [%d] {%d%c -> %02u/%02u/%04u} %s\n",
			st_mon, st_day, st_year, o->id, 
			o->rpt->freq, recur_def2char(o->rpt->type),
			end_mon, end_day, end_year, o->mesg);
	}
}

/* Load the recursive appointment description */
struct recur_apoint_s *recur_apoint_scan(FILE * f, struct tm start, 
	struct tm end, char type, int freq, struct tm until)
{
	struct tm *lt;
	char buf[MESG_MAXSIZE], *nl;
	time_t tstart, tend, t, tuntil;

	t = time(NULL);
	lt = localtime(&t);

        /* Read the appointment description */
	fgets(buf, MESG_MAXSIZE, f);
	nl = strchr(buf, '\n');
	if (nl) {
		*nl = '\0';
	}

	start.tm_sec = end.tm_sec = 0;
	start.tm_isdst = end.tm_isdst = -1;
	start.tm_year -= 1900;
	start.tm_mon--;
	end.tm_year -= 1900;
	end.tm_mon--;
	tstart = mktime(&start);
	tend = mktime(&end);

	if (until.tm_year != 0) {
		until.tm_hour = 12;
        	until.tm_min = 0;
		until.tm_sec =  0;
		until.tm_isdst = -1;
		until.tm_year -= 1900;
		until.tm_mon--;
		tuntil = mktime(&until);
	} else {
		tuntil = 0;
	}

	if (tstart == -1 || tend == -1 || tstart > tend || tuntil == -1) {
		fputs(_("FATAL ERROR in apoint_scan: date error in the appointment\n"), stderr);
		exit(EXIT_FAILURE);
	}
      
	return recur_apoint_new(buf, tstart, tend - tstart, 
		recur_char2def(type), freq, tuntil);
}

/* Load the recursive events from file */
struct recur_event_s *recur_event_scan(FILE * f, struct tm start, int id, 
	char type, int freq, struct tm until)
{
	struct tm *lt;
	char buf[MESG_MAXSIZE], *nl;
	time_t tstart, t, tuntil;

	t = time(NULL);
	lt = localtime(&t);

        /* Read the event description */
	fgets(buf, MESG_MAXSIZE, f);
	nl = strchr(buf, '\n');
	if (nl) {
		*nl = '\0';
	}
      
      	start.tm_hour = until.tm_hour = 12;
        start.tm_min = until.tm_min = 0;
	start.tm_sec = until.tm_sec =  0;
	start.tm_isdst = until.tm_isdst = -1;
	start.tm_year -= 1900;
	start.tm_mon--;
	if (until.tm_year != 0) {
		until.tm_year -= 1900;
		until.tm_mon--;
		tuntil = mktime(&until);
	} else {
		tuntil = 0;
	}
	tstart = mktime(&start);
	if ( (tstart == -1) || (tuntil == -1) ) {
		fputs(_("FATAL ERROR in recur_event_scan: date error in the event\n"), stderr);
		exit(EXIT_FAILURE);
	}
	
	return recur_event_new(buf, tstart, id, recur_char2def(type), freq, tuntil);
}

/* Write recursive items to file. */
void recur_save_data(FILE *f)
{
	struct recur_event_s *re;
	struct recur_apoint_s *ra;

	for (re = recur_elist; re != 0; re = re->next)
		recur_event_write(re, f);
	for (ra = recur_alist; ra != 0; ra = ra->next)
		recur_apoint_write(ra, f);
}

/* Check if the recurrent item belongs to the selected day. */
unsigned recur_item_inday(long item_start, int rpt_type, int rpt_freq,
			  long rpt_until, long day_start)
{
	const int DAYINSEC = 86400; 
	long day_end = day_start + DAYINSEC;
	int inday = 0;
	struct tm *lt;
	time_t t;
	char *error = 
		_("FATAL ERROR in recur_item_inday: unknown item type\n");

	if (rpt_until == 0) /* we have an endless recurrent item */
		rpt_until = day_end;
	while (item_start <= day_end && item_start <= rpt_until) {
		if (item_start < day_end && item_start >= day_start) {
			inday = 1;
			break;
		}
		t = item_start;
		lt = localtime(&t);
		if (rpt_type == RECUR_DAILY) {
			lt->tm_mday += rpt_freq;
		} else if (rpt_type == RECUR_WEEKLY) {
			lt->tm_mday += rpt_freq * 7;
		} else if (rpt_type == RECUR_MONTHLY) {
			lt->tm_mon += rpt_freq;
		} else if (rpt_type == RECUR_YEARLY) {
			lt->tm_year += rpt_freq;
		} else { /* NOT REACHED */
			fputs(error, stderr);
			exit(EXIT_FAILURE);
		}
		item_start = date2sec(lt->tm_year + 1900, lt->tm_mon + 1, 
			lt->tm_mday, 0, 0);
	}	
	return inday;
}

/* Returns a structure of type apoint_s given a structure of type recur_apoint_s */
struct apoint_s *recur_apoint_s2apoint_s(struct recur_apoint_s *p)
{
	struct apoint_s *a;

	a = (struct apoint_s *) malloc(sizeof(struct apoint_s));
	a->mesg = (char *) malloc(strlen(p->mesg) + 1);
	a->start = p->start;
	a->dur = p->dur;
	a->mesg = p->mesg;
	return a;
}