Handle errors on read only systems when creating shortcut

pull/35/head
Miepee 4 years ago
parent e177266a72
commit 6ab0f75e38

@ -817,57 +817,68 @@ public partial class MainForm : Form
string desktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop, Environment.SpecialFolderOption.Create); string desktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop, Environment.SpecialFolderOption.Create);
string shortcutFile = ""; string shortcutFile = "";
if (OS.IsWindows) try
{ {
//TODO: implement this if (OS.IsWindows)
} {
else if (OS.IsLinux) //TODO: implement this
{ }
shortcutFile = $"{desktopFolder}/{profile.Name}.desktop"; else if (OS.IsLinux)
{
const string desktopEntryTemplate = shortcutFile = $"{desktopFolder}/{profile.Name}.desktop";
"[Desktop Entry]\n" +
"Type=Application\n" + const string desktopEntryTemplate =
"Categories=Game\n" + "[Desktop Entry]\n" +
"Encoding=UTF-8\n" + "Type=Application\n" +
"Name=PROFILENAME\n" + "Categories=Game\n" +
"Comment=PROFILEDESCRIPTION\n" + "Encoding=UTF-8\n" +
"Exec=EXECUTABLE\n" + "Name=PROFILENAME\n" +
"Icon=ICONPATH\n" + "Comment=PROFILEDESCRIPTION\n" +
"Terminal=false"; "Exec=EXECUTABLE\n" +
"Icon=ICONPATH\n" +
string desktopEntryText = desktopEntryTemplate; "Terminal=false";
// Replace values string desktopEntryText = desktopEntryTemplate;
desktopEntryText = desktopEntryText.Replace("PROFILENAME", $"{profile.Name}");
desktopEntryText = desktopEntryText.Replace("PROFILEDESCRIPTION", $"A shortcut for {profile.Name}."); // Replace values
desktopEntryText = desktopEntryText.Replace("ICONPATH", $"{Core.PatchDataPath}/data/files_to_copy/icon.png"); desktopEntryText = desktopEntryText.Replace("PROFILENAME", $"{profile.Name}");
desktopEntryText = desktopEntryText.Replace("PROFILEDESCRIPTION", $"A shortcut for {profile.Name}.");
string gameName; desktopEntryText = desktopEntryText.Replace("ICONPATH", $"{Core.PatchDataPath}/data/files_to_copy/icon.png");
#if !NOAPPIMAGE
gameName = "runner"; string gameName;
#else #if !NOAPPIMAGE
gameName = "AM2R.AppImage"; gameName = "runner";
#endif #else
if (OS.IsThisRunningFromFlatpak) gameName = "AM2R.AppImage";
desktopEntryText = desktopEntryText.Replace("EXECUTABLE", $"flatpak run \"--command={Core.ProfilesPath}/{profile.Name}/{gameName}\" io.github.am2r_community_developers.AM2RLauncher"); #endif
if (OS.IsThisRunningFromFlatpak)
desktopEntryText = desktopEntryText.Replace("EXECUTABLE", $"flatpak run \"--command={Core.ProfilesPath}/{profile.Name}/{gameName}\" io.github.am2r_community_developers.AM2RLauncher");
else
desktopEntryText = desktopEntryText.Replace("EXECUTABLE", $"{Core.ProfilesPath}/{profile.Name}/{gameName}");
File.WriteAllText(shortcutFile, desktopEntryText);
}
else if (OS.IsMac)
{
throw new NotImplementedException("Creating Desktop Shortcuts on Mac has currently not been implemented!");
}
else else
desktopEntryText = desktopEntryText.Replace("EXECUTABLE", $"{Core.ProfilesPath}/{profile.Name}/{gameName}"); {
log.Error($"{OS.Name} has no way of creating shortcuts");
return;
}
File.WriteAllText(shortcutFile, desktopEntryText); CrossPlatformOperations.OpenFolderAndSelectFile(shortcutFile);
} }
else if (OS.IsMac) // We only care about io exceptions (file not readable, drive not available etc.) The rest should throw normally
catch (IOException exception)
{ {
throw new NotImplementedException("Creating Desktop Shortcuts on Mac has currently not been implemented!"); Application.Instance.Invoke(() =>
} {
else MessageBox.Show(exception.Message, Text.UnhandledException, MessageBoxType.Error);
{ });
log.Error($"{OS.Name} has no way of creating shortcuts");
return;
} }
CrossPlatformOperations.OpenFolderAndSelectFile(shortcutFile);
} }
/// <summary> /// <summary>

Loading…
Cancel
Save