From 013dfe2c0b14b116038a9179ffd13bb60a225577 Mon Sep 17 00:00:00 2001 From: Luciano Montanaro Date: Wed, 26 Oct 2011 01:01:33 +0200 Subject: [PATCH] Changed the way the station schedule model is loaded --- application/application.pro | 7 ++-- application/dataprovider.cpp | 29 +++++++++++----- application/dataprovider.h | 11 +++--- application/stationschedulemodel.cpp | 62 ++++++++++++++++++++++++++++++++++ application/stationschedulemodel.h | 53 +++++++++++++++++++++++++++++ application/view.cpp | 5 +-- 6 files changed, 149 insertions(+), 18 deletions(-) create mode 100644 application/stationschedulemodel.cpp create mode 100644 application/stationschedulemodel.h diff --git a/application/application.pro b/application/application.pro index 8ddecec..56c8400 100644 --- a/application/application.pro +++ b/application/application.pro @@ -75,7 +75,7 @@ SOURCES += \ stationlistproxymodel.cpp \ settings.cpp \ dataprovider.cpp \ - stationschedule.cpp + stationschedulemodel.cpp HEADERS += \ $$PLATFORM_HEADERS \ @@ -83,7 +83,7 @@ HEADERS += \ stationlistproxymodel.h \ settings.h \ dataprovider.h \ - stationschedule.h + stationschedulemodel.h FORMS += \ settingsdialog.ui \ @@ -117,7 +117,8 @@ OTHER_FILES += \ resources/harmattan/qml/PageHeader.qml \ resources/harmattan/qml/uiconstants.js \ resources/harmattan/qml/StationListPage.js \ - resources/harmattan/qml/AboutPage.qml + resources/harmattan/qml/AboutPage.qml \ + resources/harmattan/qml/InfoBar.qml unix { isEmpty(PREFIX) { diff --git a/application/dataprovider.cpp b/application/dataprovider.cpp index e6dfe25..a0c3d9a 100644 --- a/application/dataprovider.cpp +++ b/application/dataprovider.cpp @@ -21,11 +21,13 @@ Boston, MA 02110-1301, USA. #include "dataprovider.h" #include "settings.h" +#include "stationschedulemodel.h" #include #include #include #include +#include #include #include #include @@ -35,13 +37,22 @@ static const int RECENT_STATIONS_MAX_COUNT = 10; // Class methods +DataProvider *DataProvider::instance() +{ + static DataProvider *dataProvider = 0; + + if (!dataProvider) + dataProvider = new DataProvider(0); + return dataProvider; +} + DataProvider::DataProvider(QObject *parent) : QObject(parent), accessManager(new QNetworkAccessManager(this)) { } -void DataProvider::stationSchedule(const QString &station) +void DataProvider::fetchStationSchedule(const QString &station) { QNetworkRequest request; Settings *settings = Settings::instance(); @@ -50,13 +61,12 @@ void DataProvider::stationSchedule(const QString &station) const QByteArray query(queryString.toLocal8Bit()); stationQueryReply = accessManager->post(request, query); connect(stationQueryReply, SIGNAL(finished()), - SLOT(onStationScheduleReady())); + SLOT(onStationScheduleFetched())); settings->recentStations().push_front(station); settings->recentStations().removeDuplicates(); if (settings->recentStations().count() > RECENT_STATIONS_MAX_COUNT) { settings->recentStations().pop_back(); } - // TODO Implement busy indication... } void DataProvider::updateStation() @@ -65,17 +75,18 @@ void DataProvider::updateStation() qDebug() << "updating station data"; if (!settings->recentStations().isEmpty()) { - stationSchedule(settings->recentStations().front()); + fetchStationSchedule(settings->recentStations().front()); } } -void DataProvider::onStationScheduleReady() +void DataProvider::onStationScheduleFetched() { disconnect(stationQueryReply, SIGNAL(finished()), - this, SLOT(onStationScheduleReady())); - // TODO implement parsing or data returning... - emit stationScheduleReady(QString::fromUtf8(stationQueryReply->readAll()), - stationQueryReply->url()); + this, SLOT(onStationScheduleFetched())); + + QString name = Settings::instance()->recentStations().front(); + + emit stationScheduleReady(stationQueryReply->readAll(), stationQueryReply->url()); stationQueryReply->deleteLater(); stationQueryReply = 0; } diff --git a/application/dataprovider.h b/application/dataprovider.h index 12e4f6c..f9c652b 100644 --- a/application/dataprovider.h +++ b/application/dataprovider.h @@ -23,26 +23,29 @@ Boston, MA 02110-1301, USA. #define DATAPROVIDER_H #include +#include class QNetworkAccessManager; class QNetworkReply; class QUrl; +class StationScheduleModel; + class DataProvider : public QObject { Q_OBJECT public: explicit DataProvider(QObject *parent = 0); - + static DataProvider *instance(); signals: - void stationScheduleReady(const QString &result, const QUrl &url); + void stationScheduleReady(const QByteArray &data, const QUrl &url); public slots: - void stationSchedule(const QString &station); + void fetchStationSchedule(const QString &station); void updateStation(); private slots: - void onStationScheduleReady(void); + void onStationScheduleFetched(void); private: QNetworkAccessManager *accessManager; diff --git a/application/stationschedulemodel.cpp b/application/stationschedulemodel.cpp new file mode 100644 index 0000000..314a19a --- /dev/null +++ b/application/stationschedulemodel.cpp @@ -0,0 +1,62 @@ +/* + +Copyright (C) 2011 Luciano Montanaro + +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. + +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 General Public License +along with this program; see the file COPYING. If not, write to +the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, +Boston, MA 02110-1301, USA. + +*/ + +#include "stationschedulemodel.h" + +#include "dataprovider.h" + +#include + +StationScheduleModel::StationScheduleModel(const QString &name, QObject *parent) : + QStringListModel(parent), + m_name(name) + +{ +} + +QString & StationScheduleModel::name() +{ + return m_name; +} + +void StationScheduleModel::setName(const QString &name) +{ + m_name = name; + emit nameChanged(); +} + +void StationScheduleModel::parse(const QByteArray &htmlReply, const QUrl &baseUrl) +{ + Q_UNUSED(baseUrl); + qDebug() << "--- start of query result --- cut here ------"; + qDebug() << htmlReply; + qDebug() << "--- end of query result ----- cut here ------"; +} + +void StationScheduleModel::fetch(const QString &name) +{ + DataProvider *provider = DataProvider::instance(); + + connect(provider, SIGNAL(stationScheduleReady(QByteArray,QUrl)), + this, SLOT(parse(QByteArray,QUrl))); + provider->fetchStationSchedule(name); + setName(name); +} diff --git a/application/stationschedulemodel.h b/application/stationschedulemodel.h new file mode 100644 index 0000000..0758b2f --- /dev/null +++ b/application/stationschedulemodel.h @@ -0,0 +1,53 @@ +/* + +Copyright (C) 2011 mikelima + +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. + +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 General Public License +along with this program; see the file COPYING. If not, write to +the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, +Boston, MA 02110-1301, USA. + +*/ + +#ifndef STATIONSCHEDULEMODEL_H +#define STATIONSCHEDULEMODEL_H + +#include +#include +#include + +class StationScheduleModel : public QStringListModel +{ + Q_OBJECT + Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged) + +public: + explicit StationScheduleModel(const QString &name = "", QObject *parent = 0); + + QString &name(); + void setName(const QString &name); + +signals: + void nameChanged(); + +public slots: + void fetch(const QString &name); + +private slots: + void parse(const QByteArray &htmlReply, const QUrl &baseUrl); + +private: + QString m_name; +}; + +#endif // STATIONSCHEDULEMODEL_H diff --git a/application/view.cpp b/application/view.cpp index 5d9bab4..9e5767a 100644 --- a/application/view.cpp +++ b/application/view.cpp @@ -24,6 +24,7 @@ Boston, MA 02110-1301, USA. #include "dataprovider.h" #include "stationlistmodel.h" #include "stationlistproxymodel.h" +#include "stationschedulemodel.h" #include #include @@ -69,11 +70,11 @@ View::View(QWidget *parent) : /* Types to be made accessible to QML */ qRegisterMetaType("QModelIndex"); - //qRegisterMetaType("SortingMode"); - qmlRegisterType("net.cirulla.quandoparte", 1, 0, "DataProvider"); qmlRegisterType("net.cirulla.quandoparte", 1, 0, "Settings"); qmlRegisterType( "net.cirulla.quandoparte", 1, 0, "StationListProxyModel"); + qmlRegisterType( + "net.cirulla.quandoparte", 1, 0, "StationScheduleModel"); QDeclarativeContext *context = this->rootContext(); /* objects to be made accessible to QML */ -- 1.7.9.5