File: MessageBox.cpp

package info (click to toggle)
spring 103.0%2Bdfsg2-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 43,720 kB
  • ctags: 63,685
  • sloc: cpp: 368,283; ansic: 33,988; python: 12,417; java: 12,203; awk: 5,879; sh: 1,846; xml: 655; perl: 405; php: 211; objc: 194; makefile: 77; sed: 2
file content (129 lines) | stat: -rw-r--r-- 3,538 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
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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 std::string& message, const std::string& caption, const unsigned int& flags)
{
	char caption2[100];
	char msg2[1000];
	pid_t pid;
	int status, len;

	strncpy(caption2, caption.c_str(), sizeof(caption2) - 2);
	strncpy(msg2, message.c_str(), 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* 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", caption2, type, "--text", msg2, (char*)NULL);
			}
			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", 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);
			const bool okButton = (!WIFEXITED(status) || (WEXITSTATUS(status) != 0));
			if (!okButton) {
				break;
			}
		}
		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;
		}
	}
}

} //namespace Platform