|
| 1 | +// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd. |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: LGPL-3.0-or-later |
| 4 | + |
| 5 | +#include "urlopenerhelper.h" |
| 6 | +#include "constants.h" |
| 7 | + |
| 8 | +#include <QDBusConnection> |
| 9 | +#include <QDBusInterface> |
| 10 | +#include <QDBusConnectionInterface> |
| 11 | +#include <QDBusServiceWatcher> |
| 12 | +#include <QProcess> |
| 13 | +#include <QDebug> |
| 14 | +#include <QTimer> |
| 15 | +#include <QCoreApplication> |
| 16 | + |
| 17 | +using namespace network::sessionservice; |
| 18 | + |
| 19 | +#define SESSION_MANAG_SERVICE "com.deepin.SessionManager" |
| 20 | + |
| 21 | +UrlOpenerHelper::UrlOpenerHelper(QObject *parent) |
| 22 | + : QObject(parent) |
| 23 | + , m_process(new QProcess(this)) |
| 24 | + , m_timer(nullptr) |
| 25 | + , m_startManagerIsPrepare(false) |
| 26 | + , m_checkComplete(false) |
| 27 | +{ |
| 28 | + init(); |
| 29 | +} |
| 30 | + |
| 31 | +UrlOpenerHelper::~UrlOpenerHelper() |
| 32 | +{ |
| 33 | +} |
| 34 | + |
| 35 | +void UrlOpenerHelper::openUrl(const QString &url) |
| 36 | +{ |
| 37 | + if (url.isEmpty()) |
| 38 | + return; |
| 39 | + |
| 40 | + static UrlOpenerHelper urlHelper; |
| 41 | + if (urlHelper.isPrepare()) { |
| 42 | + urlHelper.openUrlAddress(url); |
| 43 | + } else if (!urlHelper.m_cacheUrls.contains(url)) { |
| 44 | + urlHelper.m_cacheUrls << url; |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +void UrlOpenerHelper::openUrlAddress(const QString &url) |
| 49 | +{ |
| 50 | + // 调用xdg-open来打开网页 |
| 51 | + QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); |
| 52 | + |
| 53 | + if (!m_displayEnvironment.isEmpty()) |
| 54 | + env.insert("DISPLAY", m_displayEnvironment); |
| 55 | + |
| 56 | + if (m_process->isOpen()) { |
| 57 | + m_process->close(); |
| 58 | + } |
| 59 | + m_process->setProcessEnvironment(env); |
| 60 | + m_process->start("xdg-open", QStringList() << url); |
| 61 | + m_process->waitForFinished(); |
| 62 | +} |
| 63 | + |
| 64 | +QString UrlOpenerHelper::getDisplayEnvironment() const |
| 65 | +{ |
| 66 | + QString displayenv = qgetenv("DISPLAY"); |
| 67 | + if (!displayenv.isEmpty()) { |
| 68 | + qCDebug(DSM) << "get DISPLAY Environment from local env"; |
| 69 | + return displayenv; |
| 70 | + } |
| 71 | + |
| 72 | + qCDebug(DSM) << "get DISPLAY Environment from systemd1 service"; |
| 73 | + // 有时候获取到的环境变量为空,此时就需要从systemd接口中来获取环境变量了 |
| 74 | + QDBusInterface systemdInterface("org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager"); |
| 75 | + QStringList environments = systemdInterface.property("Environment").toStringList(); |
| 76 | + for (const QString &env : environments) { |
| 77 | + QStringList envArray = env.split("="); |
| 78 | + if (envArray.size() < 2) |
| 79 | + continue; |
| 80 | + if (envArray.first() == "DISPLAY") |
| 81 | + return envArray[1]; |
| 82 | + } |
| 83 | + |
| 84 | + return QString(); |
| 85 | +} |
| 86 | + |
| 87 | +void UrlOpenerHelper::init() |
| 88 | +{ |
| 89 | + m_startManagerIsPrepare = QDBusConnection::sessionBus().interface()->isServiceRegistered(SESSION_MANAG_SERVICE); |
| 90 | + if (m_startManagerIsPrepare) { |
| 91 | + m_displayEnvironment = getDisplayEnvironment(); |
| 92 | + qCDebug(DSM) << "get display environment" << m_displayEnvironment; |
| 93 | + if (m_displayEnvironment.isEmpty()) { |
| 94 | + m_timer = new QTimer(this); |
| 95 | + m_timer->setInterval(1000); |
| 96 | + m_timer->setProperty("checktime", 0); |
| 97 | + connect(m_timer, &QTimer::timeout, this, &UrlOpenerHelper::onCheckTimeout); |
| 98 | + m_timer->start(); |
| 99 | + } |
| 100 | + } else { |
| 101 | + // 如果服务未启动,则等待服务启动 |
| 102 | + qCWarning(DSM) << SESSION_MANAG_SERVICE << "service is not register, wait it for start"; |
| 103 | + QDBusServiceWatcher *serviceWatcher = new QDBusServiceWatcher(this); |
| 104 | + serviceWatcher->setConnection(QDBusConnection::sessionBus()); |
| 105 | + serviceWatcher->addWatchedService(SESSION_MANAG_SERVICE); |
| 106 | + connect(serviceWatcher, &QDBusServiceWatcher::serviceRegistered, this, &UrlOpenerHelper::onServiceRegistered); |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +bool UrlOpenerHelper::isPrepare() const |
| 111 | +{ |
| 112 | + return m_startManagerIsPrepare && (!m_displayEnvironment.isEmpty() || m_checkComplete); |
| 113 | +} |
| 114 | + |
| 115 | +void UrlOpenerHelper::onCheckTimeout() |
| 116 | +{ |
| 117 | + auto doOpenPortal = [this] { |
| 118 | + for (const QString &url : m_cacheUrls) { |
| 119 | + openUrlAddress(url); |
| 120 | + } |
| 121 | + m_checkComplete = true; |
| 122 | + m_cacheUrls.clear(); |
| 123 | + m_timer->stop(); |
| 124 | + m_timer->deleteLater(); |
| 125 | + m_timer = nullptr; |
| 126 | + }; |
| 127 | + |
| 128 | + m_displayEnvironment = getDisplayEnvironment(); |
| 129 | + if (m_displayEnvironment.isEmpty()) { |
| 130 | + // 连接检测20次,如果20次还是没有这个环境变量,就说明这个环境变量有问题,直接尝试打开 |
| 131 | + int checktime = m_timer->property("checktime").toInt(); |
| 132 | + qCDebug(DSM) << "in " << checktime << "times, the display environment is empty"; |
| 133 | + if (checktime >= 20) { |
| 134 | + doOpenPortal(); |
| 135 | + } else { |
| 136 | + checktime++; |
| 137 | + m_timer->setProperty("checktime", checktime); |
| 138 | + } |
| 139 | + } else if (!m_displayEnvironment.isEmpty()) { |
| 140 | + qCDebug(DSM) << "the display environment is not empty" << m_displayEnvironment << ", open the url" << m_cacheUrls; |
| 141 | + doOpenPortal(); |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +void UrlOpenerHelper::onServiceRegistered(const QString &service) |
| 146 | +{ |
| 147 | + if (service != SESSION_MANAG_SERVICE) |
| 148 | + return; |
| 149 | + |
| 150 | + m_startManagerIsPrepare = true; |
| 151 | + m_displayEnvironment = getDisplayEnvironment(); |
| 152 | + qCInfo(DSM) << SESSION_MANAG_SERVICE << "start success, display envionment:" << m_displayEnvironment << "urls:" << m_cacheUrls; |
| 153 | + for (const QString &url : m_cacheUrls) { |
| 154 | + openUrlAddress(url); |
| 155 | + } |
| 156 | +} |
0 commit comments