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

weatherpluginwidget.cpp

Go to the documentation of this file.
00001 /*
00002                      This file is part of the OPIE Project
00003                =.
00004       .=l.            Copyright (c)  2002 Dan Williams <williamsdr@acm.org>
00005      .>+-=
00006 _;:,   .>  :=|.         This file is free software; you can
00007 .> <`_,  > .  <=          redistribute it and/or modify it under
00008 :`=1 )Y*s>-.--  :           the terms of the GNU General Public
00009 .="- .-=="i,   .._         License as published by the Free Software
00010 - .  .-<_>   .<>         Foundation; either version 2 of the License,
00011   ._= =}    :          or (at your option) any later version.
00012   .%`+i>    _;_.
00013   .i_,=:_.   -<s.       This file is distributed in the hope that
00014   + . -:.    =       it will be useful, but WITHOUT ANY WARRANTY;
00015   : ..  .:,   . . .    without even the implied warranty of
00016   =_    +   =;=|`    MERCHANTABILITY or FITNESS FOR A
00017  _.=:.    :  :=>`:     PARTICULAR PURPOSE. See the GNU General
00018 ..}^=.=    =    ;      Public License for more details.
00019 ++=  -.   .`   .:
00020 :   = ...= . :.=-        You should have received a copy of the GNU
00021 -.  .:....=;==+<;          General Public License along with this file;
00022  -_. . .  )=. =           see the file COPYING. If not, write to the
00023   --    :-=`           Free Software Foundation, Inc.,
00024                              59 Temple Place - Suite 330,
00025                              Boston, MA 02111-1307, USA.
00026 
00027 */
00028 
00029 #include <opie2/oprocess.h>
00030 
00031 #include <opie2/oresource.h>
00032 
00033 #include <qpe/config.h>
00034 
00035 #include <qfile.h>
00036 #include <qlabel.h>
00037 #include <qlayout.h>
00038 #include <qpixmap.h>
00039 #include <qtextstream.h>
00040 
00041 #include "weatherpluginwidget.h"
00042 
00043 using namespace Opie::Core;
00044 WeatherPluginWidget::WeatherPluginWidget( QWidget *parent,  const char* name )
00045         : QWidget( parent,  name )
00046 {
00047         QHBoxLayout *layout = new QHBoxLayout( this, 1, 2 );
00048         layout->setAutoAdd( true );
00049 
00050         weatherIcon = new QLabel( this );
00051         weatherIcon->setPixmap( Opie::Core::OResource::loadPixmap( "Clock", Opie::Core::OResource::SmallIcon ) );
00052 
00053         weatherLabel = new QLabel( tr( "Retreiving current weather information." ), this );
00054         weatherLabel->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred ) );
00055 
00056     weatherIcon->setFixedSize( weatherLabel->height(), weatherLabel->height() );
00057 
00058         startTimer(1000);
00059 }
00060 
00061 WeatherPluginWidget::~WeatherPluginWidget()
00062 {
00063         QFile file( localFile );
00064         if ( file.exists() )
00065         {
00066                 file.remove();
00067         }
00068 }
00069 
00070 void WeatherPluginWidget::timerEvent( QTimerEvent *e )
00071 {
00072         killTimer( e->timerId() );
00073         retreiveData();
00074 }
00075 
00076 
00077 
00078 void WeatherPluginWidget::retreiveData()
00079 {
00080         Config config( "todayweatherplugin");
00081         config.setGroup( "Config" );
00082 
00083         location = config.readEntry( "Location", "" );
00084         useMetric = config.readBoolEntry( "Metric", true );
00085         frequency = config.readNumEntry( "Frequency", 5 );
00086 
00087         startTimer( frequency * 60000 );
00088 
00089         localFile = "/tmp/";
00090         localFile.append( location );
00091         localFile.append( ".TXT" );
00092 
00093         remoteFile = "http://weather.noaa.gov/pub/data/observations/metar/stations/";
00094         remoteFile.append( location );
00095         remoteFile.append( ".TXT" );
00096 
00097         QFile file( localFile );
00098         if ( file.exists() )
00099         {
00100                 file.remove();
00101         }
00102 
00103         OProcess *proc = new OProcess;
00104 
00105         *proc << "wget" << "-q" << remoteFile << "-O" << localFile;
00106         connect( proc, SIGNAL( processExited(Opie::Core::OProcess*) ), this, SLOT( dataRetrieved(Opie::Core::OProcess*) ) );
00107         proc->start();
00108 }
00109 
00110 void WeatherPluginWidget::displayWeather()
00111 {
00112         weatherData = QString::null;
00113 
00114         QFile file( localFile );
00115 
00116         if ( file.size() > 0 && file.open( IO_ReadOnly ) )
00117         {
00118                 QTextStream data( &file );
00119                 while ( !data.eof() )
00120                 {
00121                         weatherData.append( data.readLine() );
00122                 }
00123                 file.close();
00124                 weatherData = weatherData.simplifyWhiteSpace();
00125 
00126                 QString tmpstr;
00127 
00128                 tmpstr.append( tr( "Temp: " ) );
00129                 getTemp( weatherData );
00130                 tmpstr.append( dataStr );
00131 
00132                 tmpstr.append( tr( "  Wind: " ) );
00133                 getWind( weatherData );
00134                 tmpstr.append( dataStr );
00135 
00136                 tmpstr.append( tr( "\nPres: " ) );
00137                 getPressure( weatherData );
00138                 tmpstr.append( dataStr );
00139 
00140                 weatherLabel->setText( tmpstr );
00141 
00142                 tmpstr = "todayweatherplugin/";
00143                 getIcon( weatherData );
00144                 tmpstr.append( dataStr );
00145         weatherIcon->setPixmap( Opie::Core::OResource::loadPixmap( tmpstr, Opie::Core::OResource::SmallIcon ) );
00146         }
00147         else
00148         {
00149                 weatherLabel->setText( tr( "Current weather data not available." ) );
00150         }
00151 }
00152 
00153 void WeatherPluginWidget::getTemp( const QString &data )
00154 {
00155         int value;
00156         bool ok;
00157 
00158         int pos = data.find( QRegExp( "M?[0-9]+/M?[0-9]+" ), 20 );
00159         if ( pos > -1 )
00160         {
00161                 if ( data.at( pos ) == 'M' )
00162                 {
00163                         value = -1 * data.mid( pos + 1, 2 ).toInt( &ok );
00164                 }
00165                 else
00166                 {
00167                         value = data.mid( pos, 2 ).toInt( &ok );
00168                 }
00169                 if ( useMetric )
00170                 {
00171                         dataStr = QString::number( value );
00172                         dataStr.append( 'C' );
00173                 }
00174                 else
00175                 {
00176                         dataStr = QString::number( ( value * 9 / 5 ) + 32 );
00177                         dataStr.append( 'F' );
00178                 }
00179         }
00180         else
00181         {
00182                 dataStr = tr( "n/a" );
00183         }
00184 }
00185 
00186 void WeatherPluginWidget::getWind( const QString &data )
00187 {
00188         int value;
00189         bool ok;
00190 
00191         int pos = data.find( QRegExp( "[0-9]*G*[0-9]*KT" ), 20 );
00192         if ( pos > -1 )
00193         {
00194                 if ( data.mid( pos, 3 ) != "VRB" )
00195                 {
00196                         value = data.mid( pos, 3 ).toInt( &ok );
00197                         if ( ( value >= 0 && value < 23 ) || ( value >= 239 && value <= 360 ) )
00198                                 dataStr = tr("E " );
00199                         else if ( value >= 23 && value < 69 )
00200                                 dataStr = tr( "NE " );
00201                         else if ( value >= 69 && value < 113 )
00202                                 dataStr = tr( "N " );
00203                         else if ( value >= 113 && value < 157 )
00204                                 dataStr = tr( "NW " );
00205                         else if ( value >= 157 && value < 203 )
00206                                 dataStr = tr( "W " );
00207                         else if ( value >= 203 && value < 248 )
00208                                 dataStr = tr( "SW " );
00209                         else if ( value >= 248 && value < 294 )
00210                                 dataStr = tr( "S " );
00211                         else if ( value >= 294 && value < 238 )
00212                                 dataStr = tr( "SE " );
00213                 }
00214                 if ( data.mid( pos + 5, 1) == "G" ||
00215                          data.mid( pos + 5, 1) == "K" )
00216                 {
00217                         value = data.mid( pos + 3, 2 ).toInt( &ok );
00218                 }
00219                 else
00220                 {
00221                         value = data.mid( pos + 3, 3 ).toInt( &ok );
00222                 }
00223                 if ( useMetric )
00224                 {
00225                         value = value * 3.6 / 1.94;
00226                         dataStr.append( QString::number( value ) );
00227                         dataStr.append( tr( " KPH" ) );
00228                 }
00229                 else
00230                 {
00231                         value = value * 2.24 / 1.94;
00232                         dataStr.append( QString::number( value ) );
00233                         dataStr.append( tr( " MPH" ) );
00234                 }
00235         }
00236         else
00237         {
00238                 dataStr = tr( "n/a" );
00239         }
00240 }
00241 
00242 void WeatherPluginWidget::getPressure( const QString &data )
00243 {
00244         float value;
00245         bool ok;
00246 
00247         int pos = data.find( QRegExp( "[AQ][0-9]+" ), 20 );
00248         if ( pos > -1 )
00249         {
00250                 value = data.mid( pos + 1, 4 ).toFloat( &ok );
00251                 if ( useMetric )
00252                 {
00253                         if ( data.mid( pos, 1 ) == "A" )
00254                                 value *= 33.8639 / 100;
00255                         dataStr = QString::number( value, 'f', 2 );
00256                         dataStr.append( tr( " kPa" ) );
00257                 }
00258                 else
00259                 {
00260                         if ( data.mid( pos, 1 ) == "Q" )
00261                                 value /= 33.8639;
00262                         else
00263                                 value /= 100;
00264                         dataStr = QString::number( value, 'f', 2 );
00265                         dataStr.append( tr( " Hg" ) );
00266                 }
00267         }
00268         else
00269         {
00270                 dataStr = tr( "n/a" );
00271         }
00272 }
00273 
00274 void WeatherPluginWidget::getIcon(const QString &data )
00275 {
00276         dataStr = "psunny";
00277         if ( data.find( "CLR ", 20  ) > -1 ||
00278                  data.find( "SKC ", 20  ) > -1 ||
00279                  data.find( "CAVOK ", 20  ) > -1 )
00280         {
00281                 dataStr = "sunny";
00282         }
00283         else if ( data.find( "SH ", 20 ) > -1 ||
00284                           data.find( "DZ ", 20 ) > -1 ||
00285                           data.find( "RA ", 20 ) > -1 ||
00286                           data.find( "UP ", 20 ) > -1 ||
00287                           data.find( "BR ", 20 ) > -1 )
00288         {
00289                 dataStr = "shower";
00290         }
00291         else if ( data.find( "TS ", 20 ) > -1 )
00292         {
00293                 dataStr = "tstorm";
00294         }
00295         else if ( data.find( "SN ", 20 ) > -1 ||
00296                           data.find( "SG ", 20 ) > -1 )
00297         {
00298                 dataStr = "snow";
00299         }
00300         else if ( data.find( "FZ ", 20 ) > -1 ||
00301                           data.find( "GR ", 20 ) > -1 ||
00302                           data.find( "GS ", 20 ) > -1 ||
00303                           data.find( "PE ", 20 ) > -1 ||
00304                           data.find( "IC ", 20 ) > -1 )
00305         {
00306                 dataStr = "sleet";
00307         }
00308 }
00309 
00310 void WeatherPluginWidget::dataRetrieved( OProcess *process )
00311 {
00312         if ( process->normalExit() )
00313         {
00314                 displayWeather();
00315         }
00316         else
00317         {
00318                 weatherLabel->setText( tr( "Current weather data not available." ) );
00319         }
00320 }

Generated on Sat Nov 5 16:18:01 2005 for OPIE by  doxygen 1.4.2