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 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
|
/*
* Copyright (C) 2023, KylinSoft Co., Ltd.
* Copyright (C) 2010-2016 LXQt team
*
* 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 <QApplication>
#include <QWidget>
#include <QDebug>
#include <QDesktopWidget>
#include <QFile>
#include <QTranslator>
#include <QCommandLineParser>
#include <QString>
#include <QSoundEffect>
#include <QTimer>
#include <QGSettings/QGSettings>
#include <X11/Xlib.h>
#include <QMessageBox>
#include <QDBusInterface>
#include <QDBusReply>
#include "ukuipower.h"
#include "mainwindow.h"
#include "loginedusers.h"
#include "lockchecker.h"
#include <QPushButton>
#include <sys/file.h>
#include <stdio.h>
#include <stdlib.h>
#include <pwd.h>
#include <ukui-log4qt.h>
#include <QStandardPaths>
#include "plasma-shell-manager.h"
#ifdef signals
#undef signals
#endif
bool messageboxCheck()
{
QMessageBox msgBox;
// msgBox.setWindowTitle(QObject::tr("conform"));
msgBox.setIcon(QMessageBox::Warning);
msgBox.setWindowFlags(Qt::WindowStaysOnTopHint);
// msgBox.setModal(false);
msgBox.setText(QObject::tr("Multiple users are logged in at the same time.Are you sure you want to close this system?"));
QPushButton *cancelButton = msgBox.addButton(QObject::tr("cancel"), QMessageBox::ActionRole);
QPushButton *confirmButton = msgBox.addButton(QObject::tr("confirm"), QMessageBox::RejectRole);
msgBox.exec();
if (msgBox.clickedButton() == cancelButton) {
qDebug() << "cancel!";
return false;
} else if (msgBox.clickedButton() == confirmButton) {
qDebug() << "confirm!";
return true;
} else {
return false;
}
}
void messagecheck()
{
QMessageBox msgBox;
msgBox.setIcon(QMessageBox::Warning);
msgBox.setWindowFlags(Qt::WindowStaysOnTopHint);
QString t1 = QObject::tr("System update or package installation in progress,this function is temporarily unavailable.");
QString t2 = QObject::tr("System backup or restore in progress,this function is temporarily unavailable.");
QFile file_update("/tmp/lock/kylin-update.lock");
QFile file_backup("/tmp/lock/kylin-backup.lock");
if (file_update.exists()) {
msgBox.setText(t1);
}
if (file_backup.exists()) {
msgBox.setText(t2);
}
QPushButton *cancelButton = msgBox.addButton(QObject::tr("OK"), QMessageBox::RejectRole);
msgBox.exec();
if (msgBox.clickedButton() == cancelButton) {
qDebug() << "OK!";
}
}
bool playShutdownMusic(UkuiPower &powermanager, int num, QTimer *up_to_time)
{
bool play_music = false;
QGSettings *gs = new QGSettings("org.ukui.session", "/org/ukui/desktop/session/");
if (num == 4) {
play_music = gs->get("logout-music").toBool();
}
if (num == 5 || num == 6) {
play_music = gs->get("poweroff-music").toBool();
}
if (num == 0 || num == 1 || num == 2) {
play_music = false;
}
gs->set("win-key-release", false);
static int action = num;
if (num == 4 || num == 0) {
QDBusInterface dbus("org.gnome.SessionManager", "/org/gnome/SessionManager",
"org.gnome.SessionManager", QDBusConnection::sessionBus());
if (!dbus.isValid()) {
qWarning() << "dbusCall: QDBusInterface is invalid";
return false;
}
QDBusMessage msg;
// if (num == 4) {
// msg = dbus.call("emitStartLogout");
// }
if (num == 0) {
msg = dbus.call("emitPrepareForSwitchuser");
}
if (!msg.errorName().isEmpty()) {
qWarning() << "Dbus error: " << msg;
}
}
if (play_music) {
// up_to_time and soundplayer can not be define out of this if().
// otherwise run ukui-session-tools --suspend with segmente error.
// because they will be delate at the end of the playShutdownMusic().
QObject::connect(up_to_time, &QTimer::timeout, [&]() {
powermanager.doAction(UkuiPower::Action(action));
exit(0);
});
QString xdg_session_type = qgetenv("XDG_SESSION_TYPE");
if (num == 5 || num == 6) {
if (xdg_session_type == "wayland") {
QProcess::startDetached("paplay /usr/share/ukui/ukui-session-manager/shutdown.wav");
} else {
QProcess::startDetached("aplay /usr/share/ukui/ukui-session-manager/shutdown.wav");
}
up_to_time->start(5000);
} else if (num == 4) {
if (xdg_session_type == "wayland") {
QProcess::startDetached("paplay /usr/share/ukui/ukui-session-manager/logout.wav");
} else {
QProcess::startDetached("aplay /usr/share/ukui/ukui-session-manager/logout.wav");
}
up_to_time->start(2000);
} else {
qDebug() << "error num";
return false;
}
} else {
powermanager.doAction(UkuiPower::Action(action));
exit(0);
}
return false;
}
/**
* @brief 判断当前是否处于锁屏
*/
bool screensaverIsActive()
{
QString SS_DBUS_SERVICE = "org.ukui.ScreenSaver";
QString SS_DBUS_PATH = "/";
QString SS_DBUS_INTERFACE = "org.ukui.ScreenSaver";
QString displayNum = QString(qgetenv("DISPLAY")).replace(":", "").replace(".", "_");
qDebug() << "dispaly = " << displayNum ;
QString sessionDbus = QString("%1%2").arg(SS_DBUS_SERVICE).arg(displayNum);
QDBusInterface *interface = new QDBusInterface(sessionDbus, SS_DBUS_PATH, SS_DBUS_INTERFACE);
if (!interface->isValid()) {
qDebug() << "interface not valid";
delete interface;
interface = new QDBusInterface(SS_DBUS_SERVICE, SS_DBUS_PATH, SS_DBUS_INTERFACE);
}
QDBusMessage response = interface->call("GetLockState");
//判断method是否被正确返回
if (response.type() == QDBusMessage::ReplyMessage) {
//从返回参数获取返回值
bool isActive = response.arguments()[0].toBool();
qDebug() << "screensaver isActive:" << isActive;
return isActive;
} else {
qDebug() << "QDBusMessage failed!";
return false;
}
}
int main(int argc, char* argv[])
{
initUkuiLog4qt("ukui-session-tools");
#if (QT_VERSION >= QT_VERSION_CHECK(5, 12, 0))
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
#endif
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
QApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
#endif
QStringList homePath = QStandardPaths::standardLocations(QStandardPaths::HomeLocation);
int fd = open(QString(homePath.at(0) + "/.config/ukui-session-tools%1.lock").arg(getenv("DISPLAY")).toUtf8().data(),
O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
if (fd < 0) {
exit(1);
}
if (lockf(fd, F_TLOCK, 0)) {
qDebug() << "Can't lock single file, ukui-session-tools is already running!";
exit(0);
}
qunsetenv("QT_IM_MODULE");
qunsetenv("SESSION_MANAGER");
QApplication a(argc, argv);
if (QGSettings::isSchemaInstalled("org.ukui.style")) {
QGSettings *settings = new QGSettings("org.ukui.style");
if (qApp->property("noChangeSystemFontSize").isValid() && qApp->property("noChangeSystemFontSize").toBool()) {
return 0;
}
double fontSize = settings->get("system-font-size").toString().toDouble();
QString fontType = settings->get("systemFont").toString();
if (fontSize > 0) {
QFont font = QApplication::font();
font.setPointSizeF(fontSize);
font.setStyleName(fontType);
QApplication::setFont(font);
}
}
// Load ts files
const QString locale = QLocale::system().name();
QTranslator translator;
qDebug() << "local: " << locale;
qDebug() << "path: " << QStringLiteral(UKUI_TRANSLATIONS_DIR) + QStringLiteral("/ukui-session-manager");
if (translator.load(locale, QStringLiteral(UKUI_TRANSLATIONS_DIR) + QStringLiteral("/ukui-session-manager"))) {
a.installTranslator(&translator);
} else {
qDebug() << "Load translations file failed!";
}
UkuiPower powermanager(&a);
bool flag = true;
// define in the main() avoid scope error.
QTimer *up_to_time = new QTimer();
up_to_time->setSingleShot(true);
QGSettings *gs = new QGSettings("org.ukui.session", "/org/ukui/desktop/session/");
gs->set("win-key-release", true);
//初始化窗口 Load qss file
MainWindow *w = new MainWindow();
QFile qss(":/powerwin.qss");
qss.open(QFile::ReadOnly);
a.setStyleSheet(qss.readAll());
qss.close();
QCommandLineParser parser;
parser.setApplicationDescription(
QApplication::tr("UKUI session tools, show the shutdown dialog without any arguments."));
const QString VERINFO = QStringLiteral("2.0");
a.setApplicationVersion(VERINFO);
parser.addHelpOption();
parser.addVersionOption();
QCommandLineOption switchuserOption(QStringLiteral("switchuser"),
QApplication::tr("Switch the user of this computer."));
parser.addOption(switchuserOption);
QCommandLineOption hibernateOption(QStringLiteral("hibernate"),
QApplication::tr("Hibernate this computer."));
parser.addOption(hibernateOption);
QCommandLineOption suspendOption(QStringLiteral("suspend"),
QApplication::tr("Suspend this computer."));
parser.addOption(suspendOption);
QCommandLineOption logoutOption(QStringLiteral("logout"),
QApplication::tr("Logout this computer."));
parser.addOption(logoutOption);
QCommandLineOption rebootOption(QStringLiteral("reboot"),
QApplication::tr("Restart this computer."));
parser.addOption(rebootOption);
QCommandLineOption shutdownOption(QStringLiteral("shutdown"),
QApplication::tr("Shutdown this computer."));
parser.addOption(shutdownOption);
parser.process(a);
if (parser.isSet(switchuserOption)) {
flag = playShutdownMusic(powermanager, 0, up_to_time);
}
if (parser.isSet(hibernateOption)) {
QVector<InhibitInfo::InhibitorInfo> res = LockChecker::listInhibitor("sleep");
if (res.isEmpty()) {
flag = playShutdownMusic(powermanager, 1, up_to_time);
} else {
w->showInhibitWarning(res, 1);
w->show();
}
}
if (parser.isSet(suspendOption)) {
QVector<InhibitInfo::InhibitorInfo> res = LockChecker::listInhibitor("sleep");
if (res.isEmpty()) {
flag = playShutdownMusic(powermanager, 2, up_to_time);
} else {
w->showInhibitWarning(res, 2);
w->show();
}
}
if (parser.isSet(logoutOption)) {
QVector<InhibitInfo::InhibitorInfo> res = LockChecker::listInhibitor("shutdown");
if (res.isEmpty()) {
flag = playShutdownMusic(powermanager, 4, up_to_time);
} else {
w->showInhibitWarning(res, 4);
w->show();
}
}
if (parser.isSet(rebootOption)) {
QVector<InhibitInfo::InhibitorInfo> res = LockChecker::listInhibitor("shutdown");
if (res.isEmpty()) {
if (LockChecker::getLoginedUsers().count() > 1) {//提醒多个用户登录的情况
if (messageboxCheck()) {
flag = playShutdownMusic(powermanager, 5, up_to_time);
} else {
gs->set("win-key-release", false);
return 0;
}
} else {
flag = playShutdownMusic(powermanager, 5, up_to_time);
}
} else {
w->showInhibitWarning(res, 5);
w->show();
}
}
if (parser.isSet(shutdownOption)) {
QVector<InhibitInfo::InhibitorInfo> res = LockChecker::listInhibitor("shutdown");
if (res.isEmpty()) {
if (LockChecker::getLoginedUsers().count() > 1) {//提醒多个用户登录的情况
if (messageboxCheck()) {
flag = playShutdownMusic(powermanager, 6, up_to_time);
} else {
gs->set("win-key-release", false);
return 0;
}
} else {
flag = playShutdownMusic(powermanager, 6, up_to_time);
}
} else {
w->showInhibitWarning(res, 6);
w->show();
}
}
if (screensaverIsActive()) {
gs->set("win-key-release", false);
exit(0);
}
if (flag) {
w->show();
QObject::connect(w, &MainWindow::signalTostart, [&]() {
// 从界面点击 切换用户、睡眠和休眠 按钮,则等待界面隐藏再执行命令
if (w->defaultnum < 3) {
w->hide();
QTimer::singleShot(500, [&]() {
playShutdownMusic(powermanager, w->defaultnum, up_to_time);
});
} else {
playShutdownMusic(powermanager, w->defaultnum, up_to_time);
}
});
}
return a.exec();
}
|