summaryrefslogtreecommitdiff
path: root/src/pmquery/main.cpp
blob: 303761f95f4b85c89d04a6cd60b6ad40cdae2ca1 (plain)
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
/*
 * Copyright (c) 2007, Aconex.  All Rights Reserved.
 * 
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */
#include <QtCore/QFile>
#include <QtCore/QTextStream>
#include <QtGui/QDesktopWidget>
#include <QtGui/QCursor>
#include "pmquery.h"

static char usage[] =
    "Usage: pmquery [options] [message...]\n\n"
    "Options:\n"
    "  -c              center the window on the display\n"
    "  -center         display in the center (alias for -c)\n"
    "  -nearmouse      pop up window near the mouse cursor\n"
    "  -b button       create a button with the label button\n"
    "  -B button       create the default button with the label button\n"
    "  -default button sets named button as the default button\n"
    "  -buttons string comma-separated list of label:exitcode\n"
    "  -h | -? | -help display this usage message\n"
    "  -t string       add string to the message displayed\n"
    "  -file filename  read message from file, \"-\" for stdin\n"
    "  -icon icontype  dialog type: info, error, question, warning, critical,\n"
    "                               host, or archive\n"
    "  -header title   set window title\n"
    "  -useslider      always display a text box slider\n"
    "  -noslider       do not display a text box slider\n"
    "  -noframe        do not display a frame around the text box\n"
    "  -print          print the button label when selected\n"
    "  -noprint        do not print the button label when selected\n"
    "  -timeout secs   exit with status 0 after \"secs\" seconds\n"
    "  -exclusive      do not allow mouse/button presses until dismissed\n";

char *getoption(int argc, char **argv)
{
    static int index;

    if (index >= argc)
	return NULL;
    return argv[++index];
}

char *catoption(char *prefix, char *option, int total)
{
    prefix = (char *)realloc(prefix, total);
    if (prefix) {
	strncat(prefix, " ", 2);
	strncat(prefix, option, total);
    }
    return prefix;
}

char *getoptions(int argc, char **argv, char *arg)
{
    int length = strlen(arg) + 1;
    char *string = (char *)malloc(length);
    strncpy(string, arg, length);
    while (string && (arg = getoption(argc, argv)) != NULL) {
	length += 1 + strlen(arg) + 1;
	string = catoption(string, arg, length);
    }
    if (!string) {
	fputs("Insufficient memory for buffering message\n", stderr);
	exit(1);
    }
    return string;
}

int main(int argc, char ** argv)
{
    char *option;
    char *filename = NULL;
    char *defaultname = NULL;
    int errflag = 0;
    int printflag = 1;
    int inputflag = 0;
    int centerflag = 0;
    int noframeflag = 0;
    int nosliderflag = 0;
    int nearmouseflag = 0;
    int usesliderflag = 0;
    int exclusiveflag = 0;

    QApplication a(argc, argv);

    while ((option = getoption(argc, argv)) != NULL) {
	if (strcmp(option, "-c") == 0 || strcmp(option, "-center") == 0) {
	    centerflag = 1;
	}
	else if (strcmp(option, "-nearmouse") == 0) {
	    nearmouseflag = 1;
	}
	else if (strcmp(option, "-b") == 0) {
	    if ((option = getoption(argc, argv)) == NULL) {
		fprintf(stderr, "The -b option requires an argument\n");
		errflag++;
	    }
	    PmQuery::addButton(option, FALSE, 0);
	}
	else if (strcmp(option, "-B") == 0) {
	    if ((option = getoption(argc, argv)) == NULL) {
		fprintf(stderr, "The -B option requires an argument\n");
		errflag++;
	    }
	    PmQuery::addButton(option, TRUE, 0);
	}
	else if (strcmp(option, "-default") == 0) {
	    if ((option = getoption(argc, argv)) == NULL) {
		fprintf(stderr, "The -default option requires an argument\n");
		errflag++;
	    }
	    else defaultname = option;
	}
	else if (strcmp(option, "-buttons") == 0) {
	    if ((option = getoption(argc, argv)) == NULL) {
		fprintf(stderr, "The -buttons option requires an argument\n");
		errflag++;
	    }
	    PmQuery::addButtons(option);
	}
	else if (strcmp(option, "-t") == 0) {
	    if ((option = getoption(argc, argv)) == NULL) {
		fprintf(stderr, "The -B option requires an argument\n");
		errflag++;
	    }
	    else if (filename) {
		fprintf(stderr, "The -file and -t options are incompatible\n");
		errflag++;
	    }
	    else PmQuery::addMessage(option);
	}
	else if (strcmp(option, "-file") == 0) {
	    if ((option = getoption(argc, argv)) == NULL) {
		fprintf(stderr, "The -file option requires an argument\n");
		errflag++;
	    }
	    else if (PmQuery::messageCount()) {
		fprintf(stderr, "The -file and -t options are incompatible\n");
		errflag++;
	    }
	    else filename = option;
	}
	else if (strcmp(option, "-icon") == 0) {
	    if ((option = getoption(argc, argv)) == NULL) {
		fprintf(stderr, "The -icon option requires an argument\n");
		errflag++;
	    }
	    else if (PmQuery::setIcontype(option) < 0) {
		fprintf(stderr, "Unknown icon type - %s\n", option);
		errflag++;
	    }
	}
	else if (strcmp(option, "-header") == 0) {
	    if ((option = getoption(argc, argv)) == NULL) {
		fprintf(stderr, "The -header option requires an argument\n");
		errflag++;
	    }
	    else PmQuery::setTitle(option);
	}
	else if (strcmp(option, "-input") == 0) {
	    inputflag = 1;
	}
	else if (strcmp(option, "-noframe") == 0) {
	    noframeflag = 1;
	}
	else if (strcmp(option, "-useslider") == 0) {
	    if (nosliderflag) {
		fprintf(stderr,
		    "The -useslider and -noslider options are incompatible\n");
		errflag++;
	    }
	    else usesliderflag = 1;
	}
	else if (strcmp(option, "-noslider") == 0) {
	    if (usesliderflag) {
		fprintf(stderr,
		    "The -useslider and -noslider options are incompatible\n");
		errflag++;
	    }
	    else nosliderflag = 1;
	}
	else if (strcmp(option, "-timeout") == 0) {
	    if ((option = getoption(argc, argv)) == NULL) {
		fprintf(stderr, "The -timeout option requires an argument\n");
		errflag++;
	    }
	    else if (PmQuery::setTimeout(option) < 0) {
		fprintf(stderr, "'%s' is not a positive non-zero timeout\n",
			option);
		errflag++;
	    }
	}
	else if (strcmp(option, "-print") == 0) {
	    printflag = 1;
	}
	else if (strcmp(option, "-noprint") == 0) {
	    printflag = 0;
	}
	else if (strcmp(option, "-exclusive") == 0) {
	    exclusiveflag = 1;
	}
	else if (strcmp(option, "-?") == 0 || strcmp(option, "-help") == 0 ||
		 strcmp(option, "-h") == 0 || strcmp(option, "--help") == 0) {
	    errflag++;
	}
	else {
	    PmQuery::addMessage(getoptions(argc, argv, option));
	}
    }

    if (errflag) {
	fprintf(stderr, "%s", usage);
	exit(1);
    }

    if (defaultname)
	PmQuery::setDefaultButton(defaultname);

    if (filename) {
	QTextStream *stream;
	QFile *file = NULL;
	QString line;

	if (strcmp(filename, "-") == 0)
	    stream = new QTextStream(stdin, QIODevice::ReadOnly);
	else {
	    file = new QFile(filename);
	    if (!file->open(QIODevice::ReadOnly)) {
		fprintf(stderr, "Cannot open %s: %s\n", filename,
			(char *)file->errorString().toAscii().data());
		exit(1);
	    }
	    stream = new QTextStream(file);
	}
	for (;;) {
	    QString line = stream->readLine();
	    if (line.isNull())
		break;
	    if ((option = strdup(line.toAscii())) == NULL) {
		fputs("Insufficient memory reading message stream\n", stderr);
		exit(1);
	    }
	    PmQuery::addMessage(option);
	}
	if (file)
	    delete file;
	delete stream;
    }

    if (!PmQuery::buttonCount())
	PmQuery::addButton("Continue", TRUE, 0);

    PmQuery q(inputflag, printflag, noframeflag,
	      nosliderflag, usesliderflag, exclusiveflag);

    if (nearmouseflag)
	q.move(QCursor::pos());
    else if (centerflag) {
	int x = (a.desktop()->screenGeometry().width() / 2) - (q.width() / 2);
	int y = (a.desktop()->screenGeometry().height() / 2) - (q.height() / 2);
	q.move(x > 0 ? x : 0, y > 0 ? y : 0);
    }

    return q.exec();
}