Browse Source

WIP: Bookmark file.

kevaview
Just Wonder 5 years ago
parent
commit
c799f67673
  1. 81
      src/qt/kevabookmarksmodel.cpp
  2. 11
      src/qt/kevabookmarksmodel.h
  3. 3
      src/qt/kevadetaildialog.cpp
  4. 4
      src/qt/kevadialog.cpp
  5. BIN
      src/qt/res/icons/star.png
  6. BIN
      src/qt/res/icons/star_empty.png
  7. 9
      src/qt/walletmodel.cpp
  8. 1
      src/qt/walletmodel.h

81
src/qt/kevabookmarksmodel.cpp

@ -8,9 +8,16 @@
#include <qt/guiutil.h> #include <qt/guiutil.h>
#include <qt/optionsmodel.h> #include <qt/optionsmodel.h>
#include <util.h>
#include <clientversion.h> #include <clientversion.h>
#include <streams.h> #include <streams.h>
#include <QFile>
#include <QJsonDocument>
#include <QJsonArray>
#include <QDir>
KevaBookmarksModel::KevaBookmarksModel(CWallet *wallet, WalletModel *parent) : KevaBookmarksModel::KevaBookmarksModel(CWallet *wallet, WalletModel *parent) :
QAbstractTableModel(parent), walletModel(parent) QAbstractTableModel(parent), walletModel(parent)
@ -126,6 +133,19 @@ void KevaBookmarksModel::setBookmarks(std::vector<BookmarkEntry> vBookmarEntries
} }
} }
void KevaBookmarksModel::setBookmarks(QJsonArray &array)
{
std::vector<BookmarkEntry> vBookmarEntries;
for (int i = 0; i < array.size(); ++i) {
QJsonObject obj = array[i].toObject();
BookmarkEntry entry;
entry.id = obj["id"].toString().toStdString();
entry.name = obj["name"].toString().toStdString();
vBookmarEntries.push_back(entry);
}
setBookmarks(std::move(vBookmarEntries));
}
void KevaBookmarksModel::sort(int column, Qt::SortOrder order) void KevaBookmarksModel::sort(int column, Qt::SortOrder order)
{ {
qSort(list.begin(), list.end(), BookmarkEntryLessThan(column, order)); qSort(list.begin(), list.end(), BookmarkEntryLessThan(column, order));
@ -133,6 +153,67 @@ void KevaBookmarksModel::sort(int column, Qt::SortOrder order)
} }
QString KevaBookmarksModel::getBookmarkFile()
{
QString dataDir = GUIUtil::boostPathToQString(GetDataDir());
return dataDir + QDir::separator() + QStringLiteral(BOOKMARK_FILE);
}
int KevaBookmarksModel::loadBookmarks()
{
QJsonArray json;
if (loadBookmarks(json)) {
setBookmarks(json);
return 1;
}
// No bookmark file. Save and load the default ones.
QJsonObject entry0;
entry0["id"] = "NgKBKkBAJMtzsuit85TpTpo5Xj6UQUg1wr";
entry0["name"] = "Kevacoin Official Blog";
// Other default entries ...
QJsonArray array;
array.append(entry0);
if (!saveBookmarks(array)) {
return 0;
}
// Load the bookmarks again.
if (loadBookmarks(json)) {
setBookmarks(json);
return 1;
}
return 0;
}
int KevaBookmarksModel::loadBookmarks(QJsonArray &json)
{
QFile loadFile(getBookmarkFile());
if (!loadFile.open(QIODevice::ReadOnly)) {
return 0;
}
QByteArray saveData = loadFile.readAll();
QJsonDocument loadDoc(QJsonDocument::fromBinaryData(saveData));
json = loadDoc.array();
return 1;
}
int KevaBookmarksModel::saveBookmarks(QJsonArray &json)
{
QFile saveFile(getBookmarkFile());
if (!saveFile.open(QIODevice::WriteOnly)) {
return 0;
}
QJsonDocument saveDoc(json);
saveFile.write(saveDoc.toBinaryData());
return 1;
}
bool BookmarkEntryLessThan::operator()(BookmarkEntry &left, BookmarkEntry &right) const bool BookmarkEntryLessThan::operator()(BookmarkEntry &left, BookmarkEntry &right) const
{ {
BookmarkEntry *pLeft = &left; BookmarkEntry *pLeft = &left;

11
src/qt/kevabookmarksmodel.h

@ -10,6 +10,10 @@
#include <QAbstractTableModel> #include <QAbstractTableModel>
#include <QStringList> #include <QStringList>
#include <QDateTime> #include <QDateTime>
#include <QJsonObject>
#include <QJsonArray>
#define BOOKMARK_FILE "bookmarks.dat"
class CWallet; class CWallet;
@ -65,6 +69,11 @@ public:
const BookmarkEntry &entry(int row) const { return list[row]; } const BookmarkEntry &entry(int row) const { return list[row]; }
void setBookmarks(std::vector<BookmarkEntry> vBookmarkEntries); void setBookmarks(std::vector<BookmarkEntry> vBookmarkEntries);
void setBookmarks(QJsonArray &json);
int loadBookmarks();
int loadBookmarks(QJsonArray &json);
int saveBookmarks(QJsonArray &json);
public Q_SLOTS: public Q_SLOTS:
void sort(int column, Qt::SortOrder order = Qt::AscendingOrder); void sort(int column, Qt::SortOrder order = Qt::AscendingOrder);
@ -74,6 +83,8 @@ private:
QStringList columns; QStringList columns;
QList<BookmarkEntry> list; QList<BookmarkEntry> list;
int64_t nReceiveRequestsMaxId; int64_t nReceiveRequestsMaxId;
QString getBookmarkFile();
}; };
#endif // BITCOIN_QT_KEVABOOKMARKSMODEL_H #endif // BITCOIN_QT_KEVABOOKMARKSMODEL_H

3
src/qt/kevadetaildialog.cpp

@ -18,7 +18,8 @@ KevaDetailDialog::KevaDetailDialog(const QModelIndex &idx, QWidget *parent) :
QModelIndex valueIdx = idx.sibling(idx.row(), KevaTableModel::Value); QModelIndex valueIdx = idx.sibling(idx.row(), KevaTableModel::Value);
setWindowTitle(tr("Value for %1").arg(keyIdx.data(Qt::DisplayRole).toString())); setWindowTitle(tr("Value for %1").arg(keyIdx.data(Qt::DisplayRole).toString()));
QString desc = valueIdx.data(Qt::DisplayRole).toString(); QString desc = valueIdx.data(Qt::DisplayRole).toString();
ui->detailText->setHtml(desc); //ui->detailText->setHtml(desc);
ui->detailText->setPlainText(desc);
} }
KevaDetailDialog::~KevaDetailDialog() KevaDetailDialog::~KevaDetailDialog()

4
src/qt/kevadialog.cpp

@ -179,9 +179,7 @@ void KevaDialog::on_bookmarksButton_clicked()
KevaBookmarksDialog *dialog = new KevaBookmarksDialog(this); KevaBookmarksDialog *dialog = new KevaBookmarksDialog(this);
std::vector<BookmarkEntry> vBookmarkEntries; model->getKevaBookmarksModel()->loadBookmarks();
model->getKevaBookmarkEntries(vBookmarkEntries);
model->getKevaBookmarksModel()->setBookmarks(std::move(vBookmarkEntries));
model->getKevaBookmarksModel()->sort(KevaBookmarksModel::Name, Qt::DescendingOrder); model->getKevaBookmarksModel()->sort(KevaBookmarksModel::Name, Qt::DescendingOrder);
dialog->setModel(model); dialog->setModel(model);

BIN
src/qt/res/icons/star.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

BIN
src/qt/res/icons/star_empty.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 63 KiB

9
src/qt/walletmodel.cpp

@ -889,15 +889,6 @@ void WalletModel::getNamespaceEntries(std::vector<NamespaceEntry>& vNamespaceEnt
} }
} }
void WalletModel::getKevaBookmarkEntries(std::vector<BookmarkEntry>& vBookmarkEntries)
{
BookmarkEntry entry;
entry.id = "NgKBKkBAJMtzsuit85TpTpo5Xj6UQUg1wr";
entry.name = "Kevacoin Official Blog";
vBookmarkEntries.push_back(std::move(entry));
}
int WalletModel::createNamespace(std::string displayNameStr, std::string& namespaceId) int WalletModel::createNamespace(std::string displayNameStr, std::string& namespaceId)
{ {
const valtype displayName = ValtypeFromString (displayNameStr); const valtype displayName = ValtypeFromString (displayNameStr);

1
src/qt/walletmodel.h

@ -238,7 +238,6 @@ public:
// Keva // Keva
void getKevaEntries(std::vector<KevaEntry>& vKevaEntries, std::string nameSpace); void getKevaEntries(std::vector<KevaEntry>& vKevaEntries, std::string nameSpace);
void getNamespaceEntries(std::vector<NamespaceEntry>& vNamespaceEntries); void getNamespaceEntries(std::vector<NamespaceEntry>& vNamespaceEntries);
void getKevaBookmarkEntries(std::vector<BookmarkEntry>& vBookmarkEntries);
int createNamespace(std::string displayName, std::string& namespaceId); int createNamespace(std::string displayName, std::string& namespaceId);
int deleteKevaEntry(std::string nameSpace, std::string key); int deleteKevaEntry(std::string nameSpace, std::string key);

Loading…
Cancel
Save