Debian Codegraph @

src/common/platform/PlatformChecker.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2017 Damir Porobic <https://github.com/damirporobic>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 #include "PlatformChecker.h"
21
22 PlatformChecker::PlatformChecker(const QSharedPointer<ICommandRunner> &commandRunner) :
23 mCommandRunner(commandRunner),
24 mEnvironment(Environment::Unknown),
25 mPlatform(Platform::Unknown),
26 mPackageManager(PackageManager::Unknown),
27 mGnomeVersion(-1),
28 mIsPlatformChecked(false),
29 mIsEnvironmentChecked(false),
30 mIsPacketManagerChecked(false),
31 mIsGnomeVersionChecked(false)
32 {
33 }
34
35 bool PlatformChecker::isX11()
36 {
37 checkPlatform();
38 return mPlatform == Platform::X11;
39 }
40
41 bool PlatformChecker::isWayland()
42 {
43 checkPlatform();
44 return mPlatform == Platform::Wayland;
45 }
46
47 bool PlatformChecker::isKde()
48 {
49 checkEnvironment();
50 return mEnvironment == Environment::KDE;
51 }
52
53 bool PlatformChecker::isGnome()
54 {
55 checkEnvironment();
56 return mEnvironment == Environment::Gnome;
57 }
58
59 bool PlatformChecker::isSnap()
60 {
61 checkPackageManager();
62 return mPackageManager == PackageManager::Snap;
63 }
64
65 int PlatformChecker::gnomeVersion()
66 {
67 checkVersion();
68 return mGnomeVersion;
69 }
70
71 void PlatformChecker::checkPlatform()
72 {
73 if(!mIsPlatformChecked) {
74 auto output = mCommandRunner->getEnvironmentVariable(QLatin1String("XDG_SESSION_TYPE"));
75 if (outputContainsValue(output, QLatin1String("x11"))) {
76 mPlatform = Platform::X11;
77 } else if (outputContainsValue(output, QLatin1String("wayland"))) {
78 mPlatform = Platform::Wayland;
79 } else {
80 mPlatform = Platform::Unknown;
81 }
82 }
83 mIsPlatformChecked = true;
84 }
85
86 void PlatformChecker::checkEnvironment()
87 {
88 if(!mIsEnvironmentChecked) {
89 auto output = mCommandRunner->getEnvironmentVariable(QLatin1String("XDG_CURRENT_DESKTOP"));
90 if (outputContainsValue(output, QLatin1String("kde"))) {
91 mEnvironment = Environment::KDE;
92 } else if (outputContainsValue(output, QLatin1String("gnome")) || outputContainsValue(output, QLatin1String("unity"))) {
93 mEnvironment = Environment::Gnome;
94 } else {
95 mEnvironment = Environment::Unknown;
96 }
97 }
98 mIsEnvironmentChecked = true;
99 }
100
101 void PlatformChecker::checkPackageManager()
102 {
103 if(!mIsPacketManagerChecked) {
104 if (mCommandRunner->isEnvironmentVariableSet(QLatin1String("SNAP"))) {
105 mPackageManager = PackageManager::Snap;
106 } else {
107 mPackageManager = PackageManager::Unknown;
108 }
109 mIsPacketManagerChecked = true;
110 }
111 }
112
113 bool PlatformChecker::outputContainsValue(const QString& output, const QString& value)
114 {
115 return output.contains(value.toLatin1(), Qt::CaseInsensitive);
116 }
117
118 void PlatformChecker::checkVersion()
119 {
120 if(!mIsGnomeVersionChecked && isGnome()) {
121 auto path = QLatin1String("/usr/share/gnome/gnome-version.xml");
122 QRegularExpression regex("<platform>(.+?)</platform>");
123 bool isParseSuccessful;
124 auto value = regex.match(mCommandRunner->readFile(path)).captured(1).toInt(&isParseSuccessful);
125 if(isParseSuccessful) {
126 mGnomeVersion = value;
127 } else {
128 QProcess proc;
129 proc.start("gnome-shell", {"--version"});
130 if(!proc.waitForFinished(1000)){
131 mGnomeVersion = -1;
132 }
133
134 const QString output = QString::fromUtf8(proc.readAllStandardOutput()).trimmed();
135
136 QRegularExpression gnomeVersionRegex(R"(((\d+)))");
137
138 auto match = gnomeVersionRegex.match(output);
139
140 if(match.hasMatch())
141 mGnomeVersion = match.captured(0).toInt();
142
143 }
144
145 }
146 mIsGnomeVersionChecked = true;
147 }