Add ICQ feature 122468: Notification if someone reads your away message
[kdenetwork.git] / krdc / main.cpp
blobfb0c728cd8ff2817c5916ef2b504c400b6f4544f
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 <kwallet.h>
27 #include <QTimer>
28 #include <QFile>
29 #include <QTextStream>
30 #include <QRegExp>
32 #include "../config.h"
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");
47 static KCmdLineOptions options[] =
49 { "f", 0, 0 },
50 { "fullscreen", I18N_NOOP("Start in fullscreen mode"), 0 },
51 { "w", 0, 0 },
52 { "window", I18N_NOOP("Start in regular window"), 0 },
53 { "l", 0, 0 },
54 { "low-quality", I18N_NOOP("Low quality mode (Tight Encoding, 8 bit color)"), 0 },
55 { "m", 0, 0 },
56 { "medium-quality", I18N_NOOP("Medium quality mode (Tight Encoding, lossy)"), 0 },
57 { "h", 0, 0 },
58 { "high-quality", I18N_NOOP("High quality mode, default (Hextile Encoding)"), 0 },
59 { "s", 0, 0 },
60 { "scale", I18N_NOOP("Start VNC in scaled mode"), 0 },
61 { "c", 0, 0 },
62 { "local-cursor", I18N_NOOP("Show local cursor (VNC only)"), 0 },
63 { "e", 0, 0 },
64 { "encodings ", I18N_NOOP("Override VNC encoding list (e.g. 'hextile raw')"), 0 },
65 { "p", 0, 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 },
68 KCmdLineLastOption
72 int main(int argc, char *argv[])
74 KAboutData aboutData( "krdc", I18N_NOOP("Remote Desktop Connection"),
75 VERSION, 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,
82 "tim@tjansen.de");
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 );
95 KApplication a;
97 QString host;
98 Quality quality = QUALITY_UNKNOWN;
99 QString encodings;
100 QString password;
101 QString resolution;
102 QString keymap;
103 WindowMode wm = WINDOW_MODE_AUTO;
104 bool scale = false;
105 bool localCursor = KGlobal::config()->readEntry("alwaysShowLocalCursor", false);
106 QSize initialWindowSize;
107 QString caption;
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"))
124 scale = true;
126 if (args->isSet("local-cursor"))
127 localCursor = true;
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));
137 return 1;
139 password = QTextStream(&f).readLine();
140 f.close();
143 if (args->count() > 0)
144 host = args->arg(0);
146 QString is;
147 args = KCmdLineArgs::parsedArgs("kde");
148 if (args && 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->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);
162 return mc.main();
165 MainController::MainController(KApplication *app, WindowMode wm,
166 const QString &host,
167 Quality quality,
168 const QString &encodings,
169 const QString &password,
170 bool scale,
171 bool localCursor,
172 QSize initialWindowSize,
173 QString &caption) :
174 m_windowMode(wm),
175 m_host(host),
176 m_encodings(encodings),
177 m_password(password),
178 m_scale(scale),
179 m_localCursor(localCursor),
180 m_initialWindowSize(initialWindowSize),
181 m_quality(quality),
182 m_caption(caption),
183 m_app(app) {
186 MainController::~MainController() {
187 if ( wallet ) {
188 delete wallet; wallet = 0;
192 int MainController::main() {
194 if (start())
195 return m_app->exec();
196 else
197 return 0;
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,
208 m_caption);
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
223 m_krdc = 0;
225 if (!start())
226 m_app->quit();
229 #include "main.moc"