Changed the way the station schedule model is loaded
authorLuciano Montanaro <mikelima@cirulla.net>
Tue, 25 Oct 2011 23:01:33 +0000 (01:01 +0200)
committerLuciano Montanaro <mikelima@cirulla.net>
Tue, 27 Dec 2011 22:19:10 +0000 (23:19 +0100)
application/application.pro
application/dataprovider.cpp
application/dataprovider.h
application/stationschedulemodel.cpp [new file with mode: 0644]
application/stationschedulemodel.h [new file with mode: 0644]
application/view.cpp

index 7ee62a6..ed97d00 100644 (file)
@@ -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) {
index e6dfe25..a0c3d9a 100644 (file)
@@ -21,11 +21,13 @@ Boston, MA 02110-1301, USA.
 
 #include "dataprovider.h"
 #include "settings.h"
+#include "stationschedulemodel.h"
 
 #include <QDebug>
 #include <QNetworkAccessManager>
 #include <QNetworkReply>
 #include <QNetworkRequest>
+#include <QSharedPointer>
 #include <QWebElement>
 #include <QWebFrame>
 #include <QWebPage>
@@ -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;
 }
index 12e4f6c..f9c652b 100644 (file)
@@ -23,26 +23,29 @@ Boston, MA 02110-1301, USA.
 #define DATAPROVIDER_H
 
 #include <QObject>
+#include <QSharedPointer>
 
 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 (file)
index 0000000..314a19a
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+
+Copyright (C) 2011 Luciano Montanaro <mikelima@cirulla.net>
+
+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 <QDebug>
+
+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 (file)
index 0000000..0758b2f
--- /dev/null
@@ -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 <QObject>
+#include <QStringListModel>
+#include <QUrl>
+
+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
index 5d9bab4..9e5767a 100644 (file)
@@ -24,6 +24,7 @@ Boston, MA 02110-1301, USA.
 #include "dataprovider.h"
 #include "stationlistmodel.h"
 #include "stationlistproxymodel.h"
+#include "stationschedulemodel.h"
 
 #include <QDebug>
 #include <QDir>
@@ -69,11 +70,11 @@ View::View(QWidget *parent) :
 
     /* Types to be made accessible to QML */
     qRegisterMetaType<QModelIndex>("QModelIndex");
-    //qRegisterMetaType<StationListProxyModel::SortingMode>("SortingMode");
-    qmlRegisterType<DataProvider>("net.cirulla.quandoparte", 1, 0, "DataProvider");
     qmlRegisterType<Settings>("net.cirulla.quandoparte", 1, 0, "Settings");
     qmlRegisterType<StationListProxyModel>(
                 "net.cirulla.quandoparte", 1, 0, "StationListProxyModel");
+    qmlRegisterType<StationScheduleModel>(
+                "net.cirulla.quandoparte", 1, 0, "StationScheduleModel");
 
     QDeclarativeContext *context = this->rootContext();
     /* objects to be made accessible to QML */