Commit 943f7690 authored by Petri Damstén's avatar Petri Damstén
Browse files

Make getting default location code public

svn path=/trunk/KDE/kdeplasma-addons/; revision=953976
parent b1726db6
set(plasmaweather_LIB_SRCS
weatherpopupapplet.cpp
weathervalidator.cpp
weatherlocation.cpp
weatherconfig.cpp
weatherconfigsearch.cpp
)
......@@ -9,6 +10,7 @@ set(plasmaweather_HEADERS
weatherpopupapplet.h
weathervalidator.h
weatherconfig.h
weatherlocation.h
plasmaweather_export.h
)
......
/*
* Copyright (C) 2009 Petri Damstén <damu@iki.fi>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "weatherlocation.h"
#include "weathervalidator.h"
class WeatherLocation::Private
{
public:
Private()
{}
Plasma::DataEngine *locationEngine;
WeatherValidator validator;
};
WeatherLocation::WeatherLocation(QObject *parent)
: QObject(parent)
, d(new Private())
{
QObject::connect(&d->validator, SIGNAL(finished(const QString&)),
this, SIGNAL(finished(const QString&)));
}
WeatherLocation::~WeatherLocation()
{
delete d;
}
void WeatherLocation::setDataEngines(Plasma::DataEngine* location, Plasma::DataEngine* weather)
{
d->locationEngine = location;
d->validator.setDataEngine(weather);
}
void WeatherLocation::getDefault()
{
if (d->locationEngine->isValid()) {
d->locationEngine->connectSource("location", this);
} else {
emit finished(QString());
}
}
void WeatherLocation::dataUpdated(const QString &source, const Plasma::DataEngine::Data &data)
{
d->locationEngine->disconnectSource(source, this);
QString city = data["city"].toString();
QString tmp = city.left(city.indexOf(','));
if (!tmp.isEmpty()) {
d->validator.validate("bbcukmet", tmp, true);
return;
}
emit finished(QString());
}
#include "weatherlocation.moc"
/*
* Copyright (C) 2009 Petri Damstén <damu@iki.fi>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef WEATHERLOCATION_HEADER
#define WEATHERLOCATION_HEADER
#include <Plasma/DataEngine>
/**
* @class WeatherLocation <plasmaweather/weatherlocation.h>
*
* @short Class to get default source for weather
*/
class WeatherLocation : public QObject
{
Q_OBJECT
public:
WeatherLocation(QObject *parent = 0);
virtual ~WeatherLocation();
/**
* Get default source
**/
void getDefault();
/**
* Sets dataengines to use
*
* @param location location dataengine
* @param weather weather dataengine
**/
void setDataEngines(Plasma::DataEngine* location, Plasma::DataEngine* weather);
Q_SIGNALS:
/**
* Emitted when validation is done
**/
void finished(const QString& source);
public Q_SLOTS:
void dataUpdated(const QString &source, const Plasma::DataEngine::Data &data);
private:
class Private;
Private * const d;
};
#endif
......@@ -19,7 +19,7 @@
#include "weatherpopupapplet.h"
#include "weatherconfig.h"
#include "weathervalidator.h"
#include "weatherlocation.h"
#include <KConfigGroup>
#include <KConfigDialog>
#include <plasma/weather/weatherutils.h>
......@@ -34,12 +34,11 @@ public:
: q(weatherapplet)
, weatherConfig(0)
, weatherEngine(0)
, locationEngine(0)
, timeEngine(0)
, updateInterval(0)
{
QObject::connect(&validator, SIGNAL(finished(const QString&)),
q, SLOT(validationFinished(const QString&)));
QObject::connect(&location, SIGNAL(finished(const QString&)),
q, SLOT(locationReady(const QString&)));
unitMap["C"] = WeatherUtils::Celsius;
unitMap["F"] = WeatherUtils::Fahrenheit;
unitMap["K"] = WeatherUtils::Kelvin;
......@@ -59,7 +58,6 @@ public:
WeatherPopupApplet *q;
WeatherConfig *weatherConfig;
Plasma::DataEngine *weatherEngine;
Plasma::DataEngine *locationEngine;
Plasma::DataEngine *timeEngine;
QString temperatureUnit;
QString speedUnit;
......@@ -67,7 +65,7 @@ public:
QString visibilityUnit;
int updateInterval;
QString source;
WeatherValidator validator;
WeatherLocation location;
QString conditionIcon;
QString tend;
......@@ -77,20 +75,8 @@ public:
double longitude;
QHash<QString, int> unitMap;
void tryCity(const QString& city)
void locationReady(const QString &src)
{
QString tmp = city.left(city.indexOf(','));
if (!tmp.isEmpty()) {
validator.validate("bbcukmet", tmp, true);
return;
}
q->setBusy(false);
q->setConfigurationRequired(true);
}
void validationFinished(const QString &src)
{
q->setBusy(false);
if (!src.isEmpty()) {
source = src;
KConfigGroup cfg = q->config();
......@@ -98,6 +84,7 @@ public:
emit q->configNeedsSaving();
q->connectToEngine();
} else {
q->setBusy(false);
q->setConfigurationRequired(true);
}
}
......@@ -207,22 +194,16 @@ void WeatherPopupApplet::init()
d->weatherEngine = dataEngine("weather");
d->timeEngine = dataEngine("time");
d->validator.setDataEngine(d->weatherEngine);
connectToEngine();
}
void WeatherPopupApplet::connectToEngine()
{
setBusy(true);
if (d->source.isEmpty()) {
d->locationEngine = dataEngine("geolocation");
if (d->locationEngine->isValid()) {
d->locationEngine->connectSource("location", this);
setBusy(true);
} else {
setConfigurationRequired(true);
}
d->location.setDataEngines(dataEngine("geolocation"), d->weatherEngine);
d->location.getDefault();
} else {
setBusy(true);
d->weatherEngine->connectSource(d->source, this, d->updateInterval * 60 * 1000);
}
}
......@@ -271,12 +252,7 @@ void WeatherPopupApplet::configAccepted()
void WeatherPopupApplet::dataUpdated(const QString& source,
const Plasma::DataEngine::Data &data)
{
if (source == "location") {
d->tryCity(data["city"].toString());
d->locationEngine->disconnectSource(source, this);
return;
}
Q_UNUSED(source)
setBusy(false);
if (data.isEmpty()) {
......
......@@ -121,7 +121,7 @@ class PLASMAWEATHER_EXPORT WeatherPopupApplet : public Plasma::PopupApplet
class Private;
Private * const d;
Q_PRIVATE_SLOT(d, void validationFinished(const QString &source))
Q_PRIVATE_SLOT(d, void locationReady(const QString &source))
};
#endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment