From cbd6f2f37fcfe863b7f8daf7e17d4d1286c163f3 Mon Sep 17 00:00:00 2001 From: Roman Vladimirov Date: Mon, 16 Sep 2024 20:00:20 +0300 Subject: [PATCH] + Added save current queries to user profile --- src/ListModels/httprequestslistmodel.cpp | 86 ++++++++++++++++++++++++ src/ListModels/httprequestslistmodel.h | 7 ++ src/ViewModels/backendviewmodel.cpp | 12 +++- src/ViewModels/backendviewmodel.h | 1 + src/ViewModels/httprequestviewmodel.cpp | 8 +++ src/ViewModels/httprequestviewmodel.h | 1 + src/main.qml | 4 ++ 7 files changed, 117 insertions(+), 2 deletions(-) diff --git a/src/ListModels/httprequestslistmodel.cpp b/src/ListModels/httprequestslistmodel.cpp index 608428e..0781a75 100644 --- a/src/ListModels/httprequestslistmodel.cpp +++ b/src/ListModels/httprequestslistmodel.cpp @@ -13,6 +13,11 @@ along with this program. If not, see . */ +#include +#include +#include +#include +#include #include "httprequestslistmodel.h" HttpRequestsListModel::HttpRequestsListModel(QObject *parent) @@ -80,8 +85,15 @@ QHash HttpRequestsListModel::roleNames() const }; } +void HttpRequestsListModel::setup(QSharedPointer textAdviser) noexcept +{ + m_textAdviser = textAdviser; +} + int HttpRequestsListModel::addItem(const HttpRequestModel* model) noexcept { + model->requestModel()->setTextAdvisor(m_textAdviser); + beginResetModel(); m_requests->append(const_cast(model)); @@ -120,6 +132,80 @@ QSharedPointer > HttpRequestsListModel::getList() cons return m_requests; } +QString HttpRequestsListModel::getProfilePath() const noexcept +{ + return QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + "/ardorquery.profile"; +} + +void HttpRequestsListModel::saveToProfile() const noexcept +{ + QJsonArray array; + foreach (auto request, *m_requests) { + auto requestModel = request->requestModel(); + auto lines = requestModel->getAllFieldsAsList(); + + if (lines.isEmpty()) continue; // not make sense save empty queries + if (lines.size() == 1 && lines.value(0).isEmpty()) continue; // not make sense save first query + + QJsonArray linesArray; + foreach (auto line, lines) { + linesArray.append(line); + } + QJsonObject item; + item["lines"] = linesArray; + array.append(item); + } + + QJsonDocument document(array); + auto json = document.toJson(); + + auto path = getProfilePath(); + QFile profileFile(path); + if (!profileFile.open(QFile::WriteOnly | QFile::Text)) return; + + profileFile.write(json); + profileFile.close(); +} + +void HttpRequestsListModel::loadFromProfile() const noexcept +{ + auto path = getProfilePath(); + if (!QFile::exists(path)) return; + + QFile profileFile(path); + if (!profileFile.open(QFile::ReadOnly | QFile::Text)) return; + + auto json = profileFile.readAll(); + profileFile.close(); + + auto document = QJsonDocument::fromJson(json); + auto array = document.array(); + foreach (auto arrayItem, array) { + auto item = arrayItem.toObject(); + if (!item.contains("lines")) continue; + + auto lines = item.value("lines").toArray(); + if (lines.isEmpty()) continue; + + auto model = new HttpRequestModel(parent()); + + auto request = model->requestModel(); + request->setTextAdvisor(m_textAdviser); + + if (!lines.isEmpty()) request->removeFirstItem(); + + foreach (auto line, lines) { + request->addRawLine(line.toString()); + } + request->setSelectedItem(0); + + m_requests->append(model); + } + + //if we load something from profile then need to remove first empty query + if (m_requests->size() > 1) m_requests->removeAt(0); +} + HttpRequestModel *HttpRequestsListModel::getSelectedRequest() const noexcept { return m_requests->value(m_selectedIndex); diff --git a/src/ListModels/httprequestslistmodel.h b/src/ListModels/httprequestslistmodel.h index 79f0801..6c48d6c 100644 --- a/src/ListModels/httprequestslistmodel.h +++ b/src/ListModels/httprequestslistmodel.h @@ -29,6 +29,7 @@ class HttpRequestsListModel : public QAbstractListModel private: QSharedPointer> m_requests { new QList() }; + QSharedPointer m_textAdviser; int m_selectedIndex { 0 }; enum HttpRequestsRoles { @@ -46,6 +47,8 @@ class HttpRequestsListModel : public QAbstractListModel QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; QHash roleNames() const override; + void setup(QSharedPointer textAdviser) noexcept; + HttpRequestModel* selectedItem() const noexcept { return m_requests->at(m_selectedIndex); } int addItem(const HttpRequestModel* model) noexcept; @@ -54,6 +57,10 @@ class HttpRequestsListModel : public QAbstractListModel QSharedPointer> getList() const noexcept; + QString getProfilePath() const noexcept; + void saveToProfile() const noexcept; + void loadFromProfile() const noexcept; + HttpRequestModel* getSelectedRequest() const noexcept; Q_INVOKABLE void selectItem(const int newIndex) noexcept; diff --git a/src/ViewModels/backendviewmodel.cpp b/src/ViewModels/backendviewmodel.cpp index c892084..410ba88 100644 --- a/src/ViewModels/backendviewmodel.cpp +++ b/src/ViewModels/backendviewmodel.cpp @@ -22,6 +22,8 @@ BackendViewModel::BackendViewModel(QObject *parent) : QObject{parent} { + m_requests->setup(m_textAdviser); + addNewRequest(); m_requestsCommandPaletter->setup(m_requests->getList()); @@ -33,6 +35,9 @@ BackendViewModel::BackendViewModel(QObject *parent) fillMappings(); fillCommands(); fillHelpShortcuts(); + + m_requests->loadFromProfile(); // load profile if exists + m_requestsCommandPaletter->recreateHistory(); // recreate history if profile not empty } void BackendViewModel::setFocusedHelpTextField(bool focusedHelpTextField) noexcept @@ -48,7 +53,6 @@ void BackendViewModel::addNewRequest(bool forceSelectedAddedItem) auto model = new HttpRequestModel(this); auto request = model->requestModel(); - request->setTextAdvisor(m_textAdviser); request->setSelectedItem(0); // select first empty field for new request m_requests->addItem(model); @@ -287,7 +291,6 @@ void BackendViewModel::importFromOpenApi(int index, bool replaceCurrent) noexcep } auto request = model->requestModel(); - request->setTextAdvisor(m_textAdviser); auto summary = route->summary(); if (!summary.isEmpty()) request->addItem(-1, HttpRequestViewModel::HttpRequestTypes::TitleType, route->summary()); @@ -366,6 +369,11 @@ void BackendViewModel::closeGlobalVariables() noexcept emit selectedGlobalVariableChanged(); } +void BackendViewModel::saveCurrentRequestsToProfile() noexcept +{ + m_requests->saveToProfile(); +} + void BackendViewModel::deleteCurrentRequest() noexcept { if (m_requests->singleRequest()) addNewRequest(); diff --git a/src/ViewModels/backendviewmodel.h b/src/ViewModels/backendviewmodel.h index 5f7d8ee..401bac0 100644 --- a/src/ViewModels/backendviewmodel.h +++ b/src/ViewModels/backendviewmodel.h @@ -137,6 +137,7 @@ class BackendViewModel : public QObject Q_INVOKABLE void importFromOpenApi(int index, bool replaceCurrent) noexcept; Q_INVOKABLE void saveDownloadedFile(const QString& fileName) noexcept; Q_INVOKABLE void closeGlobalVariables() noexcept; + Q_INVOKABLE void saveCurrentRequestsToProfile() noexcept; void deleteCurrentRequest() noexcept; diff --git a/src/ViewModels/httprequestviewmodel.cpp b/src/ViewModels/httprequestviewmodel.cpp index 8d23ac8..bcf8aac 100644 --- a/src/ViewModels/httprequestviewmodel.cpp +++ b/src/ViewModels/httprequestviewmodel.cpp @@ -179,6 +179,14 @@ void HttpRequestViewModel::addItem(const int position, const HttpRequestViewMode setSelectedItem(actualPosition); } +void HttpRequestViewModel::addRawLine(const QString line) +{ + auto item = new HttpRequestItem(); + m_items->append(item); + + setItemContent(m_items->size() - 1, line); +} + void HttpRequestViewModel::removeFirstItem() { beginResetModel(); diff --git a/src/ViewModels/httprequestviewmodel.h b/src/ViewModels/httprequestviewmodel.h index cabb047..4a8d7e4 100644 --- a/src/ViewModels/httprequestviewmodel.h +++ b/src/ViewModels/httprequestviewmodel.h @@ -85,6 +85,7 @@ class HttpRequestViewModel : public QAbstractListModel void redrawAllItems() noexcept; Q_INVOKABLE void addItem(const int position, const HttpRequestViewModel::HttpRequestTypes itemType = HttpRequestTypes::UnknownType, const QString initialValue = "", const QString& alias = ""); + Q_INVOKABLE void addRawLine(const QString line); Q_INVOKABLE void removeFirstItem(); Q_INVOKABLE void refreshItem(const int position, const QString& content); Q_INVOKABLE void setItemContent(const int position, const QString& content); diff --git a/src/main.qml b/src/main.qml index bc5c243..4a11efc 100644 --- a/src/main.qml +++ b/src/main.qml @@ -248,4 +248,8 @@ ApplicationWindow { onActiveFocusItemChanged: { if (!window.activeFocusItem) globalEventHandler.clear(); } + + Component.onDestruction: { + backend.saveCurrentRequestsToProfile(); + } }