Initial changes for sailfish port
[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 #include "stationschedulemodel.h"
28
29 #include <QDebug>
30 #include <QDir>
31 #include <QFile>
32 #include <QModelIndex>
33 #include <QtConcurrentRun>
34 #ifdef TARGET_PLATFORM_SAILFISH
35 #include <QtQuick>
36 #else
37 #include <QtDeclarative>
38 #endif
39
40 // search Paths seem to be broken in Harmattan?
41
42 static QString trueFilePath(const QString &path)
43 {
44     qDebug() << "searching for" << path;
45 #if USE_RESOURCES
46     return path;
47 #else
48     QString searchPathName = path.section(':', 0, 0);
49     qDebug() << "path is" << searchPathName;
50     QString fileName = path.section(':', 1, 1);
51     qDebug() << "filename is" << fileName;
52     QStringList dirs = QDir::searchPaths(searchPathName);
53     foreach(QString dir, dirs) {
54         qDebug() << "searching in" << dir;
55        QDir current(dir);
56        QString absoluteFileName = current.absoluteFilePath(fileName);
57         if (current.exists(fileName)) {
58             qDebug() << "found " << fileName << "in path" << absoluteFileName;
59             return absoluteFileName;
60         }
61     }
62     qDebug() << "file not found";
63     return QString();
64 #endif
65 }
66
67 #ifdef TARGET_PLATFORM_SAILFISH
68 View::View(QWindow *parent) :
69     QQuickView(parent),
70 #else
71 View::View(QWidget *parent) :
72     QDeclarativeView(parent),
73 #endif
74     stationListModel(new StationListModel(this)),
75     stationListProxyModel(new StationListProxyModel(this))
76 {
77     showFullScreen();
78     future = QtConcurrent::run(
79                 stationListModel, &StationListModel::load,
80                 trueFilePath("stations:stations.qpl"));
81     stationListProxyModel->setSourceModel(stationListModel);
82
83     /* Types to be made accessible to QML */
84     qRegisterMetaType<QModelIndex>("QModelIndex");
85     qmlRegisterType<Settings>("net.cirulla.quandoparte", 1, 0, "Settings");
86     qmlRegisterType<StationListProxyModel>(
87                 "net.cirulla.quandoparte", 1, 0, "StationListProxyModel");
88     qmlRegisterType<StationScheduleModel>(
89                 "net.cirulla.quandoparte", 1, 0, "StationScheduleModel");
90
91 #ifdef TARGET_PLATFORM_SAILFISH
92     QQmlContext *context = this->rootContext();
93 #else
94     QDeclarativeContext *context = this->rootContext();
95 #endif
96     /* objects to be made accessible to QML */
97     context->setContextProperty("settings", Settings::instance());
98     context->setContextProperty("stationList", stationListModel);
99     context->setContextProperty("stationListProxyModel", stationListProxyModel);
100
101     // This does not seem to work in harmattan. As a workaround, change dir to
102     // the qml dir, then load the file.
103     // m_view->setSource(QUrl::fromLocalFile("qml:main.qml"));
104     setSource(trueFilePath("qml:main.qml"));
105 }
106
107 View::~View()
108 {
109     future.waitForFinished();
110 }