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
|
/*
* ========================================================================== *
* *
* This file is part of the Openterface Mini KVM App QT version *
* *
* Copyright (C) 2024 <info@openterface.com> *
* *
* 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 version 3. *
* *
* 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/>. *
* *
* ========================================================================== *
*/
#include "versioninfomanager.h"
#include <QApplication>
#include <QClipboard>
#include <QSysInfo>
#include <QPushButton>
#include <QMessageBox>
#include <QProcessEnvironment>
#include <QTextStream>
#include <QRegularExpression>
#include <QMediaDevices>
#include <QAudioDevice>
#include <QCameraDevice>
#include <QJsonDocument>
#include <QJsonObject>
#include <QDesktopServices>
#include <QUrl>
#include <QVersionNumber>
VersionInfoManager::VersionInfoManager(QObject *parent)
: QObject(parent)
, networkManager(new QNetworkAccessManager(this))
{
}
VersionInfoManager::~VersionInfoManager()
{
}
void VersionInfoManager::showAbout()
{
QString message = QString("<b>Email:</b> %1<br><b>Company:</b> %2<br><b>Address:</b> %3")
.arg(EMAIL)
.arg(TEAM_NAME)
.arg(ADDRESS);
QMessageBox msgBox;
msgBox.setWindowTitle(tr("%1").arg(QApplication::applicationName()));
msgBox.setText(message);
msgBox.setTextFormat(Qt::RichText);
msgBox.exec();
}
void VersionInfoManager::showVersionInfo()
{
QString applicationName = QApplication::applicationName();
QString message = getVersionInfoString() + "<br><br>" +
getPermissionsStatus() + "<br><br>" +
getEnvironmentVariables();
QMessageBox msgBox;
msgBox.setWindowTitle(tr("%1").arg(applicationName));
msgBox.setText(message);
msgBox.setTextFormat(Qt::RichText);
QPushButton *copyButton = msgBox.addButton(tr("Copy"), QMessageBox::ActionRole);
msgBox.addButton(QMessageBox::Close);
connect(copyButton, &QPushButton::clicked, this, &VersionInfoManager::copyToClipboard);
msgBox.exec();
if (msgBox.clickedButton() == copyButton) {
copyToClipboard();
}
}
void VersionInfoManager::copyToClipboard()
{
QClipboard *clipboard = QApplication::clipboard();
QString clipboardText = getVersionInfoString().remove(QRegularExpression("<[^>]*>")) + "\n\n" +
getPermissionsStatus().remove(QRegularExpression("<[^>]*>")) + "\n\n" +
getEnvironmentVariablesPlainText();
clipboard->setText(clipboardText);
}
QString VersionInfoManager::getVersionInfoString() const
{
QString applicationVersion = QApplication::applicationVersion();
QString osVersion = QSysInfo::prettyProductName();
return QString("<b>App:</b> %1<br><b>OS:</b> %2<br><b>QT:</b> %3")
.arg(applicationVersion)
.arg(osVersion)
.arg(qVersion());
}
QString VersionInfoManager::getPermissionsStatus() const
{
QString micPermission = getMicrophonePermissionStatus();
QString videoPermission = getVideoPermissionStatus();
return QString("<b>Permissions:</b><br>"
"<table border='1' cellspacing='0' cellpadding='5'>"
"<tr><td>Microphone</td><td>%1</td></tr>"
"<tr><td>Video</td><td>%2</td></tr>"
"</table>")
.arg(micPermission)
.arg(videoPermission);
}
QString VersionInfoManager::getEnvironmentVariables() const
{
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
QString envInfo;
QTextStream stream(&envInfo);
QStringList importantVars = {"QT_QPA_PLATFORM", "XDG_SESSION_TYPE", "WAYLAND_DISPLAY", "DISPLAY"};
stream << "<b>Environment Variables:</b><br>";
stream << "<table border='1' cellspacing='0' cellpadding='5'>";
stream << "<tr><th>Variable</th><th>Value</th></tr>";
for (const QString &var : importantVars) {
stream << QString("<tr><td>%1</td><td>%2</td></tr>").arg(var, env.value(var, "(not set)"));
}
stream << "</table>";
return envInfo;
}
QString VersionInfoManager::getEnvironmentVariablesPlainText() const
{
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
QString envInfo;
QTextStream stream(&envInfo);
QStringList importantVars = {"QT_QPA_PLATFORM", "XDG_SESSION_TYPE", "WAYLAND_DISPLAY", "DISPLAY"};
stream << "Environment Variables:\n";
for (const QString &var : importantVars) {
stream << QString("%1: %2\n").arg(var, env.value(var, "(not set)"));
}
return envInfo;
}
QString VersionInfoManager::getMicrophonePermissionStatus() const
{
QList<QAudioDevice> audioDevices = QMediaDevices::audioInputs();
return audioDevices.isEmpty() ? "Not available or permission not granted" : "Available";
}
QString VersionInfoManager::getVideoPermissionStatus() const
{
QList<QCameraDevice> videoDevices = QMediaDevices::videoInputs();
return videoDevices.isEmpty() ? "Not available or permission not granted" : "Available";
}
void VersionInfoManager::checkForUpdates()
{
QNetworkRequest request((QUrl(GITHUB_REPO_API)));
request.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
request.setHeader(QNetworkRequest::UserAgentHeader, "Openterface_QT Update Checker");
QNetworkReply *reply = networkManager->get(request);
connect(reply, &QNetworkReply::finished, reply, [this, reply]() {
handleUpdateCheckResponse(reply);
reply->deleteLater();
});
}
void VersionInfoManager::handleUpdateCheckResponse(QNetworkReply *reply)
{
if (reply->error() == QNetworkReply::NoError) {
QJsonDocument doc = QJsonDocument::fromJson(reply->readAll());
QJsonObject releaseInfo = doc.object();
QString latestVersion = releaseInfo["tag_name"].toString();
QString currentVersion = QApplication::applicationVersion();
QString htmlUrl = releaseInfo["html_url"].toString();
// Remove 'v' prefix if present
latestVersion.remove(QRegularExpression("^v"));
currentVersion.remove(QRegularExpression("^v"));
QVersionNumber latest = QVersionNumber::fromString(latestVersion);
QVersionNumber current = QVersionNumber::fromString(currentVersion);
qDebug() << "version latest: " << latest;
qDebug() << "version current: " << current;
QMessageBox msgBox;
msgBox.setWindowTitle(tr("Openterface Mini KVM"));
if (latest != current) {
msgBox.setText(tr("A new version is available!\nCurrent version: %1\nLatest version: %2\n")
.arg(currentVersion)
.arg(latestVersion));
QPushButton *updateButton = msgBox.addButton(tr("Update"), QMessageBox::AcceptRole);
msgBox.addButton(tr("Cancel"), QMessageBox::RejectRole);
msgBox.exec();
if (msgBox.clickedButton() == updateButton) {
openGitHubReleasePage(htmlUrl);
}
} else {
msgBox.setText(tr("You are using the latest version"));
msgBox.addButton(QMessageBox::Ok);
msgBox.exec();
}
} else {
QMessageBox::warning(nullptr,
tr("Update Check Failed\n"),
tr("Failed to check for updates.\nError: %1\nPlease check your internet connection.\n")
.arg(reply->errorString()));
}
}
void VersionInfoManager::openGitHubReleasePage(const QString &releaseUrl)
{
QDesktopServices::openUrl(QUrl(releaseUrl));
}
|