Sandboxie/SandboxiePlus/SandMan/Windows/OptionsWindow.cpp

1023 lines
32 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 10:58:39 +01:00
#include "../../MiscHelpers/Common/NeonEffect.h"
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;
};
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;
};
2021-10-16 16:19:51 +01:00
COptionsWindow::COptionsWindow(const QSharedPointer<CSbieIni>& pBox, const QString& Name, QWidget *parent)
: QDialog(parent)
{
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);
this->setWindowTitle(tr("Sandboxie Plus - '%1' Options").arg(Name));
ui.tabs->setTabPosition(QTabWidget::West);
ui.tabs->tabBar()->setStyle(new CustomTabStyle(ui.tabs->tabBar()->style()));
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 10:58:39 +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;
if (iOptionTree)
{
QWidget* pAltView = new QWidget(this);
QGridLayout* pLayout = new QGridLayout(pAltView);
pLayout->setMargin(0);
m_pTree = new QTreeWidget();
m_pTree->setHeaderHidden(true);
m_pTree->setMinimumWidth(200);
2022-08-15 10:58:39 +01:00
QStyle* pStyle = QStyleFactory::create("windows"); // show lines
2022-07-09 15:29:16 +01:00
m_pTree->setStyle(pStyle);
connect(m_pTree, SIGNAL(itemClicked(QTreeWidgetItem*, int)), this, SLOT(OnItemClicked(QTreeWidgetItem*, int)));
2022-08-15 10:58:39 +01:00
QLineEdit* pSearch = new QLineEdit();
pSearch->setPlaceholderText(tr("Search Option"));
QObject::connect(pSearch, SIGNAL(returnPressed()), this, SLOT(OnSearchOption()));
m_SearchI = m_SearchJ = m_SearchP = 0;
m_LastFound = NULL;
pLayout->addWidget(pSearch, 0, 0);
pLayout->addWidget(m_pTree, 1, 0);
2022-07-09 15:29:16 +01:00
m_pStack = new QStackedLayout();
m_pStack->setMargin(0);
2022-08-15 10:58:39 +01:00
pLayout->addLayout(m_pStack, 0, 1, 2, 1);
2022-07-09 15:29:16 +01:00
for (int i = 0, k = 0; i < ui.tabs->count(); i++, k++) {
QTreeWidgetItem* pItem = new QTreeWidgetItem(QStringList() << ui.tabs->tabText(i));
m_pTree->addTopLevelItem(pItem);
pItem->setData(1, Qt::UserRole, k);
pItem->setIcon(0, ui.tabs->tabIcon(i));
QGridLayout* pGrid = qobject_cast<QGridLayout*>(ui.tabs->widget(i)->layout());
QTabWidget* pTabs = pGrid ? qobject_cast<QTabWidget*>(pGrid->itemAt(0)->widget()) : NULL;
if (!pTabs) {
pItem->setData(0, Qt::UserRole, m_pStack->count());
m_pStack->addWidget(ui.tabs->widget(i--));
}
else {
//pItem->setData(0, Qt::UserRole, -1);
//pItem->setFlags(pItem->flags() & ~Qt::ItemIsSelectable);
pItem->setData(0, Qt::UserRole, m_pStack->count()); // take the first tab for the parent entry
for (int j = 0; j < pTabs->count(); j++) {
QTreeWidgetItem* pSubItem = new QTreeWidgetItem(QStringList() << pTabs->tabText(j));
pItem->addChild(pSubItem);
pSubItem->setData(0, Qt::UserRole, m_pStack->count());
m_pStack->addWidget(pTabs->widget(j--));
}
}
}
m_pTree->expandAll();
ui.verticalLayout->replaceWidget(ui.tabs, pAltView);
}
else {
m_pStack = NULL;
m_pTree = NULL;
}
}
2022-08-15 10:58:39 +01:00
template <class T>
bool COptionsWindow__CompareText(T pWidget, const QString& Text) {
QString Str = pWidget->text();
if (!Str.toLower().contains(Text))
return false;
qDebug() << Str;
return true;
}
QWidget* COptionsWindow__SearchWidget(QWidget* pParent, const QString& Text, int& Pos)
{
QList<QWidget*> Widgets = pParent->findChildren<QWidget*>();
for (Pos; Pos < Widgets.count(); Pos++) {
QWidget* pWidget = Widgets[Pos];
if (!pWidget->isHidden()) {
if (QCheckBox* pCheck = qobject_cast<QCheckBox*>(pWidget)) {
if (COptionsWindow__CompareText(pCheck, Text))
return pCheck;
}
else if (QLabel* pLabel = qobject_cast<QLabel*>(pWidget)) {
if (COptionsWindow__CompareText(pLabel, Text))
return pLabel;
}
}
}
Pos = 0;
return NULL;
}
QWidget* COptionsWindow__SearchOption(QTreeWidget* pTree, QStackedLayout* pStack, const QString& Text, int& I, int& J, int& Pos)
{
for (; I < pTree->topLevelItemCount(); I++) {
QTreeWidgetItem* pItem = pTree->topLevelItem(I);
if (pItem->childCount() == 0) {
int Index = pItem->data(0, Qt::UserRole).toInt();
if (QWidget* pWidget = COptionsWindow__SearchWidget(pStack->widget(Index), Text, Pos))
return pWidget;
}
else {
for (; J < pItem->childCount(); J++) {
QTreeWidgetItem* pSubItem = pItem->child(J);
int Index = pSubItem->data(0, Qt::UserRole).toInt();
if (QWidget* pWidget = COptionsWindow__SearchWidget(pStack->widget(Index), Text, Pos))
return pWidget;
}
J = 0;
}
}
I = 0;
return NULL;
}
void COptionsWindow::OnSearchOption()
{
QLineEdit* pSearch = (QLineEdit*)sender();
QString Text = pSearch->text().toLower();
if (m_LastFound) {
//m_LastFound->setPalette(QApplication::palette());
//m_LastFound->setAutoFillBackground(false);
m_LastFound->setGraphicsEffect(NULL);
}
QWidget* pWidget = COptionsWindow__SearchOption(m_pTree, m_pStack, Text, m_SearchI, m_SearchJ, m_SearchP);
if (!pWidget) {
QApplication::beep();
return;
}
m_SearchP++; // move index to the next for the next search
QTreeWidgetItem* pItem = m_pTree->topLevelItem(m_SearchI);
if (pItem && pItem->childCount() > 0)
pItem = pItem->child(m_SearchJ);
if (!pItem) return;
//m_pTree->setItemSelected(pItem, true);
m_pTree->setCurrentItem(pItem, 0);
OnItemClicked(pItem, 0);
m_LastFound = pWidget;
//QPalette palette = m_LastFound->palette();
//palette.setColor(QPalette::Button, Qt::red);
//palette.setColor(QPalette::Base, Qt::red);
//palette.setColor(QPalette::Window, Qt::red);
//m_LastFound->setAutoFillBackground(true);
//m_LastFound->setPalette(palette);
auto neon = new CNeonEffect(5, 4);
neon->setGlowColor(Qt::red);
//neon->setColor(Qt::black);
m_LastFound->setGraphicsEffect(neon);
}
2022-07-09 15:29:16 +01:00
void COptionsWindow::OnItemClicked(QTreeWidgetItem* pItem, int Column)
{
int Index = pItem->data(0, Qt::UserRole).toInt();
if (Index != -1)
m_pStack->setCurrentIndex(Index);
QTreeWidgetItem* pRootItem = pItem;
while (pRootItem->parent()) pRootItem = pRootItem->parent();
int RootIndex = pRootItem->data(1, Qt::UserRole).toInt();
if (m_iCurrentTab != RootIndex)
OnTab(RootIndex);
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);
}