2016-12-11 16:09:39 +00:00
|
|
|
#include "application.h"
|
|
|
|
|
|
|
|
#include "models/folder.h"
|
|
|
|
#include "database.h"
|
|
|
|
#include "models/foldermodel.h"
|
2017-01-02 16:28:03 +01:00
|
|
|
#include "models/change.h"
|
2016-12-11 16:09:39 +00:00
|
|
|
#include "services/folderservice.h"
|
2016-12-27 21:25:07 +01:00
|
|
|
#include "settings.h"
|
2016-12-31 10:48:18 +01:00
|
|
|
#include "uuid.h"
|
|
|
|
#include "dispatcher.h"
|
2017-01-10 18:05:58 +01:00
|
|
|
#include "paths.h"
|
2017-01-10 19:59:12 +01:00
|
|
|
#include "constants.h"
|
2016-12-11 16:09:39 +00:00
|
|
|
|
|
|
|
using namespace jop;
|
|
|
|
|
2016-12-29 20:19:00 +01:00
|
|
|
Application::Application(int &argc, char **argv) :
|
|
|
|
QGuiApplication(argc, argv),
|
2017-01-01 23:03:42 +01:00
|
|
|
db_(jop::db()),
|
2017-01-11 00:28:51 +01:00
|
|
|
synchronizer_(db_),
|
2016-12-29 20:19:00 +01:00
|
|
|
folderModel_(db_)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
2016-12-27 21:25:07 +01:00
|
|
|
// This is linked to where the QSettings will be saved. In other words,
|
|
|
|
// if these values are changed, the settings will be reset and saved
|
|
|
|
// somewhere else.
|
2017-01-10 19:59:12 +01:00
|
|
|
QCoreApplication::setOrganizationName(jop::ORG_NAME);
|
|
|
|
QCoreApplication::setOrganizationDomain(jop::ORG_DOMAIN);
|
|
|
|
QCoreApplication::setApplicationName(jop::APP_NAME);
|
2016-12-27 21:25:07 +01:00
|
|
|
|
2017-01-10 18:05:58 +01:00
|
|
|
qDebug() << "Config dir:" << paths::configDir();
|
|
|
|
qDebug() << "Database file:" << paths::databaseFile();
|
|
|
|
|
|
|
|
jop::db().initialize(paths::databaseFile());
|
|
|
|
|
2017-01-05 23:54:13 +01:00
|
|
|
Settings::initialize();
|
|
|
|
|
2016-12-27 21:25:07 +01:00
|
|
|
Settings settings;
|
|
|
|
|
2017-01-11 00:33:56 +01:00
|
|
|
if (!settings.contains("clientId")) {
|
|
|
|
// Client ID should be unique per instance of a program
|
|
|
|
settings.setValue("clientId", uuid::createUuid());
|
|
|
|
}
|
|
|
|
|
2016-12-11 16:09:39 +00:00
|
|
|
view_.setResizeMode(QQuickView::SizeRootObjectToView);
|
|
|
|
QQmlContext *ctxt = view_.rootContext();
|
|
|
|
ctxt->setContextProperty("folderListModel", &folderModel_);
|
2016-12-12 22:33:33 +00:00
|
|
|
ctxt->setContextProperty("noteListModel", ¬eModel_);
|
2016-12-14 21:50:26 +00:00
|
|
|
ctxt->setContextProperty("noteModel", &selectedQmlNote_);
|
2017-01-11 00:28:51 +01:00
|
|
|
ctxt->setContextProperty("dispatcher", &dispatcher());
|
2016-12-11 16:09:39 +00:00
|
|
|
|
2017-01-10 20:32:10 +01:00
|
|
|
view_.setSource(QUrl("qrc:/app.qml"));
|
2016-12-11 16:09:39 +00:00
|
|
|
|
|
|
|
QObject* rootObject = (QObject*)view_.rootObject();
|
|
|
|
|
|
|
|
connect(rootObject, SIGNAL(currentFolderChanged()), this, SLOT(view_currentFolderChanged()));
|
2016-12-14 21:50:26 +00:00
|
|
|
connect(rootObject, SIGNAL(currentNoteChanged()), this, SLOT(view_currentNoteChanged()));
|
2016-12-31 10:48:18 +01:00
|
|
|
connect(rootObject, SIGNAL(addFolderButtonClicked()), this, SLOT(view_addFolderButtonClicked()));
|
2017-01-10 19:59:12 +01:00
|
|
|
connect(rootObject, SIGNAL(syncButtonClicked()), this, SLOT(view_syncButtonClicked()));
|
2016-12-11 16:09:39 +00:00
|
|
|
|
2017-01-05 18:59:01 +01:00
|
|
|
connect(&dispatcher(), SIGNAL(folderCreated(QString)), this, SLOT(dispatcher_folderCreated(QString)));
|
|
|
|
connect(&dispatcher(), SIGNAL(folderUpdated(QString)), this, SLOT(dispatcher_folderUpdated(QString)));
|
|
|
|
connect(&dispatcher(), SIGNAL(folderDeleted(QString)), this, SLOT(dispatcher_folderDeleted(QString)));
|
2017-01-11 00:28:51 +01:00
|
|
|
connect(&dispatcher(), SIGNAL(loginClicked(QString,QString,QString)), this, SLOT(dispatcher_loginClicked(QString,QString,QString)));
|
2016-12-27 21:25:07 +01:00
|
|
|
|
2017-01-05 18:59:01 +01:00
|
|
|
view_.show();
|
2016-12-27 21:25:07 +01:00
|
|
|
|
2017-01-10 19:59:12 +01:00
|
|
|
synchronizerTimer_.setInterval(1000 * 120);
|
2017-01-05 18:59:01 +01:00
|
|
|
synchronizerTimer_.start();
|
2016-12-31 10:48:18 +01:00
|
|
|
|
2017-01-05 18:59:01 +01:00
|
|
|
connect(&synchronizerTimer_, SIGNAL(timeout()), this, SLOT(synchronizerTimer_timeout()));
|
2016-12-31 10:48:18 +01:00
|
|
|
|
2017-01-05 18:59:01 +01:00
|
|
|
connect(&api_, SIGNAL(requestDone(const QJsonObject&, const QString&)), this, SLOT(api_requestDone(const QJsonObject&, const QString&)));
|
2016-12-31 10:48:18 +01:00
|
|
|
|
2017-01-11 00:28:51 +01:00
|
|
|
if (!settings.contains("user.email") || !settings.contains("session.id") || !settings.contains("api.baseUrl")) {
|
|
|
|
view_.showPage("login");
|
|
|
|
} else {
|
|
|
|
view_.showPage("main");
|
|
|
|
}
|
|
|
|
|
2017-01-06 23:09:10 +01:00
|
|
|
// Don't store password, store session ID
|
2017-01-10 20:32:10 +01:00
|
|
|
// QString clientId = "B6E12222B6E12222";
|
|
|
|
// if (!settings.contains("user.email")) {
|
|
|
|
// settings.setValue("user.email", "laurent@cozic.net");
|
|
|
|
// settings.setValue("user.password", "12345678");
|
|
|
|
// }
|
|
|
|
|
|
|
|
// QUrlQuery postData;
|
|
|
|
// postData.addQueryItem("email", settings.value("user.email").toString());
|
|
|
|
// postData.addQueryItem("password", settings.value("user.password").toString());
|
|
|
|
// postData.addQueryItem("client_id", clientId);
|
2017-01-11 00:28:51 +01:00
|
|
|
// api_.post("sessions", QUrlQuery(), postData, "getSession");
|
|
|
|
}
|
|
|
|
|
|
|
|
void Application::login(const QString &email, const QString &password) {
|
2017-01-11 00:33:56 +01:00
|
|
|
Settings settings;
|
2017-01-11 00:28:51 +01:00
|
|
|
QUrlQuery postData;
|
|
|
|
postData.addQueryItem("email", email);
|
|
|
|
postData.addQueryItem("password", password);
|
2017-01-11 00:33:56 +01:00
|
|
|
postData.addQueryItem("client_id", settings.value("clientId").toString());
|
2017-01-11 00:28:51 +01:00
|
|
|
api_.post("sessions", QUrlQuery(), postData, "getSession");
|
|
|
|
}
|
|
|
|
|
2016-12-27 21:25:07 +01:00
|
|
|
void Application::api_requestDone(const QJsonObject& response, const QString& tag) {
|
|
|
|
// TODO: handle errors
|
2017-01-06 23:09:10 +01:00
|
|
|
// Handle expired sessions
|
2016-12-27 21:25:07 +01:00
|
|
|
|
|
|
|
if (tag == "getSession") {
|
2017-01-11 00:28:51 +01:00
|
|
|
if (response.contains("error")) {
|
|
|
|
qCritical() << "Could not get session:" << response.value("error").toString();
|
|
|
|
dispatcher().emitLoginFailed();
|
|
|
|
} else {
|
|
|
|
QString sessionId = response.value("id").toString();
|
|
|
|
qDebug() << "Got session" << sessionId;
|
|
|
|
Settings settings;
|
|
|
|
settings.setValue("session.id", sessionId);
|
|
|
|
afterSessionInitialization();
|
|
|
|
dispatcher().emitLoginSuccess();
|
|
|
|
}
|
|
|
|
view_.showPage("main");
|
2016-12-27 21:25:07 +01:00
|
|
|
return;
|
|
|
|
}
|
2016-12-11 16:09:39 +00:00
|
|
|
}
|
|
|
|
|
2017-01-05 18:59:01 +01:00
|
|
|
void Application::dispatcher_folderCreated(const QString &folderId) {
|
|
|
|
qDebug() << "Folder created" << folderId;
|
2017-01-07 10:40:13 +01:00
|
|
|
//synchronizerTimer_.start(1000 * 3);
|
2017-01-05 18:59:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Application::dispatcher_folderUpdated(const QString &folderId) {
|
|
|
|
qDebug() << "Folder udpated" << folderId;
|
2017-01-07 10:40:13 +01:00
|
|
|
//synchronizerTimer_.start(1000 * 3);
|
2017-01-05 18:59:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Application::dispatcher_folderDeleted(const QString &folderId) {
|
|
|
|
qDebug() << "Folder deleted" << folderId;
|
2017-01-07 10:40:13 +01:00
|
|
|
//synchronizerTimer_.start(1000 * 3);
|
2017-01-05 18:59:01 +01:00
|
|
|
}
|
|
|
|
|
2017-01-11 00:28:51 +01:00
|
|
|
void Application::dispatcher_loginClicked(const QString &apiBaseUrl, const QString &email, const QString &password) {
|
|
|
|
qDebug() << apiBaseUrl << email << password;
|
|
|
|
dispatcher().emitLoginStarted();
|
|
|
|
|
|
|
|
Settings settings;
|
|
|
|
settings.setValue("user.email", email);
|
|
|
|
settings.setValue("api.baseUrl", apiBaseUrl);
|
|
|
|
|
|
|
|
api_.setBaseUrl(apiBaseUrl);
|
|
|
|
|
|
|
|
login(email, password);
|
|
|
|
}
|
|
|
|
|
2017-01-05 18:59:01 +01:00
|
|
|
void Application::synchronizerTimer_timeout() {
|
2017-01-07 10:40:13 +01:00
|
|
|
//synchronizerTimer_.start(1000 * 10);
|
2017-01-05 18:59:01 +01:00
|
|
|
synchronizer_.start();
|
|
|
|
}
|
|
|
|
|
2016-12-23 19:04:26 +01:00
|
|
|
QString Application::selectedFolderId() const {
|
2016-12-11 16:09:39 +00:00
|
|
|
QObject* rootObject = (QObject*)view_.rootObject();
|
|
|
|
|
|
|
|
int index = rootObject->property("currentFolderIndex").toInt();
|
|
|
|
QModelIndex modelIndex = folderModel_.index(index);
|
2016-12-23 19:04:26 +01:00
|
|
|
return folderModel_.data(modelIndex, FolderModel::IdRole).toString();
|
2016-12-11 16:09:39 +00:00
|
|
|
}
|
|
|
|
|
2016-12-23 19:04:26 +01:00
|
|
|
QString Application::selectedNoteId() const {
|
2016-12-14 21:50:26 +00:00
|
|
|
QObject* rootObject = (QObject*)view_.rootObject();
|
|
|
|
|
|
|
|
int index = rootObject->property("currentNoteIndex").toInt();
|
|
|
|
QModelIndex modelIndex = noteModel_.index(index);
|
2016-12-23 19:04:26 +01:00
|
|
|
return noteModel_.data(modelIndex, NoteModel::IdRole).toString();
|
2016-12-14 21:50:26 +00:00
|
|
|
}
|
|
|
|
|
2016-12-27 21:25:07 +01:00
|
|
|
void Application::afterSessionInitialization() {
|
|
|
|
// TODO: rather than saving the session id, save the username/password and
|
|
|
|
// request a new session everytime on startup.
|
|
|
|
|
|
|
|
Settings settings;
|
2017-01-11 00:28:51 +01:00
|
|
|
QString sessionId = settings.value("session.id").toString();
|
2016-12-27 21:25:07 +01:00
|
|
|
qDebug() << "Session:" << sessionId;
|
|
|
|
api_.setSessionId(sessionId);
|
2017-01-05 18:59:01 +01:00
|
|
|
synchronizer_.setSessionId(sessionId);
|
2017-01-02 16:28:03 +01:00
|
|
|
synchronizer_.start();
|
2016-12-27 21:25:07 +01:00
|
|
|
}
|
|
|
|
|
2016-12-11 16:09:39 +00:00
|
|
|
void Application::view_currentFolderChanged() {
|
2017-01-02 13:17:15 +01:00
|
|
|
// QString folderId = selectedFolderId();
|
|
|
|
// noteCollection_ = NoteCollection(db_, folderId, "title ASC");
|
|
|
|
// noteModel_.setCollection(noteCollection_);
|
2016-12-11 16:09:39 +00:00
|
|
|
}
|
2016-12-14 21:50:26 +00:00
|
|
|
|
|
|
|
void Application::view_currentNoteChanged() {
|
2017-01-02 13:17:15 +01:00
|
|
|
// QString noteId = selectedNoteId();
|
|
|
|
// Note note = noteCollection_.byId(noteId);
|
|
|
|
// selectedQmlNote_.setNote(note);
|
2016-12-14 21:50:26 +00:00
|
|
|
}
|
2016-12-29 20:19:00 +01:00
|
|
|
|
|
|
|
void Application::view_addNoteButtonClicked() {
|
2016-12-31 10:48:18 +01:00
|
|
|
|
2016-12-29 20:19:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Application::view_addFolderButtonClicked() {
|
2016-12-31 10:48:18 +01:00
|
|
|
// QStringList fields;
|
|
|
|
// fields << "id";
|
|
|
|
// VariantVector values;
|
|
|
|
// values << uuid::createUuid();
|
|
|
|
// QSqlQuery q = db_.buildSqlQuery(Database::Insert, "folders", fields, values);
|
|
|
|
// q.exec();
|
|
|
|
|
|
|
|
// emit jop::dispatcher().folderCreated("test");
|
2016-12-29 20:19:00 +01:00
|
|
|
|
2016-12-31 10:48:18 +01:00
|
|
|
//qDebug() << "Added" << q.lastInsertId().toString();
|
2016-12-29 20:19:00 +01:00
|
|
|
}
|
2017-01-10 19:59:12 +01:00
|
|
|
|
|
|
|
void Application::view_syncButtonClicked() {
|
|
|
|
synchronizer_.start();
|
|
|
|
}
|