From 456604040ce9cf3c22b726e8ef279a1fe4a85f39 Mon Sep 17 00:00:00 2001 From: Roman Plášil Date: Wed, 23 Aug 2017 14:49:06 +0800 Subject: Use QUrl for state save file, create directory if it doesn't exist --- connection.cpp | 28 ++++++++++++++++++---------- connection.h | 6 +++--- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/connection.cpp b/connection.cpp index 4d76f7cd..136ccda1 100644 --- a/connection.cpp +++ b/connection.cpp @@ -34,6 +34,8 @@ #include #include #include +#include +#include using namespace QMatrixClient; @@ -59,7 +61,7 @@ class Connection::Private QString userId; SyncJob* syncJob; - QString stateSaveFile; + QUrl stateSaveFile; }; Connection::Connection(const QUrl& server, QObject* parent) @@ -329,15 +331,17 @@ QByteArray Connection::generateTxnId() return d->data->generateTxnId(); } -QString Connection::getStateSaveFile() const { +QUrl Connection::getStateSaveFile() const { return d->stateSaveFile; } -void Connection::setStateSaveFile(const QString &path) { +void Connection::setStateSaveFile(const QUrl &path) { d->stateSaveFile = path; } void Connection::saveState() { + if (getStateSaveFile().isEmpty()) return; + QJsonObject rooms; for (auto i : this->roomMap()) { @@ -359,17 +363,21 @@ void Connection::saveState() { QJsonDocument doc { rootObj }; QByteArray data = doc.toJson(); - QString filepath = getStateSaveFile(); - if (filepath.isEmpty()) return; + QFileInfo stateFile { getStateSaveFile().toLocalFile() }; + QFile outfile { stateFile.absoluteFilePath() }; + if (!stateFile.dir().exists()) stateFile.dir().mkpath("."); + + if (outfile.open(QFile::WriteOnly)) { + qCDebug(MAIN) << "Writing state to file=" << outfile.fileName(); + outfile.write(data.data(), data.size()); - QFile outfile { filepath }; - outfile.open(QFile::WriteOnly); - qCDebug(MAIN) << "Writing state to file=" << outfile.fileName(); - outfile.write(data.data(), data.size()); + } else { + qCWarning(MAIN) << outfile.errorString(); + } } void Connection::loadState() { - QFile file { getStateSaveFile() }; + QFile file { getStateSaveFile().toLocalFile() }; if (!file.exists()) return; file.open(QFile::ReadOnly); QByteArray data = file.readAll(); diff --git a/connection.h b/connection.h index 063018f9..5a5ce3ac 100644 --- a/connection.h +++ b/connection.h @@ -39,7 +39,7 @@ namespace QMatrixClient class Connection: public QObject { Q_OBJECT - Q_PROPERTY(QString stateSaveFile READ getStateSaveFile WRITE setStateSaveFile) + Q_PROPERTY(QUrl stateSaveFile READ getStateSaveFile WRITE setStateSaveFile) public: explicit Connection(const QUrl& server, QObject* parent = nullptr); Connection(); @@ -148,8 +148,8 @@ namespace QMatrixClient /** * Returns the path to file for saving state (rooms, presence, ...) */ - QString getStateSaveFile() const; - void setStateSaveFile(const QString &path); + QUrl getStateSaveFile() const; + void setStateSaveFile(const QUrl &path); /** * Completes loading sync data. -- cgit v1.2.3