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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
|
/*
* Copyright (C) 2023, KylinSoft Co., Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301, USA.
**/
#include "ukuilockinfo.h"
#include "../ukui-session/xdgdesktopfile.h"
#include <QDBusInterface>
#include <QSettings>
#include <QDebug>
#include <QFile>
#include <QDir>
#include <glib.h>
#include <QDBusMetaType>
QDBusArgument &InhibitInfo::operator<<(QDBusArgument &argument, const InhibitInfo::InhibitorInfo &mystruct)
{
argument.beginStructure();
argument << mystruct.name << mystruct.icon;
argument.endStructure();
return argument;
}
const QDBusArgument &InhibitInfo::operator>>(const QDBusArgument &argument, InhibitInfo::InhibitorInfo &mystruct)
{
argument.beginStructure();
argument >> mystruct.name >> mystruct.icon ;
argument.endStructure();
return argument;
}
Ukuilockinfo::Ukuilockinfo()
{
}
QVector<InhibitInfo::InhibitorInfo> Ukuilockinfo::listInhibitorInfo(Ukuilockinfo::InhibitorType type)
{
QVector<InhibitInfo::InhibitorInfo> result;
switch (type) {
case InhibitorType::logout: {
getLogoutInhibitor(result);
break;
}
case InhibitorType::suspend: {
getSystemdInhibitor(QString("sleep"), result);
break;
}
case InhibitorType::shutdown: {
getSystemdInhibitor(QString("shutdown"), result);
break;
}
}
return result;
}
bool Ukuilockinfo::getCfgValue(Ukuilockinfo::buttonType button)
{
bool buttonActive = false;
bool newIniFile = false;//ini文件是否为新建文件
QString iniDir = "/usr/share/ukui/ukui-session-manager/config";
if (!QFile::exists(iniDir + "/btnconfig.ini")) {
qDebug() << "btnconfig.ini file is not exists!!!";
QDir dir(iniDir);
if (!dir.exists(iniDir)) {
if (dir.mkdir(iniDir)) {//目前创建不成功 没有权限
QFile iniFile(iniDir + "/btnconfig.ini");
if (iniFile.open(QIODevice::WriteOnly)) {
newIniFile = true;
iniFile.close();
}
qDebug() << "inifile open faile!";
} else {
qDebug() << "create inidir faile!";
}
}
}
QSettings *cfgSettings = new QSettings("/usr/share/ukui/ukui-session-manager/config/btnconfig.ini", QSettings::IniFormat);
if (newIniFile) {//貌似路径下文件只可读不可写
cfgSettings->setValue("btn/SwitchUserBtnHide", false);
cfgSettings->setValue("btn/HibernateBtnHide", false);
cfgSettings->setValue("btn/LockScreenBtnHide", false);
cfgSettings->setValue("btn/LogoutBtnHide", false);
cfgSettings->setValue("btn/RebootBtnHide", false);
cfgSettings->setValue("btn/ShutDownBtnHide", false);
cfgSettings->setValue("btn/SuspendBtnHide", false);
}
switch (button) {
case switchuserBtn:
buttonActive = !(cfgSettings->value("btn/SwitchUserBtnHide").toBool());
break;
case hibernateBtn:
buttonActive = !(cfgSettings->value("btn/HibernateBtnHide").toBool());
break;
case suspendBtn:
buttonActive = !(cfgSettings->value("btn/SuspendBtnHide").toBool());
break;
case lockscreenBtn:
buttonActive = !(cfgSettings->value("btn/LockScreenBtnHide").toBool());
break;
case logoutBtn:
buttonActive = !(cfgSettings->value("btn/LogoutBtnHide").toBool());
break;
case rebootBtn:
buttonActive = !(cfgSettings->value("btn/RebootBtnHide").toBool());
break;
case shutdownBtn:
buttonActive = !(cfgSettings->value("btn/ShutDownBtnHide").toBool());
break;
default:
break;
}
return buttonActive;
}
void Ukuilockinfo::getSystemdInhibitor(QString type, QVector<InhibitInfo::InhibitorInfo> &inhibitorVec)
{
qDBusRegisterMetaType<SystemdInhibitor::Inhibitor>();
QDBusInterface loginInterface("org.freedesktop.login1", "/org/freedesktop/login1",
"org.freedesktop.login1.Manager", QDBusConnection::systemBus());
if (loginInterface.isValid()) {
qDebug() << "create interface success";
}
QDBusMessage result = loginInterface.call("ListInhibitors");
QList<QVariant> outArgs = result.arguments();
QVariant first = outArgs.at(0);
const QDBusArgument &dbusArgs = first.value<QDBusArgument>();
dbusArgs.beginArray();
while (!dbusArgs.atEnd()) {
SystemdInhibitor::Inhibitor inhibitor;
dbusArgs >> inhibitor;
if (inhibitor.mode == QString("block") && inhibitor.action.contains(type)) {
InhibitInfo::InhibitorInfo inhibitInfo;
findNameAndIcon(inhibitor.name, inhibitInfo);
if (inhibitInfo.name.isEmpty()) {
inhibitInfo.name = inhibitor.name;
}
inhibitorVec.push_back(inhibitInfo);
}
}
dbusArgs.endArray();
}
void Ukuilockinfo::findNameAndIcon(QString &inhibitorName, InhibitInfo::InhibitorInfo &inhibitor)
{
QString icon;
QString name;
QStringList desktop_paths = { "/usr/share/applications", "/etc/xdg/autostart" };
for (const QString &dirName : const_cast<const QStringList&>(desktop_paths)) {
QDir dir(dirName);
if (!dir.exists()) {
continue;
}
const QFileInfoList files = dir.entryInfoList(QStringList(QLatin1String("*.desktop")), QDir::Files | QDir::Readable);
for (const QFileInfo &fi : files) {
QString base = fi.baseName();
if (base == inhibitorName) {
XdgDesktopFile desktopFile;
desktopFile.load(fi.absoluteFilePath());
icon = desktopFile.value("Icon").toString();
name = getAppLocalName(fi.absoluteFilePath());//根据系统的本地语言设置获取对应的名称
inhibitor.name = name;
inhibitor.icon = icon;
}
}
}
}
QString Ukuilockinfo::getAppLocalName(QString desktopfp)
{
GError **error = nullptr;
GKeyFileFlags flags = G_KEY_FILE_NONE;
GKeyFile *keyfile = g_key_file_new();
QByteArray fpbyte = desktopfp.toLocal8Bit();
char *filepath = fpbyte.data();
g_key_file_load_from_file(keyfile, filepath, flags,error);
char *name = g_key_file_get_locale_string(keyfile, "Desktop Entry", "Name", nullptr, nullptr);
QString namestr = QString::fromLocal8Bit(name);
g_key_file_free(keyfile);
return namestr;
}
void Ukuilockinfo::getLogoutInhibitor(QVector<InhibitInfo::InhibitorInfo> &inhibitorVec)
{
QDBusInterface sessionInterface("org.gnome.SessionManager", "/org/gnome/SessionManager",
"org.gnome.SessionManager", QDBusConnection::sessionBus());
if (!sessionInterface.isValid()) {
qDebug() << "interface not usable";
return;
}
QDBusMessage result = sessionInterface.call("GetInhibitors");
auto res = result.arguments().toVector();
for (auto it = res.begin(); it != res.end(); ++it) {
QString info = it->toString();
if (info.isEmpty()) {
continue;
}
QStringList infolist = info.split("/");
InhibitInfo::InhibitorInfo inhibitorInfo;
inhibitorInfo.name = infolist[0];
inhibitorInfo.icon = infolist[2];
inhibitorVec.push_back(inhibitorInfo);
}
}
QDBusArgument &SystemdInhibitor::operator<<(QDBusArgument &argument, const SystemdInhibitor::Inhibitor &mystruct)
{
argument.beginStructure();
argument << mystruct.action << mystruct.name << mystruct.reason << mystruct.mode << mystruct.uid << mystruct.pid;
argument.endStructure();
return argument;
}
const QDBusArgument &SystemdInhibitor::operator>>(const QDBusArgument &argument, SystemdInhibitor::Inhibitor &mystruct)
{
argument.beginStructure();
argument >> mystruct.action >> mystruct.name >> mystruct.reason >> mystruct.mode >> mystruct.uid >> mystruct.pid;
argument.endStructure();
return argument;
}
|