File: popup.c

package info (click to toggle)
plan 1.10.1-2
  • links: PTS
  • area: main
  • in suites: squeeze, wheezy
  • size: 2,228 kB
  • ctags: 1,995
  • sloc: ansic: 25,050; perl: 1,361; sh: 1,003; makefile: 528; yacc: 121; sed: 17
file content (363 lines) | stat: -rw-r--r-- 9,935 bytes parent folder | download | duplicates (3)
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
/*
 * Various popups, such as the About... dialog.
 *
 *	create_about_popup()
 *					Create About info popup
 *	create_error_popup(widget, error, fmt, ...)
 *					Create error popup with Unix error
 *	create_nodaemon_popup()
 *					Create no-daemon warning popup
 *	create_multiple_popup()
 *					Create multiple-plan's warning popup
 *	multiple_writer_warning_popup()
 *					Create popup with write warning
 */

#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include <string.h>
#include <stdarg.h>
#include <sys/types.h>
#if defined(__EMX__) || defined(LINUX) || defined(MACOSX)
#include <sys/wait.h>
#endif
#include <Xm/Xm.h>
#include <Xm/MessageB.h>
#include <Xm/Protocols.h>
#include "cal.h"
#include "version.h"

#define NODAEMON_ONCE		/* if defined, the error popup that offers */
				/* to start pland comes up only once. */
static Boolean suppress_daemon_warning = False;

#ifndef __DATE__
#define __DATE__ ""
#endif

extern char		*progname;	/* argv[0] */
extern Display		*display;	/* everybody uses the same server */
extern Widget		mainwindow;	/* popup menus hang off main window */
extern XtAppContext	app;		/* application handle */
extern BOOL		interactive;	/* interactive or fast appt entry? */
extern BOOL		startup_lock(BOOL, BOOL);


/*---------------------------------------------------------- about ----------*/
static char about_message[] = "\
\n\
Day Planner version %s\n\
Compiled %s\n\n\
Author: Thomas Driemeyer\n\
<thomas@bitrot.de>\n\
http://www.bitrot.de\n\n\
The sources are available at\n\
ftp://ftp.bitrot.de and\n\
ftp://plan.ftp.fu-berlin.de\
\n";


void create_about_popup(void)
{
	char		msg[512];
	Widget		dialog;
	XmString	s;
	Arg		args[10];
	int		n;

	sprintf(msg, _(about_message), VERSION JAPANVERSION, __DATE__);
	s = XmStringCreateLtoR(msg, XmSTRING_DEFAULT_CHARSET);
	n = 0;
	XtSetArg(args[n], XmNmessageString,	s);			n++;
	dialog = XmCreateInformationDialog(mainwindow, _("About"), args, n);
	XmStringFree(s);
	XtUnmanageChild(XmMessageBoxGetChild(dialog, XmDIALOG_HELP_BUTTON));
	XtUnmanageChild(XmMessageBoxGetChild(dialog, XmDIALOG_CANCEL_BUTTON));
	(void)XmInternAtom(display, "WM_DELETE_WINDOW", False);
	XtManageChild(dialog);
}


/*---------------------------------------------------------- errors ---------*/
void create_error_popup(
	Widget		widget,
	UNUSED int	error,
	char		*fmt, ...)
{
	va_list		parm;
	char		msg[10240];
	Widget		dialog;
	XmString	string;
	Arg		args;

	if (!widget)
		widget = mainwindow;
	strcpy(msg, _("ERROR:\n\n"));

	va_start(parm, fmt);
	vsprintf(msg + strlen(msg), fmt, parm);
	va_end(parm);
#	if defined(sgi) || defined(_sgi)
	if (error) {
		strcat(msg, "\n");
		strcat(msg, sys_errlist[error]);
	}
#	endif
	if (!interactive || !widget) {
		fputs(msg, stderr);
		return;
	}
	string = XmStringCreateLtoR(msg, XmSTRING_DEFAULT_CHARSET);
	XtSetArg(args, XmNmessageString, string);
	dialog = XmCreateWarningDialog(widget, _("Error"), &args, 1);
	XmStringFree(string);
	XtUnmanageChild(XmMessageBoxGetChild(dialog, XmDIALOG_CANCEL_BUTTON));
	XtUnmanageChild(XmMessageBoxGetChild(dialog, XmDIALOG_HELP_BUTTON));
	(void)XmInternAtom(display, "WM_DELETE_WINDOW", False);
	XtManageChild(dialog);
}


/*---------------------------------------------------------- no daemon ------*/
static char nodaemon_message[] = "\
Warning:\n\
There is no Scheduler daemon. Without a daemon,\n\
no action will be taken when an alarm triggers.\n\
Please start \"%s\" in your ~/.xsession or\n\
~/.sgisession file, or run plan with the -S option.";

void run_daemon(Widget dialog);
static void cancel_callback(Widget dialog);
static BOOL running;

void create_nodaemon_popup(void)
{
	char		msg[512];
	Widget		dialog;
	XmString	s1, s2, s3;
	Arg		args[10];
	int		n;

	if (running)
		return;
	running = TRUE;
#ifdef NODAEMON_ONCE
	if (suppress_daemon_warning)
		return;
#endif
	sprintf(msg, _(nodaemon_message), DAEMON_FN);
	s1 = XmStringCreateLtoR(msg, XmSTRING_DEFAULT_CHARSET);
	s2 = XmStringCreateSimple(_("Start daemon now"));
	s3 = XmStringCreateSimple(_("Continue without daemon"));
	n = 0;
	XtSetArg(args[n], XmNmessageString,	s1);		n++;
	XtSetArg(args[n], XmNokLabelString,	s2);		n++;
	XtSetArg(args[n], XmNcancelLabelString,	s3);		n++;
	dialog = XmCreateWarningDialog(mainwindow, _("Warning"), args, n);
	XmStringFree(s1);
	XmStringFree(s2);
	XmStringFree(s3);
	XtAddCallback(dialog, XmNokCallback, (XtCallbackProc)
			run_daemon, (XtPointer)NULL);
	XtAddCallback(dialog, XmNcancelCallback, (XtCallbackProc)
			cancel_callback, (XtPointer)NULL);
	XtUnmanageChild(XmMessageBoxGetChild(dialog, XmDIALOG_HELP_BUTTON));
	(void)XmInternAtom(display, "WM_DELETE_WINDOW", False);
	XtManageChild(dialog);
}


/*
 * start the daemon. Nohup the daemon process so that it survives the death
 * of its parent. Also used by the -S autostart option. pland immediately
 * backgrounds itself, so the main process must call wait() to avoid zombies.
 */

/*ARGSUSED*/
void run_daemon(
	UNUSED Widget	dialog)		/* not used */
{
	char		path[1024];
	PID_T		pid;

	running = FALSE;
	if (!find_file(path, DAEMON_FN, TRUE)) {
		fprintf(stderr, _("%s: WARNING: can't find %s\n"),
							progname, DAEMON_FN);
		create_nodaemon_popup();
		return;
	}
	pid = fork();
	if (pid > 0) {
		wait(0);
		return;
	}
	if (pid == 0) {
		fprintf(stderr, _("%s: starting %s\n"), progname, DAEMON_FN);
#if defined(BSD) || defined(MIPS)
		setpgrp(0, 0);
#else
#ifdef __EMX__

#else
		setsid();
#endif
#endif
		detach_from_network();
		execl(path, DAEMON_FN, (char *)0);
	}
	fprintf(stderr, _("%s: WARNING: can't start %s: "), progname,
								DAEMON_FN);
	perror("");
	create_nodaemon_popup();
}


/*
 * don't start the daemon, and set the flag that prevents the popup in the
 * future.
 */

/*ARGSUSED*/
static void cancel_callback(
	UNUSED Widget	dialog)
{
	running = FALSE;
	suppress_daemon_warning = True;
}


/*---------------------------------------------------------- multiple plan's */
static char multiple_message[] = "\
Warning:\n\
Another %s program is running.\n\
%s\n\
Press Continue to start up anyway, or Kill to\n\
attempt to kill the other program. Continuing\n\
may cause loss of new appointment entries, and\n\
command-line entry of appointments will not work.";

static void kill_callback(Widget, XtPointer, XtPointer);
static void cont_callback(Widget, XtPointer, XtPointer);
static BOOL cont;

void create_multiple_popup(
	BOOL		nolock)		/* tried to kill competitor */
{
	char		msg[512];
	Widget		dialog;
	XmString	s1, s2, s3;
	Arg		args[10];
	int		n;

	cont = FALSE;
	sprintf(msg, _(multiple_message), progname,
		nolock ? _("The other program could not be killed.\n") : "");
	s1 = XmStringCreateLtoR(msg, XmSTRING_DEFAULT_CHARSET);
	s2 = XmStringCreateSimple(_("Kill"));
	s3 = XmStringCreateSimple(_("Continue"));
	n = 0;
	XtSetArg(args[n], XmNmessageString,	s1);		n++;
	XtSetArg(args[n], XmNokLabelString,	s2);		n++;
	XtSetArg(args[n], XmNcancelLabelString,	s3);		n++;
	XtSetArg(args[n], XmNdialogStyle,
			  XmDIALOG_FULL_APPLICATION_MODAL);	n++;
	dialog = XmCreateWarningDialog(mainwindow, _("Warning"), args, n);
	XmStringFree(s1);
	XmStringFree(s2);
	XmStringFree(s3);
	XtAddCallback(dialog, XmNokCallback, (XtCallbackProc)
			kill_callback, (XtPointer)NULL);
	XtAddCallback(dialog, XmNcancelCallback, (XtCallbackProc)
			cont_callback, (XtPointer)NULL);
	XtUnmanageChild(XmMessageBoxGetChild(dialog, XmDIALOG_HELP_BUTTON));
	(void)XmInternAtom(display, "WM_DELETE_WINDOW", False);
	XtManageChild(dialog);
	while (!cont) {
		XEvent event;
		XtAppNextEvent(app, &event);
		XtDispatchEvent(&event);
	}
}


/*
 * The user pressed Kill. Try to acquire a lock, then let
 * create_multiple_popup continue so that the main plan window can come up.
 */

/*ARGSUSED*/
static void kill_callback(
	UNUSED Widget	 dialog,
	UNUSED XtPointer a,
	UNUSED XtPointer b)
{
	if (!startup_lock(FALSE, TRUE))
		create_multiple_popup(TRUE);
	cont = TRUE;
}


/*
 * The user pressed Continue. Let create_multiple_popup continue so that the
 * main plan window can come up.
 */

/*ARGSUSED*/
static void cont_callback(
	UNUSED Widget	 dialog,
	UNUSED XtPointer a,
	UNUSED XtPointer b)
{
	cont = TRUE;
}


/*---------------------------------------------------------- multiple wr ----*/
void multiple_writer_warning_popup(
	Widget		widget)		/* install near this widget */
{
	Widget		dialog;
	XmString	string;
	Arg		args[2];

	string = XmStringCreateLtoR(
_("WARNING:\n\n\
You can now edit other users' appointments.\n\
plan will normally notice multiple writes at the same\n\
time (except over NFS), but will not notice if another\n\
user has modified the same appointment file while you\n\
were changing it. The older changes will get lost in\n\
this case. Watch out!)"), XmSTRING_DEFAULT_CHARSET);
	XtSetArg(args[0], XmNmessageString, string);
	XtSetArg(args[1], XmNdialogStyle, XmDIALOG_FULL_APPLICATION_MODAL);
	dialog = XmCreateWarningDialog(widget, _("Error"), args, 2);
	XmStringFree(string);
	XtUnmanageChild(XmMessageBoxGetChild(dialog, XmDIALOG_CANCEL_BUTTON));
	XtUnmanageChild(XmMessageBoxGetChild(dialog, XmDIALOG_HELP_BUTTON));
	(void)XmInternAtom(display, "WM_DELETE_WINDOW", False);
	XtManageChild(dialog);
}


/*---------------------------------------------------------- multiple wr ----*/
void new_language_popup(
	Widget		widget)		/* install near this widget */
{
	Widget		dialog;
	XmString	string;
	Arg		args[2];

	string = XmStringCreateLtoR(_("The new language will be effective\n"
			"after plan is restarted."), XmSTRING_DEFAULT_CHARSET);
	XtSetArg(args[0], XmNmessageString, string);
	XtSetArg(args[1], XmNdialogStyle, XmDIALOG_FULL_APPLICATION_MODAL);
	dialog = XmCreateWarningDialog(widget, _("Warning"), args, 2);
	XmStringFree(string);
	XtUnmanageChild(XmMessageBoxGetChild(dialog, XmDIALOG_CANCEL_BUTTON));
	XtUnmanageChild(XmMessageBoxGetChild(dialog, XmDIALOG_HELP_BUTTON));
	(void)XmInternAtom(display, "WM_DELETE_WINDOW", False);
	XtManageChild(dialog);
}