Plasma
sensorclient.h
Go to the documentation of this file.00001 /* 00002 KSysGuard, the KDE System Guard 00003 00004 Copyright (c) 1999, 2000 Chris Schlaeger <cs@kde.org> 00005 Copyright (c) 2006 John Tapsell <tapsell@kde.org> 00006 00007 This program is free software; you can redistribute it and/or 00008 modify it under the terms of version 2 of the GNU General Public 00009 License as published by the Free Software Foundation. 00010 00011 This program is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 00016 You should have received a copy of the GNU General Public License 00017 along with this program; if not, write to the Free Software 00018 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00019 00020 */ 00021 00022 #ifndef KSG_SENSORCLIENT_H 00023 #define KSG_SENSORCLIENT_H 00024 00025 #include <QtCore/QByteArray> 00026 #include <QtCore/QList> 00027 #include <QtCore/QString> 00028 00029 namespace KSGRD { 00030 00037 class SensorClient 00038 { 00039 public: 00040 explicit SensorClient() { } 00041 virtual ~SensorClient() { } 00042 00048 virtual void answerReceived( const QString &, const QList<QByteArray>& ) { } 00049 00054 virtual void sensorLost( const QString &) { } 00055 }; 00056 00062 class SensorTokenizer 00063 { 00064 public: 00065 SensorTokenizer( const QByteArray &info, char separator ) 00066 { 00067 if ( separator == '/' ) { 00068 //This is a special case where we assume that info is a '\' escaped string 00069 00070 int i=0; 00071 int lastTokenAt = -1; 00072 00073 for( ; i < info.length(); ++i ) { 00074 if( info[i] == '\\' ) { 00075 ++i; 00076 } 00077 else if ( info[i] == separator ) { 00078 m_tokens.append( unEscapeString( info.mid( lastTokenAt + 1, i - lastTokenAt - 1 ) ) ); 00079 lastTokenAt = i; 00080 } 00081 } 00082 00083 //Add everything after the last token 00084 m_tokens.append( unEscapeString( info.mid( lastTokenAt + 1, i - lastTokenAt - 1 ) ) ); 00085 } 00086 else { 00087 m_tokens = info.split( separator ); 00088 } 00089 } 00090 00091 ~SensorTokenizer() { } 00092 00093 const QByteArray& operator[]( unsigned idx ) 00094 { 00095 Q_ASSERT(idx < (unsigned)(m_tokens.count())); 00096 return m_tokens[ idx ]; 00097 } 00098 00099 uint count() 00100 { 00101 return m_tokens.count(); 00102 } 00103 00104 private: 00105 QList<QByteArray> m_tokens; 00106 00107 QByteArray unEscapeString( QByteArray string ) { 00108 00109 int i=0; 00110 for( ; i < string.length(); ++i ) { 00111 if( string[i] == '\\' ) { 00112 string.remove( i, 1 ); 00113 ++i; 00114 } 00115 } 00116 00117 return string; 00118 } 00119 }; 00120 00126 class SensorIntegerInfo : public SensorTokenizer 00127 { 00128 public: 00129 explicit SensorIntegerInfo( const QByteArray &info ) 00130 : SensorTokenizer( info, '\t' ) { } 00131 00132 ~SensorIntegerInfo() { } 00133 00134 QString name() 00135 { 00136 return QString::fromUtf8((*this)[ 0 ]); 00137 } 00138 00139 long min() 00140 { 00141 return (*this)[ 1 ].toLong(); 00142 } 00143 00144 long max() 00145 { 00146 return (*this)[ 2 ].toLong(); 00147 } 00148 00149 QString unit() 00150 { 00151 return QString::fromUtf8((*this)[ 3 ]); 00152 } 00153 }; 00154 00160 class SensorFloatInfo : public SensorTokenizer 00161 { 00162 public: 00163 explicit SensorFloatInfo( const QByteArray &info ) 00164 : SensorTokenizer( info, '\t' ) { } 00165 00166 ~SensorFloatInfo() { } 00167 00168 QString name() 00169 { 00170 return QString::fromUtf8((*this)[ 0 ]); 00171 } 00172 00173 double min() 00174 { 00175 return (*this)[ 1 ].toDouble(); 00176 } 00177 00178 double max() 00179 { 00180 return (*this)[ 2 ].toDouble(); 00181 } 00182 00183 QString unit() 00184 { 00185 return QString::fromUtf8((*this)[ 3 ]); 00186 } 00187 }; 00188 00189 00190 } 00191 00192 #endif