not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / plasma / dataengines / systemmonitor / sensorclient.h
blob074f59c2d33586863b09856fe8d02fb009a703fd
1 /*
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>
29 namespace KSGRD {
31 /**
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.
37 class SensorClient
39 public:
40 explicit SensorClient() { }
41 virtual ~SensorClient() { }
43 /**
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>& ) { }
50 /**
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 &) { }
57 /**
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.
62 class SensorTokenizer
64 public:
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
70 int i=0;
71 int lastTokenAt = -1;
73 for( ; i < info.length(); ++i ) {
74 if( info[i] == '\\' ) {
75 ++i;
77 else if ( info[i] == separator ) {
78 m_tokens.append( unEscapeString( info.mid( lastTokenAt + 1, i - lastTokenAt - 1 ) ) );
79 lastTokenAt = i;
83 //Add everything after the last token
84 m_tokens.append( unEscapeString( info.mid( lastTokenAt + 1, i - lastTokenAt - 1 ) ) );
86 else {
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 ];
99 uint count()
101 return m_tokens.count();
104 private:
105 QList<QByteArray> m_tokens;
107 QByteArray unEscapeString( QByteArray string ) {
109 int i=0;
110 for( ; i < string.length(); ++i ) {
111 if( string[i] == '\\' ) {
112 string.remove( i, 1 );
113 ++i;
117 return string;
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
128 public:
129 explicit SensorIntegerInfo( const QByteArray &info )
130 : SensorTokenizer( info, '\t' ) { }
132 ~SensorIntegerInfo() { }
134 QString name()
136 return QString::fromUtf8((*this)[ 0 ]);
139 long min()
141 return (*this)[ 1 ].toLong();
144 long max()
146 return (*this)[ 2 ].toLong();
149 QString unit()
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
162 public:
163 explicit SensorFloatInfo( const QByteArray &info )
164 : SensorTokenizer( info, '\t' ) { }
166 ~SensorFloatInfo() { }
168 QString name()
170 return QString::fromUtf8((*this)[ 0 ]);
173 double min()
175 return (*this)[ 1 ].toDouble();
178 double max()
180 return (*this)[ 2 ].toDouble();
183 QString unit()
185 return QString::fromUtf8((*this)[ 3 ]);
192 #endif