From bd48ee5c90fade9dd555062dade9c82640dd34c8 Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Wed, 22 Jul 2020 22:00:14 +1000 Subject: [PATCH] update behaviour when to copy custom icons - copy images when not in the plugin's default images directory - do not copy if in the default directory or already in user data directory --- .../Languages/en.xaml | 1 + .../SearchSourceSetting.xaml.cs | 20 ++++--- .../SearchSourceViewModel.cs | 54 +++++++++++-------- 3 files changed, 41 insertions(+), 34 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.WebSearch/Languages/en.xaml b/Plugins/Flow.Launcher.Plugin.WebSearch/Languages/en.xaml index f1ae5bc35..c2ed7df31 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/Languages/en.xaml +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/Languages/en.xaml @@ -25,6 +25,7 @@ Please enter a URL Action keyword already exists, please enter a different one Success + Hint: You do not need to place custom images in this directory, if Flow's version is updated they will be lost. Flow will automatically copy any images outside of this directory across to WebSearch's custom image location. Web Searches Allows to perform web searches diff --git a/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceSetting.xaml.cs b/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceSetting.xaml.cs index ac1882cde..a7d001197 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceSetting.xaml.cs +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceSetting.xaml.cs @@ -38,6 +38,8 @@ namespace Flow.Launcher.Plugin.WebSearch _action = action; _context = context; _api = _context.API; + + _viewModel.SetupCustomImagesDirectory(); } private void OnCancelButtonClick(object sender, RoutedEventArgs e) @@ -114,27 +116,23 @@ namespace Flow.Launcher.Plugin.WebSearch private void OnSelectIconClick(object sender, RoutedEventArgs e) { - var directory = Main.ImagesDirectory; const string filter = "Image files (*.jpg, *.jpeg, *.gif, *.png, *.bmp) |*.jpg; *.jpeg; *.gif; *.png; *.bmp"; - var dialog = new OpenFileDialog {InitialDirectory = directory, Filter = filter}; + var dialog = new OpenFileDialog {InitialDirectory = _viewModel.DestinationDirectory, Filter = filter}; var result = dialog.ShowDialog(); if (result == true) { var fullpathToSelectedImage = dialog.FileName; + + if (_viewModel.ShouldProvideHint(fullpathToSelectedImage)) + MessageBox.Show(_api.GetTranslation("flowlauncher_plugin_websearch_iconpath_hint")); + if (!string.IsNullOrEmpty(fullpathToSelectedImage)) { - if (!_viewModel.ImageFileExistsInLocation(fullpathToSelectedImage)) - { var fullPathToOriginalImage = _searchSource.IconPath; _viewModel.UpdateIconPath(_searchSource, fullpathToSelectedImage); - _viewModel.CopyNewImageToUserDataDirectory(_searchSource, fullpathToSelectedImage, fullPathToOriginalImage); - - return; - } - - MessageBox.Show($"An image of the same file name already exists in location {directory}. " + - $"The icon image has not been updated"); + _viewModel.CopyNewImageToUserDataDirectoryIfRequired( + _searchSource, fullpathToSelectedImage, fullPathToOriginalImage); } } } diff --git a/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceViewModel.cs b/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceViewModel.cs index 2ca09d408..4c510bdc1 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceViewModel.cs +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceViewModel.cs @@ -1,57 +1,65 @@ -using Flow.Launcher.Infrastructure; using Flow.Launcher.Infrastructure.UserSettings; using System; using System.IO; -using System.Windows.Forms; namespace Flow.Launcher.Plugin.WebSearch { public class SearchSourceViewModel : BaseModel { - private readonly string destinationDirectory = - Path.Combine(DataLocation.DataDirectory(), @"Settings\Plugins\Flow.Launcher.Plugin.WebSearch\IconImages"); + internal readonly string DestinationDirectory = + Path.Combine(DataLocation.DataDirectory(), @"Settings\Plugins\Flow.Launcher.Plugin.WebSearch\CustomIcons"); public SearchSource SearchSource { get; set; } public void UpdateIconPath(SearchSource selectedSearchSource, string fullpathToSelectedImage) { + var parentDirectorySelectedImg = Directory.GetParent(fullpathToSelectedImage).ToString(); + + var iconPathDirectory = parentDirectorySelectedImg == DestinationDirectory + || parentDirectorySelectedImg == Main.ImagesDirectory + ? parentDirectorySelectedImg : DestinationDirectory; + var iconFileName = Path.GetFileName(fullpathToSelectedImage); selectedSearchSource.Icon = iconFileName; - selectedSearchSource.IconPath = Path.Combine(destinationDirectory, Path.GetFileName(fullpathToSelectedImage)); + selectedSearchSource.IconPath = Path.Combine(iconPathDirectory, Path.GetFileName(fullpathToSelectedImage)); } - public void CopyNewImageToUserDataDirectory(SearchSource selectedSearchSource, string fullpathToSelectedImage, string fullPathToOriginalImage) + public void CopyNewImageToUserDataDirectoryIfRequired( + SearchSource selectedSearchSource, string fullpathToSelectedImage, string fullPathToOriginalImage) { - var destinationFileNameFullPath = Path.Combine(destinationDirectory, Path.GetFileName(fullpathToSelectedImage)); + var destinationFileNameFullPath = Path.Combine(DestinationDirectory, Path.GetFileName(fullpathToSelectedImage)); - try - { - if (!Directory.Exists(destinationDirectory)) - Directory.CreateDirectory(destinationDirectory); - - File.Copy(fullpathToSelectedImage, destinationFileNameFullPath); - - selectedSearchSource.NotifyImageChange(); - } - catch(Exception e) + var parentDirectorySelectedImg = Directory.GetParent(fullpathToSelectedImage).ToString(); + + if (parentDirectorySelectedImg != DestinationDirectory && parentDirectorySelectedImg != Main.ImagesDirectory) { + try + { + File.Copy(fullpathToSelectedImage, destinationFileNameFullPath); + } + catch (Exception e) + { #if DEBUG - throw e; + throw e; #else MessageBox.Show(string.Format("Copying the selected image file to {0} has failed, changes will now be reverted", destinationFileNameFullPath)); UpdateIconPath(selectedSearchSource, fullPathToOriginalImage); #endif + } } + selectedSearchSource.NotifyImageChange(); } - public bool ImageFileExistsInLocation(string fullpathToSelectedImage) + internal void SetupCustomImagesDirectory() { - var fileName = Path.GetFileName(fullpathToSelectedImage); + if (!Directory.Exists(DestinationDirectory)) + Directory.CreateDirectory(DestinationDirectory); + } - var newImageFilePathToBe = Path.Combine(destinationDirectory, fileName); - - return File.Exists(newImageFilePathToBe); + internal bool ShouldProvideHint(string fullPathToSelectedImage) + { + return Directory.GetParent(fullPathToSelectedImage).ToString() == Main.ImagesDirectory; } } } \ No newline at end of file