LeechCraft 0.6.70-17335-ge406ffdcaf
Modular cross-platform feature rich live environment.
Loading...
Searching...
No Matches
sysinfo.cpp
Go to the documentation of this file.
1/**********************************************************************
2 * LeechCraft - modular cross-platform feature rich internet client.
3 * Copyright (C) 2006-2014 Georg Rudoy
4 *
5 * Distributed under the Boost Software License, Version 1.0.
6 * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7 **********************************************************************/
8
9#include "sysinfo.h"
10#if !defined(Q_OS_WIN32)
11#include <sys/utsname.h>
12#endif
13
14#include <QProcess>
15#include <QTextStream>
16#include <QFile>
17#include <QSettings>
18
20{
21 QString GetOSName ()
22 {
23 const auto& info = GetOSInfo ();
24 return info.Name_ + ' ' + info.Version_;
25 }
26
27 namespace Linux
28 {
29 QString GetLSBName ()
30 {
31 QProcess proc;
32
33 proc.start (QStringLiteral ("/bin/sh"),
34 QStringList { "-c", "lsb_release -ds" },
35 QIODevice::ReadOnly);
36 if (proc.waitForStarted ())
37 {
38 QTextStream stream (&proc);
39 QString ret;
40 while (proc.waitForReadyRead ())
41 ret += stream.readAll ();
42 proc.close ();
43 if (!ret.isEmpty ())
44 return ret.remove ('"').trimmed ();
45 }
46
47 return {};
48 }
49
50 QString GetEtcOsName ()
51 {
52 static const auto osReleaseFile = QStringLiteral ("/etc/os-release");
53 if (!QFile::exists (osReleaseFile))
54 return {};
55
56 const QSettings relFile { osReleaseFile, QSettings::IniFormat };
57 const auto& prettyName = relFile.value (QStringLiteral ("PRETTY_NAME")).toString ();
58 const auto& name = relFile.value (QStringLiteral ("NAME")).toString ();
59 const auto& version = relFile.value (QStringLiteral ("VERSION")).toString ();
60 return !prettyName.isEmpty () ? prettyName : (name + " " + version);
61 }
62
63 QString GetEtcName ()
64 {
65 struct OsInfo
66 {
67 QString path;
68 QString name;
69 };
70 static const auto osptr = std::to_array<OsInfo> ({
71 { QStringLiteral ("/etc/mandrake-release"), QStringLiteral ("Mandrake Linux") },
72 { QStringLiteral ("/etc/debian_version"), QStringLiteral ("Debian GNU/Linux") },
73 { QStringLiteral ("/etc/gentoo-release"), QStringLiteral ("Gentoo Linux") },
74 { QStringLiteral ("/etc/exherbo-release"), QStringLiteral ("Exherbo") },
75 { QStringLiteral ("/etc/arch-release"), QStringLiteral ("Arch Linux") },
76 { QStringLiteral ("/etc/slackware-version"), QStringLiteral ("Slackware Linux") },
77 { QStringLiteral ("/etc/pld-release"), {} },
78 { QStringLiteral ("/etc/lfs-release"), QStringLiteral ("LFS") },
79 { QStringLiteral ("/etc/SuSE-release"), QStringLiteral ("SuSE linux") },
80 { QStringLiteral ("/etc/conectiva-release"), QStringLiteral ("Connectiva") },
81 { QStringLiteral ("/etc/.installed"), {} },
82 { QStringLiteral ("/etc/redhat-release"), {} },
83 });
84 for (const auto& os : osptr)
85 {
86 QFile f (os.path);
87 if (f.open (QIODevice::ReadOnly))
88 {
89 QString data = QString (f.read (1024)).trimmed ();
90 return os.name.isEmpty () ?
91 data :
92 QStringLiteral ("%1 (%2)").arg (os.name, data);
93 }
94 }
95
96 return {};
97 }
98 }
99
100 namespace
101 {
102#ifndef Q_OS_MAC
103 void Normalize (QString& osName)
104 {
105 auto trimQuotes = [&osName]
106 {
107 if (osName.startsWith ('"') && osName.endsWith ('"'))
108 osName = osName.mid (1, osName.size () - 1);
109 };
110
111 trimQuotes ();
112
113 static const auto nameMarker = QStringLiteral ("NAME=");
114 if (osName.startsWith (nameMarker))
115 osName = osName.mid (nameMarker.size ());
116
117 trimQuotes ();
118 }
119#endif
120 }
121
123 {
124#if defined(Q_OS_MAC)
125 const auto retVer = [] (const QString& version)
126 {
127 // LC only supports building on OS X 10.7 and higher, which all work only on x86_64.
128 return OSInfo { .Arch_ = "x86_64", .Name_ = "Mac OS X", .Version_ = version };
129 };
130
131 for (auto minor = 7; minor < 16; ++minor)
132 if (QSysInfo::MacintoshVersion == Q_MV_OSX (10, minor))
133 return retVer ("10." + QString::number (minor));
134
135 return retVer ("Unknown version");
136#elif defined(Q_OS_WIN32)
137 const auto retVer = [] (const QString& version)
138 {
139 return OSInfo
140 {
141 .Arch_ = QSysInfo::WordSize == 64 ? "x86_64" : "x86",
142 .Name_ = "Windows",
143 .Version_ = version
144 };
145 };
146
147 switch (QSysInfo::WindowsVersion)
148 {
149 case QSysInfo::WV_95:
150 return retVer ("95");
151 case QSysInfo::WV_98:
152 return retVer ("98");
153 case QSysInfo::WV_Me:
154 return retVer ("Me");
155 case QSysInfo::WV_DOS_based:
156 return retVer ("9x/Me");
157 case QSysInfo::WV_NT:
158 return retVer ("NT 4.x");
159 case QSysInfo::WV_2000:
160 return retVer ("2000");
161 case QSysInfo::WV_XP:
162 return retVer ("XP");
163 case QSysInfo::WV_2003:
164 return retVer ("2003");
165 case QSysInfo::WV_VISTA:
166 return retVer ("Vista");
167 case QSysInfo::WV_WINDOWS7:
168 return retVer ("7");
169 case 0x00a0:
170 return retVer ("8");
171 case 0x00b0:
172 return retVer ("8.1");
173 case 0x00c0:
174 return retVer ("10");
175 case QSysInfo::WV_NT_based:
176 return retVer ("NT-based");
177 }
178#else
179 auto osName = Linux::GetEtcOsName ();
180 if (osName.isEmpty ())
181 osName = Linux::GetEtcName ();
182 if (osName.isEmpty ())
183 osName = Linux::GetLSBName ();
184
185 Normalize (osName);
186
187 utsname u;
188 uname (&u);
189
190 return
191 {
192 .Arch_ = u.machine,
193 .Name_ = osName.isEmpty () ? u.sysname : osName,
194 .Version_ = QString ("%1 %2 %3").arg (u.machine, u.release, u.version),
195 .Flavour_ = u.sysname,
196 };
197#endif
198
199 return { .Arch_ = "Unknown arch", .Name_ = "Unknown OS", .Version_ = "Unknown version", .Flavour_ = {} };
200 }
201}
OSInfo GetOSInfo()
Returns more precise information about OS name and version.
Definition sysinfo.cpp:122
QString GetOSName()
Returns a string of OS name and version joined together.
Definition sysinfo.cpp:21
Describes the OS running LeechCraft.
Definition sysinfo.h:25