From c67bfad28ae420e0735e99f3c1759b1282916eeb Mon Sep 17 00:00:00 2001 From: Jaex Date: Mon, 12 Oct 2015 11:09:02 +0300 Subject: [PATCH] Check shortcut target path too --- ShareX.HelpersLib/Helpers/ShortcutHelpers.cs | 76 ++++++++++++++----- ShareX.HelpersLib/ShareX.HelpersLib.csproj | 9 +++ ShareX.UploadersLib/FileUploaders/Pomf.cs | 3 +- .../Properties/Resources.Designer.cs | 10 +++ ShareX.UploadersLib/Properties/Resources.resx | 3 + ShareX/IntegrationHelpers.cs | 31 ++++---- 6 files changed, 97 insertions(+), 35 deletions(-) diff --git a/ShareX.HelpersLib/Helpers/ShortcutHelpers.cs b/ShareX.HelpersLib/Helpers/ShortcutHelpers.cs index f0bd7ddcb..9f4fb0250 100644 --- a/ShareX.HelpersLib/Helpers/ShortcutHelpers.cs +++ b/ShareX.HelpersLib/Helpers/ShortcutHelpers.cs @@ -24,6 +24,7 @@ You should have received a copy of the GNU General Public License #endregion License Information (GPL v3) using IWshRuntimeLibrary; +using Shell32; using System; using System.IO; using File = System.IO.File; @@ -32,7 +33,40 @@ namespace ShareX.HelpersLib { public static class ShortcutHelpers { - public static bool Create(string shortcutPath, string targetPath, string arguments = "") + public static bool SetShortcut(bool create, Environment.SpecialFolder specialFolder, string targetPath = "", string arguments = "") + { + string shortcutPath = GetShortcutPath(specialFolder); + return SetShortcut(create, shortcutPath, targetPath, arguments); + } + + public static bool SetShortcut(bool create, string shortcutPath, string targetPath = "", string arguments = "") + { + if (create) + { + return Create(shortcutPath, targetPath, arguments); + } + + return Delete(shortcutPath); + } + + public static bool CheckShortcut(Environment.SpecialFolder specialFolder, string checkPath) + { + string shortcutPath = GetShortcutPath(specialFolder); + return CheckShortcut(shortcutPath, checkPath); + } + + public static bool CheckShortcut(string shortcutPath, string targetPath) + { + if (!string.IsNullOrEmpty(shortcutPath) && !string.IsNullOrEmpty(targetPath) && File.Exists(shortcutPath)) + { + string checkPath = GetShortcutTargetPath(shortcutPath); + return !string.IsNullOrEmpty(checkPath) && checkPath.Equals(targetPath, StringComparison.InvariantCultureIgnoreCase); + } + + return false; + } + + private static bool Create(string shortcutPath, string targetPath, string arguments = "") { if (!string.IsNullOrEmpty(shortcutPath) && !string.IsNullOrEmpty(targetPath) && File.Exists(targetPath)) { @@ -56,7 +90,7 @@ public static bool Create(string shortcutPath, string targetPath, string argumen return false; } - public static bool Delete(string shortcutPath) + private static bool Delete(string shortcutPath) { if (!string.IsNullOrEmpty(shortcutPath) && File.Exists(shortcutPath)) { @@ -67,35 +101,35 @@ public static bool Delete(string shortcutPath) return false; } - public static bool SetShortcut(bool create, Environment.SpecialFolder specialFolder, string filepath = "", string arguments = "") + private static string GetShortcutTargetPath(string shortcutPath) { - string shortcutPath = GetShortcutPath(specialFolder); + string directory = Path.GetDirectoryName(shortcutPath); + string filename = Path.GetFileName(shortcutPath); - if (create) + try { - return Create(shortcutPath, filepath, arguments); + Shell shell = new ShellClass(); + Shell32.Folder folder = shell.NameSpace(directory); + FolderItem folderItem = folder.ParseName(filename); + + if (folderItem != null) + { + ShellLinkObject link = (ShellLinkObject)folderItem.GetLink; + return link.Path; + } + } + catch (Exception e) + { + DebugHelper.WriteException(e); } - return Delete(shortcutPath); - } - - public static bool CheckShortcut(Environment.SpecialFolder specialFolder) - { - string shortcutPath = GetShortcutPath(specialFolder); - return File.Exists(shortcutPath); + return null; } private static string GetShortcutPath(Environment.SpecialFolder specialFolder) { string folderPath = Environment.GetFolderPath(specialFolder); - string shortcutPath = Path.Combine(folderPath, "ShareX"); - - if (!Path.GetExtension(shortcutPath).Equals(".lnk", StringComparison.InvariantCultureIgnoreCase)) - { - shortcutPath = Path.ChangeExtension(shortcutPath, "lnk"); - } - - return shortcutPath; + return Path.Combine(folderPath, "ShareX.lnk"); } } } \ No newline at end of file diff --git a/ShareX.HelpersLib/ShareX.HelpersLib.csproj b/ShareX.HelpersLib/ShareX.HelpersLib.csproj index 4f21dec8f..7c857b27a 100644 --- a/ShareX.HelpersLib/ShareX.HelpersLib.csproj +++ b/ShareX.HelpersLib/ShareX.HelpersLib.csproj @@ -993,6 +993,15 @@ False False + + {50A7E9B0-70EF-11D1-B75A-00A0C90564FE} + 1 + 0 + 0 + tlbimp + False + False + diff --git a/ShareX.UploadersLib/FileUploaders/Pomf.cs b/ShareX.UploadersLib/FileUploaders/Pomf.cs index 57e2d2e84..5d0ef147e 100644 --- a/ShareX.UploadersLib/FileUploaders/Pomf.cs +++ b/ShareX.UploadersLib/FileUploaders/Pomf.cs @@ -25,6 +25,7 @@ You should have received a copy of the GNU General Public License using Newtonsoft.Json; using ShareX.HelpersLib; +using ShareX.UploadersLib.Properties; using System; using System.Collections.Generic; using System.IO; @@ -73,7 +74,7 @@ public override UploadResult Upload(Stream stream, string fileName) { if (Uploader == null || string.IsNullOrEmpty(Uploader.UploadURL)) { - Errors.Add("Please select one of Pomf uploaders from \"Destination settings window -> Pomf tab\"."); + Errors.Add(Resources.Pomf_Upload_Please_select_one_of_the_Pomf_uploaders_from__Destination_settings_window____Pomf_tab__); return null; } diff --git a/ShareX.UploadersLib/Properties/Resources.Designer.cs b/ShareX.UploadersLib/Properties/Resources.Designer.cs index 840fe6da9..c6a0f1b55 100644 --- a/ShareX.UploadersLib/Properties/Resources.Designer.cs +++ b/ShareX.UploadersLib/Properties/Resources.Designer.cs @@ -846,6 +846,16 @@ internal static System.Drawing.Icon Pomf { } } + /// + /// Looks up a localized string similar to Please select one of the Pomf uploaders from "Destination settings window -> Pomf tab".. + /// + internal static string Pomf_Upload_Please_select_one_of_the_Pomf_uploaders_from__Destination_settings_window____Pomf_tab__ { + get { + return ResourceManager.GetString("Pomf_Upload_Please_select_one_of_the_Pomf_uploaders_from__Destination_settings_wi" + + "ndow____Pomf_tab__", resourceCulture); + } + } + /// /// Looks up a localized resource of type System.Drawing.Icon similar to (Icon). /// diff --git a/ShareX.UploadersLib/Properties/Resources.resx b/ShareX.UploadersLib/Properties/Resources.resx index 1e88826c5..c99c13c1b 100644 --- a/ShareX.UploadersLib/Properties/Resources.resx +++ b/ShareX.UploadersLib/Properties/Resources.resx @@ -476,4 +476,7 @@ Created folders: ..\Favicons\Pomf.ico;System.Drawing.Icon, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a + + Please select one of the Pomf uploaders from "Destination settings window -> Pomf tab". + \ No newline at end of file diff --git a/ShareX/IntegrationHelpers.cs b/ShareX/IntegrationHelpers.cs index c824793ec..8d5a8d0b2 100644 --- a/ShareX/IntegrationHelpers.cs +++ b/ShareX/IntegrationHelpers.cs @@ -33,24 +33,29 @@ namespace ShareX { public static class IntegrationHelpers { + private static string GetStartupTargetPath() + { + string path; + +#if STEAM + path = Helpers.GetAbsolutePath("../ShareX_Launcher.exe"); +#else + path = Application.ExecutablePath; +#endif + + return path; + } + public static bool CheckStartupShortcut() { - return ShortcutHelpers.CheckShortcut(Environment.SpecialFolder.Startup); //RegistryHelper.CheckStartWithWindows(); + string targetPath = GetStartupTargetPath(); + return ShortcutHelpers.CheckShortcut(Environment.SpecialFolder.Startup, targetPath); } public static void CreateStartupShortcut(bool create) { - //RegistryHelper.SetStartWithWindows(cbStartWithWindows.Checked); - - string filePath; - -#if STEAM - filePath = Helpers.GetAbsolutePath("../ShareX_Launcher.exe"); -#else - filePath = Application.ExecutablePath; -#endif - - ShortcutHelpers.SetShortcut(create, Environment.SpecialFolder.Startup, filePath, "-silent"); + string targetPath = GetStartupTargetPath(); + ShortcutHelpers.SetShortcut(create, Environment.SpecialFolder.Startup, targetPath, "-silent"); } public static bool CheckShellContextMenuButton() @@ -65,7 +70,7 @@ public static void CreateShellContextMenuButton(bool create) public static bool CheckSendToMenuButton() { - return ShortcutHelpers.CheckShortcut(Environment.SpecialFolder.SendTo); + return ShortcutHelpers.CheckShortcut(Environment.SpecialFolder.SendTo, Application.ExecutablePath); } public static void CreateSendToMenuButton(bool create)