summaryrefslogtreecommitdiffstats
path: root/src/maingui/backdrop.cpp
blob: db16b01097e4ac3a9d2edc10cfa2b16f7bac3999 (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
#include "backdrop.h"
#include "../util.h"
#include "pwgui.h"

#include <QApplication>
#include <QDesktopWidget>
#include <QPainter>
#include <QPaintEvent>
#include <QPixmap>
#include <QRgb>
#include <QLocalServer>
#include <QLocalSocket>
#include <sys/stat.h>

static const QString strTest("test");

Backdrop::Backdrop() :
			QWidget(NULL),
			screenshot(NULL),
			mainWindow(NULL),
			server(NULL),
			pwgui(NULL)
{
	QPixmap shot = QPixmap::grabWindow(QApplication::desktop()->winId());
	if (!shot.isNull() && shot.height() > 0) {
		QImage img = shot.toImage();
		if (img.format() != QImage::Format_RGB32) {
			img = img.convertToFormat(QImage::Format_RGB32);
		}
		for (int i = 0; i < img.height(); ++i) {
			uchar *line = img.scanLine(i);
			if (line == NULL)
				continue;
			QRgb *rgb = (QRgb*)line;
			for (int x = 0; x < img.width(); ++x) {
				const int val = (qRed(*rgb)*11 + qGreen(*rgb)*16 + qBlue(*rgb)*5) / 32;
				*rgb = qRgb(val, val, val);
				rgb++;
			}
		}
		shot = QPixmap::fromImage(img);
	}
	screenshot = new QPixmap(shot);
	this->resize(screenshot->width(), screenshot->height());
	this->setWindowFlags(Qt::Tool | Qt::CustomizeWindowHint | Qt::FramelessWindowHint);

	char sockPath[500];
	if (NULL != util_sockPath(-1, -1, sockPath, sizeof sockPath)) {
		QString path = QString::fromUtf8(sockPath);
		QFile::remove(path);
		server = new QLocalServer(this);
		if (server->listen(sockPath)) {
			chmod(sockPath, 0600);
			QObject::connect(server, SIGNAL(newConnection()), this, SLOT(newConnection()));
		}
	}
	QObject::connect(QApplication::instance(), SIGNAL(aboutToQuit()), this, SLOT(aboutToQuit()));
}

QLocalSocket* Backdrop::getClient()
{
	QObject *sender = QObject::sender();
	if (sender == NULL || sender->property("test").toString() != strTest)
		return NULL;
	return (QLocalSocket*)sender;
}

void Backdrop::newConnection()
{
	QLocalSocket *client;
	while ((client = server->nextPendingConnection())) {
		client->setProperty("test", strTest);
		QObject::connect(client, SIGNAL(readyRead()), this, SLOT(incomingData()));
		QObject::connect(client, SIGNAL(error(QLocalSocket::LocalSocketError)), this, SLOT(closeConnection()));
		QObject::connect(client, SIGNAL(disconnected()), this, SLOT(closeConnection()));
	}
}

void Backdrop::closeConnection()
{
	QLocalSocket *client = getClient();
	client->close();
	client->waitForDisconnected(50);
	client->deleteLater();
}

void Backdrop::incomingData()
{
	QLocalSocket *client = getClient();
	char buffer[100];
	while (client->canReadLine()) {
		if (client->readLine(buffer, sizeof buffer) > 0) {
			if (strcmp(buffer, "open") == 0) {
				if (pwgui == NULL) {
					pwgui = new PwGui(this);
				}
				if (pwgui->isVisible()) {
					client->write("busy\n");
				} else {
					pwgui->exec();
					if (pwgui->isCancelled()) {
						server->blockSignals(true);
						client->write("cancel\n");
						client->waitForBytesWritten(1000);
						client->close();
						client->waitForDisconnected(50);
						QApplication::exit(0);
					}
					// Got some credentials, send to back end
					client->write(pwgui->getUser().toUtf8() += '\0' += pwgui->getPassword() += '\0');
				}
			}
		}
	}
}

Backdrop::~Backdrop()
{
	delete screenshot;
}

void Backdrop::paintEvent(QPaintEvent * event)
{
	QPainter p(this);
	p.drawPixmap(event->rect(), *screenshot, event->rect());
}

void Backdrop::mouseReleaseEvent(QMouseEvent * event)
{
	if (pwgui != NULL && pwgui->isVisible()) {
		pwgui->raise();
		pwgui->activateWindow();
	} else if (mainWindow != NULL && mainWindow->isVisible()) {
		mainWindow->raise();
		mainWindow->activateWindow();
	}
}

void Backdrop::aboutToQuit()
{
	if (server != NULL) {
		QFile::remove(server->serverName());
		server->close();
	}
}