main.cpp 2.33 KB
Newer Older
Philipp Erhardt's avatar
initial  
Philipp Erhardt committed
1
2
#include <QApplication>
#include <QString>
3
#include <QProcess>
Philipp Erhardt's avatar
initial  
Philipp Erhardt committed
4
#include <iostream>
5
#include <getopt.h>
Gregor Lang's avatar
Gregor Lang committed
6
#include "download.h"
Philipp Erhardt's avatar
initial  
Philipp Erhardt committed
7
#include "resourcemanager.h"
Philipp Erhardt's avatar
Philipp Erhardt committed
8
#include "viewer.h"
Philipp Erhardt's avatar
Philipp Erhardt committed
9
#include "config.h"
Philipp Erhardt's avatar
initial  
Philipp Erhardt committed
10

11
12
13
14
15
using namespace std;


static void print_help(char *name) {
	cout << "Usage:" << endl;
Philipp Erhardt's avatar
Philipp Erhardt committed
16
	cout << "  " << name << " ([OPTIONS] FILE|(-u URL))*" << endl;
17
18
	cout << endl;
	cout << "Options:" << endl;
Philipp Erhardt's avatar
Philipp Erhardt committed
19
	cout << "  -u, --url           Open a URL instead of a local file" << endl;
20
21
	cout << "  -p, --page NUM      Start showing page NUM" << endl;
	cout << "  -f, --fullscreen    Start in fullscreen mode" << endl;
22
	cout << "  -q, --quit          Quit on initialization failure" << endl;
23
24
	cout << "  -h, --help          Print this help and exit" << endl;
}
Philipp Erhardt's avatar
Philipp Erhardt committed
25

Philipp Erhardt's avatar
initial  
Philipp Erhardt committed
26
27
28
int main(int argc, char *argv[]) {
	QApplication app(argc, argv);

29
30
	// parse command line options
	struct option long_options[] = {
31
		{"url",			no_argument,		NULL,	'u'},
32
33
		{"page",		required_argument,	NULL,	'p'},
		{"fullscreen",	no_argument,		NULL,	'f'},
34
		{"quit",		no_argument,		NULL,	'q'},
35
36
37
38
		{"help",		no_argument,		NULL,	'h'},
		{NULL, 0, NULL, 0}
	};
	int option_index = 0;
39
	bool download_url = false;
40
	while (1) {
41
		int c = getopt_long(argc, argv, "+up:fqh", long_options, &option_index);
42
43
44
45
		if (c == -1) {
			break;
		}
		switch (c) {
46
47
48
			case 'u':
				download_url = true;
				break;
49
50
			case 'p':
				// currently no warning message on wrong input
51
				CFG::get_instance()->set_tmp_value("start_page", atoi(optarg) - 1);
52
53
				break;
			case 'f':
54
				CFG::get_instance()->set_tmp_value("fullscreen", true);
55
				break;
56
57
58
			case 'q':
				CFG::get_instance()->set_tmp_value("quit_on_init_fail", true);
				break;
59
60
61
62
63
64
65
66
67
			case 'h':
				print_help(argv[0]);
				return 0;
			default:
				// getopt prints an error message
				return 1;
		}
	}

68
69
70
71
72
73
74
75
76
	// fork more processes if there are arguments left
	if (optind < argc - 1) {
		QStringList l;
		for (int i = optind + 1; i < argc; i++) {
			l << argv[i];
		}
		QProcess::startDetached(argv[0], l);
	}

77
	QString file;
Gregor Lang's avatar
Gregor Lang committed
78
	Download download;
79
	if (argv[optind] != NULL) {
80
81
82
83
84
		if (download_url) {
			file = download.load(QString::fromUtf8(argv[optind]));
		} else {
			file = QString::fromUtf8(argv[optind]);
		}
85
86
87
		if (file == NULL) {
			return 1;
		}
88
	}
89
	// else no argument given, "open" empty string
Gregor Lang's avatar
Gregor Lang committed
90
91

	Viewer katarakt(file);
Philipp Erhardt's avatar
Philipp Erhardt committed
92
93
94
	if (!katarakt.is_valid()) {
		return 1;
	}
95
	katarakt.show();
Philipp Erhardt's avatar
initial  
Philipp Erhardt committed
96

Philipp Erhardt's avatar
fail    
Philipp Erhardt committed
97
	return app.exec();
Philipp Erhardt's avatar
initial  
Philipp Erhardt committed
98
99
}