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