9fb80cdfc8
- QML 根元素使用 AppletItem,参照 dde-weather 插件实现 - 使用 Panel.rootObject.dockItemMaxSize 适配任务栏图标尺寸 - dockOrder 设为 21,定位在系统托盘左侧 - 使用 PanelPopup + PanelToolTip + TapHandler 实现点击弹窗和悬停提示 - 弹窗打开前设置 DockPanelPositioner.bounding 修正弹窗定位 - C++ 后端新增 dbusAvailable 属性,D-Bus 不可用时优雅降级 - 使用 isServiceRegistered 正确检测 D-Bus 服务可用性 - install.sh 改用 SCRIPT_DIR 自动定位项目根目录
274 lines
7.5 KiB
C++
274 lines
7.5 KiB
C++
// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd.
|
|
//
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
#include "graphicsdriverapplet.h"
|
|
#include <pluginfactory.h>
|
|
|
|
#include <QDBusConnection>
|
|
#include <QDBusConnectionInterface>
|
|
#include <QDBusPendingReply>
|
|
#include <QDebug>
|
|
#include <QJsonDocument>
|
|
#include <QJsonObject>
|
|
#include <QJsonArray>
|
|
|
|
DCORE_USE_NAMESPACE
|
|
|
|
DS_BEGIN_NAMESPACE
|
|
|
|
static const QString DBUS_SERVICE = "com.deepin.daemon.GraphicsDriver";
|
|
static const QString DBUS_PATH = "/com/deepin/daemon/GraphicsDriver";
|
|
static const QString DBUS_INTERFACE = "com.deepin.daemon.GraphicsDriver";
|
|
|
|
GraphicsDriverApplet::GraphicsDriverApplet(QObject *parent)
|
|
: DApplet(parent)
|
|
, m_dbusInterface(nullptr)
|
|
, m_progressTimer(new QTimer(this))
|
|
, m_installProgress(0)
|
|
, m_isInstalling(false)
|
|
, m_dbusAvailable(false)
|
|
{
|
|
}
|
|
|
|
GraphicsDriverApplet::~GraphicsDriverApplet()
|
|
{
|
|
}
|
|
|
|
bool GraphicsDriverApplet::load()
|
|
{
|
|
initDBusConnection();
|
|
return DApplet::load();
|
|
}
|
|
|
|
bool GraphicsDriverApplet::init()
|
|
{
|
|
if (m_dbusAvailable) {
|
|
refreshDeviceInfo();
|
|
}
|
|
return DApplet::init();
|
|
}
|
|
|
|
QString GraphicsDriverApplet::currentDriver() const
|
|
{
|
|
return m_currentDriver;
|
|
}
|
|
|
|
QString GraphicsDriverApplet::deviceInfo() const
|
|
{
|
|
return m_deviceInfo;
|
|
}
|
|
|
|
int GraphicsDriverApplet::installProgress() const
|
|
{
|
|
return m_installProgress;
|
|
}
|
|
|
|
bool GraphicsDriverApplet::isInstalling() const
|
|
{
|
|
return m_isInstalling;
|
|
}
|
|
|
|
bool GraphicsDriverApplet::dbusAvailable() const
|
|
{
|
|
return m_dbusAvailable;
|
|
}
|
|
|
|
void GraphicsDriverApplet::refreshDeviceInfo()
|
|
{
|
|
if (!m_dbusInterface || !m_dbusInterface->isValid()) {
|
|
qWarning() << "DBus interface is not valid";
|
|
return;
|
|
}
|
|
|
|
// Get device info
|
|
QDBusPendingCall deviceCall = m_dbusInterface->asyncCall("GetDevice");
|
|
QDBusPendingCallWatcher *deviceWatcher = new QDBusPendingCallWatcher(deviceCall, this);
|
|
connect(deviceWatcher, &QDBusPendingCallWatcher::finished, this, &GraphicsDriverApplet::onGetDeviceReply);
|
|
|
|
// Get current driver
|
|
QDBusPendingCall currCall = m_dbusInterface->asyncCall("GetCurrDriverName");
|
|
QDBusPendingCallWatcher *currWatcher = new QDBusPendingCallWatcher(currCall, this);
|
|
connect(currWatcher, &QDBusPendingCallWatcher::finished, this, &GraphicsDriverApplet::onGetCurrDriverReply);
|
|
}
|
|
|
|
void GraphicsDriverApplet::prepareInstall(const QString &driverName)
|
|
{
|
|
if (!m_dbusInterface || !m_dbusInterface->isValid()) {
|
|
emit installFailed("DBus interface is not valid");
|
|
return;
|
|
}
|
|
|
|
QLocale locale;
|
|
QString language = locale.name();
|
|
|
|
QDBusPendingCall call = m_dbusInterface->asyncCall("PrepareInstall", driverName, language);
|
|
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(call, this);
|
|
connect(watcher, &QDBusPendingCallWatcher::finished, [this](QDBusPendingCallWatcher *call) {
|
|
QDBusPendingReply<> reply = *call;
|
|
if (reply.isError()) {
|
|
qWarning() << "Failed to prepare install:" << reply.error().message();
|
|
emit installFailed(reply.error().message());
|
|
} else {
|
|
qDebug() << "Prepare install succeeded";
|
|
}
|
|
call->deleteLater();
|
|
});
|
|
}
|
|
|
|
void GraphicsDriverApplet::testInstall()
|
|
{
|
|
if (!m_dbusInterface || !m_dbusInterface->isValid()) {
|
|
emit installFailed("DBus interface is not valid");
|
|
return;
|
|
}
|
|
|
|
setInstalling(true);
|
|
|
|
QDBusPendingCall call = m_dbusInterface->asyncCall("TestInstall");
|
|
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(call, this);
|
|
connect(watcher, &QDBusPendingCallWatcher::finished, [this](QDBusPendingCallWatcher *call) {
|
|
QDBusPendingReply<> reply = *call;
|
|
if (reply.isError()) {
|
|
qWarning() << "Failed to test install:" << reply.error().message();
|
|
emit installFailed(reply.error().message());
|
|
setInstalling(false);
|
|
} else {
|
|
qDebug() << "Test install succeeded";
|
|
realInstall();
|
|
}
|
|
call->deleteLater();
|
|
});
|
|
}
|
|
|
|
void GraphicsDriverApplet::realInstall()
|
|
{
|
|
if (!m_dbusInterface || !m_dbusInterface->isValid()) {
|
|
emit installFailed("DBus interface is not valid");
|
|
return;
|
|
}
|
|
|
|
QDBusPendingCall call = m_dbusInterface->asyncCall("RealInstall");
|
|
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(call, this);
|
|
connect(watcher, &QDBusPendingCallWatcher::finished, [this](QDBusPendingCallWatcher *call) {
|
|
QDBusPendingReply<> reply = *call;
|
|
if (reply.isError()) {
|
|
qWarning() << "Failed to real install:" << reply.error().message();
|
|
emit installFailed(reply.error().message());
|
|
setInstalling(false);
|
|
} else {
|
|
qDebug() << "Real install succeeded";
|
|
setInstalling(false);
|
|
emit installSuccess();
|
|
emit requestReboot();
|
|
}
|
|
call->deleteLater();
|
|
});
|
|
}
|
|
|
|
void GraphicsDriverApplet::cancelInstall()
|
|
{
|
|
if (!m_dbusInterface || !m_dbusInterface->isValid()) {
|
|
return;
|
|
}
|
|
|
|
m_dbusInterface->asyncCall("CancelInstall");
|
|
setInstalling(false);
|
|
}
|
|
|
|
void GraphicsDriverApplet::onGetDeviceReply(QDBusPendingCallWatcher *call)
|
|
{
|
|
QDBusPendingReply<QString> reply = *call;
|
|
if (reply.isError()) {
|
|
qWarning() << "Failed to get device:" << reply.error().message();
|
|
} else {
|
|
m_deviceInfo = reply.value();
|
|
qDebug() << "Device info:" << m_deviceInfo;
|
|
emit deviceInfoChanged();
|
|
}
|
|
call->deleteLater();
|
|
}
|
|
|
|
void GraphicsDriverApplet::onGetCurrDriverReply(QDBusPendingCallWatcher *call)
|
|
{
|
|
QDBusPendingReply<QString> reply = *call;
|
|
if (reply.isError()) {
|
|
qWarning() << "Failed to get current driver:" << reply.error().message();
|
|
} else {
|
|
m_currentDriver = reply.value();
|
|
qDebug() << "Current driver:" << m_currentDriver;
|
|
emit currentDriverChanged();
|
|
}
|
|
call->deleteLater();
|
|
}
|
|
|
|
void GraphicsDriverApplet::onReportProgress(const QString &ratio)
|
|
{
|
|
bool ok;
|
|
int progress = ratio.toInt(&ok);
|
|
if (ok) {
|
|
m_installProgress = progress;
|
|
emit installProgressChanged();
|
|
}
|
|
}
|
|
|
|
void GraphicsDriverApplet::initDBusConnection()
|
|
{
|
|
QDBusConnection bus = QDBusConnection::sessionBus();
|
|
|
|
if (!bus.isConnected()) {
|
|
qWarning() << "Cannot connect to D-Bus session bus";
|
|
return;
|
|
}
|
|
|
|
// 检查服务是否已注册
|
|
bool registered = bus.interface()->isServiceRegistered(DBUS_SERVICE);
|
|
if (!registered) {
|
|
qWarning() << "GraphicsDriver D-Bus service is not registered";
|
|
return;
|
|
}
|
|
|
|
m_dbusInterface = new QDBusInterface(
|
|
DBUS_SERVICE,
|
|
DBUS_PATH,
|
|
DBUS_INTERFACE,
|
|
bus,
|
|
this
|
|
);
|
|
|
|
if (!m_dbusInterface->isValid()) {
|
|
qWarning() << "DBus interface is not valid:" << m_dbusInterface->lastError().message();
|
|
return;
|
|
}
|
|
|
|
m_dbusAvailable = true;
|
|
emit dbusAvailableChanged();
|
|
|
|
connect(m_dbusInterface, SIGNAL(ReportProgress(QString)),
|
|
this, SLOT(onReportProgress(QString)));
|
|
|
|
qDebug() << "GraphicsDriver D-Bus connected successfully";
|
|
}
|
|
|
|
void GraphicsDriverApplet::setInstalling(bool installing)
|
|
{
|
|
if (m_isInstalling != installing) {
|
|
m_isInstalling = installing;
|
|
emit isInstallingChanged();
|
|
|
|
if (installing) {
|
|
m_progressTimer->start(1000);
|
|
} else {
|
|
m_progressTimer->stop();
|
|
m_installProgress = 0;
|
|
emit installProgressChanged();
|
|
}
|
|
}
|
|
}
|
|
|
|
D_APPLET_CLASS(GraphicsDriverApplet)
|
|
|
|
DS_END_NAMESPACE
|
|
|
|
#include "graphicsdriverapplet.moc"
|