DisplayMagician/HeliosDisplayManagement/UIForms/ShortcutForm.cs

420 lines
13 KiB
C#
Raw Normal View History

2017-02-26 19:23:31 +00:00
using System;
using System.Collections.Generic;
using System.Drawing.IconLib;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using HeliosDisplayManagement.Resources;
using HeliosDisplayManagement.Shared;
using HeliosDisplayManagement.Steam;
namespace HeliosDisplayManagement.UIForms
{
public partial class ShortcutForm : Form
{
public ShortcutForm()
{
InitializeComponent();
}
public ShortcutForm(Profile profile) : this()
{
Profile = profile;
}
2017-08-07 16:38:48 +00:00
public string Arguments
2017-02-26 19:23:31 +00:00
{
get => rb_args.Checked ? txt_args_executable.Text : string.Empty;
2017-08-07 16:38:48 +00:00
set
{
txt_args_executable.Text = value;
rb_args.Checked = !string.IsNullOrWhiteSpace(txt_args_executable.Text);
2017-08-07 16:38:48 +00:00
}
2017-02-26 19:23:31 +00:00
}
public string FileName
{
2018-10-20 00:27:25 +00:00
get => cb_temp.Checked && rb_standalone.Checked ? txt_executable.Text : string.Empty;
2017-02-26 19:23:31 +00:00
set
{
if (!string.IsNullOrWhiteSpace(value))
{
cb_temp.Checked = true;
rb_standalone.Checked = true;
txt_executable.Text = value;
}
}
}
2017-08-07 16:38:48 +00:00
public static string IconCache
2018-10-20 00:27:25 +00:00
{
get => Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
2017-08-07 16:38:48 +00:00
Assembly.GetExecutingAssembly().GetName().Name, @"IconCache");
2018-10-20 00:27:25 +00:00
}
2017-02-26 19:23:31 +00:00
public string ProcessName
{
2018-10-20 00:27:25 +00:00
get => cb_temp.Checked && rb_standalone.Checked && cb_process.Checked ? txt_process.Text : string.Empty;
2017-02-26 19:23:31 +00:00
set
{
txt_process.Text = value;
cb_process.Checked = !string.IsNullOrWhiteSpace(txt_process.Text);
}
}
2017-08-07 16:38:48 +00:00
public Profile Profile
{
2018-10-20 00:27:25 +00:00
get => dv_profile.Profile;
set => dv_profile.Profile = value;
2017-08-07 16:38:48 +00:00
}
2017-02-26 19:23:31 +00:00
public uint SteamAppId
{
get => cb_temp.Checked && rb_launcher.Checked ? (uint) nud_appid.Value : 0;
2017-02-26 19:23:31 +00:00
set
{
if (value > 0)
{
cb_temp.Checked = true;
rb_launcher.Checked = true;
nud_appid.Value = value;
2017-02-26 19:23:31 +00:00
}
}
}
public uint Timeout
{
get
{
if (!cb_temp.Checked)
2018-10-20 00:27:25 +00:00
{
2017-02-26 19:23:31 +00:00
return 0;
2018-10-20 00:27:25 +00:00
}
2017-02-26 19:23:31 +00:00
if (!rb_standalone.Checked)
2018-10-20 00:27:25 +00:00
{
2017-02-26 19:23:31 +00:00
return (uint) nud_steamtimeout.Value;
2018-10-20 00:27:25 +00:00
}
2017-02-26 19:23:31 +00:00
if (cb_process.Checked)
2018-10-20 00:27:25 +00:00
{
2017-02-26 19:23:31 +00:00
return (uint) nud_timeout.Value;
2018-10-20 00:27:25 +00:00
}
2017-02-26 19:23:31 +00:00
return 0;
}
set
{
if (value > 0)
{
nud_timeout.Value = value;
nud_steamtimeout.Value = value;
}
}
}
private void btn_app_executable_Click(object sender, EventArgs e)
{
if (dialog_open.ShowDialog(this) == DialogResult.OK)
2018-10-20 00:27:25 +00:00
{
if (File.Exists(dialog_open.FileName) && Path.GetExtension(dialog_open.FileName) == @".exe")
2017-02-26 19:23:31 +00:00
{
txt_executable.Text = dialog_open.FileName;
dialog_open.FileName = string.Empty;
}
else
{
MessageBox.Show(
Language.Selected_file_is_not_a_valid_executable_file,
Language.Executable,
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
}
2018-10-20 00:27:25 +00:00
}
2017-02-26 19:23:31 +00:00
}
2017-08-07 16:38:48 +00:00
private void btn_save_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.None;
2018-10-20 00:27:25 +00:00
2017-08-07 16:38:48 +00:00
try
{
if (dialog_save.ShowDialog(this) == DialogResult.OK)
{
if (CreateShortcut(dialog_save.FileName))
2018-10-20 00:27:25 +00:00
{
2017-08-07 16:38:48 +00:00
MessageBox.Show(
Language.Shortcut_place_successfully,
Language.Shortcut,
MessageBoxButtons.OK,
MessageBoxIcon.Information);
2018-10-20 00:27:25 +00:00
}
2017-08-07 16:38:48 +00:00
else
2018-10-20 00:27:25 +00:00
{
2017-08-07 16:38:48 +00:00
MessageBox.Show(
Language.Failed_to_create_the_shortcut_Unexpected_exception_occurred,
Language.Shortcut,
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
2018-10-20 00:27:25 +00:00
}
2017-08-07 16:38:48 +00:00
dialog_save.FileName = string.Empty;
DialogResult = DialogResult.OK;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, Language.Shortcut, MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private void Controls_CheckedChanged(object sender, EventArgs e)
{
g_temp.Enabled = cb_temp.Checked;
p_standalone.Enabled = rb_standalone.Checked;
txt_process.Enabled = cb_process.Checked;
nud_timeout.Enabled = cb_process.Checked;
p_steam.Enabled = rb_launcher.Checked;
2017-08-07 16:38:48 +00:00
txt_args_executable.Enabled = cb_args.Checked;
2017-08-07 16:38:48 +00:00
if (rb_launcher.Checked)
2018-10-20 00:27:25 +00:00
{
nud_steamappid_ValueChanged(rb_launcher, e);
2018-10-20 00:27:25 +00:00
}
2017-08-07 16:38:48 +00:00
}
2017-02-26 19:23:31 +00:00
// ReSharper disable once FunctionComplexityOverflow
2017-08-07 16:38:48 +00:00
// ReSharper disable once CyclomaticComplexity
2017-02-26 19:23:31 +00:00
private bool CreateShortcut(string fileName)
{
var programName = Path.GetFileNameWithoutExtension(txt_executable.Text);
var description = string.Empty;
var icon = string.Empty;
var args = new List<string>
{
$"-a {HeliosStartupAction.SwitchProfile}",
$"-p \"{dv_profile.Profile.Id}\""
2017-02-26 19:23:31 +00:00
};
2018-10-20 00:27:25 +00:00
if (!Directory.Exists(IconCache))
2018-10-20 00:27:25 +00:00
{
try
{
Directory.CreateDirectory(IconCache);
}
catch
{
// ignored
}
2018-10-20 00:27:25 +00:00
}
2017-02-26 19:23:31 +00:00
if (cb_temp.Checked)
{
if (rb_standalone.Checked)
{
if (string.IsNullOrWhiteSpace(txt_executable.Text))
2018-10-20 00:27:25 +00:00
{
2017-02-26 19:23:31 +00:00
throw new Exception(Language.Executable_address_can_not_be_empty);
2018-10-20 00:27:25 +00:00
}
2017-02-26 19:23:31 +00:00
if (!File.Exists(txt_executable.Text))
2018-10-20 00:27:25 +00:00
{
2017-02-26 19:23:31 +00:00
throw new Exception(Language.Executable_file_not_found);
2018-10-20 00:27:25 +00:00
}
2017-02-26 19:23:31 +00:00
args.Add($"-e \"{txt_executable.Text.Trim()}\"");
2018-10-20 00:27:25 +00:00
2017-02-26 19:23:31 +00:00
if (!string.IsNullOrWhiteSpace(txt_process.Text))
{
args.Add($"-w \"{txt_process.Text.Trim()}\"");
args.Add($"-t {(int) nud_timeout.Value}");
}
2018-10-20 00:27:25 +00:00
2017-02-26 19:23:31 +00:00
description = string.Format(Language.Executing_application_with_profile, programName, Profile.Name);
2018-10-20 00:27:25 +00:00
2017-02-26 19:23:31 +00:00
try
{
icon = Path.Combine(IconCache, Guid.NewGuid() + ".ico");
new ProfileIcon(Profile).ToIconOverly(txt_executable.Text)
.Save(icon, MultiIconFormat.ICO);
}
2017-08-07 16:38:48 +00:00
catch (Exception)
2017-02-26 19:23:31 +00:00
{
icon = $"{txt_executable.Text.Trim()},0";
}
}
else if (rb_launcher.Checked)
2017-02-26 19:23:31 +00:00
{
if (!SteamGame.SteamInstalled)
2018-10-20 00:27:25 +00:00
{
2017-02-26 19:23:31 +00:00
throw new Exception(Language.Steam_is_not_installed);
2018-10-20 00:27:25 +00:00
}
var steamGame = new SteamGame((uint) nud_appid.Value);
args.Add($"-s {(int) nud_appid.Value}");
2017-02-26 19:23:31 +00:00
args.Add($"-t {(int) nud_steamtimeout.Value}");
description = string.Format(Language.Executing_application_with_profile, steamGame.Name,
Profile.Name);
var steamIcon = steamGame.GetIcon().Result;
2018-10-20 00:27:25 +00:00
2017-02-26 19:23:31 +00:00
if (!string.IsNullOrWhiteSpace(steamIcon))
2018-10-20 00:27:25 +00:00
{
2017-02-26 19:23:31 +00:00
try
{
icon = Path.Combine(IconCache, Guid.NewGuid() + ".ico");
new ProfileIcon(Profile).ToIconOverly(steamIcon)
.Save(icon, MultiIconFormat.ICO);
}
2017-08-07 16:38:48 +00:00
catch (Exception)
2017-02-26 19:23:31 +00:00
{
icon = steamIcon;
}
2018-10-20 00:27:25 +00:00
}
2017-02-26 19:23:31 +00:00
else
2018-10-20 00:27:25 +00:00
{
2017-02-26 19:23:31 +00:00
icon = $"{SteamGame.SteamAddress},0";
2018-10-20 00:27:25 +00:00
}
2017-02-26 19:23:31 +00:00
}
2018-10-20 00:27:25 +00:00
if (cb_args.Checked && !string.IsNullOrWhiteSpace(txt_args_executable.Text))
2018-10-20 00:27:25 +00:00
{
args.Add($"--arguments \"{txt_args_executable.Text.Trim()}\"");
2018-10-20 00:27:25 +00:00
}
2017-02-26 19:23:31 +00:00
}
else
{
description = string.Format(Language.Switching_display_profile_to_profile, Profile.Name);
2018-10-20 00:27:25 +00:00
2017-02-26 19:23:31 +00:00
try
{
icon = Path.Combine(IconCache, Guid.NewGuid() + ".ico");
new ProfileIcon(Profile).ToIcon().Save(icon, MultiIconFormat.ICO);
}
catch
{
icon = string.Empty;
}
}
fileName = Path.ChangeExtension(fileName, @"lnk");
2018-10-20 00:27:25 +00:00
2017-02-26 19:23:31 +00:00
if (fileName != null)
2018-10-20 00:27:25 +00:00
{
2017-02-26 19:23:31 +00:00
try
{
// Remove the old file to replace it
if (File.Exists(fileName))
2018-10-20 00:27:25 +00:00
{
2017-02-26 19:23:31 +00:00
File.Delete(fileName);
2018-10-20 00:27:25 +00:00
}
2017-02-26 19:23:31 +00:00
var wshShellType = Type.GetTypeFromCLSID(new Guid("72C24DD5-D70A-438B-8A42-98424B88AFB8"));
dynamic wshShell = Activator.CreateInstance(wshShellType);
2018-10-20 00:27:25 +00:00
2017-02-26 19:23:31 +00:00
try
{
var shortcut = wshShell.CreateShortcut(fileName);
2018-10-20 00:27:25 +00:00
2017-02-26 19:23:31 +00:00
try
{
shortcut.TargetPath = Application.ExecutablePath;
shortcut.Arguments = string.Join(" ", args);
shortcut.Description = description;
shortcut.WorkingDirectory = Path.GetDirectoryName(Application.ExecutablePath) ??
string.Empty;
2018-10-20 00:27:25 +00:00
2017-02-26 19:23:31 +00:00
if (!string.IsNullOrWhiteSpace(icon))
2018-10-20 00:27:25 +00:00
{
2017-02-26 19:23:31 +00:00
shortcut.IconLocation = icon;
2018-10-20 00:27:25 +00:00
}
2017-02-26 19:23:31 +00:00
shortcut.Save();
}
finally
{
Marshal.FinalReleaseComObject(shortcut);
}
}
finally
{
Marshal.FinalReleaseComObject(wshShell);
}
}
catch
{
// Clean up a failed attempt
if (File.Exists(fileName))
2018-10-20 00:27:25 +00:00
{
2017-02-26 19:23:31 +00:00
File.Delete(fileName);
2018-10-20 00:27:25 +00:00
}
2017-02-26 19:23:31 +00:00
}
2018-10-20 00:27:25 +00:00
}
return fileName != null && File.Exists(fileName);
2017-02-26 19:23:31 +00:00
}
2017-08-07 16:38:48 +00:00
private void nud_steamappid_ValueChanged(object sender, EventArgs e)
2017-02-26 19:23:31 +00:00
{
lbl_steamname.Text = new SteamGame((uint) nud_appid.Value).ToString();
2017-08-07 16:38:48 +00:00
}
private void nud_steamapps_Click(object sender, EventArgs e)
{
var steamGamesForm = new SteamGamesForm();
2018-10-20 00:27:25 +00:00
if (steamGamesForm.ShowDialog(this) == DialogResult.OK && steamGamesForm.SteamGame != null)
{
nud_appid.Value = steamGamesForm.SteamGame.AppId;
2018-10-20 00:27:25 +00:00
}
2017-02-26 19:23:31 +00:00
}
private void txt_executable_TextChanged(object sender, EventArgs e)
{
try
{
txt_process.Text = Path.GetFileNameWithoutExtension(txt_executable.Text)?.ToLower() ?? txt_process.Text;
}
catch
{
// ignored
}
}
private void rb_switch_temp_CheckedChanged(object sender, EventArgs e)
{
g_temp.Enabled = rb_switch_temp.Checked;
p_standalone.Enabled = rb_standalone.Checked;
txt_process.Enabled = cb_process.Checked;
nud_timeout.Enabled = cb_process.Checked;
p_steam.Enabled = rb_launcher.Checked;
txt_args_executable.Enabled = cb_args.Checked;
if (rb_launcher.Checked)
{
nud_steamappid_ValueChanged(rb_launcher, e);
}
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label7_Click(object sender, EventArgs e)
{
}
private void cb_args_executable_CheckedChanged(object sender, EventArgs e)
{
}
2017-02-26 19:23:31 +00:00
}
}