LeechCraft  %{LEECHCRAFT_VERSION}
Modular cross-platform feature rich live environment.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
sysinfo.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2013 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 
40 namespace LeechCraft
41 {
42 namespace Util
43 {
44 namespace SysInfo
45 {
46  QString GetOSName ()
47  {
48  const auto& pair = GetOSNameSplit ();
49  return pair.first + ' ' + pair.second;
50  }
51 
52  typedef QPair<QString, QString> SplitInfo_t;
53 
54  QPair<QString, QString> GetOSNameSplit ()
55  {
56 #if defined(Q_OS_MAC)
57  QSysInfo::MacVersion v = QSysInfo::MacintoshVersion;
58  if (v == QSysInfo::MV_10_3)
59  return SplitInfo_t ("Mac OS X", "10.3");
60  else if (v == QSysInfo::MV_10_4)
61  return SplitInfo_t ("Mac OS X", "10.4");
62  else if (v == QSysInfo::MV_10_5)
63  return SplitInfo_t ("Mac OS X", "10.5");
64  else if (v == QSysInfo::MV_10_6)
65  return SplitInfo_t ("Mac OS X", "10.6");
66  else if (v == QSysInfo::MV_10_7)
67  return SplitInfo_t ("Mac OS X", "10.7");
68  else
69  return SplitInfo_t ("Mac OS X", "Unknown version");
70 #elif defined(Q_OS_WIN32)
71  QSysInfo::WinVersion v = QSysInfo::WindowsVersion;
72  if (v == QSysInfo::WV_95)
73  return SplitInfo_t ("Windows", "95");
74  else if (v == QSysInfo::WV_98)
75  return SplitInfo_t ("Windows", "98");
76  else if (v == QSysInfo::WV_Me)
77  return SplitInfo_t ("Windows", "Me");
78  else if (v == QSysInfo::WV_DOS_based)
79  return SplitInfo_t ("Windows", "9x/Me");
80  else if (v == QSysInfo::WV_NT)
81  return SplitInfo_t ("Windows", "NT 4.x");
82  else if (v == QSysInfo::WV_2000)
83  return SplitInfo_t ("Windows", "2000");
84  else if (v == QSysInfo::WV_XP)
85  return SplitInfo_t ("Windows", "XP");
86  else if (v == QSysInfo::WV_2003)
87  return SplitInfo_t ("Windows", "2003");
88  else if (v == QSysInfo::WV_VISTA)
89  return SplitInfo_t ("Windows", "Vista");
90  else if (v == QSysInfo::WV_WINDOWS7)
91  return SplitInfo_t ("Windows", "7");
92  else if (v == 0x00a0)
93  return SplitInfo_t ("Windows", "8");
94  else if (v == QSysInfo::WV_NT_based)
95  return SplitInfo_t ("Windows", "NT-based");
96 #else
97  QString osName;
98 
99  QProcess proc;
100  proc.start (QString ("/bin/sh"),
101  QStringList ("-c") << "lsb_release -ds", QIODevice::ReadOnly);
102  if (proc.waitForStarted ())
103  {
104  QTextStream stream (&proc);
105  QString ret;
106  while (proc.waitForReadyRead ())
107  ret += stream.readAll ();
108  proc.close ();
109  if (!ret.isEmpty ())
110  osName = ret.remove ('"').trimmed ();
111  }
112 
113  if (osName.isEmpty ())
114  {
115  struct OsInfo_t
116  {
117  QString path;
118  QString name;
119  } OsInfo [] =
120  {
121  { "/etc/mandrake-release", "Mandrake Linux" },
122  { "/etc/debian_version", "Debian GNU/Linux" },
123  { "/etc/gentoo-release", "Gentoo Linux" },
124  { "/etc/exherbo-release", "Exherbo" },
125  { "/etc/arch-release", "Arch Linux" },
126  { "/etc/slackware-version", "Slackware Linux" },
127  { "/etc/pld-release", "" },
128  { "/etc/lfs-release", "LFS" },
129  { "/etc/SuSE-release", "SuSE linux" },
130  { "/etc/conectiva-release", "Connectiva" },
131  { "/etc/.installed", "" },
132  { "/etc/redhat-release", "" },
133  { "", "" }
134  };
135  OsInfo_t *osptr = OsInfo;
136  while (!osptr->path.isEmpty ())
137  {
138  QFileInfo fi (osptr->path);
139  if (fi.exists ())
140  {
141  QFile f (osptr->path);
142  f.open (QIODevice::ReadOnly);
143  QString data = QString (f.read (1024)).trimmed ();
144  if (osptr->name.isEmpty ())
145  osName = data;
146  else
147  osName = QString ("%1 (%2)")
148  .arg (osptr->name)
149  .arg (data);
150  break;
151  }
152  ++osptr;
153  }
154  }
155 
156  utsname u;
157  uname (&u);
158 
159  return qMakePair (osName.isEmpty () ? QString (u.sysname) : osName,
160  QString ("%1 %2 %3").arg (u.machine, u.release, u.version));
161 #endif
162 
163  return qMakePair (QString ("Unknown OS"), QString ("Unknown version"));
164  }
165 }
166 }
167 }
QPair< QString, QString > GetOSNameSplit()
Returns a pair of OS name and version.
Definition: sysinfo.cpp:54
QString GetOSName()
Returns a string of OS name and version joined together.
Definition: sysinfo.cpp:46
QPair< QString, QString > SplitInfo_t
Definition: sysinfo.cpp:52