From 392627f1fe66a8063d25d05c844c7891e93ab2c7 Mon Sep 17 00:00:00 2001 From: Leon Styhre Date: Sun, 31 Jan 2021 19:56:55 +0100 Subject: [PATCH] Empty media directories are now removed when clearing or deleting games. --- .../src/views/gamelist/BasicGameListView.cpp | 76 +++++++++++++++---- .../src/views/gamelist/GridGameListView.cpp | 76 +++++++++++++++---- 2 files changed, 124 insertions(+), 28 deletions(-) diff --git a/es-app/src/views/gamelist/BasicGameListView.cpp b/es-app/src/views/gamelist/BasicGameListView.cpp index dc35e1dad..471423417 100644 --- a/es-app/src/views/gamelist/BasicGameListView.cpp +++ b/es-app/src/views/gamelist/BasicGameListView.cpp @@ -230,27 +230,75 @@ void BasicGameListView::remove(FileData* game, bool deleteFile) void BasicGameListView::removeMedia(FileData* game) { + std::string systemMediaDir = FileData::getMediaDirectory() + game->getSystem()->getName(); + std::string mediaType; + std::string path; + + // If there are no media files left in the directory after the deletion, then remove + // the directory too. Remove any empty parent directories as well. + auto removeEmptyDirFunc = [] + (std::string systemMediaDir, std::string mediaType, std::string path) { + std::string parentPath = Utils::FileSystem::getParent(path); + while (parentPath != systemMediaDir + "/" + mediaType) { + if (Utils::FileSystem::getDirContent(parentPath).size() == 0) { + Utils::FileSystem::removeDirectory(parentPath); + parentPath = Utils::FileSystem::getParent(parentPath); + } + else { + break; + } + } + }; + // Remove all game media files on the filesystem. - if (Utils::FileSystem::exists(game->getVideoPath())) - Utils::FileSystem::removeFile(game->getVideoPath()); + if (Utils::FileSystem::exists(game->getVideoPath())) { + mediaType = "videos"; + path = game->getVideoPath(); + Utils::FileSystem::removeFile(path); + removeEmptyDirFunc(systemMediaDir, mediaType, path); + } - if (Utils::FileSystem::exists(game->getMiximagePath())) - Utils::FileSystem::removeFile(game->getMiximagePath()); + if (Utils::FileSystem::exists(game->getMiximagePath())) { + mediaType = "miximages"; + path = game->getMiximagePath(); + Utils::FileSystem::removeFile(path); + removeEmptyDirFunc(systemMediaDir, mediaType, path); + } - if (Utils::FileSystem::exists(game->getScreenshotPath())) - Utils::FileSystem::removeFile(game->getScreenshotPath()); + if (Utils::FileSystem::exists(game->getScreenshotPath())) { + mediaType = "screenshots"; + path = game->getScreenshotPath(); + Utils::FileSystem::removeFile(path); + removeEmptyDirFunc(systemMediaDir, mediaType, path); + } - if (Utils::FileSystem::exists(game->getCoverPath())) - Utils::FileSystem::removeFile(game->getCoverPath()); + if (Utils::FileSystem::exists(game->getCoverPath())) { + mediaType = "covers"; + path = game->getCoverPath(); + Utils::FileSystem::removeFile(path); + removeEmptyDirFunc(systemMediaDir, mediaType, path); + } - if (Utils::FileSystem::exists(game->getMarqueePath())) - Utils::FileSystem::removeFile(game->getMarqueePath()); + if (Utils::FileSystem::exists(game->getMarqueePath())) { + mediaType = "marquees"; + path = game->getMarqueePath(); + Utils::FileSystem::removeFile(path); + removeEmptyDirFunc(systemMediaDir, mediaType, path); + } - if (Utils::FileSystem::exists(game->get3DBoxPath())) - Utils::FileSystem::removeFile(game->get3DBoxPath()); + if (Utils::FileSystem::exists(game->get3DBoxPath())) { + mediaType = "3dboxes"; + path = game->get3DBoxPath(); + Utils::FileSystem::removeFile(path); + removeEmptyDirFunc(systemMediaDir, mediaType, path); + } - if (Utils::FileSystem::exists(game->getThumbnailPath())) - Utils::FileSystem::removeFile(game->getThumbnailPath()); + if (Utils::FileSystem::exists(game->getThumbnailPath())) { + mediaType = "thumbnails"; + path = game->getThumbnailPath(); + Utils::FileSystem::removeFile(path); + removeEmptyDirFunc(systemMediaDir, mediaType, path); + } } std::vector BasicGameListView::getHelpPrompts() diff --git a/es-app/src/views/gamelist/GridGameListView.cpp b/es-app/src/views/gamelist/GridGameListView.cpp index 31bfa0b9c..591f206a8 100644 --- a/es-app/src/views/gamelist/GridGameListView.cpp +++ b/es-app/src/views/gamelist/GridGameListView.cpp @@ -574,27 +574,75 @@ void GridGameListView::remove(FileData* game, bool deleteFile) void GridGameListView::removeMedia(FileData* game) { + std::string systemMediaDir = FileData::getMediaDirectory() + game->getSystem()->getName(); + std::string mediaType; + std::string path; + + // If there are no media files left in the directory after the deletion, then remove + // the directory too. Remove any empty parent directories as well. + auto removeEmptyDirFunc = [] + (std::string systemMediaDir, std::string mediaType, std::string path) { + std::string parentPath = Utils::FileSystem::getParent(path); + while (parentPath != systemMediaDir + "/" + mediaType) { + if (Utils::FileSystem::getDirContent(parentPath).size() == 0) { + Utils::FileSystem::removeDirectory(parentPath); + parentPath = Utils::FileSystem::getParent(parentPath); + } + else { + break; + } + } + }; + // Remove all game media files on the filesystem. - if (Utils::FileSystem::exists(game->getVideoPath())) - Utils::FileSystem::removeFile(game->getVideoPath()); + if (Utils::FileSystem::exists(game->getVideoPath())) { + mediaType = "videos"; + path = game->getVideoPath(); + Utils::FileSystem::removeFile(path); + removeEmptyDirFunc(systemMediaDir, mediaType, path); + } - if (Utils::FileSystem::exists(game->getMiximagePath())) - Utils::FileSystem::removeFile(game->getMiximagePath()); + if (Utils::FileSystem::exists(game->getMiximagePath())) { + mediaType = "miximages"; + path = game->getMiximagePath(); + Utils::FileSystem::removeFile(path); + removeEmptyDirFunc(systemMediaDir, mediaType, path); + } - if (Utils::FileSystem::exists(game->getScreenshotPath())) - Utils::FileSystem::removeFile(game->getScreenshotPath()); + if (Utils::FileSystem::exists(game->getScreenshotPath())) { + mediaType = "screenshots"; + path = game->getScreenshotPath(); + Utils::FileSystem::removeFile(path); + removeEmptyDirFunc(systemMediaDir, mediaType, path); + } - if (Utils::FileSystem::exists(game->getCoverPath())) - Utils::FileSystem::removeFile(game->getCoverPath()); + if (Utils::FileSystem::exists(game->getCoverPath())) { + mediaType = "covers"; + path = game->getCoverPath(); + Utils::FileSystem::removeFile(path); + removeEmptyDirFunc(systemMediaDir, mediaType, path); + } - if (Utils::FileSystem::exists(game->getMarqueePath())) - Utils::FileSystem::removeFile(game->getMarqueePath()); + if (Utils::FileSystem::exists(game->getMarqueePath())) { + mediaType = "marquees"; + path = game->getMarqueePath(); + Utils::FileSystem::removeFile(path); + removeEmptyDirFunc(systemMediaDir, mediaType, path); + } - if (Utils::FileSystem::exists(game->get3DBoxPath())) - Utils::FileSystem::removeFile(game->get3DBoxPath()); + if (Utils::FileSystem::exists(game->get3DBoxPath())) { + mediaType = "3dboxes"; + path = game->get3DBoxPath(); + Utils::FileSystem::removeFile(path); + removeEmptyDirFunc(systemMediaDir, mediaType, path); + } - if (Utils::FileSystem::exists(game->getThumbnailPath())) - Utils::FileSystem::removeFile(game->getThumbnailPath()); + if (Utils::FileSystem::exists(game->getThumbnailPath())) { + mediaType = "thumbnails"; + path = game->getThumbnailPath(); + Utils::FileSystem::removeFile(path); + removeEmptyDirFunc(systemMediaDir, mediaType, path); + } } std::vector GridGameListView::getMDLabels()