Skip to content

Commit

Permalink
Get shortcut arguments and icon path to use when quick building custo…
Browse files Browse the repository at this point in the history
…m shortcut (#168)
  • Loading branch information
DavidIQ authored Apr 25, 2021
1 parent 21e1c15 commit 47262f5
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 31 deletions.
2 changes: 1 addition & 1 deletion TileIconifier.Core/Custom/Steam/SteamLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public string GetSteamExePath()

if (SteamShortcutItem != null)
{
return SteamShortcutItem.TargetFilePath;
return SteamShortcutItem.TargetInfo.FilePath;
}
var assumedSteamExePath = _steamInstallationFolderPath + "Steam.exe";
if (File.Exists(assumedSteamExePath))
Expand Down
26 changes: 13 additions & 13 deletions TileIconifier.Core/Shortcut/ShortcutItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public CustomShortcut CustomShortcut
}
try
{
return CustomShortcut.Load(TargetFilePath);
return CustomShortcut.Load(TargetInfo.FilePath);
}
catch
{
Expand All @@ -96,7 +96,7 @@ public ShortcutUser ShortcutUser
}
}

public bool IsValidForIconification => !string.IsNullOrEmpty(TargetFilePath) && File.Exists(TargetFilePath);
public bool IsValidForIconification => !string.IsNullOrEmpty(TargetInfo.FilePath) && File.Exists(TargetInfo.FilePath);

//todo: merge this with property IconifiedByTileIconifier at some point, should be a better indicator but won't work retroactively
public bool IsIconified => File.Exists(VisualElementManifestPath)
Expand Down Expand Up @@ -151,47 +151,47 @@ public Bitmap StandardIcon

#region Path properties

private string _targetFilePath;
private ShortcutItemTarget _targetInfo = new ShortcutItemTarget();

public string TargetFilePath
public ShortcutItemTarget TargetInfo
{
get
{
if (string.IsNullOrEmpty(_targetFilePath))
if (string.IsNullOrEmpty(_targetInfo.FilePath))
{
_targetFilePath = ShortcutUtils.GetTargetPath(ShortcutFileInfo.FullName);
_targetInfo = ShortcutUtils.GetTargetInfo(ShortcutFileInfo.FullName);
}

return
Environment.ExpandEnvironmentVariables("%PATHEXT%").Split(';').Any(
e =>
string.Equals(Path.GetExtension(_targetFilePath), e,
string.Equals(Path.GetExtension(_targetInfo.FilePath), e,
StringComparison.InvariantCultureIgnoreCase))
? _targetFilePath
: null;
? _targetInfo
: new ShortcutItemTarget();
}
}

public string VisualElementManifestPath =>
$"{TargetFolderPath}{Path.GetFileNameWithoutExtension(TargetFilePath)}.VisualElementsManifest.xml";
$"{TargetFolderPath}{Path.GetFileNameWithoutExtension(TargetInfo.FilePath)}.VisualElementsManifest.xml";

public string VisualElementManifestPathOriginalBackup =>
VisualElementManifestPath + ".originalbak";

public string TargetFolderPath => Path.GetDirectoryName(TargetFilePath) + "\\";
public string TargetFolderPath => Path.GetDirectoryName(TargetInfo.FilePath) + "\\";

public string VisualElementsPath => TargetFolderPath + @"\VisualElements\";

public string MediumIconName
=> $"MediumIcon{Path.GetFileNameWithoutExtension(TargetFilePath)}.png";
=> $"MediumIcon{Path.GetFileNameWithoutExtension(TargetInfo.FilePath)}.png";

public string RelativeMediumIconPath
=> $"{Path.GetFileName(Path.GetDirectoryName(VisualElementsPath))}\\{MediumIconName}";

public string FullMediumIconPath => $"{VisualElementsPath}{MediumIconName}";

public string SmallIconName
=> $"SmallIcon{Path.GetFileNameWithoutExtension(TargetFilePath)}.png";
=> $"SmallIcon{Path.GetFileNameWithoutExtension(TargetInfo.FilePath)}.png";

public string RelativeSmallIconPath
=> $"{Path.GetFileName(Path.GetDirectoryName(VisualElementsPath))}\\{SmallIconName}";
Expand Down
2 changes: 1 addition & 1 deletion TileIconifier.Core/Shortcut/ShortcutItemEnumeration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public ShortcutItem FindShortcutMatch(List<ShortcutItem> shortcutsCache)
if (!string.IsNullOrEmpty(DesktopApplicationId))
{
matchingShortcutItems.AddRange(_shortcutsCache.Where(
s => Path.GetFullPath(Environment.ExpandEnvironmentVariables(s.TargetFilePath)) ==
s => Path.GetFullPath(Environment.ExpandEnvironmentVariables(s.TargetInfo.FilePath)) ==
Path.GetFullPath(Environment.ExpandEnvironmentVariables(DesktopApplicationId))));
matchingShortcutItems.AddRange(shortcutsCache.Where(s => s.AppId == DesktopApplicationId));
}
Expand Down
16 changes: 16 additions & 0 deletions TileIconifier.Core/Shortcut/ShortcutItemTarget.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TileIconifier.Core.Shortcut
{
[Serializable]
public class ShortcutItemTarget
{
public string FilePath { get; set; }
public string Arguments { get; set; }
public string IconLocation { get; set; }
}
}
1 change: 1 addition & 0 deletions TileIconifier.Core/TileIconifier.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
<Compile Include="Shortcut\ShortcutItem.cs" />
<Compile Include="Shortcut\ShortcutItemEnumeration.cs" />
<Compile Include="Shortcut\ShortcutItemImage.cs" />
<Compile Include="Shortcut\ShortcutItemTarget.cs" />
<Compile Include="Shortcut\State\ShortcutItemState.cs" />
<Compile Include="Shortcut\State\ShortcutItemStateController.cs" />
<Compile Include="TileIconify\TileIcon.cs" />
Expand Down
2 changes: 1 addition & 1 deletion TileIconifier.Core/TileIconify/TileIcon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ private void DeleteFilesAndFolders()
}

var manifestPath =
$"{_shortcutItem.TargetFolderPath}\\{Path.GetFileNameWithoutExtension(_shortcutItem.TargetFilePath)}.VisualElementsManifest.xml";
$"{_shortcutItem.TargetFolderPath}\\{Path.GetFileNameWithoutExtension(_shortcutItem.TargetInfo.FilePath)}.VisualElementsManifest.xml";

if (File.Exists(manifestPath))
{
Expand Down
21 changes: 14 additions & 7 deletions TileIconifier.Core/Utilities/ShortcutUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,19 @@
using System.Text;
using IWshRuntimeLibrary;
using TileIconifier.Core.Properties;
using TileIconifier.Core.Shortcut;
using File = System.IO.File;

namespace TileIconifier.Core.Utilities
{
// Source: https://astoundingprogramming.wordpress.com/2012/12/17/how-to-get-the-target-of-a-windows-shortcut-c/
public static class ShortcutUtils
{
public static string GetTargetPath(string filePath)
public static ShortcutItemTarget GetTargetInfo(string filePath)
{
var targetPath = ResolveMsiShortcut(filePath) ?? ResolveShortcut(filePath);
var targetInfo = ResolveMsiShortcut(filePath) ?? ResolveShortcut(filePath);

return targetPath;
return targetInfo;
}

public static string GetInternetShortcut(string filePath)
Expand All @@ -66,15 +67,21 @@ public static string GetInternetShortcut(string filePath)
return url;
}

public static string ResolveShortcut(string filePath)
public static ShortcutItemTarget ResolveShortcut(string filePath)
{
// IWshRuntimeLibrary is in the COM library "Windows Script Host Object Model"
var shell = new WshShell();

try
{
var shortcut = (IWshShortcut) shell.CreateShortcut(filePath);
return shortcut.TargetPath;
var iconLocation = shortcut.IconLocation ?? string.Empty;
return new ShortcutItemTarget
{
FilePath = shortcut.TargetPath,
Arguments = shortcut.Arguments,
IconLocation = shortcut.IconLocation.Split(',')[0]
};
}
catch (COMException)
{
Expand Down Expand Up @@ -110,7 +117,7 @@ public static void CreateUrlFile(string path, string target)
File.WriteAllText(path, string.Format(Resources.UrlFileTemplate, target));
}

private static string ResolveMsiShortcut(string file)
private static ShortcutItemTarget ResolveMsiShortcut(string file)
{
var product = new StringBuilder(NativeMethods.MAX_GUID_LENGTH + 1);
var feature = new StringBuilder(NativeMethods.MAX_FEATURE_LENGTH + 1);
Expand All @@ -124,7 +131,7 @@ private static string ResolveMsiShortcut(string file)
var installState = NativeMethods.MsiGetComponentPath(product.ToString(), component.ToString(), path,
ref pathLength);

return installState == NativeMethods.InstallState.Local ? path.ToString() : null;
return installState == NativeMethods.InstallState.Local ? (new ShortcutItemTarget { FilePath = path.ToString() }) : null;
}

private static class NativeMethods
Expand Down
6 changes: 3 additions & 3 deletions TileIconifier/Controls/IconifierPanel/TileIconifierPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,19 +203,19 @@ private void IconSet(object sender)
{
try
{
var customShortcutExecutionTarget = CustomShortcut.Load(CurrentShortcutItem.TargetFilePath);
var customShortcutExecutionTarget = CustomShortcut.Load(CurrentShortcutItem.TargetInfo.FilePath);
imagePath = customShortcutExecutionTarget.TargetPath.UnQuoteWrap();
}
catch (InvalidCustomShortcutException)
{
//corrupted custom shortcut?
imagePath = CurrentShortcutItem.TargetFilePath;
imagePath = CurrentShortcutItem.TargetInfo.FilePath;
}
}
else
{
//otherwise we just use the target file path from the shortcut
imagePath = CurrentShortcutItem.TargetFilePath;
imagePath = CurrentShortcutItem.TargetInfo.FilePath;
}
}
selectedImage = FrmIconSelector.GetImage(this, imagePath);
Expand Down
7 changes: 4 additions & 3 deletions TileIconifier/Forms/Main/FrmMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,11 @@ private void btnBuildCustomShortcut_Click(object sender, EventArgs e)

shortcutName = cloneConfirmation.ShortcutName;

var parameters = new GenerateCustomShortcutParams(CurrentShortcutItem.TargetFilePath, string.Empty,
var parameters = new GenerateCustomShortcutParams(CurrentShortcutItem.TargetInfo.FilePath, CurrentShortcutItem.TargetInfo.Arguments,
CustomShortcutGetters.CustomShortcutCurrentUserPath)
{
WorkingFolder = CurrentShortcutItem.ShortcutFileInfo.Directory?.FullName
WorkingFolder = CurrentShortcutItem.ShortcutFileInfo.Directory?.FullName,
IconPath = CurrentShortcutItem.TargetInfo.IconLocation
};

var customShortcut = new OtherCustomShortcutBuilder(parameters).GenerateCustomShortcut(shortcutName);
Expand Down Expand Up @@ -276,7 +277,7 @@ private void btnDeleteCustomShortcut_Click(object sender, EventArgs e)

try
{
var customShortcut = CustomShortcut.Load(CurrentShortcutItem.TargetFilePath);
var customShortcut = CustomShortcut.Load(CurrentShortcutItem.TargetInfo.FilePath);
customShortcut.Delete();
}
catch (Exception ex)
Expand Down
4 changes: 2 additions & 2 deletions TileIconifier/Forms/Main/FrmMainMethods.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 47262f5

Please sign in to comment.