1 /***************************************************************************
2 main.cpp - main control
4 begin : Thu Dec 20 15:11:42 CET 2001
5 copyright : (C) 2001-2003 by Tim Jansen
7 ***************************************************************************/
9 /***************************************************************************
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. *
16 ***************************************************************************/
19 #include <kcmdlineargs.h>
20 #include <kaboutdata.h>
21 #include <kapplication.h>
23 #include <kmessagebox.h>
30 #include <QTextStream>
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
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,
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
;
72 options
.add("fullscreen", ki18n("Start in fullscreen mode"));
74 options
.add("window", ki18n("Start in regular window"));
76 options
.add("low-quality", ki18n("Low quality mode (Tight Encoding, 8 bit color)"));
78 options
.add("medium-quality", ki18n("Medium quality mode (Tight Encoding, lossy)"));
80 options
.add("high-quality", ki18n("High quality mode, default (Hextile Encoding)"));
82 options
.add("scale", ki18n("Start VNC in scaled mode"));
84 options
.add("local-cursor", ki18n("Show local cursor (VNC only)"));
86 options
.add("encodings ", ki18n("Override VNC encoding list (e.g. 'hextile raw')"));
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
);
95 KRemoteView::Quality quality
= KRemoteView::Unknown
;
100 WindowMode wm
= WINDOW_MODE_AUTO
;
102 bool localCursor
= KGlobal::config()->readEntry("alwaysShowLocalCursor", false);
103 QSize initialWindowSize
;
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"))
123 if (args
->isSet("local-cursor"))
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
));
136 password
= QTextStream(&f
).readLine();
140 if (args
->count() > 0)
145 args
= KCmdLineArgs::parsedArgs("kde");
148 if (args
->isSet("geometry"))
149 is
= args
->getOption("geometry");
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");
161 MainController
mc(&a
, wm
, host
, quality
, encodings
, password
,
162 scale
, localCursor
, initialWindowSize
, caption
);
166 MainController::MainController(KApplication
*app
, WindowMode wm
,
168 KRemoteView::Quality quality
,
169 const QString
&encodings
,
170 const QString
&password
,
173 QSize initialWindowSize
,
177 m_encodings(encodings
),
178 m_password(password
),
180 m_localCursor(localCursor
),
181 m_initialWindowSize(initialWindowSize
),
187 MainController::~MainController() {
188 delete wallet
; wallet
= 0;
191 int MainController::main() {
194 return m_app
->exec();
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
,
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