From 2c4eeb00bd0bd996601808dc148b9c90bef6a831 Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Wed, 22 Jul 2020 08:18:36 +1000 Subject: [PATCH] add Search Source custom icon setting selection and saving --- .../SearchSourceSetting.xaml.cs | 18 ++++--- .../SearchSourceViewModel.cs | 50 ++++++++++++++++++- 2 files changed, 60 insertions(+), 8 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceSetting.xaml.cs b/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceSetting.xaml.cs index 9ecdda23f..ac1882cde 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceSetting.xaml.cs +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceSetting.xaml.cs @@ -1,5 +1,4 @@ using System.Collections.Generic; -using System.IO; using System.Windows; using Microsoft.Win32; using Flow.Launcher.Core.Plugin; @@ -122,15 +121,20 @@ namespace Flow.Launcher.Plugin.WebSearch var result = dialog.ShowDialog(); if (result == true) { - var fullpath = dialog.FileName; - if (!string.IsNullOrEmpty(fullpath)) + var fullpathToSelectedImage = dialog.FileName; + if (!string.IsNullOrEmpty(fullpathToSelectedImage)) { - _searchSource.Icon = Path.GetFileName(fullpath); - if (!File.Exists(_searchSource.IconPath)) + if (!_viewModel.ImageFileExistsInLocation(fullpathToSelectedImage)) { - _searchSource.Icon = SearchSource.DefaultIcon; - MessageBox.Show($"The file should be put under {directory}"); + 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"); } } } diff --git a/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceViewModel.cs b/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceViewModel.cs index 049c50560..5bcb3938d 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceViewModel.cs +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceViewModel.cs @@ -1,7 +1,55 @@ -namespace Flow.Launcher.Plugin.WebSearch +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 { + private readonly string destinationDirectory = + Path.Combine(DataLocation.DataDirectory(), @"Settings\Plugins\Flow.Launcher.Plugin.WebSearch\IconImages"); + public SearchSource SearchSource { get; set; } + + public void UpdateIconPath(SearchSource selectedSearchSource, string fullpathToSelectedImage) + { + var iconFileName = Path.GetFileName(fullpathToSelectedImage); + selectedSearchSource.Icon = iconFileName; + selectedSearchSource.IconPath = Path.Combine(destinationDirectory, Path.GetFileName(fullpathToSelectedImage)); + } + + public void CopyNewImageToUserDataDirectory(SearchSource selectedSearchSource, string fullpathToSelectedImage, string fullPathToOriginalImage) + { + var destinationFileNameFullPath = Path.Combine(destinationDirectory, Path.GetFileName(fullpathToSelectedImage)); + + try + { + if (!Directory.Exists(destinationDirectory)) + Directory.CreateDirectory(destinationDirectory); + + File.Copy(fullpathToSelectedImage, destinationFileNameFullPath); + } + catch(Exception e) + { +#if DEBUG + 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 + } + + } + + public bool ImageFileExistsInLocation(string fullpathToSelectedImage) + { + var fileName = Path.GetFileName(fullpathToSelectedImage); + + var newImageFilePathToBe = Path.Combine(destinationDirectory, fileName); + + return File.Exists(newImageFilePathToBe); + } } } \ No newline at end of file