Skip to content

Fix "file already exists" error when toggling events #4

@SinisterSpatula

Description

@SinisterSpatula

Generated with github copilot.

Problem

Users sometimes get an error that a file already exists when clicking to enable or disable events. This happens because the UI state gets out of sync with the actual file state on disk.

Root Cause

The code uses QFile::rename() to toggle events by renaming files between active and inactive states (e.g., LiveTuningData_Event.jsonOFF_LiveTuningData_Event.json).

QFile::rename() fails if the destination file already exists. This can happen when:

  1. The UI state gets out of sync with the actual file state
  2. A previous operation partially failed
  3. Files were manually modified in the filesystem

Solution

Check if the destination file exists and remove it before attempting to rename. This needs to be fixed in three functions:

  1. onEventSwitchChanged() - handles standard events like Cosmic Chaos, Armor Incursion, etc.
  2. onCustomEventSwitchChanged() - handles custom events
  3. onPandemoniumProtocolToggle() - handles Pandemonium Protocol event

Implementation Details

For each function, add checks before the rename operation:

When enabling an event (renaming OFF_file to active file):

// Check if destination already exists and remove it
if (activeFile.exists()) {
    if (!activeFile.remove()) {
        QMessageBox::critical(this, "Error", QString("Failed to remove existing active file: %1").arg(activeFile.errorString()));
        return;
    }
}

When disabling an event (renaming active file to OFF_file):

// Check if destination already exists and remove it
if (inactiveFile.exists()) {
    if (!inactiveFile.remove()) {
        QMessageBox::critical(this, "Error", QString("Failed to remove existing inactive file: %1").arg(inactiveFile.errorString()));
        return;
    }
}

Files to Modify

  • MHServerEmuUI/mainwindow.cpp
    • Line ~1519-1567: onEventSwitchChanged()
    • Line ~1571-1624: onCustomEventSwitchChanged()
    • Line ~1884-1917: onPandemoniumProtocolToggle()

Expected Behavior After Fix

  • Event toggles will work reliably even when files are out of sync
  • Users will no longer see "file already exists" errors
  • The application will gracefully handle edge cases where both active and inactive versions exist

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions