summaryrefslogblamecommitdiffstats
path: root/src/pvsgui.cpp
blob: 25f1cd6803b2b85562a4617abd9772a3744cfc68 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444



























































































































































































































































































































































































































































                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
/*
 # Copyright (c) 2009, 2010 - OpenSLX Project, Computer Center University of
 # Freiburg
 #
 # This program is free software distributed under the GPL version 2.
 # See http://openslx.org/COPYING
 #
 # If you have any feedback please consult http://openslx.org/feedback and
 # send your suggestions, praise, or complaints to feedback@openslx.org
 #
 # General information about OpenSLX can be found at http://openslx.org/
 # -----------------------------------------------------------------------------
 # pvsClientGUI.cpp
 #  - main loop for pvs client GUI.
 #  - draws toolbar and system tray icon.
 #  - uses DBUS to communicate with pvs client.
 # -----------------------------------------------------------------------------
 */

#include "pvsgui.h"

PVSGUI::PVSGUI(QWidget *parent) :
    QWidget(parent)
{
    setupUi(this);

    _menu = new QMenu(this);
    _hostMenu = new QMenu(tr("Connect"), this);
    _hosts = new QHash<QString, QAction*> ();
    _vncViewer = new ClientVNCViewer();
    _chatDialog = new ClientChatDialog();
    _configDialog = new ClientConfigDialog();
    _infoDialog = new ClientInfoDialog();
    _aboutDialog = new AboutDialog();

    _hostMenu->setEnabled(false);
    hostButton->setEnabled(false);

    setupMenu();

    if (QSystemTrayIcon::isSystemTrayAvailable())
    {
        qDebug("[%s] System tray available.", metaObject()->className());
        _trayIcon = new QSystemTrayIcon(QIcon(":cam_off32.svg"), this);
        _trayIcon->setContextMenu(_menu);
        _trayIcon->setVisible(true);
        _chatDialog->setTrayIcon(_trayIcon);
    }
    else
        _trayIcon = NULL;

    // connect to D-Bus and get interface
    QDBusConnection dbus = QDBusConnection::sessionBus();
    dbus.registerObject("/", this);
    dbus.registerService("org.openslx.pvsgui");
    _ifaceDBus = new OrgOpenslxPvsInterface("org.openslx.pvs", "/", dbus, this);
    _ifaceDBus->start(); // start pvs if not running

    // get available hosts
    QDBusPendingReply<QStringList> reply0 = _ifaceDBus->getAvailableHosts();
    reply0.waitForFinished();
    QStringList hosts = reply0.value();
    if (reply0.isValid() && !hosts.isEmpty())
        foreach (QString host, hosts)
                addHost(host);

    // already connected?
    QDBusPendingReply<QString> reply1 = _ifaceDBus->isConnected();
    reply1.waitForFinished();
    QString host = reply1.value();
    if (reply1.isValid() && host != "")
        connected(host);
    else
        disconnected();

    if (dbus.isConnected())
        qDebug("[%s] Connection to DBus successful!", metaObject()->className());

    // TODO: perhaps this can go if fadi does his work
    // check if vnc is allowed and setup checkbox
    QFile file(QDir::toNativeSeparators(QDir::homePath() + "/.pvs/.allow"));
    if (file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        QTextStream in(&file);
        QString line = in.readLine();
        if (line == "1")
            vncCheckBox->setChecked(true);
        file.close();
    }

    // listen on port 29481 for incoming file transfers
    _serverSocket = new QTcpServer();
    _serverSocket->listen(QHostAddress::Any, 29481);
    connect(_serverSocket, SIGNAL(newConnection()), this, SLOT(receiveFile()));

    // signals & slots - menu
    connect(_disconnectAction, SIGNAL(triggered()), this, SLOT(pvsDisconnect()));
    connect(_startChatAction, SIGNAL(triggered()), _chatDialog, SLOT(open()));
    connect(_sendFileAction, SIGNAL(triggered()), this, SLOT(sendFile()));
    connect(_configAction, SIGNAL(triggered()), _configDialog, SLOT(open()));
    connect(_showInfoAction, SIGNAL(triggered()), _infoDialog, SLOT(open()));
    connect(_aboutAction, SIGNAL(triggered()), _aboutDialog, SLOT(open()));
    connect(_quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
    connect(_configDialog, SIGNAL(configChanged()), this, SLOT(updateConfig()));

    // signals & slots - toolbar
    connect(_menu, SIGNAL(aboutToHide()), this, SLOT(hide()));
    connect(_hostMenu, SIGNAL(aboutToHide()), this, SLOT(hide()));
    connect(_hostMenu, SIGNAL(triggered(QAction*)), this,
            SLOT(pvsConnect(QAction*)));
    connect(vncCheckBox, SIGNAL(stateChanged(int)), this,
            SLOT(setVncAllow(int)));

    // signals & slots - dbus
    connect(_ifaceDBus, SIGNAL(showMessage(QString, QString, bool)), this,
            SLOT(showMessage(QString, QString, bool)));
    connect(_ifaceDBus, SIGNAL(connected(QString)), this,
            SLOT(connected(QString)));
    connect(_ifaceDBus, SIGNAL(disconnected()), this, SLOT(disconnected()));
    connect(_ifaceDBus, SIGNAL(addHost(QString)), this, SLOT(addHost(QString)));
    connect(_ifaceDBus, SIGNAL(delHost(QString)), this, SLOT(delHost(QString)));

    // show toolbar
    setWindowFlags(Qt::WindowStaysOnTopHint | Qt::X11BypassWindowManagerHint);
    setAttribute(Qt::WA_AlwaysShowToolTips);
    updateConfig();
    setVisible(true);
    hide();
}

PVSGUI::~PVSGUI()
{
    _ifaceDBus->quit();
}

////////////////////////////////////////////////////////////////////////////////
// Public

void PVSGUI::updateConfig()
{
    if (_settings.value("Display/location").isNull())
        setLocation(POSITION_TOP_CENTER);
    else
        setLocation(_settings.value("Display/location").toInt());
}

////////////////////////////////////////////////////////////////////////////////
// Protected

void PVSGUI::leaveEvent(QEvent * e)
{
    if (!_menu->isVisible() && !_hostMenu->isVisible())
        hide(true);
    QWidget::leaveEvent(e);
}

void PVSGUI::enterEvent(QEvent * e)
{
    hide(false);
    QWidget::enterEvent(e);
}

void PVSGUI::mousePressEvent(QMouseEvent *event)
{
    QApplication::setOverrideCursor(QCursor(Qt::ClosedHandCursor));
    _clickPoint = event->pos();
}

void PVSGUI::mouseReleaseEvent(QMouseEvent *event)
{
    QApplication::setOverrideCursor(QCursor(Qt::ArrowCursor));
}

void PVSGUI::mouseMoveEvent(QMouseEvent *event)
{
    if (event->globalX() - _clickPoint.x() >= 0 && event->globalX()
            - _clickPoint.x() + width() <= QApplication::desktop()->width())
    {
        move(event->globalX() - _clickPoint.x(), y());
    }
}

////////////////////////////////////////////////////////////////////////////////
// Private

void PVSGUI::setupMenu()
{
    // setup actions
    _disconnectAction = new QAction(tr("&Disconnect"), this);
    _startChatAction = new QAction(tr("C&hat"), this);
    _sendFileAction = new QAction(tr("&Send File"), this);
    _configAction = new QAction(tr("&Config"), this);
    _showInfoAction = new QAction(tr("&Information"), this);
    _aboutAction = new QAction(tr("&About"), this);
    _quitAction = new QAction(tr("&Quit"), this);

    // setup menu
    _menu->addMenu(_hostMenu);
    _menu->addAction(_disconnectAction);
    _menu->addAction(_showInfoAction);
    _menu->addSeparator();
    _menu->addAction(_startChatAction);
    _menu->addAction(_sendFileAction);
    _menu->addSeparator();
    _menu->addAction(_configAction);
    _menu->addAction(_aboutAction);
    _menu->addSeparator();
    _menu->addAction(_quitAction);

    pvsButton->setMenu(_menu);
    hostButton->setMenu(_hostMenu);
}

void PVSGUI::setLocation(int location)
{
    _location = location;
    switch (_location)
    {
    case POSITION_TOP_LEFT:
        move(0, 0);
        break;
    case POSITION_TOP_CENTER:
        move((QApplication::desktop()->width() - width()) / 2, 0);
        break;
    case POSITION_TOP_RIGHT:
        move(QApplication::desktop()->width() - width(), 0);
        break;
    case POSITION_BOTTOM_LEFT:
        move(0, QApplication::desktop()->height() - height());
        break;
    case POSITION_BOTTOM_CENTER:
        move((QApplication::desktop()->width() - width()) / 2,
                QApplication::desktop()->height() - height());
        break;
    case POSITION_BOTTOM_RIGHT:
        move(QApplication::desktop()->width() - width(),
                QApplication::desktop()->height() - height());
        break;
    default:
        break;
    }
}

void PVSGUI::hide(bool b)
{
    if (b)
    {
        if (_location <= POSITION_TOP_RIGHT)
            move(x(), 2 - height());
        else
            move(x(), QApplication::desktop()->height() - 2);
    }
    else
    {
        if (_location <= POSITION_TOP_RIGHT)
            move(x(), 0);
        else
            move(x(), QApplication::desktop()->height() - height());
    }
}

void PVSGUI::hide()
{
    if (!_menu->isVisible() && !_hostMenu->isVisible())
        hide(true);
}

void PVSGUI::pvsConnect(QAction *action)
{
    QString host = action->text();
    action->setChecked(false); // we set it manually

    // already connected?
    if (host == hostButton->text())
    {
        action->setChecked(true);
        return;
    }

    // ask user for passwd
    bool ok = false;
    QString passwd = QInputDialog::getText(0, tr("PVS Connection"), tr(
            "Please enter password (If not needed leave blank):"),
            QLineEdit::Password, QString(), &ok);

    if (ok)
    {
        _ifaceDBus->pvsConnect(host, passwd); // send via dbus
        _passwd = passwd; // TODO: we have to ask the backend for passwd!
        qDebug("[%s] Host '%s' send via DBus.", metaObject()->className(),
                qPrintable(host));
    }
}

void PVSGUI::pvsDisconnect()
{
    QMessageBox::StandardButton result = QMessageBox::question(0, tr(
            "PVS Connection"), tr("Are you sure you want to disconnect?"),
            QMessageBox::Ok | QMessageBox::Cancel, QMessageBox::Ok);

    if (result == QMessageBox::Ok)
        _ifaceDBus->pvsDisconnect();
}

void PVSGUI::showMessage(QString title, QString msg, bool useDialog)
{
    if (!isVisible())
        return;

    // show balloon message if supported, otherwise show message dialog
    if (!useDialog && _trayIcon && QSystemTrayIcon::supportsMessages())
        _trayIcon->showMessage(title, msg, QSystemTrayIcon::Information, 10000);
    else
        QMessageBox::about(0, title, msg);
}

void PVSGUI::connected(QString host)
{
    statusLabel->setText(
            "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\"><html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">p, li { white-space: pre-wrap; }</style></head><body style=\" font-family:'DejaVu Sans'; font-size:9pt; font-weight:400; font-style:normal;\"><p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" color:#00aa00;\">Online</span></p></body></html>");
    showMessage(tr("PVS connection"), tr("Connected to ") + host);
    if (_hosts->contains(host))
        _hosts->value(host)->setChecked(true);
    hostButton->setText(host);
    _disconnectAction->setEnabled(true);
    _startChatAction->setEnabled(true);
    _sendFileAction->setEnabled(true);
    _showInfoAction->setEnabled(true);
    _infoDialog->setHost(host);
    _infoDialog->setPasswd(_passwd);

    if (_trayIcon)
    {
        _trayIcon->setIcon(QIcon(":cam_on32.svg"));
        _trayIcon->setToolTip(tr("Connected to ") + host);
    }
}

void PVSGUI::disconnected()
{
    statusLabel->setText(
            "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\"><html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">p, li { white-space: pre-wrap; }</style></head><body style=\" font-family:'DejaVu Sans'; font-size:9pt; font-weight:400; font-style:normal;\"><p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" color:#ff0000;\">Offline</span></p></body></html>");
    showMessage(tr("PVS connection"), tr("Disconnected"));
    if (_hosts->contains(hostButton->text()))
        _hosts->value(hostButton->text())->setChecked(false);
    hostButton->setText("-");
    _disconnectAction->setEnabled(false);
    _startChatAction->setEnabled(false);
    _sendFileAction->setEnabled(false);
    _showInfoAction->setEnabled(false);
    _passwd = "";
    if (_trayIcon)
    {
        _trayIcon->setIcon(QIcon(":cam_off32.svg"));
        _trayIcon->setToolTip(tr("Disconnected"));
    }
    _vncViewer->close();
}

void PVSGUI::addHost(QString host)
{
    // avoid duplicates
    if (_hosts->contains(host))
        return;

    QAction *action = _hostMenu->addAction(host);
    action->setCheckable(true);
    _hosts->insert(host, action);

    if (!_hosts->isEmpty())
    {
        _hostMenu->setEnabled(true);
        hostButton->setEnabled(true);
    }

    if (hostButton->text() == "-")
        showMessage(tr("PVS Connection"), tr("New host available: ") + host);
}

void PVSGUI::delHost(QString host)
{
    if (_hosts->contains(host))
    {
        _hostMenu->removeAction(_hosts->value(host));
        _hosts->remove(host);
    }

    if (_hosts->isEmpty())
    {
        _hostMenu->setEnabled(false);
        hostButton->setEnabled(false);
    }
}

// TODO: perhaps this can go if fadi does his work
void PVSGUI::setVncAllow(int i)
{
    QFile file(QDir::toNativeSeparators(QDir::homePath() + "/.pvs/.allow"));
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
        return;

    QTextStream out(&file);
    if (i == 0)
        out << 0;
    else
        out << 1;

    file.close();
}

void PVSGUI::sendFile()
{
    ClientFileSendDialog *d = new ClientFileSendDialog();
    d->open();
}

void PVSGUI::receiveFile()
{
    QTcpSocket *socket = _serverSocket->nextPendingConnection();
    ClientFileReceiveDialog *d = new ClientFileReceiveDialog(socket);
    d->open();
}

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Main

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    app.setOrganizationName("openslx");
    app.setOrganizationDomain("openslx.org");
    app.setApplicationName("pvsgui");

    // use system locale as language to translate gui
    QTranslator translator;
    translator.load(":pvsgui");
    app.installTranslator(&translator);

    PVSGUI pvsgui;

    return app.exec();
}