aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/systeminfo/systeminfo.cpp
blob: d153259c2d8967d8f6d773bbf5223e8673168eb2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/****************************************************************************
**
** Copyright (C) 2019 Luxoft Sweden AB
** Copyright (C) 2018 Pelagicore AG
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Neptune IVI UI.
**
** $QT_BEGIN_LICENSE:GPL-QTAS$
** Commercial License Usage
** Licensees holding valid commercial Qt Automotive Suite licenses may use
** this file in accordance with the commercial license agreement provided
** with the Software or, alternatively, in accordance with the terms
** contained in a written agreement between you and The Qt Company.  For
** licensing terms and conditions see https://www.qt.io/terms-conditions.
** For further information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
** SPDX-License-Identifier: GPL-3.0
**
****************************************************************************/

#include <QNetworkInterface>
#include <QtNetwork>
#include <QStandardPaths>
#include <QSysInfo>
#include <QLibraryInfo>
#include <QtQml/qqmlinfo.h>
#include <QtGui/QOpenGLContext>
#include <QtGlobal>

#include "systeminfo.h"

/*!
  \qmltype SystemInfo
  \instantiates QObject
  \inqmlmodule shared.com.pelagicore.systeminfo
  \since 5.11
  \brief Provides information about Qt configuration, network state, operating system and hardware.

  A SystemInfo type provides information about the system, in this case the device and the
  Operating System, which the app runs on. It provides Network availability related properties and
  a subset of the C++ \l{QSysInfo} API. All of these properties are read-only.

  \sa QSysInfo
*/
SystemInfo::SystemInfo(QObject *parent)
    : QObject(parent)
    , m_networkManager(new QNetworkAccessManager(this))
{
    connect(
        m_networkManager, &QNetworkAccessManager::finished,
        this, &SystemInfo::replyFinished
    );
}

SystemInfo::~SystemInfo()
{
    killTimer(m_timerId);
#if QT_CONFIG(process)
    delete m_diagProc;
#endif
}

void SystemInfo::init()
{
    getAddress();
    if (QSslSocket::supportsSsl()) {
        m_timerId = startTimer(1000, Qt::VeryCoarseTimer);
    } else {
        updateInternetAccessStatus(false);
        qmlWarning(this) << "SSL/TLS is not supported in this installation. HTTPS connections can't be established";
        qmlWarning(this) << "Please verify, SSL library is installed and/or updated";
        qmlWarning(this) << "This installation was build with: " << QSslSocket::sslLibraryBuildVersionString();
    }
}

/*
 * This function fills all available Addresses of your device in a list.
*/
void SystemInfo::getAddress()
{
    m_addressList.clear();
    for (const QNetworkInterface &_interface : QNetworkInterface::allInterfaces()) {
        if (_interface.flags().testFlag(QNetworkInterface::IsUp)
                && !_interface.flags().testFlag(QNetworkInterface::IsLoopBack)
                && _interface.type() != QNetworkInterface::InterfaceType::Unknown
                && _interface.type() != QNetworkInterface::InterfaceType::Loopback
                && _interface.type() != QNetworkInterface::InterfaceType::Virtual) {
            for (const QNetworkAddressEntry &entry : _interface.addressEntries()) {
                if (_interface.hardwareAddress() != QLatin1String("00:00:00:00:00:00")) {
                    m_addressList.append(_interface.name() + QLatin1String(" ") + entry.ip().toString() + QLatin1String(" ") + _interface.hardwareAddress());
                    emit addressListChanged();
                }
            }
        }
    }

    if (m_addressList.removeDuplicates() > 0 || m_addressList.isEmpty()) {
        emit addressListChanged();
    }

    // Here we suppose that if there is any physical connection, then device is connected
    updateConnectedStatus(!m_addressList.isEmpty());
}

void SystemInfo::getQtDiagInfo()
{
    m_qtDiagContents.clear();
    // first try in the current Qt dir
    QString qtdiagExe = QStandardPaths::findExecutable(QStringLiteral("qtdiag"), {QLibraryInfo::location(QLibraryInfo::BinariesPath)});
    if (qtdiagExe.isEmpty()) {
        // try in $PATH
        qtdiagExe = QStandardPaths::findExecutable(QStringLiteral("qtdiag"));
    }
    if (qtdiagExe.isEmpty()) {
        m_qtDiagContents = QObject::tr("The qtdiag program could not be found.");
        emit qtDiagChanged();
        return;
    }

#if QT_CONFIG(process)
    m_diagProc = new QProcess;
    connect(m_diagProc, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), this,
            [this, qtdiagExe](int, QProcess::ExitStatus exitStatus) {
        if (exitStatus == QProcess::NormalExit) {
            m_qtDiagContents = QString::fromUtf8(m_diagProc->readAllStandardOutput());
        } else {
            m_qtDiagContents = QObject::tr("The qtdiag program exited unsuccessfully/crashed.");
        }
        m_diagProc->close();
        m_qtDiagContents.prepend(QObject::tr("Output from %1:").arg(qtdiagExe) + QStringLiteral("\n\n"));
        emit qtDiagChanged();
    });
    m_diagProc->start(qtdiagExe, QProcess::ReadOnly);
#endif
}

bool SystemInfo::allow3dStudioPresentations()
{
#ifndef QT_NO_OPENGL
    QOpenGLContext *globalShareContext = QOpenGLContext::globalShareContext();

    if (globalShareContext && globalShareContext->isValid()) {
        return (globalShareContext->isOpenGLES()
                        && globalShareContext->format().version() >= qMakePair(3,0))
                || (!globalShareContext->isOpenGLES()
                        && globalShareContext->format().version() >= qMakePair(4,3));
    }
#endif

    return false;
}

bool SystemInfo::allowOpenGLContent()
{
#ifndef QT_NO_OPENGL
    QOpenGLContext *globalShareContext = QOpenGLContext::globalShareContext();
    return globalShareContext && globalShareContext->isValid();
#endif

    return false;
}

void SystemInfo::timerEvent(QTimerEvent *event)
{
    Q_UNUSED(event);
    getAddress();
    auto reply = m_networkManager->get(QNetworkRequest(QUrl("https://www.google.com")));
    connect(reply, QOverload<QNetworkReply::NetworkError>::of(&QNetworkReply::error),
        [=](QNetworkReply::NetworkError code) {
            updateInternetAccessStatus(code == QNetworkReply::NetworkError::NoError);
        }
    );
}

void SystemInfo::updateConnectedStatus(bool status)
{
    if (m_connected != status) {
        m_connected = status;
        emit connectedChanged();
    }
}

void SystemInfo::replyFinished(QNetworkReply *reply)
{
    if (reply->error()) {
        qDebug() << reply->errorString();
        updateInternetAccessStatus(false);
    } else {
        if (reply->bytesAvailable()) {
            updateInternetAccessStatus(true);
        } else {
            updateInternetAccessStatus(false);
        }
    }

    reply->deleteLater();
}

void SystemInfo::updateInternetAccessStatus(bool status)
{
    if (status != m_internetAccess) {
        m_internetAccess = status;
        emit internetAccessChanged();
    }
}

/*!
  \qmlproperty var SystemInfo::addressList

   Returns addresses for all available network interfaces of the device in the following format:

   \c {<interface name> <ip address> <hardware address>}

   \code
   SystemInfo {
        id: sysinfo
   }
   Text {
        text: sysinfo.addressList.join("\n")
   }
   \endcode

   \sa {QNetworkInterface::allInterfaces()}
*/

QStringList SystemInfo::addressList() const
{
    return m_addressList;
}

/*!
    \qmlproperty bool SystemInfo::connected

    Specifies whether the device is connected to the network.
*/
bool SystemInfo::connected() const
{
    return m_connected;
}

/*!
    \qmlproperty bool SystemInfo::internetAccess

    Specifies whether the device has connection to the Internet.
*/
bool SystemInfo::internetAccess() const
{
    return m_internetAccess;
}

/*!
    \qmlproperty string SystemInfo::qtVersion

    Returns the Qt version.

    \sa {QtCore}{qVersion}
*/
QString SystemInfo::qtVersion() const
{
    return QString::fromLatin1(qVersion());
}

/*!
    \qmlproperty string SystemInfo::productName

    Returns a product type, version, as well as tokens like the Operating System type, codenames,
    and more. The result of this function is suitable to display to the user, but not for
    long-term storage, as the string may change in future versions of Qt.

    \sa {QSysInfo::prettyProductName()}
*/
QString SystemInfo::productName() const
{
    return QSysInfo::prettyProductName();
}

/*!
    \qmlproperty string SystemInfo::cpu

    Returns the full architecture string that Qt was compiled for: CPU Architecture, endianness,
    word size and ABI (optional).

    \sa {QSysInfo::buildAbi()}
*/
QString SystemInfo::cpu() const
{
    return QSysInfo::buildAbi();
}

/*!
    \qmlproperty string SystemInfo::kernel

    Returns the Operating System kernel type, for which Qt was compiled.

    \sa {QSysInfo::kernelType()}
*/
QString SystemInfo::kernel() const
{
    return QSysInfo::kernelType();
}

/*!
    \qmlproperty string SystemInfo::kernelVersion

    Returns the Operating System's kernel version.

    \sa {QSysInfo::kernelVersion()}
*/
QString SystemInfo::kernelVersion() const
{
    return QSysInfo::kernelVersion();
}

/*!
    \qmlproperty string SystemInfo::qtDiag

    Returns the output from a \c qtdiag run, which contains information on the current Qt
    installation.
*/

QString SystemInfo::qtDiag() const
{
    return m_qtDiagContents;
}

QVariant SystemInfo::readEnvironmentVariable(const QString &name) const
{
    return qgetenv(name.toLocal8Bit());
}

bool SystemInfo::isEnvironmentVariableSet(const QString &name) const
{
    return !qgetenv(name.toLocal8Bit()).isNull();
}

bool SystemInfo::isEnvironmentVariableEmpty(const QString &name) const
{
    return qgetenv(name.toLocal8Bit()).isEmpty();
}

void SystemInfo::classBegin()
{
    getQtDiagInfo();
}

void SystemInfo::componentComplete()
{
    metaObject()->invokeMethod(this, "init", Qt::QueuedConnection);
}