2020-06-01 17:11:56 +01:00
# include "stdafx.h"
# include "SbieView.h"
# include "..\SandMan.h"
# include "../QSbieAPI/SbieAPI.h"
# include "../../MiscHelpers/Common/SortFilterProxyModel.h"
# include "../../MiscHelpers/Common/Settings.h"
2020-07-19 21:09:02 +01:00
# include "../Windows/OptionsWindow.h"
2020-09-05 16:45:39 +01:00
# include "../Windows/SnapshotsWindow.h"
2020-06-01 17:11:56 +01:00
2020-10-10 17:18:01 +01:00
# include "qt_windows.h"
# include "qwindowdefs_win.h"
# include <shellapi.h>
2020-06-01 17:11:56 +01:00
CSbieView : : CSbieView ( QWidget * parent ) : CPanelView ( parent )
{
m_pMainLayout = new QVBoxLayout ( ) ;
m_pMainLayout - > setMargin ( 0 ) ;
this - > setLayout ( m_pMainLayout ) ;
m_pSbieModel = new CSbieModel ( ) ;
m_pSbieModel - > SetTree ( true ) ;
m_pSbieModel - > SetUseIcons ( true ) ;
m_pSortProxy = new CSortFilterProxyModel ( false , this ) ;
m_pSortProxy - > setSortRole ( Qt : : EditRole ) ;
m_pSortProxy - > setSourceModel ( m_pSbieModel ) ;
m_pSortProxy - > setDynamicSortFilter ( true ) ;
// SbieTree
m_pSbieTree = new QTreeViewEx ( ) ;
//m_pSbieTree->setItemDelegate(theGUI->GetItemDelegate());
m_pSbieTree - > setModel ( m_pSortProxy ) ;
m_pSbieTree - > setSelectionMode ( QAbstractItemView : : ExtendedSelection ) ;
m_pSbieTree - > setSortingEnabled ( true ) ;
m_pSbieTree - > setContextMenuPolicy ( Qt : : CustomContextMenu ) ;
connect ( m_pSbieTree , SIGNAL ( customContextMenuRequested ( const QPoint & ) ) , this , SLOT ( OnMenu ( const QPoint & ) ) ) ;
2020-07-19 21:09:02 +01:00
connect ( m_pSbieTree , SIGNAL ( doubleClicked ( const QModelIndex & ) ) , this , SLOT ( OnDoubleClicked ( const QModelIndex & ) ) ) ;
2020-06-01 17:11:56 +01:00
connect ( m_pSbieTree - > selectionModel ( ) , SIGNAL ( selectionChanged ( QItemSelection , QItemSelection ) ) , SLOT ( ProcessSelection ( QItemSelection , QItemSelection ) ) ) ;
//connect(theGUI, SIGNAL(ReloadPanels()), m_pSbieModel, SLOT(Clear()));
m_pMainLayout - > addWidget ( m_pSbieTree ) ;
//
m_pMainLayout - > addWidget ( new CFinder ( m_pSortProxy , this ) ) ;
2020-07-04 11:07:36 +01:00
connect ( m_pSbieModel , SIGNAL ( ToolTipCallback ( const QVariant & , QString & ) ) , this , SLOT ( OnToolTipCallback ( const QVariant & , QString & ) ) , Qt : : DirectConnection ) ;
2020-06-01 17:11:56 +01:00
m_pMenuRun = m_pMenu - > addMenu ( tr ( " Run " ) ) ;
m_pMenuRunAny = m_pMenuRun - > addAction ( tr ( " Run Program " ) , this , SLOT ( OnSandBoxAction ( ) ) ) ;
m_pMenuRunBrowser = m_pMenuRun - > addAction ( tr ( " Run Web Browser " ) , this , SLOT ( OnSandBoxAction ( ) ) ) ;
m_pMenuRunExplorer = m_pMenuRun - > addAction ( tr ( " Run Explorer " ) , this , SLOT ( OnSandBoxAction ( ) ) ) ;
m_pMenuRunCmd = m_pMenuRun - > addAction ( tr ( " Run Cmd.exe " ) , this , SLOT ( OnSandBoxAction ( ) ) ) ;
m_pMenuEmptyBox = m_pMenu - > addAction ( tr ( " Terminate All Programs " ) , this , SLOT ( OnSandBoxAction ( ) ) ) ;
m_pMenu - > addSeparator ( ) ;
2020-10-10 17:18:01 +01:00
m_pMenuExplore = m_pMenu - > addAction ( tr ( " Explore Content " ) , this , SLOT ( OnSandBoxAction ( ) ) ) ;
m_pMenu - > addSeparator ( ) ;
2020-09-05 16:45:39 +01:00
m_pMenuSnapshots = m_pMenu - > addAction ( tr ( " Snapshots Manager " ) , this , SLOT ( OnSandBoxAction ( ) ) ) ;
2020-06-01 17:11:56 +01:00
m_pMenuCleanUp = m_pMenu - > addAction ( tr ( " Delete Content " ) , this , SLOT ( OnSandBoxAction ( ) ) ) ;
m_pMenu - > addSeparator ( ) ;
2020-07-04 11:07:36 +01:00
m_pMenuPresets = m_pMenu - > addMenu ( tr ( " Sandbox Presets " ) ) ;
m_pMenuPresetsLogApi = m_pMenuPresets - > addAction ( tr ( " Enable API Call logging " ) , this , SLOT ( OnSandBoxAction ( ) ) ) ;
m_pMenuPresetsLogApi - > setCheckable ( true ) ;
m_pMenuPresetsINet = m_pMenuPresets - > addAction ( tr ( " Block Internet Access " ) , this , SLOT ( OnSandBoxAction ( ) ) ) ;
m_pMenuPresetsINet - > setCheckable ( true ) ;
m_pMenuPresetsShares = m_pMenuPresets - > addAction ( tr ( " Allow Network Shares " ) , this , SLOT ( OnSandBoxAction ( ) ) ) ;
m_pMenuPresetsShares - > setCheckable ( true ) ;
m_pMenuPresetsNoAdmin = m_pMenuPresets - > addAction ( tr ( " Drop Admin Rights " ) , this , SLOT ( OnSandBoxAction ( ) ) ) ;
m_pMenuPresetsNoAdmin - > setCheckable ( true ) ;
2020-07-19 21:09:02 +01:00
m_pMenuOptions = m_pMenu - > addAction ( tr ( " Sandbox Options " ) , this , SLOT ( OnSandBoxAction ( ) ) ) ;
2020-06-04 09:34:40 +01:00
m_pMenuRename = m_pMenu - > addAction ( tr ( " Rename Sandbox " ) , this , SLOT ( OnSandBoxAction ( ) ) ) ;
2020-06-01 17:11:56 +01:00
m_pMenuRemove = m_pMenu - > addAction ( tr ( " Remove Sandbox " ) , this , SLOT ( OnSandBoxAction ( ) ) ) ;
m_iMenuBox = m_pMenu - > actions ( ) . count ( ) ;
m_pMenuTerminate = m_pMenu - > addAction ( tr ( " Terminate " ) , this , SLOT ( OnProcessAction ( ) ) ) ;
2020-07-04 11:07:36 +01:00
m_pMenuTerminate - > setShortcut ( QKeySequence : : Delete ) ;
m_pMenuTerminate - > setShortcutContext ( Qt : : WidgetWithChildrenShortcut ) ;
this - > addAction ( m_pMenuTerminate ) ;
2020-06-01 17:11:56 +01:00
m_pMenuSuspend = m_pMenu - > addAction ( tr ( " Suspend " ) , this , SLOT ( OnProcessAction ( ) ) ) ;
m_pMenuResume = m_pMenu - > addAction ( tr ( " Resume " ) , this , SLOT ( OnProcessAction ( ) ) ) ;
m_iMenuProc = m_pMenu - > actions ( ) . count ( ) ;
QByteArray Columns = theConf - > GetBlob ( " GUI/BoxTree_Columns " ) ;
if ( Columns . isEmpty ( ) )
m_pSbieTree - > OnResetColumns ( ) ;
else
m_pSbieTree - > restoreState ( Columns ) ;
//m_pMenu = new QMenu();
AddPanelItemsToMenu ( ) ;
}
CSbieView : : ~ CSbieView ( )
{
theConf - > SetBlob ( " GUI/BoxTree_Columns " , m_pSbieTree - > saveState ( ) ) ;
}
void CSbieView : : Refresh ( )
{
QList < QVariant > Added = m_pSbieModel - > Sync ( theAPI - > GetAllBoxes ( ) ) ;
if ( m_pSbieModel - > IsTree ( ) )
{
QTimer : : singleShot ( 100 , this , [ this , Added ] ( ) {
foreach ( const QVariant ID , Added ) {
m_pSbieTree - > expand ( m_pSortProxy - > mapFromSource ( m_pSbieModel - > FindIndex ( ID ) ) ) ;
}
} ) ;
}
}
2020-07-04 11:07:36 +01:00
void CSbieView : : OnToolTipCallback ( const QVariant & ID , QString & ToolTip )
{
if ( ID . type ( ) = = QVariant : : String )
{
QString BoxName = ID . toString ( ) ;
CSandBoxPtr pBox = theAPI - > GetBoxByName ( BoxName ) ;
CSandBoxPlus * pBoxEx = qobject_cast < CSandBoxPlus * > ( pBox . data ( ) ) ;
if ( ! pBoxEx )
return ;
// todo more info
ToolTip = BoxName + " \n " ;
ToolTip + = tr ( " File root: %1 \n " ) . arg ( pBoxEx - > GetFileRoot ( ) ) ;
ToolTip + = tr ( " Registry root: %1 \n " ) . arg ( pBoxEx - > GetRegRoot ( ) ) ;
ToolTip + = tr ( " IPC root: %1 \n " ) . arg ( pBoxEx - > GetIpcRoot ( ) ) ;
ToolTip + = tr ( " Options: \n " ) ;
ToolTip + = pBoxEx - > GetStatusStr ( ) . replace ( " , " , " \n " ) ;
}
else if ( quint64 ProcessId = ID . toULongLong ( ) )
{
// todo proc info
}
}
2020-06-01 17:11:56 +01:00
void CSbieView : : OnMenu ( const QPoint & Point )
{
2020-07-04 11:07:36 +01:00
CSandBoxPtr pBox ;
CBoxedProcessPtr pProcess ;
2020-06-01 17:11:56 +01:00
int iProcessCount = 0 ;
int iSandBoxeCount = 0 ;
int iSuspendedCount = 0 ;
foreach ( const QModelIndex & Index , m_pSbieTree - > selectedRows ( ) )
{
QModelIndex ModelIndex = m_pSortProxy - > mapToSource ( Index ) ;
2020-07-04 11:07:36 +01:00
pProcess = m_pSbieModel - > GetProcess ( ModelIndex ) ;
2020-06-01 17:11:56 +01:00
if ( pProcess )
{
iProcessCount + + ;
if ( pProcess - > IsSuspended ( ) )
iSuspendedCount + + ;
}
else
{
2020-07-04 11:07:36 +01:00
pBox = m_pSbieModel - > GetSandBox ( ModelIndex ) ;
2020-06-01 17:11:56 +01:00
if ( pBox )
iSandBoxeCount + + ;
}
}
QList < QAction * > MenuActions = m_pMenu - > actions ( ) ;
for ( int i = 0 ; i < m_iMenuBox ; i + + )
MenuActions [ i ] - > setVisible ( iSandBoxeCount > 0 & & iProcessCount = = 0 ) ;
m_pMenuRun - > setEnabled ( iSandBoxeCount = = 1 ) ;
m_pMenuRename - > setEnabled ( iSandBoxeCount = = 1 ) ;
2020-07-04 11:07:36 +01:00
m_pMenuPresets - > setEnabled ( iSandBoxeCount = = 1 ) ;
m_pMenuPresetsLogApi - > setChecked ( pBox & & pBox . objectCast < CSandBoxPlus > ( ) - > HasLogApi ( ) ) ;
m_pMenuPresetsINet - > setChecked ( pBox & & pBox . objectCast < CSandBoxPlus > ( ) - > IsINetBlocked ( ) ) ;
m_pMenuPresetsShares - > setChecked ( pBox & & pBox . objectCast < CSandBoxPlus > ( ) - > HasSharesAccess ( ) ) ;
m_pMenuPresetsNoAdmin - > setChecked ( pBox & & pBox . objectCast < CSandBoxPlus > ( ) - > IsDropRights ( ) ) ;
2020-07-19 21:09:02 +01:00
m_pMenuOptions - > setEnabled ( iSandBoxeCount = = 1 ) ;
2020-09-05 16:45:39 +01:00
m_pMenuSnapshots - > setEnabled ( iSandBoxeCount = = 1 ) ;
2020-07-04 11:07:36 +01:00
2020-06-01 17:11:56 +01:00
for ( int i = m_iMenuBox ; i < m_iMenuProc ; i + + )
MenuActions [ i ] - > setVisible ( iProcessCount > 0 & & iSandBoxeCount = = 0 ) ;
m_pMenuSuspend - > setEnabled ( iProcessCount > iSuspendedCount ) ;
m_pMenuResume - > setEnabled ( iSuspendedCount > 0 ) ;
CPanelView : : OnMenu ( Point ) ;
}
void CSbieView : : OnSandBoxAction ( )
{
QList < SB_STATUS > Results ;
QAction * Action = qobject_cast < QAction * > ( sender ( ) ) ;
QList < CSandBoxPtr > SandBoxes = CSbieView : : GetSelectedBoxes ( ) ;
if ( SandBoxes . isEmpty ( ) )
return ;
if ( Action = = m_pMenuRunAny )
{
/*QString Command = ShowRunDialog(SandBoxes.first()->GetName());
if ( ! Command . isEmpty ( ) )
SandBoxes . first ( ) - > RunCommand ( Command ) ; */
Results . append ( SandBoxes . first ( ) - > RunStart ( " run_dialog " ) ) ;
}
else if ( Action = = m_pMenuRunBrowser )
Results . append ( SandBoxes . first ( ) - > RunStart ( " default_browser " ) ) ;
else if ( Action = = m_pMenuRunExplorer )
Results . append ( SandBoxes . first ( ) - > RunCommand ( " explorer.exe /e,::{20D04FE0-3AEA-1069-A2D8-08002B30309D} " ) ) ;
else if ( Action = = m_pMenuRunCmd )
Results . append ( SandBoxes . first ( ) - > RunCommand ( " cmd.exe " ) ) ;
2020-07-04 11:07:36 +01:00
else if ( Action = = m_pMenuPresetsLogApi )
SandBoxes . first ( ) . objectCast < CSandBoxPlus > ( ) - > SetLogApi ( m_pMenuPresetsLogApi - > isChecked ( ) ) ;
else if ( Action = = m_pMenuPresetsINet )
SandBoxes . first ( ) . objectCast < CSandBoxPlus > ( ) - > SetINetBlock ( m_pMenuPresetsINet - > isChecked ( ) ) ;
else if ( Action = = m_pMenuPresetsShares )
SandBoxes . first ( ) . objectCast < CSandBoxPlus > ( ) - > SetAllowShares ( m_pMenuPresetsShares - > isChecked ( ) ) ;
else if ( Action = = m_pMenuPresetsNoAdmin )
SandBoxes . first ( ) . objectCast < CSandBoxPlus > ( ) - > SetDropRights ( m_pMenuPresetsNoAdmin - > isChecked ( ) ) ;
2020-07-19 21:09:02 +01:00
else if ( Action = = m_pMenuOptions )
{
COptionsWindow * pOptionsWindow = new COptionsWindow ( SandBoxes . first ( ) , SandBoxes . first ( ) - > GetName ( ) , this ) ;
pOptionsWindow - > show ( ) ;
}
2020-10-10 17:18:01 +01:00
else if ( Action = = m_pMenuExplore )
{
: : ShellExecute ( NULL , NULL , SandBoxes . first ( ) - > GetFileRoot ( ) . toStdWString ( ) . c_str ( ) , NULL , NULL , SW_SHOWNORMAL ) ;
// if (ret <= 32) error
}
2020-09-05 16:45:39 +01:00
else if ( Action = = m_pMenuSnapshots )
{
CSnapshotsWindow * pSnapshotsWindow = new CSnapshotsWindow ( SandBoxes . first ( ) , this ) ;
pSnapshotsWindow - > show ( ) ;
}
2020-06-01 17:11:56 +01:00
else if ( Action = = m_pMenuRename )
{
2020-07-04 11:07:36 +01:00
QString OldValue = SandBoxes . first ( ) - > GetName ( ) . replace ( " _ " , " " ) ;
2020-07-19 21:09:02 +01:00
QString Value = QInputDialog : : getText ( this , " Sandboxie-Plus " , tr ( " Please enter a new name for the Sandbox. " ) , QLineEdit : : Normal , OldValue ) ;
2020-07-04 11:07:36 +01:00
if ( Value . isEmpty ( ) | | Value = = OldValue )
2020-06-01 17:11:56 +01:00
return ;
Results . append ( ( SandBoxes . first ( ) - > RenameBox ( Value ) ) ) ;
}
else if ( Action = = m_pMenuRemove )
{
2020-06-19 09:19:27 +01:00
if ( QMessageBox ( " Sandboxie-Plus " , tr ( " Do you really want remove the selected sandbox(es)? " ) , QMessageBox : : Warning , QMessageBox : : Yes , QMessageBox : : No | QMessageBox : : Default | QMessageBox : : Escape , QMessageBox : : NoButton ) . exec ( ) ! = QMessageBox : : Yes )
2020-06-01 17:11:56 +01:00
return ;
foreach ( const CSandBoxPtr & pBox , SandBoxes )
Results . append ( pBox - > RemoveBox ( ) ) ;
}
else if ( Action = = m_pMenuCleanUp )
{
2020-06-19 09:19:27 +01:00
if ( QMessageBox ( " Sandboxie-Plus " , tr ( " Do you really want delete the content of the selected sandbox(es)? " ) , QMessageBox : : Warning , QMessageBox : : Yes , QMessageBox : : No | QMessageBox : : Default | QMessageBox : : Escape , QMessageBox : : NoButton ) . exec ( ) ! = QMessageBox : : Yes )
2020-06-01 17:11:56 +01:00
return ;
foreach ( const CSandBoxPtr & pBox , SandBoxes )
2020-06-18 16:44:29 +01:00
{
2020-09-05 16:45:39 +01:00
SB_PROGRESS Status = pBox - > CleanBox ( ) ;
if ( Status . GetStatus ( ) = = OP_ASYNC )
theGUI - > AddAsyncOp ( Status . GetValue ( ) ) ;
else if ( Status . IsError ( ) )
Results . append ( Status ) ;
2020-06-18 16:44:29 +01:00
}
2020-06-01 17:11:56 +01:00
}
else if ( Action = = m_pMenuEmptyBox )
{
foreach ( const CSandBoxPtr & pBox , SandBoxes )
Results . append ( pBox - > TerminateAll ( ) ) ;
}
CSandMan : : CheckResults ( Results ) ;
}
2020-06-18 16:44:29 +01:00
2020-06-01 17:11:56 +01:00
void CSbieView : : OnProcessAction ( )
{
2020-06-08 16:17:37 +01:00
QList < SB_STATUS > Results ;
2020-06-01 17:11:56 +01:00
QAction * Action = qobject_cast < QAction * > ( sender ( ) ) ;
2020-07-04 11:07:36 +01:00
if ( Action = = m_pMenuTerminate )
{
if ( QMessageBox ( " Sandboxie-Plus " , tr ( " Do you want to %1 the selected process(es) " ) . arg ( ( ( QAction * ) sender ( ) ) - > text ( ) . toLower ( ) )
, QMessageBox : : Question , QMessageBox : : Yes | QMessageBox : : Default , QMessageBox : : No | QMessageBox : : Escape , QMessageBox : : NoButton ) . exec ( ) ! = QMessageBox : : Yes )
return ;
}
2020-06-01 17:11:56 +01:00
foreach ( const CBoxedProcessPtr & pProcess , CSbieView : : GetSelectedProcesses ( ) )
{
if ( Action = = m_pMenuTerminate )
2020-06-08 16:17:37 +01:00
Results . append ( pProcess - > Terminate ( ) ) ;
2020-06-01 17:11:56 +01:00
else if ( Action = = m_pMenuSuspend )
2020-06-08 16:17:37 +01:00
Results . append ( pProcess - > SetSuspend ( true ) ) ;
2020-06-01 17:11:56 +01:00
else if ( Action = = m_pMenuResume )
2020-06-08 16:17:37 +01:00
Results . append ( pProcess - > SetSuspend ( false ) ) ;
2020-06-01 17:11:56 +01:00
}
2020-06-08 16:17:37 +01:00
CSandMan : : CheckResults ( Results ) ;
2020-06-01 17:11:56 +01:00
}
2020-07-19 21:09:02 +01:00
void CSbieView : : OnDoubleClicked ( const QModelIndex & index )
{
QModelIndex ModelIndex = m_pSortProxy - > mapToSource ( index ) ;
CSandBoxPtr pBox = m_pSbieModel - > GetSandBox ( ModelIndex ) ;
if ( pBox . isNull ( ) )
return ;
COptionsWindow * pOptionsWindow = new COptionsWindow ( pBox , pBox - > GetName ( ) , this ) ;
pOptionsWindow - > show ( ) ;
}
2020-06-01 17:11:56 +01:00
void CSbieView : : ProcessSelection ( const QItemSelection & selected , const QItemSelection & deselected )
{
if ( selected . empty ( ) )
return ;
QItemSelectionModel * selectionModel = m_pSbieTree - > selectionModel ( ) ;
QItemSelection selection = selectionModel - > selection ( ) ;
QModelIndex root_parent = m_pSbieTree - > currentIndex ( ) . parent ( ) ;
while ( root_parent . isValid ( ) & & root_parent . parent ( ) . isValid ( ) )
root_parent = root_parent . parent ( ) ;
QItemSelection invalid ;
foreach ( const QModelIndex & index , selection . indexes ( ) )
{
QModelIndex parent = index . parent ( ) ;
while ( parent . isValid ( ) & & parent . parent ( ) . isValid ( ) )
parent = parent . parent ( ) ;
if ( parent ! = root_parent )
invalid . select ( index , index ) ;
}
selectionModel - > select ( invalid , QItemSelectionModel : : Deselect ) ;
}
QList < CSandBoxPtr > CSbieView : : GetSelectedBoxes ( )
{
QList < CSandBoxPtr > List ;
foreach ( const QModelIndex & Index , m_pSbieTree - > selectedRows ( ) )
{
QModelIndex ModelIndex = m_pSortProxy - > mapToSource ( Index ) ;
CSandBoxPtr pBox = m_pSbieModel - > GetSandBox ( ModelIndex ) ;
if ( ! pBox )
continue ;
List . append ( pBox ) ;
}
return List ;
}
QList < CBoxedProcessPtr > CSbieView : : GetSelectedProcesses ( )
{
QList < CBoxedProcessPtr > List ;
foreach ( const QModelIndex & Index , m_pSbieTree - > selectedRows ( ) )
{
QModelIndex ModelIndex = m_pSortProxy - > mapToSource ( Index ) ;
CBoxedProcessPtr pProcess = m_pSbieModel - > GetProcess ( ModelIndex ) ;
if ( ! pProcess )
return QList < CBoxedProcessPtr > ( ) ;
List . append ( pProcess ) ;
}
return List ;
2020-07-04 11:07:36 +01:00
}