Branch data Line data Source code
1 : : // Copyright (C) 2021 The Qt Company Ltd.
2 : : // Copyright (C) 2019 Luxoft Sweden AB
3 : : // Copyright (C) 2018 Pelagicore AG
4 : : // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
5 : :
6 : : #include "iostatus.h"
7 : :
8 : : #include <QFile>
9 : :
10 : : using namespace Qt::StringLiterals;
11 : :
12 : : /*!
13 : : \qmltype IoStatus
14 : : \inqmlmodule QtApplicationManager
15 : : \ingroup common-instantiatable
16 : : \brief Provides information on the status of I/O devices.
17 : :
18 : : IoStatus provides information on the status of I/O devices.
19 : : Its property values are updated whenever the method update() is called.
20 : :
21 : : You can use this component as a MonitorModel data source if you want to plot its
22 : : previous values over time.
23 : :
24 : : \qml
25 : : import QtQuick
26 : : import QtApplicationManager
27 : : ...
28 : : MonitorModel {
29 : : IoStatus {
30 : : deviceNames: ["sda", "sdb"]
31 : : }
32 : : }
33 : : \endqml
34 : :
35 : : You can also use it alongside a Timer for instance, when you're only interested in its current value.
36 : :
37 : : \qml
38 : : import QtQuick
39 : : import QtApplicationManager
40 : : ...
41 : : IoStatus {
42 : : id: ioStatus
43 : : deviceNames: ["sda", "sdb"]
44 : : }
45 : : Timer {
46 : : interval: 500
47 : : running: true
48 : : repeat: true
49 : : onTriggered: ioStatus.update()
50 : : }
51 : : Text {
52 : : property string loadPercent: Number(ioStatus.ioLoad.sda * 100).toLocaleString(Qt.locale("en_US"), 'f', 1)
53 : : text: "sda load: " + loadPercent + "%"
54 : : }
55 : : \endqml
56 : : */
57 : :
58 : : QT_USE_NAMESPACE_AM
59 : :
60 : 2 : IoStatus::IoStatus(QObject *parent)
61 : 2 : : QObject(parent)
62 : : {
63 : 2 : }
64 : :
65 : 2 : IoStatus::~IoStatus()
66 : : {
67 : 2 : qDeleteAll(m_ioHash);
68 : 2 : }
69 : :
70 : : /*!
71 : : \qmlproperty list<string> IoStatus::deviceNames
72 : :
73 : : Names of the I/O devices to be probed.
74 : :
75 : : \note Currently this is only supported on Linux: device names have to match to filenames in
76 : : the \c /sys/block directory.
77 : : */
78 : 4 : QStringList IoStatus::deviceNames() const
79 : : {
80 : 4 : return m_deviceNames;
81 : : }
82 : :
83 : 2 : void IoStatus::setDeviceNames(const QStringList &value)
84 : : {
85 : 2 : qDeleteAll(m_ioHash);
86 : 2 : m_ioHash.clear();
87 : :
88 : 2 : m_deviceNames = value;
89 : :
90 [ + + ]: 4 : for (auto it = m_deviceNames.cbegin(); it != m_deviceNames.cend(); ++it)
91 : 2 : addIoReader(*it);
92 : :
93 : 2 : emit deviceNamesChanged();
94 : 2 : }
95 : :
96 : : /*!
97 : : \qmlproperty var IoStatus::ioLoad
98 : : \readonly
99 : :
100 : : A map of devices registered in deviceNames and their corresponding I/O loads in the
101 : : range [0, 1]. For instance the load of a device named "sda" can be accessed through
102 : : \c ioLoad.sda.
103 : :
104 : : Devices whose status could not be fetched won't be present in this property.
105 : :
106 : : The value of this property is updated when update() is called.
107 : :
108 : : \sa update
109 : : */
110 : 46 : QVariantMap IoStatus::ioLoad() const
111 : : {
112 [ + - - - ]: 92 : return m_ioLoad;
113 : : }
114 : :
115 : : /*!
116 : : \qmlproperty list<string> IoStatus::roleNames
117 : : \readonly
118 : :
119 : : Names of the roles provided by IoStatus when used as a MonitorModel data source.
120 : :
121 : : \sa MonitorModel
122 : : */
123 : 2 : QStringList IoStatus::roleNames() const
124 : : {
125 [ + + ]: 4 : return { u"ioLoad"_s };
126 : : }
127 : :
128 : : /*!
129 : : \qmlmethod IoStatus::update
130 : :
131 : : Updates the ioLoad property.
132 : :
133 : : \sa ioLoad
134 : : */
135 : 46 : void IoStatus::update()
136 : : {
137 : 46 : m_ioLoad.clear();
138 [ + + ]: 92 : for (auto it = m_ioHash.cbegin(); it != m_ioHash.cend(); ++it) {
139 : 46 : qreal ioVal = it.value()->readLoadValue();
140 [ + - ]: 46 : m_ioLoad.insert(it.key(), ioVal);
141 : : }
142 : 46 : emit ioLoadChanged();
143 : 46 : }
144 : :
145 : 2 : void IoStatus::addIoReader(const QString &deviceName)
146 : : {
147 [ + - + - : 2 : if (!QFile::exists(u"/dev/"_s + deviceName))
+ - ]
148 : 0 : return;
149 [ + - ]: 2 : if (m_ioHash.contains(deviceName))
150 : : return;
151 : :
152 [ + - + - ]: 4 : IoReader *ior = new IoReader(deviceName.toLocal8Bit().constData());
153 : 2 : m_ioHash.insert(deviceName, ior);
154 : : }
155 : :
156 : : #include "moc_iostatus.cpp"
|