Load station list asynchronously
[quandoparte] / application / view.cpp
1 /*
2
3 Copyright (C) 2011 Luciano Montanaro <mikelima@cirulla.net>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; see the file COPYING.  If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19
20 */
21
22 #include "view.h"
23 #include "settings.h"
24 #include "dataprovider.h"
25 #include "stationlistmodel.h"
26 #include "stationlistproxymodel.h"
27
28 #include <QDebug>
29 #include <QDir>
30 #include <QFile>
31 #include <QtConcurrentRun>
32 #include <QtDeclarative>
33
34 // search Paths seem to be broken in Harmattan?
35
36 static QString trueFilePath(QString path)
37 {
38     qDebug() << "searching for" << path;
39     QString searchPathName = path.section(':', 0, 0);
40     qDebug() << "path is" << searchPathName;
41     QString fileName = path.section(':', 1, 1);
42     qDebug() << "filename is" << fileName;
43     QStringList dirs = QDir::searchPaths(searchPathName);
44     foreach(QString dir, dirs) {
45         qDebug() << "searching in" << dir;
46        QDir current(dir);
47        QString absoluteFileName = current.absoluteFilePath(fileName);
48         if (current.exists(fileName)) {
49             qDebug() << "found " << fileName << "in path" << absoluteFileName;
50             return absoluteFileName;
51         }
52     }
53     qDebug() << "file not found";
54     return QString();
55 }
56
57 View::View(QWidget *parent) :
58     QDeclarativeView(parent),
59     stationListModel(new StationListModel(this)),
60     stationListProxyModel(new StationListProxyModel(this))
61 {
62     showFullScreen();
63     future = QtConcurrent::run(
64                 stationListModel, &StationListModel::load,
65                 trueFilePath("stations:stations.qpl"));
66
67     stationListProxyModel->setSourceModel(stationListModel);
68
69     /* Types to be made accessible to QML */
70     qmlRegisterType<DataProvider>("net.cirulla.quandoparte", 1, 0, "DataProvider");
71     qmlRegisterType<Settings>("net.cirulla.quandoparte", 1, 0, "Settings");
72     qmlRegisterType<StationListProxyModel>(
73                 "net.cirulla.quandoparte", 1, 0, "StationListProxyModel");
74
75     QDeclarativeContext *context = this->rootContext();
76     /* objects to be made accessible to QML */
77     context->setContextProperty("settings", Settings::instance());
78     context->setContextProperty("stationList", stationListModel);
79     context->setContextProperty("stationListProxyModel", stationListProxyModel);
80
81     // This does not seem ot work in harmattan. As a workaround, change dir to
82     // the qml dir, then load the file.
83     // m_view->setSource(QUrl::fromLocalFile("qml:main.qml"));
84     setSource(QUrl::fromLocalFile(trueFilePath("qml:main.qml")));
85 }
86
87 View::~View()
88 {
89     future.waitForFinished();
90 }