/
common.cpp
190 lines (175 loc) · 6.41 KB
/
common.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
# powerdwarf <https://github.com/rodlie/powerdwarf>
# Copyright (c) 2018, Ole-André Rodlie <ole.andre.rodlie@gmail.com> All rights reserved.
#
# Available under the 3-clause BSD license
# See the LICENSE file for full details
*/
#include "common.h"
#include <QFile>
#include <QFileInfo>
#include <QIcon>
#include <QApplication>
#include <QDir>
#include <QSettings>
#include <QDebug>
#include <QDirIterator>
#include <QTextStream>
#include "def.h"
void Common::savePowerSettings(QString type, QVariant value)
{
QSettings settings(PD, PD);
settings.setValue(type, value);
settings.sync();
}
QVariant Common::loadPowerSettings(QString type)
{
QSettings settings(PD, PD);
return settings.value(type);
}
bool Common::validPowerSettings(QString type)
{
QSettings settings(PD, PD);
return settings.value(type).isValid();
}
void Common::setIconTheme()
{
// setup icon theme search path
QStringList iconsPath = QIcon::themeSearchPaths();
QString iconsHomeLocal = QString("%1/.local/share/icons").arg(QDir::homePath());
QString iconsHome = QString("%1/.icons").arg(QDir::homePath());
if (QFile::exists(iconsHomeLocal) && !iconsPath.contains(iconsHomeLocal)) { iconsPath.prepend(iconsHomeLocal); }
if (QFile::exists(iconsHome) && !iconsPath.contains(iconsHome)) { iconsPath.prepend(iconsHome); }
iconsPath << QString("%1/../share/icons").arg(qApp->applicationDirPath());
QIcon::setThemeSearchPaths(iconsPath);
qDebug() << "using icon theme search path" << QIcon::themeSearchPaths();
QString theme = QIcon::themeName();
if (theme.isEmpty() || theme == "hicolor") { // try to load saved theme
qDebug() << "checking for icon theme in settings";
theme = loadPowerSettings("icon_theme").toString();
}
if(theme.isEmpty() || theme == "hicolor") { // Nope, then scan for first available
// gtk
if(QFile::exists(QDir::homePath() + "/" + ".gtkrc-2.0")) {
qDebug() << "checking for icon theme in gtkrc-2.0";
QSettings gtkFile(QDir::homePath() + "/.gtkrc-2.0", QSettings::IniFormat);
theme = gtkFile.value("gtk-icon-theme-name").toString().remove("\"");
} else {
qDebug() << "checking for icon theme in gtk-3.0";
QSettings gtkFile(QDir::homePath() + "/.config/gtk-3.0/settings.ini", QSettings::IniFormat);
theme = gtkFile.value("gtk-fallback-icon-theme").toString().remove("\"");
}
// fallback
if(theme.isNull()) {
qDebug() << "checking for icon theme in static fallback";
QStringList themes;
themes << QString("%1/../share/icons/Humanity").arg(qApp->applicationFilePath());
themes << "/usr/share/icons/Humanity" << "/usr/local/share/icons/Humanity";
themes << QString("%1/../share/icons/Adwaita").arg(qApp->applicationFilePath());
themes << "/usr/share/icons/Adwaita" << "/usr/local/share/icons/Adwaita";
themes << QString("%1/../share/icons/gnome").arg(qApp->applicationFilePath());
themes << "/usr/share/icons/gnome" << "/usr/local/share/icons/gnome";
themes << QString("%1/../share/icons/oxygen").arg(qApp->applicationFilePath());
themes << "/usr/share/icons/oxygen" << "/usr/local/share/icons/oxygen";
themes << QString("%1/../share/icons/hicolor").arg(qApp->applicationFilePath());
themes << QString("%1/../share/icons/Tango").arg(qApp->applicationFilePath());
themes << "/usr/share/icons/Tango" << "/usr/local/share/icons/Tango";
themes << "/usr/share/icons/hicolor" << "/usr/local/share/icons/hicolor";
for (int i=0;i<themes.size();++i) {
if (QFile::exists(themes.at(i))) {
theme = QString(themes.at(i)).split("/").takeLast();
break;
}
}
}
if (theme != "hicolor" && !theme.isEmpty()) {
qDebug() << "save icon theme for later use";
savePowerSettings("icon_theme", theme);
} else {
qDebug() << "No icons available!";
}
}
QIcon::setThemeName(theme);
}
QString Common::confFile()
{
return QString("%1/.config/powerdwarf/powerdwarf.conf").arg(QDir::homePath());
}
QString Common::confDir()
{
return QString("%1/.config/powerdwarf").arg(QDir::homePath());
}
bool Common::kernelCanResume()
{
#ifdef __FreeBSD__
// ???
return true;
#endif
QFile cmdline("/proc/cmdline");
if (cmdline.open(QIODevice::ReadOnly)) {
QByteArray result = cmdline.readAll();
cmdline.close();
if (result.contains("resume=")) { return true;}
}
return false;
}
QString Common::backlightDevice()
{
#ifdef __FreeBSD__
// ???
return QString();
#else
QString path = "/sys/class/backlight";
QDirIterator it(path, QDirIterator::Subdirectories);
while (it.hasNext()) {
QString foundDir = it.next();
if (foundDir.startsWith(QString("%1/radeon").arg(path))) {
return foundDir;
} else if (foundDir.startsWith(QString("%1/intel").arg(path))) {
return foundDir;
} else if (foundDir.startsWith(QString("%1/acpi").arg(path))) {
return foundDir;
}
}
return QString();
#endif
}
bool Common::canAdjustBacklight(QString device)
{
QFileInfo backlight(QString("%1/brightness").arg(device));
if (backlight.isWritable()) { return true; }
return false;
}
int Common::backlightMax(QString device)
{
int result = 0;
QFile backlight(QString("%1/max_brightness").arg(device));
if (backlight.open(QIODevice::ReadOnly)) {
result = backlight.readAll().trimmed().toInt();
backlight.close();
}
return result;
}
int Common::backlightValue(QString device)
{
int result = 0;
QFile backlight(QString("%1/brightness").arg(device));
if (backlight.open(QIODevice::ReadOnly)) {
result = backlight.readAll().trimmed().toInt();
backlight.close();
}
return result;
}
bool Common::adjustBacklight(QString device, int value)
{
if (!canAdjustBacklight(device)) { return false; }
QFile backlight(QString("%1/brightness").arg(device));
if (backlight.open(QIODevice::WriteOnly|QIODevice::Truncate)) {
QTextStream out(&backlight);
if (value<1) { value = 1; }
out << QString::number(value);
backlight.close();
if (value == backlightValue(device)) { return true;}
}
return false;
}