LeechCraft  %{LEECHCRAFT_VERSION}
Modular cross-platform feature rich live environment.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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  * Boost Software License - Version 1.0 - August 17th, 2003
6  *
7  * Permission is hereby granted, free of charge, to any person or organization
8  * obtaining a copy of the software and accompanying documentation covered by
9  * this license (the "Software") to use, reproduce, display, distribute,
10  * execute, and transmit the Software, and to prepare derivative works of the
11  * Software, and to permit third-parties to whom the Software is furnished to
12  * do so, all subject to the following:
13  *
14  * The copyright notices in the Software and this entire statement, including
15  * the above license grant, this restriction and the following disclaimer,
16  * must be included in all copies of the Software, in whole or in part, and
17  * all derivative works of the Software, unless such copies or derivative
18  * works are solely in the form of machine-executable object code generated by
19  * a source language processor.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  **********************************************************************/
29 
30 #include "sysinfo.h"
31 #if !defined(Q_OS_WIN32)
32 #include <sys/utsname.h>
33 #endif
34 
35 #include <QProcess>
36 #include <QTextStream>
37 #include <QFileInfo>
38 #include <QFile>
39 #include <QSettings>
40 
41 namespace LeechCraft
42 {
43 namespace Util
44 {
45 namespace SysInfo
46 {
47  QString GetOSName ()
48  {
49  const auto& pair = GetOSNameSplit ();
50  return pair.first + ' ' + pair.second;
51  }
52 
53  typedef QPair<QString, QString> SplitInfo_t;
54 
55  namespace Linux
56  {
57  QString GetLSBName ()
58  {
59  QProcess proc;
60 
61  proc.start (QString ("/bin/sh"),
62  QStringList ("-c") << "lsb_release -ds", QIODevice::ReadOnly);
63  if (proc.waitForStarted ())
64  {
65  QTextStream stream (&proc);
66  QString ret;
67  while (proc.waitForReadyRead ())
68  ret += stream.readAll ();
69  proc.close ();
70  if (!ret.isEmpty ())
71  return ret.remove ('"').trimmed ();
72  }
73 
74  return {};
75  }
76 
77  QString GetEtcOsName ()
78  {
79  if (!QFile::exists ("/etc/os-release"))
80  return {};
81 
82  QSettings relFile { "/etc/os-release", QSettings::IniFormat };
83  relFile.setIniCodec ("UTF-8");
84 
85  const auto& prettyName = relFile.value ("PRETTY_NAME").toString ();
86  const auto& name = relFile.value ("NAME").toString ();
87  const auto& version = relFile.value ("VERSION").toString ();
88  return !prettyName.isEmpty () ? prettyName : (name + " " + version);
89  }
90 
91  QString GetEtcName ()
92  {
93  struct OsInfo_t
94  {
95  QString path;
96  QString name;
97  } OsInfo [] =
98  {
99  { "/etc/mandrake-release", "Mandrake Linux" },
100  { "/etc/debian_version", "Debian GNU/Linux" },
101  { "/etc/gentoo-release", "Gentoo Linux" },
102  { "/etc/exherbo-release", "Exherbo" },
103  { "/etc/arch-release", "Arch Linux" },
104  { "/etc/slackware-version", "Slackware Linux" },
105  { "/etc/pld-release", "" },
106  { "/etc/lfs-release", "LFS" },
107  { "/etc/SuSE-release", "SuSE linux" },
108  { "/etc/conectiva-release", "Connectiva" },
109  { "/etc/.installed", "" },
110  { "/etc/redhat-release", "" },
111  { "", "" }
112  };
113  OsInfo_t *osptr = OsInfo;
114  while (!osptr->path.isEmpty ())
115  {
116  QFileInfo fi (osptr->path);
117  if (fi.exists ())
118  {
119  QFile f (osptr->path);
120  f.open (QIODevice::ReadOnly);
121  QString data = QString (f.read (1024)).trimmed ();
122  if (osptr->name.isEmpty ())
123  return data;
124  else
125  return QString ("%1 (%2)")
126  .arg (osptr->name)
127  .arg (data);
128  }
129  ++osptr;
130  }
131 
132  return {};
133  }
134  }
135 
136  namespace
137  {
138 #ifndef Q_OS_MAC
139  void Normalize (QString& osName)
140  {
141  auto trimQuotes = [&osName]
142  {
143  if (osName.startsWith ('"') && osName.endsWith ('"'))
144  osName = osName.mid (1, osName.size () - 1);
145  };
146 
147  trimQuotes ();
148 
149  const QString nameMarker ("NAME=");
150  if (osName.startsWith (nameMarker))
151  osName = osName.mid (nameMarker.size ());
152 
153  trimQuotes ();
154  }
155 #endif
156  }
157 
158  QPair<QString, QString> GetOSNameSplit ()
159  {
160 #if defined(Q_OS_MAC)
161  QSysInfo::MacVersion v = QSysInfo::MacintoshVersion;
162  switch (v)
163  {
164  case QSysInfo::MV_10_3:
165  return { "Mac OS X", "10.3" };
166  case QSysInfo::MV_10_4:
167  return { "Mac OS X", "10.4" };
168  case QSysInfo::MV_10_5:
169  return { "Mac OS X", "10.5" };
170  case QSysInfo::MV_10_6:
171  return { "Mac OS X", "10.6" };
172  case QSysInfo::MV_10_7:
173  return { "Mac OS X", "10.7" };
174  case QSysInfo::MV_10_8:
175  return { "Mac OS X", "10.8" };
176  case QSysInfo::MV_10_9:
177  return { "Mac OS X", "10.9" };
178  default:
179  return { "Max OS X", "Unknown version" };
180  }
181 #elif defined(Q_OS_WIN32)
182  QSysInfo::WinVersion v = QSysInfo::WindowsVersion;
183  if (v == QSysInfo::WV_95)
184  return SplitInfo_t ("Windows", "95");
185  else if (v == QSysInfo::WV_98)
186  return SplitInfo_t ("Windows", "98");
187  else if (v == QSysInfo::WV_Me)
188  return SplitInfo_t ("Windows", "Me");
189  else if (v == QSysInfo::WV_DOS_based)
190  return SplitInfo_t ("Windows", "9x/Me");
191  else if (v == QSysInfo::WV_NT)
192  return SplitInfo_t ("Windows", "NT 4.x");
193  else if (v == QSysInfo::WV_2000)
194  return SplitInfo_t ("Windows", "2000");
195  else if (v == QSysInfo::WV_XP)
196  return SplitInfo_t ("Windows", "XP");
197  else if (v == QSysInfo::WV_2003)
198  return SplitInfo_t ("Windows", "2003");
199  else if (v == QSysInfo::WV_VISTA)
200  return SplitInfo_t ("Windows", "Vista");
201  else if (v == QSysInfo::WV_WINDOWS7)
202  return SplitInfo_t ("Windows", "7");
203  else if (v == 0x00a0)
204  return SplitInfo_t ("Windows", "8");
205  else if (v == QSysInfo::WV_NT_based)
206  return SplitInfo_t ("Windows", "NT-based");
207 #else
208  auto osName = Linux::GetEtcOsName ();
209 
210  if (osName.isEmpty ())
211  osName = Linux::GetEtcName ();
212 
213  if (osName.isEmpty ())
214  osName = Linux::GetLSBName ();
215 
216  Normalize (osName);
217 
218  utsname u;
219  uname (&u);
220 
221  return qMakePair (osName.isEmpty () ? QString (u.sysname) : osName,
222  QString ("%1 %2 %3").arg (u.machine, u.release, u.version));
223 #endif
224 
225  return qMakePair (QString ("Unknown OS"), QString ("Unknown version"));
226  }
227 }
228 }
229 }
QString GetOSName()
Returns a string of OS name and version joined together.
Definition: sysinfo.cpp:47
QPair< QString, QString > SplitInfo_t
Definition: sysinfo.cpp:53
QPair< QString, QString > GetOSNameSplit()
Returns a pair of OS name and version.
Definition: sysinfo.cpp:158