File: edit.c

package info (click to toggle)
plan 1.8.7-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,196 kB
  • ctags: 1,971
  • sloc: ansic: 24,392; perl: 1,361; sh: 1,030; makefile: 514; yacc: 120; sed: 17
file content (575 lines) | stat: -rw-r--r-- 15,842 bytes parent folder | download
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
/*
 * Handle all the more complicated user interactions with an entry list menu.
 * Whenever the user begins editing a row, the entry in that row is copied
 * to the edit struct. All editing takes place, until it is confirmed and
 * put back into the main list. The reason for this is that entering a new
 * date or time doesn't make the row disappear from the current list because
 * it now belongs to a different day before editing is finished.
 *
 *	got_entry_press(w,lw,x,y,on,b)	The user pressed on one of the
 *					buttons in the list menu button array.
 *	confirm_new_entry()		Puts the currently edited row back
 *					into the mainlist, and re-sorts.
 *	undo_new_entry()		Kill the edit buffer, undoing the
 *					changes to the current row.
 *	got_entry_text(lw, x, y, text)	The user entered text into the text
 *					button. There is never more than one.
 *	sensitize_edit_buttons()	Desensitize all buttons in the
 *					bottom button row that don't make
 *					sense at the moment.
 */

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <Xm/Xm.h>
#include <Xm/Form.h>
#include <Xm/RowColumn.h>
#include <Xm/LabelP.h>
#include <Xm/LabelG.h>
#include <Xm/PushBP.h>
#include <Xm/PushBG.h>
#include <Xm/ToggleB.h>
#include <Xm/Protocols.h>
#include "cal.h"

extern Display		*display;	/* everybody uses the same server */
extern Pixel		color[NCOLS];	/* colors: COL_* */
extern struct config	config;		/* global configuration data */
extern struct plist	*mainlist;	/* list of all schedule entries */
extern Widget		mainwindow;	/* popup menus hang off main window */
extern struct user	*user;		/* user list (from file_r.c) */
extern BOOL		interactive;	/* interactive or fast appt entry? */
extern int		curr_month;	/* month being displayed, 0..11 */
extern int		curr_year;	/* year being displayed, since 1900 */
extern time_t		last_x_timer;	/* when did last X timer happen? */

struct edit		edit;		/* info about entry being edited */


/*
 * user selection popup is up, and the user has made a selection. Store the
 * new user number in the edited appointment. The user selection popup had
 * been installed by got_entry_press.
 */

static void got_new_user(
	int	newuser)
{
	int	olduser;

	olduser = name_to_user(edit.entry.user);
	if (!prepare_for_modify(user[olduser].name) ||
	    !prepare_for_modify(user[newuser].name))
		return;
	if (edit.entry.user)
		free(edit.entry.user);
	edit.entry.user = newuser<0 ? 0 : mystrdup(user[newuser].name);
	edit.changed = TRUE;
	edit.editing = TRUE;
	confirm_new_entry();
	update_all_listmenus(TRUE);
	sensitize_edit_buttons();
}


/*
 * user clicked on one of the buttons in the appointment entry menu
 */

void got_entry_press(
	struct listmenu		*lw,		/* which menu */
	int			x,		/* which column/row */
	int			y,
	int			on,		/* enable radio but: on/off */
	int			shift)		/* arrow but: 1=shift key */
{
	register struct entry	*ep;		/* affected entry */
	int			num;		/* number of entries so far */
	time_t			now, temp;	/* current time */
	int			inc;		/* if arrow button, +1 or -1 */
	int			xp = 0;		/* after inc/dec, reprint x */

	destroy_user_sel_popup();
	num = lw->sublist ? lw->sublist->nentries : 0;
	if (y-1 > num)					/* illegal entry? */
		return;
	if (y-1 < num) {
		ep = lw->sublist->entry[y-1];
		if (user[name_to_user(ep->user)].readonly)
			switch(x) {			/* readonly: show */
			  case SC_MESSAGE:
				create_text_popup(_("Message"),
						&ep->message, x, TRUE);
				return;

			  case SC_SCRIPT:
				if (!ep->notime)
					create_text_popup(_("Shell Script"),
						&ep->script, x, TRUE);
				return;
			}
		if (!prepare_for_modify(lw->sublist->entry[y-1]->user)) {
			if (x == SC_ENABLE)
				XtVaSetValues(lw->entry[y][x], XmNset, !on, 0);
			return;
		}
	}
	if (edit.editing && (edit.y != y || edit.menu != lw))
		confirm_new_entry();
	num = lw->sublist ? lw->sublist->nentries : 0;
	inc = x < SC_D ? 1 : -1;
	if (!edit.editing) {
		if (y-1 < num) {
			clone_entry(&edit.entry, lw->sublist->entry[y-1]);
			if (!server_entry_op(&edit.entry, 'l')) {
				create_error_popup(lw->shell, 0, "%s\n%s\n",
					_("Somebody else is editing this "\
					  "entry. Please try again later."));
				update_all_listmenus(FALSE);
				return;
			}
		} else {
			if (x != SC_DATE && x != SC_TIME && x != SC_NOTE) {
				create_error_popup(lw->shell, 0,
					_("Please enter time or date first"));
				update_all_listmenus(FALSE);
				return;
			}
			clone_entry(&edit.entry, (struct entry *)0);
			now = get_time();
			edit.entry.time = lw->time ? lw->time : now - now%60;
			edit.entry.notime = x == SC_NOTE;
			edit.entry.late_warn  = config.late_time;
			edit.entry.early_warn = config.early_time;
		}
		edit.editing	= TRUE;
		edit.changed	= FALSE;
		edit.y		= y;
		edit.menu	= lw;
		edit.srcentry	= 0;
		sensitize_edit_buttons();
	}
	ep = &edit.entry;
	switch(x) {
	  case SC_ENABLE:
		ep->suspended = !on;
		edit.changed = TRUE;
		sensitize_edit_buttons();
		update_all_listmenus(FALSE);
		break;

	  case SC_USER:
		create_user_sel_popup(lw->entry[y][x], name_to_user(ep->user),
						(BOOL (*)())got_new_user);
		break;

	  case SC_TIME:
		if (lw->time && !lw->period &&
				(!lw->sublist || y > lw->sublist->nentries))
			print_button(lw->entry[y][SC_DATE],
				mkdatestring(lw->time ? lw->time : now));
		edit_list_button(1, lw, x, y);
		break;

	  case SC_LENGTH:
		if (ep->notime)
			break;
	  case SC_ENDDATE:
	  case SC_DATE:
	  case SC_ADVDAYS:
	  case SC_NOTE:
		if (x == SC_ENDDATE)
			destroy_recycle_popup();
		edit_list_button(1, lw, x, y);
		break;

	  case SC_ADVANCE:
		if (ep->notime)
			break;
		edit_list_button(1, lw, x, y);
		break;

	  case SC_RECYCLE:
		create_recycle_popup();
		break;

	  case SC_MESSAGE:
		create_text_popup(_("Message"), &edit.entry.message, x, FALSE);
		break;

	  case SC_SCRIPT:
		if (ep->notime)
			break;
		create_text_popup(_("Shell Script"), &edit.entry.script, x,
									FALSE);
		break;

	  case SC_EXCEPT:
		create_except_popup();
		break;

	  case SC_PRIVATE:
		ep->private ^= TRUE;
		got_new_user(0);
		break;

	  case SC_TODO:
		print_pixmap(lw->entry[y][SC_TODO], PIC_TODO, ep->todo^=TRUE);
		edit.changed = TRUE;
		edit.entry.suspended = FALSE;
		sensitize_edit_buttons();
		break;

	  case SC_I_DATE:
	  case SC_D_DATE:
		temp = inc * (shift ? 7 : 1) * 86400;
		ep->time += temp;
		if (ep->rep_last) {
			ep->rep_last += temp;
			print_button(lw->entry[y][SC_ENDDATE],
					mkbuttonstring(lw, SC_ENDDATE, y));
		}
		xp = SC_DATE;
		break;

	  case SC_D_ENDDATE:
	  	if (ep->rep_every != 86400 ||
		    ep->rep_last < ep->time - ep->time % 86400 + 86400)
			break;
	  case SC_I_ENDDATE:
		destroy_recycle_popup();
		if (ep->rep_every != 86400 || !ep->rep_last)
			ep->rep_last = ep->time - ep->time % 86400;
		ep->rep_last += inc * (shift ? 7 : 1) * 86400;
		ep->rep_every = 86400;
	  	if (ep->rep_last < ep->time - ep->time % 86400 + 86400) {
			ep->rep_last  = 0;
			ep->rep_every = 0;
		}
		print_pixmap(lw->entry[y][SC_RECYCLE], PIC_RECYCLE,
					ep->rep_every  || ep->rep_days ||
					ep->rep_yearly || ep->rep_weekdays);
		xp = SC_ENDDATE;
		break;

	  case SC_I_TIME:
	  case SC_D_TIME:
		temp = ep->time % 86400;
		temp += inc * (shift ? 60 : 5) * 60;
		ep->time += temp % 86400 - ep->time % 86400;
		xp = SC_TIME;
		break;

	  case SC_I_LENGTH:
	  case SC_D_LENGTH:
		if (ep->notime)
			break;
	  	ep->length += inc * (shift ? 60 : 5) * 60;
		if (ep->length < 0)
			ep->length = 0;
		if (ep->length > 86400 - ep->time % 86400)
			ep->length = 86400 - ep->time % 86400;
		xp = SC_LENGTH;
		break;

	  case SC_I_ADVANCE:
	  case SC_D_ADVANCE: {
		time_t *warn = shift ? &ep->early_warn : &ep->late_warn;
		*warn += inc * 5 * 60;
		if (*warn < 0)
			*warn = 0;
		if (*warn > ep->time % 86400)
			*warn = ep->time % 86400;
		xp = SC_ADVANCE;
		break; }

	  case SC_I_ADVDAYS:
	  case SC_D_ADVDAYS:
		ep->days_warn += inc * (shift ? 7 : 1) * 86400;
		if (ep->days_warn < 0)
			ep->days_warn = 0;
		xp = SC_ADVDAYS;
		break;
	}
	if (xp) {
		print_button(lw->entry[y][xp], mkbuttonstring(lw, xp, y));
		edit.changed = TRUE;
		edit.entry.suspended = FALSE;
		sensitize_edit_buttons();
	}
}


/*
 * confirm new entry, put it in the list. If the user has changed, mark
 * the old and the new user modified (if non-null), or reclassify as own
 * if old or new user is read-only. If it ends up with a different user,
 * tell the old server to delete and the new server to add.
 * If it's more than 30 seconds since the last timer event, assume it's
 * dead (bug or incompatibility in the XFree server as of June 99) and
 * write synchronously from now on.
 */

void confirm_new_entry(void)
{
	BOOL		changed;
	unsigned int	new_file;
	unsigned int	old_file = edit.entry.file;
	struct user	*u;
	BOOL            addentry = TRUE;

	static char werror_msg[] = "The netplan server reported an error\n"
				   "when this record was committed. Check\n"
				   "access permissions.\n";
	destroy_user_sel_popup();
	edit_list_button(0, edit.menu, 0, 0);
	destroy_text_popup();
	destroy_recycle_popup();
	destroy_except_popup();
	changed = edit.editing && edit.changed;
	if (changed) {
		struct entry *ep = 0;
		mainlist->locked++;
		if (!edit.menu)
			ep = edit.srcentry;
		else if (edit.menu->sublist &&
				edit.y-1 < edit.menu->sublist->nentries)
			ep = edit.menu->sublist->entry[edit.y-1];
		if (!edit.entry.user && user[0].name)
			edit.entry.user = mystrdup(user[0].name);
		u = &user[name_to_user(edit.entry.user)];
		new_file = u->file_id;
		if (edit.entry.private && ep && !ep->private) {	/* private */
			server_entry_op(&edit.entry, 'u');
			server_entry_op(&edit.entry, 'd');
			edit.entry.file = 0;
			edit.entry.id   = 0;

		} else if (!edit.entry.id) {			/* new */
			edit.entry.file = new_file;
			if (u == &user[0] && user[0].readonly) {
				prepare_for_modify(0);
				addentry = FALSE;
			} else if (!server_entry_op(&edit.entry, 'w')) {
				create_error_popup(0, 0, _(werror_msg));
				edit.entry.file = old_file;
			}
		} else if (new_file == edit.entry.file) {	/* same file */
			if (!server_entry_op(&edit.entry, 'w')) {
				create_error_popup(0, 0, _(werror_msg));
				addentry = FALSE;
			}
			server_entry_op(&edit.entry, 'u');
		} else {					/* diff file */
			unsigned int old_id = edit.entry.id;
			server_entry_op(&edit.entry, 'u');
			server_entry_op(&edit.entry, 'd');
			edit.entry.file = new_file;
			edit.entry.id   = 0;
			if (!server_entry_op(&edit.entry, 'w')) {
				create_error_popup(0, 0, _(werror_msg));
				edit.entry.file = old_file;
				edit.entry.id   = old_id;
			}
		}
		if (ep)
			delete_entry(mainlist, ep - mainlist->entry);
		if (addentry) {
			(void)add_entry(&mainlist, &edit.entry);
			rebuild_repeat_chain(mainlist);
		}
		mainlist->locked--;
	}
	destroy_entry(&edit.entry);
	edit.editing = FALSE;
	edit.changed = FALSE;
	if (interactive) {
		sensitize_edit_buttons();
		if (changed) {
			/* new entry vanishes from edit menu if arg is FALSE */
			update_all_listmenus(TRUE);
			redraw_all_views();
			mainlist->modified = TRUE;
			if (last_x_timer != (time_t)-1 &&
			    last_x_timer < get_time()-30) {
				fprintf(stderr, "plan: X server fails to send "
						"timer events, switching to sy"
						"nchronous file writing. Time "
						"displays will be wrong.\n");
				last_x_timer = (time_t)-1;
			}
			if (last_x_timer == (time_t)-1)
				write_mainlist();
		}
	}
}


/*
 * undo all editing, restore the entry before we started editing
 */

void undo_new_entry(void)
{
	if (edit.editing) {
		server_entry_op(&edit.entry, 'u');
		destroy_text_popup();
		destroy_recycle_popup();
		destroy_entry(&edit.entry);
		edit.editing = FALSE;
		edit.changed = FALSE;
		sensitize_edit_buttons();
		draw_list(edit.menu);
	}
}


/*
 * user entered text into one of the buttons in the entry list
 */

void got_entry_text(
	struct listmenu		*lw,		/* which menu */
	int			x,		/* which column/row */
	int			y,
	char			*text)		/* text input by user */
{
	register struct entry	*ep;		/* affected entry */
	register char		*p;		/* to remove trailing \n's */
	time_t			temp;

	while (*text == ' ' || *text == '\t')
		text++;
	ep = &edit.entry;
	switch(x) {
	  case SC_DATE:
		if (*text && (temp = parse_datestring(text, 0)) > EON)
			ep->time = ep->time % 86400 + temp;

		edit_list_button(1, lw, SC_ENDDATE, y);
		print_button(lw->entry[y][SC_DATE],
					mkbuttonstring(lw, SC_DATE, y));
		break;

	  case SC_ENDDATE:
		if (*text && (temp = parse_datestring(text, 0)) > EON) {
			ep->rep_last  = temp;
			ep->rep_every = 86400;

		} else if (ep->rep_last && ep->rep_every == 86400) {
			ep->rep_last  = 0;
			ep->rep_every = 0;
		}
		edit_list_button(1, lw, SC_TIME, y);
		print_button(lw->entry[y][SC_ENDDATE],
					mkbuttonstring(lw, SC_ENDDATE, y));
		break;

	  case SC_TIME:
		if (*text == '-') {
			ep->notime = TRUE;
			ep->time -= ep->time % 86400;
		} else if (*text) {
			ep->notime = FALSE;
			ep->time -= ep->time % 86400;
			ep->time += parse_timestring(text, FALSE);
		}
		edit_list_button(1,lw, ep->notime ? SC_NOTE : SC_LENGTH, y);
		print_button(lw->entry[y][SC_TIME],
					mkbuttonstring(lw, SC_TIME, y));
		print_button(lw->entry[y][SC_LENGTH],
					mkbuttonstring(lw, SC_LENGTH, y));
		print_button(lw->entry[y][SC_ADVANCE],
					mkbuttonstring(lw, SC_ADVANCE, y));
		break;

	  case SC_LENGTH: {
		time_t length;
		length = *text==0   ? 0 :
			 *text=='-' ? parse_timestring(text+1, FALSE) -
								 ep->time%86400
				    : parse_timestring(text,   FALSE);
		if (length < 0)
			length = 0;
		if (length > 86400)
			length = 86400;
		ep->length = length;
		edit_list_button(1, lw, ep->notime ? SC_NOTE:SC_ADVANCE, y);
		print_button(lw->entry[y][SC_LENGTH],
					mkbuttonstring(lw, SC_LENGTH, y));
		break; }

	  case SC_ADVANCE:
		parse_warnstring(text, &ep->early_warn, &ep->late_warn,
							&ep->noalarm);
		edit_list_button(1, lw, SC_ADVDAYS, y);
		print_button(lw->entry[y][SC_ADVANCE],
					mkbuttonstring(lw, SC_ADVANCE, y));
		break;

	  case SC_ADVDAYS:
		ep->days_warn = atoi(text) * 86400;
		if (ep->days_warn < 0)
			ep->days_warn = 0;
		edit_list_button(1, lw, SC_NOTE, y);
		print_button(lw->entry[y][SC_ADVDAYS],
					mkbuttonstring(lw, SC_ADVDAYS, y));
		break;

	  case SC_NOTE:
		if (ep->note)
			free(ep->note);
		ep->note = 0;
		for (p=text+strlen(text)-1; p != text &&
			(*p == '\n' || *p == '\t' || *p == ' '); *p-- = 0);
		for (p=text; *p; p++)
			if (*p == '\n') *p = ' ';
		if (*text)
			ep->note = mystrdup(text);
		edit_list_button(0, lw, 0, 0);
		print_button(lw->entry[y][SC_NOTE], "%s",
					mkbuttonstring(lw, SC_NOTE, y));
		XmProcessTraversal(lw->done, XmTRAVERSE_CURRENT);
	}
	if (x == SC_DATE || x == SC_ENDDATE || x == SC_TIME)
		if (ep->rep_last && ep->rep_last < ep->time) {
			create_error_popup(lw->shell, 0,
				_("The new date is after the last-trigger "\
				  "date,\n %s. The last-trigger date has been"\
				  " disabled."), mkdatestring(ep->rep_last));
			ep->rep_every = 0;
			ep->rep_last  = 0;
		}
	edit.changed = TRUE;
	edit.entry.suspended = FALSE;
#if 1						/* 1=Return in note confirms */
	if (x == SC_NOTE) /* eoln */
		confirm_new_entry();
#endif
	sensitize_edit_buttons();
}


/*
 * while a new entry is being edited, it is kept in the <edit> struct,
 * which overrides the appropriate entry (if any) in the mainlist. If
 * it is being edited, the Confirm button should be sensitive. If it is
 * being edited and has been changed, the Undo button should be sensitive.
 */

void sensitize_edit_buttons(void)
{
	Arg			args;

	if (edit.menu) {
		XtSetArg(args, XmNsensitive, edit.changed);
		XtSetValues(edit.menu->confirm, &args, 1);
		XtSetArg(args, XmNsensitive, edit.changed);
		XtSetValues(edit.menu->undo,    &args, 1);
		XtSetArg(args, XmNsensitive, edit.editing);
		XtSetValues(edit.menu->dup,     &args, 1);
		XtSetArg(args, XmNsensitive, edit.editing);
		XtSetValues(edit.menu->del,     &args, 1);
	}
}