Files
JSwitchDisplayApplet/graphicsdriverapplet.cpp
T

256 lines
7.1 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 <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)
{
}
GraphicsDriverApplet::~GraphicsDriverApplet()
{
}
bool GraphicsDriverApplet::load()
{
initDBusConnection();
connect(m_progressTimer, &QTimer::timeout, [this]() {
// Poll progress
});
return DApplet::load();
}
bool GraphicsDriverApplet::init()
{
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;
}
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;
}
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;
}
connect(m_dbusInterface, SIGNAL(ReportProgress(QString)),
this, SLOT(onReportProgress(QString)));
}
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"