[WIP] Improved Shortcut handling

Created ShortcutRepository which handles
the loading/saving and lifecycle management
of shortcuts. There can only be one shortcut
repository at a time, hence it's mainly static
methods.

Also started down the path of troubleshooting
the profileIcon overlays. The ToBitmapOverlay
still isn't working properly, so I need to figure
out how it was working originally, and how I
broke it :). Then I need to unbreak it.
This commit is contained in:
Terry MacDonald 2020-06-01 22:25:52 +12:00
parent b31d233678
commit f59490a1ff
9 changed files with 591 additions and 305 deletions

View File

@ -127,7 +127,7 @@ namespace HeliosPlus.Shared
return path;
}
public Bitmap ToBitmap(int width, int height, PixelFormat format = PixelFormat.Format32bppArgb)
public Bitmap ToBitmap(int width = 128, int height = 128, PixelFormat format = PixelFormat.Format32bppArgb)
{
var bitmap = new Bitmap(width, height, format);
bitmap.MakeTransparent();
@ -141,17 +141,40 @@ namespace HeliosPlus.Shared
return bitmap;
}
public Bitmap ToBitmapOverly(Bitmap bitmap)
public Bitmap ToBitmapOverlay(Bitmap bitmap, int width = 0, int height = 0, PixelFormat format = PixelFormat.Format32bppArgb)
{
var viewSize = CalculateViewSize(_profile.Viewports, true, PaddingX, PaddingY);
var width = bitmap.Width * 0.7f;
var height = width / viewSize.Width * viewSize.Height;
using (var g = Graphics.FromImage(bitmap))
if (width == 0)
width = bitmap.Width;
if (height == 0)
height = bitmap.Height;
var viewSize = CalculateViewSize(_profile.Viewports, true, PaddingX, PaddingY);
int viewSizeRatio = (int) Math.Round(viewSize.Width * viewSize.Height);
int overlayWidth = (int) Math.Round(width * 0.7f,0);
int overlayHeight = overlayWidth / viewSizeRatio;
int overlayX = width - overlayWidth;
int overlayY = height - overlayHeight;
Point overlayPosition = new Point(overlayX, overlayY);
Size overlaySize = new Size(overlayWidth, overlayHeight);
Rectangle overlayRect = new Rectangle(overlayPosition, overlaySize);
//var width = bitmap.Width * 0.7f;
//var height = width / viewSize.Width * viewSize.Height;
var combinedBitmap = new Bitmap(width, height, format);
combinedBitmap.MakeTransparent();
using (var g = Graphics.FromImage(combinedBitmap))
{
g.SmoothingMode = SmoothingMode.HighQuality;
g.TranslateTransform(bitmap.Width - width, bitmap.Height - height * 1.1f);
DrawView(g, width, height);
//g.DrawImage(bitmap, 0, 0, width, height);
g.TranslateTransform(overlayX, overlayY);
//Rectangle compressionRectangle = new Rectangle(300, 10,
//myBitmap.Width / 2, myBitmap.Height / 2);
g.DrawRectangle(new Pen(Color.FromArgb(125, 50, 50, 50), 2f), overlayRect);
DrawView(g, overlayWidth, overlayHeight);
}
return bitmap;
@ -186,7 +209,7 @@ namespace HeliosPlus.Shared
return multiIcon;
}
public MultiIcon ToIconOverly(string iconAddress)
public MultiIcon ToIconOverlay(string iconAddress)
{
var multiIcon = new MultiIcon();
var icon = multiIcon.Add("Icon1");
@ -226,7 +249,7 @@ namespace HeliosPlus.Shared
bitmap = clone;
}
icon.Add(singleIcon.Size.Height * singleIcon.Size.Width < 24 * 24 ? bitmap : ToBitmapOverly(bitmap));
icon.Add(singleIcon.Size.Height * singleIcon.Size.Width < 24 * 24 ? bitmap : ToBitmapOverlay(bitmap));
if (singleIcon.Size.Width >= 256 && singleIcon.Size.Height >= 256)
{

View File

@ -78,6 +78,7 @@
<Compile Include="GameLibraries\SteamAppInfoParser\PackageInfo.cs" />
<Compile Include="GameLibraries\SteamAppInfoParser\App.cs" />
<Compile Include="Shortcut.cs" />
<Compile Include="ShortcutRepository.cs" />
<Compile Include="UIForms\ShortcutAdaptor.cs" />
<Compile Include="UIForms\ProfileAdaptor.cs" />
<Compile Include="UIForms\MainForm.cs">

View File

@ -38,6 +38,7 @@ namespace HeliosPlus
private MultiIcon _shortcutIcon, _originalIcon = null;
private Bitmap _shortcutBitmap, _originalBitmap = null;
private Profile _profileToUse = null;
private uint _id = 0;
private string _profileName = "";
private bool _isPossible = false;
@ -52,7 +53,19 @@ namespace HeliosPlus
public static Version Version = new Version(1, 0);
public uint Id { get; set; } = 0;
public uint Id
{
get
{
if (_id == 0)
_id = ShortcutRepository.GetNextAvailableShortcutId();
return _id;
}
set
{
_id = value;
}
}
public string Name { get; set; } = "";
@ -146,7 +159,7 @@ namespace HeliosPlus
if (OriginalBitmap == null)
return null;
_shortcutBitmap = new ProfileIcon(ProfileToUse).ToBitmapOverly(OriginalBitmap);
_shortcutBitmap = new ProfileIcon(ProfileToUse).ToBitmapOverlay(OriginalBitmap,128 ,128);
return _shortcutBitmap;
}
}
@ -157,34 +170,9 @@ namespace HeliosPlus
}
}
[JsonIgnore]
public static string SavedShortcutsFilePath
{
get => Path.Combine(Program.AppDataPath, $"Shortcuts\\Shortcuts_{Version.ToString(2)}.json");
}
[JsonIgnore]
public static string SavedShortcutsPath
{
get => Path.Combine(Program.AppDataPath, $"Shortcuts");
}
public string SavedShortcutIconCacheFilename { get; set; }
[JsonIgnore]
public static List<Shortcut> AllSavedShortcuts
{
get
{
if (_allSavedShortcuts.Count == 0)
{
Shortcut.LoadAllShortcuts();
}
return _allSavedShortcuts;
}
}
[JsonIgnore]
public bool IsPossible
{
@ -195,9 +183,46 @@ namespace HeliosPlus
set
{
_isPossible = value;
}
}
}
public bool CopyTo (Shortcut shortcut, bool overwriteId = false)
{
if (!(shortcut is Shortcut))
return false;
if (overwriteId)
shortcut.Id = Id;
// Copy all the shortcut data over to the other Shortcut
shortcut.Name = Name;
shortcut.ProfileToUse = ProfileToUse;
shortcut.ProfileName = ProfileName;
shortcut.Permanence = Permanence;
shortcut.Category = Category;
shortcut.DifferentExecutableToMonitor = DifferentExecutableToMonitor;
shortcut.ExecutableNameAndPath = ExecutableNameAndPath;
shortcut.ExecutableTimeout = ExecutableTimeout;
shortcut.ExecutableArguments = ExecutableArguments;
shortcut.ExecutableArgumentsRequired = ExecutableArgumentsRequired;
shortcut.ProcessNameToMonitorUsesExecutable = ProcessNameToMonitorUsesExecutable;
shortcut.GameAppId = GameAppId;
shortcut.GameName = GameName;
shortcut.GameLibrary = GameLibrary;
shortcut.GameTimeout = GameTimeout;
shortcut.GameArguments = GameArguments;
shortcut.GameArgumentsRequired = GameArgumentsRequired;
shortcut.OriginalIconPath = OriginalIconPath;
shortcut.OriginalBitmap = OriginalBitmap;
shortcut.ShortcutBitmap = ShortcutBitmap;
shortcut.SavedShortcutIconCacheFilename = SavedShortcutIconCacheFilename;
shortcut.IsPossible = IsPossible;
return true;
}
public static Bitmap ExtractVistaIcon(Icon icoIcon)
{
Bitmap bmpPngExtracted = null;
@ -231,193 +256,6 @@ namespace HeliosPlus
return bmpPngExtracted;
}
public void SaveShortcutIconToCache()
{
if (_shortcutIcon == null)
{
if (!Directory.Exists(SavedShortcutsPath))
{
try
{
Directory.CreateDirectory(SavedShortcutsPath);
}
catch (Exception ex)
{
Console.WriteLine("Unable to create Shortcut folder " + SavedShortcutsPath + ": " + ex.Message);
}
}
// Only add the rest of the options if the permanence is temporary
if (Permanence == ShortcutPermanence.Temporary)
{
// Only add this set of options if the shortcut is to an standalone application
if (Category == ShortcutCategory.Application)
{
// Work out the name of the shortcut we'll save.
SavedShortcutIconCacheFilename = Path.Combine(SavedShortcutsPath, String.Concat(@"executable-", Program.GetValidFilename(Name).ToLower(CultureInfo.InvariantCulture), "-", Path.GetFileNameWithoutExtension(ExecutableNameAndPath), @".ico"));
}
// Only add the rest of the options if the temporary switch radio button is set
// and if the game launching radio button is set
else if (Permanence == ShortcutPermanence.Temporary)
{
// TODO need to make this work so at least one game library is installed
// i.e. if (!SteamGame.SteamInstalled && !UplayGame.UplayInstalled )
if (GameLibrary == SupportedGameLibrary.Steam)
{
// Work out the name of the shortcut we'll save.
SavedShortcutIconCacheFilename = Path.Combine(SavedShortcutsPath, String.Concat(@"steam-", Program.GetValidFilename(Name).ToLower(CultureInfo.InvariantCulture), "-", GameAppId.ToString(), @".ico"));
}
else if (GameLibrary == SupportedGameLibrary.Uplay)
{
// Work out the name of the shortcut we'll save.
SavedShortcutIconCacheFilename = Path.Combine(SavedShortcutsPath, String.Concat(@"uplay-", Program.GetValidFilename(Name).ToLower(CultureInfo.InvariantCulture), "-", GameAppId.ToString(), @".ico"));
}
}
}
// Only add the rest of the options if the shortcut is permanent
else
{
// Work out the name of the shortcut we'll save.
SavedShortcutIconCacheFilename = Path.Combine(SavedShortcutsPath, String.Concat(@"permanent-", Program.GetValidFilename(Name).ToLower(CultureInfo.InvariantCulture), @".ico"));
}
try
{
_shortcutIcon = new ProfileIcon(ProfileToUse).ToIconOverly(OriginalIconPath);
_shortcutIcon.Save(SavedShortcutIconCacheFilename, MultiIconFormat.ICO);
}
catch (Exception ex)
{
// If we fail to create an icon based on the original executable or game
// Then we use the standard HeliosPlus profile one.
_shortcutIcon = new ProfileIcon(ProfileToUse).ToIcon();
_shortcutIcon.Save(SavedShortcutIconCacheFilename, MultiIconFormat.ICO);
}
}
}
public static List<Shortcut> LoadAllShortcuts()
{
if (File.Exists(SavedShortcutsFilePath))
{
var json = File.ReadAllText(SavedShortcutsFilePath, Encoding.Unicode);
if (!string.IsNullOrWhiteSpace(json))
{
List<Shortcut> shortcuts = new List<Shortcut>();
try
{
shortcuts = JsonConvert.DeserializeObject<List<Shortcut>>(json, new JsonSerializerSettings
{
MissingMemberHandling = MissingMemberHandling.Ignore,
NullValueHandling = NullValueHandling.Ignore,
DefaultValueHandling = DefaultValueHandling.Include,
TypeNameHandling = TypeNameHandling.Auto
});
}
catch (Exception ex)
{
// ignored
Console.WriteLine("Unable to deserialize shortcut: " + ex.Message);
}
// Lookup all the Profile Names in the Saved Profiles
foreach (Shortcut updatedShortcut in shortcuts)
{
foreach (Profile profile in Profile.AllSavedProfiles)
{
if (profile.Name.Equals(updatedShortcut.ProfileName))
{
// And assign the matching Profile if we find it.
updatedShortcut.ProfileToUse = profile;
updatedShortcut.IsPossible = true;
break;
}
}
}
_allSavedShortcuts = shortcuts;
return _allSavedShortcuts;
}
}
// If we get here, then we don't have any shortcuts saved!
// So we gotta start from scratch
// Create a new empty list of all our display profiles as we don't have any saved!
_allSavedShortcuts = new List<Shortcut>();
return _allSavedShortcuts;
}
public static bool SaveAllShortcuts()
{
if (SaveAllShortcuts(_allSavedShortcuts))
return true;
return false;
}
public static bool SaveAllShortcuts(List<Shortcut> shortcutsToSave)
{
if (!Directory.Exists(SavedShortcutsPath))
{
try
{
Directory.CreateDirectory(SavedShortcutsPath);
}
catch (Exception ex)
{
Console.WriteLine("Unable to create Shortcut folder " + SavedShortcutsPath + ": " + ex.Message);
}
}
// Now we loop over the profiles and save their images for later
foreach (Shortcut shortcutToSave in shortcutsToSave)
shortcutToSave.SaveShortcutIconToCache();
try
{
var json = JsonConvert.SerializeObject(shortcutsToSave, Formatting.Indented, new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Include,
DefaultValueHandling = DefaultValueHandling.Populate,
TypeNameHandling = TypeNameHandling.Auto
});
if (!string.IsNullOrWhiteSpace(json))
{
var dir = Path.GetDirectoryName(SavedShortcutsPath);
if (dir != null)
{
Directory.CreateDirectory(dir);
File.WriteAllText(SavedShortcutsFilePath, json, Encoding.Unicode);
return true;
}
}
}
catch (Exception ex)
{
Console.WriteLine("Unable to serialize profile: " + ex.Message);
}
// Overwrite the list of saved profiles as the new lot we received.
_allSavedShortcuts = shortcutsToSave;
return false;
}
// ReSharper disable once FunctionComplexityOverflow
// ReSharper disable once CyclomaticComplexity
@ -519,14 +357,6 @@ namespace HeliosPlus
return shortcutFileName != null && File.Exists(shortcutFileName);
}
public static bool NameAlreadyExists(string shortcutName)
{
if (AllSavedShortcuts.Exists(item => item.Name.Equals(shortcutName)))
return true;
else
return false;
}
}
#region JsonConverterBitmap

View File

@ -0,0 +1,462 @@
using HeliosPlus.Shared;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Drawing.IconLib;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace HeliosPlus
{
class ShortcutRepository
{
#region Class Variables
// Common items to the class
private static List<Shortcut> _allShortcuts = new List<Shortcut>();
public static Version Version = new Version(1, 0, 0);
// Other constants that are useful
private static string _shortcutStorageJsonPath = Path.Combine(Program.AppDataPath, $"Shortcuts");
private static string _shortcutStorageJsonFileName = Path.Combine(_shortcutStorageJsonPath, $"Shortcuts_{Version.ToString(2)}.json");
private static uint _lastShortcutId;
#endregion
#region Instance Variables
// Individual items per class instance
#endregion
#region Class Constructors
public ShortcutRepository()
{
// Load the Shortcuts from storage
if (LoadShortcuts() && ShortcutCount > 0)
{
// Work out the starting NextShortcutId value
long max = _allShortcuts.Max<Shortcut>(item => item.Id);
_lastShortcutId = Convert.ToUInt32(max);
} else
_lastShortcutId = 0;
}
public ShortcutRepository(Shortcut shortcut) : this()
{
if (shortcut is Shortcut)
AddShortcut(shortcut);
}
#endregion
#region Class Properties
public static List<Shortcut> AllShortcuts
{
get
{
if (_allShortcuts == null)
// Load the Shortcuts from storage
if (LoadShortcuts() && ShortcutCount > 0)
{
// Work out the starting NextShortcutId value
long max = _allShortcuts.Max<Shortcut>(item => item.Id);
_lastShortcutId = Convert.ToUInt32(max);
}
else
_lastShortcutId = 0;
return _allShortcuts;
}
}
public static int ShortcutCount
{
get
{
return _allShortcuts.Count;
}
}
#endregion
#region Class Methods
public static bool AddShortcut(Shortcut shortcut)
{
if (!(shortcut is Shortcut))
return false;
// Doublecheck if it already exists
// Because then we just update the one that already exists
if (ContainsShortcut(shortcut))
{
// We update the existing Shortcut with the data over
Shortcut shortcutToUpdate = GetShortcut(shortcut.Id);
shortcut.CopyTo(shortcutToUpdate);
}
else
{
// Add the shortcut to the list of shortcuts
_allShortcuts.Add(shortcut);
}
//Doublecheck it's been added
if (ContainsShortcut(shortcut))
{
// Generate the Shortcut Icon ready to be used
SaveShortcutIconToCache(shortcut);
// Save the shortcuts JSON as it's different
SaveShortcuts();
return true;
}
else
return false;
}
public static bool RemoveShortcut(Shortcut shortcut)
{
if (!(shortcut is Shortcut))
return false;
// Remove the Shortcut Icons from the Cache
List<Shortcut> shortcutsToRemove = _allShortcuts.FindAll(item => item.Id.Equals(shortcut.Id));
foreach (Shortcut shortcutToRemove in shortcutsToRemove)
{
try
{
File.Delete(shortcutToRemove.SavedShortcutIconCacheFilename);
}
catch
{
// TODO check and report
}
}
// Remove the shortcut from the list.
int numRemoved = _allShortcuts.RemoveAll(item => item.Id.Equals(shortcut.Id));
if (numRemoved == 1)
{
SaveShortcuts();
return true;
}
else if (numRemoved == 0)
return false;
else
throw new ShortcutRepositoryException();
}
public static bool RemoveShortcut(string shortcutName)
{
if (String.IsNullOrWhiteSpace(shortcutName))
return false;
// Remove the Shortcut Icons from the Cache
List<Shortcut> shortcutsToRemove = _allShortcuts.FindAll(item => item.Name.Equals(shortcutName));
foreach (Shortcut shortcutToRemove in shortcutsToRemove)
{
try
{
File.Delete(shortcutToRemove.SavedShortcutIconCacheFilename);
}
catch
{
// TODO check and report
}
}
// Remove the shortcut from the list.
int numRemoved = _allShortcuts.RemoveAll(item => item.Name.Equals(shortcutName));
if (numRemoved == 1)
{
SaveShortcuts();
return true;
}
else if (numRemoved == 0)
return false;
else
throw new ShortcutRepositoryException();
}
public static bool RemoveShortcut(uint shortcutId)
{
if (shortcutId == 0)
return false;
// Remove the Shortcut Icons from the Cache
List<Shortcut> shortcutsToRemove = _allShortcuts.FindAll(item => item.Id.Equals(shortcutId));
foreach (Shortcut shortcutToRemove in shortcutsToRemove)
{
try
{
File.Delete(shortcutToRemove.SavedShortcutIconCacheFilename);
}
catch
{
// TODO check and report
}
}
// Remove the shortcut from the list.
int numRemoved = _allShortcuts.RemoveAll(item => item.Id.Equals(shortcutId));
if (numRemoved == 1)
{
SaveShortcuts();
return true;
}
else if (numRemoved == 0)
return false;
else
throw new ShortcutRepositoryException();
}
public static bool ContainsShortcut(Shortcut shortcut)
{
if (!(shortcut is Shortcut))
return false;
foreach (Shortcut testShortcut in _allShortcuts)
{
if (testShortcut.Id.Equals(shortcut.Id))
return true;
}
return false;
}
public static bool ContainsShortcut(string shortcutName)
{
if (String.IsNullOrWhiteSpace(shortcutName))
return false;
foreach (Shortcut testShortcut in _allShortcuts)
{
if (testShortcut.Name.Equals(shortcutName))
return true;
}
return false;
}
public static bool ContainsShortcut(uint shortcutId)
{
if (shortcutId == 0)
return true;
foreach (Shortcut testShortcut in _allShortcuts)
{
if (testShortcut.Id.Equals(shortcutId))
return true;
}
return false;
}
public static Shortcut GetShortcut(string shortcutName)
{
if (String.IsNullOrWhiteSpace(shortcutName))
return null;
foreach (Shortcut testShortcut in _allShortcuts)
{
if (testShortcut.Name.Equals(shortcutName))
return testShortcut;
}
return null;
}
public static Shortcut GetShortcut(uint shortcutId)
{
if (shortcutId == 0)
return null;
foreach (Shortcut testShortcut in _allShortcuts)
{
if (testShortcut.Id.Equals(shortcutId))
return testShortcut;
}
return null;
}
public static uint GetNextAvailableShortcutId()
{
return ++_lastShortcutId;
}
private static bool LoadShortcuts()
{
if (File.Exists(_shortcutStorageJsonFileName))
{
var json = File.ReadAllText(_shortcutStorageJsonFileName, Encoding.Unicode);
if (!string.IsNullOrWhiteSpace(json))
{
List<Shortcut> shortcuts = new List<Shortcut>();
try
{
_allShortcuts = JsonConvert.DeserializeObject<List<Shortcut>>(json, new JsonSerializerSettings
{
MissingMemberHandling = MissingMemberHandling.Ignore,
NullValueHandling = NullValueHandling.Ignore,
DefaultValueHandling = DefaultValueHandling.Include,
TypeNameHandling = TypeNameHandling.Auto
});
}
catch (Exception ex)
{
// ignored
Console.WriteLine($"Unable to load Shortcuts from JSON file {_shortcutStorageJsonFileName}: " + ex.Message);
}
// Lookup all the Profile Names in the Saved Profiles
foreach (Shortcut updatedShortcut in _allShortcuts)
{
foreach (Profile profile in Profile.AllSavedProfiles)
{
if (profile.Name.Equals(updatedShortcut.ProfileName))
{
// And assign the matching Profile if we find it.
updatedShortcut.ProfileToUse = profile;
updatedShortcut.IsPossible = true;
break;
}
}
}
}
}
return true;
}
private static bool SaveShortcuts()
{
if (!Directory.Exists(_shortcutStorageJsonPath))
{
try
{
Directory.CreateDirectory(_shortcutStorageJsonPath);
}
catch (Exception ex)
{
Console.WriteLine($"Unable to create Shortcut folder {_shortcutStorageJsonPath}: " + ex.Message);
}
}
try
{
var json = JsonConvert.SerializeObject(_allShortcuts, Formatting.Indented, new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Include,
DefaultValueHandling = DefaultValueHandling.Populate,
TypeNameHandling = TypeNameHandling.Auto
});
if (!string.IsNullOrWhiteSpace(json))
{
File.WriteAllText(_shortcutStorageJsonFileName, json, Encoding.Unicode);
return true;
}
}
catch (Exception ex)
{
Console.WriteLine($"Unable to save Shortcut JSON file {_shortcutStorageJsonFileName}: " + ex.Message);
}
return false;
}
private static void SaveShortcutIconToCache(Shortcut shortcut)
{
// Only add the rest of the options if the permanence is temporary
if (shortcut.Permanence == ShortcutPermanence.Temporary)
{
// Only add this set of options if the shortcut is to an standalone application
if (shortcut.Category == ShortcutCategory.Application)
{
// Work out the name of the shortcut we'll save.
shortcut.SavedShortcutIconCacheFilename = Path.Combine(_shortcutStorageJsonPath, String.Concat(@"executable-", Program.GetValidFilename(shortcut.Name).ToLower(CultureInfo.InvariantCulture), "-", Path.GetFileNameWithoutExtension(shortcut.ExecutableNameAndPath), @".ico"));
}
// Only add the rest of the options if the temporary switch radio button is set
// and if the game launching radio button is set
else if (shortcut.Permanence == ShortcutPermanence.Temporary)
{
// TODO need to make this work so at least one game library is installed
// i.e. if (!SteamGame.SteamInstalled && !UplayGame.UplayInstalled )
if (shortcut.GameLibrary == SupportedGameLibrary.Steam)
{
// Work out the name of the shortcut we'll save.
shortcut.SavedShortcutIconCacheFilename = Path.Combine(_shortcutStorageJsonPath, String.Concat(@"steam-", Program.GetValidFilename(shortcut.Name).ToLower(CultureInfo.InvariantCulture), "-", shortcut.GameAppId.ToString(), @".ico"));
}
else if (shortcut.GameLibrary == SupportedGameLibrary.Uplay)
{
// Work out the name of the shortcut we'll save.
shortcut.SavedShortcutIconCacheFilename = Path.Combine(_shortcutStorageJsonPath, String.Concat(@"uplay-", Program.GetValidFilename(shortcut.Name).ToLower(CultureInfo.InvariantCulture), "-", shortcut.GameAppId.ToString(), @".ico"));
}
}
}
// Only add the rest of the options if the shortcut is permanent
else
{
// Work out the name of the shortcut we'll save.
shortcut.SavedShortcutIconCacheFilename = Path.Combine(_shortcutStorageJsonPath, String.Concat(@"permanent-", Program.GetValidFilename(shortcut.Name).ToLower(CultureInfo.InvariantCulture), @".ico"));
}
MultiIcon shortcutIcon;
try
{
shortcutIcon = new ProfileIcon(shortcut.ProfileToUse).ToIconOverlay(shortcut.OriginalIconPath);
shortcutIcon.Save(shortcut.SavedShortcutIconCacheFilename, MultiIconFormat.ICO);
}
catch (Exception ex)
{
// If we fail to create an icon based on the original executable or game
// Then we use the standard HeliosPlus profile one.
shortcutIcon = new ProfileIcon(shortcut.ProfileToUse).ToIcon();
shortcutIcon.Save(shortcut.SavedShortcutIconCacheFilename, MultiIconFormat.ICO);
}
}
#endregion
}
[global::System.Serializable]
public class ShortcutRepositoryException : Exception
{
public ShortcutRepositoryException() { }
public ShortcutRepositoryException(string message) : base(message) { }
public ShortcutRepositoryException(string message, Exception inner) : base(message, inner) { }
protected ShortcutRepositoryException(
System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context) : base(info, context) { }
}
}

View File

@ -42,26 +42,16 @@ namespace HeliosPlus.UIForms
try
{
string shortcutName = key.ToString();
Shortcut shortcut = (Shortcut) key;
Shortcut shortcutToUse = null;
foreach (Shortcut profileToTest in Shortcut.AllSavedShortcuts)
{
if (profileToTest.Name == shortcutName)
{
shortcutToUse = profileToTest;
}
}
if (shortcutToUse == null)
if (shortcut == null)
{
return null;
}
Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(() => { return false; });
return shortcutToUse.ShortcutBitmap.GetThumbnailImage(size.Width, size.Height, myCallback, IntPtr.Zero);
//Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(() => { return false; });
//return shortcut.ShortcutBitmap.GetThumbnailImage(size.Width, size.Height, myCallback, IntPtr.Zero);
return shortcut.ShortcutBitmap;
}
catch {
// If we have a problem with converting the submitted key to a profile
@ -89,12 +79,12 @@ namespace HeliosPlus.UIForms
try
{
string shortcutName = (string)key;
return shortcutName;
Shortcut shortcutName = (Shortcut) key;
return shortcutName.Name;
}
catch
{
// If we have a problem with converting the submitted key to a profile
// If we have a problem with converting the submitted key to a Shortcut
// Then we return null
return null;
}
@ -145,31 +135,21 @@ namespace HeliosPlus.UIForms
try
{
Shortcut shortcut = (Shortcut)key;
Shortcut shortcutToUse = null;
foreach (Shortcut shortcutToTest in Shortcut.AllSavedShortcuts)
{
if (shortcutToTest.Name == shortcut.Name)
{
shortcutToUse = shortcutToTest;
}
}
Shortcut shortcut = (Shortcut) key;
// Get file info
if (shortcutToUse.ShortcutBitmap is Bitmap)
if (shortcut.ShortcutBitmap is Bitmap)
{
DateTime now = DateTime.Now;
details.Add(new Utility.Tuple<ColumnType, string, object>(ColumnType.DateCreated, string.Empty, now));
details.Add(new Utility.Tuple<ColumnType, string, object>(ColumnType.DateAccessed, string.Empty, now));
details.Add(new Utility.Tuple<ColumnType, string, object>(ColumnType.DateModified, string.Empty, now));
details.Add(new Utility.Tuple<ColumnType, string, object>(ColumnType.FileSize, string.Empty, (long)0));
details.Add(new Utility.Tuple<ColumnType, string, object>(ColumnType.FilePath, string.Empty, shortcutToUse.SavedShortcutIconCacheFilename));
details.Add(new Utility.Tuple<ColumnType, string, object>(ColumnType.FilePath, string.Empty, shortcut.SavedShortcutIconCacheFilename));
details.Add(new Utility.Tuple<ColumnType, string, object>(ColumnType.FolderName, string.Empty, ""));
details.Add(new Utility.Tuple<ColumnType, string, object>(ColumnType.Dimensions, string.Empty, new Size(shortcutToUse.ShortcutBitmap.Width, shortcutToUse.ShortcutBitmap.Height)));
details.Add(new Utility.Tuple<ColumnType, string, object>(ColumnType.Resolution, string.Empty, new SizeF((float)shortcutToUse.ShortcutBitmap.Width, (float)shortcutToUse.ShortcutBitmap.Height)));
details.Add(new Utility.Tuple<ColumnType, string, object>(ColumnType.ImageDescription, string.Empty, shortcutToUse.Name));
details.Add(new Utility.Tuple<ColumnType, string, object>(ColumnType.Dimensions, string.Empty, new Size(shortcut.ShortcutBitmap.Width, shortcut.ShortcutBitmap.Height)));
details.Add(new Utility.Tuple<ColumnType, string, object>(ColumnType.Resolution, string.Empty, new SizeF((float)shortcut.ShortcutBitmap.Width, (float)shortcut.ShortcutBitmap.Height)));
details.Add(new Utility.Tuple<ColumnType, string, object>(ColumnType.ImageDescription, string.Empty, shortcut.Name));
details.Add(new Utility.Tuple<ColumnType, string, object>(ColumnType.EquipmentModel, string.Empty, ""));
details.Add(new Utility.Tuple<ColumnType, string, object>(ColumnType.DateTaken, string.Empty, now));
details.Add(new Utility.Tuple<ColumnType, string, object>(ColumnType.Artist, string.Empty, ""));

View File

@ -258,7 +258,7 @@ namespace HeliosPlus.UIForms
}
// Please use a plain name that can be
if (_isNewShortcut && Shortcut.NameAlreadyExists(txt_shortcut_save_name.Text))
if (_isNewShortcut && ShortcutRepository.ContainsShortcut(txt_shortcut_save_name.Text))
{
MessageBox.Show(
@"A shortcut has already been created with this name. Please enter a different name for this shortcut.",
@ -444,16 +444,9 @@ namespace HeliosPlus.UIForms
else if (rb_standalone.Checked)
_shortcutToEdit.Category = ShortcutCategory.Application;
// Save the shortcut icon
_shortcutToEdit.SaveShortcutIconToCache();
// Add the Shortcut to the list of saved Shortcuts so it gets saved for later
// but only if it's new... if it is an edit then it will already be in the list.
if (_isNewShortcut)
Shortcut.AllSavedShortcuts.Add(_shortcutToEdit);
// Save all shortcuts just to be sure
Shortcut.SaveAllShortcuts();
ShortcutRepository.AddShortcut(_shortcutToEdit);
// We've saved, so mark it as so
_isUnsaved = false;
@ -604,7 +597,6 @@ namespace HeliosPlus.UIForms
matchingImageListViewItems.First().Selected = true;
matchingImageListViewItems.First().Focused = true;
}
}
private async void ShortcutForm_Load(object sender, EventArgs e)
@ -686,21 +678,7 @@ namespace HeliosPlus.UIForms
// Now start populating the other fields
if (_shortcutToEdit.Id == 0)
{
// This is a new Shortcut so we need to figure out what the next
// ID will need to be set to.
try
{
_id = (from shortcut in Shortcut.AllSavedShortcuts select shortcut.Id).Max<uint>() + 1;
}
catch
{
_id = 1;
}
}
else
_id = _shortcutToEdit.Id;
_id = _shortcutToEdit.Id;
// Set if we launch App/Game/NoGame
switch (_shortcutToEdit.Category)
{

View File

@ -75,6 +75,7 @@
this.btn_delete.TabIndex = 26;
this.btn_delete.Text = "&Delete";
this.btn_delete.UseVisualStyleBackColor = true;
this.btn_delete.Click += new System.EventHandler(this.btn_delete_Click);
//
// btn_back
//

View File

@ -23,6 +23,7 @@ namespace HeliosPlus.UIForms
private ShortcutAdaptor _shortcutAdaptor;
private ImageListViewItem _selectedShortcutILVItem = null;
private Shortcut _selectedShortcut = null;
private ShortcutRepository _shortcutRepository = new ShortcutRepository();
public ShortcutLibraryForm()
{
@ -49,7 +50,7 @@ namespace HeliosPlus.UIForms
private void ShortcutLibraryForm_Load(object sender, EventArgs e)
{
// Load all the shortcuts we have saved earlier
List<Shortcut> _savedShortcuts = Shortcut.LoadAllShortcuts();
List<Shortcut> _savedShortcuts = ShortcutRepository.AllShortcuts;
// Refresh the Shortcut Library UI
RefreshShortcutLibraryUI();
}
@ -57,21 +58,17 @@ namespace HeliosPlus.UIForms
private void RefreshShortcutLibraryUI()
{
if (Shortcut.AllSavedShortcuts.Count > 0)
if (ShortcutRepository.ShortcutCount > 0)
{
// Temporarily stop updating the saved_profiles listview
ilv_saved_shortcuts.SuspendLayout();
ImageListViewItem newItem = null;
foreach (Shortcut loadedShortcut in Shortcut.AllSavedShortcuts)
ilv_saved_shortcuts.Items.Clear();
foreach (Shortcut loadedShortcut in ShortcutRepository.AllShortcuts)
{
bool thisLoadedProfileIsAlreadyHere = (from item in ilv_saved_shortcuts.Items where item.Text == loadedShortcut.Name select item.Text).Any();
if (!thisLoadedProfileIsAlreadyHere)
{
newItem = new ImageListViewItem(loadedShortcut, loadedShortcut.Name);
//ilv_saved_profiles.Items.Add(newItem);
ilv_saved_shortcuts.Items.Add(newItem, _shortcutAdaptor);
}
newItem = new ImageListViewItem(loadedShortcut, loadedShortcut.Name);
ilv_saved_shortcuts.Items.Add(newItem, _shortcutAdaptor);
if (_selectedShortcut != null && _selectedShortcut is Shortcut)
RefreshImageListView(_selectedShortcut);
@ -103,7 +100,7 @@ namespace HeliosPlus.UIForms
private Shortcut GetShortcutFromName(string shortcutName)
{
return (from item in Shortcut.AllSavedShortcuts where item.Name == shortcutName select item).First();
return (from item in ShortcutRepository.AllShortcuts where item.Name == shortcutName select item).First();
}
private void btn_save_Click(object sender, EventArgs e)
@ -203,5 +200,19 @@ namespace HeliosPlus.UIForms
}
}
private void btn_delete_Click(object sender, EventArgs e)
{
if (_selectedShortcut == null)
return;
if (MessageBox.Show($"Are you sure you want to delete the '{_selectedShortcut.Name}' Shortcut?", $"Delete '{_selectedShortcut.Name}' Shortcut?", MessageBoxButtons.YesNo, MessageBoxIcon.Error) == DialogResult.No)
return;
ShortcutRepository.RemoveShortcut(_selectedShortcut);
_selectedShortcut = null;
RefreshShortcutLibraryUI();
}
}
}

View File

@ -66,7 +66,7 @@ namespace HeliosPlus
}
// Create an array of shortcuts we have
var shortcuts = Shortcut.LoadAllShortcuts().ToArray();
var shortcuts = ShortcutRepository.AllShortcuts.ToArray();
// Check if the user supplied a valid shortcut name
int profileIndex = shortcuts.Length > 0 ? Array.FindIndex(shortcuts, p => p.Name.Contains(shortcutName)) : -1;