add Search Source custom icon setting selection and saving

This commit is contained in:
Jeremy Wu 2020-07-22 08:18:36 +10:00
parent 91e7b1cfb8
commit 2c4eeb00bd
2 changed files with 60 additions and 8 deletions

View file

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

View file

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