Sandboxie/SandboxiePlus/SandMan/Windows/OptionsGrouping.cpp

289 lines
7.7 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"
void COptionsWindow::LoadGroups()
{
ui.treeGroups->clear();
QStringList ProcessGroups = m_pBox->GetTextList("ProcessGroup", m_Template);
foreach(const QString& Group, ProcessGroups)
{
QStringList Entries = Group.split(",");
QString GroupName = Entries.takeFirst();
QTreeWidgetItem* pItem = new QTreeWidgetItem();
pItem->setData(0, Qt::UserRole, GroupName);
if (GroupName.length() > 2)
GroupName = GroupName.mid(1, GroupName.length() - 2);
pItem->setText(0, GroupName);
for (int i = 0; i < Entries.count(); i++)
{
if (Entries[i].isEmpty())
continue;
QTreeWidgetItem* pSubItem = new QTreeWidgetItem();
SetProgramItem(Entries[i], pSubItem, 0);
2021-12-03 19:26:09 +00:00
pSubItem->setFlags(pSubItem->flags() | Qt::ItemIsEditable);
2021-10-16 16:19:51 +01:00
pItem->addChild(pSubItem);
}
2021-12-03 19:26:09 +00:00
pItem->setFlags(pItem->flags() | Qt::ItemIsEditable);
2021-10-16 16:19:51 +01:00
ui.treeGroups->addTopLevelItem(pItem);
}
LoadGroupsTmpl();
ui.treeGroups->expandAll();
m_GroupsChanged = false;
}
void COptionsWindow::LoadGroupsTmpl(bool bUpdate)
{
if (ui.chkShowGroupTmpl->isChecked())
{
foreach(const QString& Template, m_pBox->GetTemplates())
{
foreach(const QString& Group, m_pBox->GetTextListTmpl("ProcessGroup", Template))
{
QStringList Entries = Group.split(",");
QString GroupName = Entries.takeFirst();
QTreeWidgetItem* pItem = new QTreeWidgetItem();
if (GroupName.length() > 2)
GroupName = GroupName.mid(1, GroupName.length() - 2);
pItem->setText(0, GroupName + " (" + Template + ")");
for (int i = 0; i < Entries.count(); i++)
{
if (Entries[i].isEmpty())
continue;
QTreeWidgetItem* pSubItem = new QTreeWidgetItem();
SetProgramItem(Entries[i], pSubItem, 0);
pItem->addChild(pSubItem);
}
ui.treeGroups->addTopLevelItem(pItem);
}
}
if (bUpdate)
ui.treeGroups->expandAll();
}
else if (bUpdate)
{
for (int i = 0; i < ui.treeGroups->topLevelItemCount(); )
{
QTreeWidgetItem* pItem = ui.treeGroups->topLevelItem(i);
QString GroupName = pItem->data(0, Qt::UserRole).toString();
if (GroupName.isEmpty()) {
delete pItem;
continue; // entry from template
}
i++;
}
}
}
2021-11-13 08:28:32 +00:00
QStringList COptionsWindow::GetCurrentGroups()
{
QStringList Groups;
for (int i = 0; i < ui.treeGroups->topLevelItemCount(); i++)
{
QTreeWidgetItem* pItem = ui.treeGroups->topLevelItem(i);
QString GroupName = pItem->data(0, Qt::UserRole).toString();
Groups.append(GroupName);
}
foreach(const QString& Template, m_pBox->GetTemplates())
{
foreach(const QString& Group, m_pBox->GetTextListTmpl("ProcessGroup", Template))
{
QStringList Entries = Group.split(",");
QString GroupName = Entries.takeFirst();
if (!Groups.contains(GroupName))
Groups.append(GroupName);
}
}
return Groups;
}
2021-10-16 16:19:51 +01:00
void COptionsWindow::SaveGroups()
{
QStringList ProcessGroups;
for (int i = 0; i < ui.treeGroups->topLevelItemCount(); i++)
{
QTreeWidgetItem* pItem = ui.treeGroups->topLevelItem(i);
QString GroupName = pItem->data(0, Qt::UserRole).toString();
if (GroupName.isEmpty())
2022-12-07 16:32:40 +00:00
continue; // this is a template entry, don't save
2021-10-16 16:19:51 +01:00
QStringList Programs;
for (int j = 0; j < pItem->childCount(); j++)
Programs.append(pItem->child(j)->data(0, Qt::UserRole).toString());
QString Group = GroupName + "," + Programs.join(",");
ProcessGroups.append(Group);
}
WriteTextList("ProcessGroup", ProcessGroups);
m_GroupsChanged = false;
}
void COptionsWindow::OnAddGroup()
{
QString Value = QInputDialog::getText(this, "Sandboxie-Plus", tr("Please enter a name for the new group"), QLineEdit::Normal, "NewGroup");
if (Value.isEmpty())
return;
for (int i = 0; i < ui.treeGroups->topLevelItemCount(); i++) {
QTreeWidgetItem* pItem = ui.treeGroups->topLevelItem(i);
if (pItem->text(0).compare(Value, Qt::CaseInsensitive) == 0)
return;
}
QTreeWidgetItem* pItem = new QTreeWidgetItem();
pItem->setText(0, Value);
pItem->setData(0, Qt::UserRole, "<" + Value + ">");
2021-12-03 19:26:09 +00:00
pItem->setFlags(pItem->flags() | Qt::ItemIsEditable);
2021-10-16 16:19:51 +01:00
ui.treeGroups->addTopLevelItem(pItem);
m_GroupsChanged = true;
2021-11-13 08:28:32 +00:00
OnOptChanged();
2021-10-16 16:19:51 +01:00
}
void COptionsWindow::AddProgramToGroup(const QString& Program, const QString& Group)
{
QTreeWidgetItem* pItem = FindGroupByName(Group, true);
QTreeWidgetItem* pSubItem = new QTreeWidgetItem();
SetProgramItem(Program, pSubItem, 0);
2021-12-03 19:26:09 +00:00
pSubItem->setFlags(pSubItem->flags() | Qt::ItemIsEditable);
2021-10-16 16:19:51 +01:00
pItem->addChild(pSubItem);
m_GroupsChanged = true;
2021-11-13 08:28:32 +00:00
OnOptChanged();
2021-10-16 16:19:51 +01:00
}
2021-12-03 19:26:09 +00:00
bool COptionsWindow::DelProgramFromGroup(const QString& Program, const QString& Group)
2021-10-16 16:19:51 +01:00
{
2022-10-26 11:49:38 +01:00
QTreeWidgetItem* pItem = FindGroupByName(Group, false);
if (!pItem) return false;
2021-10-16 16:19:51 +01:00
2021-12-03 19:26:09 +00:00
bool bFound = false;
2021-10-16 16:19:51 +01:00
for (int j = 0; j < pItem->childCount(); j++){
QTreeWidgetItem* pProgItem = pItem->child(j);
if (pProgItem->data(0, Qt::UserRole).toString().compare(Program, Qt::CaseInsensitive) == 0) {
delete pProgItem;
2021-12-03 19:26:09 +00:00
bFound = true;
2022-10-26 11:49:38 +01:00
if (pItem->childCount() == 0)
delete pItem;
2021-10-16 16:19:51 +01:00
break;
}
}
m_GroupsChanged = true;
2021-11-13 08:28:32 +00:00
OnOptChanged();
2021-12-03 19:26:09 +00:00
return bFound;
2021-10-16 16:19:51 +01:00
}
QTreeWidgetItem* COptionsWindow::FindGroupByName(const QString& Group, bool bAdd)
{
for (int i = 0; i < ui.treeGroups->topLevelItemCount(); i++) {
QTreeWidgetItem* pItem = ui.treeGroups->topLevelItem(i);
if (pItem->data(0, Qt::UserRole).toString().compare(Group, Qt::CaseInsensitive) == 0)
return pItem;
}
if (bAdd)
{
QString GroupName = Group;
QTreeWidgetItem* pItem = new QTreeWidgetItem();
pItem->setData(0, Qt::UserRole, GroupName);
if (GroupName.length() > 2)
GroupName = GroupName.mid(1, GroupName.length() - 2);
pItem->setText(0, GroupName);
2021-12-03 19:26:09 +00:00
pItem->setFlags(pItem->flags() | Qt::ItemIsEditable);
2021-10-16 16:19:51 +01:00
ui.treeGroups->addTopLevelItem(pItem);
return pItem;
}
return NULL;
}
void COptionsWindow::OnAddProg()
{
QTreeWidgetItem* pItem = ui.treeGroups->currentItem();
while (pItem && pItem->parent())
pItem = pItem->parent();
if (!pItem)
{
QMessageBox::warning(this, "SandboxiePlus", tr("Please select group first."));
return;
}
if (pItem->data(0, Qt::UserRole).toString().isEmpty()) {
QMessageBox::warning(this, "SandboxiePlus", tr("Template values can not be edited."));
return;
}
QString Value = SelectProgram();
if (Value.isEmpty())
return;
QTreeWidgetItem* pSubItem = new QTreeWidgetItem();
SetProgramItem(Value, pSubItem, 0);
2021-12-03 19:26:09 +00:00
pSubItem->setFlags(pSubItem->flags() | Qt::ItemIsEditable);
2021-10-16 16:19:51 +01:00
pItem->addChild(pSubItem);
m_GroupsChanged = true;
2021-11-13 08:28:32 +00:00
OnOptChanged();
2021-10-16 16:19:51 +01:00
}
void COptionsWindow::OnDelProg()
{
QTreeWidgetItem* pItem = ui.treeGroups->currentItem();
if (!pItem)
return;
if ((pItem->parent() ? pItem->parent() : pItem)->data(0, Qt::UserRole).toString().isEmpty()) {
QMessageBox::warning(this, "SandboxiePlus", tr("Template values can not be edited."));
return;
}
delete pItem;
m_GroupsChanged = true;
2021-11-13 08:28:32 +00:00
OnOptChanged();
2021-10-16 16:19:51 +01:00
}
2022-12-07 16:32:40 +00:00
void COptionsWindow::CopyGroupToList(const QString& Group, QTreeWidget* pTree, bool disabled)
2021-10-16 16:19:51 +01:00
{
for (int i = 0; i < ui.treeGroups->topLevelItemCount(); i++)
{
QTreeWidgetItem* pItem = ui.treeGroups->topLevelItem(i);
2022-12-07 16:32:40 +00:00
if (pItem->data(0, Qt::UserRole).toString().compare(Group, Qt::CaseInsensitive) == 0)
2021-10-16 16:19:51 +01:00
{
for (int j = 0; j < pItem->childCount(); j++)
{
QString Value = pItem->child(j)->data(0, Qt::UserRole).toString();
QTreeWidgetItem* pSubItem = new QTreeWidgetItem();
2021-12-03 19:26:09 +00:00
pSubItem->setCheckState(0, disabled ? Qt::Unchecked : Qt::Checked);
2021-10-16 16:19:51 +01:00
SetProgramItem(Value, pSubItem, 0);
2021-12-03 19:26:09 +00:00
pSubItem->setFlags(pSubItem->flags() | Qt::ItemIsEditable);
2021-10-16 16:19:51 +01:00
pTree->addTopLevelItem(pSubItem);
}
break;
}
}
}