merge in my changes from soc-krdc branch
[kdenetwork.git] / krdc / main.cpp
blob9666f71b4ec2d8cc734b2afe2b3b0bd808e79d45
1 /***************************************************************************
2 main.cpp - main control
3 -------------------
4 begin : Thu Dec 20 15:11:42 CET 2001
5 copyright : (C) 2001-2003 by Tim Jansen
6 email : tim@tjansen.de
7 ***************************************************************************/
9 /***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
19 #include <kcmdlineargs.h>
20 #include <kaboutdata.h>
21 #include <kapplication.h>
22 #include <klocale.h>
23 #include <kmessagebox.h>
24 #include <kconfig.h>
25 #include <kdebug.h>
26 #include <kglobal.h>
27 #include <kwallet.h>
28 #include <QTimer>
29 #include <QFile>
30 #include <QTextStream>
31 #include <QRegExp>
33 #include "main.h"
35 // NOTE: I'm not comfortable with the wallet being global data and this high up
36 // in the heirarchy, but there are 3 reasons for its current placement here:
37 // 1) There are some important threading issues where it comes to the password
38 // handling code, and a lot of it is done outside of the objects.
39 // 2) Different backends need access to the same wallet. so that it is not
40 // opened multiple times.
41 // 3) MainController is about the only thing that isn't deleted in between connection
42 // attempts.
43 KWallet::Wallet *wallet = 0;
45 static const char description[] = I18N_NOOP("Remote desktop connection");
48 int main(int argc, char *argv[])
50 KAboutData aboutData( "krdc", 0, ki18n("Remote Desktop Connection"),
51 KDE_VERSION_STRING, ki18n(description), KAboutData::License_GPL,
52 ki18n("(c) 2001-2003, Tim Jansen"
53 "(c) 2002-2003, Arend van Beelen jr."
54 "(c) 2000-2002, Const Kaplinsky\n"
55 "(c) 2000, Tridia Corporation\n"
56 "(c) 1999, AT&T Laboratories Cambridge\n"
57 "(c) 1999-2003, Matthew Chapman\n"), KLocalizedString(), 0,
58 "tim@tjansen.de");
59 aboutData.addAuthor(ki18n("Tim Jansen"),KLocalizedString(), "tim@tjansen.de");
60 aboutData.addAuthor(ki18n("Arend van Beelen jr."),
61 ki18n("RDP backend"), "arend@auton.nl");
62 aboutData.addCredit(ki18n("AT&T Laboratories Cambridge"),
63 ki18n("Original VNC viewer and protocol design"));
64 aboutData.addCredit(ki18n("Const Kaplinsky"),
65 ki18n("TightVNC encoding"));
66 aboutData.addCredit(ki18n("Tridia Corporation"),
67 ki18n("ZLib encoding"));
68 KCmdLineArgs::init( argc, argv, &aboutData );
70 KCmdLineOptions options;
71 options.add("f");
72 options.add("fullscreen", ki18n("Start in fullscreen mode"));
73 options.add("w");
74 options.add("window", ki18n("Start in regular window"));
75 options.add("l");
76 options.add("low-quality", ki18n("Low quality mode (Tight Encoding, 8 bit color)"));
77 options.add("m");
78 options.add("medium-quality", ki18n("Medium quality mode (Tight Encoding, lossy)"));
79 options.add("h");
80 options.add("high-quality", ki18n("High quality mode, default (Hextile Encoding)"));
81 options.add("s");
82 options.add("scale", ki18n("Start VNC in scaled mode"));
83 options.add("c");
84 options.add("local-cursor", ki18n("Show local cursor (VNC only)"));
85 options.add("e");
86 options.add("encodings ", ki18n("Override VNC encoding list (e.g. 'hextile raw')"));
87 options.add("p");
88 options.add("password-file ", ki18n("Provide the password in a file"));
89 options.add("+[host]", ki18n("The name of the host, e.g. 'localhost:1'"));
90 KCmdLineArgs::addCmdLineOptions( options );
92 KApplication a;
94 QString host;
95 KRemoteView::Quality quality = KRemoteView::Unknown;
96 QString encodings;
97 QString password;
98 QString resolution;
99 QString keymap;
100 WindowMode wm = WINDOW_MODE_AUTO;
101 bool scale = false;
102 bool localCursor = KGlobal::config()->readEntry("alwaysShowLocalCursor", false);
103 QSize initialWindowSize;
104 QString caption;
106 KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
108 if (args->isSet("low-quality"))
109 quality = KRemoteView::Low;
110 else if (args->isSet("medium-quality"))
111 quality = KRemoteView::Medium;
112 else if (args->isSet("high-quality"))
113 quality = KRemoteView::High;
115 if (args->isSet("fullscreen"))
116 wm = WINDOW_MODE_FULLSCREEN;
117 else if (args->isSet("window"))
118 wm = WINDOW_MODE_NORMAL;
120 if (args->isSet("scale"))
121 scale = true;
123 if (args->isSet("local-cursor"))
124 localCursor = true;
126 if (args->isSet("encodings"))
127 encodings = args->getOption("encodings");
129 if (args->isSet("password-file")) {
130 QString passwordFile = args->getOption("password-file");
131 QFile f(passwordFile);
132 if (!f.open(QIODevice::ReadOnly)) {
133 KMessageBox::error(0, i18n("The password file '%1' does not exist.", passwordFile));
134 return 1;
136 password = QTextStream(&f).readLine();
137 f.close();
140 if (args->count() > 0)
141 host = args->arg(0);
142 args->clear();
144 QString is;
145 args = KCmdLineArgs::parsedArgs("kde");
146 if( args)
148 if (args->isSet("geometry"))
149 is = args->getOption("geometry");
150 if (!is.isNull()) {
151 QRegExp re("([0-9]+)[xX]([0-9]+)");
152 if (!re.exactMatch(is))
153 args->usageError(i18n("Wrong geometry format, must be widthXheight"));
154 initialWindowSize = QSize(re.cap(1).toInt(), re.cap(2).toInt());
157 if (args->isSet("caption"))
158 caption = args->getOption("caption");
159 args->clear();
161 MainController mc(&a, wm, host, quality, encodings, password,
162 scale, localCursor, initialWindowSize, caption);
163 return mc.main();
166 MainController::MainController(KApplication *app, WindowMode wm,
167 const QString &host,
168 KRemoteView::Quality quality,
169 const QString &encodings,
170 const QString &password,
171 bool scale,
172 bool localCursor,
173 QSize initialWindowSize,
174 QString &caption) :
175 m_windowMode(wm),
176 m_host(host),
177 m_encodings(encodings),
178 m_password(password),
179 m_scale(scale),
180 m_localCursor(localCursor),
181 m_initialWindowSize(initialWindowSize),
182 m_quality(quality),
183 m_caption(caption),
184 m_app(app) {
187 MainController::~MainController() {
188 delete wallet; wallet = 0;
191 int MainController::main() {
193 if (start())
194 return m_app->exec();
195 else
196 return 0;
199 void MainController::errorRestartRequested() {
200 QTimer::singleShot(0, this, SLOT(errorRestart()));
203 bool MainController::start() {
204 m_krdc = new KRDC(m_windowMode, m_host,
205 m_quality, m_encodings, m_password,
206 m_scale, m_localCursor, m_initialWindowSize,
207 m_caption);
209 QObject::connect(m_krdc, SIGNAL(disconnected()),
210 m_app, SLOT(quit()));
211 connect(m_krdc, SIGNAL(disconnectedError()),
212 SLOT(errorRestartRequested()));
214 return m_krdc->start();
217 void MainController::errorRestart() {
218 if (!m_host.isEmpty())
219 KRDC::setLastHost(m_host);
220 m_host.clear(); // only auto-connect once
222 m_krdc = 0;
224 if (!start())
225 m_app->quit();
228 #include "main.moc"