textfilesensor.cpp
00001 /*************************************************************************** 00002 * Copyright (C) 2003 by Hans Karlsson * 00003 * karlsson.h@home.se * 00004 * * 00005 * This program is free software; you can redistribute it and/or modify * 00006 * it under the terms of the GNU General Public License as published by * 00007 * the Free Software Foundation; either version 2 of the License, or * 00008 * (at your option) any later version. * 00009 ***************************************************************************/ 00010 #include "textfilesensor.h" 00011 #include "tqdom.h" 00012 00013 TextFileSensor::TextFileSensor( const TQString &fn, bool iRdf, int interval, const TQString &encoding ) 00014 : Sensor( interval ) 00015 { 00016 fileName = fn; 00017 rdf = iRdf; 00018 00019 if( !encoding.isEmpty() ) 00020 { 00021 codec = TQTextCodec::codecForName( encoding.ascii() ); 00022 if ( codec == 0) 00023 codec = TQTextCodec::codecForLocale(); 00024 } 00025 else 00026 codec = TQTextCodec::codecForLocale(); 00027 } 00028 00029 TextFileSensor::~TextFileSensor() 00030 {} 00031 00032 void TextFileSensor::update() 00033 { 00034 TQValueVector<TQString> lines; 00035 TQFile file(fileName); 00036 TQString line; 00037 if ( file.open(IO_ReadOnly | IO_Translate) ) 00038 { 00039 if (rdf) 00040 { 00041 TQDomDocument doc; 00042 if ( !doc.setContent( &file ) ) 00043 { 00044 file.close(); 00045 return; 00046 } 00047 TQDomElement docElem = doc.documentElement(); 00048 TQDomNode n = docElem.firstChild(); 00049 if (!n.isNull()) 00050 { 00051 TQDomNodeList titles = docElem.elementsByTagName( "title" ); 00052 TQDomNodeList links = docElem.elementsByTagName( "link" ); 00053 00054 uint i; 00055 for ( i = 0; i < titles.count(); ++i ) 00056 { 00057 TQDomElement element = titles.item( i ).toElement(); 00058 lines.push_back(element.text()); 00059 00060 element = links.item( i ).toElement(); 00061 lines.push_back(element.text()); 00062 } 00063 } 00064 } 00065 else 00066 { 00067 TQTextStream t( &file ); // use a text stream 00068 while( (line = t.readLine()) !=0 ) 00069 { 00070 lines.push_back(line); 00071 } 00072 } 00073 file.close(); 00074 } 00075 00076 int lineNbr; 00077 SensorParams *sp; 00078 Meter *meter; 00079 00080 int count = (int) lines.size(); 00081 TQObjectListIt it( *objList ); 00082 while (it != 0) 00083 { 00084 sp = (SensorParams*)(*it); 00085 meter = sp->getMeter(); 00086 lineNbr = (sp->getParam("LINE")).toInt(); 00087 if ( lineNbr >= 1 && lineNbr <= (int) count ) 00088 { 00089 meter->setValue(lines[lineNbr-1]); 00090 } 00091 if ( -lineNbr >= 1 && -lineNbr <= (int) count ) 00092 { 00093 meter->setValue(lines[count+lineNbr]); 00094 } 00095 00096 if ( lineNbr == 0 ) 00097 { 00098 TQString text; 00099 for( int i=0; i < count; i++ ) 00100 { 00101 text += lines[i] + "\n"; 00102 } 00103 meter->setValue( text ); 00104 } 00105 ++it; 00106 } 00107 } 00108 00109 #include "textfilesensor.moc"