autoload bookmarks on start if bookmarks.csv found
[qnavitctl] / src / mainwindow.cpp
1 #include "mainwindow.h"
2 #include "ui_mainwindow.h"
3 #include "csvreader.h"
4 #include <QFileDialog>
5 #include <QProcess>
6
7 MainWindow::MainWindow(QWidget *parent)
8                 : QMainWindow(parent), ui(new Ui::MainWindow)
9 {
10         ui->setupUi(this);
11         navitInstance = NULL;
12         navitIter = NULL;
13
14         QStringList env = QProcess::systemEnvironment();
15         QString homedir = "";
16         int pathpos = env.indexOf(QRegExp("^HOME.*=.*"));
17         if(pathpos>-1) {
18                 homedir = (env.at(pathpos).split("="))[1];
19         }
20         qDebug() << "HOME:" << homedir;
21         QString bookmarksCsv = "/dev/null";
22         if(QFileInfo("bookmarks.csv").isReadable()) {
23                 bookmarksCsv = "bookmarks.csv";
24         } else if (QFileInfo(homedir + "/bookmarks.csv").isReadable()) {
25                 bookmarksCsv = homedir + "/bookmarks.csv";
26         }
27         qDebug() << "bookmarks file:" << bookmarksCsv;
28         model = new BookmarkModel(CSVReader::getBookmarks(bookmarksCsv));
29         ui->bookmarksTableView->setModel(model);
30         ui->bookmarksTableView->resizeColumnsToContents();
31
32         connect(ui->setPosNavitButton, SIGNAL(clicked()), this, SLOT(setPosition()));
33         connect(ui->centerNavitButton, SIGNAL(clicked()), this, SLOT(setCenter()));
34         connect(ui->setDestNavitButton, SIGNAL(clicked()), this, SLOT(setDestination()));
35         connect(ui->registerNavitButton, SIGNAL(clicked()), this, SLOT(registerNavitInstance()));
36         connect(ui->releaseNavitButton, SIGNAL(clicked()), this, SLOT(releaseNavitInstance()));
37
38         connect(ui->bmFileOpenButton, SIGNAL(clicked()), this, SLOT(selectBookmarksFile()));
39         connect(ui->bmFileLoadButton, SIGNAL(clicked()), this, SLOT(readBookmarksFile()));
40         connect(ui->clearBmButton, SIGNAL(clicked()), model, SLOT(clear()));
41
42         connect(ui->sizeButton, SIGNAL(clicked()), this, SLOT(toggleFullscreen()));
43         connect(ui->exitButton, SIGNAL(clicked()), this, SLOT(close()));
44
45         registerNavitInstance();
46 }
47
48 MainWindow::~MainWindow()
49 {
50         releaseNavitInstance();
51         delete ui;
52 }
53
54 void MainWindow::registerNavitInstance() {
55         QDBusInterface navitInterface("org.navit_project.navit", "/org/navit_project/navit","org.navit_project.navit" );
56         QDBusReply<QDBusObjectPath> reply = navitInterface.call( "iter" );
57         qDebug() << "reply:" << reply.value().path();
58         if(reply.isValid()) {
59                 navitIter = new QDBusObjectPath(reply.value().path());
60                 QDBusReply<QDBusObjectPath> instancePath = navitInterface.call(QString("get_navit"), qVariantFromValue(*navitIter));
61                 qDebug() << "iter:" << navitIter->path();
62                 qDebug() << "instancePath:" << instancePath.value().path();
63                 if(instancePath.isValid()) {
64                         navitInstance = new QDBusInterface("org.navit_project.navit", instancePath.value().path(),"org.navit_project.navit.navit" );
65                         qDebug() << "instance set:" << navitInstance;
66                         ui->statusBar->showMessage(tr("registered Navit instance"),defaultStatusTimeout);
67                 } else {
68                         qDebug() << "instance not valid" << navitInstance;
69                 }
70         } else {
71                 qDebug() << "coud not get iter";
72                 ui->statusBar->showMessage(tr("could not get Navit instance"),defaultStatusTimeout);
73         }
74 }
75
76 void MainWindow::releaseNavitInstance() {
77         if(navitIter) {
78                 qDebug() << "releasing:" << navitIter->path();
79                 QDBusInterface navitInterface("org.navit_project.navit", "/org/navit_project/navit","org.navit_project.navit" );
80                 QDBusReply<bool> reply = navitInterface.call("iter_destroy",qVariantFromValue(*navitIter));
81                 if(reply.isValid()) {
82                         qDebug() << "released:" << navitIter->path();
83                 }
84                 delete navitInstance;
85                 navitInstance = NULL;
86                 delete navitIter;
87                 navitIter = NULL;
88         }
89 }
90
91 void MainWindow::setCenter() {
92         Bookmark bm = model->bookmark(ui->bookmarksTableView->currentIndex());
93         if(! bm.isValid()) {
94                 ui->statusBar->showMessage(tr("no valid bookmark"),defaultStatusTimeout);
95                 return;
96         }
97         if(navitInstance) {
98                 QDBusMessage msg = navitInstance->call("set_center", QString("%1 %2").arg(bm.getLon()).arg(bm.getLat()));
99                 qDebug() << __FUNCTION__ << QString("%1 %2").arg(bm.getLon()).arg(bm.getLat());
100                 if(msg.type() == QDBusMessage::ErrorMessage) {
101                         qDebug() << msg.errorMessage();
102                         ui->statusBar->showMessage(msg.errorMessage(),defaultStatusTimeout);
103                 }
104         }
105 }
106
107 void MainWindow::setPosition() {
108         Bookmark bm = model->bookmark(ui->bookmarksTableView->currentIndex());
109         if(! bm.isValid()) {
110                 ui->statusBar->showMessage(tr("no valid bookmark"),defaultStatusTimeout);
111                 return;
112         }
113         if(navitInstance) {
114                 QDBusMessage msg = navitInstance->call("set_position", QString("%1 %2").arg(bm.getLon()).arg(bm.getLat()));
115                 qDebug() << __FUNCTION__ << QString("%1 %2").arg(bm.getLon()).arg(bm.getLat());
116                 if(msg.type() == QDBusMessage::ErrorMessage) {
117                         qDebug() << msg.errorMessage();
118                         ui->statusBar->showMessage(msg.errorMessage(),defaultStatusTimeout);
119                 }
120         }
121 }
122
123 void MainWindow::setDestination() {
124         Bookmark bm = model->bookmark(ui->bookmarksTableView->currentIndex());
125         if(! bm.isValid()) {
126                 ui->statusBar->showMessage(tr("no valid bookmark"),defaultStatusTimeout);
127                 return;
128         }
129         if(navitInstance) {
130                 QDBusMessage msg = navitInstance->call("set_destination", QString("%1 %2").arg(bm.getLon()).arg(bm.getLat()),bm.getName());
131                 qDebug() << __FUNCTION__ << QString("%1 %2").arg(bm.getLon()).arg(bm.getLat()) << bm.getName();
132                 if(msg.type() == QDBusMessage::ErrorMessage) {
133                         qDebug() << msg.errorMessage();
134                         ui->statusBar->showMessage(msg.errorMessage(),defaultStatusTimeout);
135                 }
136         }
137 }
138
139 void MainWindow::toggleFullscreen() {
140         if (this->isFullScreen()) {
141                 this->showNormal();
142                 ui->sizeButton->setText(QApplication::translate("mainWindow", "Fullscreen", 0, QApplication::UnicodeUTF8));
143         } else {
144                 this->showFullScreen();
145                 ui->sizeButton->setText(QApplication::translate("mainWindow", "Window", 0, QApplication::UnicodeUTF8));
146         }
147         return;
148 }
149
150 void MainWindow::readBookmarksFile() {
151         QString filename = ui->bmFileLineEdit->text();
152         model->appendBookmark(CSVReader::getBookmarks(filename));
153         ui->bookmarksTableView->resizeColumnsToContents();
154 }
155
156 void MainWindow::selectBookmarksFile() {
157         ui->bmFileLineEdit->setText(
158                                 QFileDialog::getOpenFileName(this,tr("Select Bookmarsk File"))
159                         );
160 }