main.cpp 3.51 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"
10
#include "dbus/dbus.h"
Philipp Erhardt's avatar
initial  
Philipp Erhardt committed
11

12
13
14
15
16
using namespace std;


static void print_help(char *name) {
	cout << "Usage:" << endl;
Philipp Erhardt's avatar
Philipp Erhardt committed
17
	cout << "  " << name << " ([OPTIONS] FILE|(-u URL))*" << endl;
18
19
	cout << endl;
	cout << "Options:" << endl;
20
21
22
23
24
	cout << "  -u, --url                    Open a URL instead of a local file" << endl;
	cout << "  -p, --page NUM               Start showing page NUM" << endl;
	cout << "  -f, --fullscreen             Start in fullscreen mode" << endl;
	cout << "  -q, --quit                   Quit on initialization failure" << endl;
	cout << "  -h, --help                   Print this help and exit" << endl;
25
	cout << "  --write-defaults FILE        Write the default configuration to FILE and exit" << endl;
26
	cout << "  --single-instance true|false Whether to have a single instance per file" << endl;
27
}
Philipp Erhardt's avatar
Philipp Erhardt committed
28

Philipp Erhardt's avatar
initial  
Philipp Erhardt committed
29
30
31
int main(int argc, char *argv[]) {
	QApplication app(argc, argv);

32
33
	// parse command line options
	struct option long_options[] = {
34
		{"url",			no_argument,		NULL,	'u'},
35
36
		{"page",		required_argument,	NULL,	'p'},
		{"fullscreen",	no_argument,		NULL,	'f'},
37
		{"quit",		no_argument,		NULL,	'q'},
38
		{"help",		no_argument,		NULL,	'h'},
39
		{"single-instance",	required_argument,	NULL,	0},
40
		{"write-defaults",	required_argument,	NULL,	0},
41
42
43
		{NULL, 0, NULL, 0}
	};
	int option_index = 0;
44
	bool download_url = false;
45
	while (1) {
46
		int c = getopt_long(argc, argv, "+up:fqh", long_options, &option_index);
47
48
49
50
		if (c == -1) {
			break;
		}
		switch (c) {
51
52
53
54
55
56
			case 0: {
				const char* option_name = long_options[option_index].name;
				if (!strcmp(option_name, "single-instance")) {
					// (according to QVariant) any string can be converted to
					// bool, so no type check needed here
					CFG::get_instance()->set_tmp_value("single_instance_per_file", optarg);
57
58
59
				} else if (!strcmp(option_name, "write-defaults")) {
					CFG::write_defaults(optarg);
					return 0;
60
61
62
				}
				break;
			}
63
64
65
			case 'u':
				download_url = true;
				break;
66
67
			case 'p':
				// currently no warning message on wrong input
68
				CFG::get_instance()->set_tmp_value("start_page", atoi(optarg) - 1);
69
70
				break;
			case 'f':
71
				CFG::get_instance()->set_tmp_value("fullscreen", true);
72
				break;
73
74
75
			case 'q':
				CFG::get_instance()->set_tmp_value("quit_on_init_fail", true);
				break;
76
77
78
79
80
81
82
83
84
			case 'h':
				print_help(argv[0]);
				return 0;
			default:
				// getopt prints an error message
				return 1;
		}
	}

85
86
87
88
89
90
91
92
93
	// 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);
	}

94
	QString file;
Gregor Lang's avatar
Gregor Lang committed
95
	Download download;
96
	if (argv[optind] != NULL) {
97
98
99
100
101
		if (download_url) {
			file = download.load(QString::fromUtf8(argv[optind]));
		} else {
			file = QString::fromUtf8(argv[optind]);
		}
102
103
104
		if (file == NULL) {
			return 1;
		}
105
	}
106
	// else no argument given, "open" empty string
Gregor Lang's avatar
Gregor Lang committed
107

108
109
110
111
112
113
	if (CFG::get_instance()->get_most_current_value("single_instance_per_file").toBool()) {
		if (activate_katarakt_with_file(file)) {
			return 0;
		}
	}

114
115
116
117
118
	// load stylesheet from config if no stylesheet was specified on the command line
	if (app.styleSheet().isEmpty()) {
		app.setStyleSheet(CFG::get_instance()->get_value("stylesheet").toString());
	}

Gregor Lang's avatar
Gregor Lang committed
119
	Viewer katarakt(file);
Philipp Erhardt's avatar
Philipp Erhardt committed
120
121
122
	if (!katarakt.is_valid()) {
		return 1;
	}
123
	katarakt.show();
Philipp Erhardt's avatar
initial  
Philipp Erhardt committed
124

125
126
127
	// initialize dbus interfaces
	dbus_init(&katarakt);

Philipp Erhardt's avatar
fail    
Philipp Erhardt committed
128
	return app.exec();
Philipp Erhardt's avatar
initial  
Philipp Erhardt committed
129
130
}