summaryrefslogtreecommitdiffstats
path: root/src/client/toolbar/toolbar.cpp
blob: 8e80fe32810b4b86639064d9d9cec32991127ee7 (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
/*
 * toolbar.cpp
 *
 *  Created on: 21.01.2013
 *      Author: sr
 */

#include "toolbar.h"
#include "../../shared/settings.h"
#include "../net/serverconnection.h"
#include "../vnc/vncwindow.h"
#include "../vnc/vncserver.h"

Toolbar::Toolbar(QWidget *parent) :
	QWidget(parent), _location(POSITION_TOP_CENTER), _hideTimer(0), _connection(NULL)
{
	setupUi(this);
	setWindowFlags(Qt::WindowStaysOnTopHint | Qt::X11BypassWindowManagerHint | Qt::FramelessWindowHint);
	setAttribute(Qt::WA_AlwaysShowToolTips);
	setAttribute(Qt::WA_QuitOnClose);
	setVisible(true);
	// VNC Window
	_vnc = new VncWindow(NULL);
	// Connect window
	_connectWindow = new ConnectWindow(NULL);
	connect(_connectWindow, SIGNAL(disconnect()), this, SLOT(onDoDisconnect()));
	connect(_connectWindow, SIGNAL(connected(ServerConnection*)), this, SLOT(onConnected(ServerConnection*)));
    connect(VncServer::instance(), SIGNAL(started(int, QString&, QString&)), this, SLOT(onVncServerIsRunning(int, QString&, QString&)));
	//
	setupMenu();
	setLocation();
	hideBar();
}

void Toolbar::setupMenu()
{
	_menu = new QMenu(this);
	// setup actions
	_acnDisconnect = new QAction(tr("Set &session ID"), this);
	//_acnDisconnect->setEnabled(false);
	_acnQuit = new QAction(tr("&Quit"), this);

	// setup menu
	_menu->addAction(_acnDisconnect);
	_menu->addSeparator();
	_menu->addAction(_acnQuit);

	cmdMenu->setMenu(_menu);

	connect(_acnQuit, SIGNAL(triggered()), this, SLOT(onQuit()));
	connect(_acnDisconnect, SIGNAL(triggered()), _connectWindow, SLOT(show()));
}

Toolbar::~Toolbar()
{
	VncServer::instance()->stop();
	_vnc->deleteLater();
	_connectWindow->deleteLater();
}

//###########\\/\/

void Toolbar::setLocation()
{
	const QDesktopWidget desktop;
	const QRect primaryScreen = desktop.screenGeometry();
	switch (_location)
	{
	case POSITION_TOP_LEFT:
		move(primaryScreen.left(), primaryScreen.top());
		break;
	case POSITION_TOP_CENTER:
		move((primaryScreen.width() - this->width()) / 2 + primaryScreen.left(), primaryScreen.top());
		break;
	case POSITION_TOP_RIGHT:
		move(primaryScreen.right() - width(), primaryScreen.top());
		break;
	case POSITION_BOTTOM_LEFT:
		move(primaryScreen.left(), primaryScreen.bottom() - height());
		break;
	case POSITION_BOTTOM_CENTER:
		move((primaryScreen.width() - this->width()) / 2 + primaryScreen.left(), primaryScreen.bottom() - height());
		break;
	case POSITION_BOTTOM_RIGHT:
		move(primaryScreen.right() - width(), primaryScreen.bottom() - height());
		break;
	default:
		break;
	}
}

void Toolbar::setBarVisible(bool shown)
{
	const QDesktopWidget desktop;
	const QRect primaryScreen = desktop.screenGeometry();
	if (!shown)
	{
		if (_location <= POSITION_TOP_RIGHT)
			move(x(), primaryScreen.top() + 2 - height());
		else
			move(x(), primaryScreen.bottom() - 2);
	}
	else
	{
		if (_location <= POSITION_TOP_RIGHT)
			move(x(), primaryScreen.top());
		else
			move(x(), primaryScreen.bottom() - height());
	}
}

bool Toolbar::hideBar()
{
    // Don't hide window if any menu is open or VNC Server is running from this client.
    if (_menu->isVisible() || VncServer::instance()->isVncServerRunning())
		return false;
	setBarVisible(false);
	return true;
}

/*
 * Override
 */

void Toolbar::leaveEvent(QEvent* e)
{
	if (_hideTimer == 0)
		_hideTimer = startTimer(100);
	_hideDelay = 6;
	QWidget::leaveEvent(e);
}

void Toolbar::enterEvent(QEvent* e)
{
	if (_hideTimer != 0)
	{
		killTimer(_hideTimer);
		_hideTimer = 0;
	}
	setBarVisible(true);
	QWidget::enterEvent(e);
}

void Toolbar::timerEvent(QTimerEvent* event)
{
	if (event->timerId() == _hideTimer)
	{
		if (--_hideDelay <= 0)
		{
			if (hideBar())
			{
				killTimer(_hideTimer);
				_hideTimer = 0;
			}
		}
	}
}

/*
 * Slots
 */

void Toolbar::onVncServerIsRunning(int port, QString&, QString&)
{
    if (port > 0)
    {
        setBarVisible(true);
        return;
    }
    hideBar();
    return;
}

void Toolbar::onDisconnected()
{
	_connectWindow->setConnected(false);
	if (_connection != NULL)
		_connection->blockSignals(true);
	_connection = NULL;
	lblStatus->setStyleSheet("color:red");
	lblStatus->setText(tr("Offline"));
}

void Toolbar::onConnected(ServerConnection* connection)
{
	lblStatus->setStyleSheet("color:green");
	lblStatus->setText(tr("Online"));
	//
	if (_connection != NULL)
	{
		disconnect(_connection, SIGNAL(disconnected()), this, SLOT(onDisconnected()));
		_connection->blockSignals(true);
		_connection->disconnectFromServer();
	}
	_connection = connection;
	connect(_connection, SIGNAL(disconnected()), this, SLOT(onDisconnected()));
	connect(_connection, SIGNAL(openVnc(const QString&, int, const QString&, bool, bool, const QString&, const int)),
		_vnc, SLOT(open(const QString&, int, const QString&, bool, bool, const QString&, const int)));
	connect(_connection, SIGNAL(closeVnc()), _vnc, SLOT(close()));
	connect(_vnc, SIGNAL(running(const bool, const int)), _connection, SLOT(onVncViewerStartStop(const bool, const int)));
	_connectWindow->setConnected(true);
}

void Toolbar::onDoDisconnect()
{
	if (_connection != NULL)
		_connection->disconnectFromServer();
}

void Toolbar::onQuit()
{
	QApplication::exit(0);
}