Skip to content

Commit

Permalink
Qt: simplify shortcut creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Megamouse committed Dec 9, 2022
1 parent 681a6ef commit fa1b11d
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 120 deletions.
148 changes: 95 additions & 53 deletions rpcs3/rpcs3qt/game_list_frame.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "game_list_frame.h"
#include "qt_utils.h"
#include "shortcut_utils.h"
#include "settings_dialog.h"
#include "pad_settings_dialog.h"
#include "table_item_delegate.h"
Expand Down Expand Up @@ -895,6 +894,98 @@ void game_list_frame::ItemSelectionChangedSlot()
Q_EMIT NotifyGameSelection(game);
}

void game_list_frame::CreateShortcuts(const game_info& gameinfo, std::vector<gui::utils::shortcut_location> locations)
{
if (locations.empty())
{
game_list_log.error("Failed to create shortcuts for %s. No locations selected.", sstr(qstr(gameinfo->info.name).simplified()));
return;
}

std::string gameid_token_value;

if (gameinfo->info.category == "DG" && !fs::is_file(rpcs3::utils::get_hdd0_dir() + "/game/" + gameinfo->info.serial + "/USRDIR/EBOOT.BIN"))
{
const usz ps3_game_dir_pos = fs::get_parent_dir(gameinfo->info.path).size();
std::string relative_boot_dir = gameinfo->info.path.substr(ps3_game_dir_pos);

if (usz char_pos = relative_boot_dir.find_first_not_of(fs::delim); char_pos != umax)
{
relative_boot_dir = relative_boot_dir.substr(char_pos);
}
else
{
relative_boot_dir.clear();
}

if (!relative_boot_dir.empty())
{
if (relative_boot_dir != "PS3_GAME")
{
gameid_token_value = gameinfo->info.serial + "/" + relative_boot_dir;
}
else
{
gameid_token_value = gameinfo->info.serial;
}
}
}
else
{
gameid_token_value = gameinfo->info.serial;
}

const std::string target_cli_args = fmt::format("--no-gui \"%%RPCS3_GAMEID%%:%s\"", gameid_token_value);
const std::string target_icon_dir = fmt::format("%sIcons/game_icons/%s/", fs::get_config_dir(), gameinfo->info.serial);

if (!fs::create_path(target_icon_dir))
{
game_list_log.error("Failed to create shortcut path %s (%s)", sstr(qstr(gameinfo->info.name).simplified()), target_icon_dir, fs::g_tls_error);
return;
}

bool success = true;

for (const gui::utils::shortcut_location& location : locations)
{
std::string destination;

switch (location)
{
case gui::utils::shortcut_location::desktop:
destination = "desktop";
break;
case gui::utils::shortcut_location::applications:
destination = "application menu";
break;
#ifdef _WIN32
case gui::utils::shortcut_location::rpcs3_shortcuts:
destination = "/games/shortcuts/";
break;
#endif
}

if (!gameid_token_value.empty() && gui::utils::create_shortcut(gameinfo->info.name, target_cli_args, gameinfo->info.name, gameinfo->info.icon_path, target_icon_dir, location))
{
game_list_log.success("Created %s shortcut for %s", destination, sstr(qstr(gameinfo->info.name).simplified()));
}
else
{
game_list_log.error("Failed to create %s shortcut for %s", destination, sstr(qstr(gameinfo->info.name).simplified()));
success = false;
}
}

if (success)
{
QMessageBox::information(this, tr("Success!"), tr("Successfully created shortcuts."));
}
else
{
QMessageBox::warning(this, tr("Warning!"), tr("Failed to create shortcuts!"));
}
}

void game_list_frame::ShowContextMenu(const QPoint &pos)
{
QPoint global_pos;
Expand Down Expand Up @@ -1016,66 +1107,17 @@ void game_list_frame::ShowContextMenu(const QPoint &pos)

menu.addSeparator();

const auto on_shortcut = [this, gameinfo](bool is_desktop_shortcut)
{
std::string gameid_token_value;

if (gameinfo->info.category == "DG" && !fs::is_file(rpcs3::utils::get_hdd0_dir() + "/game/" + gameinfo->info.serial + "/USRDIR/EBOOT.BIN"))
{
const usz ps3_game_dir_pos = fs::get_parent_dir(gameinfo->info.path).size();
std::string relative_boot_dir = gameinfo->info.path.substr(ps3_game_dir_pos);

if (usz char_pos = relative_boot_dir.find_first_not_of(fs::delim); char_pos != umax)
{
relative_boot_dir = relative_boot_dir.substr(char_pos);
}
else
{
relative_boot_dir.clear();
}

if (!relative_boot_dir.empty())
{
if (relative_boot_dir != "PS3_GAME")
{
gameid_token_value = gameinfo->info.serial + "/" + relative_boot_dir;
}
else
{
gameid_token_value = gameinfo->info.serial;
}
}
}
else
{
gameid_token_value = gameinfo->info.serial;
}

const std::string target_cli_args = fmt::format("--no-gui \"%%RPCS3_GAMEID%%:%s\"", gameid_token_value);
const std::string target_icon_dir = fmt::format("%sIcons/game_icons/%s/", fs::get_config_dir(), gameinfo->info.serial);

if (!gameid_token_value.empty() && gui::utils::create_shortcut(gameinfo->info.name, target_cli_args, gameinfo->info.name, gameinfo->info.icon_path, target_icon_dir, is_desktop_shortcut ? gui::utils::shortcut_location::desktop : gui::utils::shortcut_location::applications))
{
game_list_log.success("Created %s shortcut for %s", is_desktop_shortcut ? "desktop" : "application menu", sstr(qstr(gameinfo->info.name).simplified()));
QMessageBox::information(this, tr("Success!"), tr("Successfully created a shortcut."));
}
else
{
game_list_log.error("Failed to create %s shortcut for %s", is_desktop_shortcut ? "desktop" : "application menu", sstr(qstr(gameinfo->info.name).simplified()));
QMessageBox::warning(this, tr("Warning!"), tr("Failed to create a shortcut!"));
}
};
QMenu* shortcut_menu = menu.addMenu(tr("&Create Shortcut"));
QAction* create_desktop_shortcut = shortcut_menu->addAction(tr("&Create Desktop Shortcut"));
connect(create_desktop_shortcut, &QAction::triggered, this, [this, gameinfo, on_shortcut](){ on_shortcut(true); });
connect(create_desktop_shortcut, &QAction::triggered, this, [this, gameinfo](){ CreateShortcuts(gameinfo, { gui::utils::shortcut_location::desktop }); });
#ifdef _WIN32
QAction* create_start_menu_shortcut = shortcut_menu->addAction(tr("&Create Start Menu Shortcut"));
#elif defined(__APPLE__)
QAction* create_start_menu_shortcut = shortcut_menu->addAction(tr("&Create Launchpad Shortcut"));
#else
QAction* create_start_menu_shortcut = shortcut_menu->addAction(tr("&Create Application Menu Shortcut"));
#endif
connect(create_start_menu_shortcut, &QAction::triggered, this, [this, gameinfo, on_shortcut](){ on_shortcut(false); });
connect(create_start_menu_shortcut, &QAction::triggered, this, [this, gameinfo](){ CreateShortcuts(gameinfo, { gui::utils::shortcut_location::applications }); });

menu.addSeparator();

Expand Down Expand Up @@ -2795,7 +2837,7 @@ void game_list_frame::SetPlayHoverGifs(bool play)
}
}

QList<game_info> game_list_frame::GetGameInfo() const
const QList<game_info>& game_list_frame::GetGameInfo() const
{
return m_game_data;
}
Expand Down
5 changes: 4 additions & 1 deletion rpcs3/rpcs3qt/game_list_frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "game_list.h"
#include "custom_dock_widget.h"
#include "gui_save.h"
#include "shortcut_utils.h"
#include "Utilities/lockless.h"
#include "Emu/System.h"

Expand Down Expand Up @@ -56,11 +57,13 @@ class game_list_frame : public custom_dock_widget

game_compatibility* GetGameCompatibility() const { return m_game_compat; }

QList<game_info> GetGameInfo() const;
const QList<game_info>& GetGameInfo() const;

// Returns the visible version string in the game list
static std::string GetGameVersion(const game_info& game);

void CreateShortcuts(const game_info& gameinfo, std::vector<gui::utils::shortcut_location> locations);

public Q_SLOTS:
void BatchCreatePPUCaches();
void BatchRemovePPUCaches();
Expand Down
85 changes: 19 additions & 66 deletions rpcs3/rpcs3qt/main_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,7 @@ void main_window::HandlePackageInstallation(QStringList file_paths)
package_error error = package_error::no_error;

bool cancelled = false;
std::map<std::string, QString> bootable_paths_installed; // -> title
std::map<std::string, QString> bootable_paths_installed; // -> title id

for (usz i = 0, count = packages.size(); i < count; i++)
{
Expand Down Expand Up @@ -943,7 +943,7 @@ void main_window::HandlePackageInstallation(QStringList file_paths)

if (!bootable_path.empty())
{
bootable_paths_installed[bootable_path] = package.title;
bootable_paths_installed[bootable_path] = package.title_id;
}

if (i == (count - 1))
Expand Down Expand Up @@ -997,73 +997,26 @@ void main_window::HandlePackageInstallation(QStringList file_paths)
dlg->exec();
}

for (const auto& [boot_path, title] : bootable_paths_installed)
{
if (std::string game_dir = fs::get_parent_dir(boot_path, 2); fs::is_dir(game_dir) && fs::is_file(boot_path))
{
const std::string target_cli_args = fmt::format("--no-gui \"%%RPCS3_GAMEID%%:%s\"", game_dir.substr(game_dir.find_last_of(fs::delim) + 1));
const std::string std_title_id = sstr(package.title_id);
const std::string target_icon_dir = fmt::format("%sIcons/game_icons/%s/", fs::get_config_dir(), std_title_id);

// Copy the icon used by rpcs3 to a file
QTemporaryFile tmp_file(QDir::tempPath() + "/tempFile");
if (!tmp_file.open())
{
gui_log.error("Failed to create icon for '%s'", sstr(title.simplified()));
continue;
}

const QIcon icon = gui::utils::get_app_icon_from_path(rpcs3::utils::get_sfo_dir_from_game_path(boot_path + "/../../"), std_title_id);
QPixmap pix = icon.pixmap(icon.actualSize(QSize(1000, 1000)));
QByteArray bytes;
QBuffer buffer(&bytes);
buffer.open(QIODevice::ReadWrite);
pix.save(&buffer, "PNG");
tmp_file.write(bytes.data(), bytes.size());

std::string icon_path = sstr(tmp_file.fileName());
std::vector<gui::utils::shortcut_location> locations;
#ifdef _WIN32
if (gui::utils::create_shortcut(sstr(title), target_cli_args, sstr(title), icon_path, target_icon_dir, gui::utils::shortcut_location::rpcs3_shortcuts))
{
gui_log.success("Created a shortcut for '%s' at '%s/games/shortcuts/'", sstr(title.simplified()), fs::get_config_dir());
}
locations.push_back(gui::utils::shortcut_location::rpcs3_shortcuts);
#endif
if (create_desktop_shortcuts)
{
locations.push_back(gui::utils::shortcut_location::desktop);
}
if (create_app_shortcut)
{
locations.push_back(gui::utils::shortcut_location::applications);
}

struct install_shortcut_info
{
std::string type;
gui::utils::shortcut_location location;
bool to_install;
};

std::initializer_list<install_shortcut_info> installing_locations =
{
{"desktop", gui::utils::shortcut_location::desktop, create_desktop_shortcuts},
#ifdef _WIN32
{"Start menu", gui::utils::shortcut_location::applications, create_app_shortcut},
#elif defined(__APPLE__)
{"dock", gui::utils::shortcut_location::applications, create_app_shortcut},
#else
{"launcher", gui::utils::shortcut_location::applications, create_app_shortcut},
#endif
};


for (const auto& loc : installing_locations)
for (const auto& [boot_path, title_id] : bootable_paths_installed)
{
for (const game_info& gameinfo : m_game_list_frame->GetGameInfo())
{
if (gameinfo && gameinfo->info.bootable && gameinfo->info.serial == sstr(title_id) && boot_path.starts_with(gameinfo->info.path))
{
if (!loc.to_install)
{
continue;
}

if (gui::utils::create_shortcut(sstr(title), target_cli_args, sstr(title), icon_path, target_icon_dir, loc.location))
{
gui_log.success("Created %s shortcut for %s", loc.type, sstr(title.simplified()));
}
else
{
gui_log.error("Failed to create %s shortcut for %s", loc.type, sstr(title.simplified()));
}
m_game_list_frame->CreateShortcuts(gameinfo, locations);
}
}
}
Expand Down Expand Up @@ -2378,7 +2331,7 @@ void main_window::CreateConnects()
std::unordered_map<std::string, std::set<std::string>> games;
if (m_game_list_frame)
{
for (const auto& game : m_game_list_frame->GetGameInfo())
for (const game_info& game : m_game_list_frame->GetGameInfo())
{
if (game)
{
Expand Down

0 comments on commit fa1b11d

Please sign in to comment.