summaryrefslogtreecommitdiffstats
path: root/src/pvsgui.cpp
blob: 7e256829047a708d7f5356630425b14f24cae762 (plain) (blame)
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/*
 # 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 <getopt.h>
#include "pvsgui.h"
#include "version.h"

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

    // stop running pvs
    qDebug("[%s] Stopping pvs daemon.", metaObject()->className());
    QProcess::execute("pvs -c stop");

    _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);

    _trayIcon = new QSystemTrayIcon(QIcon(":cam_off32.svg"), this);
    _trayIcon->setContextMenu(_menu);
    _trayIcon->setVisible(true);
    _chatDialog->setTrayIcon(_trayIcon);

    // connect to D-Bus and get interface
    QDBusConnection dbus = QDBusConnection::sessionBus();
    _ifaceDBus = new OrgOpenslxPvsInterface("org.openslx.pvs", "/", dbus, this);
    if (dbus.isConnected())
        qDebug("[%s] Connection to DBus successful.", metaObject()->className());
    else
        qDebug("[%s] ERROR: Could not connect to DBus!", metaObject()->className());

    // start pvs daemon
    qDebug("[%s] Starting pvs daemon.", metaObject()->className());
    QDBusPendingReply<bool> reply0 = _ifaceDBus->start();
    reply0.waitForFinished();
    if (reply0.isValid() && reply0.value())
        qDebug("[%s] Connection to PVS daemon successful.", metaObject()->className());
    else
        qDebug("[%s] ERROR: Could not connect to PVS daemon!", metaObject()->className());

    // ask if restricted mode is on
    QDBusPendingReply<bool> reply1 = _ifaceDBus->isRestricted();
    reply1.waitForFinished();
    _restricted = reply1.value();

    setupMenu();

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

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

    // 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(_showAction, SIGNAL(toggled(bool)), this, SLOT(setVisible(bool)));
    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*)));

    // 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)));
    connect(_ifaceDBus, SIGNAL(incomingMulticastTransferNew(qulonglong, QString, QString, qulonglong)), SLOT(incomingMulticastFile(qulonglong, QString, QString, qulonglong)));

    // show toolbar
    setWindowFlags(Qt::WindowStaysOnTopHint | Qt::X11BypassWindowManagerHint | Qt::FramelessWindowHint);
    setAttribute(Qt::WA_AlwaysShowToolTips);
}

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

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

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


void PVSGUI::setVisible(bool visible)
{
    QWidget::setVisible(visible);
    _showAction->setChecked(isVisible());
}

void PVSGUI::setPosition(int position)
{
    _position = position;
    switch (_position)
    {
    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:
        updateConfig();
    }
}

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

////////////////////////////////////////////////////////////////////////////////
// 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
    _showAction = new QAction(tr("Show &toolbar"), this);
    _showAction->setCheckable(true);
    _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->addAction(_showAction);
    if (!_restricted) _menu->addMenu(_hostMenu);
    if (!_restricted) _menu->addAction(_disconnectAction);
    _menu->addAction(_showInfoAction);
    _menu->addSeparator();
    _menu->addAction(_startChatAction);
    _menu->addAction(_sendFileAction);
    _menu->addSeparator();
    if (!_restricted) _menu->addAction(_configAction);
    _menu->addAction(_aboutAction);
    _menu->addSeparator();
    if (!_restricted) _menu->addAction(_quitAction);

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

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

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 (!_settings.value("Display/showmsgs").toBool()) 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);
    }

    // try to disable screensaver
    if (QProcess::execute("xscreensaver-command -exit") == 0)
    	qDebug("xscreensaver disabled");
    else
    	qDebug("xscreensaver NOT disabled");

    if (QProcess::execute("gnome-screensaver-command --exit") == 0)
    	qDebug("gnome-screensaver disabled");
    else
    	qDebug("gnome-screensaver NOT disabled");
}

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>");
    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);
    }
}

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

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

void PVSGUI::incomingMulticastFile(qulonglong transferID, QString sender, QString basename, qulonglong size)
{
    ClientFileReceiveDialog *d = new ClientFileReceiveDialog(sender, transferID, basename, size, _ifaceDBus, this);
    d->open();
}

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

void printHelp()
{
    QTextStream qout(stdout);
    qout << QObject::tr("Usage: pvsgui [OPTIONS]...") << endl;
    qout << QObject::tr("Start the Pool Video Switch GUI.") << endl;
    qout << QObject::tr("Options:") << endl << endl;
    qout << "-b or --toolbar" << "\t\t" << QObject::tr("Start with toolbar.") << endl;
    qout << "-p or --position" << "\t" << QObject::tr("Set toolbar position (0-5)") << endl;
    qout << "-h or --help" << "\t\t" << QObject::tr("Show this help text and quit.") << endl;
    qout << "-v or --version" << "\t\t" << QObject::tr("Show version and quit.") << endl;
    qout << endl;
    qout.flush();
    exit(0);
}

void printVersion()
{
    QTextStream qout(stdout);
    qout << QObject::tr("Version: ") << VERSION_STRING << endl;
    qout << endl;
    qout.flush();
    exit(0);
}

int main(int argc, char *argv[])
{
    bool visible = false;
    int position = -1;

    // parse command line arguments
    int opt = 0;
    int longIndex = 0;
    static const char *optString = "hvp:b?";
    static const struct option longOpts[] =
    {
    	{ "help", no_argument, NULL, 'h' },
    	{ "version", no_argument, NULL, 'v' },
    	{ "position", required_argument, NULL, 'p' },
    	{ "toolbar", no_argument, NULL, 'b' }
    };

	opt = getopt_long( argc, argv, optString, longOpts, &longIndex );
	while( opt != -1 )
	{
		switch( opt )
		{
		case 'h':
			printHelp();
			break;
		case 'v':
			printVersion();
			break;
		case 'p':
			position = atoi(optarg);
			break;
		case 'b':
			visible = true;
			break;
        case '?':
        	exit(1);
		}
		opt = getopt_long( argc, argv, optString, longOpts, &longIndex );
	}


    QtSingleApplication app(argc, argv);
    app.setQuitOnLastWindowClosed(false);
    app.setOrganizationName("openslx");
    app.setOrganizationDomain("openslx.org");
    app.setApplicationName("pvsgui");

    // only one instance should be allowed
    if (app.sendMessage(""))
    {
        qDebug("[PVSGUI] ERROR: Already running. Exiting");
        return 0;
    }

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

    PVSGUI pvsgui;
    pvsgui.setPosition(position);
    pvsgui.setVisible(visible);
    pvsgui.hide();

    return app.exec();
}