Sandboxie/SandboxiePlus/SandMan/Windows/OptionsWindow.cpp

915 lines
28 KiB
C++
Raw Normal View History

2021-10-16 16:19:51 +01:00
#include "stdafx.h"
#include "OptionsWindow.h"
#include "SandMan.h"
#include "SettingsWindow.h"
#include "../MiscHelpers/Common/Settings.h"
#include "../MiscHelpers/Common/Common.h"
#include "../MiscHelpers/Common/ComboInputDialog.h"
#include "../MiscHelpers/Common/SettingsWidgets.h"
#include "Helpers/WinAdmin.h"
2022-08-15 12:18:26 +01:00
2021-10-16 16:19:51 +01:00
2021-12-03 19:26:09 +00:00
class NoEditDelegate : public QStyledItemDelegate {
public:
NoEditDelegate(QObject* parent = 0) : QStyledItemDelegate(parent) {}
virtual QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const {
return NULL;
}
};
class QTreeWidgetHacker : public QTreeWidget
{
public:
friend class ProgramsDelegate;
//QModelIndex indexFromItem(const QTreeWidgetItem *item, int column = 0) const;
//QTreeWidgetItem *itemFromIndex(const QModelIndex &index) const;
};
2022-08-20 13:48:18 +01:00
//////////////////////////////////////////////////////////////////////////
// ProgramsDelegate
2021-12-03 19:26:09 +00:00
class ProgramsDelegate : public QStyledItemDelegate {
public:
ProgramsDelegate(COptionsWindow* pOptions, QTreeWidget* pTree, int Column, QObject* parent = 0) : QStyledItemDelegate(parent) { m_pOptions = pOptions; m_pTree = pTree; m_Column = Column; }
virtual QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const {
QTreeWidgetItem* pItem = ((QTreeWidgetHacker*)m_pTree)->itemFromIndex(index);
if (!pItem->data(index.column(), Qt::UserRole).isValid())
return NULL;
if (m_Column == -1 || pItem->data(m_Column, Qt::UserRole).toInt() == COptionsWindow::eProcess) {
QComboBox* pBox = new QComboBox(parent);
pBox->setEditable(true);
foreach(const QString Group, m_pOptions->GetCurrentGroups()) {
QString GroupName = Group.mid(1, Group.length() - 2);
pBox->addItem(tr("Group: %1").arg(GroupName), Group);
}
foreach(const QString & Name, m_pOptions->GetPrograms())
pBox->addItem(Name, Name);
connect(pBox->lineEdit(), &QLineEdit::textEdited, [pBox](const QString& text){
/*if (pBox->currentIndex() != -1) {
int pos = pBox->lineEdit()->cursorPosition();
pBox->setCurrentIndex(-1);
pBox->setCurrentText(text);
pBox->lineEdit()->setCursorPosition(pos);
}*/
pBox->setProperty("value", text);
});
connect(pBox, qOverload<int>(&QComboBox::currentIndexChanged), [pBox](int index){
if (index != -1)
pBox->setProperty("value", pBox->itemData(index));
});
return pBox;
}
else if (pItem->data(0, Qt::UserRole).toInt() == COptionsWindow::ePath)
return QStyledItemDelegate::createEditor(parent, option, index);
else
return NULL;
}
virtual void setEditorData(QWidget* editor, const QModelIndex& index) const {
QComboBox* pBox = qobject_cast<QComboBox*>(editor);
if (pBox) {
QTreeWidgetItem* pItem = ((QTreeWidgetHacker*)m_pTree)->itemFromIndex(index);
QString Program = pItem->data(index.column(), Qt::UserRole).toString();
pBox->setProperty("value", Program);
int Index = pBox->findData(Program);
pBox->setCurrentIndex(Index);
if (Index == -1)
pBox->setCurrentText(Program);
}
else
QStyledItemDelegate::setEditorData(editor, index);
}
virtual void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const {
QComboBox* pBox = qobject_cast<QComboBox*>(editor);
if (pBox) {
QTreeWidgetItem* pItem = ((QTreeWidgetHacker*)m_pTree)->itemFromIndex(index);
QString Value = pBox->property("value").toString();
pItem->setText(index.column(), pBox->currentText());
//QString Text = pBox->currentText();
//QVariant Data = pBox->currentData();
pItem->setData(index.column(), Qt::UserRole, Value);
}
QLineEdit* pEdit = qobject_cast<QLineEdit*>(editor);
if (pEdit) {
QTreeWidgetItem* pItem = ((QTreeWidgetHacker*)m_pTree)->itemFromIndex(index);
pItem->setText(index.column(), pEdit->text());
pItem->setData(index.column(), Qt::UserRole, pEdit->text());
}
}
protected:
COptionsWindow* m_pOptions;
QTreeWidget* m_pTree;
int m_Column;
};
2022-08-15 12:18:26 +01:00
//////////////////////////////////////////////////////////////////////////
// COptionsWindow
2021-10-16 16:19:51 +01:00
COptionsWindow::COptionsWindow(const QSharedPointer<CSbieIni>& pBox, const QString& Name, QWidget *parent)
2022-08-15 12:18:26 +01:00
: CConfigDialog(parent)
2021-10-16 16:19:51 +01:00
{
m_pBox = pBox;
m_Template = pBox->GetName().left(9).compare("Template_", Qt::CaseInsensitive) == 0;
bool ReadOnly = /*pBox->GetAPI()->IsConfigLocked() ||*/ (m_Template && pBox->GetName().mid(9, 6).compare("Local_", Qt::CaseInsensitive) != 0);
2021-11-13 08:28:32 +00:00
m_HoldChange = false;
2021-10-16 16:19:51 +01:00
QSharedPointer<CSandBoxPlus> pBoxPlus = m_pBox.objectCast<CSandBoxPlus>();
if (!pBoxPlus.isNull())
m_Programs = pBoxPlus->GetRecentPrograms();
Qt::WindowFlags flags = windowFlags();
flags |= Qt::CustomizeWindowHint;
//flags &= ~Qt::WindowContextHelpButtonHint;
//flags &= ~Qt::WindowSystemMenuHint;
//flags &= ~Qt::WindowMinMaxButtonsHint;
flags |= Qt::WindowMinimizeButtonHint;
//flags &= ~Qt::WindowCloseButtonHint;
setWindowFlags(flags);
bool bAlwaysOnTop = theConf->GetBool("Options/AlwaysOnTop", false);
this->setWindowFlag(Qt::WindowStaysOnTopHint, bAlwaysOnTop);
ui.setupUi(this);
2022-08-20 13:48:18 +01:00
this->setWindowTitle(tr("Sandboxie Plus - '%1' Options").arg(QString(Name).replace("_", " ")));
2021-10-16 16:19:51 +01:00
ui.tabs->setTabPosition(QTabWidget::West);
ui.tabs->tabBar()->setStyle(new CustomTabStyle(ui.tabs->tabBar()->style()));
2022-08-20 13:48:18 +01:00
ui.tabs->tabBar()->setProperty("isSidebar", true);
2021-10-16 16:19:51 +01:00
2022-07-09 15:29:16 +01:00
//this->setMinimumHeight(490);
ui.tabs->setTabIcon(eGeneral, CSandMan::GetIcon("Options"));
ui.tabs->setTabIcon(eGroups, CSandMan::GetIcon("Group"));
ui.tabs->setTabIcon(eForce, CSandMan::GetIcon("Force"));
ui.tabs->setTabIcon(eStop, CSandMan::GetIcon("Stop"));
ui.tabs->setTabIcon(eStart, CSandMan::GetIcon("Start"));
ui.tabs->setTabIcon(eInternet, CSandMan::GetIcon("Wall"));
ui.tabs->setTabIcon(eAccess, CSandMan::GetIcon("Ampel"));
ui.tabs->setTabIcon(eRecover, CSandMan::GetIcon("Recover"));
//ui.tabs->setTabIcon(eOther, CSandMan::GetIcon("Settings"));
ui.tabs->setTabIcon(eAdvanced, CSandMan::GetIcon("Advanced"));
ui.tabs->setTabIcon(eTemplates, CSandMan::GetIcon("Template"));
ui.tabs->setTabIcon(eEditIni, CSandMan::GetIcon("EditIni"));
2021-10-16 16:19:51 +01:00
2022-08-15 12:18:26 +01:00
//if(theConf->GetInt("Options/ViewMode", 1) != 1 && (QApplication::keyboardModifiers() & Qt::ControlModifier) == 0)
// ui.tabs->removeTab(eEditIni);
2022-07-29 09:24:32 +01:00
if (theConf->GetBool("Options/AltRowColors", false)) {
ui.treeRun->setAlternatingRowColors(true);
ui.treeGroups->setAlternatingRowColors(true);
ui.treeForced->setAlternatingRowColors(true);
ui.treeStop->setAlternatingRowColors(true);
ui.treeStart->setAlternatingRowColors(true);
ui.treeINet->setAlternatingRowColors(true);
ui.treeNetFw->setAlternatingRowColors(true);
ui.treeAccess->setAlternatingRowColors(true);
ui.treeRecovery->setAlternatingRowColors(true);
ui.treeTriggers->setAlternatingRowColors(true);
ui.treeTemplates->setAlternatingRowColors(true);
ui.treeFolders->setAlternatingRowColors(true);
}
2021-10-16 16:19:51 +01:00
CreateDebug();
if (m_Template)
{
ui.tabGeneral->setEnabled(false);
ui.tabStart->setEnabled(false);
ui.tabInternet->setEnabled(false);
ui.tabAdvanced->setEnabled(false);
ui.tabTemplates->setEnabled(false);
for (int i = 0; i < ui.tabs->count(); i++)
ui.tabs->setTabEnabled(i, ui.tabs->widget(i)->isEnabled());
ui.tabs->setCurrentIndex(ui.tabs->indexOf(ui.tabAccess));
ui.chkShowForceTmpl->setEnabled(false);
ui.chkShowStopTmpl->setEnabled(false);
ui.chkShowNetFwTmpl->setEnabled(false);
ui.chkShowAccessTmpl->setEnabled(false);
ui.chkShowRecoveryTmpl->setEnabled(false);
//ui.chkWithTemplates->setEnabled(false);
}
ui.tabs->setCurrentIndex(m_Template ? 10 : 0);
if(m_Template)
OnTab();
//connect(ui.chkWithTemplates, SIGNAL(clicked(bool)), this, SLOT(OnWithTemplates()));
m_ConfigDirty = true;
CreateGeneral();
// Groupes
connect(ui.btnAddGroup, SIGNAL(clicked(bool)), this, SLOT(OnAddGroup()));
connect(ui.btnAddProg, SIGNAL(clicked(bool)), this, SLOT(OnAddProg()));
connect(ui.btnDelProg, SIGNAL(clicked(bool)), this, SLOT(OnDelProg()));
connect(ui.chkShowGroupTmpl, SIGNAL(clicked(bool)), this, SLOT(OnShowGroupTmpl()));
2021-12-03 19:26:09 +00:00
ui.treeGroups->setItemDelegateForColumn(0, new ProgramsDelegate(this, ui.treeGroups, -1, this));
connect(ui.treeGroups, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(OnGroupsChanged(QTreeWidgetItem *, int)));
2021-10-16 16:19:51 +01:00
//
// Force
connect(ui.btnForceProg, SIGNAL(clicked(bool)), this, SLOT(OnForceProg()));
connect(ui.btnForceDir, SIGNAL(clicked(bool)), this, SLOT(OnForceDir()));
connect(ui.btnDelForce, SIGNAL(clicked(bool)), this, SLOT(OnDelForce()));
connect(ui.chkShowForceTmpl, SIGNAL(clicked(bool)), this, SLOT(OnShowForceTmpl()));
2021-12-03 19:26:09 +00:00
//ui.treeForced->setEditTriggers(QAbstractItemView::DoubleClicked);
ui.treeForced->setItemDelegateForColumn(0, new NoEditDelegate(this));
ui.treeForced->setItemDelegateForColumn(1, new ProgramsDelegate(this, ui.treeForced, 0, this));
connect(ui.treeForced, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(OnForcedChanged(QTreeWidgetItem *, int)));
2021-10-16 16:19:51 +01:00
//
// Stop
connect(ui.btnAddLingering, SIGNAL(clicked(bool)), this, SLOT(OnAddLingering()));
connect(ui.btnAddLeader, SIGNAL(clicked(bool)), this, SLOT(OnAddLeader()));
connect(ui.btnDelStopProg, SIGNAL(clicked(bool)), this, SLOT(OnDelStopProg()));
connect(ui.chkShowStopTmpl, SIGNAL(clicked(bool)), this, SLOT(OnShowStopTmpl()));
2021-12-03 19:26:09 +00:00
ui.treeStop->setItemDelegateForColumn(1, new ProgramsDelegate(this, ui.treeStop, -1, this));
connect(ui.treeStop, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(OnStopChanged(QTreeWidgetItem *, int)));
2021-10-16 16:19:51 +01:00
//
// Start
connect(ui.radStartAll, SIGNAL(clicked(bool)), this, SLOT(OnRestrictStart()));
connect(ui.radStartExcept, SIGNAL(clicked(bool)), this, SLOT(OnRestrictStart()));
connect(ui.radStartSelected, SIGNAL(clicked(bool)), this, SLOT(OnRestrictStart()));
connect(ui.btnAddStartProg, SIGNAL(clicked(bool)), this, SLOT(OnAddStartProg()));
connect(ui.btnDelStartProg, SIGNAL(clicked(bool)), this, SLOT(OnDelStartProg()));
connect(ui.chkStartBlockMsg, SIGNAL(clicked(bool)), this, SLOT(OnStartChanged()));
2021-12-03 19:26:09 +00:00
ui.treeStart->setItemDelegateForColumn(0, new ProgramsDelegate(this, ui.treeStart, -1, this));
connect(ui.treeStart, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(OnStartChanged(QTreeWidgetItem *, int)));
2021-10-16 16:19:51 +01:00
//
CreateNetwork();
CreateAccess();
// Recovery
connect(ui.chkAutoRecovery, SIGNAL(clicked(bool)), this, SLOT(OnRecoveryChanged()));
connect(ui.btnAddRecovery, SIGNAL(clicked(bool)), this, SLOT(OnAddRecFolder()));
connect(ui.btnDelRecovery, SIGNAL(clicked(bool)), this, SLOT(OnDelRecEntry()));
connect(ui.btnAddRecIgnore, SIGNAL(clicked(bool)), this, SLOT(OnAddRecIgnore()));
connect(ui.btnAddRecIgnoreExt, SIGNAL(clicked(bool)), this, SLOT(OnAddRecIgnoreExt()));
connect(ui.chkShowRecoveryTmpl, SIGNAL(clicked(bool)), this, SLOT(OnShowRecoveryTmpl()));
//
CreateAdvanced();
// Templates
connect(ui.cmbCategories, SIGNAL(currentIndexChanged(int)), this, SLOT(OnFilterTemplates()));
connect(ui.txtTemplates, SIGNAL(textChanged(const QString&)), this, SLOT(OnFilterTemplates()));
2021-10-19 22:35:59 +01:00
//connect(ui.treeTemplates, SIGNAL(itemClicked(QTreeWidgetItem*, int)), this, SLOT(OnTemplateClicked(QTreeWidgetItem*, int)));
connect(ui.treeTemplates, SIGNAL(itemChanged(QTreeWidgetItem*, int)), this, SLOT(OnTemplateClicked(QTreeWidgetItem*, int)));
2021-10-16 16:19:51 +01:00
connect(ui.treeTemplates, SIGNAL(itemDoubleClicked(QTreeWidgetItem*, int)), this, SLOT(OnTemplateDoubleClicked(QTreeWidgetItem*, int)));
connect(ui.btnAddTemplate, SIGNAL(clicked(bool)), this, SLOT(OnAddTemplates()));
connect(ui.btnDelTemplate, SIGNAL(clicked(bool)), this, SLOT(OnDelTemplates()));
connect(ui.chkScreenReaders, SIGNAL(clicked(bool)), this, SLOT(OnScreenReaders()));
//
connect(ui.tabs, SIGNAL(currentChanged(int)), this, SLOT(OnTab()));
// edit
connect(ui.btnEditIni, SIGNAL(clicked(bool)), this, SLOT(OnEditIni()));
connect(ui.btnSaveIni, SIGNAL(clicked(bool)), this, SLOT(OnSaveIni()));
connect(ui.btnCancelEdit, SIGNAL(clicked(bool)), this, SLOT(OnCancelEdit()));
2021-11-13 08:28:32 +00:00
connect(ui.txtIniSection, SIGNAL(textChanged()), this, SLOT(OnOptChanged()));
2021-10-16 16:19:51 +01:00
//
connect(ui.buttonBox->button(QDialogButtonBox::Ok), SIGNAL(clicked(bool)), this, SLOT(ok()));
connect(ui.buttonBox->button(QDialogButtonBox::Apply), SIGNAL(clicked(bool)), this, SLOT(apply()));
connect(ui.buttonBox, SIGNAL(rejected()), this, SLOT(close()));
if (ReadOnly)
{
ui.btnEditIni->setEnabled(false);
ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
}
if (theAPI->IsRunningAsAdmin())
{
ui.chkDropRights->setEnabled(false);
ui.chkFakeElevation->setEnabled(false);
}
else
ui.lblAdmin->setVisible(false);
LoadConfig();
2022-04-15 17:37:39 +01:00
UpdateCurrentTab();
2021-11-13 08:28:32 +00:00
ui.buttonBox->button(QDialogButtonBox::Apply)->setEnabled(false);
2021-10-16 16:19:51 +01:00
ui.treeAccess->viewport()->installEventFilter(this);
ui.treeINet->viewport()->installEventFilter(this);
ui.treeNetFw->viewport()->installEventFilter(this);
2022-08-15 10:58:39 +01:00
this->installEventFilter(this); // prevent enter from closing the dialog
2021-10-16 16:19:51 +01:00
restoreGeometry(theConf->GetBlob("OptionsWindow/Window_Geometry"));
QByteArray
Columns = theConf->GetBlob("OptionsWindow/Run_Columns");
if (!Columns.isEmpty()) ui.treeRun->header()->restoreState(Columns);
2022-02-04 21:08:25 +00:00
Columns = theConf->GetBlob("OptionsWindow/Triggers_Columns");
if (!Columns.isEmpty()) ui.treeTriggers->header()->restoreState(Columns);
2021-10-16 16:19:51 +01:00
Columns = theConf->GetBlob("OptionsWindow/Groups_Columns");
if (!Columns.isEmpty()) ui.treeGroups->header()->restoreState(Columns);
Columns = theConf->GetBlob("OptionsWindow/Forced_Columns");
if (!Columns.isEmpty()) ui.treeForced->header()->restoreState(Columns);
Columns = theConf->GetBlob("OptionsWindow/Stop_Columns");
if (!Columns.isEmpty()) ui.treeStop->header()->restoreState(Columns);
Columns = theConf->GetBlob("OptionsWindow/Start_Columns");
if (!Columns.isEmpty()) ui.treeStart->header()->restoreState(Columns);
Columns = theConf->GetBlob("OptionsWindow/INet_Columns");
if (!Columns.isEmpty()) ui.treeINet->header()->restoreState(Columns);
Columns = theConf->GetBlob("OptionsWindow/NetFw_Columns");
if (!Columns.isEmpty()) ui.treeNetFw->header()->restoreState(Columns);
Columns = theConf->GetBlob("OptionsWindow/Access_Columns");
if (!Columns.isEmpty()) ui.treeAccess->header()->restoreState(Columns);
Columns = theConf->GetBlob("OptionsWindow/Recovery_Columns");
if (!Columns.isEmpty()) ui.treeRecovery->header()->restoreState(Columns);
Columns = theConf->GetBlob("OptionsWindow/Templates_Columns");
if (!Columns.isEmpty()) ui.treeTemplates->header()->restoreState(Columns);
2022-07-09 15:29:16 +01:00
int iViewMode = theConf->GetInt("Options/ViewMode", 1);
int iOptionTree = theConf->GetInt("Options/OptionTree", 2);
if (iOptionTree == 2)
iOptionTree = iViewMode == 2 ? 1 : 0;
2022-08-20 13:48:18 +01:00
ui.tabsGeneral->setTabIcon(0, CSandMan::GetIcon("Box"));
ui.tabsGeneral->setTabIcon(1, CSandMan::GetIcon("File"));
ui.tabsGeneral->setTabIcon(2, CSandMan::GetIcon("Security"));
ui.tabsGeneral->setTabIcon(3, CSandMan::GetIcon("NoAccess"));
ui.tabsGeneral->setTabIcon(4, CSandMan::GetIcon("Run"));
ui.tabsInternet->setTabIcon(0, CSandMan::GetIcon("List"));
ui.tabsInternet->setTabIcon(1, CSandMan::GetIcon("Network"));
ui.tabsAccess->setTabIcon(0, CSandMan::GetIcon("Rules"));
ui.tabsAccess->setTabIcon(1, CSandMan::GetIcon("Policy"));
ui.tabsAdvanced->setTabIcon(0, CSandMan::GetIcon("Miscellaneous"));
ui.tabsAdvanced->setTabIcon(1, CSandMan::GetIcon("Fence"));
ui.tabsAdvanced->setTabIcon(2, CSandMan::GetIcon("Trigger"));
ui.tabsAdvanced->setTabIcon(3, CSandMan::GetIcon("Anon"));
ui.tabsAdvanced->setTabIcon(4, CSandMan::GetIcon("Users"));
ui.tabsAdvanced->setTabIcon(5, CSandMan::GetIcon("SetLogging"));
ui.tabsAdvanced->setTabIcon(6, CSandMan::GetIcon("Bug"));
ui.tabsTemplates->setTabIcon(0, CSandMan::GetIcon("Compatibility"));
ui.tabsTemplates->setTabIcon(1, CSandMan::GetIcon("Explore"));
ui.tabsTemplates->setTabIcon(2, CSandMan::GetIcon("Accessibility"));
2022-07-09 15:29:16 +01:00
if (iOptionTree)
2022-08-15 12:18:26 +01:00
OnSetTree();
2022-07-09 15:29:16 +01:00
else {
2022-08-15 18:32:38 +01:00
QWidget* pSearch = AddConfigSearch(ui.tabs);
ui.horizontalLayout->insertWidget(0, pSearch);
QTimer::singleShot(0, [this]() {
m_pSearch->setMaximumWidth(m_pTabs->tabBar()->width());
});
2022-08-15 10:58:39 +01:00
}
2022-08-16 18:58:13 +01:00
m_pSearch->setPlaceholderText(tr("Search for Options"));
2022-08-15 10:58:39 +01:00
}
2022-08-15 12:18:26 +01:00
void COptionsWindow::OnSetTree()
2022-07-09 15:29:16 +01:00
{
2022-08-15 12:18:26 +01:00
if (!ui.tabs) return;
QWidget* pAltView = ConvertToTree(ui.tabs);
ui.verticalLayout->replaceWidget(ui.tabs, pAltView);
ui.tabs->deleteLater();
ui.tabs = NULL;
2021-10-16 16:19:51 +01:00
}
2022-07-09 15:29:16 +01:00
void COptionsWindow::OnOptChanged()
{
2021-11-13 08:28:32 +00:00
if (m_HoldChange)
return;
ui.buttonBox->button(QDialogButtonBox::Apply)->setEnabled(true);
}
2021-10-16 16:19:51 +01:00
COptionsWindow::~COptionsWindow()
{
theConf->SetBlob("OptionsWindow/Window_Geometry",saveGeometry());
theConf->SetBlob("OptionsWindow/Run_Columns", ui.treeRun->header()->saveState());
2022-02-04 21:08:25 +00:00
theConf->SetBlob("OptionsWindow/Triggers_Columns", ui.treeTriggers->header()->saveState());
2021-10-16 16:19:51 +01:00
theConf->SetBlob("OptionsWindow/Groups_Columns", ui.treeGroups->header()->saveState());
theConf->SetBlob("OptionsWindow/Forced_Columns", ui.treeForced->header()->saveState());
theConf->SetBlob("OptionsWindow/Stop_Columns", ui.treeStop->header()->saveState());
theConf->SetBlob("OptionsWindow/Start_Columns", ui.treeStart->header()->saveState());
theConf->SetBlob("OptionsWindow/INet_Columns", ui.treeINet->header()->saveState());
theConf->SetBlob("OptionsWindow/NetFw_Columns", ui.treeNetFw->header()->saveState());
theConf->SetBlob("OptionsWindow/Access_Columns", ui.treeAccess->header()->saveState());
theConf->SetBlob("OptionsWindow/Recovery_Columns", ui.treeRecovery->header()->saveState());
theConf->SetBlob("OptionsWindow/Templates_Columns", ui.treeTemplates->header()->saveState());
}
void COptionsWindow::closeEvent(QCloseEvent *e)
{
emit Closed();
this->deleteLater();
}
bool COptionsWindow::eventFilter(QObject *source, QEvent *event)
{
if (event->type() == QEvent::KeyPress && ((QKeyEvent*)event)->key() == Qt::Key_Escape
&& ((QKeyEvent*)event)->modifiers() == Qt::NoModifier)
{
CloseINetEdit(false);
CloseNetFwEdit(false);
CloseAccessEdit(false);
return true; // cancel event
}
if (event->type() == QEvent::KeyPress && (((QKeyEvent*)event)->key() == Qt::Key_Enter || ((QKeyEvent*)event)->key() == Qt::Key_Return)
&& ((QKeyEvent*)event)->modifiers() == Qt::NoModifier)
{
CloseINetEdit(true);
CloseNetFwEdit(true);
CloseAccessEdit(true);
return true; // cancel event
}
if (source == ui.treeAccess->viewport() && event->type() == QEvent::MouseButtonPress)
{
CloseAccessEdit();
}
if (source == ui.treeINet->viewport() && event->type() == QEvent::MouseButtonPress)
{
CloseINetEdit();
}
if (source == ui.treeNetFw->viewport() && event->type() == QEvent::MouseButtonPress)
{
CloseNetFwEdit();
}
return QDialog::eventFilter(source, event);
}
//void COptionsWindow::OnWithTemplates()
//{
// m_Template = ui.chkWithTemplates->isChecked();
// ui.buttonBox->setEnabled(!m_Template);
// LoadConfig();
//}
void COptionsWindow::ReadAdvancedCheck(const QString& Name, QCheckBox* pCheck, const QString& Value)
{
QString Data = m_pBox->GetText(Name, "");
if (Data == Value) pCheck->setCheckState(Qt::Checked);
else if (Data.isEmpty()) pCheck->setCheckState(Qt::Unchecked);
else pCheck->setCheckState(Qt::PartiallyChecked);
}
void COptionsWindow::LoadConfig()
{
m_ConfigDirty = false;
2022-05-01 09:44:28 +01:00
m_HoldChange = true;
2021-10-16 16:19:51 +01:00
LoadGeneral();
LoadGroups();
LoadForced();
LoadStop();
LoadStart();
LoadINetAccess();
LoadNetFwRules();
LoadAccessList();
LoadRecoveryList();
LoadAdvanced();
LoadDebug();
LoadTemplates();
2021-11-13 08:28:32 +00:00
UpdateBoxType();
2022-05-01 09:44:28 +01:00
m_HoldChange = false;
2021-10-16 16:19:51 +01:00
}
void COptionsWindow::WriteAdvancedCheck(QCheckBox* pCheck, const QString& Name, const QString& Value)
{
SB_STATUS Status;
if (pCheck->checkState() == Qt::Checked)
Status = m_pBox->SetText(Name, Value);
else if (pCheck->checkState() == Qt::Unchecked)
Status = m_pBox->DelValue(Name);
if (!Status)
throw Status;
}
void COptionsWindow::WriteAdvancedCheck(QCheckBox* pCheck, const QString& Name, const QString& OnValue, const QString& OffValue)
{
//if (pCheck->checkState() == Qt::PartiallyChecked)
// return;
2021-11-13 08:28:32 +00:00
if (!pCheck->isEnabled())
return;
2021-10-16 16:19:51 +01:00
SB_STATUS Status;
if (pCheck->checkState() == Qt::Checked)
{
if(!OnValue.isEmpty())
Status = m_pBox->SetText(Name, OnValue);
else
Status = m_pBox->DelValue(Name);
}
else if (pCheck->checkState() == Qt::Unchecked)
{
if (!OffValue.isEmpty())
Status = m_pBox->SetText(Name, OffValue);
else
Status = m_pBox->DelValue(Name);
}
if (!Status)
throw Status;
}
void COptionsWindow::WriteText(const QString& Name, const QString& Value)
{
SB_STATUS Status = m_pBox->SetText(Name, Value);
if (!Status)
throw Status;
}
void COptionsWindow::WriteTextList(const QString& Setting, const QStringList& List)
{
SB_STATUS Status = m_pBox->UpdateTextList(Setting, List, m_Template);
if (!Status)
throw Status;
}
void COptionsWindow::SaveConfig()
{
2022-01-29 09:18:22 +00:00
bool UpdatePaths = false;
2021-10-16 16:19:51 +01:00
m_pBox->SetRefreshOnChange(false);
try
{
if (m_GeneralChanged)
SaveGeneral();
if (m_GroupsChanged)
SaveGroups();
if (m_ForcedChanged)
SaveForced();
if (m_StopChanged)
SaveStop();
if (m_StartChanged)
SaveStart();
if (m_INetBlockChanged)
SaveINetAccess();
if (m_NetFwRulesChanged)
SaveNetFwRules();
2022-01-29 09:18:22 +00:00
if (m_AccessChanged) {
2021-10-16 16:19:51 +01:00
SaveAccessList();
2022-01-29 09:18:22 +00:00
UpdatePaths = true;
}
2021-10-16 16:19:51 +01:00
if (m_RecoveryChanged)
SaveRecoveryList();
if (m_AdvancedChanged)
SaveAdvanced();
SaveDebug();
if (m_TemplatesChanged)
SaveTemplates();
if (m_FoldersChanged)
SaveFolders();
}
catch (SB_STATUS Status)
{
theGUI->CheckResults(QList<SB_STATUS>() << Status);
}
m_pBox->SetRefreshOnChange(true);
m_pBox->GetAPI()->CommitIniChanges();
2022-01-29 09:18:22 +00:00
if (UpdatePaths)
TriggerPathReload();
2021-10-16 16:19:51 +01:00
}
void COptionsWindow::apply()
{
if (m_pBox->GetText("Enabled").isEmpty() && !(m_Template && m_pBox->GetName().mid(9, 6).compare("Local_", Qt::CaseInsensitive) == 0)) {
QMessageBox::critical(this, "Sandboxie-Plus", tr("This sandbox has been deleted hence configuration can not be saved."));
return;
}
CloseINetEdit();
CloseNetFwEdit();
CloseAccessEdit();
if (!ui.btnEditIni->isEnabled())
SaveIniSection();
else
SaveConfig();
LoadConfig();
UpdateCurrentTab();
emit OptionsChanged();
2021-11-13 08:28:32 +00:00
ui.buttonBox->button(QDialogButtonBox::Apply)->setEnabled(false);
2021-10-16 16:19:51 +01:00
}
void COptionsWindow::ok()
{
apply();
this->close();
}
void COptionsWindow::reject()
{
if (m_GeneralChanged
|| m_GroupsChanged
|| m_ForcedChanged
|| m_StopChanged
|| m_StartChanged
// || m_RestrictionChanged
|| m_INetBlockChanged
|| m_AccessChanged
|| m_TemplatesChanged
|| m_FoldersChanged
|| m_RecoveryChanged
|| m_AdvancedChanged)
{
if (QMessageBox("Sandboxie-Plus", tr("Some changes haven't been saved yet, do you really want to close this options window?")
, QMessageBox::Warning, QMessageBox::Yes | QMessageBox::Default, QMessageBox::No | QMessageBox::Escape, QMessageBox::NoButton, this).exec() != QMessageBox::Yes)
return;
}
this->close();
}
void COptionsWindow::SetProgramItem(QString Program, QTreeWidgetItem* pItem, int Column)
{
pItem->setData(Column, Qt::UserRole, Program);
if (Program.left(1) == "<")
Program = tr("Group: %1").arg(Program.mid(1, Program.length() - 2));
else
m_Programs.insert(Program);
pItem->setText(Column, Program);
}
QString COptionsWindow::SelectProgram(bool bOrGroup)
{
CComboInputDialog progDialog(this);
progDialog.setText(tr("Enter program:"));
progDialog.setEditable(true);
if (bOrGroup)
{
2021-11-13 08:28:32 +00:00
foreach(const QString Group, GetCurrentGroups()){
QString GroupName = Group.mid(1, Group.length() - 2);
2021-12-03 19:26:09 +00:00
progDialog.addItem(tr("Group: %1").arg(GroupName), Group);
2021-10-16 16:19:51 +01:00
}
}
foreach(const QString & Name, m_Programs)
progDialog.addItem(Name, Name);
progDialog.setValue("");
if (!progDialog.exec())
return QString();
// Note: pressing enter adds the value to the combo list !
QString Program = progDialog.value();
int Index = progDialog.findValue(Program);
if (Index != -1 && progDialog.data().isValid())
Program = progDialog.data().toString();
return Program;
}
void COptionsWindow::OnTab()
{
2022-07-09 15:29:16 +01:00
OnTab(ui.tabs->currentIndex());
}
void COptionsWindow::OnTab(int iTabID)
{
m_iCurrentTab = iTabID;
if (m_iCurrentTab == eEditIni)
2021-10-16 16:19:51 +01:00
{
LoadIniSection();
ui.txtIniSection->setReadOnly(true);
}
else
{
if (m_ConfigDirty)
LoadConfig();
UpdateCurrentTab();
}
}
void COptionsWindow::UpdateCurrentTab()
{
2022-07-09 15:29:16 +01:00
if (m_iCurrentTab == eGeneral) {
2022-04-15 17:37:39 +01:00
ui.chkVmRead->setChecked(GetAccessEntry(eIPC, "", eReadOnly, "$:*") != NULL);
}
2022-07-09 15:29:16 +01:00
else if (m_iCurrentTab == eStart)
2021-10-16 16:19:51 +01:00
{
if (GetAccessEntry(eIPC, "!<StartRunAccess>", eClosed, "*") != NULL)
ui.radStartSelected->setChecked(true);
else if (GetAccessEntry(eIPC, "<StartRunAccess>", eClosed, "*") != NULL)
ui.radStartExcept->setChecked(true);
else
ui.radStartAll->setChecked(true);
2021-12-03 19:26:09 +00:00
ui.treeStart->clear();
2021-10-16 16:19:51 +01:00
CopyGroupToList("<StartRunAccess>", ui.treeStart);
2021-12-03 19:26:09 +00:00
CopyGroupToList("<StartRunAccessDisabled>", ui.treeStart, true);
2021-10-16 16:19:51 +01:00
OnRestrictStart();
}
2022-07-09 15:29:16 +01:00
else if (m_iCurrentTab == eInternet)
2021-10-16 16:19:51 +01:00
{
CheckINetBlock();
LoadBlockINet();
}
2022-07-09 15:29:16 +01:00
else if (m_iCurrentTab == eAdvanced)
2021-10-16 16:19:51 +01:00
{
2021-11-13 08:28:32 +00:00
ui.chkOpenCOM->setChecked(GetAccessEntry(eIPC, "", eOpen, "\\RPC Control\\epmapper") != NULL);
if (GetAccessEntry(eWnd, "", eOpen, "*") != NULL)
2021-10-16 16:19:51 +01:00
{
ui.chkNoWindowRename->setEnabled(false);
ui.chkNoWindowRename->setChecked(true);
}
else
{
ui.chkNoWindowRename->setEnabled(true);
2021-11-13 08:28:32 +00:00
ui.chkNoWindowRename->setChecked(GetAccessEntry(eWnd, "", eOpen, "#") != NULL);
2021-10-16 16:19:51 +01:00
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2022-01-14 21:28:44 +00:00
// Raw section ini Editor
2021-10-16 16:19:51 +01:00
//
void COptionsWindow::SetIniEdit(bool bEnable)
{
2022-07-09 15:29:16 +01:00
if (m_pTree) {
m_pTree->setEnabled(!bEnable);
}
else {
for (int i = 0; i < ui.tabs->count() - 1; i++) {
bool Enabled = ui.tabs->widget(i)->isEnabled();
ui.tabs->setTabEnabled(i, !bEnable && Enabled);
ui.tabs->widget(i)->setEnabled(Enabled);
}
2021-10-16 16:19:51 +01:00
}
ui.btnSaveIni->setEnabled(bEnable);
ui.btnCancelEdit->setEnabled(bEnable);
ui.txtIniSection->setReadOnly(!bEnable);
ui.btnEditIni->setEnabled(!bEnable);
}
void COptionsWindow::OnEditIni()
{
SetIniEdit(true);
}
void COptionsWindow::OnSaveIni()
{
SaveIniSection();
SetIniEdit(false);
}
void COptionsWindow::OnCancelEdit()
{
SetIniEdit(false);
}
void COptionsWindow::LoadIniSection()
{
QString Section;
2022-01-14 21:28:44 +00:00
// Note: the service only caches sandboxie.ini not templates.ini, hence for global templates we need to load the section through the driver
2021-10-16 17:24:16 +01:00
if (m_Template && m_pBox->GetName().mid(9, 6).compare("Local_", Qt::CaseInsensitive) != 0)
{
m_Settings = m_pBox->GetIniSection(NULL, m_Template);
2021-10-16 16:19:51 +01:00
2021-10-16 17:24:16 +01:00
for (QList<QPair<QString, QString>>::const_iterator I = m_Settings.begin(); I != m_Settings.end(); ++I)
Section += I->first + "=" + I->second + "\n";
}
else
Section = m_pBox->GetAPI()->SbieIniGetEx(m_pBox->GetName(), "");
2021-10-16 16:19:51 +01:00
2021-11-13 08:28:32 +00:00
m_HoldChange = true;
2021-10-16 16:19:51 +01:00
ui.txtIniSection->setPlainText(Section);
2021-11-13 08:28:32 +00:00
m_HoldChange = false;
2021-10-16 16:19:51 +01:00
}
void COptionsWindow::SaveIniSection()
{
m_ConfigDirty = true;
/*m_pBox->SetRefreshOnChange(false);
// Note: an incremental update would be more elegant but it would change the entry order in the ini,
// hence it's better for the user to fully rebuild the section each time.
//
for (QList<QPair<QString, QString>>::const_iterator I = m_Settings.begin(); I != m_Settings.end(); ++I)
m_pBox->DelValue(I->first, I->second);
//QList<QPair<QString, QString>> NewSettings;
//QList<QPair<QString, QString>> OldSettings = m_Settings;
QStringList Section = SplitStr(ui.txtIniSection->toPlainText(), "\n");
foreach(const QString& Line, Section)
{
if (Line.isEmpty())
return;
StrPair Settings = Split2(Line, "=");
//if (!OldSettings.removeOne(Settings))
// NewSettings.append(Settings);
m_pBox->InsertText(Settings.first, Settings.second);
}
//for (QList<QPair<QString, QString>>::const_iterator I = OldSettings.begin(); I != OldSettings.end(); ++I)
// m_pBox->DelValue(I->first, I->second);
//
//for (QList<QPair<QString, QString>>::const_iterator I = NewSettings.begin(); I != NewSettings.end(); ++I)
// m_pBox->InsertText(I->first, I->second);
m_pBox->SetRefreshOnChange(true);
m_pBox->GetAPI()->CommitIniChanges();*/
m_pBox->GetAPI()->SbieIniSet(m_pBox->GetName(), "", ui.txtIniSection->toPlainText());
LoadIniSection();
}
2022-01-29 09:18:22 +00:00
2022-06-08 16:23:19 +01:00
#include "OptionsAccess.cpp"
#include "OptionsAdvanced.cpp"
#include "OptionsForce.cpp"
#include "OptionsGeneral.cpp"
#include "OptionsGrouping.cpp"
#include "OptionsNetwork.cpp"
#include "OptionsRecovery.cpp"
#include "OptionsStart.cpp"
#include "OptionsStop.cpp"
#include "OptionsTemplates.cpp"
2022-01-29 09:18:22 +00:00
#include <windows.h>
void COptionsWindow::TriggerPathReload()
{
//
// this message makes all boxes reload thair path presets
//
DWORD bsm_app = BSM_APPLICATIONS;
BroadcastSystemMessage(BSF_POSTMESSAGE, &bsm_app, WM_DEVICECHANGE, 'sb', 0);
}