File: X_MessageBox.cpp

package info (click to toggle)
spring 0.81.2.1%2Bdfsg1-6
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 28,496 kB
  • ctags: 37,096
  • sloc: cpp: 238,659; ansic: 13,784; java: 12,175; awk: 3,428; python: 1,159; xml: 738; perl: 405; sh: 297; makefile: 267; pascal: 228; objc: 192
file content (116 lines) | stat: -rw-r--r-- 3,317 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
/**
 * @file X_MessageBox.cpp
 * @brief X_MessageBox function
 * @author Tobi Vollebregt
 *
 * Defines the X_MessageBox() function.
 *
 * Copyright (C) 2006.  Licensed under the terms of the
 * GNU GPL, v2 or later
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>

#include "Platform/errorhandler.h"

/**
 * @brief X message box function
 *
 * X clone of the Windows MessageBox() function, using the commandline
 * dialog creation tools zenity, kdialog or xmessage as a workhorse.
 *
 * If DESKTOP_SESSION is set to "gnome" or "kde", it attempts to use
 * zenity (Gnome) or kdialog (KDE).
 *
 * If DESKTOP_SESSION is not set, or if executing zenity or kdialog failed,
 * it attempts to use the generic (but ugly!) xmessage.
 *
 * As a last resort (if forking failed, none of the above mentioned programs
 * could be found, or the used program returned an error) the message is
 * written to stderr.
 */
void X_MessageBox(const char *msg, const char *caption, unsigned int flags)
{
	char caption2[100];
	char msg2[1000];
	pid_t pid;
	int status, len;

	strncpy(caption2, caption, sizeof(caption2) - 2);
	strncpy(msg2, msg, sizeof(msg2) - 2);

	caption2[sizeof(caption2) - 2] = 0;
	msg2[sizeof(msg2) - 2] = 0;

   /* xmessage interprets some strings beginning with '-' as command-line
	* options. To avoid this we make sure all strings we pass to it have
	* newlines on the end. This is also useful for the fputs() case.
	*
	* kdialog and zenity always treat the argument after --error or --text
	* as a non-option argument.
    */

	len = strlen(caption2);
	if (len == 0 || caption2[len - 1] != '\n')
		strcat(caption2, "\n");

	len = strlen(msg2);
	if (len == 0 || msg2[len - 1] != '\n')
		strcat(msg2, "\n");

	/* fork a child */
	pid = fork();
	switch (pid) {

		case 0: /* child process */
		{
			const char* session = getenv("DESKTOP_SESSION");
			const char* type = "--error";

			if (session && !strcasecmp(session, "gnome")) {
				// --warning shows 2 buttons, so it should only be used with a true _warning_
				// ie. one which allows you to continue/cancel execution
// 				if (flags & MBF_EXCL) type = "--warning";
				if (flags & MBF_INFO) type = "--info";
				execlp("zenity", "zenity", "--title", caption2, type, "--text", msg2, (char*)NULL);
			}
			if (session && (strstr(session, "kde") || strstr(session, "KDE"))) {
				if (flags & MBF_EXCL) type = "--sorry";
				if (flags & MBF_INFO) type = "--msgbox";
				execlp("kdialog", "kdialog", "--title", caption2, type, msg2, (char*)NULL);
			}
			execlp("xmessage", "xmessage", "-title", caption2, "-buttons", "OK:0", "-default", "OK", "-center", msg2, (char*)NULL);

			/* if execution reaches here, it means execlp failed */
			_exit(EXIT_FAILURE);
			break;
		}
		default: /* parent process */
		{
			waitpid(pid, &status, 0);
			if ((!WIFEXITED(status)) || (WEXITSTATUS(status) != 0)) /* ok button */
			{

			case -1: /* fork error */

				/* I kept this basically the same as the original
				console-only error reporting. */
				if (flags & MBF_INFO)
					fputs("Info: ", stderr);
				else if (flags & MBF_EXCL)
					fputs("Warning: ", stderr);
				else
					fputs("Error: ", stderr);
				fputs(caption2, stderr);
				fputs("  ", stderr);
				fputs(msg2, stderr);
			}

			break;
		}
	}
}