-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkaffeine.cpp
90 lines (76 loc) · 2.17 KB
/
kaffeine.cpp
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
#include "kaffeine.h"
#include <QtDebug>
#include <QApplication>
#include <QtDBus/QtDBus>
Kaffeine::Kaffeine(QWidget *parent)
{
(void)parent;
trayIcon = new QSystemTrayIcon();
iconEnabled = QIcon(":/icons/caffeine-cup-full.svg");
iconDisabled = QIcon(":/icons/caffeine-cup-empty.svg");
contextMenu = new QMenu("Kaffeine-indicator");
contextMenu->addAction("Toggle screensaver", this, SLOT(on_actionToggle()));
contextMenu->addAction("Exit", this, SLOT(on_actionQuit()));
trayIcon->setContextMenu(contextMenu);
trayIcon->setIcon(iconDisabled);
trayIcon->showMessage("Kaffeine-indicator", "Kaffeine disabled");
state = INACTIVE;
cookie = 0;
}
Kaffeine::~Kaffeine()
{
trayIcon->hide();
delete contextMenu;
delete trayIcon;
}
void Kaffeine::show()
{
trayIcon->show();
trayIcon->showMessage("Kaffeine-indicator", "Kaffeine is running");
}
void Kaffeine::on_actionQuit()
{
QApplication::quit();
}
void Kaffeine::on_actionToggle()
{
state ? state = INACTIVE : state = ACTIVE;
state ? trayIcon->setIcon(iconEnabled) : trayIcon->setIcon(iconDisabled);
state ? disableScreenSaver() : enableScreenSaver();
}
void Kaffeine::disableScreenSaver()
{
QDBusInterface iface("org.kde.screensaver", "/ScreenSaver", "org.freedesktop.ScreenSaver", QDBusConnection::sessionBus());
if (iface.isValid())
{
QDBusReply<uint> reply = iface.call("Inhibit", "Kaffeine-indicator", "User wished");
if (reply.isValid())
{
cookie = reply.value();
}
else
{
qCritical() << reply.error().message();
}
}
else
{
qCritical() << QDBusConnection::sessionBus().lastError().message();
}
}
void Kaffeine::enableScreenSaver()
{
QDBusInterface iface("org.kde.screensaver", "/ScreenSaver", "org.freedesktop.ScreenSaver", QDBusConnection::sessionBus());
if (iface.isValid())
{
QDBusReply<void> reply = iface.call("UnInhibit", cookie);
if (!reply.isValid())
{
qCritical() << reply.error().message();
}
}
else
{
qCritical() << QDBusConnection::sessionBus().lastError().message();
}
}