summaryrefslogtreecommitdiff
path: root/src/notify.c
blob: 8681bac69e14ad9dbca0b39924b29365abf193a5 (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
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
/*	$calcurse: notify.c,v 1.20 2007/08/19 13:16:45 culot Exp $	*/

/*
 * Calcurse - text-based organizer
 * Copyright (c) 2004-2007 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 <time.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "i18n.h"
#include "utils.h"
#include "custom.h"
#include "notify.h"

static struct notify_vars_s *notify = NULL;
static struct notify_app_s *notify_app = NULL;
static pthread_t notify_t_main;

/* Return 1 if we need to display the notify-bar, else 0. */
int 
notify_bar(void)
{
	int display_bar = 0;

	pthread_mutex_lock(&nbar->mutex);
	display_bar = (nbar->show) ? 1 : 0;
	pthread_mutex_unlock(&nbar->mutex);
	
	return display_bar;
}

/* Initialize the nbar variable used to store notification options. */
void 
notify_init_vars(void)
{
	char *time_format = "%T";
	char *date_format = "%a %F";
	char *cmd = "printf '\\a'";

	nbar = (struct nbar_s *) malloc(sizeof(struct nbar_s));
	pthread_mutex_init(&nbar->mutex, NULL);
	nbar->show = 1;
	nbar->cntdwn = 300; 
	strncpy(nbar->datefmt, date_format, strlen(date_format) + 1); 
	strncpy(nbar->timefmt, time_format, strlen(time_format) + 1);
	strncpy(nbar->cmd, cmd, strlen(cmd) + 1);

	if ((nbar->shell = getenv("SHELL")) == NULL)
		nbar->shell = "/bin/sh"; 
}

/* Extract the appointment file name from the complete file path. */
static void 
extract_aptsfile(void)
{
	char *file;

	file = strrchr(path_apts, '/');
	if (!file)
		notify->apts_file = path_apts;
	else {
		notify->apts_file = file;
		notify->apts_file++;
	}
}

/* 
 * Create the notification bar, by initializing all the variables and 
 * creating the notification window (l is the number of lines, c the
 * number of columns, y and x are its coordinates). 
 */
void 
notify_init_bar(void)
{
	notify = (struct notify_vars_s *) malloc(sizeof(struct notify_vars_s));	
	notify_app = (struct notify_app_s *) 
	    malloc(sizeof(struct notify_app_s));
	pthread_mutex_init(&notify->mutex, NULL);
	pthread_mutex_init(&notify_app->mutex, NULL);
	notify_app->got_app = 0;
	notify->win = newwin(wins_prop(NOTIFY, HEIGHT), 
	    wins_prop(NOTIFY, WIDTH), wins_prop(NOTIFY, YPOS), 
	    wins_prop(NOTIFY, XPOS));
	extract_aptsfile();
}

/* Stop the notify-bar main thread. */
void 
notify_stop_main_thread(void)
{
	pthread_cancel(notify_t_main);
	return;
}

/* 
 * The calcurse window geometry has changed so we need to reset the 
 * notification window. 
 */
void 
notify_reinit_bar(int l, int c, int y, int x)
{
	delwin(notify->win);
	notify->win = newwin(l, c, y, x);
}

/* Launch user defined command as a notification. */
static void
launch_cmd(char *cmd, char *shell)
{
	int pid;

	pid = fork();
	
	if (pid < 0)
		ierror(_("FATAL ERROR in launch_cmd: could not fork"));
	else if (pid == 0) /* Child: launch user defined command */
		if (execlp(shell, shell, "-c", cmd, (char *)NULL) < 0)
			ierror(_("FATAL ERROR in launch_cmd: could not "
			    "launch user command"));
}

/* 
 * Update the notification bar. This is useful when changing color theme
 * for example.
 */
void 
notify_update_bar(void)
{
	const int space = 3;
	int file_pos, date_pos, app_pos, txt_max_len, too_long = 0;
	int time_left, hours_left, minutes_left;
	int blinking;
	char buf[BUFSIZ];
	
	date_pos = space;
	pthread_mutex_lock(&notify->mutex);

	file_pos = strlen(notify->date) + strlen(notify->time) + 7 + 2*space;
	app_pos = file_pos + strlen(notify->apts_file) + 2 + space;
	txt_max_len = col - (app_pos + 12 + space);

	custom_apply_attr(notify->win, ATTR_HIGHEST);
	wattron(notify->win, A_UNDERLINE | A_REVERSE);
	mvwhline(notify->win, 0, 0, ACS_HLINE, col);
	mvwprintw(notify->win, 0, date_pos, "[ %s | %s ]", 
		notify->date, notify->time);
	mvwprintw(notify->win, 0, file_pos, "(%s)", notify->apts_file);

	pthread_mutex_lock(&notify_app->mutex);
	if (notify_app->got_app) {
		if (strlen(notify_app->txt) > txt_max_len) {
			too_long = 1;
			strncpy(buf, notify_app->txt, txt_max_len - 3);
			buf[txt_max_len - 3] = '\0';
		}
		
		time_left = notify_app->time - notify->time_in_sec; 
		if (time_left > 0) {
			hours_left = (time_left / HOURINSEC);
			minutes_left = (time_left - hours_left * HOURINSEC) / 
			    MININSEC;
			pthread_mutex_lock(&nbar->mutex);

			if (time_left < nbar->cntdwn &&
			    (notify_app->state & APOINT_NOTIFY))
				blinking = 1;
			else
				blinking = 0;

			if (blinking)
				wattron(notify->win, A_BLINK);
			if (too_long) 	
				mvwprintw(notify->win, 0, app_pos, 
			            "> %02d:%02d :: %s.. <", 
			            hours_left, minutes_left, buf);
			else
				mvwprintw(notify->win, 0, app_pos, 
			            "> %02d:%02d :: %s <", 
			            hours_left, minutes_left, 
				    notify_app->txt);
			if (blinking)
				wattroff(notify->win, A_BLINK);

			if (blinking && 
			    !(notify_app->state & APOINT_NOTIFIED)) {
				notify_app->state |= APOINT_NOTIFIED;
				launch_cmd(nbar->cmd, nbar->shell);
			}
			pthread_mutex_unlock(&nbar->mutex);
		} else {
			notify_app->got_app = 0;
			pthread_mutex_unlock(&notify_app->mutex);
			pthread_mutex_unlock(&notify->mutex);
			notify_check_next_app();
			return;
		}
	}
	pthread_mutex_unlock(&notify_app->mutex);

	wattroff(notify->win, A_UNDERLINE | A_REVERSE);
	custom_remove_attr(notify->win, ATTR_HIGHEST);
	wrefresh(notify->win);

	pthread_mutex_unlock(&notify->mutex);
}

/* Update the notication bar content */
static void *
notify_main_thread(void *arg)
{
	const unsigned thread_sleep = 1;
	const unsigned check_app = MININSEC;
	int elapse= 0, got_app = 0;
	struct tm *ntime;
	time_t ntimer;

	elapse = 0;
	got_app = 0;

	for (;;) {
		ntimer = time(NULL);
		ntime = localtime(&ntimer);
		pthread_mutex_lock(&notify->mutex);
		notify->time_in_sec = ntimer;
		pthread_mutex_lock(&nbar->mutex);
		strftime(notify->time, NOTIFY_FIELD_LENGTH, nbar->timefmt, ntime);
		strftime(notify->date, NOTIFY_FIELD_LENGTH, nbar->datefmt, ntime);
		pthread_mutex_unlock(&nbar->mutex);
		pthread_mutex_unlock(&notify->mutex);
		notify_update_bar();
		sleep(thread_sleep);
		elapse += thread_sleep;
		if (elapse >= check_app) {
			elapse = 0;
			pthread_mutex_lock(&notify_app->mutex);
			got_app = notify_app->got_app;
			pthread_mutex_unlock(&notify_app->mutex);
			if (!got_app) 
				notify_check_next_app();
		}
	}
	pthread_exit((void*) 0);
}

/* Look for the next appointment within the next 24 hours. */
static void *
notify_thread_app(void *arg)
{
	struct notify_app_s *tmp_app;
	time_t current_time;

	current_time = time(NULL);

	/* Use a temporary structure not to lock the mutex for a too
	 * long time while looking for next appointment. */
	tmp_app = (struct notify_app_s *) malloc(sizeof(struct notify_app_s));
	tmp_app->time = current_time + DAYINSEC;
	tmp_app->got_app = 0;
	tmp_app = recur_apoint_check_next(tmp_app, current_time, get_today());
	tmp_app = apoint_check_next(tmp_app, current_time);

	pthread_mutex_lock(&notify_app->mutex);
	if (tmp_app->got_app) {
		notify_app->got_app = 1;
		notify_app->time = tmp_app->time;
		notify_app->txt = mycpy(tmp_app->txt);
		notify_app->state = tmp_app->state;
	} else {
		notify_app->got_app = 0;
	}
	pthread_mutex_unlock(&notify_app->mutex);

	free(tmp_app);
	notify_update_bar();

	pthread_exit((void*) 0);
}

/* Launch the thread notify_thread_app to look for next appointment. */
void 
notify_check_next_app(void)
{
	pthread_t notify_t_app;

	pthread_create(&notify_t_app, NULL, notify_thread_app, NULL);
	return;
}

/* Check if the newly created appointment is to be notified. */
void 
notify_check_added(char *mesg, long start, char state)
{
	time_t current_time;
	int update_notify = 0;
	long gap;

	current_time = time(NULL);
	pthread_mutex_lock(&notify_app->mutex);
	if (!notify_app->got_app) {
		gap = start - current_time;
		if (gap >= 0 && gap <= DAYINSEC) 
			update_notify = 1;
	} else if (start < notify_app->time && start >= current_time) {
		update_notify = 1;
	} else if (start == notify_app->time && state != notify_app->state)
		update_notify = 1;

	if (update_notify) {
		notify_app->got_app = 1;
		notify_app->time = start;
		notify_app->txt = mycpy(mesg);	
		notify_app->state = state;
	}
	pthread_mutex_unlock(&notify_app->mutex);
	notify_update_bar();
}

/* Check if the newly repeated appointment is to be notified. */
void 
notify_check_repeated(recur_apoint_llist_node_t *i)
{
	long real_app_time;
	int update_notify = 0;
	time_t current_time;

	current_time = time(NULL);
	pthread_mutex_lock(&notify_app->mutex);
	if ((real_app_time = recur_item_inday(i->start, i->exc, i->rpt->type,
	    i->rpt->freq, i->rpt->until, get_today()) > current_time)) {
		if (!notify_app->got_app) {
			if (real_app_time - current_time <= DAYINSEC) 
				update_notify = 1;
		} else if (real_app_time < notify_app->time && 
		    real_app_time >= current_time) {
			update_notify = 1;
		} else if (real_app_time == notify_app->time &&
		    i->state != notify_app->state)
			update_notify = 1;
	}
	if (update_notify) {
		notify_app->got_app = 1;
		notify_app->time = real_app_time;
		notify_app->txt = mycpy(i->mesg);	
		notify_app->state = i->state;
	}
	pthread_mutex_unlock(&notify_app->mutex);
	notify_update_bar();
}

int 
notify_same_item(long time)
{
	int same = 0;
	
	pthread_mutex_lock(&(notify_app->mutex));
	if (notify_app->got_app && notify_app->time == time)
		same = 1;
	pthread_mutex_unlock(&(notify_app->mutex));

	return same;
}

int 
notify_same_recur_item(recur_apoint_llist_node_t *i)
{
	int same = 0;
	long item_start = 0;

	item_start = recur_item_inday(i->start, i->exc, i->rpt->type,
		i->rpt->freq, i->rpt->until, get_today());
	pthread_mutex_lock(&notify_app->mutex);
	if (notify_app->got_app && item_start == notify_app->time)
		same = 1;
	pthread_mutex_unlock(&(notify_app->mutex));

	return same;
}

/* Launch the notify-bar main thread. */
void 
notify_start_main_thread(void) 
{
	pthread_create(&notify_t_main, NULL, notify_main_thread, NULL);
	notify_check_next_app();
	return;
}

/* Print options related to the notify-bar. */
static void 
notify_print_options(WINDOW *win, int col)
{
	enum {SHOW, DATE, CLOCK, WARN, CMD, NB_OPT};

	struct opt_s {
		char name[BUFSIZ];
		char desc[BUFSIZ];
		char value[BUFSIZ];
	} opt[NB_OPT];

	int i, y, x, l, x_pos, y_pos, x_offset, y_offset, maxcol, maxlen;
	char buf[BUFSIZ];

	x_pos = 3;
	x_offset = 4;
	y_pos = 4;
	y_offset = 3;
	maxcol = col - 2;

	strncpy(opt[SHOW].name, _("notify-bar_show = "), BUFSIZ);
	strncpy(opt[DATE].name, _("notify-bar_date = "), BUFSIZ);
	strncpy(opt[CLOCK].name, _("notify-bar_clock = "), BUFSIZ);
	strncpy(opt[WARN].name, _("notify-bar_warning = "), BUFSIZ);
	strncpy(opt[CMD].name, _("notify-bar_command = "), BUFSIZ);

	strncpy(opt[SHOW].desc, 
	    _("(if set to YES, notify-bar will be displayed)"), 
	    BUFSIZ);
	strncpy(opt[DATE].desc, 
	    _("(Format of the date to be displayed inside notify-bar)"), 
	    BUFSIZ);
	strncpy(opt[CLOCK].desc, 
	    _("(Format of the time to be displayed inside notify-bar)"),
	    BUFSIZ);
	strncpy(opt[WARN].desc, 
	    _("(Warn user if an appointment is within next 'notify-bar_warning'"
	    " seconds)"),
	    BUFSIZ);
	strncpy(opt[CMD].desc, 
	    _("(Command used to notify user of an upcoming appointment)"),
	    BUFSIZ);

	pthread_mutex_lock(&nbar->mutex);

	strncpy(opt[DATE].value, nbar->datefmt, BUFSIZ);
	strncpy(opt[CLOCK].value, nbar->timefmt, BUFSIZ);
	snprintf(opt[WARN].value, BUFSIZ, "%d", nbar->cntdwn);
	strncpy(opt[CMD].value, nbar->cmd, BUFSIZ);

	l = strlen(opt[SHOW].name);
	x = x_pos + x_offset + l;
	mvwprintw(win, y_pos, x_pos, "[1] %s", opt[SHOW].name);
	erase_window_part(win, x, y_pos, maxcol, y_pos);
	print_option_incolor(win, nbar->show, y_pos, x);
	mvwprintw(win, y_pos + 1, x_pos, opt[SHOW].desc);

	for (i = 1; i < NB_OPT; i++) {
		l = strlen(opt[i].name);
		y = y_pos + i * y_offset;
		x = x_pos + x_offset + l;
		maxlen = maxcol - x - 2;

		mvwprintw(win, y, x_pos, "[%d] %s", i + 1, opt[i].name);
		erase_window_part(win, x, y, maxcol, y);
		custom_apply_attr(win, ATTR_HIGHEST);
		if (strlen(opt[i].value) < maxlen)
			mvwprintw(win, y, x, "%s", opt[i].value);
		else {
			strncpy(buf, opt[i].value, maxlen - 1);
			buf[maxlen - 1] = '\0';
			mvwprintw(win, y, x, "%s...", buf);
		}
		custom_remove_attr(win, ATTR_HIGHEST);
		mvwprintw(win, y + 1, x_pos, opt[i].desc);
	}

	pthread_mutex_unlock(&nbar->mutex);
	wmove(swin, 1, 0);
	wnoutrefresh(win);
	doupdate();
}

/* Notify-bar configuration. */
void 
notify_config_bar(void)
{
	WINDOW *conf_win = 0L;
	char label[BUFSIZ];
	char *buf;
	char *number_str = 
	    _("Enter an option number to change its value [Q to quit] ");
	char *date_str = 
	    _("Enter the date format (see 'man 3 strftime' for possible formats) ");
	char *time_str = 
	    _("Enter the time format (see 'man 3 strftime' for possible formats) ");
	char *count_str = 
	    _("Enter the number of seconds (0 not to be warned before an appointment)");
	char *cmd_str = _("Enter the notification command ");
	int ch = 0 , win_row, change_win = 1;

	buf = (char *)malloc(BUFSIZ);
	win_row = (notify_bar()) ? row - 3 : row - 2;
	snprintf(label, BUFSIZ, 
	    _("CalCurse %s | notify-bar options"), VERSION);

	while (ch != 'q') {
		if (change_win) {
			conf_win = newwin(win_row, col, 0, 0);
			box(conf_win, 0, 0);
			wins_show(conf_win, label);
		}
		status_mesg(number_str, "");
		notify_print_options(conf_win, col);
		*buf = '\0';
		ch = wgetch(swin);

		switch (ch) {
		case '1':	
			pthread_mutex_lock(&nbar->mutex);
			nbar->show = !nbar->show;
			pthread_mutex_unlock(&nbar->mutex);
			notify_stop_main_thread();
			if (notify_bar()) {
				notify_start_main_thread();
				win_row = row - 3;
			} else {
				win_row = row - 2;
			}
			delwin(conf_win);
			change_win = 1;
			break;
		case '2':
			status_mesg(date_str, "");
			pthread_mutex_lock(&nbar->mutex);
			strncpy(buf, nbar->datefmt, strlen(nbar->datefmt) + 1);
			pthread_mutex_unlock(&nbar->mutex);
			if (updatestring(swin, &buf, 0, 1) == 0) {
				pthread_mutex_lock(&nbar->mutex);
				strncpy(nbar->datefmt, buf, strlen(buf) + 1);
				pthread_mutex_unlock(&nbar->mutex);
			}
			change_win = 0;
			break;
		case '3':
			status_mesg(time_str, "");
			pthread_mutex_lock(&nbar->mutex);
			strncpy(buf, nbar->timefmt, strlen(nbar->timefmt) + 1);
			pthread_mutex_unlock(&nbar->mutex);
			if (updatestring(swin, &buf, 0, 1) == 0) {
				pthread_mutex_lock(&nbar->mutex);
				strncpy(nbar->timefmt, buf, strlen(buf) + 1);
				pthread_mutex_unlock(&nbar->mutex);
			}
			change_win = 0;
			break;
                case '4':
			status_mesg(count_str, "");
			pthread_mutex_lock(&nbar->mutex);
			printf(buf, "%d", nbar->cntdwn);
			pthread_mutex_unlock(&nbar->mutex);
			if (updatestring(swin, &buf, 0, 1) == 0 && 
				is_all_digit(buf) && 
				atoi(buf) >= 0 && atoi(buf) <= DAYINSEC) {
				pthread_mutex_lock(&nbar->mutex);
				nbar->cntdwn = atoi(buf);
				pthread_mutex_unlock(&nbar->mutex);
			}
			change_win = 0;
                        break;
		case '5':
			status_mesg(cmd_str, "");
			pthread_mutex_lock(&nbar->mutex);
			strncpy(buf, nbar->cmd, strlen(nbar->cmd) + 1);
			pthread_mutex_unlock(&nbar->mutex);
			if (updatestring(swin, &buf, 0, 1) == 0) {
				pthread_mutex_lock(&nbar->mutex);
				strncpy(nbar->cmd, buf, strlen(buf) + 1);
				pthread_mutex_unlock(&nbar->mutex);
			}
			change_win = 0;
                        break;
		}
	}
	free(buf);
	delwin(conf_win);
}