1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
|
/***************************************************************************
weatherlib.cpp - description
-------------------
begin : Wed Jul 5 2000
copyright : (C) 2000 by Ian Reinhart Geiser
email : geiseri@msoe.edu
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "config.h"
#include <qfile.h>
#include <qdatetime.h>
#include <qtextstream.h>
#include <kglobal.h>
#include <klocale.h>
#include <kdebug.h>
#include <math.h>
#include <krfcdate.h>
#include <kio/job.h>
#include <kurl.h>
#include <knotifyclient.h>
#include <ktempfile.h>
#include <unistd.h>
#include <kapplication.h>
#include <kpassivepopup.h>
#include "metar_parser.h"
#include "stationdatabase.h"
#include "sun.h"
#include "weatherlib.h"
#include "weatherlib.moc"
class WeatherLib::Data
{
public:
Data();
~Data(){ if ( target ) delete target; }
void clear();
/** The current weather state outside */
struct WeatherInfo wi;
QDateTime age;
KTempFile *target;
bool downloading;
bool updated;
KIO::Job *job;
};
WeatherLib::Data::Data()
: target( 0 ), job( 0 )
{
clear();
}
void WeatherLib::Data::clear()
{
age = QDateTime::currentDateTime();
downloading = false;
updated = false;
job = 0;
if ( target )
{
delete target;
target = 0;
}
}
WeatherLib::WeatherLib(StationDatabase *stationDB, QObject *parent, const char *name)
: QObject (parent, name)
{
KGlobal::locale()->insertCatalogue("kweather");
m_StationDb = stationDB;
data.setAutoDelete( true );
}
WeatherLib::~WeatherLib()
{
}
void WeatherLib::slotCopyDone(KIO::Job* job)
{
kdDebug(12006) << "Copy done..." << endl;
if( job->error())
{
kdDebug(12006) << "Error code: " << job->error() << endl;
//job->showErrorDialog(0L);
if ((job->error() == KIO::ERR_COULD_NOT_CONNECT || job->error() == KIO::ERR_UNKNOWN_HOST)
&& !hostDown)
{
hostDown= true;
// no need to show a passive popup here, as below all stations will show "dunno" icon
}
}
// Find the job
QDictIterator<Data> it( data );
Data *d = 0L;
for( ; it.current(); ++it )
{
kdDebug(12006) << "Searching for job..." << endl;
if(it.current()->job == job)
{
d = it.current();
d->downloading = false;
if( !job->error() )
{
kdDebug( 12006) << "Reading: " << d->target->name() << endl;
QFile file( d->target->name() );
file.open( IO_ReadOnly );
QTextStream *t = new QTextStream( &file );
//QTextStream *t = d->target->textStream();
if( t )
{
QString s = QString::null;
while ( !t->eof() )
{
s += " " + t->readLine();
}
if ( !s.isEmpty() )
{
kdDebug( 12006 ) << "Parse: " << s << endl;
MetarParser parser(m_StationDb, KGlobal::locale()->measureSystem());
d->wi = parser.processData(d->wi.reportLocation, s);
d->age = QDateTime::currentDateTime().addSecs(1800);
emit fileUpdate(d->wi.reportLocation);
d->updated = true;
}
else
{
// File error
kdDebug( 12006 ) << "File empty error..." << endl;
KPassivePopup::message( i18n("KWeather Error!"),
i18n("The temp file %1 was empty.").arg(d->target->name()), 0L,"error" );
d->updated = false;
}
}
else
{
// File error
kdDebug( 12006 ) << "File read error..." << endl;
KPassivePopup::message( i18n("KWeather Error!"),
i18n("Could not read the temp file %1.").arg(d->target->name()), 0L,"error" );
d->updated = false;
}
delete d->target;
d->target = 0L;
d->job = 0L;
}
else if( job->error() == KIO::ERR_DOES_NOT_EXIST)
{
data.remove(d->wi.reportLocation);
kdDebug( 12006 ) << "Bad station data so i am going to remove it" << endl;
KPassivePopup::message( i18n("KWeather Error!"),
i18n("The requested station does not exist."), 0L,"error" );
}
else if(job->error() == KIO::ERR_COULD_NOT_CONNECT ||
job->error() == KIO::ERR_UNKNOWN_HOST)
{
kdDebug( 12006 ) << "Offline now..." << endl;
d->clear();
d->wi.theWeather = "dunno";
d->wi.qsCurrentList.append(i18n("The network is currently offline..."));
d->wi.qsCurrentList.append(i18n("Please update later."));
emit fileUpdate(d->wi.reportLocation);
}
else
{
kdDebug( 12006 ) << "Duh?..." << endl;
}
}
}
}
void WeatherLib::getData(Data *d, bool force /* try even if host was down last time*/)
{
if(!d->downloading && (force || !hostDown) )
{
d->downloading = true;
d->updated = false;
QString u = "http://weather.noaa.gov/pub/data/observations/metar/stations/";
u += d->wi.reportLocation.upper().stripWhiteSpace();
u += ".TXT";
d->target = new KTempFile(QString::null, "-weather");
d->target->setAutoDelete(true);
d->target->file();
KURL url(u);
KURL local(d->target->name());
d->job = KIO::file_copy( url, local, -1, true, false, false);
d->job->addMetaData("cache", "reload"); // Make sure to get fresh info
connect( d->job, SIGNAL( result( KIO::Job *)),
SLOT(slotCopyDone(KIO::Job *)));
kdDebug( 12006 ) << "Copying " << url.prettyURL() << " to "
<< local.prettyURL() << endl;
emit fileUpdating(d->wi.reportLocation);
}
}
WeatherLib::Data* WeatherLib::findData(const QString &stationID)
{
Data *d = data[stationID];
if (!d)
{
d = new Data();
d->wi.reportLocation = stationID;
d->wi.theWeather = "dunno";
d->wi.qsCurrentList.append( i18n( "Retrieving weather data..." ) );
data.insert(stationID, d);
getData(d);
}
return d;
}
QString WeatherLib::temperature(const QString &stationID){
Data *d = findData(stationID);
return d->wi.qsTemperature;
}
QString WeatherLib::pressure(const QString &stationID){
Data *d = findData(stationID);
return d->wi.qsPressure;
}
QString WeatherLib::wind(const QString &stationID){
Data *d = findData(stationID);
return (d->wi.qsWindSpeed + " " + d->wi.qsWindDirection);
}
/** */
QString WeatherLib::dewPoint(const QString &stationID){
Data *d = findData(stationID);
return d->wi.qsDewPoint;
}
QString WeatherLib::relHumidity(const QString &stationID){
Data *d = findData(stationID);
return d->wi.qsRelHumidity;
}
QString WeatherLib::heatIndex(const QString &stationID){
Data *d = findData(stationID);
return d->wi.qsHeatIndex;
}
QString WeatherLib::windChill(const QString &stationID){
Data *d = findData(stationID);
return d->wi.qsWindChill;
}
QString WeatherLib::iconName(const QString &stationID){
QString result("dunno");
// isEmpty is true for null or 0 length strings
if ( !stationID.isEmpty() )
{
Data *d = findData(stationID);
result = d->wi.theWeather;
}
return result;
}
QString WeatherLib::date(const QString &stationID){
Data *d = findData(stationID);
if ( ! d->wi.qsDate.isValid() )
return "";
else
{
QDateTime gmtDateTime(d->wi.qsDate, d->wi.qsTime);
QDateTime localDateTime = gmtDateTime.addSecs(KRFCDate::localUTCOffset() * 60);
return KGlobal::locale()->formatDateTime(localDateTime, false, false);
}
}
/** Returns the current cover */
QStringList WeatherLib::cover(const QString &stationID){
Data *d = findData(stationID);
return d->wi.qsCoverList;
}
/** return the visibility */
QString WeatherLib::visibility(const QString &stationID){
Data *d = findData(stationID);
return d->wi.qsVisibility;
}
/** return the weather text */
QStringList WeatherLib::weather(const QString &stationID){
Data *d = findData(stationID);
return d->wi.qsCurrentList;
}
bool WeatherLib::stationNeedsMaintenance(const QString &stationID)
{
Data *d = findData(stationID);
return d->wi.stationNeedsMaintenance;
}
void WeatherLib::update(const QString &stationID)
{
// Only grab new data if its more than 50 minutes old
Data *d = findData(stationID);
QDateTime timeout = QDateTime::currentDateTime();
kdDebug (12006) << "Current Time: " << KGlobal::locale()->formatDateTime(timeout, false, false) <<
" Update at: " << KGlobal::locale()->formatDateTime(d->age, false, false) << endl;
if( timeout >= d->age || !d->updated)
getData(d, true /* try even if host was down last time */);
else
emit fileUpdate(d->wi.reportLocation);
}
QStringList WeatherLib::stations()
{
QStringList l;
QDictIterator<Data> it( data );
for( ; it.current(); ++it )
l += it.currentKey();
return l;
}
void WeatherLib::forceUpdate(const QString &stationID)
{
hostDown = false; // we want to show error message if host is still down
Data *d = findData(stationID);
getData( d );
}
void WeatherLib::remove(const QString &stationID)
{
data.remove(stationID);
emit stationRemoved(stationID);
}
|