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");
47 static KCmdLineOptions options
[] =
50 { "fullscreen", I18N_NOOP("Start in fullscreen mode"), 0 },
52 { "window", I18N_NOOP("Start in regular window"), 0 },
54 { "low-quality", I18N_NOOP("Low quality mode (Tight Encoding, 8 bit color)"), 0 },
56 { "medium-quality", I18N_NOOP("Medium quality mode (Tight Encoding, lossy)"), 0 },
58 { "high-quality", I18N_NOOP("High quality mode, default (Hextile Encoding)"), 0 },
60 { "scale", I18N_NOOP("Start VNC in scaled mode"), 0 },
62 { "local-cursor", I18N_NOOP("Show local cursor (VNC only)"), 0 },
64 { "encodings ", I18N_NOOP("Override VNC encoding list (e.g. 'hextile raw')"), 0 },
66 { "password-file ", I18N_NOOP("Provide the password in a file"), 0 },
67 { "+[host]", I18N_NOOP("The name of the host, e.g. 'localhost:1'"), 0 },
72 int main(int argc
, char *argv
[])
74 KAboutData
aboutData( "krdc", I18N_NOOP("Remote Desktop Connection"),
75 KDE_VERSION_STRING
, description
, KAboutData::License_GPL
,
76 "(c) 2001-2003, Tim Jansen"
77 "(c) 2002-2003, Arend van Beelen jr."
78 "(c) 2000-2002, Const Kaplinsky\n"
79 "(c) 2000, Tridia Corporation\n"
80 "(c) 1999, AT&T Laboratories Cambridge\n"
81 "(c) 1999-2003, Matthew Chapman\n", 0, 0,
83 aboutData
.addAuthor("Tim Jansen",0, "tim@tjansen.de");
84 aboutData
.addAuthor("Arend van Beelen jr.",
85 I18N_NOOP("RDP backend"), "arend@auton.nl");
86 aboutData
.addCredit("AT&T Laboratories Cambridge",
87 I18N_NOOP("Original VNC viewer and protocol design"));
88 aboutData
.addCredit("Const Kaplinsky",
89 I18N_NOOP("TightVNC encoding"));
90 aboutData
.addCredit("Tridia Corporation",
91 I18N_NOOP("ZLib encoding"));
92 KCmdLineArgs::init( argc
, argv
, &aboutData
);
93 KCmdLineArgs::addCmdLineOptions( options
);
98 Quality quality
= QUALITY_UNKNOWN
;
103 WindowMode wm
= WINDOW_MODE_AUTO
;
105 bool localCursor
= KGlobal::config()->readEntry("alwaysShowLocalCursor", false);
106 QSize initialWindowSize
;
109 KCmdLineArgs
*args
= KCmdLineArgs::parsedArgs();
111 if (args
->isSet("low-quality"))
112 quality
= QUALITY_LOW
;
113 else if (args
->isSet("medium-quality"))
114 quality
= QUALITY_MEDIUM
;
115 else if (args
->isSet("high-quality"))
116 quality
= QUALITY_HIGH
;
118 if (args
->isSet("fullscreen"))
119 wm
= WINDOW_MODE_FULLSCREEN
;
120 else if (args
->isSet("window"))
121 wm
= WINDOW_MODE_NORMAL
;
123 if (args
->isSet("scale"))
126 if (args
->isSet("local-cursor"))
129 if (args
->isSet("encodings"))
130 encodings
= args
->getOption("encodings");
132 if (args
->isSet("password-file")) {
133 QString passwordFile
= args
->getOption("password-file");
134 QFile
f(passwordFile
);
135 if (!f
.open(QIODevice::ReadOnly
)) {
136 KMessageBox::error(0, i18n("The password file '%1' does not exist.", passwordFile
));
139 password
= QTextStream(&f
).readLine();
143 if (args
->count() > 0)
147 args
= KCmdLineArgs::parsedArgs("kde");
148 if (args
&& args
->isSet("geometry"))
149 is
= args
->getOption("geometry");
151 QRegExp
re("([0-9]+)[xX]([0-9]+)");
152 if (!re
.exactMatch(is
))
153 args
->usage(i18n("Wrong geometry format, must be widthXheight"));
154 initialWindowSize
= QSize(re
.cap(1).toInt(), re
.cap(2).toInt());
157 if (args
&& args
->isSet("caption"))
158 caption
= args
->getOption("caption");
160 MainController
mc(&a
, wm
, host
, quality
, encodings
, password
,
161 scale
, localCursor
, initialWindowSize
, caption
);
165 MainController::MainController(KApplication
*app
, WindowMode wm
,
168 const QString
&encodings
,
169 const QString
&password
,
172 QSize initialWindowSize
,
176 m_encodings(encodings
),
177 m_password(password
),
179 m_localCursor(localCursor
),
180 m_initialWindowSize(initialWindowSize
),
186 MainController::~MainController() {
188 delete wallet
; wallet
= 0;
192 int MainController::main() {
195 return m_app
->exec();
200 void MainController::errorRestartRequested() {
201 QTimer::singleShot(0, this, SLOT(errorRestart()));
204 bool MainController::start() {
205 m_krdc
= new KRDC(m_windowMode
, m_host
,
206 m_quality
, m_encodings
, m_password
,
207 m_scale
, m_localCursor
, m_initialWindowSize
,
210 QObject::connect(m_krdc
, SIGNAL(disconnected()),
211 m_app
, SLOT(quit()));
212 connect(m_krdc
, SIGNAL(disconnectedError()),
213 SLOT(errorRestartRequested()));
215 return m_krdc
->start();
218 void MainController::errorRestart() {
219 if (!m_host
.isEmpty())
220 KRDC::setLastHost(m_host
);
221 m_host
.clear(); // only auto-connect once