summaryrefslogtreecommitdiff
authordrw <drw>2002-11-22 01:48:26 (UTC)
committer drw <drw>2002-11-22 01:48:26 (UTC)
commit772c1321e4e95a415b9de700c3474f81f98b5482 (patch) (unidiff)
treee220beb65ea90ae7aee52c0ac83ecbc69bd8191a
parent87240f18c4e7b12c56d7f61a3431221d94f59f4c (diff)
downloadopie-772c1321e4e95a415b9de700c3474f81f98b5482.zip
opie-772c1321e4e95a415b9de700c3474f81f98b5482.tar.gz
opie-772c1321e4e95a415b9de700c3474f81f98b5482.tar.bz2
Make sure update frequency is initialized 1st time in (thanks to Simon)
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/todayplugins/weather/weatherpluginwidget.cpp4
1 files changed, 2 insertions, 2 deletions
diff --git a/noncore/todayplugins/weather/weatherpluginwidget.cpp b/noncore/todayplugins/weather/weatherpluginwidget.cpp
index 6444ebf..f4ea0f2 100644
--- a/noncore/todayplugins/weather/weatherpluginwidget.cpp
+++ b/noncore/todayplugins/weather/weatherpluginwidget.cpp
@@ -1,324 +1,324 @@
1/* 1/*
2                This file is part of the OPIE Project 2                This file is part of the OPIE Project
3 =. 3 =.
4             .=l. Copyright (c) 2002 Dan Williams <williamsdr@acm.org> 4             .=l. Copyright (c) 2002 Dan Williams <williamsdr@acm.org>
5           .>+-= 5           .>+-=
6 _;:,     .>    :=|. This file is free software; you can 6 _;:,     .>    :=|. This file is free software; you can
7.> <`_,   >  .   <= redistribute it and/or modify it under 7.> <`_,   >  .   <= redistribute it and/or modify it under
8:`=1 )Y*s>-.--   : the terms of the GNU General Public 8:`=1 )Y*s>-.--   : the terms of the GNU General Public
9.="- .-=="i,     .._ License as published by the Free Software 9.="- .-=="i,     .._ License as published by the Free Software
10 - .   .-<_>     .<> Foundation; either version 2 of the License, 10 - .   .-<_>     .<> Foundation; either version 2 of the License,
11     ._= =}       : or (at your option) any later version. 11     ._= =}       : or (at your option) any later version.
12    .%`+i>       _;_. 12    .%`+i>       _;_.
13    .i_,=:_.      -<s. This file is distributed in the hope that 13    .i_,=:_.      -<s. This file is distributed in the hope that
14     +  .  -:.       = it will be useful, but WITHOUT ANY WARRANTY; 14     +  .  -:.       = it will be useful, but WITHOUT ANY WARRANTY;
15    : ..    .:,     . . . without even the implied warranty of 15    : ..    .:,     . . . without even the implied warranty of
16    =_        +     =;=|` MERCHANTABILITY or FITNESS FOR A 16    =_        +     =;=|` MERCHANTABILITY or FITNESS FOR A
17  _.=:.       :    :=>`: PARTICULAR PURPOSE. See the GNU General 17  _.=:.       :    :=>`: PARTICULAR PURPOSE. See the GNU General
18..}^=.=       =       ; Public License for more details. 18..}^=.=       =       ; Public License for more details.
19++=   -.     .`     .: 19++=   -.     .`     .:
20 :     =  ...= . :.=- You should have received a copy of the GNU 20 :     =  ...= . :.=- You should have received a copy of the GNU
21 -.   .:....=;==+<; General Public License along with this file; 21 -.   .:....=;==+<; General Public License along with this file;
22  -_. . .   )=.  = see the file COPYING. If not, write to the 22  -_. . .   )=.  = see the file COPYING. If not, write to the
23    --        :-=` Free Software Foundation, Inc., 23    --        :-=` Free Software Foundation, Inc.,
24 59 Temple Place - Suite 330, 24 59 Temple Place - Suite 330,
25 Boston, MA 02111-1307, USA. 25 Boston, MA 02111-1307, USA.
26 26
27*/ 27*/
28 28
29#include <qfile.h> 29#include <qfile.h>
30#include <qimage.h> 30#include <qimage.h>
31#include <qlabel.h> 31#include <qlabel.h>
32#include <qlayout.h> 32#include <qlayout.h>
33#include <qpixmap.h> 33#include <qpixmap.h>
34#include <qtextstream.h> 34#include <qtextstream.h>
35 35
36#include <opie/oprocess.h> 36#include <opie/oprocess.h>
37 37
38#include <qpe/config.h> 38#include <qpe/config.h>
39#include <qpe/resource.h> 39#include <qpe/resource.h>
40 40
41#include "weatherpluginwidget.h" 41#include "weatherpluginwidget.h"
42 42
43WeatherPluginWidget::WeatherPluginWidget( QWidget *parent, const char* name ) 43WeatherPluginWidget::WeatherPluginWidget( QWidget *parent, const char* name )
44 : QWidget( parent, name ) 44 : QWidget( parent, name )
45{ 45{
46 QHBoxLayout *layout = new QHBoxLayout( this ); 46 QHBoxLayout *layout = new QHBoxLayout( this );
47 layout->setAutoAdd( TRUE ); 47 layout->setAutoAdd( TRUE );
48 layout->setSpacing( 2 ); 48 layout->setSpacing( 2 );
49 49
50 weatherIcon = new QLabel( this ); 50 weatherIcon = new QLabel( this );
51 weatherIcon->setMaximumWidth( 32 ); 51 weatherIcon->setMaximumWidth( 32 );
52 QImage logo1 = Resource::loadImage( "todayweatherplugin/wait" ); 52 QImage logo1 = Resource::loadImage( "todayweatherplugin/wait" );
53 QPixmap pic; 53 QPixmap pic;
54 pic.convertFromImage( logo1 ); 54 pic.convertFromImage( logo1 );
55 weatherIcon->setPixmap( pic ); 55 weatherIcon->setPixmap( pic );
56 56
57 weatherLabel = new QLabel( tr( "Retreiving current weather information." ), this ); 57 weatherLabel = new QLabel( tr( "Retreiving current weather information." ), this );
58 weatherLabel->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred ) ); 58 weatherLabel->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred ) );
59 59
60 startTimer(1000); 60 startTimer(1000);
61} 61}
62 62
63WeatherPluginWidget::~WeatherPluginWidget() 63WeatherPluginWidget::~WeatherPluginWidget()
64{ 64{
65 QFile file( localFile ); 65 QFile file( localFile );
66 if ( file.exists() ) 66 if ( file.exists() )
67 { 67 {
68 file.remove(); 68 file.remove();
69 } 69 }
70} 70}
71 71
72void WeatherPluginWidget::timerEvent( QTimerEvent *e ) 72void WeatherPluginWidget::timerEvent( QTimerEvent *e )
73{ 73{
74 killTimer( e->timerId() ); 74 killTimer( e->timerId() );
75 retreiveData(); 75 retreiveData();
76} 76}
77 77
78 78
79 79
80void WeatherPluginWidget::retreiveData() 80void WeatherPluginWidget::retreiveData()
81{ 81{
82 startTimer( frequency * 60000 );
83
84 Config config( "todayweatherplugin"); 82 Config config( "todayweatherplugin");
85 config.setGroup( "Config" ); 83 config.setGroup( "Config" );
86 84
87 location = config.readEntry( "Location", "" ); 85 location = config.readEntry( "Location", "" );
88 useMetric = config.readBoolEntry( "Metric", TRUE ); 86 useMetric = config.readBoolEntry( "Metric", TRUE );
89 frequency = config.readNumEntry( "Frequency", 5 ); 87 frequency = config.readNumEntry( "Frequency", 5 );
88
89 startTimer( frequency * 60000 );
90 90
91 localFile = "/tmp/"; 91 localFile = "/tmp/";
92 localFile.append( location ); 92 localFile.append( location );
93 localFile.append( ".TXT" ); 93 localFile.append( ".TXT" );
94 94
95 remoteFile = "http://weather.noaa.gov/pub/data/observations/metar/stations/"; 95 remoteFile = "http://weather.noaa.gov/pub/data/observations/metar/stations/";
96 remoteFile.append( location ); 96 remoteFile.append( location );
97 remoteFile.append( ".TXT" ); 97 remoteFile.append( ".TXT" );
98 98
99 QFile file( localFile ); 99 QFile file( localFile );
100 if ( file.exists() ) 100 if ( file.exists() )
101 { 101 {
102 file.remove(); 102 file.remove();
103 } 103 }
104 104
105 OProcess *proc = new OProcess; 105 OProcess *proc = new OProcess;
106 106
107 *proc << "wget" << "-q" << remoteFile << "-O" << localFile; 107 *proc << "wget" << "-q" << remoteFile << "-O" << localFile;
108 connect( proc, SIGNAL( processExited( OProcess * ) ), this, SLOT( dataRetrieved( OProcess * ) ) ); 108 connect( proc, SIGNAL( processExited( OProcess * ) ), this, SLOT( dataRetrieved( OProcess * ) ) );
109 proc->start(); 109 proc->start();
110} 110}
111 111
112void WeatherPluginWidget::displayWeather() 112void WeatherPluginWidget::displayWeather()
113{ 113{
114 weatherData = QString::null; 114 weatherData = QString::null;
115 115
116 QFile file( localFile ); 116 QFile file( localFile );
117 if ( file.open( IO_ReadOnly ) ) 117 if ( file.open( IO_ReadOnly ) )
118 { 118 {
119 QTextStream data( &file ); 119 QTextStream data( &file );
120 while ( !data.eof() ) 120 while ( !data.eof() )
121 { 121 {
122 weatherData.append( data.readLine() ); 122 weatherData.append( data.readLine() );
123 } 123 }
124 file.close(); 124 file.close();
125 weatherData = weatherData.simplifyWhiteSpace(); 125 weatherData = weatherData.simplifyWhiteSpace();
126 126
127 QString tmpstr; 127 QString tmpstr;
128 128
129 tmpstr.append( tr( "Temp: " ) ); 129 tmpstr.append( tr( "Temp: " ) );
130 getTemp( weatherData ); 130 getTemp( weatherData );
131 tmpstr.append( dataStr ); 131 tmpstr.append( dataStr );
132 132
133 tmpstr.append( tr( " Wind: " ) ); 133 tmpstr.append( tr( " Wind: " ) );
134 getWind( weatherData ); 134 getWind( weatherData );
135 tmpstr.append( dataStr ); 135 tmpstr.append( dataStr );
136 136
137 tmpstr.append( tr( "\nPres: " ) ); 137 tmpstr.append( tr( "\nPres: " ) );
138 getPressure( weatherData ); 138 getPressure( weatherData );
139 tmpstr.append( dataStr ); 139 tmpstr.append( dataStr );
140 140
141 weatherLabel->setText( tmpstr ); 141 weatherLabel->setText( tmpstr );
142 142
143 tmpstr = "todayweatherplugin/"; 143 tmpstr = "todayweatherplugin/";
144 getIcon( weatherData ); 144 getIcon( weatherData );
145 tmpstr.append( dataStr ); 145 tmpstr.append( dataStr );
146 QImage logo1 = Resource::loadImage( tmpstr ); 146 QImage logo1 = Resource::loadImage( tmpstr );
147 QPixmap pic; 147 QPixmap pic;
148 pic.convertFromImage( logo1 ); 148 pic.convertFromImage( logo1 );
149 weatherIcon->setPixmap( pic ); 149 weatherIcon->setPixmap( pic );
150 } 150 }
151 else 151 else
152 { 152 {
153 weatherLabel->setText( tr( "Current weather data not available.\nTry looking out the window." ) ); 153 weatherLabel->setText( tr( "Current weather data not available.\nTry looking out the window." ) );
154 } 154 }
155} 155}
156 156
157void WeatherPluginWidget::getTemp( const QString &data ) 157void WeatherPluginWidget::getTemp( const QString &data )
158{ 158{
159 int value; 159 int value;
160 bool ok; 160 bool ok;
161 161
162 int pos = data.find( QRegExp( "M?[0-9]+/M?[0-9]+" ), 20 ); 162 int pos = data.find( QRegExp( "M?[0-9]+/M?[0-9]+" ), 20 );
163 if ( pos > -1 ) 163 if ( pos > -1 )
164 { 164 {
165 if ( data.at( pos ) == 'M' ) 165 if ( data.at( pos ) == 'M' )
166 { 166 {
167 value = -1 * data.mid( pos + 1, 2 ).toInt( &ok ); 167 value = -1 * data.mid( pos + 1, 2 ).toInt( &ok );
168 } 168 }
169 else 169 else
170 { 170 {
171 value = data.mid( pos, 2 ).toInt( &ok ); 171 value = data.mid( pos, 2 ).toInt( &ok );
172 } 172 }
173 if ( useMetric ) 173 if ( useMetric )
174 { 174 {
175 dataStr = QString::number( value ); 175 dataStr = QString::number( value );
176 dataStr.append( 'C' ); 176 dataStr.append( 'C' );
177 } 177 }
178 else 178 else
179 { 179 {
180 dataStr = QString::number( ( value * 9 / 5 ) + 32 ); 180 dataStr = QString::number( ( value * 9 / 5 ) + 32 );
181 dataStr.append( 'F' ); 181 dataStr.append( 'F' );
182 } 182 }
183 } 183 }
184 else 184 else
185 { 185 {
186 dataStr = tr( "n/a" ); 186 dataStr = tr( "n/a" );
187 } 187 }
188} 188}
189 189
190void WeatherPluginWidget::getWind( const QString &data ) 190void WeatherPluginWidget::getWind( const QString &data )
191{ 191{
192 int value; 192 int value;
193 bool ok; 193 bool ok;
194 194
195 int pos = data.find( QRegExp( "[0-9]*G*[0-9]*KT" ), 20 ); 195 int pos = data.find( QRegExp( "[0-9]*G*[0-9]*KT" ), 20 );
196 if ( pos > -1 ) 196 if ( pos > -1 )
197 { 197 {
198 if ( data.mid( pos, 3 ) != "VRB" ) 198 if ( data.mid( pos, 3 ) != "VRB" )
199 { 199 {
200 value = data.mid( pos, 3 ).toInt( &ok ); 200 value = data.mid( pos, 3 ).toInt( &ok );
201 if ( ( value >= 0 && value < 23 ) || ( value >= 239 && value <= 360 ) ) 201 if ( ( value >= 0 && value < 23 ) || ( value >= 239 && value <= 360 ) )
202 dataStr = tr("E " ); 202 dataStr = tr("E " );
203 else if ( value >= 23 && value < 69 ) 203 else if ( value >= 23 && value < 69 )
204 dataStr = tr( "NE " ); 204 dataStr = tr( "NE " );
205 else if ( value >= 69 && value < 113 ) 205 else if ( value >= 69 && value < 113 )
206 dataStr = tr( "N " ); 206 dataStr = tr( "N " );
207 else if ( value >= 113 && value < 157 ) 207 else if ( value >= 113 && value < 157 )
208 dataStr = tr( "NW " ); 208 dataStr = tr( "NW " );
209 else if ( value >= 157 && value < 203 ) 209 else if ( value >= 157 && value < 203 )
210 dataStr = tr( "W " ); 210 dataStr = tr( "W " );
211 else if ( value >= 203 && value < 248 ) 211 else if ( value >= 203 && value < 248 )
212 dataStr = tr( "SW " ); 212 dataStr = tr( "SW " );
213 else if ( value >= 248 && value < 294 ) 213 else if ( value >= 248 && value < 294 )
214 dataStr = tr( "S " ); 214 dataStr = tr( "S " );
215 else if ( value >= 294 && value < 238 ) 215 else if ( value >= 294 && value < 238 )
216 dataStr = tr( "SE " ); 216 dataStr = tr( "SE " );
217 } 217 }
218 if ( data.mid( pos + 5, 1) == "G" || 218 if ( data.mid( pos + 5, 1) == "G" ||
219 data.mid( pos + 5, 1) == "K" ) 219 data.mid( pos + 5, 1) == "K" )
220 { 220 {
221 value = data.mid( pos + 3, 2 ).toInt( &ok ); 221 value = data.mid( pos + 3, 2 ).toInt( &ok );
222 } 222 }
223 else 223 else
224 { 224 {
225 value = data.mid( pos + 3, 3 ).toInt( &ok ); 225 value = data.mid( pos + 3, 3 ).toInt( &ok );
226 } 226 }
227 if ( useMetric ) 227 if ( useMetric )
228 { 228 {
229 value = value * 3.6 / 1.94; 229 value = value * 3.6 / 1.94;
230 dataStr.append( QString::number( value ) ); 230 dataStr.append( QString::number( value ) );
231 dataStr.append( tr( " KPH" ) ); 231 dataStr.append( tr( " KPH" ) );
232 } 232 }
233 else 233 else
234 { 234 {
235 value = value * 2.24 / 1.94; 235 value = value * 2.24 / 1.94;
236 dataStr.append( QString::number( value ) ); 236 dataStr.append( QString::number( value ) );
237 dataStr.append( tr( " MPH" ) ); 237 dataStr.append( tr( " MPH" ) );
238 } 238 }
239 } 239 }
240 else 240 else
241 { 241 {
242 dataStr = tr( "n/a" ); 242 dataStr = tr( "n/a" );
243 } 243 }
244} 244}
245 245
246void WeatherPluginWidget::getPressure( const QString &data ) 246void WeatherPluginWidget::getPressure( const QString &data )
247{ 247{
248 float value; 248 float value;
249 bool ok; 249 bool ok;
250 250
251 int pos = data.find( QRegExp( "[AQ][0-9]+" ), 20 ); 251 int pos = data.find( QRegExp( "[AQ][0-9]+" ), 20 );
252 if ( pos > -1 ) 252 if ( pos > -1 )
253 { 253 {
254 value = data.mid( pos + 1, 4 ).toFloat( &ok ); 254 value = data.mid( pos + 1, 4 ).toFloat( &ok );
255 if ( useMetric ) 255 if ( useMetric )
256 { 256 {
257 if ( data.mid( pos, 1 ) == "A" ) 257 if ( data.mid( pos, 1 ) == "A" )
258 value *= 33.8639 / 100; 258 value *= 33.8639 / 100;
259 dataStr = QString::number( value, 'f', 2 ); 259 dataStr = QString::number( value, 'f', 2 );
260 dataStr.append( tr( " hPa" ) ); 260 dataStr.append( tr( " hPa" ) );
261 } 261 }
262 else 262 else
263 { 263 {
264 if ( data.mid( pos, 1 ) == "Q" ) 264 if ( data.mid( pos, 1 ) == "Q" )
265 value /= 33.8639; 265 value /= 33.8639;
266 else 266 else
267 value /= 100; 267 value /= 100;
268 dataStr = QString::number( value, 'f', 2 ); 268 dataStr = QString::number( value, 'f', 2 );
269 dataStr.append( tr( " Hg" ) ); 269 dataStr.append( tr( " Hg" ) );
270 } 270 }
271 } 271 }
272 else 272 else
273 { 273 {
274 dataStr = tr( "n/a" ); 274 dataStr = tr( "n/a" );
275 } 275 }
276} 276}
277 277
278void WeatherPluginWidget::getIcon(const QString &data ) 278void WeatherPluginWidget::getIcon(const QString &data )
279{ 279{
280 dataStr = "psunny"; 280 dataStr = "psunny";
281 if ( data.find( "CLR ", 20 ) > -1 || 281 if ( data.find( "CLR ", 20 ) > -1 ||
282 data.find( "SKC ", 20 ) > -1 || 282 data.find( "SKC ", 20 ) > -1 ||
283 data.find( "CAVOK ", 20 ) > -1 ) 283 data.find( "CAVOK ", 20 ) > -1 )
284 { 284 {
285 dataStr = "sunny"; 285 dataStr = "sunny";
286 } 286 }
287 else if ( data.find( "SH ", 20 ) > -1 || 287 else if ( data.find( "SH ", 20 ) > -1 ||
288 data.find( "DZ ", 20 ) > -1 || 288 data.find( "DZ ", 20 ) > -1 ||
289 data.find( "RA ", 20 ) > -1 || 289 data.find( "RA ", 20 ) > -1 ||
290 data.find( "UP ", 20 ) > -1 || 290 data.find( "UP ", 20 ) > -1 ||
291 data.find( "BR ", 20 ) > -1 ) 291 data.find( "BR ", 20 ) > -1 )
292 { 292 {
293 dataStr = "shower"; 293 dataStr = "shower";
294 } 294 }
295 else if ( data.find( "TS ", 20 ) > -1 ) 295 else if ( data.find( "TS ", 20 ) > -1 )
296 { 296 {
297 dataStr = "tstorm"; 297 dataStr = "tstorm";
298 } 298 }
299 else if ( data.find( "SN ", 20 ) > -1 || 299 else if ( data.find( "SN ", 20 ) > -1 ||
300 data.find( "SG ", 20 ) > -1 ) 300 data.find( "SG ", 20 ) > -1 )
301 { 301 {
302 dataStr = "snow"; 302 dataStr = "snow";
303 } 303 }
304 else if ( data.find( "FZ ", 20 ) > -1 || 304 else if ( data.find( "FZ ", 20 ) > -1 ||
305 data.find( "GR ", 20 ) > -1 || 305 data.find( "GR ", 20 ) > -1 ||
306 data.find( "GS ", 20 ) > -1 || 306 data.find( "GS ", 20 ) > -1 ||
307 data.find( "PE ", 20 ) > -1 || 307 data.find( "PE ", 20 ) > -1 ||
308 data.find( "IC ", 20 ) > -1 ) 308 data.find( "IC ", 20 ) > -1 )
309 { 309 {
310 dataStr = "sleet"; 310 dataStr = "sleet";
311 } 311 }
312} 312}
313 313
314void WeatherPluginWidget::dataRetrieved( OProcess *process ) 314void WeatherPluginWidget::dataRetrieved( OProcess *process )
315{ 315{
316 if ( process->normalExit() ) 316 if ( process->normalExit() )
317 { 317 {
318 displayWeather(); 318 displayWeather();
319 } 319 }
320 else 320 else
321 { 321 {
322 weatherLabel->setText( tr( "Current weather data not available.\nTry looking out the window." ) ); 322 weatherLabel->setText( tr( "Current weather data not available.\nTry looking out the window." ) );
323 } 323 }
324} 324}