Merge pull request #1280 from Trial97/shortcut

Fixed illegal characters in shortcuts name
This commit is contained in:
seth 2023-06-27 20:01:48 +00:00 committed by Sefa Eyeoglu
parent 6464127d05
commit 5fdbc9d75e
No known key found for this signature in database
GPG Key ID: E13DFD4B47127951
2 changed files with 97 additions and 119 deletions

View File

@ -372,7 +372,7 @@ void create_link::make_link_list(const QString& offset)
auto src_path = source_it.next(); auto src_path = source_it.next();
auto relative_path = src_dir.relativeFilePath(src_path); auto relative_path = src_dir.relativeFilePath(src_path);
if (m_max_depth >= 0 && pathDepth(relative_path) > m_max_depth){ if (m_max_depth >= 0 && pathDepth(relative_path) > m_max_depth) {
relative_path = pathTruncate(relative_path, m_max_depth); relative_path = pathTruncate(relative_path, m_max_depth);
src_path = src_dir.filePath(relative_path); src_path = src_dir.filePath(relative_path);
if (linkedPaths.contains(src_path)) { if (linkedPaths.contains(src_path)) {
@ -663,7 +663,7 @@ QString pathTruncate(const QString& path, int depth)
QString trunc = QFileInfo(path).path(); QString trunc = QFileInfo(path).path();
if (pathDepth(trunc) > depth ) { if (pathDepth(trunc) > depth) {
return pathTruncate(trunc, depth); return pathTruncate(trunc, depth);
} }
@ -769,6 +769,9 @@ QString getDesktopDir()
// Cross-platform Shortcut creation // Cross-platform Shortcut creation
bool createShortcut(QString destination, QString target, QStringList args, QString name, QString icon) bool createShortcut(QString destination, QString target, QStringList args, QString name, QString icon)
{ {
if (destination.isEmpty()) {
destination = PathCombine(getDesktopDir(), RemoveInvalidFilenameChars(name));
}
#if defined(Q_OS_MACOS) #if defined(Q_OS_MACOS)
destination += ".command"; destination += ".command";
@ -791,6 +794,8 @@ bool createShortcut(QString destination, QString target, QStringList args, QStri
return true; return true;
#elif defined(Q_OS_LINUX) || defined(Q_OS_FREEBSD) || defined(Q_OS_OPENBSD) #elif defined(Q_OS_LINUX) || defined(Q_OS_FREEBSD) || defined(Q_OS_OPENBSD)
if (!destination.endsWith(".desktop")) // in case of isFlatpak destination is already populated
destination += ".desktop";
QFile f(destination); QFile f(destination);
f.open(QIODevice::WriteOnly | QIODevice::Text); f.open(QIODevice::WriteOnly | QIODevice::Text);
QTextStream stream(&f); QTextStream stream(&f);
@ -974,7 +979,7 @@ FilesystemType getFilesystemType(const QString& name)
{ {
for (auto iter = s_filesystem_type_names.constBegin(); iter != s_filesystem_type_names.constEnd(); ++iter) { for (auto iter = s_filesystem_type_names.constBegin(); iter != s_filesystem_type_names.constEnd(); ++iter) {
auto fs_names = iter.value(); auto fs_names = iter.value();
if(fs_names.contains(name.toUpper())) if (fs_names.contains(name.toUpper()))
return iter.key(); return iter.key();
} }
return FilesystemType::UNKNOWN; return FilesystemType::UNKNOWN;

View File

@ -1510,140 +1510,113 @@ void MainWindow::on_actionKillInstance_triggered()
void MainWindow::on_actionCreateInstanceShortcut_triggered() void MainWindow::on_actionCreateInstanceShortcut_triggered()
{ {
if (m_selectedInstance) if (!m_selectedInstance)
{ return;
auto desktopPath = FS::getDesktopDir(); auto desktopPath = FS::getDesktopDir();
if (desktopPath.isEmpty()) { if (desktopPath.isEmpty()) {
// TODO come up with an alternative solution (open "save file" dialog) // TODO come up with an alternative solution (open "save file" dialog)
QMessageBox::critical(this, tr("Create instance shortcut"), tr("Couldn't find desktop?!")); QMessageBox::critical(this, tr("Create instance shortcut"), tr("Couldn't find desktop?!"));
return; return;
} }
QString desktopFilePath;
QString appPath = QApplication::applicationFilePath();
QString iconPath;
QStringList args;
#if defined(Q_OS_MACOS) #if defined(Q_OS_MACOS)
QString appPath = QApplication::applicationFilePath(); if (appPath.startsWith("/private/var/")) {
if (appPath.startsWith("/private/var/")) { QMessageBox::critical(this, tr("Create instance shortcut"),
QMessageBox::critical(this, tr("Create instance shortcut"), tr("The launcher is in the folder it was extracted from, therefore it cannot create shortcuts.")); tr("The launcher is in the folder it was extracted from, therefore it cannot create shortcuts."));
return; return;
} }
if (FS::createShortcut(FS::PathCombine(desktopPath, m_selectedInstance->name()),
appPath, { "--launch", m_selectedInstance->id() },
m_selectedInstance->name(), "")) {
QMessageBox::information(this, tr("Create instance shortcut"), tr("Created a shortcut to this instance on your desktop!"));
}
else
{
QMessageBox::critical(this, tr("Create instance shortcut"), tr("Failed to create instance shortcut!"));
}
#elif defined(Q_OS_LINUX) || defined(Q_OS_FREEBSD) || defined(Q_OS_OPENBSD) #elif defined(Q_OS_LINUX) || defined(Q_OS_FREEBSD) || defined(Q_OS_OPENBSD)
QString appPath = QApplication::applicationFilePath(); if (appPath.startsWith("/tmp/.mount_")) {
if (appPath.startsWith("/tmp/.mount_")) { // AppImage!
// AppImage! appPath = QProcessEnvironment::systemEnvironment().value(QStringLiteral("APPIMAGE"));
appPath = QProcessEnvironment::systemEnvironment().value(QStringLiteral("APPIMAGE")); if (appPath.isEmpty()) {
if (appPath.isEmpty()) QMessageBox::critical(this, tr("Create instance shortcut"),
{ tr("Launcher is running as misconfigured AppImage? ($APPIMAGE environment variable is missing)"));
QMessageBox::critical(this, tr("Create instance shortcut"), tr("Launcher is running as misconfigured AppImage? ($APPIMAGE environment variable is missing)")); } else if (appPath.endsWith("/")) {
} appPath.chop(1);
else if (appPath.endsWith("/"))
{
appPath.chop(1);
}
} }
}
auto icon = APPLICATION->icons()->icon(m_selectedInstance->iconKey()); auto icon = APPLICATION->icons()->icon(m_selectedInstance->iconKey());
if (icon == nullptr) if (icon == nullptr) {
{ icon = APPLICATION->icons()->icon("grass");
icon = APPLICATION->icons()->icon("grass"); }
}
QString iconPath = FS::PathCombine(m_selectedInstance->instanceRoot(), "icon.png"); iconPath = FS::PathCombine(m_selectedInstance->instanceRoot(), "icon.png");
QFile iconFile(iconPath); QFile iconFile(iconPath);
if (!iconFile.open(QFile::WriteOnly)) if (!iconFile.open(QFile::WriteOnly)) {
{ QMessageBox::critical(this, tr("Create instance shortcut"), tr("Failed to create icon for shortcut."));
QMessageBox::critical(this, tr("Create instance shortcut"), tr("Failed to create icon for shortcut.")); return;
return; }
} bool success = icon->icon().pixmap(64, 64).save(&iconFile, "PNG");
bool success = icon->icon().pixmap(64, 64).save(&iconFile, "PNG"); iconFile.close();
iconFile.close();
if (!success) if (!success) {
{ iconFile.remove();
iconFile.remove(); QMessageBox::critical(this, tr("Create instance shortcut"), tr("Failed to create icon for shortcut."));
QMessageBox::critical(this, tr("Create instance shortcut"), tr("Failed to create icon for shortcut.")); return;
return; }
}
if (DesktopServices::isFlatpak()) {
desktopFilePath = FS::PathCombine(desktopPath, FS::RemoveInvalidFilenameChars(m_selectedInstance->name()) + ".desktop");
QFileDialog fileDialog;
// workaround to make sure the portal file dialog opens in the desktop directory
fileDialog.setDirectoryUrl(desktopPath);
desktopFilePath = fileDialog.getSaveFileName(this, tr("Create Shortcut"), desktopFilePath, tr("Desktop Entries (*.desktop)"));
if (desktopFilePath.isEmpty())
return; // file dialog canceled by user
appPath = "flatpak";
QString flatpakAppId = BuildConfig.LAUNCHER_DESKTOPFILENAME;
flatpakAppId.remove(".desktop");
args.append({ "run", flatpakAppId });
}
QString desktopFilePath = FS::PathCombine(desktopPath, m_selectedInstance->name() + ".desktop");
QStringList args;
if (DesktopServices::isFlatpak()) {
QFileDialog fileDialog;
// workaround to make sure the portal file dialog opens in the desktop directory
fileDialog.setDirectoryUrl(desktopPath);
desktopFilePath = fileDialog.getSaveFileName(
this, tr("Create Shortcut"), desktopFilePath,
tr("Desktop Entries (*.desktop)"));
if (desktopFilePath.isEmpty())
return; // file dialog canceled by user
appPath = "flatpak";
QString flatpakAppId = BuildConfig.LAUNCHER_DESKTOPFILENAME;
flatpakAppId.remove(".desktop");
args.append({ "run", flatpakAppId });
}
args.append({ "--launch", m_selectedInstance->id() });
if (FS::createShortcut(desktopFilePath, appPath, args, m_selectedInstance->name(), iconPath)) {
QMessageBox::information(this, tr("Create instance shortcut"), tr("Created a shortcut to this instance on your desktop!"));
}
else
{
iconFile.remove();
QMessageBox::critical(this, tr("Create instance shortcut"), tr("Failed to create instance shortcut!"));
}
#elif defined(Q_OS_WIN) #elif defined(Q_OS_WIN)
auto icon = APPLICATION->icons()->icon(m_selectedInstance->iconKey()); auto icon = APPLICATION->icons()->icon(m_selectedInstance->iconKey());
if (icon == nullptr) if (icon == nullptr) {
{ icon = APPLICATION->icons()->icon("grass");
icon = APPLICATION->icons()->icon("grass"); }
}
QString iconPath = FS::PathCombine(m_selectedInstance->instanceRoot(), "icon.ico"); iconPath = FS::PathCombine(m_selectedInstance->instanceRoot(), "icon.ico");
// part of fix for weird bug involving the window icon being replaced // part of fix for weird bug involving the window icon being replaced
// dunno why it happens, but this 2-line fix seems to be enough, so w/e // dunno why it happens, but this 2-line fix seems to be enough, so w/e
auto appIcon = APPLICATION->getThemedIcon("logo"); auto appIcon = APPLICATION->getThemedIcon("logo");
QFile iconFile(iconPath); QFile iconFile(iconPath);
if (!iconFile.open(QFile::WriteOnly)) if (!iconFile.open(QFile::WriteOnly)) {
{ QMessageBox::critical(this, tr("Create instance shortcut"), tr("Failed to create icon for shortcut."));
QMessageBox::critical(this, tr("Create instance shortcut"), tr("Failed to create icon for shortcut.")); return;
return; }
} bool success = icon->icon().pixmap(64, 64).save(&iconFile, "ICO");
bool success = icon->icon().pixmap(64, 64).save(&iconFile, "ICO"); iconFile.close();
iconFile.close();
// restore original window icon // restore original window icon
QGuiApplication::setWindowIcon(appIcon); QGuiApplication::setWindowIcon(appIcon);
if (!success) if (!success) {
{ iconFile.remove();
iconFile.remove(); QMessageBox::critical(this, tr("Create instance shortcut"), tr("Failed to create icon for shortcut."));
QMessageBox::critical(this, tr("Create instance shortcut"), tr("Failed to create icon for shortcut.")); return;
return; }
}
if (FS::createShortcut(FS::PathCombine(desktopPath, m_selectedInstance->name()),
QApplication::applicationFilePath(), { "--launch", m_selectedInstance->id() },
m_selectedInstance->name(), iconPath)) {
QMessageBox::information(this, tr("Create instance shortcut"), tr("Created a shortcut to this instance on your desktop!"));
}
else
{
iconFile.remove();
QMessageBox::critical(this, tr("Create instance shortcut"), tr("Failed to create instance shortcut!"));
}
#else #else
QMessageBox::critical(this, tr("Create instance shortcut"), tr("Not supported on your platform!")); QMessageBox::critical(this, tr("Create instance shortcut"), tr("Not supported on your platform!"));
return;
#endif #endif
args.append({ "--launch", m_selectedInstance->id() });
if (FS::createShortcut(desktopFilePath, appPath, args, m_selectedInstance->name(), iconPath)) {
QMessageBox::information(this, tr("Create instance shortcut"), tr("Created a shortcut to this instance on your desktop!"));
} else {
#if not defined(Q_OS_MACOS)
iconFile.remove();
#endif
QMessageBox::critical(this, tr("Create instance shortcut"), tr("Failed to create instance shortcut!"));
} }
} }