35fc77f6fda0c0c5199853f3b1b4f603fcada5b2
[harbour-pedalo.git] / src / harbour-pedalo.cpp
1 #include <QtQuick>
2 #ifdef QT_QML_DEBUG
3 #include <QDebug>
4 #endif
5
6 #include <sailfishapp.h>
7
8 #include "journey.h"
9 #include "journeymodel.h"
10 #include "statsmodel.h"
11 #include "status.h"
12 #include "settings.h"
13 #include "imageprovider.h"
14 #include "graph.h"
15 #include "statsweekdayave.h"
16 #include "statsweekdaycongestion.h"
17
18 #include "harbour-pedalo.h"
19
20 int main(int argc, char *argv[])
21 {
22 // SailfishApp::main() will display "qml/harbour-pedalo.qml", if you need more
23 // control over initialization, you can use:
24 //
25 // - SailfishApp::application(int, char *[]) to get the QGuiApplication *
26 // - SailfishApp::createView() to get a new QQuickView * instance
27 // - SailfishApp::pathTo(QString) to get a QUrl to a resource file
28 // - SailfishApp::pathToMainQml() to get a QUrl to the main QML file
29 //
30 // To display the view, call "show()" (will show fullscreen on device).
31
32 QScopedPointer<QGuiApplication> app(SailfishApp::application(argc, argv));
33 // These values are used by QSettings to access the config file in
34 // /home/nemo/.local/share/flypig/harbour-pedalo.conf
35 //QCoreApplication::setOrganizationName("flypig");
36 QCoreApplication::setOrganizationDomain("www.flypig.co.uk");
37 QCoreApplication::setApplicationName(APP_NAME);
38
39 Settings::instantiate();
40 qmlRegisterSingletonType<Settings>("harbour.pedalo.settings", 1, 0, "Settings", Settings::provider);
41 qmlRegisterType<JourneyModel>("harbour.pedalo.journeymodel", 1, 0, "JourneyModel");
42 qmlRegisterType<Graph>("harbour.pedalo.graph", 1, 0, "Graph");
43
44 JourneyModel journeys;
45 Status currentStatus(journeys);
46 Settings::getInstance().setMainStatus(currentStatus);
47 Settings::getInstance().loadSettings();
48
49 StatsModel statsmodel;
50
51 StatsWeekdayAve statsweekdayave(&journeys);
52 statsmodel.addStats(statsweekdayave);
53
54 StatsWeekdayCongestion statsweekdaycongestion(&journeys);
55 statsmodel.addStats(statsweekdaycongestion);
56
57 QFile file;
58 file.setFileName(Settings::getConfigDir() + "/journeys.csv");
59 journeys.importFromFile(file);
60 journeys.sort(JourneyModel::StartRole, Qt::DescendingOrder);
61
62 QScopedPointer<QQuickView> view(SailfishApp::createView());
63 view->engine()->addImageProvider(QLatin1String("pedalo"), new ImageProvider(Settings::getInstance()));
64 view->setSource(SailfishApp::pathTo("qml/harbour-pedalo.qml"));
65
66 QQmlContext *ctxt = view->rootContext();
67 ctxt->setContextProperty("version", VERSION);
68 qDebug() << "harbour-pedalo VERSION string: " << VERSION;
69 qDebug() << "VERSION_MAJOR: " << VERSION_MAJOR;
70 qDebug() << "VERSION_MINOR: " << VERSION_MINOR;
71 qDebug() << "VERSION_BUILD: " << VERSION_BUILD;
72
73 ctxt->setContextProperty("journeymodel", &journeys);
74 ctxt->setContextProperty("currentStatus", &currentStatus);
75 ctxt->setContextProperty("statsmodel", &statsmodel);
76
77 view->show();
78 int result = app->exec();
79
80 // Write out the journey data
81 QDir dir;
82 dir.mkpath(Settings::getConfigDir());
83 file.setFileName(Settings::getConfigDir() + "/journeys.csv");
84 qDebug() << "File saved as: " << file.fileName();
85 journeys.exportToFile(file);
86
87 Settings::getInstance().saveSettings();
88
89 return result;
90 }