~ubuntu-branches/ubuntu/wily/psi/wily-proposed

« back to all changes in this revision

Viewing changes to src/systeminfo.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2009-09-25 17:49:51 UTC
  • mfrom: (6.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20090925174951-lvm7kdap82o8xhn3
Tags: 0.13-1
* Updated to upstream version 0.13
* Set Standards-Version to 3.8.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2007-2008  Psi Development Team
 
3
 * Licensed under the GNU General Public License.
 
4
 * See the COPYING file for more information.
 
5
 */
 
6
 
1
7
#include <QString>
2
8
#include <QStringList>
3
9
#include <QFile>
4
10
#include <QFileInfo>
 
11
#include <QDir>
5
12
#include <QCoreApplication>
6
13
#include <QSysInfo>
7
14
#include <QProcess>
11
18
#include <time.h>
12
19
#include <stdlib.h>
13
20
#include <string.h>
14
 
#include <ctype.h>
15
21
#include <sys/utsname.h>
16
22
#endif
17
23
 
21
27
 
22
28
#include "systeminfo.h"
23
29
 
24
 
#define LSB_RELEASE "/usr/bin/lsb_release"
25
 
 
26
 
SystemInfo::SystemInfo() : QObject(QCoreApplication::instance())
 
30
#if defined(Q_WS_X11)
 
31
static QString lsbRelease(const QStringList& args)
27
32
{
28
 
        // Initialize
29
 
        timezone_offset_ = 0;
30
 
        timezone_str_ = "N/A";
31
 
        os_str_ = "Unknown";
32
 
        
33
 
        // Detect
34
 
#if defined(Q_WS_X11) || defined(Q_WS_MAC)
35
 
        time_t x;
36
 
        time(&x);
37
 
        char str[256];
38
 
        char fmt[32];
39
 
        strcpy(fmt, "%z");
40
 
        strftime(str, 256, fmt, localtime(&x));
41
 
        if(strcmp(fmt, str)) {
42
 
                QString s = str;
43
 
                if(s.at(0) == '+')
44
 
                        s.remove(0,1);
45
 
                s.truncate(s.length()-2);
46
 
                timezone_offset_ = s.toInt();
47
 
        }
48
 
        strcpy(fmt, "%Z");
49
 
        strftime(str, 256, fmt, localtime(&x));
50
 
        if(strcmp(fmt, str))
51
 
                timezone_str_ = str;
52
 
#endif
53
 
#if defined(Q_WS_X11)
54
 
        // attempt to get LSB version before trying the distro-specific approach
55
 
        if(QFileInfo(LSB_RELEASE).exists())
56
 
        {
57
 
                os_str_ = lsbRelease(QStringList("-ds"));
58
 
                
59
 
                if(!os_str_.isEmpty())
60
 
                        return;
61
 
        }
 
33
        QStringList path = QString(qgetenv("PATH")).split(':');
 
34
        QString found;
 
35
        
 
36
        foreach(QString dirname, path) {
 
37
                QDir dir(dirname);
 
38
                QFileInfo cand(dir.filePath("lsb_release"));
 
39
                if (cand.isExecutable()) {
 
40
                        found = cand.absoluteFilePath();
 
41
                        break;
 
42
                }
 
43
        }
 
44
 
 
45
        if (found.isEmpty()) {
 
46
                return QString();
 
47
        }
 
48
 
 
49
        QProcess process;
 
50
        process.start(found, args, QIODevice::ReadOnly);
 
51
 
 
52
        if(!process.waitForStarted())
 
53
                return QString();   // process failed to start
 
54
 
 
55
    QTextStream stream(&process);
 
56
        QString ret;
 
57
 
 
58
        while(process.waitForReadyRead())
 
59
           ret += stream.readAll();
 
60
 
 
61
        process.close();
 
62
        return ret.trimmed();
 
63
}
 
64
 
 
65
 
 
66
static QString unixHeuristicDetect() {
 
67
        
 
68
        QString ret;
62
69
        
63
70
        struct utsname u;
64
71
        uname(&u);
65
 
        os_str_.sprintf("%s", u.sysname);
 
72
        ret.sprintf("%s", u.sysname);
66
73
 
67
74
        // get description about os
68
75
        enum LinuxName {
132
139
 
133
140
                        switch ( osInfo[i].flags ) {
134
141
                                case OsUseFile:
135
 
                                        os_str_ = desc;
 
142
                                        ret = desc;
136
143
                                        break;
137
144
                                case OsUseName:
138
 
                                        os_str_ = osInfo[i].name;
 
145
                                        ret = osInfo[i].name;
139
146
                                        break;
140
147
                                case OsAppendFile:
141
 
                                        os_str_ = osInfo[i].name + " (" + desc + ")";
 
148
                                        ret = osInfo[i].name + " (" + desc + ")";
142
149
                                        break;
143
150
                        }
144
151
 
145
152
                        break;
146
153
                }
147
154
        }
 
155
        return ret;
 
156
}
 
157
#endif
 
158
 
 
159
 
 
160
 
 
161
SystemInfo::SystemInfo() : QObject(QCoreApplication::instance())
 
162
{
 
163
        // Initialize
 
164
        timezone_offset_ = 0;
 
165
        timezone_str_ = "N/A";
 
166
        os_str_ = "Unknown";
 
167
        
 
168
        // Detect
 
169
#if defined(Q_WS_X11) || defined(Q_WS_MAC)
 
170
        time_t x;
 
171
        time(&x);
 
172
        char str[256];
 
173
        char fmt[32];
 
174
        strcpy(fmt, "%z");
 
175
        strftime(str, 256, fmt, localtime(&x));
 
176
        if(strcmp(fmt, str)) {
 
177
                QString s = str;
 
178
                if(s.at(0) == '+')
 
179
                        s.remove(0,1);
 
180
                s.truncate(s.length()-2);
 
181
                timezone_offset_ = s.toInt();
 
182
        }
 
183
        strcpy(fmt, "%Z");
 
184
        strftime(str, 256, fmt, localtime(&x));
 
185
        if(strcmp(fmt, str))
 
186
                timezone_str_ = str;
 
187
#endif
 
188
#if defined(Q_WS_X11)
 
189
        // attempt to get LSB version before trying the distro-specific approach
 
190
        os_str_ = lsbRelease(QStringList() << "--description" << "--short");
 
191
 
 
192
        if(os_str_.isEmpty()) {
 
193
                os_str_ = unixHeuristicDetect();
 
194
        }
 
195
 
148
196
#elif defined(Q_WS_MAC)
149
197
        QSysInfo::MacVersion v = QSysInfo::MacintoshVersion;
150
198
        if(v == QSysInfo::MV_10_3)
153
201
                os_str_ = "Mac OS X 10.4";
154
202
        else if(v == QSysInfo::MV_10_5)
155
203
                os_str_ = "Mac OS X 10.5";
 
204
#if QT_VERSION >= 0x040500
 
205
        else if(v == QSysInfo::MV_10_6)
 
206
                os_str_ = "Mac OS X 10.6";
 
207
#endif
156
208
        else
157
209
                os_str_ = "Mac OS X";
158
210
#endif
194
246
                os_str_ = "Windows Server 2003";
195
247
        else if(v == QSysInfo::WV_VISTA)
196
248
                os_str_ = "Windows Vista";
 
249
#if QT_VERSION >= 0x040500
 
250
        else if(v == QSysInfo::WV_WINDOWS7)
 
251
                os_str_ = "Windows 7";
 
252
#endif
197
253
        else if(v == QSysInfo::WV_NT_based)
198
254
                os_str_ = "Windows NT";
199
255
#endif
200
256
}
201
257
 
202
 
#if defined(Q_WS_X11)
203
 
QString SystemInfo::lsbRelease(const QStringList& args)
204
 
{
205
 
        QProcess process;
206
 
        process.start(LSB_RELEASE, args, QIODevice::ReadOnly);
207
 
        
208
 
        if(!process.waitForStarted())
209
 
                return QString();   // process failed to start
210
 
 
211
 
    QTextStream stream(&process);
212
 
        QString ret;
213
 
        
214
 
        while(process.waitForReadyRead())
215
 
           ret += stream.readAll();
216
 
        
217
 
        process.close();
218
 
        return ret.trimmed();
219
 
}
220
 
#endif
221
 
 
222
258
SystemInfo* SystemInfo::instance()
223
259
{
224
260
        if (!instance_)