Skip to content

Commit

Permalink
Improve clearing recent file list
Browse files Browse the repository at this point in the history
  • Loading branch information
clsid2 committed Feb 22, 2024
1 parent b18387f commit 6ee7f55
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 44 deletions.
69 changes: 60 additions & 9 deletions src/mpc-hc/AppSettings.cpp
Expand Up @@ -860,6 +860,22 @@ CString CAppSettings::SelectedAudioRenderer() const
return strResult;
}

void CAppSettings::ClearRecentFiles() {
MRU.RemoveAll();

for (int i = MRUDub.GetSize() - 1; i >= 0; i--) {
MRUDub.Remove(i);
}
MRUDub.WriteList();

// Empty the Windows "Recent" jump list
CComPtr<IApplicationDestinations> pDests;
HRESULT hr = pDests.CoCreateInstance(CLSID_ApplicationDestinations, nullptr, CLSCTX_INPROC_SERVER);
if (SUCCEEDED(hr)) {
pDests->RemoveAllDestinations();
}
}

void CAppSettings::SaveSettings(bool write_full_history /* = false */)
{
CMPlayerCApp* pApp = AfxGetMyApp();
Expand Down Expand Up @@ -1276,19 +1292,39 @@ void CAppSettings::SaveSettings(bool write_full_history /* = false */)
MRU.SaveMediaHistory();
}

size_t maxsize = AfxGetAppSettings().fKeepHistory ? iRecentFilesNumber : 0;
CStringW section = L"PlaylistHistory";
auto timeToHash = LoadHistoryHashes(section, L"LastUpdated");
pApp->FlushProfile();
}

int entries = 0;
for (auto iter = timeToHash.rbegin(); iter != timeToHash.rend(); ++iter, ++entries) {
CStringW hash = iter->second;
if (entries >= maxsize) {
PurgeExpiredHash(section, hash);
void CAppSettings::PurgeMediaHistory(size_t maxsize) {
CStringW section = L"MediaHistory";
auto timeToHash = LoadHistoryHashes(section, L"LastUpdated");
size_t entries = timeToHash.size();
if (entries > maxsize) {
for (auto iter = timeToHash.rbegin(); iter != timeToHash.rend(); ++iter) {
if (entries > maxsize) {
PurgeExpiredHash(section, iter->second);
entries--;
} else {
break;
}
}
}
}

pApp->FlushProfile();
void CAppSettings::PurgePlaylistHistory(size_t maxsize) {
CStringW section = L"PlaylistHistory";
auto timeToHash = LoadHistoryHashes(section, L"LastUpdated");
size_t entries = timeToHash.size();
if (entries > maxsize) {
for (auto iter = timeToHash.rbegin(); iter != timeToHash.rend(); ++iter) {
if (entries > maxsize) {
PurgeExpiredHash(section, iter->second);
entries--;
} else {
break;
}
}
}
}

std::multimap<CStringW, CStringW> CAppSettings::LoadHistoryHashes(CStringW section, CStringW dateField) {
Expand Down Expand Up @@ -2807,6 +2843,7 @@ CStringW getRFEHash(RecentFileEntry &r) {
}
}

/*
void CAppSettings::CRecentFileListWithMoreInfo::Remove(size_t nIndex) {
if (nIndex >= 0 && nIndex < rfe_array.GetCount()) {
auto pApp = AfxGetMyApp();
Expand All @@ -2822,6 +2859,7 @@ void CAppSettings::CRecentFileListWithMoreInfo::Remove(size_t nIndex) {
current_rfe_hash.Empty();
}
}
*/

void CAppSettings::CRecentFileListWithMoreInfo::Add(LPCTSTR fn) {
RecentFileEntry r;
Expand Down Expand Up @@ -3232,6 +3270,9 @@ void CAppSettings::CRecentFileListWithMoreInfo::ReadMediaHistory() {
} else {
rfe_last_added = lastAddedStored;
}

// The playlist history size is not managed elsewhere
CAppSettings::PurgePlaylistHistory(maxsize);
}

void CAppSettings::CRecentFileListWithMoreInfo::WriteMediaHistoryAudioIndex(RecentFileEntry& r) {
Expand Down Expand Up @@ -3386,8 +3427,18 @@ void CAppSettings::CRecentFileListWithMoreInfo::SetSize(size_t nSize) {
m_maxSize = nSize;
if (rfe_array.GetCount() > m_maxSize) {
rfe_array.SetCount(m_maxSize);
PurgeMediaHistory(m_maxSize);
PurgePlaylistHistory(m_maxSize);
}
rfe_array.FreeExtra();

if (nSize == 0) {
current_rfe_hash.Empty();
}
}

void CAppSettings::CRecentFileListWithMoreInfo::RemoveAll() {
SetSize(0);
}

bool CAppSettings::IsVSFilterInstalled()
Expand Down
8 changes: 6 additions & 2 deletions src/mpc-hc/AppSettings.h
Expand Up @@ -521,7 +521,7 @@ class CAppSettings
return rfe_array[nIndex];
}

void Remove(size_t nIndex);
//void Remove(size_t nIndex);
void Add(LPCTSTR fn);
void Add(LPCTSTR fn, ULONGLONG llDVDGuid);
void Add(RecentFileEntry r, bool current_open = false);
Expand Down Expand Up @@ -552,6 +552,7 @@ class CAppSettings
bool LoadMediaHistoryEntry(CStringW hash, RecentFileEntry& r);
void MigrateLegacyHistory();
void SetSize(size_t nSize);
void RemoveAll();
};

public:
Expand Down Expand Up @@ -1014,8 +1015,11 @@ class CAppSettings
CAppSettings& operator = (const CAppSettings&) = delete;

void SaveSettings(bool write_full_history = false);
void ClearRecentFiles();
static void PurgeMediaHistory(size_t maxsize = 0);
static void PurgePlaylistHistory(size_t maxsize = 0);
static std::multimap<CStringW, CStringW> LoadHistoryHashes(CStringW section, CStringW dateField);
static void PurgeExpiredHash(CStringW section, CStringW hash);
static void PurgeExpiredHash(CStringW section, CStringW hash);
void LoadSettings();
void SaveExternalFilters() {
if (bInitialized) {
Expand Down
19 changes: 1 addition & 18 deletions src/mpc-hc/MainFrm.cpp
Expand Up @@ -10587,24 +10587,7 @@ void CMainFrame::OnRecentFileClear()
}

CAppSettings& s = AfxGetAppSettings();

// Empty MPC-HC's recent menu (iterating reverse because the indexes change)
for (int i = s.MRU.GetSize() - 1; i >= 0; i--) {
s.MRU.Remove(i);
}
for (int i = s.MRUDub.GetSize() - 1; i >= 0; i--) {
s.MRUDub.Remove(i);
}
s.MRUDub.WriteList();

// Empty the "Recent" jump list
CComPtr<IApplicationDestinations> pDests;
HRESULT hr = pDests.CoCreateInstance(CLSID_ApplicationDestinations, nullptr, CLSCTX_INPROC_SERVER);
if (SUCCEEDED(hr)) {
pDests->RemoveAllDestinations();
}

// Remove the saved positions in media
s.ClearRecentFiles();
}

void CMainFrame::OnUpdateRecentFileClear(CCmdUI* pCmdUI)
Expand Down
16 changes: 1 addition & 15 deletions src/mpc-hc/PPagePlayer.cpp
Expand Up @@ -142,21 +142,7 @@ BOOL CPPagePlayer::OnApply()
s.bEnableCoverArt = !!m_bEnableCoverArt;

if (!m_fKeepHistory) {
// Empty MPC-HC's recent menu (iterating reverse because the indexes change)
for (int i = s.MRU.GetSize() - 1; i >= 0; i--) {
s.MRU.Remove(i);
}
for (int i = s.MRUDub.GetSize() - 1; i >= 0; i--) {
s.MRUDub.Remove(i);
}
s.MRUDub.WriteList();

// Empty the "Recent" jump list
CComPtr<IApplicationDestinations> pDests;
HRESULT hr = pDests.CoCreateInstance(CLSID_ApplicationDestinations, nullptr, CLSCTX_INPROC_SERVER);
if (SUCCEEDED(hr)) {
pDests->RemoveAllDestinations();
}
s.ClearRecentFiles();

// Ensure no new items are added in Windows recent menu and in the "Recent" jump list
s.fileAssoc.SetNoRecentDocs(true, true);
Expand Down

0 comments on commit 6ee7f55

Please sign in to comment.