-rw-r--r-- | noncore/todayplugins/weather/weatherpluginwidget.cpp | 65 | ||||
-rw-r--r-- | noncore/todayplugins/weather/weatherpluginwidget.h | 8 |
2 files changed, 39 insertions, 34 deletions
diff --git a/noncore/todayplugins/weather/weatherpluginwidget.cpp b/noncore/todayplugins/weather/weatherpluginwidget.cpp index 0fd8e57..6444ebf 100644 --- a/noncore/todayplugins/weather/weatherpluginwidget.cpp +++ b/noncore/todayplugins/weather/weatherpluginwidget.cpp @@ -4,139 +4,130 @@ .=l. Copyright (c) 2002 Dan Williams <williamsdr@acm.org> .>+-= _;:, .> :=|. This file is free software; you can .> <`_, > . <= redistribute it and/or modify it under :`=1 )Y*s>-.-- : the terms of the GNU General Public .="- .-=="i, .._ License as published by the Free Software - . .-<_> .<> Foundation; either version 2 of the License, ._= =} : or (at your option) any later version. .%`+i> _;_. .i_,=:_. -<s. This file is distributed in the hope that + . -:. = it will be useful, but WITHOUT ANY WARRANTY; : .. .:, . . . without even the implied warranty of =_ + =;=|` MERCHANTABILITY or FITNESS FOR A _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU General ..}^=.= = ; Public License for more details. ++= -. .` .: : = ...= . :.=- You should have received a copy of the GNU -. .:....=;==+<; General Public License along with this file; -_. . . )=. = see the file COPYING. If not, write to the -- :-=` Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -#include <stdio.h> #include <qfile.h> #include <qimage.h> #include <qlabel.h> #include <qlayout.h> #include <qpixmap.h> #include <qtextstream.h> +#include <opie/oprocess.h> + #include <qpe/config.h> #include <qpe/resource.h> #include "weatherpluginwidget.h" WeatherPluginWidget::WeatherPluginWidget( QWidget *parent, const char* name ) : QWidget( parent, name ) { - Config config( "todayweatherplugin"); - config.setGroup( "Config" ); - - location = config.readEntry( "Location", "" ); - useMetric = config.readBoolEntry( "Metric", TRUE ); - frequency = config.readNumEntry( "Frequency", 5 ); - - localFile = "/tmp/"; - localFile.append( location ); - localFile.append( ".TXT" ); - - remoteFile = "http://weather.noaa.gov/pub/data/observations/metar/stations/"; - remoteFile.append( location ); - remoteFile.append( ".TXT" ); - QHBoxLayout *layout = new QHBoxLayout( this ); layout->setAutoAdd( TRUE ); layout->setSpacing( 2 ); weatherIcon = new QLabel( this ); weatherIcon->setMaximumWidth( 32 ); QImage logo1 = Resource::loadImage( "todayweatherplugin/wait" ); QPixmap pic; pic.convertFromImage( logo1 ); weatherIcon->setPixmap( pic ); weatherLabel = new QLabel( tr( "Retreiving current weather information." ), this ); weatherLabel->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred ) ); startTimer(1000); - //retreiveData(); } WeatherPluginWidget::~WeatherPluginWidget() { QFile file( localFile ); if ( file.exists() ) { file.remove(); } } void WeatherPluginWidget::timerEvent( QTimerEvent *e ) { killTimer( e->timerId() ); retreiveData(); } void WeatherPluginWidget::retreiveData() { startTimer( frequency * 60000 ); + Config config( "todayweatherplugin"); + config.setGroup( "Config" ); + + location = config.readEntry( "Location", "" ); + useMetric = config.readBoolEntry( "Metric", TRUE ); + frequency = config.readNumEntry( "Frequency", 5 ); + + localFile = "/tmp/"; + localFile.append( location ); + localFile.append( ".TXT" ); + + remoteFile = "http://weather.noaa.gov/pub/data/observations/metar/stations/"; + remoteFile.append( location ); + remoteFile.append( ".TXT" ); + QFile file( localFile ); if ( file.exists() ) { file.remove(); } - QString command = "wget -q "; - command.append( remoteFile ); - command.append( " -O " ); - command.append( localFile ); - FILE *get = popen( command.latin1(), "r" ); - if ( get ) - { - pclose( get ); - displayWeather(); - } - else - { - weatherLabel->setText( tr( "Current weather data not available.\nTry looking out the window." ) ); - } + OProcess *proc = new OProcess; + + *proc << "wget" << "-q" << remoteFile << "-O" << localFile; + connect( proc, SIGNAL( processExited( OProcess * ) ), this, SLOT( dataRetrieved( OProcess * ) ) ); + proc->start(); } void WeatherPluginWidget::displayWeather() { weatherData = QString::null; QFile file( localFile ); if ( file.open( IO_ReadOnly ) ) { QTextStream data( &file ); while ( !data.eof() ) { weatherData.append( data.readLine() ); } file.close(); weatherData = weatherData.simplifyWhiteSpace(); QString tmpstr; tmpstr.append( tr( "Temp: " ) ); getTemp( weatherData ); tmpstr.append( dataStr ); tmpstr.append( tr( " Wind: " ) ); @@ -298,24 +289,36 @@ void WeatherPluginWidget::getIcon(const QString &data ) data.find( "RA ", 20 ) > -1 || data.find( "UP ", 20 ) > -1 || data.find( "BR ", 20 ) > -1 ) { dataStr = "shower"; } else if ( data.find( "TS ", 20 ) > -1 ) { dataStr = "tstorm"; } else if ( data.find( "SN ", 20 ) > -1 || data.find( "SG ", 20 ) > -1 ) { dataStr = "snow"; } else if ( data.find( "FZ ", 20 ) > -1 || data.find( "GR ", 20 ) > -1 || data.find( "GS ", 20 ) > -1 || data.find( "PE ", 20 ) > -1 || data.find( "IC ", 20 ) > -1 ) { dataStr = "sleet"; } } + +void WeatherPluginWidget::dataRetrieved( OProcess *process ) +{ + if ( process->normalExit() ) + { + displayWeather(); + } + else + { + weatherLabel->setText( tr( "Current weather data not available.\nTry looking out the window." ) ); + } +} diff --git a/noncore/todayplugins/weather/weatherpluginwidget.h b/noncore/todayplugins/weather/weatherpluginwidget.h index 2c0238d..d2bbd8b 100644 --- a/noncore/todayplugins/weather/weatherpluginwidget.h +++ b/noncore/todayplugins/weather/weatherpluginwidget.h @@ -11,59 +11,61 @@ ._= =} : or (at your option) any later version. .%`+i> _;_. .i_,=:_. -<s. This file is distributed in the hope that + . -:. = it will be useful, but WITHOUT ANY WARRANTY; : .. .:, . . . without even the implied warranty of =_ + =;=|` MERCHANTABILITY or FITNESS FOR A _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU General ..}^=.= = ; Public License for more details. ++= -. .` .: : = ...= . :.=- You should have received a copy of the GNU -. .:....=;==+<; General Public License along with this file; -_. . . )=. = see the file COPYING. If not, write to the -- :-=` Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #ifndef WEATHER_PLUGIN_WIDGET_H #define WEATHER_PLUGIN_WIDGET_H #include <qstring.h> #include <qwidget.h> -#include <unistd.h> - +class OProcess; class QLabel; class QTimer; class WeatherPluginWidget : public QWidget { Q_OBJECT public: WeatherPluginWidget( QWidget *parent, const char *name ); ~WeatherPluginWidget(); private: QString location; QString remoteFile; QString localFile; QString weatherData; QString dataStr; bool useMetric; int frequency; QLabel *weatherLabel; QLabel *weatherIcon; - + void timerEvent( QTimerEvent * ); void retreiveData(); void displayWeather(); void getTemp( const QString & ); void getWind( const QString & ); void getPressure( const QString & ); void getIcon( const QString & ); + + private slots: + void dataRetrieved( OProcess * ); }; #endif |