LeechCraft  0.6.70-18450-gabe19ee3b0
Modular cross-platform feature rich live environment.
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) || defined(Q_OS_WIN32)
125  return OSInfo
126  {
127  .Arch_ = QSysInfo::currentCpuArchitecture (),
128  .Name_ = QSysInfo::productType (),
129  .Version_ = QSysInfo::productVersion (),
130  };
131 #else
132  auto osName = Linux::GetEtcOsName ();
133  if (osName.isEmpty ())
134  osName = Linux::GetEtcName ();
135  if (osName.isEmpty ())
136  osName = Linux::GetLSBName ();
137 
138  Normalize (osName);
139 
140  utsname u;
141  uname (&u);
142 
143  return
144  {
145  .Arch_ = u.machine,
146  .Name_ = osName.isEmpty () ? u.sysname : osName,
147  .Version_ = QString ("%1 %2 %3").arg (u.machine, u.release, u.version),
148  .Flavour_ = u.sysname,
149  };
150 #endif
151 
152  return { .Arch_ = "Unknown arch", .Name_ = "Unknown OS", .Version_ = "Unknown version", .Flavour_ = {} };
153  }
154 }
constexpr detail::ExprTree< detail::ExprType::LeafStaticPlaceholder, detail::MemberPtrs< Ptr > > f
Definition: oral.h:1083
QString GetOSName()
Returns a string of OS name and version joined together.
Definition: sysinfo.cpp:21
Describes the OS running LeechCraft.
Definition: sysinfo.h:24
QString Arch_
Describes the CPU architecture of the OS.
Definition: sysinfo.h:32
OSInfo GetOSInfo()
Returns more precise information about OS name and version.
Definition: sysinfo.cpp:122