Sandboxie/SandboxiePlus/SandMan/Windows/SettingsWindow.cpp

388 lines
13 KiB
C++
Raw Normal View History

2020-07-19 21:09:02 +01:00
#include "stdafx.h"
#include "SettingsWindow.h"
#include "SandMan.h"
#include "../MiscHelpers/Common/Settings.h"
#include "Helpers/WinAdmin.h"
2020-09-05 16:45:39 +01:00
#include "../QSbieAPI/Sandboxie/SbieTemplates.h"
2020-09-12 09:09:24 +01:00
#include "../QSbieAPI/SbieUtils.h"
2020-07-19 21:09:02 +01:00
CSettingsWindow::CSettingsWindow(QWidget *parent)
: QMainWindow(parent)
{
QWidget* centralWidget = new QWidget();
ui.setupUi(centralWidget);
this->setCentralWidget(centralWidget);
this->setWindowTitle(tr("Sandboxie Plus - Settings"));
2020-09-12 09:09:24 +01:00
ui.uiLang->addItem("International English", "");
2020-07-19 21:09:02 +01:00
QDir langDir(QApplication::applicationDirPath() + "/translations/");
foreach(const QString& langFile, langDir.entryList(QStringList("taskexplorer_*.qm"), QDir::Files))
{
QString Code = langFile.mid(13, langFile.length() - 13 - 3);
QLocale Locale(Code);
QString Lang = Locale.nativeLanguageName();
ui.uiLang->addItem(Lang, Code);
}
ui.uiLang->setCurrentIndex(ui.uiLang->findData(theConf->GetString("Options/Language")));
ui.chkAutoStart->setChecked(IsAutorunEnabled());
2020-12-07 16:34:20 +00:00
switch (theConf->GetInt("Options/CheckForUpdates", 2)) {
case 0: ui.chkAutoUpdate->setCheckState(Qt::Unchecked); break;
case 1: ui.chkAutoUpdate->setCheckState(Qt::Checked); break;
case 2: ui.chkAutoUpdate->setCheckState(Qt::PartiallyChecked); break;
}
2020-09-12 09:09:24 +01:00
ui.chkShellMenu->setCheckState((Qt::CheckState)CSbieUtils::IsContextMenu());
2020-07-19 21:09:02 +01:00
ui.chkDarkTheme->setChecked(theConf->GetBool("Options/DarkTheme", false));
ui.chkNotifications->setChecked(theConf->GetBool("Options/ShowNotifications", true));
ui.chkWatchConfig->setChecked(theConf->GetBool("Options/WatchIni", true));
ui.onClose->addItem(tr("Close to Tray"), "ToTray");
ui.onClose->addItem(tr("Prompt before Close"), "Prompt");
ui.onClose->addItem(tr("Close"), "Close");
ui.onClose->setCurrentIndex(ui.onClose->findData(theConf->GetString("Options/OnClose", "ToTray")));
ui.chkShowTray->setChecked(theConf->GetBool("Options/ShowSysTray", true));
connect(ui.chkShowTray, SIGNAL(stateChanged(int)), this, SLOT(OnChange()));
//connect(ui.chkUseCycles, SIGNAL(stateChanged(int)), this, SLOT(OnChange()));
2020-09-12 09:09:24 +01:00
if (theAPI->IsConnected())
{
ui.fileRoot->setText(theAPI->GetGlobalSettings()->GetText("FileRootPath"));
ui.chkSeparateUserFolders->setChecked(theAPI->GetGlobalSettings()->GetBool("SeparateUserFolders", true));
ui.regRoot->setText(theAPI->GetGlobalSettings()->GetText("KeyRootPath"));
ui.ipcRoot->setText(theAPI->GetGlobalSettings()->GetText("IpcRootPath"));
ui.chkAdminOnly->setChecked(theAPI->GetGlobalSettings()->GetBool("EditAdminOnly", false));
ui.chkPassRequired->setChecked(!theAPI->GetGlobalSettings()->GetText("EditPassword", "").isEmpty());
connect(ui.chkPassRequired, SIGNAL(stateChanged(int)), this, SLOT(OnChange()));
connect(ui.btnSetPassword, SIGNAL(pressed()), this, SLOT(OnSetPassword()));
ui.chkAdminOnlyFP->setChecked(theAPI->GetGlobalSettings()->GetBool("ForceDisableAdminOnly", false));
ui.chkClearPass->setChecked(theAPI->GetGlobalSettings()->GetBool("ForgetPassword", false));
2020-11-28 16:20:56 +00:00
ui.chkStartBlock->setChecked(theAPI->GetGlobalSettings()->GetBool("StartRunAlertDenied", false));
connect(ui.chkStartBlock, SIGNAL(stateChanged(int)), this, SLOT(OnWarnChanged()));
ui.chkStartBlockMsg->setChecked(theAPI->GetGlobalSettings()->GetBool("NotifyStartRunAccessDenied", true));
connect(ui.chkStartBlockMsg, SIGNAL(stateChanged(int)), this, SLOT(OnWarnChanged()));
2020-09-12 09:09:24 +01:00
connect(ui.btnAddWarnProg, SIGNAL(pressed()), this, SLOT(OnAddWarnProg()));
2020-11-28 16:20:56 +00:00
connect(ui.btnAddWarnFolder, SIGNAL(pressed()), this, SLOT(OnAddWarnFolder()));
2020-09-12 09:09:24 +01:00
connect(ui.btnDelWarnProg, SIGNAL(pressed()), this, SLOT(OnDelWarnProg()));
2020-11-28 16:20:56 +00:00
foreach(const QString& Value, theAPI->GetGlobalSettings()->GetTextList("AlertProcess", false))
AddWarnEntry(Value, 1);
foreach(const QString& Value, theAPI->GetGlobalSettings()->GetTextList("AlertFolder", false))
AddWarnEntry(Value, 2);
2020-09-12 09:09:24 +01:00
}
else
{
ui.fileRoot->setEnabled(false);
ui.chkSeparateUserFolders->setEnabled(false);
ui.regRoot->setEnabled(false);
ui.ipcRoot->setEnabled(false);
ui.chkAdminOnly->setEnabled(false);
ui.chkPassRequired->setEnabled(false);
ui.chkAdminOnlyFP->setEnabled(false);
ui.chkClearPass->setEnabled(false);
ui.btnSetPassword->setEnabled(false);
ui.treeWarnProgs->setEnabled(false);
ui.btnAddWarnProg->setEnabled(false);
ui.btnDelWarnProg->setEnabled(false);
ui.treeCompat->setEnabled(false);
ui.btnAddCompat->setEnabled(false);
ui.btnDelCompat->setEnabled(false);
2020-09-05 16:45:39 +01:00
}
m_WarnProgsChanged = false;
connect(ui.btnAddCompat, SIGNAL(pressed()), this, SLOT(OnAddCompat()));
connect(ui.btnDelCompat, SIGNAL(pressed()), this, SLOT(OnDelCompat()));
m_CompatLoaded = 0;
m_CompatChanged = false;
ui.chkNoCompat->setChecked(!theConf->GetBool("Options/AutoRunSoftCompat", true));
connect(ui.treeCompat, SIGNAL(itemClicked(QTreeWidgetItem*, int)), this, SLOT(OnTemplateClicked(QTreeWidgetItem*, int)));
connect(ui.tabs, SIGNAL(currentChanged(int)), this, SLOT(OnTab()));
2020-07-19 21:09:02 +01:00
connect(ui.buttonBox->button(QDialogButtonBox::Ok), SIGNAL(pressed()), this, SLOT(accept()));
connect(ui.buttonBox->button(QDialogButtonBox::Apply), SIGNAL(pressed()), this, SLOT(apply()));
connect(ui.buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
restoreGeometry(theConf->GetBlob("SettingsWindow/Window_Geometry"));
OnChange();
}
CSettingsWindow::~CSettingsWindow()
{
theConf->SetBlob("SettingsWindow/Window_Geometry",saveGeometry());
}
2020-09-05 16:45:39 +01:00
void CSettingsWindow::showCompat()
{
m_CompatLoaded = 2;
ui.tabs->setCurrentWidget(ui.tabCompat);
show();
}
2020-07-19 21:09:02 +01:00
void CSettingsWindow::closeEvent(QCloseEvent *e)
{
this->deleteLater();
}
void CSettingsWindow::apply()
{
theConf->SetValue("Options/Language", ui.uiLang->currentData());
theConf->SetValue("Options/DarkTheme", ui.chkDarkTheme->isChecked());
AutorunEnable(ui.chkAutoStart->isChecked());
2020-12-07 16:34:20 +00:00
switch (ui.chkAutoUpdate->checkState()) {
case Qt::Unchecked: theConf->SetValue("Options/CheckForUpdates", 0); break;
case Qt::PartiallyChecked: theConf->SetValue("Options/CheckForUpdates", 2); break;
case Qt::Checked: theConf->SetValue("Options/CheckForUpdates", 1); break;
}
2020-09-12 09:09:24 +01:00
if (ui.chkShellMenu->checkState() != CSbieUtils::IsContextMenu())
{
if (ui.chkShellMenu->isChecked())
CSbieUtils::AddContextMenu(QApplication::applicationDirPath().replace("/", "\\") + "\\Start.exe");
else
CSbieUtils::RemoveContextMenu();
}
2020-07-19 21:09:02 +01:00
theConf->SetValue("Options/ShowNotifications", ui.chkNotifications->isChecked());
theConf->SetValue("Options/WatchIni", ui.chkWatchConfig->isChecked());
theConf->SetValue("Options/OnClose", ui.onClose->currentData());
theConf->SetValue("Options/ShowSysTray", ui.chkShowTray->isChecked());
2020-09-12 09:09:24 +01:00
if (theAPI->IsConnected())
2020-07-19 21:09:02 +01:00
{
2020-09-12 09:09:24 +01:00
if (ui.fileRoot->text().isEmpty())
ui.fileRoot->setText("\\??\\%SystemDrive%\\Sandbox\\%USER%\\%SANDBOX%");
theAPI->GetGlobalSettings()->SetText("FileRootPath", ui.fileRoot->text());
theAPI->GetGlobalSettings()->SetBool("SeparateUserFolders", ui.chkSeparateUserFolders->isChecked());
if (ui.regRoot->text().isEmpty())
ui.regRoot->setText("\\REGISTRY\\USER\\Sandbox_%USER%_%SANDBOX%");
theAPI->GetGlobalSettings()->SetText("KeyRootPath", ui.regRoot->text());
if (ui.ipcRoot->text().isEmpty())
ui.ipcRoot->setText("\\Sandbox\\%USER%\\%SANDBOX%\\Session_%SESSION%");
theAPI->GetGlobalSettings()->SetText("IpcRootPath", ui.ipcRoot->text());
theAPI->GetGlobalSettings()->SetBool("EditAdminOnly", ui.chkAdminOnly->isChecked());
bool isPassSet = !theAPI->GetGlobalSettings()->GetText("EditPassword", "").isEmpty();
if (ui.chkPassRequired->isChecked())
{
if (!isPassSet && m_NewPassword.isEmpty())
OnSetPassword(); // request password entry if it wasn't entered already
2020-09-12 09:09:24 +01:00
if (!m_NewPassword.isEmpty()) {
theAPI->LockConfig(m_NewPassword); // set new/changed password
m_NewPassword.clear();
}
2020-07-19 21:09:02 +01:00
}
2020-09-12 09:09:24 +01:00
else if (isPassSet)
theAPI->LockConfig(QString()); // clear password
theAPI->GetGlobalSettings()->SetBool("ForceDisableAdminOnly", ui.chkAdminOnlyFP->isChecked());
theAPI->GetGlobalSettings()->SetBool("ForgetPassword", ui.chkClearPass->isChecked());
if (m_WarnProgsChanged)
{
2020-11-28 16:20:56 +00:00
theAPI->GetGlobalSettings()->SetBool("StartRunAlertDenied", ui.chkStartBlock->isChecked());
theAPI->GetGlobalSettings()->SetBool("NotifyStartRunAccessDenied", ui.chkStartBlockMsg->isChecked());
QStringList AlertProcess;
QStringList AlertFolder;
for (int i = 0; i < ui.treeWarnProgs->topLevelItemCount(); i++)
{
2020-09-12 09:09:24 +01:00
QTreeWidgetItem* pItem = ui.treeWarnProgs->topLevelItem(i);
2020-11-28 16:20:56 +00:00
int Type = pItem->data(0, Qt::UserRole).toInt();
switch (Type)
{
case 1: AlertProcess.append(pItem->data(1, Qt::UserRole).toString()); break;
case 2: AlertFolder.append(pItem->data(1, Qt::UserRole).toString()); break;
}
2020-09-12 09:09:24 +01:00
}
2020-11-28 16:20:56 +00:00
theAPI->GetGlobalSettings()->UpdateTextList("AlertProcess", AlertProcess, false);
theAPI->GetGlobalSettings()->UpdateTextList("AlertFolder", AlertFolder, false);
2020-09-12 09:09:24 +01:00
m_WarnProgsChanged = false;
2020-09-05 16:45:39 +01:00
}
2020-09-12 09:09:24 +01:00
if (m_CompatChanged)
{
QStringList Used;
QStringList Rejected;
for (int i = 0; i < ui.treeCompat->topLevelItemCount(); i++) {
QTreeWidgetItem* pItem = ui.treeCompat->topLevelItem(i);
if (pItem->checkState(0) == Qt::Checked)
2020-11-16 16:15:03 +00:00
Used.append(pItem->data(0, Qt::UserRole).toString());
2020-09-12 09:09:24 +01:00
else
2020-11-16 16:15:03 +00:00
Rejected.append(pItem->data(0, Qt::UserRole).toString());
2020-09-12 09:09:24 +01:00
}
2020-11-16 16:15:03 +00:00
theAPI->GetGlobalSettings()->UpdateTextList("Template", Used, false);
theAPI->GetGlobalSettings()->UpdateTextList("TemplateReject", Rejected, false);
2020-09-12 09:09:24 +01:00
m_CompatChanged = false;
2020-09-05 16:45:39 +01:00
}
}
theConf->SetValue("Options/AutoRunSoftCompat", !ui.chkNoCompat->isChecked());
2020-07-19 21:09:02 +01:00
emit OptionsChanged();
}
void CSettingsWindow::accept()
{
apply();
this->close();
}
void CSettingsWindow::reject()
{
this->close();
}
void CSettingsWindow::OnChange()
{
//ui.chkLinuxStyle->setEnabled(!ui.chkUseCycles->isChecked());
QStandardItemModel *model = qobject_cast<QStandardItemModel *>(ui.onClose->model());
QStandardItem *item = model->item(0);
item->setFlags((!ui.chkShowTray->isChecked()) ? item->flags() & ~Qt::ItemIsEnabled : item->flags() | Qt::ItemIsEnabled);
ui.btnSetPassword->setEnabled(ui.chkPassRequired->isChecked());
}
2020-09-05 16:45:39 +01:00
void CSettingsWindow::OnTab()
{
2020-09-12 09:09:24 +01:00
if (ui.tabs->currentWidget() == ui.tabCompat && m_CompatLoaded != 1 && theAPI->IsConnected())
2020-09-05 16:45:39 +01:00
{
if(m_CompatLoaded == 0)
theGUI->GetCompat()->RunCheck();
ui.treeCompat->clear();
QMap<QString, int> Templates = theGUI->GetCompat()->GetTemplates();
for (QMap<QString, int>::iterator I = Templates.begin(); I != Templates.end(); ++I)
{
if (I.value() == CSbieTemplates::eNone)
continue;
2020-11-16 16:15:03 +00:00
QSharedPointer<CSbieIni> pTemplate = QSharedPointer<CSbieIni>(new CSbieIni("Template_" + I.key(), theAPI));
2020-09-05 16:45:39 +01:00
QTreeWidgetItem* pItem = new QTreeWidgetItem();
2020-11-16 16:15:03 +00:00
pItem->setText(0, pTemplate->GetText("Tmpl.Title"));
pItem->setData(0, Qt::UserRole, I.key());
2020-09-05 16:45:39 +01:00
pItem->setCheckState(0, (I.value() & CSbieTemplates::eEnabled) ? Qt::Checked : Qt::Unchecked);
ui.treeCompat->addTopLevelItem(pItem);
}
m_CompatLoaded = 1;
m_CompatChanged = false;
}
}
2020-07-19 21:09:02 +01:00
void CSettingsWindow::OnSetPassword()
{
retry:
QString Value1 = QInputDialog::getText(this, "Sandboxie-Plus", tr("Please enter the new configuration password."), QLineEdit::Password);
if (Value1.isEmpty())
return;
QString Value2 = QInputDialog::getText(this, "Sandboxie-Plus", tr("Please re-enter the new configuration password."), QLineEdit::Password);
2020-07-19 21:09:02 +01:00
if (Value2.isEmpty())
return;
if (Value1 != Value2) {
QMessageBox::warning(this, "Sandboxie-Plus", tr("Passwords did not match, please retry."));
goto retry;
}
m_NewPassword = Value1;
2020-09-05 16:45:39 +01:00
}
2020-11-28 16:20:56 +00:00
void CSettingsWindow::AddWarnEntry(const QString& Name, int type)
{
QTreeWidgetItem* pItem = new QTreeWidgetItem();
pItem->setText(0, (type == 1 ? tr("Process") : tr("Folder")));
pItem->setData(0, Qt::UserRole, type);
pItem->setData(1, Qt::UserRole, Name);
pItem->setText(1, Name);
ui.treeWarnProgs->addTopLevelItem(pItem);
}
2020-09-05 16:45:39 +01:00
void CSettingsWindow::OnAddWarnProg()
{
2020-11-28 16:20:56 +00:00
QString Value = QInputDialog::getText(this, "Sandboxie-Plus", tr("Please enter a program file name"));
2020-09-05 16:45:39 +01:00
if (Value.isEmpty())
return;
2020-11-28 16:20:56 +00:00
AddWarnEntry(Value, 1);
m_WarnProgsChanged = true;
}
2020-09-05 16:45:39 +01:00
2020-11-28 16:20:56 +00:00
void CSettingsWindow::OnAddWarnFolder()
{
2020-12-22 14:50:58 +00:00
QString Value = QFileDialog::getExistingDirectory(this, tr("Select Directory")).replace("/", "\\");
2020-11-28 16:20:56 +00:00
if (Value.isEmpty())
return;
2020-12-22 14:50:58 +00:00
AddWarnEntry(Value, 2);
2020-09-05 16:45:39 +01:00
m_WarnProgsChanged = true;
}
void CSettingsWindow::OnDelWarnProg()
{
QTreeWidgetItem* pItem = ui.treeWarnProgs->currentItem();
if (!pItem)
return;
delete pItem;
m_WarnProgsChanged = true;
}
void CSettingsWindow::OnTemplateClicked(QTreeWidgetItem* pItem, int Column)
{
// todo: check if really changed
m_CompatChanged = true;
}
void CSettingsWindow::OnAddCompat()
{
QTreeWidgetItem* pItem = ui.treeCompat->currentItem();
if (!pItem)
return;
pItem->setCheckState(0, Qt::Checked);
m_CompatChanged = true;
}
void CSettingsWindow::OnDelCompat()
{
QTreeWidgetItem* pItem = ui.treeCompat->currentItem();
if (!pItem)
return;
pItem->setCheckState(0, Qt::Unchecked);
m_CompatChanged = true;
}