update about dialog
[presencevnc] / src / main.cpp
1 /*
2    Presence VNC
3    Copyright (C) 2010 Christian Pulvermacher
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License along
16    with this program; if not, write to the Free Software Foundation, Inc.,
17    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18    */
19 #include "mainwindow.h"
20 #include "rfb/rfbclient.h"
21
22 #include <QApplication>
23 #include <QString>
24
25 #include <iostream>
26
27
28 const QString APPNAME("Presence VNC");
29
30 void printHelp()
31 {
32     std::cout << "Usage: " << qPrintable(QCoreApplication::arguments().at(0)) << " [options] [URL [quality]]\n"
33
34               << "\nOptions:\n"
35               << " --help\t\t\t Print this text and exit.\n"
36               << " --listen [port]\t Listen for incoming connections on given port, or 5500 if omitted. Cannot be used with an URL.\n"
37               << " --viewonly\t\t Don't send mouse/keyboard input to remote desktop. This is only useful if you also supply a URL, or --listen.\n"
38
39               << "\nURLs:\n"
40               << " vnc://:password@server:display\n\n"
41               << " Password and display can be omitted, e.g. vnc://server is a valid URL.\n"
42               << " Optionally, you can define the quality as a second argument (1-3, where 1 is the best). Default is 2.\n";
43 }
44
45 int main(int argc, char *argv[])
46 {
47     QCoreApplication::setOrganizationName(APPNAME);
48     QCoreApplication::setApplicationName(APPNAME);
49
50     QApplication app(argc, argv);
51
52     QString url;
53     int quality = 2;
54     int listen_port = 0; //only used if >0
55     bool view_only = false;
56
57     QStringList arguments = QCoreApplication::arguments();
58     for(int i = 1; i < arguments.count(); i++) {
59         if(arguments.at(i) == "--help") {
60             printHelp();
61
62             return 0;
63         } else if(arguments.at(i) == "--listen") {
64             if(arguments.count() <= i+1 or arguments.at(i+1).startsWith("--")) {
65                 //no argument found, use default
66                 listen_port = LISTEN_PORT_OFFSET;
67             } else {
68                 //next argument should be a port number
69                 listen_port = arguments.at(i+1).toInt();
70                 if(listen_port < 1 or listen_port > 65535) {
71                     std::cerr << "\"" << qPrintable(arguments.at(i+1)) << "\" is not a valid port number!\n\n";
72                     return 1;
73                 }
74
75                 //everything ok
76                 i++;
77             }
78         } else if(arguments.at(i) == "--viewonly") {
79             view_only = true;
80         } else { //not a valid command line option, should be the url
81             url = arguments.at(i);
82
83             //check url
84             if(!url.startsWith("vnc://")) {
85                 std::cerr << "\"" << qPrintable(url) << "\" is not a valid command line option!\n\n";
86                 printHelp();
87
88                 return 1;
89             }
90
91             if(arguments.count() > i+1) { //TODO: having a --quality option would make more sense.
92                 int arg = arguments.at(i+1).toInt();
93                 if(1 <= arg and arg <= 3) { //check if arg is valid, might also be another option
94                     quality = arg;
95                     i++;
96                 }
97             }
98         }
99     }
100
101     if(!url.isNull() and listen_port > 0) {
102         std::cerr << "--listen cannot be used with an URL!\n";
103         return 1;
104     }
105
106     MainWindow main(url, quality, listen_port, view_only);
107     main.show();
108     return app.exec();
109 }