aboutsummaryrefslogtreecommitdiffstats
path: root/src/calcurse.c
blob: 63466a5b1f766df2c57716d12360074261f3fc8a (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
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
/*	$calcurse: calcurse.c,v 1.53 2007/08/04 14:34:03 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
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /* HAVE_CONFIG_H */

#include <stdlib.h>
#include <signal.h>

#include "i18n.h"
#include "io.h"
#include "help.h"
#include "custom.h"
#include "utils.h"
#include "sigs.h"
#include "day.h"
#include "todo.h"
#include "args.h"
#include "notify.h"


/*
 * Calcurse  is  a text-based personal organizer which helps keeping track
 * of events and everyday tasks. It contains a calendar, a 'todo' list,
 * and puts your appointments in order. The user interface is configurable,
 * and one can choose between different color schemes and layouts. 
 * All of the commands are documented within an online help system.
 */
int 
main(int argc, char **argv)
{
	conf_t conf;
	window_t win[NBWINS]; 
	day_items_nb_t inday;
	int ch, background, foreground;
	int non_interactive;
	int no_data_file = 1;
	int first_todo_onscreen = 1;
	int hilt_app = 0;
	int hilt_tod = 0;
	int nb_tod = 0;
	int sav_hilt_app = 0;
	int sav_hilt_tod = 0;
	struct sigaction sigact;
	bool do_storage = false;
	bool day_changed = false;
	char *saved_t_mesg;
        char *no_color_support = 
            _("Sorry, colors are not supported by your terminal\n"
            "(Press [ENTER] to continue)");
	char *quit_message = _("Do you really want to quit ?");
	char choices[] = "[y/n] ";

#if ENABLE_NLS
        setlocale (LC_ALL, "");
        bindtextdomain (PACKAGE, LOCALEDIR);
        textdomain (PACKAGE);
#endif /* ENABLE_NLS */
		
	/* Thread-safe data structure init */
	apoint_llist_init();
	recur_apoint_llist_init();

	/* 
	 * Begin by parsing and handling command line arguments.
	 * The data path is also initialized here.
	 */
	non_interactive = parse_args(argc, argv, &conf);
	if (non_interactive) 
		return (EXIT_SUCCESS);

	/* Begin of interactive mode with ncurses interface. */
	sigs_init(&sigact);		/* signal handling init */
	initscr();			/* start the curses mode */
	cbreak();			/* control chars generate a signal */
	noecho();			/* controls echoing of typed chars */
	curs_set(0);			/* make cursor invisible */
        calendar_set_current_date();
	notify_init_vars();
	wins_get_config(&conf, &win[STATUS], &win[NOTIFY], &win[APPOINTMENT],
	    &win[TODO], &win[CALENDAR]);
	
        /* Check if terminal supports color. */
	if (has_colors()) {
                colorize = true;
		background = COLOR_BLACK;
		foreground = COLOR_WHITE;
		start_color();

#ifdef NCURSES_VERSION
		if (use_default_colors() != ERR) {
			background = -1;
			foreground = -1;
		}
#endif /* NCURSES_VERSION */

		/* Color assignment */
		init_pair(COLR_RED, COLOR_RED, background);
		init_pair(COLR_GREEN, COLOR_GREEN, background);
		init_pair(COLR_YELLOW, COLOR_YELLOW, background);
		init_pair(COLR_BLUE, COLOR_BLUE, background);
		init_pair(COLR_MAGENTA, COLOR_MAGENTA, background);
		init_pair(COLR_CYAN, COLOR_CYAN, background);
		init_pair(COLR_DEFAULT, foreground, background);
		init_pair(COLR_HIGH, COLOR_BLACK, COLOR_GREEN);
		init_pair(COLR_CUSTOM, COLOR_RED, background);

	} else {
                colorize = false;
		background = COLOR_BLACK;
	}

	vars_init(&conf);
	wins_init(&win[CALENDAR], &win[APPOINTMENT], &win[TODO], &win[STATUS]);
	wins_slctd_init();
	notify_init_bar(&win[NOTIFY]);
	reset_status_page();

	/* 
	 * Read the data from files : first the user
	 * configuration (the display is then updated), and then
	 * the todo list, appointments and events.
	 */
	no_data_file = io_check_data_files();
	custom_load_conf(&conf, background);
	erase_status_bar();
	nb_tod = io_load_todo();	
	io_load_app();
	if (notify_bar()) {
		notify_start_main_thread();
		notify_check_next_app();
	}
	wins_get_config(&conf, &win[STATUS], &win[NOTIFY], &win[APPOINTMENT],
	    &win[TODO], &win[CALENDAR]);
        wins_reinit(&conf, &win[STATUS], &win[APPOINTMENT], 
	    &win[TODO], &win[CALENDAR], &win[NOTIFY]);
	wins_update(&conf, &win[STATUS], &win[APPOINTMENT], &win[TODO], 
	    hilt_app, hilt_tod, nb_tod, first_todo_onscreen, &saved_t_mesg);
        io_startup_screen(conf.skip_system_dialogs, no_data_file);
	inday = *day_process_storage(0, day_changed, &inday);
	wins_slctd_set(CALENDAR);
	wins_update(&conf, &win[STATUS], &win[APPOINTMENT], &win[TODO], 
	    hilt_app, hilt_tod, nb_tod, first_todo_onscreen, &saved_t_mesg);
	calendar_start_date_thread();

	/* User input */
	for (;;) {
		
		/* Check terminal size. */
		getmaxyx(stdscr, row, col);
		if ((col < 80) | (row < 24)) {
		        endwin();
			fputs(_("Please resize your terminal screen\n"
				"(to at least 80x24),\n"
				"and restart calcurse.\n"), stderr);
			return (EXIT_FAILURE);
		}

		/* Get user input. */
		ch = wgetch(swin);
		switch (ch) {

		case 9:	/* The TAB key was hit. */
			reset_status_page();
			/* Save previously highlighted event. */
			switch (wins_slctd()) {
			case TODO:
				sav_hilt_tod = hilt_tod;
				hilt_tod = 0;
				break;
			case APPOINTMENT:
				sav_hilt_app = hilt_app;
				hilt_app = 0;
				break;
			default:
				break;
			}
			wins_slctd_next();

			/* Select the event to highlight. */
			switch (wins_slctd()) {
			case TODO:
				if ((sav_hilt_tod == 0) && (nb_tod != 0))
					hilt_tod = 1;
				else
					hilt_tod = sav_hilt_tod;
				break;
			case APPOINTMENT:
				if ((sav_hilt_app == 0) && 
				    ((inday.nb_events + inday.nb_apoints) != 0))
					hilt_app = 1;
				else
					hilt_app = sav_hilt_app;
				break;
			default:
				break;
			}
			break;

		case CTRL('R'):
                        wins_reinit(&conf, &win[STATUS], 
			    &win[APPOINTMENT], &win[TODO], &win[CALENDAR], 
			    &win[NOTIFY]);
			wins_update(&conf, &win[STATUS], &win[APPOINTMENT], 
			    &win[TODO], hilt_app, hilt_tod, 
			    nb_tod, first_todo_onscreen, &saved_t_mesg);
			do_storage = true;
			break;

		case 'O':
		case 'o':
			other_status_page(wins_slctd());
			break;

		case 'G':
		case 'g':	/* Goto function */
			erase_status_bar();
			calendar_set_current_date();
			calendar_change_day();
			do_storage = true;
			day_changed = true;
			break;

		case 'V':
		case 'v':	/* View function */
			if ((wins_slctd() == APPOINTMENT) && (hilt_app != 0))
				day_popup_item();
			else if ((wins_slctd() == TODO) && (hilt_tod != 0)) 
				item_in_popup(NULL, NULL, saved_t_mesg,
						_("To do :"));
			break;

		case 'C':
		case 'c':	/* Configuration menu */
			erase_status_bar();
			config_bar();
			while ((ch = wgetch(swin)) != 'q') {
				switch (ch) {
				case 'C':
				case 'c':
                                        if (has_colors()) {
                                                custom_color_config(
						    notify_bar()); 
                                        } else {
                                                colorize = false;
						erase_status_bar();
                                                mvwprintw(swin, 0, 0, 
                                                          _(no_color_support));
                                                wgetch(swin);
                                        }
					break;
				case 'L':
				case 'l':
					conf.layout = 
					    layout_config(conf.layout);
					break;
				case 'G':
				case 'g':
					custom_general_config(&conf);
					break;
				case 'N':
				case 'n':
					notify_config_bar();
					break;
				}
                                wins_reinit(&conf, &win[STATUS], 
				    &win[APPOINTMENT], &win[TODO], 
				    &win[CALENDAR], &win[NOTIFY]);
				wins_update(&conf, &win[STATUS], 
				    &win[APPOINTMENT], &win[TODO], hilt_app,
				    hilt_tod, nb_tod, 
				    first_todo_onscreen, &saved_t_mesg);
				do_storage = true;
				erase_status_bar();
				config_bar();
			}
                        wins_update(&conf, &win[STATUS], &win[APPOINTMENT], 
			    &win[TODO], hilt_app, hilt_tod, nb_tod, 
			    first_todo_onscreen, &saved_t_mesg);
			break;

		case CTRL('A'):	/* Add an app, whatever panel selected */
			apoint_add(&hilt_app);
			do_storage = true;
			break;

		case CTRL('T'):	/* Add a todo, whatever panel selected */
			nb_tod = todo_new_item(nb_tod);
			if (hilt_tod == 0 && nb_tod == 1)
				hilt_tod++;
			break;

		case 'A':
		case 'a':	/* Add an item */
			switch (wins_slctd()) {
			case APPOINTMENT:
				apoint_add(&hilt_app);
				do_storage = true;
				break;
			case TODO:
				nb_tod = todo_new_item(nb_tod);
				if (hilt_tod == 0 && nb_tod == 1)
					hilt_tod++;
				break;
			default:
				break;
			}
			break;

		case 'E':
		case 'e':	/* Edit an existing item */
			if (wins_slctd() == APPOINTMENT && hilt_app != 0)
				day_edit_item(hilt_app);
			else if (wins_slctd() == TODO && hilt_tod != 0)
				todo_edit_item(hilt_tod);
			do_storage = true;
			break;

		case 'D':
		case 'd':	/* Delete an item */
			if (wins_slctd() == APPOINTMENT && hilt_app != 0)
				apoint_delete(&conf, &inday.nb_events, 
				    &inday.nb_apoints, &hilt_app);
			else if (wins_slctd() == TODO && hilt_tod != 0)
				todo_delete(&conf, &nb_tod, &hilt_tod);
			do_storage = true;
			break;

		case 'R':
		case 'r':
			if (wins_slctd() == APPOINTMENT && hilt_app != 0)
				recur_repeat_item(hilt_app);
				do_storage = true;
			break;

		case '!':
			if (wins_slctd() == APPOINTMENT && hilt_app != 0)
				apoint_switch_notify(hilt_app);
				do_storage = true;
			break;
	
		case '+':
		case '-':
			if (wins_slctd() == TODO && hilt_tod != 0) {
				hilt_tod = todo_chg_priority(ch, hilt_tod);
				if (hilt_tod < first_todo_onscreen)
					first_todo_onscreen = hilt_tod;
				else if (hilt_tod - first_todo_onscreen >=
				    win[TODO].h - 4)
					first_todo_onscreen = hilt_tod 
					    - win[TODO].h + 5;	
			}
			break;

		case '?':	/* Online help system */
			status_bar();
			help_screen();
			break;

		case 'S':
		case 's':	/* Save function */
			io_save_cal(&conf); 
			break;

		case 'X':
		case 'x':	/* Export function */
			io_export_data(IO_EXPORT_INTERACTIVE, &conf);
			break;

		case (261):	/* right arrow */
		case ('L'):
		case ('l'):
		case CTRL('L'):
			if (wins_slctd() == CALENDAR || ch == CTRL('L')) {
				do_storage = true;
				day_changed = true;
				calendar_move_right();
			}
			break;

		case (260):	/* left arrow */
		case ('H'):
		case ('h'):
		case CTRL('H'):
			if (wins_slctd() == CALENDAR || ch == CTRL('H')) {
				do_storage = true;
				day_changed = true;
				calendar_move_left();
			}
			break;

		case (259):	/* up arrow */
		case ('K'):
		case ('k'):
		case CTRL('K'):
			if (wins_slctd() == CALENDAR || ch == CTRL('K')) {
				do_storage = true;
				day_changed = true;
				calendar_move_up();
			} else {
				if ((wins_slctd() == APPOINTMENT) && 
				    (hilt_app > 1)) {
					hilt_app--;
					scroll_pad_up(hilt_app, 
		    			    inday.nb_events); 
				} else if ((wins_slctd() == TODO) && 
				    (hilt_tod > 1)) {
					hilt_tod--;
					if (hilt_tod < first_todo_onscreen)
						first_todo_onscreen--;
				}
			}
			break;

		case (258):	/* down arrow */
		case ('J'):
		case ('j'):
		case CTRL('J'):
			if (wins_slctd() == CALENDAR || ch == CTRL('J')) {
				do_storage = true;
				day_changed = true;
				calendar_move_down();
			} else {
				if ((wins_slctd() == APPOINTMENT) && 
				    (hilt_app < inday.nb_events + 
				    inday.nb_apoints)) {
					hilt_app++;
					scroll_pad_down(hilt_app, 
					    inday.nb_events, 
					    win[APPOINTMENT].h);
				}
				if ((wins_slctd() == TODO) && 
				    (hilt_tod < nb_tod)) {
					++hilt_tod;
					if (hilt_tod - first_todo_onscreen ==
					    win[TODO].h - 4)
						first_todo_onscreen++;
				}
			}
			break;

		case ('Q'):	/* Quit calcurse :( */
		case ('q'):
			if (conf.auto_save)
				io_save_cal(&conf);

			if (conf.confirm_quit) {
				status_mesg(_(quit_message), choices);
				ch = wgetch(swin);
				if ( ch == 'y' )
					exit_calcurse(EXIT_SUCCESS);
				else {
					erase_status_bar();
					break;
				}
			} else
				exit_calcurse(EXIT_SUCCESS);
			break;
		}

		if (do_storage) {
			inday = *day_process_storage(calendar_get_slctd_day(),
			    day_changed, &inday);
			do_storage = !do_storage;
			if (day_changed) {
				sav_hilt_app = 0;
				day_changed = !day_changed;
				if ((wins_slctd() == APPOINTMENT) && 
				    (inday.nb_events + inday.nb_apoints != 0))
					hilt_app = 1;
			}
		}

		wins_update(&conf, &win[STATUS], &win[APPOINTMENT], 
		    &win[TODO], hilt_app, hilt_tod, nb_tod, 
		    first_todo_onscreen, &saved_t_mesg);
	}
}