aboutsummaryrefslogtreecommitdiffstats
path: root/src/notify.c
blob: 3d973d1cd7847424d4076d8c17f93b97738fec4c (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
/*	$calcurse: notify.c,v 1.1 2006/09/09 20:15:34 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 <pthread.h>
#include <time.h>

#include "custom.h"
#include "vars.h"
#include "notify.h"

static struct notify_vars_s *notify = NULL;

/* 
 * 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(int l, int c, int y, int x)
{
	pthread_t notify_thread;

	notify = (struct notify_vars_s *) malloc(sizeof(struct notify_vars_s));	
	pthread_mutex_init(&notify->mut, NULL);
	notify->win = newwin(l, c, y, x);
	notify_extract_aptsfile();
	pthread_create(&notify_thread, NULL, notify_thread_sub, NULL);
}

/* 
 * The calcurse window geometry as 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);
}

/* 
 * Update the notification bar. This is useful when changing color theme
 * for example.
 */
void notify_update_bar(void)
{
	int file_pos, date_pos;
	
	date_pos = 3;
	file_pos = date_pos + 2*NOTIFY_FIELD_LENGTH;

	pthread_mutex_lock(&notify->mut);

	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);
	wattroff(notify->win, A_UNDERLINE | A_REVERSE);
	custom_remove_attr(notify->win, ATTR_HIGHEST);
	wrefresh(notify->win);

	pthread_mutex_unlock(&notify->mut);
}

/* Extract the appointment file name from the complete file path. */
void notify_extract_aptsfile(void)
{
	notify->apts_file = strrchr(path_apts, '/');
	notify->apts_file++;
}

/* Update the notication bar content */
void *notify_thread_sub(void *arg)
{
	unsigned thread_sleep = 1;
	struct tm *ntime;
	time_t ntimer;
	char *time_format = "%T";
	char *date_format = "%a %F";

	for (;;) {
		ntimer = time(NULL);
		ntime = localtime(&ntimer);
		pthread_mutex_lock(&notify->mut);
		strftime(notify->time, NOTIFY_FIELD_LENGTH, time_format, ntime);
		strftime(notify->date, NOTIFY_FIELD_LENGTH, date_format, ntime);
		notify_update_bar();
		pthread_mutex_unlock(&notify->mut);
		sleep(thread_sleep);
	}
	pthread_exit((void*) 0);
}