rsssensor.cpp
00001 /*************************************************************************** 00002 * Copyright (C) 2003 by Ralph M. Churchill * 00003 * mrchucho@yahoo.com * 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 00011 #include "karambaapp.h" 00012 #include "rsssensor.h" 00013 #include <tqdom.h> 00014 #include <tqregexp.h> 00015 #include <kurl.h> 00016 #include <kio/netaccess.h> 00017 00018 RssSensor::RssSensor( const TQString &src, int interval, const TQString &form, const TQString &enc) 00019 : Sensor(interval), 00020 source(src), 00021 format(form), 00022 encoding(enc) 00023 00024 { 00025 // Format: 00026 // %t = title (DEFAULT) 00027 // %d = desc 00028 00029 if( !encoding.isEmpty() ) 00030 { 00031 codec = TQTextCodec::codecForName( encoding.ascii() ); 00032 if ( codec == 0) 00033 codec = TQTextCodec::codecForLocale(); 00034 } 00035 else 00036 codec = TQTextCodec::codecForLocale(); 00037 } 00038 00039 RssSensor::~RssSensor() 00040 { 00041 } 00042 00043 void RssSensor::update() 00044 { 00045 TQDomDocument doc; 00046 TQFile file; 00047 TQString tmpFile; 00048 bool OK = false; 00049 00050 #if defined(KDE_3_3) 00051 if(KIO::NetAccess::download(KURL(source), tmpFile, karambaApp->parentWindow())) 00052 #else 00053 if(KIO::NetAccess::download(KURL(source), tmpFile)) 00054 #endif 00055 { 00056 file.setName(tmpFile); 00057 if ( file.open(IO_ReadOnly | IO_Translate) ) 00058 { 00059 if ( doc.setContent( &file ) ) 00060 { 00061 OK = true; 00062 } 00063 else 00064 { 00065 qDebug("Error on building DOM"); 00066 } 00067 } 00068 else 00069 { 00070 qDebug("Error opening file"); 00071 } 00072 } 00073 else { 00074 qDebug( "Error Downloading: %s", source.ascii()); 00075 } 00076 00077 if ( OK ) 00078 { 00079 SensorParams *sp; 00080 Meter *meter; 00081 00082 TQObjectListIt it( *objList ); 00083 while (it != 0) 00084 { 00085 sp = (SensorParams*)(*it); 00086 meter = sp->getMeter(); 00087 00088 // this is a hack to force the 00089 // clickmap to reset its data lists 00090 meter->setValue(0); 00091 00092 TQDomElement docElem = doc.documentElement(); 00093 TQDomNode n = docElem.firstChild(); 00094 if (!n.isNull()) 00095 { 00096 TQDomNodeList links = docElem.elementsByTagName( "link" ); 00097 TQDomNodeList displays; 00098 if ( format.contains( "%d", false ) > 0 ) 00099 { 00100 displays = docElem.elementsByTagName( "description" ); 00101 } 00102 else 00103 { 00104 displays = docElem.elementsByTagName( "title" ); 00105 } 00106 00107 TQRegExp rx("^http://", false ); 00108 for (uint i=1; i < displays.count(); ++i ) 00109 { 00110 TQString dispTxt = displays.item( i ).toElement().text(); 00111 TQString linkTxt = links.item( i ).toElement().text(); 00112 if( (rx.search(dispTxt) == -1) && (rx.search(linkTxt) != -1) ) 00113 { 00114 meter->setValue( dispTxt ); 00115 meter->setValue( linkTxt ); 00116 } 00117 else 00118 { 00119 qDebug("Skipping"); 00120 } 00121 } 00122 } 00123 else 00124 { 00125 qDebug ("Document Node was null!!"); 00126 } 00127 00128 ++it; 00129 } 00130 } 00131 // Cleanup 00132 file.close(); 00133 KIO::NetAccess::removeTempFile( tmpFile ); 00134 } 00135 00136 #include "rsssensor.moc"