| 1667 | } |
| 1668 | |
| 1669 | void ProjectFileManager::Compact() |
| 1670 | { |
| 1671 | auto &project = mProject; |
| 1672 | auto &undoManager = UndoManager::Get(project); |
| 1673 | auto &clipboard = Clipboard::Get(); |
| 1674 | auto &projectFileIO = ProjectFileIO::Get(project); |
| 1675 | bool isBatch = project.mBatchMode > 0; |
| 1676 | |
| 1677 | // Purpose of this is to remove the -wal file. |
| 1678 | projectFileIO.ReopenProject(); |
| 1679 | |
| 1680 | auto savedState = undoManager.GetSavedState(); |
| 1681 | const auto currentState = undoManager.GetCurrentState(); |
| 1682 | if (savedState < 0) { |
| 1683 | undoManager.StateSaved(); |
| 1684 | savedState = undoManager.GetSavedState(); |
| 1685 | if (savedState < 0) { |
| 1686 | wxASSERT(false); |
| 1687 | savedState = 0; |
| 1688 | } |
| 1689 | } |
| 1690 | const auto least = std::min<size_t>(savedState, currentState); |
| 1691 | const auto greatest = std::max<size_t>(savedState, currentState); |
| 1692 | std::vector<const TrackList*> trackLists; |
| 1693 | auto fn = [&](const UndoStackElem& elem) { |
| 1694 | if (auto pTracks = UndoTracks::Find(elem)) |
| 1695 | trackLists.push_back(pTracks); |
| 1696 | }; |
| 1697 | undoManager.VisitStates(fn, least, 1 + least); |
| 1698 | if (least != greatest) |
| 1699 | undoManager.VisitStates(fn, greatest, 1 + greatest); |
| 1700 | |
| 1701 | int64_t total = projectFileIO.GetTotalUsage(); |
| 1702 | int64_t used = projectFileIO.GetCurrentUsage(trackLists); |
| 1703 | |
| 1704 | auto before = wxFileName::GetSize(projectFileIO.GetFileName()); |
| 1705 | |
| 1706 | CompactDialog dlg( |
| 1707 | XO("Compacting this project will free up disk space by removing unused bytes within the file.\n\n" |
| 1708 | "There is %s of free disk space and this project is currently using %s.\n" |
| 1709 | "\n" |
| 1710 | "If you proceed, the current Undo/Redo History and clipboard contents will be discarded " |
| 1711 | "and you will recover approximately %s of disk space.\n" |
| 1712 | "\n" |
| 1713 | "Do you want to continue?") |
| 1714 | .Format(Internat::FormatSize(projectFileIO.GetFreeDiskSpace()), |
| 1715 | Internat::FormatSize(before.GetValue()), |
| 1716 | Internat::FormatSize(total - used))); |
| 1717 | if (isBatch || dlg.ShowModal() == wxYES) |
| 1718 | { |
| 1719 | // We can remove redo states, if they are after the saved state. |
| 1720 | undoManager.RemoveStates(1 + greatest, undoManager.GetNumStates()); |
| 1721 | |
| 1722 | // We can remove all states between the current and the last saved. |
| 1723 | if (least < greatest) |
| 1724 | undoManager.RemoveStates(least + 1, greatest); |
| 1725 | |
| 1726 | // We can remove all states before the current and the last saved. |
no test coverage detected