Solid

fdinhibition.cpp
1/*
2 SPDX-FileCopyrightText: 2014 Alejandro Fiestas Olivares <afiestas@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5*/
6
7#include "fdinhibition.h"
8#include "logindinhibitionargument.h"
9
10#include <unistd.h>
11
12#include <QCoreApplication>
13#include <QDBusConnection>
14#include <QDBusUnixFileDescriptor>
15#include <QDebug>
16#include <QVariant>
17
18using namespace Solid;
19
20FdInhibition::FdInhibition(Power::InhibitionTypes inhibitions, const QString &description, QObject *parent)
21 : AbstractInhibition(parent)
22 , m_state(Inhibition::Stopped)
23 , m_description(description)
24 , m_inhibitions(inhibitions)
25{
26}
27
28FdInhibition::~FdInhibition()
29{
30 close(m_fd);
31}
32
33void FdInhibition::start()
34{
35 QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.login1"),
36 QStringLiteral("/org/freedesktop/login1"),
37 QStringLiteral("org.freedesktop.login1.Manager"),
38 QStringLiteral("Inhibit"));
39
40 QList<QVariant> args;
41 args << LogindInhibitionArgument::fromPowerState(m_inhibitions) << QCoreApplication::instance()->applicationName() << m_description
42 << QStringLiteral("block");
43 msg.setArguments(args);
44
45 QDBusConnection::systemBus().callWithCallback(msg, this, SLOT(slotDBusReply(QDBusMessage)), SLOT(slotDBusError(QDBusError)));
46}
47
48void FdInhibition::stop()
49{
50 close(m_fd);
51 setState(Inhibition::Stopped);
52}
53
54void FdInhibition::setDescription(const QString &description)
55{
56 m_description = description;
57}
58
59void FdInhibition::slotDBusReply(const QDBusMessage &msg)
60{
61 m_fd = msg.arguments().first().value<QDBusUnixFileDescriptor>().fileDescriptor();
62 setState(Inhibition::Started);
63}
64
65void FdInhibition::slotDBusError(const QDBusError &error)
66{
67 qDebug() << error.message();
68}
69
70void FdInhibition::setState(const Inhibition::State &state)
71{
72 if (m_state == state) {
73 return;
74 }
75
76 m_state = state;
77 Q_EMIT stateChanged(m_state);
78}
79
80Inhibition::State FdInhibition::state() const
81{
82 return m_state;
83}
84
85#include "moc_fdinhibition.cpp"
Represents an inhibition, allows to stop and start it.
Holds an inhibition.
Definition inhibition.h:27
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
KGuiItem close()
The single responsibility of this class is to create arguments valid for logind Inhibit call.
Definition fakebattery.h:16
QCoreApplication * instance()
bool callWithCallback(const QDBusMessage &message, QObject *receiver, const char *returnMethod, const char *errorMethod, int timeout) const const
QDBusConnection systemBus()
QList< QVariant > arguments() const const
QDBusMessage createMethodCall(const QString &service, const QString &path, const QString &interface, const QString &method)
void setArguments(const QList< QVariant > &arguments)
Q_EMITQ_EMIT
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Oct 4 2024 11:53:07 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.