Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Flow.Launcher.Infrastructure/UserSettings/DataLocation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ public static bool PortableDataLocationInUse()
}

public static readonly string PluginsDirectory = Path.Combine(DataDirectory(), Constant.Plugins);
public static readonly string PluginSettingsDirectory = Path.Combine(DataDirectory(), "Settings", Constant.Plugins);
}
}
8 changes: 7 additions & 1 deletion Flow.Launcher.Plugin/SharedCommands/FilesFolders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ public static class FilesFolders

private const string FileExplorerProgramEXE = "explorer.exe";

/// <summary>
/// Copies the folder and all of its files and folders
/// including subfolders to the target location
/// </summary>
/// <param name="sourcePath"></param>
/// <param name="targetPath"></param>
public static void Copy(this string sourcePath, string targetPath)
{
// Get the subdirectories for the specified directory.
Expand Down Expand Up @@ -172,7 +178,7 @@ public static bool IsLocationPathString(string querySearchString)
///<summary>
/// Gets the previous level directory from a path string.
/// Checks that previous level directory exists and returns it
/// as a path string, or empty string if doesn't exit
/// as a path string, or empty string if doesn't exist
///</summary>
public static string GetPreviousExistingDirectory(Func<string, bool> locationExists, string path)
{
Expand Down
1 change: 1 addition & 0 deletions Plugins/Flow.Launcher.Plugin.WebSearch/Languages/en.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
16 changes: 12 additions & 4 deletions Plugins/Flow.Launcher.Plugin.WebSearch/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Windows.Controls;
using Flow.Launcher.Infrastructure;
using Flow.Launcher.Infrastructure.Storage;
using Flow.Launcher.Infrastructure.UserSettings;
using Flow.Launcher.Plugin.SharedCommands;

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

public const string Images = "Images";
public static string ImagesDirectory;
internal const string Images = "Images";
internal static string DefaultImagesDirectory;
internal static string CustomImagesDirectory;

private readonly string SearchSourceGlobalPluginWildCardSign = "*";

Expand Down Expand Up @@ -172,8 +174,14 @@ public void Init(PluginInitContext context)
_context = context;
var pluginDirectory = _context.CurrentPluginMetadata.PluginDirectory;
var bundledImagesDirectory = Path.Combine(pluginDirectory, Images);
ImagesDirectory = Path.Combine(_context.CurrentPluginMetadata.PluginDirectory, Images);
Helper.ValidateDataDirectory(bundledImagesDirectory, ImagesDirectory);

// Default images directory is in the WebSearch's application folder
DefaultImagesDirectory = Path.Combine(pluginDirectory, Images);
Helper.ValidateDataDirectory(bundledImagesDirectory, DefaultImagesDirectory);

// Custom images directory is in the WebSearch's data location folder
var name = Path.GetFileNameWithoutExtension(_context.CurrentPluginMetadata.ExecuteFileName);
CustomImagesDirectory = Path.Combine(DataLocation.PluginSettingsDirectory, name, "CustomIcons");
}

#region ISettingProvider Members
Expand Down
26 changes: 18 additions & 8 deletions Plugins/Flow.Launcher.Plugin.WebSearch/SearchSource.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,38 @@
using System.IO;
using System.IO;
using System.Windows.Media;
using JetBrains.Annotations;
using Newtonsoft.Json;
using Flow.Launcher.Infrastructure.Image;
using Flow.Launcher.Infrastructure;
using System.Reflection;

namespace Flow.Launcher.Plugin.WebSearch
{
public class SearchSource : BaseModel
{
public const string DefaultIcon = "web_search.png";
public string Title { get; set; }
public string ActionKeyword { get; set; }

[NotNull]
public string Icon { get; set; } = DefaultIcon;
public string Icon { get; set; } = "web_search.png";

public bool CustomIcon { get; set; } = false;

/// <summary>
/// All icon should be put under Images directory
/// Default icons are placed in Images directory in the app location.
/// Custom icons are placed in the user data directory
/// </summary>
[NotNull]
[JsonIgnore]
internal string IconPath => Path.Combine(Main.ImagesDirectory, Icon);
public string IconPath
{
get
{
if (CustomIcon)
return Path.Combine(Main.CustomImagesDirectory, Icon);

[JsonIgnore]
public ImageSource Image => ImageLoader.Load(IconPath);
return Path.Combine(Main.DefaultImagesDirectory, Icon);
}
}

public string Url { get; set; }
public bool Enabled { get; set; }
Expand All @@ -36,6 +45,7 @@ public SearchSource DeepCopy()
ActionKeyword = string.Copy(ActionKeyword),
Url = string.Copy(Url),
Icon = string.Copy(Icon),
CustomIcon = CustomIcon,
Enabled = Enabled
};
return webSearch;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<TextBlock Margin="10" FontSize="14" Grid.Row="4" Grid.Column="0" VerticalAlignment="Center"
HorizontalAlignment="Right" Text="{DynamicResource flowlauncher_plugin_websearch_icon}" />
<StackPanel Orientation="Horizontal" Grid.Row="4" Grid.Column="1" Margin="10">
<Image Source="{Binding SearchSource.Image ,IsAsync=True}" Width="24" Height="24" Margin="0 0 20 0" />
<Image Name="imgPreviewIcon" Width="24" Height="24" Margin="0 0 20 0" />
<Button Click="OnSelectIconClick" Height="35"
Content="{DynamicResource flowlauncher_plugin_websearch_select_icon}" />
</StackPanel>
Expand Down
32 changes: 21 additions & 11 deletions Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceSetting.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.IO;
using System.Windows;
using Microsoft.Win32;
using Flow.Launcher.Core.Plugin;
Expand All @@ -15,6 +14,7 @@ public partial class SearchSourceSettingWindow
private PluginInitContext _context;
private IPublicAPI _api;
private SearchSourceViewModel _viewModel;
private string selectedNewIconImageFullPath;


public SearchSourceSettingWindow(IList<SearchSource> sources, PluginInitContext context, SearchSource old)
Expand All @@ -39,6 +39,10 @@ private void Initilize(IList<SearchSource> sources, PluginInitContext context, A
_action = action;
_context = context;
_api = _context.API;

_viewModel.SetupCustomImagesDirectory();

imgPreviewIcon.Source = _viewModel.LoadPreviewIcon(_searchSource.IconPath);
}

private void OnCancelButtonClick(object sender, RoutedEventArgs e)
Expand Down Expand Up @@ -111,26 +115,32 @@ private void EditSearchSource()
var warning = _api.GetTranslation("newActionKeywordsHasBeenAssigned");
MessageBox.Show(warning);
}

if (!string.IsNullOrEmpty(selectedNewIconImageFullPath))
{
_viewModel.UpdateIconAttributes(_searchSource, selectedNewIconImageFullPath);

_viewModel.CopyNewImageToUserDataDirectoryIfRequired(
_searchSource, selectedNewIconImageFullPath, _oldSearchSource.IconPath);
}
}

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 = Main.CustomImagesDirectory, Filter = filter};

var result = dialog.ShowDialog();
if (result == true)
{
var fullpath = dialog.FileName;
if (!string.IsNullOrEmpty(fullpath))
selectedNewIconImageFullPath = dialog.FileName;

if (!string.IsNullOrEmpty(selectedNewIconImageFullPath))
{
_searchSource.Icon = Path.GetFileName(fullpath);
if (!File.Exists(_searchSource.IconPath))
{
_searchSource.Icon = SearchSource.DefaultIcon;
MessageBox.Show($"The file should be put under {directory}");
}
if (_viewModel.ShouldProvideHint(selectedNewIconImageFullPath))
MessageBox.Show(_api.GetTranslation("flowlauncher_plugin_websearch_iconpath_hint"));

imgPreviewIcon.Source = _viewModel.LoadPreviewIcon(selectedNewIconImageFullPath);
}
}
}
Expand Down
62 changes: 60 additions & 2 deletions Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,65 @@
namespace Flow.Launcher.Plugin.WebSearch
using Flow.Launcher.Infrastructure.Image;
using System;
using System.Drawing;
using System.IO;
using System.Windows;
using System.Windows.Media;

namespace Flow.Launcher.Plugin.WebSearch
{
public class SearchSourceViewModel
public class SearchSourceViewModel : BaseModel
{
public SearchSource SearchSource { get; set; }

public void UpdateIconAttributes(SearchSource selectedSearchSource, string fullpathToSelectedImage)
{
var parentDirectorySelectedImg = Directory.GetParent(fullpathToSelectedImage).ToString();

selectedSearchSource.CustomIcon = parentDirectorySelectedImg != Main.DefaultImagesDirectory;

var iconFileName = Path.GetFileName(fullpathToSelectedImage);
selectedSearchSource.Icon = iconFileName;
}

public void CopyNewImageToUserDataDirectoryIfRequired(
SearchSource selectedSearchSource, string fullpathToSelectedImage, string fullPathToOriginalImage)
{
var destinationFileNameFullPath = Path.Combine(Main.CustomImagesDirectory, Path.GetFileName(fullpathToSelectedImage));

var parentDirectorySelectedImg = Directory.GetParent(fullpathToSelectedImage).ToString();

if (parentDirectorySelectedImg != Main.CustomImagesDirectory && parentDirectorySelectedImg != Main.DefaultImagesDirectory)
{
try
{
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));
UpdateIconAttributes(selectedSearchSource, fullPathToOriginalImage);
#endif
}
}
}

internal void SetupCustomImagesDirectory()
{
if (!Directory.Exists(Main.CustomImagesDirectory))
Directory.CreateDirectory(Main.CustomImagesDirectory);
}

internal bool ShouldProvideHint(string fullPathToSelectedImage)
{
return Directory.GetParent(fullPathToSelectedImage).ToString() == Main.DefaultImagesDirectory;
}

internal ImageSource LoadPreviewIcon(string pathToPreviewIconImage)
{
return ImageLoader.Load(pathToPreviewIconImage);
}
}
}
2 changes: 1 addition & 1 deletion Plugins/Flow.Launcher.Plugin.WebSearch/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"Name": "Web Searches",
"Description": "Provide the web search ability",
"Author": "qianlifeng",
"Version": "1.0.0",
"Version": "1.0.1",
"Language": "csharp",
"Website": "https://github.com/Flow-Launcher/Flow.Launcher",
"ExecuteFileName": "Flow.Launcher.Plugin.WebSearch.dll",
Expand Down