File indexing completed on 2024-11-10 09:46:01

0001 /*
0002     SPDX-FileCopyrightText: 2014 Alejandro Fiestas Olivares <afiestas@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #include "fakelogind.h"
0008 
0009 #include <fcntl.h>
0010 #include <qtemporaryfile.h>
0011 #include <sys/stat.h>
0012 #include <unistd.h>
0013 
0014 #include <QDebug>
0015 #include <QTemporaryFile>
0016 #include <QTimer>
0017 
0018 FakeLogind::FakeLogind(QObject *parent)
0019     : QObject(parent)
0020 {
0021 }
0022 
0023 QDBusUnixFileDescriptor FakeLogind::Inhibit(const QString &what, const QString &who, const QString &why, const QString &mode)
0024 {
0025     Q_EMIT newInhibition(what, who, why, mode);
0026 
0027     QDBusUnixFileDescriptor foo;
0028     foo.setFileDescriptor(-1);
0029 
0030     QTemporaryFile file;
0031     if (!file.open()) {
0032         qDebug() << "Could not open a temporary file";
0033         return foo;
0034     }
0035 
0036     m_fd = file.handle();
0037     foo.giveFileDescriptor(m_fd);
0038 
0039     // We could use epoll for this, but it will make the code harder to read for a test.
0040     auto t = new QTimer();
0041     t->setInterval(100);
0042     connect(t, SIGNAL(timeout()), SLOT(checkFd()));
0043     t->start();
0044 
0045     return foo;
0046 }
0047 
0048 void FakeLogind::checkFd()
0049 {
0050     int e = fcntl(m_fd, F_GETFD) != -1 || errno != EBADF;
0051 
0052     if (e == 0) {
0053         Q_EMIT inhibitionRemoved();
0054         delete sender();
0055     }
0056 }
0057 
0058 #include "moc_fakelogind.cpp"