diff --git a/src/qt/kevabookmarksmodel.cpp b/src/qt/kevabookmarksmodel.cpp index 9e823f806..939b37b13 100644 --- a/src/qt/kevabookmarksmodel.cpp +++ b/src/qt/kevabookmarksmodel.cpp @@ -146,6 +146,16 @@ void KevaBookmarksModel::setBookmarks(QJsonArray &array) setBookmarks(std::move(vBookmarEntries)); } +bool KevaBookmarksModel::isBookmarked(std::string& ns) +{ + for (int i = 0; i < list.size(); ++i) { + if (list.at(i).id == ns) { + return true; + } + } + return false; +} + void KevaBookmarksModel::sort(int column, Qt::SortOrder order) { qSort(list.begin(), list.end(), BookmarkEntryLessThan(column, order)); diff --git a/src/qt/kevabookmarksmodel.h b/src/qt/kevabookmarksmodel.h index b2821b3a1..9c7d428a9 100644 --- a/src/qt/kevabookmarksmodel.h +++ b/src/qt/kevabookmarksmodel.h @@ -74,6 +74,7 @@ public: int loadBookmarks(); int loadBookmarks(QJsonArray &json); int saveBookmarks(QJsonArray &json); + bool isBookmarked(std::string& ns); public Q_SLOTS: void sort(int column, Qt::SortOrder order = Qt::AscendingOrder); diff --git a/src/qt/kevadialog.cpp b/src/qt/kevadialog.cpp index f3e5ad33f..f73853f8a 100644 --- a/src/qt/kevadialog.cpp +++ b/src/qt/kevadialog.cpp @@ -55,6 +55,9 @@ KevaDialog::KevaDialog(const PlatformStyle *_platformStyle, QWidget *parent) : QAction *copyMessageAction = new QAction(tr("Copy message"), this); QAction *copyAmountAction = new QAction(tr("Copy amount"), this); + bookmarks = new KevaBookmarksModel(NULL, NULL); + bookmarks->loadBookmarks(); + // context menu contextMenu = new QMenu(this); contextMenu->addAction(copyURIAction); @@ -147,10 +150,23 @@ void KevaDialog::onNamespaceChanged(const QString& nameSpace) { std::string namespaceStr = nameSpace.toStdString(); valtype nameSpaceVal; + bool isValidNamespace = false; if (DecodeKevaNamespace(namespaceStr, Params(), nameSpaceVal)) { ui->addKVButton->setEnabled(true); + isValidNamespace = true; } else { ui->addKVButton->setEnabled(false); + ui->bookmarkNamespace->setIcon(QIcon(":/icons/star_empty")); + } + + if (!isValidNamespace) { + return; + } + + if (bookmarks->isBookmarked(namespaceStr)) { + ui->bookmarkNamespace->setIcon(QIcon(":/icons/star")); + } else { + ui->bookmarkNamespace->setIcon(QIcon(":/icons/star_empty")); } } @@ -215,7 +231,50 @@ void KevaDialog::on_kevaView_doubleClicked(const QModelIndex &index) void KevaDialog::on_bookmarkNamespace_clicked() { - ui->bookmarkNamespace->setIcon(QIcon(":/icons/star")); + valtype namespaceVal; + QString nameSpace = ui->nameSpace->text(); + std::string namespaceStr = nameSpace.toStdString(); + if (!DecodeKevaNamespace(namespaceStr, Params(), namespaceVal)) { + return; + } + QJsonArray array; + bookmarks->loadBookmarks(array); + + int index = -1; + for (int i = 0; i < array.size(); ++i) { + QJsonObject obj = array[i].toObject(); + BookmarkEntry entry; + std::string id = obj["id"].toString().toStdString(); + if (id == namespaceStr) { + index = i; + break; + } + } + + if (index >= 0) { + // Remove bookmark + array.removeAt(index); + ui->bookmarkNamespace->setIcon(QIcon(":/icons/star_empty")); + } else { + // Add bookmark + QJsonObject entry; + entry["id"] = namespaceStr.c_str(); + entry["name"] = ""; + + std::vector vNamespaceEntries; + this->model->getNamespaceEntries(vNamespaceEntries); + for(auto it = vNamespaceEntries.begin(); it != vNamespaceEntries.end(); it++) { + if ((*it).id == namespaceStr) { + entry["name"] = (*it).name.c_str(); + break; + } + } + + array.prepend(entry); + ui->bookmarkNamespace->setIcon(QIcon(":/icons/star")); + } + + bookmarks->saveBookmarks(array); } void KevaDialog::kevaView_selectionChanged() diff --git a/src/qt/kevadialog.h b/src/qt/kevadialog.h index 75a0dabb8..2820ca310 100644 --- a/src/qt/kevadialog.h +++ b/src/qt/kevadialog.h @@ -17,6 +17,7 @@ class PlatformStyle; class WalletModel; +class KevaBookmarksModel; namespace Ui { class KevaDialog; @@ -62,6 +63,7 @@ private: WalletModel *model; QMenu *contextMenu; const PlatformStyle *platformStyle; + KevaBookmarksModel* bookmarks; QModelIndex selectedRow(); void copyColumnToClipboard(int column);