Skip to content

Commit e4979ab

Browse files
authored
merge PR #127: update WebSearch plugin
fix location of custom icons
2 parents 3c85c03 + e3fe619 commit e4979ab

File tree

9 files changed

+122
-28
lines changed

9 files changed

+122
-28
lines changed

Flow.Launcher.Infrastructure/UserSettings/DataLocation.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,6 @@ public static bool PortableDataLocationInUse()
3030
}
3131

3232
public static readonly string PluginsDirectory = Path.Combine(DataDirectory(), Constant.Plugins);
33+
public static readonly string PluginSettingsDirectory = Path.Combine(DataDirectory(), "Settings", Constant.Plugins);
3334
}
3435
}

Flow.Launcher.Plugin/SharedCommands/FilesFolders.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ public static class FilesFolders
1111

1212
private const string FileExplorerProgramEXE = "explorer.exe";
1313

14+
/// <summary>
15+
/// Copies the folder and all of its files and folders
16+
/// including subfolders to the target location
17+
/// </summary>
18+
/// <param name="sourcePath"></param>
19+
/// <param name="targetPath"></param>
1420
public static void Copy(this string sourcePath, string targetPath)
1521
{
1622
// Get the subdirectories for the specified directory.
@@ -172,7 +178,7 @@ public static bool IsLocationPathString(string querySearchString)
172178
///<summary>
173179
/// Gets the previous level directory from a path string.
174180
/// Checks that previous level directory exists and returns it
175-
/// as a path string, or empty string if doesn't exit
181+
/// as a path string, or empty string if doesn't exist
176182
///</summary>
177183
public static string GetPreviousExistingDirectory(Func<string, bool> locationExists, string path)
178184
{

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/Main.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Windows.Controls;
99
using Flow.Launcher.Infrastructure;
1010
using Flow.Launcher.Infrastructure.Storage;
11+
using Flow.Launcher.Infrastructure.UserSettings;
1112
using Flow.Launcher.Plugin.SharedCommands;
1213

1314
namespace Flow.Launcher.Plugin.WebSearch
@@ -21,8 +22,9 @@ public class Main : IPlugin, ISettingProvider, IPluginI18n, ISavable, IResultUpd
2122
private CancellationTokenSource _updateSource;
2223
private CancellationToken _updateToken;
2324

24-
public const string Images = "Images";
25-
public static string ImagesDirectory;
25+
internal const string Images = "Images";
26+
internal static string DefaultImagesDirectory;
27+
internal static string CustomImagesDirectory;
2628

2729
private readonly string SearchSourceGlobalPluginWildCardSign = "*";
2830

@@ -172,8 +174,14 @@ public void Init(PluginInitContext context)
172174
_context = context;
173175
var pluginDirectory = _context.CurrentPluginMetadata.PluginDirectory;
174176
var bundledImagesDirectory = Path.Combine(pluginDirectory, Images);
175-
ImagesDirectory = Path.Combine(_context.CurrentPluginMetadata.PluginDirectory, Images);
176-
Helper.ValidateDataDirectory(bundledImagesDirectory, ImagesDirectory);
177+
178+
// Default images directory is in the WebSearch's application folder
179+
DefaultImagesDirectory = Path.Combine(pluginDirectory, Images);
180+
Helper.ValidateDataDirectory(bundledImagesDirectory, DefaultImagesDirectory);
181+
182+
// Custom images directory is in the WebSearch's data location folder
183+
var name = Path.GetFileNameWithoutExtension(_context.CurrentPluginMetadata.ExecuteFileName);
184+
CustomImagesDirectory = Path.Combine(DataLocation.PluginSettingsDirectory, name, "CustomIcons");
177185
}
178186

179187
#region ISettingProvider Members

Plugins/Flow.Launcher.Plugin.WebSearch/SearchSource.cs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,38 @@
1-
using System.IO;
1+
using System.IO;
22
using System.Windows.Media;
33
using JetBrains.Annotations;
44
using Newtonsoft.Json;
55
using Flow.Launcher.Infrastructure.Image;
6+
using Flow.Launcher.Infrastructure;
7+
using System.Reflection;
68

79
namespace Flow.Launcher.Plugin.WebSearch
810
{
911
public class SearchSource : BaseModel
1012
{
11-
public const string DefaultIcon = "web_search.png";
1213
public string Title { get; set; }
1314
public string ActionKeyword { get; set; }
1415

1516
[NotNull]
16-
public string Icon { get; set; } = DefaultIcon;
17+
public string Icon { get; set; } = "web_search.png";
18+
19+
public bool CustomIcon { get; set; } = false;
1720

1821
/// <summary>
19-
/// All icon should be put under Images directory
22+
/// Default icons are placed in Images directory in the app location.
23+
/// Custom icons are placed in the user data directory
2024
/// </summary>
21-
[NotNull]
2225
[JsonIgnore]
23-
internal string IconPath => Path.Combine(Main.ImagesDirectory, Icon);
26+
public string IconPath
27+
{
28+
get
29+
{
30+
if (CustomIcon)
31+
return Path.Combine(Main.CustomImagesDirectory, Icon);
2432

25-
[JsonIgnore]
26-
public ImageSource Image => ImageLoader.Load(IconPath);
33+
return Path.Combine(Main.DefaultImagesDirectory, Icon);
34+
}
35+
}
2736

2837
public string Url { get; set; }
2938
public bool Enabled { get; set; }
@@ -36,6 +45,7 @@ public SearchSource DeepCopy()
3645
ActionKeyword = string.Copy(ActionKeyword),
3746
Url = string.Copy(Url),
3847
Icon = string.Copy(Icon),
48+
CustomIcon = CustomIcon,
3949
Enabled = Enabled
4050
};
4151
return webSearch;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
<TextBlock Margin="10" FontSize="14" Grid.Row="4" Grid.Column="0" VerticalAlignment="Center"
4848
HorizontalAlignment="Right" Text="{DynamicResource flowlauncher_plugin_websearch_icon}" />
4949
<StackPanel Orientation="Horizontal" Grid.Row="4" Grid.Column="1" Margin="10">
50-
<Image Source="{Binding SearchSource.Image ,IsAsync=True}" Width="24" Height="24" Margin="0 0 20 0" />
50+
<Image Name="imgPreviewIcon" Width="24" Height="24" Margin="0 0 20 0" />
5151
<Button Click="OnSelectIconClick" Height="35"
5252
Content="{DynamicResource flowlauncher_plugin_websearch_select_icon}" />
5353
</StackPanel>

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

Lines changed: 21 additions & 11 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;
@@ -15,6 +14,7 @@ public partial class SearchSourceSettingWindow
1514
private PluginInitContext _context;
1615
private IPublicAPI _api;
1716
private SearchSourceViewModel _viewModel;
17+
private string selectedNewIconImageFullPath;
1818

1919

2020
public SearchSourceSettingWindow(IList<SearchSource> sources, PluginInitContext context, SearchSource old)
@@ -39,6 +39,10 @@ private void Initilize(IList<SearchSource> sources, PluginInitContext context, A
3939
_action = action;
4040
_context = context;
4141
_api = _context.API;
42+
43+
_viewModel.SetupCustomImagesDirectory();
44+
45+
imgPreviewIcon.Source = _viewModel.LoadPreviewIcon(_searchSource.IconPath);
4246
}
4347

4448
private void OnCancelButtonClick(object sender, RoutedEventArgs e)
@@ -111,26 +115,32 @@ private void EditSearchSource()
111115
var warning = _api.GetTranslation("newActionKeywordsHasBeenAssigned");
112116
MessageBox.Show(warning);
113117
}
118+
119+
if (!string.IsNullOrEmpty(selectedNewIconImageFullPath))
120+
{
121+
_viewModel.UpdateIconAttributes(_searchSource, selectedNewIconImageFullPath);
122+
123+
_viewModel.CopyNewImageToUserDataDirectoryIfRequired(
124+
_searchSource, selectedNewIconImageFullPath, _oldSearchSource.IconPath);
125+
}
114126
}
115127

116128
private void OnSelectIconClick(object sender, RoutedEventArgs e)
117129
{
118-
var directory = Main.ImagesDirectory;
119130
const string filter = "Image files (*.jpg, *.jpeg, *.gif, *.png, *.bmp) |*.jpg; *.jpeg; *.gif; *.png; *.bmp";
120-
var dialog = new OpenFileDialog {InitialDirectory = directory, Filter = filter};
131+
var dialog = new OpenFileDialog {InitialDirectory = Main.CustomImagesDirectory, Filter = filter};
121132

122133
var result = dialog.ShowDialog();
123134
if (result == true)
124135
{
125-
var fullpath = dialog.FileName;
126-
if (!string.IsNullOrEmpty(fullpath))
136+
selectedNewIconImageFullPath = dialog.FileName;
137+
138+
if (!string.IsNullOrEmpty(selectedNewIconImageFullPath))
127139
{
128-
_searchSource.Icon = Path.GetFileName(fullpath);
129-
if (!File.Exists(_searchSource.IconPath))
130-
{
131-
_searchSource.Icon = SearchSource.DefaultIcon;
132-
MessageBox.Show($"The file should be put under {directory}");
133-
}
140+
if (_viewModel.ShouldProvideHint(selectedNewIconImageFullPath))
141+
MessageBox.Show(_api.GetTranslation("flowlauncher_plugin_websearch_iconpath_hint"));
142+
143+
imgPreviewIcon.Source = _viewModel.LoadPreviewIcon(selectedNewIconImageFullPath);
134144
}
135145
}
136146
}
Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,65 @@
1-
namespace Flow.Launcher.Plugin.WebSearch
1+
using Flow.Launcher.Infrastructure.Image;
2+
using System;
3+
using System.Drawing;
4+
using System.IO;
5+
using System.Windows;
6+
using System.Windows.Media;
7+
8+
namespace Flow.Launcher.Plugin.WebSearch
29
{
3-
public class SearchSourceViewModel
10+
public class SearchSourceViewModel : BaseModel
411
{
512
public SearchSource SearchSource { get; set; }
13+
14+
public void UpdateIconAttributes(SearchSource selectedSearchSource, string fullpathToSelectedImage)
15+
{
16+
var parentDirectorySelectedImg = Directory.GetParent(fullpathToSelectedImage).ToString();
17+
18+
selectedSearchSource.CustomIcon = parentDirectorySelectedImg != Main.DefaultImagesDirectory;
19+
20+
var iconFileName = Path.GetFileName(fullpathToSelectedImage);
21+
selectedSearchSource.Icon = iconFileName;
22+
}
23+
24+
public void CopyNewImageToUserDataDirectoryIfRequired(
25+
SearchSource selectedSearchSource, string fullpathToSelectedImage, string fullPathToOriginalImage)
26+
{
27+
var destinationFileNameFullPath = Path.Combine(Main.CustomImagesDirectory, Path.GetFileName(fullpathToSelectedImage));
28+
29+
var parentDirectorySelectedImg = Directory.GetParent(fullpathToSelectedImage).ToString();
30+
31+
if (parentDirectorySelectedImg != Main.CustomImagesDirectory && parentDirectorySelectedImg != Main.DefaultImagesDirectory)
32+
{
33+
try
34+
{
35+
File.Copy(fullpathToSelectedImage, destinationFileNameFullPath);
36+
}
37+
catch (Exception e)
38+
{
39+
#if DEBUG
40+
throw e;
41+
#else
42+
MessageBox.Show(string.Format("Copying the selected image file to {0} has failed, changes will now be reverted", destinationFileNameFullPath));
43+
UpdateIconAttributes(selectedSearchSource, fullPathToOriginalImage);
44+
#endif
45+
}
46+
}
47+
}
48+
49+
internal void SetupCustomImagesDirectory()
50+
{
51+
if (!Directory.Exists(Main.CustomImagesDirectory))
52+
Directory.CreateDirectory(Main.CustomImagesDirectory);
53+
}
54+
55+
internal bool ShouldProvideHint(string fullPathToSelectedImage)
56+
{
57+
return Directory.GetParent(fullPathToSelectedImage).ToString() == Main.DefaultImagesDirectory;
58+
}
59+
60+
internal ImageSource LoadPreviewIcon(string pathToPreviewIconImage)
61+
{
62+
return ImageLoader.Load(pathToPreviewIconImage);
63+
}
664
}
765
}

Plugins/Flow.Launcher.Plugin.WebSearch/plugin.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"Name": "Web Searches",
2626
"Description": "Provide the web search ability",
2727
"Author": "qianlifeng",
28-
"Version": "1.0.0",
28+
"Version": "1.0.1",
2929
"Language": "csharp",
3030
"Website": "https://github.com/Flow-Launcher/Flow.Launcher",
3131
"ExecuteFileName": "Flow.Launcher.Plugin.WebSearch.dll",

0 commit comments

Comments
 (0)