File: fakelogind.cpp

package info (click to toggle)
solid 5.116.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,420 kB
  • sloc: cpp: 19,542; xml: 464; lex: 111; yacc: 83; sh: 14; makefile: 5
file content (58 lines) | stat: -rw-r--r-- 1,293 bytes parent folder | download
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"