Skip to content

Commit 2c4eeb0

Browse files
committed
add Search Source custom icon setting selection and saving
1 parent 91e7b1c commit 2c4eeb0

File tree

2 files changed

+60
-8
lines changed

2 files changed

+60
-8
lines changed

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Collections.Generic;
2-
using System.IO;
32
using System.Windows;
43
using Microsoft.Win32;
54
using Flow.Launcher.Core.Plugin;
@@ -122,15 +121,20 @@ private void OnSelectIconClick(object sender, RoutedEventArgs e)
122121
var result = dialog.ShowDialog();
123122
if (result == true)
124123
{
125-
var fullpath = dialog.FileName;
126-
if (!string.IsNullOrEmpty(fullpath))
124+
var fullpathToSelectedImage = dialog.FileName;
125+
if (!string.IsNullOrEmpty(fullpathToSelectedImage))
127126
{
128-
_searchSource.Icon = Path.GetFileName(fullpath);
129-
if (!File.Exists(_searchSource.IconPath))
127+
if (!_viewModel.ImageFileExistsInLocation(fullpathToSelectedImage))
130128
{
131-
_searchSource.Icon = SearchSource.DefaultIcon;
132-
MessageBox.Show($"The file should be put under {directory}");
129+
var fullPathToOriginalImage = _searchSource.IconPath;
130+
_viewModel.UpdateIconPath(_searchSource, fullpathToSelectedImage);
131+
_viewModel.CopyNewImageToUserDataDirectory(_searchSource, fullpathToSelectedImage, fullPathToOriginalImage);
132+
133+
return;
133134
}
135+
136+
MessageBox.Show($"An image of the same file name already exists in location {directory}. " +
137+
$"The icon image has not been updated");
134138
}
135139
}
136140
}
Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,55 @@
1-
namespace Flow.Launcher.Plugin.WebSearch
1+
using Flow.Launcher.Infrastructure;
2+
using Flow.Launcher.Infrastructure.UserSettings;
3+
using System;
4+
using System.IO;
5+
using System.Windows.Forms;
6+
7+
namespace Flow.Launcher.Plugin.WebSearch
28
{
39
public class SearchSourceViewModel
410
{
11+
private readonly string destinationDirectory =
12+
Path.Combine(DataLocation.DataDirectory(), @"Settings\Plugins\Flow.Launcher.Plugin.WebSearch\IconImages");
13+
514
public SearchSource SearchSource { get; set; }
15+
16+
public void UpdateIconPath(SearchSource selectedSearchSource, string fullpathToSelectedImage)
17+
{
18+
var iconFileName = Path.GetFileName(fullpathToSelectedImage);
19+
selectedSearchSource.Icon = iconFileName;
20+
selectedSearchSource.IconPath = Path.Combine(destinationDirectory, Path.GetFileName(fullpathToSelectedImage));
21+
}
22+
23+
public void CopyNewImageToUserDataDirectory(SearchSource selectedSearchSource, string fullpathToSelectedImage, string fullPathToOriginalImage)
24+
{
25+
var destinationFileNameFullPath = Path.Combine(destinationDirectory, Path.GetFileName(fullpathToSelectedImage));
26+
27+
try
28+
{
29+
if (!Directory.Exists(destinationDirectory))
30+
Directory.CreateDirectory(destinationDirectory);
31+
32+
File.Copy(fullpathToSelectedImage, destinationFileNameFullPath);
33+
}
34+
catch(Exception e)
35+
{
36+
#if DEBUG
37+
throw e;
38+
#else
39+
MessageBox.Show(string.Format("Copying the selected image file to {0} has failed, changes will now be reverted", destinationFileNameFullPath));
40+
UpdateIconPath(selectedSearchSource, fullPathToOriginalImage);
41+
#endif
42+
}
43+
44+
}
45+
46+
public bool ImageFileExistsInLocation(string fullpathToSelectedImage)
47+
{
48+
var fileName = Path.GetFileName(fullpathToSelectedImage);
49+
50+
var newImageFilePathToBe = Path.Combine(destinationDirectory, fileName);
51+
52+
return File.Exists(newImageFilePathToBe);
53+
}
654
}
755
}

0 commit comments

Comments
 (0)