LeechCraft 0.6.70-16373-g319c272718
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 QSettings relFile { osReleaseFile, QSettings::IniFormat };
57 relFile.setIniCodec ("UTF-8");
58
59 const auto& prettyName = relFile.value (QStringLiteral ("PRETTY_NAME")).toString ();
60 const auto& name = relFile.value (QStringLiteral ("NAME")).toString ();
61 const auto& version = relFile.value (QStringLiteral ("VERSION")).toString ();
62 return !prettyName.isEmpty () ? prettyName : (name + " " + version);
63 }
64
65 QString GetEtcName ()
66 {
67 struct OsInfo
68 {
69 QString path;
70 QString name;
71 };
72 static const auto osptr = std::to_array<OsInfo> ({
73 { QStringLiteral ("/etc/mandrake-release"), QStringLiteral ("Mandrake Linux") },
74 { QStringLiteral ("/etc/debian_version"), QStringLiteral ("Debian GNU/Linux") },
75 { QStringLiteral ("/etc/gentoo-release"), QStringLiteral ("Gentoo Linux") },
76 { QStringLiteral ("/etc/exherbo-release"), QStringLiteral ("Exherbo") },
77 { QStringLiteral ("/etc/arch-release"), QStringLiteral ("Arch Linux") },
78 { QStringLiteral ("/etc/slackware-version"), QStringLiteral ("Slackware Linux") },
79 { QStringLiteral ("/etc/pld-release"), {} },
80 { QStringLiteral ("/etc/lfs-release"), QStringLiteral ("LFS") },
81 { QStringLiteral ("/etc/SuSE-release"), QStringLiteral ("SuSE linux") },
82 { QStringLiteral ("/etc/conectiva-release"), QStringLiteral ("Connectiva") },
83 { QStringLiteral ("/etc/.installed"), {} },
84 { QStringLiteral ("/etc/redhat-release"), {} },
85 });
86 for (const auto& os : osptr)
87 {
88 QFile f (os.path);
89 if (f.open (QIODevice::ReadOnly))
90 {
91 QString data = QString (f.read (1024)).trimmed ();
92 return os.name.isEmpty () ?
93 data :
94 QStringLiteral ("%1 (%2)").arg (os.name, data);
95 }
96 }
97
98 return {};
99 }
100 }
101
102 namespace
103 {
104#ifndef Q_OS_MAC
105 void Normalize (QString& osName)
106 {
107 auto trimQuotes = [&osName]
108 {
109 if (osName.startsWith ('"') && osName.endsWith ('"'))
110 osName = osName.mid (1, osName.size () - 1);
111 };
112
113 trimQuotes ();
114
115 static const auto nameMarker = QStringLiteral ("NAME=");
116 if (osName.startsWith (nameMarker))
117 osName = osName.mid (nameMarker.size ());
118
119 trimQuotes ();
120 }
121#endif
122 }
123
125 {
126#if defined(Q_OS_MAC)
127 const auto retVer = [] (const QString& version)
128 {
129 // LC only supports building on OS X 10.7 and higher, which all work only on x86_64.
130 return OSInfo { .Arch_ = "x86_64", .Name_ = "Mac OS X", .Version_ = version };
131 };
132
133 for (auto minor = 7; minor < 16; ++minor)
134 if (QSysInfo::MacintoshVersion == Q_MV_OSX (10, minor))
135 return retVer ("10." + QString::number (minor));
136
137 return retVer ("Unknown version");
138#elif defined(Q_OS_WIN32)
139 const auto retVer = [] (const QString& version)
140 {
141 return OSInfo
142 {
143 .Arch_ = QSysInfo::WordSize == 64 ? "x86_64" : "x86",
144 .Name_ = "Windows",
145 .Version_ = version
146 };
147 };
148
149 switch (QSysInfo::WindowsVersion)
150 {
151 case QSysInfo::WV_95:
152 return retVer ("95");
153 case QSysInfo::WV_98:
154 return retVer ("98");
155 case QSysInfo::WV_Me:
156 return retVer ("Me");
157 case QSysInfo::WV_DOS_based:
158 return retVer ("9x/Me");
159 case QSysInfo::WV_NT:
160 return retVer ("NT 4.x");
161 case QSysInfo::WV_2000:
162 return retVer ("2000");
163 case QSysInfo::WV_XP:
164 return retVer ("XP");
165 case QSysInfo::WV_2003:
166 return retVer ("2003");
167 case QSysInfo::WV_VISTA:
168 return retVer ("Vista");
169 case QSysInfo::WV_WINDOWS7:
170 return retVer ("7");
171 case 0x00a0:
172 return retVer ("8");
173 case 0x00b0:
174 return retVer ("8.1");
175 case 0x00c0:
176 return retVer ("10");
177 case QSysInfo::WV_NT_based:
178 return retVer ("NT-based");
179 }
180#else
181 auto osName = Linux::GetEtcOsName ();
182 if (osName.isEmpty ())
183 osName = Linux::GetEtcName ();
184 if (osName.isEmpty ())
185 osName = Linux::GetLSBName ();
186
187 Normalize (osName);
188
189 utsname u;
190 uname (&u);
191
192 return
193 {
194 .Arch_ = u.machine,
195 .Name_ = osName.isEmpty () ? u.sysname : osName,
196 .Version_ = QString ("%1 %2 %3").arg (u.machine, u.release, u.version),
197 .Flavour_ = u.sysname,
198 };
199#endif
200
201 return { .Arch_ = "Unknown arch", .Name_ = "Unknown OS", .Version_ = "Unknown version", .Flavour_ = {} };
202 }
203}
OSInfo GetOSInfo()
Returns more precise information about OS name and version.
Definition sysinfo.cpp:124
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