Skip to content

Commit bd48ee5

Browse files
committed
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
1 parent dbfa2bd commit bd48ee5

File tree

3 files changed

+40
-33
lines changed

3 files changed

+40
-33
lines changed

Plugins/Flow.Launcher.Plugin.WebSearch/Languages/en.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
<system:String x:Key="flowlauncher_plugin_websearch_input_url">Please enter a URL</system:String>
2626
<system:String x:Key="flowlauncher_plugin_websearch_action_keyword_exist">Action keyword already exists, please enter a different one</system:String>
2727
<system:String x:Key="flowlauncher_plugin_websearch_succeed">Success</system:String>
28+
<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>
2829

2930
<system:String x:Key="flowlauncher_plugin_websearch_plugin_name">Web Searches</system:String>
3031
<system:String x:Key="flowlauncher_plugin_websearch_plugin_description">Allows to perform web searches</system:String>

Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceSetting.xaml.cs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ private void Initilize(IList<SearchSource> sources, PluginInitContext context, A
3838
_action = action;
3939
_context = context;
4040
_api = _context.API;
41+
42+
_viewModel.SetupCustomImagesDirectory();
4143
}
4244

4345
private void OnCancelButtonClick(object sender, RoutedEventArgs e)
@@ -114,27 +116,23 @@ private void EditSearchSource()
114116

115117
private void OnSelectIconClick(object sender, RoutedEventArgs e)
116118
{
117-
var directory = Main.ImagesDirectory;
118119
const string filter = "Image files (*.jpg, *.jpeg, *.gif, *.png, *.bmp) |*.jpg; *.jpeg; *.gif; *.png; *.bmp";
119-
var dialog = new OpenFileDialog {InitialDirectory = directory, Filter = filter};
120+
var dialog = new OpenFileDialog {InitialDirectory = _viewModel.DestinationDirectory, Filter = filter};
120121

121122
var result = dialog.ShowDialog();
122123
if (result == true)
123124
{
124125
var fullpathToSelectedImage = dialog.FileName;
126+
127+
if (_viewModel.ShouldProvideHint(fullpathToSelectedImage))
128+
MessageBox.Show(_api.GetTranslation("flowlauncher_plugin_websearch_iconpath_hint"));
129+
125130
if (!string.IsNullOrEmpty(fullpathToSelectedImage))
126131
{
127-
if (!_viewModel.ImageFileExistsInLocation(fullpathToSelectedImage))
128-
{
129132
var fullPathToOriginalImage = _searchSource.IconPath;
130133
_viewModel.UpdateIconPath(_searchSource, fullpathToSelectedImage);
131-
_viewModel.CopyNewImageToUserDataDirectory(_searchSource, fullpathToSelectedImage, fullPathToOriginalImage);
132-
133-
return;
134-
}
135-
136-
MessageBox.Show($"An image of the same file name already exists in location {directory}. " +
137-
$"The icon image has not been updated");
134+
_viewModel.CopyNewImageToUserDataDirectoryIfRequired(
135+
_searchSource, fullpathToSelectedImage, fullPathToOriginalImage);
138136
}
139137
}
140138
}
Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,65 @@
1-
using Flow.Launcher.Infrastructure;
21
using Flow.Launcher.Infrastructure.UserSettings;
32
using System;
43
using System.IO;
5-
using System.Windows.Forms;
64

75
namespace Flow.Launcher.Plugin.WebSearch
86
{
97
public class SearchSourceViewModel : BaseModel
108
{
11-
private readonly string destinationDirectory =
12-
Path.Combine(DataLocation.DataDirectory(), @"Settings\Plugins\Flow.Launcher.Plugin.WebSearch\IconImages");
9+
internal readonly string DestinationDirectory =
10+
Path.Combine(DataLocation.DataDirectory(), @"Settings\Plugins\Flow.Launcher.Plugin.WebSearch\CustomIcons");
1311

1412
public SearchSource SearchSource { get; set; }
1513

1614
public void UpdateIconPath(SearchSource selectedSearchSource, string fullpathToSelectedImage)
1715
{
16+
var parentDirectorySelectedImg = Directory.GetParent(fullpathToSelectedImage).ToString();
17+
18+
var iconPathDirectory = parentDirectorySelectedImg == DestinationDirectory
19+
|| parentDirectorySelectedImg == Main.ImagesDirectory
20+
? parentDirectorySelectedImg : DestinationDirectory;
21+
1822
var iconFileName = Path.GetFileName(fullpathToSelectedImage);
1923
selectedSearchSource.Icon = iconFileName;
20-
selectedSearchSource.IconPath = Path.Combine(destinationDirectory, Path.GetFileName(fullpathToSelectedImage));
24+
selectedSearchSource.IconPath = Path.Combine(iconPathDirectory, Path.GetFileName(fullpathToSelectedImage));
2125
}
2226

23-
public void CopyNewImageToUserDataDirectory(SearchSource selectedSearchSource, string fullpathToSelectedImage, string fullPathToOriginalImage)
27+
public void CopyNewImageToUserDataDirectoryIfRequired(
28+
SearchSource selectedSearchSource, string fullpathToSelectedImage, string fullPathToOriginalImage)
2429
{
25-
var destinationFileNameFullPath = Path.Combine(destinationDirectory, Path.GetFileName(fullpathToSelectedImage));
26-
27-
try
28-
{
29-
if (!Directory.Exists(destinationDirectory))
30-
Directory.CreateDirectory(destinationDirectory);
30+
var destinationFileNameFullPath = Path.Combine(DestinationDirectory, Path.GetFileName(fullpathToSelectedImage));
3131

32-
File.Copy(fullpathToSelectedImage, destinationFileNameFullPath);
32+
var parentDirectorySelectedImg = Directory.GetParent(fullpathToSelectedImage).ToString();
3333

34-
selectedSearchSource.NotifyImageChange();
35-
}
36-
catch(Exception e)
34+
if (parentDirectorySelectedImg != DestinationDirectory && parentDirectorySelectedImg != Main.ImagesDirectory)
3735
{
36+
try
37+
{
38+
File.Copy(fullpathToSelectedImage, destinationFileNameFullPath);
39+
}
40+
catch (Exception e)
41+
{
3842
#if DEBUG
39-
throw e;
43+
throw e;
4044
#else
4145
MessageBox.Show(string.Format("Copying the selected image file to {0} has failed, changes will now be reverted", destinationFileNameFullPath));
4246
UpdateIconPath(selectedSearchSource, fullPathToOriginalImage);
4347
#endif
48+
}
4449
}
4550

51+
selectedSearchSource.NotifyImageChange();
4652
}
4753

48-
public bool ImageFileExistsInLocation(string fullpathToSelectedImage)
54+
internal void SetupCustomImagesDirectory()
4955
{
50-
var fileName = Path.GetFileName(fullpathToSelectedImage);
51-
52-
var newImageFilePathToBe = Path.Combine(destinationDirectory, fileName);
56+
if (!Directory.Exists(DestinationDirectory))
57+
Directory.CreateDirectory(DestinationDirectory);
58+
}
5359

54-
return File.Exists(newImageFilePathToBe);
60+
internal bool ShouldProvideHint(string fullPathToSelectedImage)
61+
{
62+
return Directory.GetParent(fullPathToSelectedImage).ToString() == Main.ImagesDirectory;
5563
}
5664
}
5765
}

0 commit comments

Comments
 (0)