2012-03-15 10:17:03 +04:00
|
|
|
/*
|
|
|
|
This file is part of the PhantomJS project from Ofi Labs.
|
|
|
|
|
|
|
|
Copyright (C) 2012 execjosh, http://execjosh.blogspot.com
|
2012-11-06 21:39:45 +04:00
|
|
|
Copyright (C) 2012 James M. Greene <james.m.greene@gmail.com>
|
2012-03-15 10:17:03 +04:00
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the <organization> nor the
|
|
|
|
names of its contributors may be used to endorse or promote products
|
|
|
|
derived from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
|
|
|
|
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "system.h"
|
|
|
|
|
2012-09-03 17:50:34 +04:00
|
|
|
#include <QSslSocket>
|
2012-05-18 17:19:27 +04:00
|
|
|
#include <QSysInfo>
|
2012-09-03 17:50:34 +04:00
|
|
|
#include <QVariantMap>
|
2013-04-02 22:39:18 +04:00
|
|
|
#include <QTextCodec>
|
2012-05-18 17:19:27 +04:00
|
|
|
|
2012-03-15 12:19:23 +04:00
|
|
|
#include "../env.h"
|
2013-03-26 16:34:28 +04:00
|
|
|
#include "terminal.h"
|
2012-03-15 12:19:23 +04:00
|
|
|
|
2014-09-22 10:44:17 +04:00
|
|
|
#if defined(Q_OS_LINUX) || defined(Q_OS_MAC)
|
|
|
|
#include <sys/utsname.h>
|
|
|
|
QString getOSRelease()
|
|
|
|
{
|
|
|
|
QString release;
|
|
|
|
struct utsname un;
|
|
|
|
if (uname(&un) != -1) {
|
|
|
|
release = QString::fromLatin1(un.release);
|
|
|
|
}
|
|
|
|
|
|
|
|
return release;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-03-15 10:17:03 +04:00
|
|
|
System::System(QObject *parent) :
|
2012-12-29 20:23:10 +04:00
|
|
|
QObject(parent)
|
2012-12-15 15:01:36 +04:00
|
|
|
, m_stdout((File *)NULL)
|
|
|
|
, m_stderr((File *)NULL)
|
|
|
|
, m_stdin((File *)NULL)
|
2012-03-15 10:17:03 +04:00
|
|
|
{
|
2012-05-18 17:19:27 +04:00
|
|
|
// Populate "env"
|
|
|
|
m_env = Env::instance()->asVariantMap();
|
|
|
|
|
|
|
|
// Populate "os"
|
|
|
|
// "osarchitecture" word size
|
|
|
|
m_os.insert("architecture", QString("%1bit").arg(QSysInfo::WordSize));
|
|
|
|
|
|
|
|
// "os.name" and "os.version"
|
|
|
|
#if defined(Q_OS_WIN32)
|
|
|
|
m_os.insert("name", "windows");
|
|
|
|
switch (QSysInfo::WindowsVersion) {
|
|
|
|
case QSysInfo::WV_32s:
|
|
|
|
m_os.insert("version", "3.1");
|
|
|
|
break;
|
|
|
|
case QSysInfo::WV_95:
|
|
|
|
m_os.insert("version", "95");
|
|
|
|
break;
|
|
|
|
case QSysInfo::WV_98:
|
|
|
|
m_os.insert("version", "98");
|
|
|
|
break;
|
|
|
|
case QSysInfo::WV_Me:
|
|
|
|
m_os.insert("version", "Me");
|
|
|
|
break;
|
|
|
|
case QSysInfo::WV_NT:
|
|
|
|
m_os.insert("version", "NT");
|
|
|
|
break;
|
|
|
|
case QSysInfo::WV_2000:
|
|
|
|
m_os.insert("version", "2000");
|
|
|
|
break;
|
|
|
|
case QSysInfo::WV_XP:
|
|
|
|
m_os.insert("version", "XP");
|
|
|
|
break;
|
|
|
|
case QSysInfo::WV_2003:
|
|
|
|
m_os.insert("version", "2003");
|
|
|
|
break;
|
|
|
|
case QSysInfo::WV_VISTA:
|
|
|
|
m_os.insert("version", "Vista");
|
|
|
|
break;
|
|
|
|
case QSysInfo::WV_WINDOWS7:
|
|
|
|
m_os.insert("version", "7");
|
|
|
|
break;
|
2012-07-30 10:34:36 +04:00
|
|
|
case QSysInfo::WV_WINDOWS8:
|
|
|
|
m_os.insert("version", "8");
|
|
|
|
break;
|
2012-05-18 17:19:27 +04:00
|
|
|
default:
|
|
|
|
m_os.insert("version", "unknown");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#elif defined(Q_OS_MAC)
|
|
|
|
m_os.insert("name", "mac");
|
2014-09-23 18:51:36 +04:00
|
|
|
|
|
|
|
QString osRelease = getOSRelease();
|
|
|
|
m_os.insert("release", osRelease);
|
|
|
|
|
|
|
|
int kernelVersionMajor = 0;
|
|
|
|
QStringList releaseParts = osRelease.split('.');
|
|
|
|
if (releaseParts.length() == 3) {
|
|
|
|
kernelVersionMajor = releaseParts[0].toInt();
|
|
|
|
}
|
|
|
|
|
2012-05-18 17:19:27 +04:00
|
|
|
switch (QSysInfo::MacintoshVersion) {
|
|
|
|
case QSysInfo::MV_10_3:
|
|
|
|
m_os.insert("version", "10.3 (Panther)");
|
|
|
|
break;
|
|
|
|
case QSysInfo::MV_10_4:
|
|
|
|
m_os.insert("version", "10.4 (Tiger)");
|
|
|
|
break;
|
|
|
|
case QSysInfo::MV_10_5:
|
|
|
|
m_os.insert("version", "10.5 (Leopard)");
|
|
|
|
break;
|
|
|
|
case QSysInfo::MV_10_6:
|
|
|
|
m_os.insert("version", "10.6 (Snow Leopard)");
|
|
|
|
break;
|
|
|
|
case QSysInfo::MV_10_7:
|
|
|
|
m_os.insert("version", "10.7 (Lion)");
|
|
|
|
break;
|
2012-08-01 10:37:53 +04:00
|
|
|
case QSysInfo::MV_10_8:
|
|
|
|
m_os.insert("version", "10.8 (Mountain Lion)");
|
|
|
|
break;
|
2014-09-23 07:46:51 +04:00
|
|
|
case QSysInfo::MV_10_9:
|
|
|
|
m_os.insert("version", "10.9 (Mavericks)");
|
|
|
|
break;
|
2012-05-18 17:19:27 +04:00
|
|
|
default:
|
2014-09-23 18:51:36 +04:00
|
|
|
// Deduce OS X version from the kernel version.
|
|
|
|
// This is only used for version not yet recognized by Qt
|
|
|
|
// (there is no associated QSysInfo::MV_ enum).
|
|
|
|
switch (kernelVersionMajor) {
|
|
|
|
case 14:
|
|
|
|
m_os.insert("version", "10.10 (Yosemite)");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
m_os.insert("version", "unknown");
|
|
|
|
break;
|
|
|
|
}
|
2012-05-18 17:19:27 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
#elif defined(Q_OS_LINUX)
|
|
|
|
m_os.insert("name", "linux");
|
|
|
|
m_os.insert("version", "unknown");
|
2014-09-22 10:44:17 +04:00
|
|
|
m_os.insert("release", getOSRelease());
|
2012-05-18 17:19:27 +04:00
|
|
|
#else
|
|
|
|
m_os.insert("name", "unknown");
|
|
|
|
m_os.insert("version", "unknown");
|
|
|
|
#endif
|
2013-03-26 16:34:28 +04:00
|
|
|
|
|
|
|
connect(Terminal::instance(), SIGNAL(encodingChanged(QString)), this, SLOT(_onTerminalEncodingChanged(QString)));
|
2012-03-15 10:17:03 +04:00
|
|
|
}
|
|
|
|
|
2012-12-15 15:01:36 +04:00
|
|
|
System::~System()
|
|
|
|
{
|
|
|
|
// Clean-up standard streams
|
|
|
|
if ((File *)NULL != m_stdout) {
|
|
|
|
delete m_stdout;
|
|
|
|
m_stdout = (File *)NULL;
|
|
|
|
}
|
|
|
|
if ((File *)NULL != m_stderr) {
|
|
|
|
delete m_stderr;
|
|
|
|
m_stderr = (File *)NULL;
|
|
|
|
}
|
|
|
|
if ((File *)NULL != m_stdin) {
|
|
|
|
delete m_stdin;
|
|
|
|
m_stdin = (File *)NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-06 21:39:45 +04:00
|
|
|
qint64 System::pid() const
|
|
|
|
{
|
|
|
|
return QApplication::applicationPid();
|
|
|
|
}
|
|
|
|
|
2012-03-15 11:40:49 +04:00
|
|
|
void System::setArgs(const QStringList &args)
|
|
|
|
{
|
|
|
|
m_args = args;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList System::args() const
|
|
|
|
{
|
|
|
|
return m_args;
|
|
|
|
}
|
|
|
|
|
2012-03-15 12:19:23 +04:00
|
|
|
QVariant System::env() const
|
|
|
|
{
|
2012-05-18 17:19:27 +04:00
|
|
|
return m_env;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant System::os() const
|
|
|
|
{
|
|
|
|
return m_os;
|
2012-03-15 12:19:23 +04:00
|
|
|
}
|
|
|
|
|
2012-09-03 17:50:34 +04:00
|
|
|
bool System::isSSLSupported() const
|
|
|
|
{
|
|
|
|
return QSslSocket::supportsSsl();
|
|
|
|
}
|
|
|
|
|
2012-12-15 15:01:36 +04:00
|
|
|
QObject *System::_stdout() {
|
|
|
|
if ((File *)NULL == m_stdout) {
|
|
|
|
QFile *f = new QFile();
|
|
|
|
f->open(stdout, QIODevice::WriteOnly | QIODevice::Unbuffered);
|
2013-04-02 22:39:18 +04:00
|
|
|
m_stdout = createFileInstance(f);
|
2012-12-15 15:01:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return m_stdout;
|
|
|
|
}
|
|
|
|
|
|
|
|
QObject *System::_stderr() {
|
|
|
|
if ((File *)NULL == m_stderr) {
|
|
|
|
QFile *f = new QFile();
|
|
|
|
f->open(stderr, QIODevice::WriteOnly | QIODevice::Unbuffered);
|
2013-04-02 22:39:18 +04:00
|
|
|
m_stderr = createFileInstance(f);
|
2012-12-15 15:01:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return m_stderr;
|
|
|
|
}
|
|
|
|
|
|
|
|
QObject *System::_stdin() {
|
|
|
|
if ((File *)NULL == m_stdin) {
|
|
|
|
QFile *f = new QFile();
|
|
|
|
f->open(stdin, QIODevice::ReadOnly | QIODevice::Unbuffered);
|
2013-04-02 22:39:18 +04:00
|
|
|
m_stdin = createFileInstance(f);
|
2012-12-15 15:01:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return m_stdin;
|
|
|
|
}
|
2013-04-02 22:39:18 +04:00
|
|
|
|
2013-03-26 16:34:28 +04:00
|
|
|
// private slots:
|
|
|
|
|
|
|
|
void System::_onTerminalEncodingChanged(const QString &encoding)
|
|
|
|
{
|
|
|
|
if ((File *)NULL != m_stdin) {
|
|
|
|
m_stdin->setEncoding(encoding);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((File *)NULL != m_stdout) {
|
|
|
|
m_stdout->setEncoding(encoding);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((File *)NULL != m_stderr) {
|
|
|
|
m_stderr->setEncoding(encoding);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-02 22:39:18 +04:00
|
|
|
// private:
|
|
|
|
|
|
|
|
File *System::createFileInstance(QFile *f)
|
|
|
|
{
|
2013-03-26 16:34:28 +04:00
|
|
|
// Get the Encoding used by the Terminal at this point in time
|
|
|
|
Encoding e(Terminal::instance()->getEncoding());
|
|
|
|
QTextCodec *codec = e.getCodec();
|
2013-04-02 22:39:18 +04:00
|
|
|
return new File(f, codec, this);
|
|
|
|
}
|