#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) : QDialog(parent) { ui.setupUi(this); 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 ui.treeSnapshots->setExpandsOnDoubleClick(false); m_pSnapshotModel = new CSimpleTreeModel(); m_pSnapshotModel->AddColumn(tr("Snapshot"), "Name"); /*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())); ui.groupBox->setEnabled(false); ui.btnSelect->setEnabled(false); ui.btnRemove->setEnabled(false); //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); } } CSnapshotsWindow::~CSnapshotsWindow() { theConf->SetBlob("SnapshotsWindow/Window_Geometry",saveGeometry()); } void CSnapshotsWindow::closeEvent(QCloseEvent *e) { this->deleteLater(); } void CSnapshotsWindow::UpdateSnapshots() { m_SnapshotMap.clear(); QList 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; 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) { ui.groupBox->setEnabled(true); ui.btnSelect->setEnabled(true); ui.btnRemove->setEnabled(true); //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::Question, 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 to delete the selected snapshot?"), QMessageBox::Question, 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() << Status); UpdateSnapshots(); }