2 KSysGuard, the KDE System Guard
4 Copyright (c) 1999, 2000 Chris Schlaeger <cs@kde.org>
5 Copyright (c) 2006 John Tapsell <tapsell@kde.org>
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of version 2 of the GNU General Public
9 License as published by the Free Software Foundation.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #ifndef KSG_SENSORCLIENT_H
23 #define KSG_SENSORCLIENT_H
25 #include <QtCore/QByteArray>
26 #include <QtCore/QList>
27 #include <QtCore/QString>
32 Every object that should act as a client to a sensor must inherit from
33 this class. A pointer to the client object is passed as SensorClient*
34 to the SensorAgent. When the requested information is available or a
35 problem occurred one of the member functions is called.
40 explicit SensorClient() { }
41 virtual ~SensorClient() { }
44 This function is called whenever the information from the sensor has
45 been received by the sensor agent. This function must be reimplemented
46 by the sensor client to receive and process this information.
48 virtual void answerReceived( const QString
&, const QList
<QByteArray
>& ) { }
51 In case of an unexpected fatal problem with the sensor the sensor
52 agent will call this function to notify the client about it.
54 virtual void sensorLost( const QString
&) { }
58 The following classes are utility classes that provide a
59 convenient way to retrieve pieces of information from the sensor
60 answers. For each type of answer there is a separate class.
65 SensorTokenizer( const QByteArray
&info
, char separator
)
67 if ( separator
== '/' ) {
68 //This is a special case where we assume that info is a '\' escaped string
73 for( ; i
< info
.length(); ++i
) {
74 if( info
[i
] == '\\' ) {
77 else if ( info
[i
] == separator
) {
78 m_tokens
.append( unEscapeString( info
.mid( lastTokenAt
+ 1, i
- lastTokenAt
- 1 ) ) );
83 //Add everything after the last token
84 m_tokens
.append( unEscapeString( info
.mid( lastTokenAt
+ 1, i
- lastTokenAt
- 1 ) ) );
87 m_tokens
= info
.split( separator
);
91 ~SensorTokenizer() { }
93 const QByteArray
& operator[]( unsigned idx
)
95 Q_ASSERT(idx
< (unsigned)(m_tokens
.count()));
96 return m_tokens
[ idx
];
101 return m_tokens
.count();
105 QList
<QByteArray
> m_tokens
;
107 QByteArray
unEscapeString( QByteArray string
) {
110 for( ; i
< string
.length(); ++i
) {
111 if( string
[i
] == '\\' ) {
112 string
.remove( i
, 1 );
122 An integer info contains 4 fields separated by TABS, a description
123 (name), the minimum and the maximum values and the unit.
124 e.g. Swap Memory 0 133885952 KB
126 class SensorIntegerInfo
: public SensorTokenizer
129 explicit SensorIntegerInfo( const QByteArray
&info
)
130 : SensorTokenizer( info
, '\t' ) { }
132 ~SensorIntegerInfo() { }
136 return QString::fromUtf8((*this)[ 0 ]);
141 return (*this)[ 1 ].toLong();
146 return (*this)[ 2 ].toLong();
151 return QString::fromUtf8((*this)[ 3 ]);
156 An float info contains 4 fields separated by TABS, a description
157 (name), the minimum and the maximum values and the unit.
158 e.g. CPU Voltage 0.0 5.0 V
160 class SensorFloatInfo
: public SensorTokenizer
163 explicit SensorFloatInfo( const QByteArray
&info
)
164 : SensorTokenizer( info
, '\t' ) { }
166 ~SensorFloatInfo() { }
170 return QString::fromUtf8((*this)[ 0 ]);
175 return (*this)[ 1 ].toDouble();
180 return (*this)[ 2 ].toDouble();
185 return QString::fromUtf8((*this)[ 3 ]);