overlaycontroller.cpp 9.47 KB
Newer Older
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
1
#include "overlaycontroller.h"
Carsten Fuhrmann's avatar
update  
Carsten Fuhrmann committed
2
#include <QDesktopWidget>
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
3 4 5
#include <QStringList>
#include "plugin.h"
#include <qdebug.h>
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
6
#include "teamspeak\public_errors.h"
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
7
#include <QRegExp>
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
8
#include <QFile>
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
9

10

Carsten Fuhrmann's avatar
update  
Carsten Fuhrmann committed
11 12 13 14
//#ifndef _DEBUG
//#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )
//#endif

Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
15 16
enum nodeType {
	channel,
17 18
	client,
	spacer
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
19 20 21 22
};

Q_DECLARE_METATYPE(nodeType);

23
OverlayController::OverlayController(const struct TS3Functions funcs, quint64 serverConnectionHandlerID) : QObject(), m_ts3(funcs)
Carsten Fuhrmann's avatar
update  
Carsten Fuhrmann committed
24 25 26
{
	QDesktopWidget desktop;

27 28 29
	m_muted = false;
	m_treeState = full;

Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
30 31 32
	m_channelIcon.addFile("plugins\\qtTsOverlay\\channel.png");
	m_clientIcon.addFile("plugins\\qtTsOverlay\\client.png");

Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
33 34 35 36 37
	QFile styleSheet("plugins\\qtTsOverlay\\label.styl");
	styleSheet.open(QIODevice::ReadOnly);
	m_stylesheet = styleSheet.readAll();
	styleSheet.close();

Carsten Fuhrmann's avatar
update  
Carsten Fuhrmann committed
38 39 40 41 42 43
	m_screenHeight = desktop.screenGeometry().height();
	m_screenWidth = desktop.screenGeometry().width();

	m_speakerOffset = 0;

	m_debugWindow = new QLabel;
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
44
	m_debugWindow->setGeometry(320, 200, 750, 500);
Carsten Fuhrmann's avatar
update  
Carsten Fuhrmann committed
45 46 47
	m_debugWindow->setAlignment(Qt::AlignTop | Qt::AlignLeft);
	m_debugWindow->show();

Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
48
	m_SCHID = serverConnectionHandlerID;
49

50
	m_tree = new ChannelListWidget;
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
51
	connect(m_tree->getTree(), &QTreeWidget::itemDoubleClicked, this, &OverlayController::treeItemClicked);
52
	connect(m_tree, &ChannelListWidget::treeStateChanged, this, &OverlayController::changeTreeState);
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
53 54

	updateChannelList();
55
	updateClientList();
Carsten Fuhrmann's avatar
update  
Carsten Fuhrmann committed
56 57 58 59 60 61 62 63 64 65 66 67
}

OverlayController::~OverlayController()
{
	for (auto &it : m_speakers)
		delete it;
	m_speakers.clear();

	for (auto &it : m_msgLines)
		delete it;
	m_msgLines.clear();

Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
68 69 70 71 72
	m_channelList.clear();

	while (!m_clientList.isEmpty())
		delete m_clientList.takeFirst();

Carsten Fuhrmann's avatar
update  
Carsten Fuhrmann committed
73
	delete m_debugWindow;
74
	delete m_tree;
Carsten Fuhrmann's avatar
update  
Carsten Fuhrmann committed
75 76
}

Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
77
void OverlayController::deleteChatLine(QWidget * line, QTimer *timer)
Carsten Fuhrmann's avatar
update  
Carsten Fuhrmann committed
78 79
{
	//TODO: delete the given line instead of the last
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
80 81 82 83 84 85
	int index = m_msgLines.indexOf(line);
	if (index != -1)
	{
		delete m_msgLines.at(index);
		m_msgLines.remove(index);
	}
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
86 87 88

	if (timer)
		delete timer;
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
89 90
}

Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
91 92 93 94 95 96 97
void OverlayController::treeItemClicked(QTreeWidgetItem * item, int column)
{
	int type = item->data(2, Qt::UserRole).toInt();

	if (type == channel)
	{
		anyID clientID;
98
		m_ts3.getClientID(m_SCHID, &clientID);
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
99 100

		quint64 id = item->data(1, Qt::UserRole).value<quint64>();
101
		m_ts3.requestClientMove(m_SCHID, clientID, id, "", NULL);
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
102 103 104 105
	}
	else if (type == client)
	{
		anyID id = item->data(1, Qt::UserRole).value<anyID>();
106
		m_ts3.requestClientPoke(m_SCHID, id, "", NULL);
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
107 108 109
	}
}

110 111 112 113 114 115 116
void OverlayController::changeTreeState(TreeState state)
{
	m_treeState = state;
	updateChannelList();
	updateClientList();
}

Carsten Fuhrmann's avatar
update  
Carsten Fuhrmann committed
117 118 119
void OverlayController::addChatLine(QString message)
{
	// generate new chatline
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
120 121 122 123 124
	QWidget* w = new QWidget;
	QLabel* newChatLine = new QLabel(w);

	w->setAttribute(Qt::WA_TranslucentBackground);
	w->setAttribute(Qt::WA_ShowWithoutActivating);
125
	w->setWindowFlags(Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint | Qt::WindowTransparentForInput | Qt::SplashScreen);
Carsten Fuhrmann's avatar
update  
Carsten Fuhrmann committed
126 127

	newChatLine->setText(message);
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
128
	newChatLine->setStyleSheet(m_stylesheet);
Carsten Fuhrmann's avatar
update  
Carsten Fuhrmann committed
129 130 131
	newChatLine->adjustSize();

	newChatLine->setAttribute(Qt::WA_ShowWithoutActivating);
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
132 133 134 135 136 137
	newChatLine->setWindowFlags(Qt::WindowStaysOnTopHint | Qt::WindowTransparentForInput);
	newChatLine->setAlignment(Qt::AlignCenter | Qt::AlignHCenter);

	w->adjustSize();
	w->setGeometry(BORDEROFFSET, BORDEROFFSET, w->size().width(), w->size().height() + SPACING);

Carsten Fuhrmann's avatar
update  
Carsten Fuhrmann committed
138
	newChatLine->show();
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
139
	w->show();
Carsten Fuhrmann's avatar
update  
Carsten Fuhrmann committed
140 141 142

	// move old messages down
	for (auto &it : m_msgLines)
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
143
		it->setGeometry(it->geometry().x(), it->geometry().y() + w->size().height(), it->geometry().width(), it->geometry().height());
Carsten Fuhrmann's avatar
update  
Carsten Fuhrmann committed
144 145

	// inseart new chat line
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
146
	m_msgLines.push_front(w);
Carsten Fuhrmann's avatar
update  
Carsten Fuhrmann committed
147 148 149 150 151

	// limit the number of lines
	if (m_msgLines.size() > MAXLINES)
		deleteChatLine(m_msgLines.last());

Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
152
	// hide after time
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
153 154 155 156 157
	QTimer* timer = new QTimer(this);
	timer->setSingleShot(true);
	timer->setInterval(TIMEOUT);
	connect(timer, &QTimer::timeout, [=]() {deleteChatLine(w, timer);});
	timer->start();
Carsten Fuhrmann's avatar
update  
Carsten Fuhrmann committed
158 159 160 161
}

void OverlayController::addSpeaker(QString name)
{
162 163 164
	if (m_muted)
		return;

Carsten Fuhrmann's avatar
update  
Carsten Fuhrmann committed
165
	int labelWidth = 0;
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
166 167 168 169 170
	QWidget* w = new QWidget;
	QLabel* newSpeaker = new QLabel(w);

	w->setAttribute(Qt::WA_TranslucentBackground);
	w->setAttribute(Qt::WA_ShowWithoutActivating);
171
	w->setWindowFlags(Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint | Qt::WindowTransparentForInput | Qt::SplashScreen);
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
172
	w->setObjectName(name);
Carsten Fuhrmann's avatar
update  
Carsten Fuhrmann committed
173 174

	newSpeaker->setText(name);
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
175
	newSpeaker->setStyleSheet(m_stylesheet);
Carsten Fuhrmann's avatar
update  
Carsten Fuhrmann committed
176
	newSpeaker->adjustSize();
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
177
	
Carsten Fuhrmann's avatar
update  
Carsten Fuhrmann committed
178
	newSpeaker->setAttribute(Qt::WA_ShowWithoutActivating);
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
179 180 181 182 183 184 185
	newSpeaker->setWindowFlags(Qt::WindowStaysOnTopHint | Qt::WindowTransparentForInput);
	newSpeaker->setAlignment(Qt::AlignCenter | Qt::AlignHCenter);

	w->adjustSize();
	w->setGeometry(BORDEROFFSET + m_speakerOffset, m_screenHeight - BORDEROFFSET - w->size().height(), w->size().width() + SPACING, w->size().height());
	labelWidth = w->geometry().width();

Carsten Fuhrmann's avatar
update  
Carsten Fuhrmann committed
186
	newSpeaker->show();
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
187
	w->show();
Carsten Fuhrmann's avatar
update  
Carsten Fuhrmann committed
188 189 190

	m_speakerOffset += labelWidth;

Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
191
	m_speakers.push_back(w);
Carsten Fuhrmann's avatar
update  
Carsten Fuhrmann committed
192 193 194
}

void OverlayController::removeSpeaker(QString name)
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
195
{
Carsten Fuhrmann's avatar
update  
Carsten Fuhrmann committed
196 197 198 199 200 201
	int index = -1;
	int labelWidth = 0;

	// move all speaker to the left side and find the one to delete
	for (int i = 0; i < m_speakers.size(); i++)
	{
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
202
		QWidget* it = m_speakers.at(i);
Carsten Fuhrmann's avatar
update  
Carsten Fuhrmann committed
203 204 205

		it->setGeometry(it->geometry().x() - labelWidth, it->geometry().y(), it->geometry().width(), it->geometry().height());

Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
206
		if (it->objectName() == name)
Carsten Fuhrmann's avatar
update  
Carsten Fuhrmann committed
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
		{
			labelWidth = it->geometry().width();
			m_speakerOffset -= labelWidth;
			index = i;
			delete it;
		}
	}

	// remove the deleted label from the list
	if (index != -1)
		m_speakers.remove(index);
}

void OverlayController::debugPrint(QString text)
{
	m_debugWindow->setText(text + "\n" + m_debugWindow->text());
}

void OverlayController::reset()
{
	for (auto &it : m_speakers)
		delete it;
	m_speakers.clear();

	for (auto &it : m_msgLines)
		delete it;
	m_msgLines.clear();

	m_speakerOffset = 0;
236 237
}

Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
238
void OverlayController::updateChannelList()
239
{
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
240
	// get list of all channelIDs
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
241
	uint64* channelIDList;
242
	if (m_ts3.getChannelList(m_SCHID, &channelIDList) != ERROR_ok)
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
243
		return;
244

Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
245 246 247 248 249 250
	// remove old stuff
	while (int nb = m_tree->getTree()->topLevelItemCount())
		delete m_tree->getTree()->takeTopLevelItem(nb - 1);
	m_clientList.clear();
	m_channelList.clear();

Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
251
	// get a list of all channels containing name, id, parent,..
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
252 253 254
	int i(0);
	while (channelIDList[i] != NULL)
	{
255
		ChannelInfo tmp;
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
256 257
		tmp.id = channelIDList[i];
		tmp.name = channelID2Name(m_SCHID, channelIDList[i]);
258 259
		m_ts3.getChannelVariableAsInt(m_SCHID, tmp.id, CHANNEL_ORDER, &tmp.order);
		m_ts3.getParentChannelOfChannel(m_SCHID, channelIDList[i], &tmp.parent);
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
260 261 262
		tmp.entry = new QTreeWidgetItem;
		tmp.entry->setData(0, Qt::DisplayRole, tmp.name);
		tmp.entry->setData(1, Qt::UserRole, tmp.id);
263
		if (tmp.name.startsWith('[') && tmp.name.contains("spacer"))
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
264
		{
265
			tmp.entry->setData(2, Qt::UserRole, spacer);
266 267
			tmp.name = tmp.name.remove(QRegExp("\\[.*spacer.*\\]"));
			tmp.entry->setData(0, Qt::DisplayRole, tmp.name);
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
268
		}
269
		else
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
270
		{
271
			tmp.entry->setData(2, Qt::UserRole, channel);
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
272 273
			tmp.entry->setIcon(0, m_channelIcon);
		}
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
274

Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
275
		m_channelList.push_back(tmp);
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
276 277 278
		i++;
	}

279
	if (m_treeState == full)
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

		// for every channel
		for (auto &parent : m_channelList)
		{
			// collect all childs
			QVector<ChannelInfo> childs;
			for (auto &child : m_channelList)
				if (child.parent == parent.id)
					childs.push_back(child);

			// add childs in correct order
			int prevID(0);
			while (!childs.isEmpty())
			{
				int i;
				for (i = 0; i < childs.size(); i++)
					if (childs[i].order == prevID)
						break;

				prevID = childs[i].id;
				parent.entry->addChild(childs.takeAt(i).entry);
			}
		}

		// get all toplvl channels
		QVector<ChannelInfo> topLvlList;
		for (auto &it : m_channelList)
			if (it.parent == 0)
				topLvlList.push_back(it);

		// add toplvl to tree in correct order
		int prevID = 0;
		while (!topLvlList.isEmpty())
314 315
		{
			int i;
316 317
			for (i = 0; i < topLvlList.size(); i++)
				if (topLvlList[i].order == prevID)
318 319
					break;

320 321
			prevID = topLvlList[i].id;
			m_tree->getTree()->addTopLevelItem(topLvlList.takeAt(i).entry);
322 323
		}
	}
324 325 326 327 328
	else if (m_treeState == minimum)
	{
		//TODO: different order
		uint64 channel = getCurrentChannel(m_SCHID);
		QTreeWidgetItem* item = NULL;
329

330 331 332
		for (auto &it : m_channelList)
			if (it.id == channel)
				item = it.entry;
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
333

334 335 336 337 338 339 340
		if (item == NULL)
		{
			debugPrint("i was here");
			m_ts3.freeMemory(channelIDList);
			return;
		}
		m_tree->getTree()->addTopLevelItem(item);
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
341 342 343

	}

Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
344
	m_tree->getTree()->expandAll();
345
	m_ts3.freeMemory(channelIDList);
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
346 347 348 349
}

void OverlayController::updateClientList()
{
350 351 352
	if (m_treeState == minimum)
		updateChannelList();

Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
353 354 355 356 357 358
	//TODO: remove all clients
	while (!m_clientList.isEmpty())
		delete m_clientList.takeFirst();

	//add all clients
	for (auto& it : m_channelList)
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
359 360
	{
		anyID* clientIDList;
361
		if (m_ts3.getChannelClientList(m_SCHID, it.id, &clientIDList) != ERROR_ok)
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
362
			continue;
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
363 364 365 366 367 368

		int i(0);
		while (clientIDList[i] != NULL)
		{
			QTreeWidgetItem* tmp = new QTreeWidgetItem;
			tmp->setData(0, Qt::DisplayRole, clientID2Name(m_SCHID, clientIDList[i]));
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
369
			tmp->setIcon(0, m_clientIcon);
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
370 371
			tmp->setData(1, Qt::UserRole, clientIDList[i]);
			tmp->setData(2, Qt::UserRole, client);
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
372
			m_clientList.push_back(tmp);
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
373 374 375 376

			it.entry->addChild(tmp);
			i++;
		}
377
		m_ts3.freeMemory(clientIDList);
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
378
	}
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
379
}
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
380

Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
381 382
void OverlayController::displayChannelList()
{
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
383
	m_tree->getTree()->adjustSize();
384
	m_tree->adjustSize();
385
	m_tree->show();
Carsten Fuhrmann's avatar
Carsten Fuhrmann committed
386
}
387 388 389 390 391 392 393 394 395 396 397 398

void OverlayController::mute(bool value)
{
	m_muted = value;

	if (m_muted)
	{
		m_speakerOffset = 0;
		while (!m_speakers.isEmpty())
			delete m_speakers.takeFirst();
	}
}