Skip to content

Commit

Permalink
+ Added save current queries to user profile
Browse files Browse the repository at this point in the history
  • Loading branch information
trueromanus committed Sep 16, 2024
1 parent 79d6fa0 commit cbd6f2f
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 2 deletions.
86 changes: 86 additions & 0 deletions src/ListModels/httprequestslistmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#include <QJsonArray>
#include <QJsonObject>
#include <QJsonDocument>
#include <QFile>
#include <QStandardPaths>
#include "httprequestslistmodel.h"

HttpRequestsListModel::HttpRequestsListModel(QObject *parent)
Expand Down Expand Up @@ -80,8 +85,15 @@ QHash<int, QByteArray> HttpRequestsListModel::roleNames() const
};
}

void HttpRequestsListModel::setup(QSharedPointer<TextAdvisorViewModel> textAdviser) noexcept
{
m_textAdviser = textAdviser;
}

int HttpRequestsListModel::addItem(const HttpRequestModel* model) noexcept
{
model->requestModel()->setTextAdvisor(m_textAdviser);

beginResetModel();

m_requests->append(const_cast<HttpRequestModel*>(model));
Expand Down Expand Up @@ -120,6 +132,80 @@ QSharedPointer<QList<HttpRequestModel *> > 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);
Expand Down
7 changes: 7 additions & 0 deletions src/ListModels/httprequestslistmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class HttpRequestsListModel : public QAbstractListModel

private:
QSharedPointer<QList<HttpRequestModel*>> m_requests { new QList<HttpRequestModel*>() };
QSharedPointer<TextAdvisorViewModel> m_textAdviser;
int m_selectedIndex { 0 };

enum HttpRequestsRoles {
Expand All @@ -46,6 +47,8 @@ class HttpRequestsListModel : public QAbstractListModel
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
QHash<int, QByteArray> roleNames() const override;

void setup(QSharedPointer<TextAdvisorViewModel> textAdviser) noexcept;

HttpRequestModel* selectedItem() const noexcept { return m_requests->at(m_selectedIndex); }

int addItem(const HttpRequestModel* model) noexcept;
Expand All @@ -54,6 +57,10 @@ class HttpRequestsListModel : public QAbstractListModel

QSharedPointer<QList<HttpRequestModel*>> 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;
Expand Down
12 changes: 10 additions & 2 deletions src/ViewModels/backendviewmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
BackendViewModel::BackendViewModel(QObject *parent)
: QObject{parent}
{
m_requests->setup(m_textAdviser);

addNewRequest();

m_requestsCommandPaletter->setup(m_requests->getList());
Expand All @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions src/ViewModels/backendviewmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
8 changes: 8 additions & 0 deletions src/ViewModels/httprequestviewmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions src/ViewModels/httprequestviewmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions src/main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -248,4 +248,8 @@ ApplicationWindow {
onActiveFocusItemChanged: {
if (!window.activeFocusItem) globalEventHandler.clear();
}

Component.onDestruction: {
backend.saveCurrentRequestsToProfile();
}
}

0 comments on commit cbd6f2f

Please sign in to comment.