Skip to content

Conversation

Copy link

Copilot AI commented Jul 17, 2025

This PR addresses significant code duplication in the Sandboxie codebase by consolidating similar functions and patterns into reusable helper functions.

Changes Made

1. Context Menu Functions Refactoring

Consolidated 4 similar context menu functions (AddContextMenu, AddContextMenu2, AddContextMenu3, AddContextMenu4) that contained nearly identical logic:

Before:

void CSbieUtils::AddContextMenu2(const QString& StartPath, const QString& RunStr, const QString& IconPath)
{
    std::wstring start_path = L"\"" + StartPath.toStdWString() + L"\"";
    std::wstring icon_path = L"\"" + (IconPath.isEmpty() ? StartPath : IconPath).toStdWString() + L"\",-104";
    CreateShellEntry(L"*", L"unbox", RunStr.toStdWString(), icon_path, start_path + L" /disable_force \"%1\" %*");
}

After:

void CSbieUtils::AddContextMenu2(const QString& StartPath, const QString& RunStr, const QString& IconPath)
{
    AddContextMenuHelper(StartPath, RunStr, IconPath, "unbox", "/disable_force", true, false, ",-104");
}

New Helper Function:

  • AddContextMenuHelper() - Handles common logic for creating context menu entries
  • HasContextMenuHelper() - Checks for context menu existence
  • RemoveContextMenuHelper() - Removes context menu entries

2. Registry Access Pattern Consolidation

Created a helper function to reduce duplicated registry read operations:

Before:

HKEY hkey;
LONG rc = RegOpenKeyExW(HKEY_CURRENT_USER, key, 0, KEY_READ, &hkey);
if (rc != 0) return QString();
// ... registry read logic ...
RegCloseKey(hkey);

After:

return ReadRegistryValue(HKEY_CURRENT_USER, "Software\\Classes\\*\\shell\\sandbox\\command");

3. Application Directory Path Standardization

Eliminated repeated QApplication::applicationDirPath().replace("/", "\\") calls:

Before:

CSbieUtils::AddContextMenu2(QApplication::applicationDirPath().replace("/", "\\") + "\\SandMan.exe",
    tr("Run &Un-Sandboxed"),
    QApplication::applicationDirPath().replace("/", "\\") + "\\Start.exe");

After:

CSbieUtils::AddContextMenu2(CSbieUtils::GetApplicationDirPath() + "\\SandMan.exe",
    tr("Run &Un-Sandboxed"),
    CSbieUtils::GetApplicationDirPath() + "\\Start.exe");

Impact

  • Code Reduction: ~100+ lines of duplicated code eliminated
  • Functions Consolidated: 12 functions refactored to use 3 helper functions
  • Files Improved: 5 files with cleaner, more maintainable code
  • Maintainability: Reduced risk of inconsistencies and bugs from copy-paste errors

Files Modified

  • SandboxiePlus/QSbieAPI/SbieUtils.cpp - Main refactoring of utility functions
  • SandboxiePlus/QSbieAPI/SbieUtils.h - Added helper function declarations
  • SandboxiePlus/SandMan/Windows/SettingsWindow.cpp - Updated to use helper functions
  • SandboxiePlus/SandMan/Engine/SbieObject.cpp - Updated to use helper functions
  • SandboxiePlus/SandMan/SandMan.cpp - Updated to use helper functions

Testing

The refactored code maintains the same functionality while improving maintainability. All existing context menu operations, registry access patterns, and path handling continue to work as expected.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 3 commits July 17, 2025 17:32
Co-authored-by: offhub <6871698+offhub@users.noreply.github.com>
Co-authored-by: offhub <6871698+offhub@users.noreply.github.com>
Co-authored-by: offhub <6871698+offhub@users.noreply.github.com>
Copilot AI changed the title [WIP] Go to test-6 brach and Find and refactor duplicated code Refactor duplicated code patterns in context menu and utility functions Jul 17, 2025
Copilot AI requested a review from offhub July 17, 2025 17:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants