File: MessageBox.cpp

package info (click to toggle)
spring 106.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 55,316 kB
  • sloc: cpp: 543,954; ansic: 44,800; python: 12,575; java: 12,201; awk: 5,889; sh: 1,796; asm: 1,546; xml: 655; perl: 405; php: 211; objc: 194; makefile: 76; sed: 2
file content (126 lines) | stat: -rw-r--r-- 3,332 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
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sys/wait.h>
#include <unistd.h>

#include "System/Platform/MessageBox.h"
#include "System/Platform/errorhandler.h"

namespace Platform {

/**
 * @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 MsgBox(const char* message, const char* caption, unsigned int flags)
{
	char cap[1024];
	char msg[1024];
	pid_t pid;
	int status;
	int len;

	strncpy(cap, caption, sizeof(cap) - 2);
	strncpy(msg, message, sizeof(msg) - 2);

	cap[sizeof(cap) - 2] = 0;
	msg[sizeof(msg) - 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.
	 */

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

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

	// fork a child
	switch (pid = fork()) {
		case 0: {
			// child process
			const char* kde   = getenv("KDE_FULL_SESSION");
			const char* gnome = getenv("GNOME_DESKTOP_SESSION_ID");
			const char* type = "--error";

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

			// if execution reaches here, it means execlp failed
			_exit(EXIT_FAILURE);
		} break;

		default: {
			// parent process
			waitpid(pid, &status, 0);

			const bool okButton = (!WIFEXITED(status) || (WEXITSTATUS(status) != 0));

			if (!okButton)
				break;
		}

		case -1: {
			// fork error
			if (flags & MBF_INFO) {
				fputs("Info: ", stderr);
			} else if (flags & MBF_EXCL) {
				fputs("Warning: ", stderr);
			} else {
				fputs("Error: ", stderr);
			}
			fputs(cap, stderr);
			fputs("  ", stderr);
			fputs(msg, stderr);
		} break;
	}
}

} //namespace Platform