Sandboxie/SandboxiePlus/SandMan/Windows/SnapshotsWindow.cpp

179 lines
6.0 KiB
C++
Raw Normal View History

2020-09-05 16:45:39 +01:00
#include "stdafx.h"
#include "SnapshotsWindow.h"
#include "SandMan.h"
#include "../MiscHelpers/Common/Settings.h"
#include "../MiscHelpers/Common/TreeItemModel.h"
CSnapshotsWindow::CSnapshotsWindow(const CSandBoxPtr& pBox, QWidget *parent)
: QMainWindow(parent)
{
QWidget* centralWidget = new QWidget();
ui.setupUi(centralWidget);
this->setCentralWidget(centralWidget);
this->setWindowTitle(tr("%1 - Snapshots").arg(pBox->GetName()));
m_pBox = pBox;
m_SaveInfoPending = 0;
#ifdef WIN32
QStyle* pStyle = QStyleFactory::create("windows");
ui.treeSnapshots->setStyle(pStyle);
#endif
m_pSnapshotModel = new CSimpleTreeModel();
m_pSnapshotModel->setHeaderLabels(tr("Snapshot").split("|"));
/*m_pSortProxy = new CSortFilterProxyModel(false, this);
m_pSortProxy->setSortRole(Qt::EditRole);
m_pSortProxy->setSourceModel(m_pSnapshotModel);
m_pSortProxy->setDynamicSortFilter(true);*/
//ui.treeSnapshots->setItemDelegate(theGUI->GetItemDelegate());
//ui.treeSnapshots->setModel(m_pSortProxy);
ui.treeSnapshots->setModel(m_pSnapshotModel);
connect(ui.treeSnapshots, SIGNAL(clicked(const QModelIndex&)), this, SLOT(UpdateSnapshot(const QModelIndex&)));
connect(ui.treeSnapshots->selectionModel(), SIGNAL(currentChanged(QModelIndex, QModelIndex)), this, SLOT(UpdateSnapshot(const QModelIndex&)));
connect(ui.treeSnapshots, SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(OnSelectSnapshot()));
connect(ui.btnTake, SIGNAL(pressed()), this, SLOT(OnTakeSnapshot()));
connect(ui.btnSelect, SIGNAL(pressed()), this, SLOT(OnSelectSnapshot()));
connect(ui.btnRemove, SIGNAL(pressed()), this, SLOT(OnRemoveSnapshot()));
connect(ui.txtName, SIGNAL(textEdited(const QString&)), this, SLOT(SaveInfo()));
connect(ui.txtInfo, SIGNAL(textChanged()), this, SLOT(SaveInfo()));
statusBar();
restoreGeometry(theConf->GetBlob("SnapshotsWindow/Window_Geometry"));
for (int i = 0; i < m_pSnapshotModel->columnCount(); i++)
m_pSnapshotModel->SetColumnEnabled(i, true);
UpdateSnapshots();
QModelIndex CurIndex = m_pSnapshotModel->FindIndex(m_CurSnapshot);
if (CurIndex.isValid()) {
ui.treeSnapshots->selectionModel()->select(CurIndex, QItemSelectionModel::ClearAndSelect);
UpdateSnapshot(CurIndex);
}
}
CSnapshotsWindow::~CSnapshotsWindow()
{
theConf->SetBlob("SnapshotsWindow/Window_Geometry",saveGeometry());
}
void CSnapshotsWindow::closeEvent(QCloseEvent *e)
{
this->deleteLater();
}
void CSnapshotsWindow::UpdateSnapshots()
{
m_SnapshotMap.clear();
QList<SBoxSnapshot> SnapshotList = m_pBox->GetSnapshots(&m_CurSnapshot);
foreach(const SBoxSnapshot& Snapshot, SnapshotList)
{
QVariantMap BoxSnapshot;
BoxSnapshot["ID"] = Snapshot.ID;
BoxSnapshot["ParentID"] = Snapshot.Parent;
BoxSnapshot["Name"] = Snapshot.NameStr;
BoxSnapshot["Info"] = Snapshot.InfoStr;
BoxSnapshot["Date"] = Snapshot.SnapDate;
QVariantMap Values;
Values["0"] = Snapshot.NameStr;
BoxSnapshot["Values"] = Values;
if(m_CurSnapshot == Snapshot.ID)
BoxSnapshot["IsBold"] = true;
m_SnapshotMap.insert(Snapshot.ID, BoxSnapshot);
}
m_pSnapshotModel->Sync(m_SnapshotMap);
ui.treeSnapshots->expandAll();
}
void CSnapshotsWindow::UpdateSnapshot(const QModelIndex& Index)
{
//QModelIndex Index = ui.treeSnapshots->currentIndex();
//QModelIndex ModelIndex = m_pSortProxy->mapToSource(Index);
//QVariant ID = m_pSnapshotModel->GetItemID(ModelIndex);
QVariant ID = m_pSnapshotModel->GetItemID(Index);
OnSaveInfo();
m_SellectedID = ID;
QVariantMap BoxSnapshot = m_SnapshotMap[ID];
m_SaveInfoPending = -1;
ui.txtName->setText(BoxSnapshot["Name"].toString());
ui.txtInfo->setPlainText(BoxSnapshot["Info"].toString());
m_SaveInfoPending = 0;
statusBar()->showMessage(tr("Snapshot: %1 taken: %2").arg(BoxSnapshot["Name"].toString()).arg(BoxSnapshot["Date"].toDateTime().toString()));
}
void CSnapshotsWindow::SaveInfo()
{
if (m_SaveInfoPending)
return;
m_SaveInfoPending = 1;
QTimer::singleShot(500, this, SLOT(OnSaveInfo()));
}
void CSnapshotsWindow::OnSaveInfo()
{
if (m_SaveInfoPending != 1)
return;
m_SaveInfoPending = 0;
m_pBox->SetSnapshotInfo(m_SellectedID.toString(), ui.txtName->text(), ui.txtInfo->toPlainText());
UpdateSnapshots();
}
void CSnapshotsWindow::OnTakeSnapshot()
{
QString Value = QInputDialog::getText(this, "Sandboxie-Plus", tr("Please enter a name for the new Snapshot."), QLineEdit::Normal, tr("New Snapshot"));
if (Value.isEmpty())
return;
HandleResult(m_pBox->TakeSnapshot(Value));
}
void CSnapshotsWindow::OnSelectSnapshot()
{
QModelIndex Index = ui.treeSnapshots->currentIndex();
//QModelIndex ModelIndex = m_pSortProxy->mapToSource(Index);
//QVariant ID = m_pSnapshotModel->GetItemID(ModelIndex);
QVariant ID = m_pSnapshotModel->GetItemID(Index);
if (QMessageBox("Sandboxie-Plus", tr("Do you really want to switch the active snapshot? Doing so will delete the current state!"), QMessageBox::Warning, QMessageBox::Yes, QMessageBox::No | QMessageBox::Default | QMessageBox::Escape, QMessageBox::NoButton).exec() != QMessageBox::Yes)
return;
HandleResult(m_pBox->SelectSnapshot(ID.toString()));
}
void CSnapshotsWindow::OnRemoveSnapshot()
{
QModelIndex Index = ui.treeSnapshots->currentIndex();
//QModelIndex ModelIndex = m_pSortProxy->mapToSource(Index);
//QVariant ID = m_pSnapshotModel->GetItemID(ModelIndex);
QVariant ID = m_pSnapshotModel->GetItemID(Index);
if (QMessageBox("Sandboxie-Plus", tr("Do you really want delete the sellected snapshot?"), QMessageBox::Warning, QMessageBox::Yes, QMessageBox::No | QMessageBox::Default | QMessageBox::Escape, QMessageBox::NoButton).exec() != QMessageBox::Yes)
return;
HandleResult(m_pBox->RemoveSnapshot(ID.toString()));
}
void CSnapshotsWindow::HandleResult(SB_PROGRESS Status)
{
if (Status.GetStatus() == OP_ASYNC)
{
connect(Status.GetValue().data(), SIGNAL(Finished()), this, SLOT(UpdateSnapshots()));
theGUI->AddAsyncOp(Status.GetValue());
}
else if (Status.IsError())
CSandMan::CheckResults(QList<SB_STATUS>() << Status);
UpdateSnapshots();
}