2017-05-16 07:34:54 +00:00
|
|
|
#include "mainwindow.h"
|
|
|
|
#include "ui_mainwindow.h"
|
|
|
|
|
2017-05-20 02:57:51 +00:00
|
|
|
#include "boltdb.h"
|
2017-05-16 07:34:54 +00:00
|
|
|
|
|
|
|
#include <QFileDialog>
|
|
|
|
#include <QMessageBox>
|
|
|
|
|
|
|
|
MainWindow::MainWindow(QWidget *parent) :
|
|
|
|
QMainWindow(parent),
|
|
|
|
ui(new Ui::MainWindow)
|
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
2017-05-20 23:47:16 +00:00
|
|
|
|
|
|
|
on_bucketTree_currentItemChanged(nullptr, nullptr);
|
|
|
|
|
|
|
|
databaseContext = new QMenu();
|
|
|
|
databaseContext->addAction(ui->actionRefresh_buckets);
|
|
|
|
databaseContext->addAction(ui->actionDisconnect);
|
|
|
|
|
|
|
|
bucketContext = new QMenu();
|
|
|
|
bucketContext->addAction(ui->actionDelete_bucket);
|
2017-05-16 07:34:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MainWindow::~MainWindow()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
2017-05-20 23:47:16 +00:00
|
|
|
static const int BdbPointerRole = Qt::UserRole + 1;
|
|
|
|
#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*>() )
|
|
|
|
|
2017-05-21 00:39:55 +00:00
|
|
|
/*
|
|
|
|
static BoltDB* GET_BDB(QTreeWidgetItem *top) {
|
|
|
|
while(top->parent() != nullptr) {
|
|
|
|
top = top->parent();
|
|
|
|
}
|
|
|
|
|
|
|
|
return static_cast<BoltDB*>( top->data(0, BdbPointerRole).value<void*>() );
|
|
|
|
}
|
|
|
|
*/
|
2017-05-16 07:34:54 +00:00
|
|
|
|
|
|
|
void MainWindow::on_actionOpen_database_triggered()
|
|
|
|
{
|
|
|
|
QString file = QFileDialog::getOpenFileName(this, tr("Select bolt database..."));
|
|
|
|
if (! file.length()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-20 02:57:51 +00:00
|
|
|
// Open
|
|
|
|
QString error;
|
|
|
|
auto *bdb = BoltDB::createFrom(file, error);
|
|
|
|
if (bdb == nullptr) {
|
2017-05-16 07:34:54 +00:00
|
|
|
QMessageBox qmb;
|
2017-05-20 05:28:27 +00:00
|
|
|
qmb.setText(tr("Error opening database: %1").arg(error));
|
2017-05-16 07:34:54 +00:00
|
|
|
qmb.exec();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-20 03:47:05 +00:00
|
|
|
QTreeWidgetItem *top = new QTreeWidgetItem();
|
2017-05-20 23:47:16 +00:00
|
|
|
top->setText(0, QFileInfo(file).fileName());
|
2017-05-20 03:47:05 +00:00
|
|
|
top->setIcon(0, QIcon(":/rsrc/database.png"));
|
2017-05-20 23:47:16 +00:00
|
|
|
SET_BDB(top, bdb);
|
2017-05-20 03:47:05 +00:00
|
|
|
ui->bucketTree->addTopLevelItem(top);
|
|
|
|
|
2017-05-20 23:47:16 +00:00
|
|
|
refreshBucketTree(top);
|
2017-05-21 01:31:28 +00:00
|
|
|
ui->bucketTree->setCurrentItem(top);
|
2017-05-20 23:47:16 +00:00
|
|
|
}
|
|
|
|
|
2017-05-21 01:31:28 +00:00
|
|
|
void MainWindow::refreshBucketTree(QTreeWidgetItem* itm)
|
2017-05-20 23:47:16 +00:00
|
|
|
{
|
2017-05-21 01:31:28 +00:00
|
|
|
// ui->bucketTree->clearSelection();
|
|
|
|
QTreeWidgetItem *top = itm;
|
|
|
|
QStringList browsePath;
|
|
|
|
while(top->parent() != nullptr) {
|
|
|
|
browsePath.push_front(top->text(0));
|
|
|
|
top = top->parent();
|
|
|
|
}
|
2017-05-20 23:47:16 +00:00
|
|
|
|
2017-05-21 01:31:28 +00:00
|
|
|
// Remove existing children
|
|
|
|
for (int i = itm->childCount(); i --> 0;) {
|
|
|
|
delete itm->takeChild(i);
|
|
|
|
}
|
2017-05-21 01:47:46 +00:00
|
|
|
/*
|
2017-05-21 01:31:28 +00:00
|
|
|
if (browsePath.size() > 1) {
|
|
|
|
return; // only go one level deep
|
2017-05-20 23:47:16 +00:00
|
|
|
}
|
2017-05-21 01:47:46 +00:00
|
|
|
*/
|
2017-05-21 01:31:28 +00:00
|
|
|
auto *bdb = GET_BDB(top);
|
|
|
|
|
2017-05-20 23:47:16 +00:00
|
|
|
QString error;
|
2017-05-21 01:31:28 +00:00
|
|
|
bool ok = bdb->listBuckets(
|
|
|
|
browsePath,
|
|
|
|
error,
|
|
|
|
[=](QByteArray qba){
|
|
|
|
QTreeWidgetItem *child = new QTreeWidgetItem();
|
|
|
|
child->setText(0, QString::fromUtf8(qba));
|
|
|
|
child->setIcon(0, QIcon(":/rsrc/table.png"));
|
|
|
|
itm->addChild(child);
|
|
|
|
|
|
|
|
refreshBucketTree(child);
|
|
|
|
}
|
|
|
|
);
|
2017-05-20 05:28:27 +00:00
|
|
|
|
|
|
|
if (!ok) {
|
|
|
|
QMessageBox qmb;
|
|
|
|
qmb.setText(tr("Error listing buckets: %1").arg(error));
|
|
|
|
qmb.exec();
|
|
|
|
// (continue)
|
|
|
|
}
|
2017-05-16 07:34:54 +00:00
|
|
|
}
|
2017-05-20 03:20:30 +00:00
|
|
|
|
|
|
|
void MainWindow::on_actionExit_triggered()
|
|
|
|
{
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::on_actionAbout_Qt_triggered()
|
|
|
|
{
|
|
|
|
QApplication::aboutQt();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::on_actionAbout_qbolt_triggered()
|
|
|
|
{
|
|
|
|
QMessageBox::about(
|
|
|
|
this,
|
|
|
|
QApplication::applicationDisplayName(),
|
|
|
|
"<b>QBolt</b><br>Graphical interface for managing Bolt databases<br><br>"
|
|
|
|
"- <a href='https://github.com/boltdb/bolt'>About BoltDB</a><br>"
|
|
|
|
"- <a href='http://www.famfamfam.com/lab/icons/silk/'>FamFamFam "Silk" icon set</a><br>"
|
|
|
|
"- <a href='https://code.ivysaur.me/qbolt'>QBolt homepage</a><br>"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-05-20 23:47:16 +00:00
|
|
|
void MainWindow::on_actionDisconnect_triggered()
|
|
|
|
{
|
|
|
|
QTreeWidgetItem *top = lastContextSelection;
|
|
|
|
if (top->parent()) {
|
|
|
|
return; // somehow we didn't select a top-level item
|
|
|
|
}
|
|
|
|
|
|
|
|
auto *bdb = GET_BDB(top);
|
|
|
|
|
|
|
|
// Remove UI
|
|
|
|
ui->bucketTree->clearSelection();
|
|
|
|
delete top;
|
|
|
|
|
|
|
|
// Disconnect from DB
|
|
|
|
delete bdb;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::on_bucketTree_customContextMenuRequested(const QPoint &pos)
|
|
|
|
{
|
|
|
|
auto *itm = ui->bucketTree->itemAt(pos);
|
|
|
|
if (itm == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
lastContextSelection = itm;
|
|
|
|
|
|
|
|
if (itm->parent() != nullptr) {
|
|
|
|
// Child item, show the bucket menu
|
|
|
|
bucketContext->popup(ui->bucketTree->mapToGlobal(pos));
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// Top-level item, show the database menu
|
|
|
|
databaseContext->popup(ui->bucketTree->mapToGlobal(pos));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::on_actionRefresh_buckets_triggered()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::on_bucketTree_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous)
|
|
|
|
{
|
|
|
|
Q_UNUSED(previous);
|
|
|
|
if (current == nullptr) {
|
2017-05-21 00:39:55 +00:00
|
|
|
ui->stackedWidget->setVisible(false);
|
2017-05-20 23:47:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-21 00:39:55 +00:00
|
|
|
ui->stackedWidget->setVisible(true);
|
2017-05-20 23:59:38 +00:00
|
|
|
|
|
|
|
if (current->parent() == nullptr) {
|
|
|
|
// Selected a database
|
2017-05-21 00:39:55 +00:00
|
|
|
ui->stackedWidget->setCurrentWidget(ui->databasePage);
|
2017-05-21 01:47:46 +00:00
|
|
|
ui->databasePropertiesArea->clear();
|
2017-05-21 00:39:55 +00:00
|
|
|
|
2017-05-20 23:59:38 +00:00
|
|
|
auto *bdb = GET_BDB(current);
|
2017-05-21 00:39:55 +00:00
|
|
|
bdb->getStatsJSON(
|
|
|
|
[=](QString j) {
|
|
|
|
ui->databasePropertiesArea->setPlainText(j);
|
|
|
|
},
|
|
|
|
[=](QString error) {
|
2017-05-21 01:47:46 +00:00
|
|
|
ui->databasePropertiesArea->setPlainText(tr("Error retrieving database statistics: %1").arg(error));
|
2017-05-21 00:39:55 +00:00
|
|
|
}
|
|
|
|
);
|
2017-05-20 23:59:38 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
// Selected a bucket
|
2017-05-21 00:39:55 +00:00
|
|
|
|
|
|
|
ui->stackedWidget->setCurrentWidget(ui->bucketPage);
|
2017-05-21 01:47:46 +00:00
|
|
|
ui->bucketPropertiesArea->clear();
|
2017-05-21 00:39:55 +00:00
|
|
|
|
|
|
|
QStringList browse;
|
|
|
|
QTreeWidgetItem *top = current;
|
|
|
|
while (top->parent() != nullptr) {
|
|
|
|
browse.push_front(top->text(0));
|
|
|
|
top = top->parent();
|
|
|
|
}
|
|
|
|
auto *bdb = GET_BDB(top);
|
|
|
|
|
|
|
|
bdb->getBucketStatsJSON(
|
|
|
|
browse,
|
|
|
|
[=](QString j) {
|
|
|
|
ui->bucketPropertiesArea->setPlainText(j);
|
|
|
|
},
|
|
|
|
[=](QString error) {
|
2017-05-21 01:47:46 +00:00
|
|
|
ui->bucketPropertiesArea->setPlainText(tr("Error retrieving bucket statistics: %1").arg(error));
|
2017-05-21 00:39:55 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2017-05-20 23:59:38 +00:00
|
|
|
// Load the data tab
|
|
|
|
// TODO
|
|
|
|
}
|
2017-05-20 23:47:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::on_actionClear_selection_triggered()
|
|
|
|
{
|
|
|
|
ui->bucketTree->setCurrentItem(nullptr);
|
|
|
|
}
|