1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-07-03 23:50:33 +02:00
Files
joplin/QtClient/JoplinQtClient/application.cpp

167 lines
5.3 KiB
C++
Raw Normal View History

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"
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()),
2016-12-29 20:19:00 +01:00
api_("http://joplin.local"),
2017-01-05 18:59:01 +01:00
synchronizer_(api_.baseUrl(), db_),
2016-12-29 20:19:00 +01:00
folderModel_(db_)
{
2017-01-06 23:09:10 +01:00
QString dbPath = "D:/Web/www/joplin/QtClient/data/notes.sqlite";
jop::db().initialize(dbPath);
2017-01-01 23:03:42 +01:00
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.
QCoreApplication::setOrganizationName("Cozic");
QCoreApplication::setOrganizationDomain("cozic.net");
QCoreApplication::setApplicationName("Joplin");
2017-01-05 23:54:13 +01:00
Settings::initialize();
2016-12-27 21:25:07 +01:00
Settings settings;
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", &noteModel_);
2016-12-14 21:50:26 +00:00
ctxt->setContextProperty("noteModel", &selectedQmlNote_);
2016-12-11 16:09:39 +00:00
view_.setSource(QUrl("qrc:/main.qml"));
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()));
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)));
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-07 10:40:13 +01:00
synchronizerTimer_.setInterval(1000 * 10);
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-06 23:09:10 +01:00
// Don't store password, store session ID
QString clientId = "B6E12222B6E12222";
if (!settings.contains("user.email")) {
settings.setValue("user.email", "laurent@cozic.net");
settings.setValue("user.password", "12345678");
}
2017-01-05 18:59:01 +01:00
QUrlQuery postData;
2017-01-06 23:09:10 +01:00
postData.addQueryItem("email", settings.value("user.email").toString());
postData.addQueryItem("password", settings.value("user.password").toString());
postData.addQueryItem("client_id", clientId);
2017-01-05 18:59:01 +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") {
QString sessionId = response.value("id").toString();
Settings settings;
settings.setValue("sessionId", sessionId);
afterSessionInitialization();
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
}
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;
QString sessionId = settings.value("sessionId").toString();
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() {
// 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() {
// 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
}