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
This commit is contained in:
Jeremy Wu 2020-07-22 22:00:14 +10:00
parent dbfa2bd6c4
commit bd48ee5c90
3 changed files with 41 additions and 34 deletions

View file

@ -25,6 +25,7 @@
<system:String x:Key="flowlauncher_plugin_websearch_input_url">Please enter a URL</system:String>
<system:String x:Key="flowlauncher_plugin_websearch_action_keyword_exist">Action keyword already exists, please enter a different one</system:String>
<system:String x:Key="flowlauncher_plugin_websearch_succeed">Success</system:String>
<system:String x:Key="flowlauncher_plugin_websearch_iconpath_hint">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.</system:String>
<system:String x:Key="flowlauncher_plugin_websearch_plugin_name">Web Searches</system:String>
<system:String x:Key="flowlauncher_plugin_websearch_plugin_description">Allows to perform web searches</system:String>

View file

@ -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);
}
}
}

View file

@ -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;
}
}
}