2020-03-14 22:27:32 +08:00
|
|
|
|
/*
|
|
|
|
|
|
* Copyright (C) 2019 Tianjin KYLIN Information Technology Co., Ltd.
|
|
|
|
|
|
*
|
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
|
|
* the Free Software Foundation; either version 2.1, 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, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
*
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2020-01-17 17:23:40 +08:00
|
|
|
|
#include "startmenu.h"
|
|
|
|
|
|
#include <QMouseEvent>
|
|
|
|
|
|
#include <QHBoxLayout>
|
|
|
|
|
|
#include <QScreen>
|
|
|
|
|
|
#include <QDebug>
|
2020-02-07 16:28:43 +08:00
|
|
|
|
#include <QMenu>
|
2020-03-11 00:26:29 +08:00
|
|
|
|
#include <QStyle>
|
2020-10-30 15:21:48 +08:00
|
|
|
|
#include <QFile>
|
|
|
|
|
|
#include "QDebug"
|
|
|
|
|
|
#include "QByteArray"
|
|
|
|
|
|
#include "QFileInfo"
|
|
|
|
|
|
|
2020-03-11 00:26:29 +08:00
|
|
|
|
#include "../panel/customstyle.h"
|
2020-04-03 15:11:35 +08:00
|
|
|
|
|
|
|
|
|
|
UKUIStartMenuPlugin::UKUIStartMenuPlugin(const IUKUIPanelPluginStartupInfo &startupInfo):
|
2020-01-17 17:23:40 +08:00
|
|
|
|
QObject(),
|
2020-04-03 15:11:35 +08:00
|
|
|
|
IUKUIPanelPlugin(startupInfo),
|
|
|
|
|
|
mWidget(new UKUIStartMenuButton(this))
|
2020-01-17 17:23:40 +08:00
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-04-03 15:11:35 +08:00
|
|
|
|
UKUIStartMenuPlugin::~UKUIStartMenuPlugin()
|
2020-01-17 17:23:40 +08:00
|
|
|
|
{
|
2020-04-03 15:11:35 +08:00
|
|
|
|
delete mWidget;
|
2020-02-07 16:28:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-04-03 15:11:35 +08:00
|
|
|
|
QWidget *UKUIStartMenuPlugin::widget()
|
|
|
|
|
|
{
|
|
|
|
|
|
return mWidget;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void UKUIStartMenuPlugin::realign()
|
|
|
|
|
|
{
|
|
|
|
|
|
mWidget->realign();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
UKUIStartMenuButton::UKUIStartMenuButton( IUKUIPanelPlugin *plugin, QWidget* parent ):
|
|
|
|
|
|
QToolButton(parent),
|
|
|
|
|
|
mPlugin(plugin)
|
|
|
|
|
|
{
|
2021-06-28 11:40:57 +08:00
|
|
|
|
qDebug()<<"Plugin-StartMenu :: UKUIStartMenuButton start";
|
2020-04-03 15:11:35 +08:00
|
|
|
|
this->setIcon(QIcon("/usr/share/ukui-panel/panel/img/startmenu.svg"));
|
|
|
|
|
|
this->setStyle(new CustomStyle());
|
2020-12-18 16:19:45 +08:00
|
|
|
|
setStyleSheet("QToolButton { margin-left: 4px; } ");
|
2021-01-08 10:43:52 +08:00
|
|
|
|
QTimer::singleShot(5000,[this] {this->setToolTip(tr("UKui Menu")); });
|
2020-10-30 15:21:48 +08:00
|
|
|
|
// this->setWindowFlags(Qt::NoFocus);
|
2020-11-14 16:04:28 +08:00
|
|
|
|
//setAttribute(Qt::WA_X11DoNotAcceptFocus, true);
|
|
|
|
|
|
//setAttribute(Qt::WA_ShowWithoutActivating,true);
|
|
|
|
|
|
//setFocusPolicy(Qt::NoFocus);
|
2021-06-28 11:40:57 +08:00
|
|
|
|
qDebug()<<"Plugin-StartMenu :: UKUIStartMenuButton end";
|
2020-04-03 15:11:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
UKUIStartMenuButton::~UKUIStartMenuButton()
|
2020-02-07 16:28:43 +08:00
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-05-21 16:42:51 +08:00
|
|
|
|
/*plugin-startmenu refresh function*/
|
2020-04-03 15:11:35 +08:00
|
|
|
|
void UKUIStartMenuButton::realign()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (mPlugin->panel()->isHorizontal())
|
2020-09-24 16:11:06 +08:00
|
|
|
|
this->setFixedSize(mPlugin->panel()->panelSize()*1.3,mPlugin->panel()->panelSize());
|
2020-04-03 15:11:35 +08:00
|
|
|
|
else
|
2020-10-30 15:21:48 +08:00
|
|
|
|
this->setFixedSize(mPlugin->panel()->panelSize(),mPlugin->panel()->panelSize()*1.3);
|
2020-04-03 15:11:35 +08:00
|
|
|
|
this->setIconSize(QSize(mPlugin->panel()->iconSize(),mPlugin->panel()->iconSize()));
|
2020-10-30 15:21:01 +08:00
|
|
|
|
|
2020-04-03 15:11:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void UKUIStartMenuButton::mousePressEvent(QMouseEvent* event)
|
2020-03-11 16:17:31 +08:00
|
|
|
|
{
|
|
|
|
|
|
const Qt::MouseButton b = event->button();
|
|
|
|
|
|
|
|
|
|
|
|
if (Qt::LeftButton == b)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(QFileInfo::exists(QString("/usr/bin/ukui-menu")))
|
|
|
|
|
|
{
|
2020-04-03 15:11:35 +08:00
|
|
|
|
QProcess *process =new QProcess(this);
|
|
|
|
|
|
process->startDetached("/usr/bin/ukui-menu");
|
2020-10-30 15:21:01 +08:00
|
|
|
|
process->deleteLater();
|
2020-03-11 16:17:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
else{qDebug()<<"not find /usr/bin/ukui-start-menu"<<endl;}
|
|
|
|
|
|
}
|
|
|
|
|
|
QWidget::mousePressEvent(event);
|
|
|
|
|
|
}
|
2020-03-25 21:42:25 +08:00
|
|
|
|
|
2020-04-03 15:11:35 +08:00
|
|
|
|
void UKUIStartMenuButton::contextMenuEvent(QContextMenuEvent *event)
|
2020-02-07 16:28:43 +08:00
|
|
|
|
{
|
2020-04-03 15:11:35 +08:00
|
|
|
|
rightPressMenu=new QMenu();
|
|
|
|
|
|
rightPressMenu->setAttribute(Qt::WA_DeleteOnClose);
|
2020-02-07 16:28:43 +08:00
|
|
|
|
|
2020-11-14 17:17:26 +08:00
|
|
|
|
QMenu *pUserAction=new QMenu(tr("User Action")); //用户操作
|
|
|
|
|
|
QMenu *pSleepHibernate=new QMenu(tr("Sleep or Hibernate")); //重启或休眠
|
|
|
|
|
|
QMenu *pPowerSupply=new QMenu(tr("Power Supply")); //电源
|
2020-08-24 15:33:16 +08:00
|
|
|
|
rightPressMenu->addMenu(pUserAction);
|
|
|
|
|
|
rightPressMenu->addMenu(pSleepHibernate);
|
|
|
|
|
|
rightPressMenu->addMenu(pPowerSupply);
|
|
|
|
|
|
|
2020-10-30 15:21:01 +08:00
|
|
|
|
pUserAction->addAction(QIcon::fromTheme("system-lock-screen-symbolic"),
|
2020-10-30 15:21:48 +08:00
|
|
|
|
tr("Lock Screen"),
|
|
|
|
|
|
this, SLOT(ScreenServer())
|
2020-11-14 17:17:26 +08:00
|
|
|
|
); //锁屏
|
2020-10-30 15:21:01 +08:00
|
|
|
|
pUserAction->addAction(QIcon::fromTheme("stock-people-symbolic"),
|
2020-10-30 15:21:48 +08:00
|
|
|
|
tr("Switch User"),
|
|
|
|
|
|
this, SLOT(SessionSwitch())
|
2020-11-14 17:17:26 +08:00
|
|
|
|
); //切换用户
|
2020-10-30 15:21:01 +08:00
|
|
|
|
pUserAction->addAction(QIcon::fromTheme("system-logout-symbolic"),
|
2020-10-30 15:21:48 +08:00
|
|
|
|
tr("Logout"),
|
|
|
|
|
|
this, SLOT(SessionLogout())
|
2020-11-14 17:17:26 +08:00
|
|
|
|
); //注销
|
|
|
|
|
|
/*
|
2020-10-30 15:21:48 +08:00
|
|
|
|
//社区版本 安装时未强求建立 swap分区,若未建swap分区,会导致休眠(hibernate)失败,所以在20.04上屏蔽该功能
|
2020-11-14 17:17:26 +08:00
|
|
|
|
getOsRelease();
|
|
|
|
|
|
if(QString::compare(version,"Ubuntu"))
|
2020-12-05 11:06:17 +08:00
|
|
|
|
或使用!QString::compare(getCanHibernateResult(),"yes") 【目前该接口有bug】
|
2020-11-14 17:17:26 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
//检测CanHibernate接口的返回值,判断是否可以执行挂起操作
|
2021-03-18 19:21:12 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
QString filename = QDir::homePath() + "/.config/ukui/panel-commission.ini";
|
|
|
|
|
|
QSettings m_settings(filename, QSettings::IniFormat);
|
|
|
|
|
|
m_settings.setIniCodec("UTF-8");
|
|
|
|
|
|
|
|
|
|
|
|
m_settings.beginGroup("Hibernate");
|
|
|
|
|
|
QString hibernate_action = m_settings.value("hibernate", "").toString();
|
|
|
|
|
|
if (hibernate_action.isEmpty()) {
|
|
|
|
|
|
hibernate_action = "show";
|
|
|
|
|
|
}
|
|
|
|
|
|
m_settings.endGroup();
|
|
|
|
|
|
|
|
|
|
|
|
if(QString::compare(version,"Ubuntu") && hibernate_action != "hide"){
|
2021-02-04 17:54:15 +08:00
|
|
|
|
pSleepHibernate->addAction(QIcon::fromTheme("kylin-sleep-symbolic"),
|
2020-11-14 17:17:26 +08:00
|
|
|
|
tr("Hibernate Mode"),
|
|
|
|
|
|
this, SLOT(SessionHibernate())
|
|
|
|
|
|
); //休眠
|
|
|
|
|
|
}
|
2021-02-04 17:54:15 +08:00
|
|
|
|
pSleepHibernate->addAction(QIcon::fromTheme("system-sleep"),
|
2020-10-30 15:21:48 +08:00
|
|
|
|
tr("Sleep Mode"),
|
2020-11-05 15:11:10 +08:00
|
|
|
|
this, SLOT(SessionSuspend())
|
2021-02-04 17:54:15 +08:00
|
|
|
|
); //睡眠
|
2020-10-30 15:21:01 +08:00
|
|
|
|
pPowerSupply->addAction(QIcon::fromTheme("system-restart-symbolic"),
|
2020-10-30 15:21:48 +08:00
|
|
|
|
tr("Restart"),
|
|
|
|
|
|
this, SLOT(SessionReboot())
|
2020-11-14 17:17:26 +08:00
|
|
|
|
); //重启
|
2020-11-23 19:25:06 +08:00
|
|
|
|
QFileInfo file("/usr/bin/time-shutdown");
|
|
|
|
|
|
if(file.exists())
|
2021-04-25 14:01:27 +08:00
|
|
|
|
pPowerSupply->addAction(QIcon::fromTheme("ukui-shutdown-timer-symbolic"),
|
2020-11-23 19:25:06 +08:00
|
|
|
|
tr("TimeShutdown"),
|
|
|
|
|
|
this, SLOT(TimeShutdown())
|
|
|
|
|
|
); //定时开关机
|
2020-10-30 15:21:01 +08:00
|
|
|
|
pPowerSupply->addAction(QIcon::fromTheme("system-shutdown-symbolic"),
|
2020-10-30 15:21:48 +08:00
|
|
|
|
tr("Power Off"),
|
|
|
|
|
|
this, SLOT(SessionShutdown())
|
2020-11-14 17:17:26 +08:00
|
|
|
|
); //关机
|
2020-02-07 16:28:43 +08:00
|
|
|
|
|
2020-04-03 15:11:35 +08:00
|
|
|
|
rightPressMenu->setGeometry(mPlugin->panel()->calculatePopupWindowPos(mapToGlobal(event->pos()), rightPressMenu->sizeHint()));
|
|
|
|
|
|
rightPressMenu->show();
|
2020-02-07 16:28:43 +08:00
|
|
|
|
}
|
2020-03-11 00:26:29 +08:00
|
|
|
|
|
2020-05-21 16:42:51 +08:00
|
|
|
|
/*开始菜单按钮右键菜单选项,与开始菜单中电源按钮的右键功能是相同的*/
|
2020-11-05 15:11:10 +08:00
|
|
|
|
//锁屏
|
2020-04-03 15:11:35 +08:00
|
|
|
|
void UKUIStartMenuButton::ScreenServer()
|
2020-02-07 16:28:43 +08:00
|
|
|
|
{
|
|
|
|
|
|
system("ukui-screensaver-command -l");
|
|
|
|
|
|
}
|
2020-11-05 15:11:10 +08:00
|
|
|
|
|
|
|
|
|
|
//切换用户
|
2020-04-03 15:11:35 +08:00
|
|
|
|
void UKUIStartMenuButton::SessionSwitch()
|
2020-02-07 16:28:43 +08:00
|
|
|
|
{
|
|
|
|
|
|
QProcess::startDetached(QString("ukui-session-tools --switchuser"));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-11-05 15:11:10 +08:00
|
|
|
|
//注销
|
2020-04-03 15:11:35 +08:00
|
|
|
|
void UKUIStartMenuButton::SessionLogout()
|
2020-02-07 16:28:43 +08:00
|
|
|
|
{
|
|
|
|
|
|
system("ukui-session-tools --logout");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-11-05 15:11:10 +08:00
|
|
|
|
//休眠 睡眠
|
2020-08-24 15:33:16 +08:00
|
|
|
|
void UKUIStartMenuButton::SessionHibernate()
|
|
|
|
|
|
{
|
|
|
|
|
|
system("ukui-session-tools --hibernate");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-02-04 17:54:15 +08:00
|
|
|
|
//睡眠
|
2020-11-05 15:11:10 +08:00
|
|
|
|
void UKUIStartMenuButton::SessionSuspend()
|
2020-10-30 15:21:01 +08:00
|
|
|
|
{
|
|
|
|
|
|
system("ukui-session-tools --suspend");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-11-05 15:11:10 +08:00
|
|
|
|
//重启
|
2020-04-03 15:11:35 +08:00
|
|
|
|
void UKUIStartMenuButton::SessionReboot()
|
2020-02-07 16:28:43 +08:00
|
|
|
|
{
|
|
|
|
|
|
system("ukui-session-tools --reboot");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-11-05 15:11:10 +08:00
|
|
|
|
//定时关机
|
2020-10-30 15:21:01 +08:00
|
|
|
|
void UKUIStartMenuButton::TimeShutdown()
|
|
|
|
|
|
{
|
|
|
|
|
|
QProcess *process_timeshutdowm =new QProcess(this);
|
2020-11-05 15:11:10 +08:00
|
|
|
|
process_timeshutdowm->startDetached("/usr/bin/time-shutdown");
|
2020-10-30 15:21:01 +08:00
|
|
|
|
process_timeshutdowm->deleteLater();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-11-05 15:11:10 +08:00
|
|
|
|
//关机
|
2020-04-03 15:11:35 +08:00
|
|
|
|
void UKUIStartMenuButton::SessionShutdown()
|
2020-02-07 16:28:43 +08:00
|
|
|
|
{
|
|
|
|
|
|
system("ukui-session-tools --shutdown");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-11-14 17:17:26 +08:00
|
|
|
|
//获取系统版本,若为ubuntu则取消休眠功能
|
2020-10-30 15:21:48 +08:00
|
|
|
|
void UKUIStartMenuButton::getOsRelease()
|
|
|
|
|
|
{
|
2020-11-14 17:17:26 +08:00
|
|
|
|
QFile file("/etc/lsb-release");
|
2020-10-30 15:21:48 +08:00
|
|
|
|
if (!file.open(QIODevice::ReadOnly)) qDebug() << "Read file Failed.";
|
|
|
|
|
|
while (!file.atEnd()) {
|
|
|
|
|
|
QByteArray line = file.readLine();
|
|
|
|
|
|
QString str(line);
|
|
|
|
|
|
if (str.contains("DISTRIB_ID")){
|
|
|
|
|
|
version=str.remove("DISTRIB_ID=");
|
2020-11-14 17:17:26 +08:00
|
|
|
|
version=str.remove("\n");
|
2020-10-30 15:21:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-11-14 17:17:26 +08:00
|
|
|
|
|
2020-11-14 17:20:36 +08:00
|
|
|
|
//检测当前系统能否执行休眠操作
|
2020-11-14 17:17:26 +08:00
|
|
|
|
QString UKUIStartMenuButton::getCanHibernateResult()
|
|
|
|
|
|
{
|
|
|
|
|
|
QDBusInterface interface("org.freedesktop.login1", "/org/freedesktop/login1",
|
|
|
|
|
|
"org.freedesktop.login1.Manager",
|
|
|
|
|
|
QDBusConnection::systemBus());
|
|
|
|
|
|
if (!interface.isValid()) {
|
|
|
|
|
|
qCritical() << QDBusConnection::sessionBus().lastError().message();
|
|
|
|
|
|
}
|
2020-11-14 17:34:31 +08:00
|
|
|
|
/*调用远程的 CanHibernate 方法,判断是否可以执行休眠的操作,返回值为yes为允许执行休眠,no为无法执行休眠 na为交换分区不足*/
|
2020-11-14 17:17:26 +08:00
|
|
|
|
QDBusReply<QString> reply = interface.call("CanHibernate");
|
|
|
|
|
|
if (reply.isValid()) {
|
|
|
|
|
|
return reply;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
qCritical() << "Call Dbus method failed";
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-11-14 16:04:28 +08:00
|
|
|
|
|
|
|
|
|
|
void UKUIStartMenuButton::enterEvent(QEvent *) {
|
|
|
|
|
|
repaint();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void UKUIStartMenuButton::leaveEvent(QEvent *) {
|
|
|
|
|
|
repaint();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|