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
|
/*
SPDX-FileCopyrightText: 2014 Alejandro Fiestas Olivares <afiestas@kde.org>
SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/
#include "fakelogind.h"
#include <fcntl.h>
#include <qtemporaryfile.h>
#include <sys/stat.h>
#include <unistd.h>
#include <QDebug>
#include <QTemporaryFile>
#include <QTimer>
FakeLogind::FakeLogind(QObject *parent)
: QObject(parent)
{
}
QDBusUnixFileDescriptor FakeLogind::Inhibit(const QString &what, const QString &who, const QString &why, const QString &mode)
{
Q_EMIT newInhibition(what, who, why, mode);
QDBusUnixFileDescriptor foo;
foo.setFileDescriptor(-1);
QTemporaryFile file;
if (!file.open()) {
qDebug() << "Could not open a temporary file";
return foo;
}
m_fd = file.handle();
foo.giveFileDescriptor(m_fd);
// We could use epoll for this, but it will make the code harder to read for a test.
auto t = new QTimer();
t->setInterval(100);
connect(t, SIGNAL(timeout()), SLOT(checkFd()));
t->start();
return foo;
}
void FakeLogind::checkFd()
{
int e = fcntl(m_fd, F_GETFD) != -1 || errno != EBADF;
if (e == 0) {
Q_EMIT inhibitionRemoved();
delete sender();
}
}
#include "moc_fakelogind.cpp"
|