Qt: ask for cache and config removal on app remove (#4016)

This commit is contained in:
Megamouse 2018-02-28 17:06:44 +01:00 committed by Ivan
parent e0f53ace19
commit 72e54e8b60
3 changed files with 85 additions and 66 deletions

View file

@ -539,16 +539,15 @@ void game_list_frame::ShowSpecifiedContextMenu(const QPoint &pos, int row)
GameInfo currGame = m_game_data[row].info; GameInfo currGame = m_game_data[row].info;
QMenu myMenu;
// Make Actions // Make Actions
QMenu myMenu;
QAction* boot = myMenu.addAction(tr("&Boot")); QAction* boot = myMenu.addAction(tr("&Boot"));
QFont f = boot->font(); QFont f = boot->font();
f.setBold(true); f.setBold(true);
boot->setFont(f); boot->setFont(f);
QAction* configure = myMenu.addAction(tr("&Configure")); QAction* configure = myMenu.addAction(tr("&Configure"));
myMenu.addSeparator(); myMenu.addSeparator();
QAction* removeGame = myMenu.addAction(tr("&Remove")); QAction* removeGame = myMenu.addAction(tr("&Remove %1").arg(qstr(currGame.category)));
QAction* removeConfig = myMenu.addAction(tr("&Remove Custom Configuration")); QAction* removeConfig = myMenu.addAction(tr("&Remove Custom Configuration"));
QAction* deleteShadersCache = myMenu.addAction(tr("&Delete Shaders Cache")); QAction* deleteShadersCache = myMenu.addAction(tr("&Delete Shaders Cache"));
QAction* deleteLLVMCache = myMenu.addAction(tr("&Delete LLVM Cache")); QAction* deleteLLVMCache = myMenu.addAction(tr("&Delete LLVM Cache"));
@ -559,6 +558,8 @@ void game_list_frame::ShowSpecifiedContextMenu(const QPoint &pos, int row)
QAction* checkCompat = myMenu.addAction(tr("&Check Game Compatibility")); QAction* checkCompat = myMenu.addAction(tr("&Check Game Compatibility"));
QAction* downloadCompat = myMenu.addAction(tr("&Download Compatibility Database")); QAction* downloadCompat = myMenu.addAction(tr("&Download Compatibility Database"));
const std::string config_base_dir = fs::get_config_dir() + "data/" + m_game_data[row].info.serial;
connect(boot, &QAction::triggered, [=] connect(boot, &QAction::triggered, [=]
{ {
LOG_NOTICE(LOADER, "Booting from gamelist per context menu..."); LOG_NOTICE(LOADER, "Booting from gamelist per context menu...");
@ -575,47 +576,36 @@ void game_list_frame::ShowSpecifiedContextMenu(const QPoint &pos, int row)
}); });
connect(removeGame, &QAction::triggered, [=] connect(removeGame, &QAction::triggered, [=]
{ {
if (QMessageBox::question(this, tr("Confirm Delete"), tr("Permanently delete files?")) == QMessageBox::Yes) QMessageBox* mb = new QMessageBox(QMessageBox::Question, tr("Confirm %1 Removal").arg(qstr(currGame.category)), tr("Permanently remove %1 from drive?").arg(qstr(currGame.name)), QMessageBox::Yes | QMessageBox::No, this);
mb->setCheckBox(new QCheckBox(tr("Remove caches and custom config")));
mb->deleteLater();
if (mb->exec() == QMessageBox::Yes)
{ {
if (mb->checkBox()->isChecked())
{
DeleteShadersCache(config_base_dir);
DeleteLLVMCache(config_base_dir);
RemoveCustomConfiguration(config_base_dir);
}
fs::remove_all(currGame.path); fs::remove_all(currGame.path);
m_game_data.erase(m_game_data.begin() + row); m_game_data.erase(m_game_data.begin() + row);
Refresh(); Refresh();
LOG_SUCCESS(GENERAL, "Removed %s %s in %s", currGame.category, currGame.name, currGame.path);
} }
}); });
connect(removeConfig, &QAction::triggered, [=]() connect(removeConfig, &QAction::triggered, [=]()
{ {
RemoveCustomConfiguration(row); if (RemoveCustomConfiguration(config_base_dir, true))
Refresh(true, false); Refresh(true, false);
}); });
connect(deleteShadersCache, &QAction::triggered, [=]() connect(deleteShadersCache, &QAction::triggered, [=]()
{ {
DeleteShadersCache(row); DeleteShadersCache(config_base_dir, true);
}); });
connect(deleteLLVMCache, &QAction::triggered, [=]() connect(deleteLLVMCache, &QAction::triggered, [=]()
{ {
if (QMessageBox::question(this, tr("Confirm Delete"), tr("Delete LLVM cache?")) == QMessageBox::Yes) DeleteLLVMCache(config_base_dir, true);
{
const std::string config_base_dir = fs::get_config_dir() + "data/" + m_game_data[row].info.serial;
for (auto&& subdir : fs::dir{config_base_dir})
{
if (!subdir.is_directory || subdir.name == "." || subdir.name == "..")
{
continue;
}
for (auto&& entry : fs::dir{config_base_dir + "/" + subdir.name})
{
if (entry.name.size() >= 4 && entry.name.compare(entry.name.size() - 4, 4, ".obj", 4) == 0)
{
fs::remove_file(config_base_dir + "/" + subdir.name + "/" + entry.name);
}
}
}
}
}); });
connect(openGameFolder, &QAction::triggered, [=]() connect(openGameFolder, &QAction::triggered, [=]()
{ {
open_dir(currGame.path); open_dir(currGame.path);
@ -657,59 +647,88 @@ void game_list_frame::ShowSpecifiedContextMenu(const QPoint &pos, int row)
// Disable removeconfig if no config exists. // Disable removeconfig if no config exists.
removeConfig->setEnabled(m_game_data[row].hasCustomConfig); removeConfig->setEnabled(m_game_data[row].hasCustomConfig);
// remove delete options if necessary
if (!fs::is_dir(config_base_dir))
{
deleteShadersCache->setEnabled(false);
deleteLLVMCache->setEnabled(false);
}
myMenu.exec(globalPos); myMenu.exec(globalPos);
} }
void game_list_frame::RemoveCustomConfiguration(int row) bool game_list_frame::RemoveCustomConfiguration(const std::string& base_dir, bool is_interactive)
{ {
const std::string config_path = fs::get_config_dir() + "data/" + m_game_data[row].info.serial + "/config.yml"; const std::string config_path = base_dir + "/config.yml";
if (fs::is_file(config_path)) if (!fs::is_file(config_path))
return false;
if (is_interactive && QMessageBox::question(this, tr("Confirm Delete"), tr("Delete custom game configuration?")) != QMessageBox::Yes)
return false;
if (fs::remove_file(config_path))
{ {
if (QMessageBox::question(this, tr("Confirm Delete"), tr("Delete custom game configuration?")) == QMessageBox::Yes) LOG_SUCCESS(GENERAL, "Removed configuration file: %s", config_path);
{ return true;
if (fs::remove_file(config_path))
{
LOG_SUCCESS(GENERAL, "Removed configuration file: %s", config_path);
}
else
{
QMessageBox::warning(this, tr("Warning!"), tr("Failed to delete configuration file!"));
LOG_FATAL(GENERAL, "Failed to delete configuration file: %s\nError: %s", config_path, fs::g_tls_error);
}
}
} }
else else
{ {
QMessageBox::warning(this, tr("Warning!"), tr("No custom configuration found!")); QMessageBox::warning(this, tr("Warning!"), tr("Failed to delete configuration file!"));
LOG_ERROR(GENERAL, "Configuration file not found: %s", config_path); LOG_FATAL(GENERAL, "Failed to delete configuration file: %s\nError: %s", config_path, fs::g_tls_error);
return false;
} }
} }
void game_list_frame::DeleteShadersCache(int row) bool game_list_frame::DeleteShadersCache(const std::string& base_dir, bool is_interactive)
{ {
if (QMessageBox::question(this, tr("Confirm Delete"), tr("Delete shaders cache?")) != QMessageBox::Yes) if (!fs::is_dir(base_dir))
return; return false;
const std::string config_base_dir = fs::get_config_dir() + "data/" + m_game_data[row].info.serial; if (is_interactive && QMessageBox::question(this, tr("Confirm Delete"), tr("Delete shaders cache?")) != QMessageBox::Yes)
return false;
if (fs::is_dir(config_base_dir)) fs::dir root = fs::dir(base_dir);
fs::dir_entry tmp;
while (root.read(tmp))
{ {
fs::dir root = fs::dir(config_base_dir); if (!fs::is_dir(base_dir + "/" + tmp.name))
fs::dir_entry tmp; continue;
while (root.read(tmp)) const std::string shader_cache_name = base_dir + "/" + tmp.name + "/shaders_cache";
if (fs::is_dir(shader_cache_name))
fs::remove_all(shader_cache_name, true);
}
LOG_SUCCESS(GENERAL, "Removed shaders cache in %s", base_dir);
return true;
}
bool game_list_frame::DeleteLLVMCache(const std::string& base_dir, bool is_interactive)
{
if (!fs::is_dir(base_dir))
return false;
if (is_interactive && QMessageBox::question(this, tr("Confirm Delete"), tr("Delete LLVM cache?")) != QMessageBox::Yes)
return false;
for (auto&& subdir : fs::dir{ base_dir })
{
if (!subdir.is_directory || subdir.name == "." || subdir.name == "..")
continue;
const std::string dir = base_dir + "/" + subdir.name;
for (auto&& entry : fs::dir{ dir })
{ {
if (!fs::is_dir(config_base_dir + "/" + tmp.name)) if (entry.name.size() >= 4 && entry.name.compare(entry.name.size() - 4, 4, ".obj", 4) == 0)
continue; fs::remove_file(dir + "/" + entry.name);
const std::string shader_cache_name = config_base_dir + "/" + tmp.name + "/shaders_cache";
if (fs::is_dir(shader_cache_name))
{
fs::remove_all(shader_cache_name, true);
}
} }
} }
LOG_SUCCESS(GENERAL, "Removed llvm cache in %s", base_dir);
return true;
} }
QPixmap game_list_frame::PaintedPixmap(const QImage& img, bool paintConfigIcon) QPixmap game_list_frame::PaintedPixmap(const QImage& img, bool paintConfigIcon)

View file

@ -201,8 +201,9 @@ public Q_SLOTS:
void SetSearchText(const QString& text); void SetSearchText(const QString& text);
private Q_SLOTS: private Q_SLOTS:
void RemoveCustomConfiguration(int row); bool RemoveCustomConfiguration(const std::string& base_dir, bool is_interactive = false);
void DeleteShadersCache(int row); bool DeleteShadersCache(const std::string& base_dir, bool is_interactive = false);
bool DeleteLLVMCache(const std::string& base_dir, bool is_interactive = false);
void OnColClicked(int col); void OnColClicked(int col);
void ShowContextMenu(const QPoint &pos); void ShowContextMenu(const QPoint &pos);
void ShowSpecifiedContextMenu(const QPoint &pos, int index); // Different name because the notation for overloaded connects is messy void ShowSpecifiedContextMenu(const QPoint &pos, int index); // Different name because the notation for overloaded connects is messy

View file

@ -258,9 +258,8 @@ void gui_settings::ShowInfoBox(const gui_save& entry, const QString& title, cons
{ {
if (GetValue(entry).toBool()) if (GetValue(entry).toBool())
{ {
QCheckBox* cb = new QCheckBox(tr("Don't show again"));
QMessageBox* mb = new QMessageBox(QMessageBox::Information, title, text, QMessageBox::Ok, parent); QMessageBox* mb = new QMessageBox(QMessageBox::Information, title, text, QMessageBox::Ok, parent);
mb->setCheckBox(cb); mb->setCheckBox(new QCheckBox(tr("Don't show again")));
mb->deleteLater(); mb->deleteLater();
mb->exec(); mb->exec();
if (mb->checkBox()->isChecked()) if (mb->checkBox()->isChecked())