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