preserve the binary content of keys and bucket names during edit operations

This commit is contained in:
mappu 2017-06-19 20:27:05 +12:00
parent 21588021d3
commit 26f7a11d80
1 changed files with 26 additions and 8 deletions

View File

@ -35,6 +35,8 @@ MainWindow::~MainWindow()
} }
static const int BdbPointerRole = Qt::UserRole + 1; static const int BdbPointerRole = Qt::UserRole + 1;
static const int BinaryDataRole = Qt::UserRole + 2;
#define SET_BDB(top, bdb) top->setData(0, BdbPointerRole, QVariant::fromValue<void*>(static_cast<void*>(bdb))) #define SET_BDB(top, bdb) top->setData(0, BdbPointerRole, QVariant::fromValue<void*>(static_cast<void*>(bdb)))
#define GET_BDB(top) static_cast<BoltDB*>( top->data(0, BdbPointerRole).value<void*>() ) #define GET_BDB(top) static_cast<BoltDB*>( top->data(0, BdbPointerRole).value<void*>() )
@ -86,6 +88,12 @@ void MainWindow::openDatabase(QString file, bool readOnly)
ui->bucketTree->expandItem(top); ui->bucketTree->expandItem(top);
} }
static const QString getDisplayName(const QByteArray &qba) {
// FIXME the formatting isn't so great when control characters, etc. are used
// A C-style escape display, or the unicode-replacement-character would be preferable
return QString::fromUtf8(qba);
}
void MainWindow::refreshBucketTree(QTreeWidgetItem* itm) void MainWindow::refreshBucketTree(QTreeWidgetItem* itm)
{ {
QTreeWidgetItem *top = itm; QTreeWidgetItem *top = itm;
@ -108,7 +116,8 @@ void MainWindow::refreshBucketTree(QTreeWidgetItem* itm)
error, error,
[=](QByteArray qba){ [=](QByteArray qba){
QTreeWidgetItem *child = new QTreeWidgetItem(); QTreeWidgetItem *child = new QTreeWidgetItem();
child->setText(0, QString::fromUtf8(qba)); child->setText(0, getDisplayName(qba));
child->setData(0, BinaryDataRole, qba);
child->setIcon(0, QIcon(":/rsrc/table.png")); child->setIcon(0, QIcon(":/rsrc/table.png"));
itm->addChild(child); itm->addChild(child);
@ -257,7 +266,8 @@ void MainWindow::refreshData(BoltDB *bdb, QStringList browse)
QString err; QString err;
bool ok = bdb->listKeys(browse, err, [=](QByteArray name, int64_t dataLen) { bool ok = bdb->listKeys(browse, err, [=](QByteArray name, int64_t dataLen) {
auto *itm = new QTreeWidgetItem(); auto *itm = new QTreeWidgetItem();
itm->setText(0, QString::fromUtf8(name)); itm->setText(0, getDisplayName(name));
itm->setData(0, BinaryDataRole, name);
itm->setText(1, QString("%1").arg(dataLen)); itm->setText(1, QString("%1").arg(dataLen));
ui->bucketData->addTopLevelItem(itm); ui->bucketData->addTopLevelItem(itm);
}); });
@ -320,15 +330,15 @@ void MainWindow::on_bucketData_doubleClicked(const QModelIndex &index)
// Get item key // Get item key
auto model = index.model(); auto model = index.model();
QString key = model->data(model->index(index.row(), 0), 0).toString(); const QByteArray& key = model->data(model->index(index.row(), 0), BinaryDataRole).toByteArray();
// DB lookup // DB lookup
bdb->getData( bdb->getData(
browse, browse,
key.toUtf8(), key,
[=](QByteArray content) { [=](QByteArray content) {
openEditor(bdb, browse, key.toUtf8(), content); openEditor(bdb, browse, key, content);
}, },
[=](QString error) { [=](QString error) {
QMessageBox qmb; QMessageBox qmb;
@ -369,8 +379,16 @@ void MainWindow::on_actionDelete_bucket_triggered()
GET_ITM_TOP_BROWSE_BDB; GET_ITM_TOP_BROWSE_BDB;
// Prompt for confirmation // Prompt for confirmation
QString bucketToDelete = itm->text(0); const QByteArray& bucketToDelete = itm->data(0, BinaryDataRole).toByteArray();
if (QMessageBox::question(this, tr("Delete bucket"), tr("Are you sure you want to remove the bucket '%1'?").arg(bucketToDelete), QMessageBox::Yes, QMessageBox::Cancel) != QMessageBox::Yes) { if (
QMessageBox::question(
this,
tr("Delete bucket"),
tr("Are you sure you want to remove the bucket '%1'?").arg(getDisplayName(bucketToDelete)),
QMessageBox::Yes,
QMessageBox::Cancel
) != QMessageBox::Yes
) {
return; return;
} }
@ -381,7 +399,7 @@ void MainWindow::on_actionDelete_bucket_triggered()
browse.pop_back(); browse.pop_back();
QString err; QString err;
if (! bdb->deleteBucket(browse, bucketToDelete.toUtf8(), err)) { if (! bdb->deleteBucket(browse, bucketToDelete, err)) {
QMessageBox qmb; QMessageBox qmb;
qmb.setText(tr("Error removing bucket: %1").arg(err)); qmb.setText(tr("Error removing bucket: %1").arg(err));
qmb.exec(); qmb.exec();