Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

textfilesensor.cpp

Go to the documentation of this file.
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 "qdom.h"
00012 
00013 TextFileSensor::TextFileSensor( QString fn, bool iRdf, int interval, QString encoding )
00014         : Sensor( interval )
00015 {
00016     fileName = fn;
00017     rdf = iRdf;
00018 
00019     if( encoding != "" )
00020     {
00021         codec = QTextCodec::codecForName( encoding );
00022         if ( codec == 0)
00023             codec = QTextCodec::codecForLocale();
00024     }
00025     else
00026         codec = QTextCodec::codecForLocale();
00027 }
00028 
00029 TextFileSensor::~TextFileSensor()
00030 {}
00031 
00032 void TextFileSensor::update()
00033 {
00034     QValueVector<QString> lines;
00035     QFile file(fileName);
00036     QString line;
00037     if ( file.open(IO_ReadOnly | IO_Translate) )
00038     {
00039         if (rdf)
00040         {
00041             QDomDocument doc;
00042             if ( !doc.setContent( &file ) )
00043             {
00044                 file.close();
00045                 return;
00046             }
00047             QDomElement docElem = doc.documentElement();
00048             QDomNode n = docElem.firstChild();
00049             if (!n.isNull())
00050             {
00051                 QDomNodeList titles = docElem.elementsByTagName( "title" );
00052                 QDomNodeList links = docElem.elementsByTagName( "link" );
00053 
00054                 uint i;
00055                 for ( i = 0; i < titles.count(); ++i )
00056                 {
00057                     QDomElement 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             QTextStream 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     QObjectListIt 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             QString 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 }

Generated on Mon May 16 13:59:20 2005 for karamba by  doxygen 1.3.9.1