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