aboutsummaryrefslogtreecommitdiffstats
path: root/src/event.c
blob: 7c371e4df65563226c3ffea69487a241bd776576 (plain) (blame)
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
/*
 * Calcurse - text-based organizer
 *
 * Copyright (c) 2004-2023 calcurse Development Team <misc@calcurse.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the
 *        following disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the
 *        following disclaimer in the documentation and/or other
 *        materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Send your feedback or comments to : misc@calcurse.org
 * Calcurse home page : http://calcurse.org
 *
 */

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

#include "calcurse.h"
#include "sha1.h"

llist_t eventlist;
/* Dummy event for the APP panel for an otherwise empty day. */
struct event dummy = { DUMMY, 0, "", NULL };

void event_free(struct event *ev)
{
	mem_free(ev->mesg);
	erase_note(&ev->note);
	mem_free(ev);
}

struct event *event_dup(struct event *in)
{
	EXIT_IF(!in, _("null pointer"));

	struct event *ev = mem_malloc(sizeof(struct event));
	ev->id = in->id;
	ev->day = in->day;
	ev->mesg = mem_strdup(in->mesg);
	if (in->note)
		ev->note = mem_strdup(in->note);
	else
		ev->note = NULL;

	return ev;
}

void event_llist_init(void)
{
	LLIST_INIT(&eventlist);
}

void event_llist_free(void)
{
	LLIST_FREE_INNER(&eventlist, event_free);
	LLIST_FREE(&eventlist);
}

static int event_cmp(struct event *a, struct event *b)
{
	if (a->day < b->day)
		return -1;
	if (a->day > b->day)
		return 1;

	return strcmp(a->mesg, b->mesg);
}

/* Create a new event */
struct event *event_new(char *mesg, char *note, time_t day, int id)
{
	struct event *ev;

	ev = mem_malloc(sizeof(struct event));
	ev->mesg = mem_strdup(mesg);
	ev->day = day;
	ev->id = id;
	ev->note = (note != NULL) ? mem_strdup(note) : NULL;

	LLIST_ADD_SORTED(&eventlist, ev, event_cmp);

	return ev;
}

/* Check if the event belongs to the selected day */
unsigned event_inday(struct event *i, time_t *start)
{
	return (date_cmp_day(i->day, *start) == 0);
}

char *event_tostr(struct event *o)
{
	struct string s;
	struct tm lt;
	time_t t;

	string_init(&s);

	t = o->day;
	localtime_r(&t, &lt);
	string_catf(&s, "%02u/%02u/%04u [%d] ", lt.tm_mon + 1, lt.tm_mday,
		1900 + lt.tm_year, o->id);
	if (o->note != NULL)
		string_catf(&s, ">%s ", o->note);
	string_catf(&s, "%s", o->mesg);

	return string_buf(&s);
}

char *event_hash(struct event *ev)
{
	char *raw = event_tostr(ev);
	char *sha1 = mem_malloc(SHA1_DIGESTLEN * 2 + 1);
	sha1_digest(raw, sha1);
	mem_free(raw);

	return sha1;
}

void event_write(struct event *o, FILE * f)
{
	char *str = event_tostr(o);
	fprintf(f, "%s\n", str);
	mem_free(str);
}

/* Load the events from file */
char *event_scan(FILE * f, struct tm start, int id, char *note,
			 struct item_filter *filter)
{
	char buf[BUFSIZ], *nl;
	time_t tstart, tend;
	struct event *ev = NULL;
	int cond;

	if (!check_date(start.tm_year, start.tm_mon, start.tm_mday) ||
	    !check_time(start.tm_hour, start.tm_min))
		return _("illegal date in event");

	/* Read the event description */
	if (!fgets(buf, sizeof buf, f))
		return _("error in appointment description");

	nl = strchr(buf, '\n');
	if (nl) {
		*nl = '\0';
	}
	start.tm_hour = 0;
	start.tm_min = 0;
	start.tm_sec = 0;
	start.tm_isdst = -1;
	start.tm_year -= 1900;
	start.tm_mon--;

	tstart = mktime(&start);
	if (tstart == -1)
		return _("date error in event\n");
	tend = ENDOFDAY(tstart);

	/* Filter item. */
	if (filter) {
		cond = (
		    !(filter->type_mask & TYPE_MASK_EVNT) ||
		    (filter->regex && regexec(filter->regex, buf, 0, 0, 0)) ||
		    (filter->start_from != -1 && tstart < filter->start_from) ||
		    (filter->start_to != -1 && tstart > filter->start_to) ||
		    (filter->end_from != -1 && tend < filter->end_from) ||
		    (filter->end_to != -1 && tend > filter->end_to)
		);
		if (filter->hash) {
			ev = event_new(buf, note, tstart, id);
			char *hash = event_hash(ev);
			cond = cond || !hash_matches(filter->hash, hash);
			mem_free(hash);
		}

		if ((!filter->invert && cond) || (filter->invert && !cond)) {
			if (filter->hash)
				event_delete(ev);
			return NULL;
		}
	}
	if (!ev)
		ev = event_new(buf, note, tstart, id);
	return NULL;
}

/* Delete an event from the list. */
void event_delete(struct event *ev)
{
	llist_item_t *i = LLIST_FIND_FIRST(&eventlist, ev, NULL);

	if (!i)
		EXIT(_("no such appointment"));

	LLIST_REMOVE(&eventlist, i);
}

void event_paste_item(struct event *ev, time_t date)
{
	ev->day = date;
	LLIST_ADD_SORTED(&eventlist, ev, event_cmp);
}

/* Return true if the day_item is the dummy event. */
int event_dummy(struct day_item *item)
{
	return item->item.ev == &dummy;
}