summaryrefslogtreecommitdiff
Unidiff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/todayplugins/weather/weatherpluginwidget.cpp75
1 files changed, 37 insertions, 38 deletions
diff --git a/noncore/todayplugins/weather/weatherpluginwidget.cpp b/noncore/todayplugins/weather/weatherpluginwidget.cpp
index 8e00c36..8c41189 100644
--- a/noncore/todayplugins/weather/weatherpluginwidget.cpp
+++ b/noncore/todayplugins/weather/weatherpluginwidget.cpp
@@ -1,279 +1,278 @@
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 <opie2/oprocess.h>
30
31#include <qpe/applnk.h>
32#include <qpe/config.h>
33#include <qpe/resource.h>
34
29#include <qfile.h> 35#include <qfile.h>
30#include <qimage.h> 36#include <qimage.h>
31#include <qlabel.h> 37#include <qlabel.h>
32#include <qlayout.h> 38#include <qlayout.h>
33#include <qpixmap.h> 39#include <qpixmap.h>
34#include <qtextstream.h> 40#include <qtextstream.h>
35 41
36#include <opie2/oprocess.h>
37
38#include <qpe/config.h>
39#include <qpe/resource.h>
40
41#include "weatherpluginwidget.h" 42#include "weatherpluginwidget.h"
42 43
43using namespace Opie::Core; 44using namespace Opie::Core;
44WeatherPluginWidget::WeatherPluginWidget( QWidget *parent, const char* name ) 45WeatherPluginWidget::WeatherPluginWidget( QWidget *parent, const char* name )
45 : QWidget( parent, name ) 46 : QWidget( parent, name )
46{ 47{
47 QHBoxLayout *layout = new QHBoxLayout( this ); 48 QHBoxLayout *layout = new QHBoxLayout( this, 1, 2 );
48 layout->setAutoAdd( TRUE ); 49 layout->setAutoAdd( true );
49 layout->setSpacing( 2 );
50 50
51 weatherIcon = new QLabel( this ); 51 weatherIcon = new QLabel( this );
52 weatherIcon->setMaximumWidth( 32 ); 52 QPixmap pic;
53 QImage logo1 = Resource::loadImage( "Clock" ); 53 pic.convertFromImage( Resource::loadImage( "Clock" ).smoothScale( AppLnk::smallIconSize(), AppLnk::smallIconSize() ) );
54 QPixmap pic;
55 pic.convertFromImage( logo1 );
56 weatherIcon->setPixmap( pic ); 54 weatherIcon->setPixmap( pic );
57 55
58 weatherLabel = new QLabel( tr( "Retreiving current weather information." ), this ); 56 weatherLabel = new QLabel( tr( "Retreiving current weather information." ), this );
59 weatherLabel->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred ) ); 57 weatherLabel->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred ) );
60 58
59 weatherIcon->setFixedSize( weatherLabel->height(), weatherLabel->height() );
60
61 startTimer(1000); 61 startTimer(1000);
62} 62}
63 63
64WeatherPluginWidget::~WeatherPluginWidget() 64WeatherPluginWidget::~WeatherPluginWidget()
65{ 65{
66 QFile file( localFile ); 66 QFile file( localFile );
67 if ( file.exists() ) 67 if ( file.exists() )
68 { 68 {
69 file.remove(); 69 file.remove();
70 } 70 }
71} 71}
72 72
73void WeatherPluginWidget::timerEvent( QTimerEvent *e ) 73void WeatherPluginWidget::timerEvent( QTimerEvent *e )
74{ 74{
75 killTimer( e->timerId() ); 75 killTimer( e->timerId() );
76 retreiveData(); 76 retreiveData();
77} 77}
78 78
79 79
80 80
81void WeatherPluginWidget::retreiveData() 81void WeatherPluginWidget::retreiveData()
82{ 82{
83 Config config( "todayweatherplugin"); 83 Config config( "todayweatherplugin");
84 config.setGroup( "Config" ); 84 config.setGroup( "Config" );
85 85
86 location = config.readEntry( "Location", "" ); 86 location = config.readEntry( "Location", "" );
87 useMetric = config.readBoolEntry( "Metric", TRUE ); 87 useMetric = config.readBoolEntry( "Metric", true );
88 frequency = config.readNumEntry( "Frequency", 5 ); 88 frequency = config.readNumEntry( "Frequency", 5 );
89 89
90 startTimer( frequency * 60000 ); 90 startTimer( frequency * 60000 );
91 91
92 localFile = "/tmp/"; 92 localFile = "/tmp/";
93 localFile.append( location ); 93 localFile.append( location );
94 localFile.append( ".TXT" ); 94 localFile.append( ".TXT" );
95 95
96 remoteFile = "http://weather.noaa.gov/pub/data/observations/metar/stations/"; 96 remoteFile = "http://weather.noaa.gov/pub/data/observations/metar/stations/";
97 remoteFile.append( location ); 97 remoteFile.append( location );
98 remoteFile.append( ".TXT" ); 98 remoteFile.append( ".TXT" );
99 99
100 QFile file( localFile ); 100 QFile file( localFile );
101 if ( file.exists() ) 101 if ( file.exists() )
102 { 102 {
103 file.remove(); 103 file.remove();
104 } 104 }
105 105
106 OProcess *proc = new OProcess; 106 OProcess *proc = new OProcess;
107 107
108 *proc << "wget" << "-q" << remoteFile << "-O" << localFile; 108 *proc << "wget" << "-q" << remoteFile << "-O" << localFile;
109 connect( proc, SIGNAL( processExited(Opie::Core::OProcess*) ), this, SLOT( dataRetrieved(Opie::Core::OProcess*) ) ); 109 connect( proc, SIGNAL( processExited(Opie::Core::OProcess*) ), this, SLOT( dataRetrieved(Opie::Core::OProcess*) ) );
110 proc->start(); 110 proc->start();
111} 111}
112 112
113void WeatherPluginWidget::displayWeather() 113void WeatherPluginWidget::displayWeather()
114{ 114{
115 weatherData = QString::null; 115 weatherData = QString::null;
116 116
117 QFile file( localFile ); 117 QFile file( localFile );
118 118
119 if ( file.size() > 0 && file.open( IO_ReadOnly ) ) 119 if ( file.size() > 0 && file.open( IO_ReadOnly ) )
120 { 120 {
121 QTextStream data( &file ); 121 QTextStream data( &file );
122 while ( !data.eof() ) 122 while ( !data.eof() )
123 { 123 {
124 weatherData.append( data.readLine() ); 124 weatherData.append( data.readLine() );
125 } 125 }
126 file.close(); 126 file.close();
127 weatherData = weatherData.simplifyWhiteSpace(); 127 weatherData = weatherData.simplifyWhiteSpace();
128 128
129 QString tmpstr; 129 QString tmpstr;
130 130
131 tmpstr.append( tr( "Temp: " ) ); 131 tmpstr.append( tr( "Temp: " ) );
132 getTemp( weatherData ); 132 getTemp( weatherData );
133 tmpstr.append( dataStr ); 133 tmpstr.append( dataStr );
134 134
135 tmpstr.append( tr( " Wind: " ) ); 135 tmpstr.append( tr( " Wind: " ) );
136 getWind( weatherData ); 136 getWind( weatherData );
137 tmpstr.append( dataStr ); 137 tmpstr.append( dataStr );
138 138
139 tmpstr.append( tr( "\nPres: " ) ); 139 tmpstr.append( tr( "\nPres: " ) );
140 getPressure( weatherData ); 140 getPressure( weatherData );
141 tmpstr.append( dataStr ); 141 tmpstr.append( dataStr );
142 142
143 weatherLabel->setText( tmpstr ); 143 weatherLabel->setText( tmpstr );
144 144
145 tmpstr = "todayweatherplugin/"; 145 tmpstr = "todayweatherplugin/";
146 getIcon( weatherData ); 146 getIcon( weatherData );
147 tmpstr.append( dataStr ); 147 tmpstr.append( dataStr );
148 QImage logo1 = Resource::loadImage( tmpstr ); 148 QPixmap pic;
149 QPixmap pic; 149 pic.convertFromImage( Resource::loadImage( tmpstr ).smoothScale( AppLnk::smallIconSize(), AppLnk::smallIconSize() ) );
150 pic.convertFromImage( logo1 ); 150 weatherIcon->setPixmap( pic );
151 weatherIcon->setPixmap( pic );
152 } 151 }
153 else 152 else
154 { 153 {
155 weatherLabel->setText( tr( "Current weather data not available." ) ); 154 weatherLabel->setText( tr( "Current weather data not available." ) );
156 } 155 }
157} 156}
158 157
159void WeatherPluginWidget::getTemp( const QString &data ) 158void WeatherPluginWidget::getTemp( const QString &data )
160{ 159{
161 int value; 160 int value;
162 bool ok; 161 bool ok;
163 162
164 int pos = data.find( QRegExp( "M?[0-9]+/M?[0-9]+" ), 20 ); 163 int pos = data.find( QRegExp( "M?[0-9]+/M?[0-9]+" ), 20 );
165 if ( pos > -1 ) 164 if ( pos > -1 )
166 { 165 {
167 if ( data.at( pos ) == 'M' ) 166 if ( data.at( pos ) == 'M' )
168 { 167 {
169 value = -1 * data.mid( pos + 1, 2 ).toInt( &ok ); 168 value = -1 * data.mid( pos + 1, 2 ).toInt( &ok );
170 } 169 }
171 else 170 else
172 { 171 {
173 value = data.mid( pos, 2 ).toInt( &ok ); 172 value = data.mid( pos, 2 ).toInt( &ok );
174 } 173 }
175 if ( useMetric ) 174 if ( useMetric )
176 { 175 {
177 dataStr = QString::number( value ); 176 dataStr = QString::number( value );
178 dataStr.append( 'C' ); 177 dataStr.append( 'C' );
179 } 178 }
180 else 179 else
181 { 180 {
182 dataStr = QString::number( ( value * 9 / 5 ) + 32 ); 181 dataStr = QString::number( ( value * 9 / 5 ) + 32 );
183 dataStr.append( 'F' ); 182 dataStr.append( 'F' );
184 } 183 }
185 } 184 }
186 else 185 else
187 { 186 {
188 dataStr = tr( "n/a" ); 187 dataStr = tr( "n/a" );
189 } 188 }
190} 189}
191 190
192void WeatherPluginWidget::getWind( const QString &data ) 191void WeatherPluginWidget::getWind( const QString &data )
193{ 192{
194 int value; 193 int value;
195 bool ok; 194 bool ok;
196 195
197 int pos = data.find( QRegExp( "[0-9]*G*[0-9]*KT" ), 20 ); 196 int pos = data.find( QRegExp( "[0-9]*G*[0-9]*KT" ), 20 );
198 if ( pos > -1 ) 197 if ( pos > -1 )
199 { 198 {
200 if ( data.mid( pos, 3 ) != "VRB" ) 199 if ( data.mid( pos, 3 ) != "VRB" )
201 { 200 {
202 value = data.mid( pos, 3 ).toInt( &ok ); 201 value = data.mid( pos, 3 ).toInt( &ok );
203 if ( ( value >= 0 && value < 23 ) || ( value >= 239 && value <= 360 ) ) 202 if ( ( value >= 0 && value < 23 ) || ( value >= 239 && value <= 360 ) )
204 dataStr = tr("E " ); 203 dataStr = tr("E " );
205 else if ( value >= 23 && value < 69 ) 204 else if ( value >= 23 && value < 69 )
206 dataStr = tr( "NE " ); 205 dataStr = tr( "NE " );
207 else if ( value >= 69 && value < 113 ) 206 else if ( value >= 69 && value < 113 )
208 dataStr = tr( "N " ); 207 dataStr = tr( "N " );
209 else if ( value >= 113 && value < 157 ) 208 else if ( value >= 113 && value < 157 )
210 dataStr = tr( "NW " ); 209 dataStr = tr( "NW " );
211 else if ( value >= 157 && value < 203 ) 210 else if ( value >= 157 && value < 203 )
212 dataStr = tr( "W " ); 211 dataStr = tr( "W " );
213 else if ( value >= 203 && value < 248 ) 212 else if ( value >= 203 && value < 248 )
214 dataStr = tr( "SW " ); 213 dataStr = tr( "SW " );
215 else if ( value >= 248 && value < 294 ) 214 else if ( value >= 248 && value < 294 )
216 dataStr = tr( "S " ); 215 dataStr = tr( "S " );
217 else if ( value >= 294 && value < 238 ) 216 else if ( value >= 294 && value < 238 )
218 dataStr = tr( "SE " ); 217 dataStr = tr( "SE " );
219 } 218 }
220 if ( data.mid( pos + 5, 1) == "G" || 219 if ( data.mid( pos + 5, 1) == "G" ||
221 data.mid( pos + 5, 1) == "K" ) 220 data.mid( pos + 5, 1) == "K" )
222 { 221 {
223 value = data.mid( pos + 3, 2 ).toInt( &ok ); 222 value = data.mid( pos + 3, 2 ).toInt( &ok );
224 } 223 }
225 else 224 else
226 { 225 {
227 value = data.mid( pos + 3, 3 ).toInt( &ok ); 226 value = data.mid( pos + 3, 3 ).toInt( &ok );
228 } 227 }
229 if ( useMetric ) 228 if ( useMetric )
230 { 229 {
231 value = value * 3.6 / 1.94; 230 value = value * 3.6 / 1.94;
232 dataStr.append( QString::number( value ) ); 231 dataStr.append( QString::number( value ) );
233 dataStr.append( tr( " KPH" ) ); 232 dataStr.append( tr( " KPH" ) );
234 } 233 }
235 else 234 else
236 { 235 {
237 value = value * 2.24 / 1.94; 236 value = value * 2.24 / 1.94;
238 dataStr.append( QString::number( value ) ); 237 dataStr.append( QString::number( value ) );
239 dataStr.append( tr( " MPH" ) ); 238 dataStr.append( tr( " MPH" ) );
240 } 239 }
241 } 240 }
242 else 241 else
243 { 242 {
244 dataStr = tr( "n/a" ); 243 dataStr = tr( "n/a" );
245 } 244 }
246} 245}
247 246
248void WeatherPluginWidget::getPressure( const QString &data ) 247void WeatherPluginWidget::getPressure( const QString &data )
249{ 248{
250 float value; 249 float value;
251 bool ok; 250 bool ok;
252 251
253 int pos = data.find( QRegExp( "[AQ][0-9]+" ), 20 ); 252 int pos = data.find( QRegExp( "[AQ][0-9]+" ), 20 );
254 if ( pos > -1 ) 253 if ( pos > -1 )
255 { 254 {
256 value = data.mid( pos + 1, 4 ).toFloat( &ok ); 255 value = data.mid( pos + 1, 4 ).toFloat( &ok );
257 if ( useMetric ) 256 if ( useMetric )
258 { 257 {
259 if ( data.mid( pos, 1 ) == "A" ) 258 if ( data.mid( pos, 1 ) == "A" )
260 value *= 33.8639 / 100; 259 value *= 33.8639 / 100;
261 dataStr = QString::number( value, 'f', 2 ); 260 dataStr = QString::number( value, 'f', 2 );
262 dataStr.append( tr( " kPa" ) ); 261 dataStr.append( tr( " kPa" ) );
263 } 262 }
264 else 263 else
265 { 264 {
266 if ( data.mid( pos, 1 ) == "Q" ) 265 if ( data.mid( pos, 1 ) == "Q" )
267 value /= 33.8639; 266 value /= 33.8639;
268 else 267 else
269 value /= 100; 268 value /= 100;
270 dataStr = QString::number( value, 'f', 2 ); 269 dataStr = QString::number( value, 'f', 2 );
271 dataStr.append( tr( " Hg" ) ); 270 dataStr.append( tr( " Hg" ) );
272 } 271 }
273 } 272 }
274 else 273 else
275 { 274 {
276 dataStr = tr( "n/a" ); 275 dataStr = tr( "n/a" );
277 } 276 }
278} 277}
279 278