2016-06-20 23:14:32 +00:00
|
|
|
|
using System.Collections.Generic;
|
2025-04-11 04:23:17 +00:00
|
|
|
|
using System.Linq;
|
2014-01-28 03:19:27 +00:00
|
|
|
|
using System.Windows;
|
2025-04-11 04:23:17 +00:00
|
|
|
|
using System.Windows.Input;
|
2015-01-03 07:20:34 +00:00
|
|
|
|
using Microsoft.Win32;
|
2014-01-28 03:19:27 +00:00
|
|
|
|
|
2020-04-21 09:12:17 +00:00
|
|
|
|
namespace Flow.Launcher.Plugin.WebSearch
|
2014-01-28 03:19:27 +00:00
|
|
|
|
{
|
2016-06-20 23:14:32 +00:00
|
|
|
|
public partial class SearchSourceSettingWindow
|
2014-01-28 03:19:27 +00:00
|
|
|
|
{
|
2016-06-20 23:14:32 +00:00
|
|
|
|
private readonly SearchSource _oldSearchSource;
|
|
|
|
|
|
private SearchSource _searchSource;
|
|
|
|
|
|
private IList<SearchSource> _searchSources;
|
|
|
|
|
|
private Action _action;
|
|
|
|
|
|
private PluginInitContext _context;
|
|
|
|
|
|
private IPublicAPI _api;
|
|
|
|
|
|
private SearchSourceViewModel _viewModel;
|
2020-08-11 22:23:10 +00:00
|
|
|
|
private string selectedNewIconImageFullPath;
|
2016-06-20 23:14:32 +00:00
|
|
|
|
|
|
|
|
|
|
public SearchSourceSettingWindow(IList<SearchSource> sources, PluginInitContext context, SearchSource old)
|
2014-01-28 03:19:27 +00:00
|
|
|
|
{
|
2016-06-20 23:14:32 +00:00
|
|
|
|
_oldSearchSource = old;
|
2016-06-20 23:18:35 +00:00
|
|
|
|
_viewModel = new SearchSourceViewModel {SearchSource = old.DeepCopy()};
|
2022-12-31 15:43:07 +00:00
|
|
|
|
Initialize(sources, context, Action.Edit);
|
2014-01-28 03:19:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-06-20 23:14:32 +00:00
|
|
|
|
public SearchSourceSettingWindow(IList<SearchSource> sources, PluginInitContext context)
|
2014-02-02 08:15:34 +00:00
|
|
|
|
{
|
2016-06-20 23:18:35 +00:00
|
|
|
|
_viewModel = new SearchSourceViewModel {SearchSource = new SearchSource()};
|
2022-12-31 15:43:07 +00:00
|
|
|
|
Initialize(sources, context, Action.Add);
|
2014-02-02 08:15:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-15 05:22:22 +00:00
|
|
|
|
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD100:Avoid async void methods", Justification = "<Pending>")]
|
2022-12-31 15:43:07 +00:00
|
|
|
|
private async void Initialize(IList<SearchSource> sources, PluginInitContext context, Action action)
|
2014-02-06 14:22:02 +00:00
|
|
|
|
{
|
2016-06-20 23:14:32 +00:00
|
|
|
|
InitializeComponent();
|
|
|
|
|
|
DataContext = _viewModel;
|
|
|
|
|
|
_searchSource = _viewModel.SearchSource;
|
|
|
|
|
|
_searchSources = sources;
|
|
|
|
|
|
_action = action;
|
|
|
|
|
|
_context = context;
|
|
|
|
|
|
_api = _context.API;
|
2020-07-22 12:00:14 +00:00
|
|
|
|
|
|
|
|
|
|
_viewModel.SetupCustomImagesDirectory();
|
2020-08-11 22:23:10 +00:00
|
|
|
|
|
2022-10-30 19:23:04 +00:00
|
|
|
|
imgPreviewIcon.Source = await _viewModel.LoadPreviewIconAsync(_searchSource.IconPath);
|
2014-02-06 14:22:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-06-20 23:14:32 +00:00
|
|
|
|
private void OnCancelButtonClick(object sender, RoutedEventArgs e)
|
2014-01-28 03:19:27 +00:00
|
|
|
|
{
|
|
|
|
|
|
Close();
|
|
|
|
|
|
}
|
2014-01-29 14:44:57 +00:00
|
|
|
|
|
2016-06-20 23:14:32 +00:00
|
|
|
|
private void OnConfirmButtonClick(object sender, RoutedEventArgs e)
|
2014-01-29 14:44:57 +00:00
|
|
|
|
{
|
2016-06-20 23:14:32 +00:00
|
|
|
|
if (string.IsNullOrEmpty(_searchSource.Title))
|
2014-01-29 14:44:57 +00:00
|
|
|
|
{
|
2020-04-21 12:54:41 +00:00
|
|
|
|
var warning = _api.GetTranslation("flowlauncher_plugin_websearch_input_title");
|
2024-11-27 04:21:34 +00:00
|
|
|
|
_context.API.ShowMsgBox(warning);
|
2014-01-29 14:44:57 +00:00
|
|
|
|
}
|
2016-06-20 23:14:32 +00:00
|
|
|
|
else if (string.IsNullOrEmpty(_searchSource.Url))
|
|
|
|
|
|
{
|
2020-04-21 12:54:41 +00:00
|
|
|
|
var warning = _api.GetTranslation("flowlauncher_plugin_websearch_input_url");
|
2024-11-27 04:21:34 +00:00
|
|
|
|
_context.API.ShowMsgBox(warning);
|
2016-06-20 23:14:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
else if (string.IsNullOrEmpty(_searchSource.ActionKeyword))
|
2014-01-29 14:44:57 +00:00
|
|
|
|
{
|
2020-04-21 12:54:41 +00:00
|
|
|
|
var warning = _api.GetTranslation("flowlauncher_plugin_websearch_input_action_keyword");
|
2024-11-27 04:21:34 +00:00
|
|
|
|
_context.API.ShowMsgBox(warning);
|
2014-01-29 14:44:57 +00:00
|
|
|
|
}
|
2016-06-20 23:14:32 +00:00
|
|
|
|
else if (_action == Action.Add)
|
|
|
|
|
|
{
|
|
|
|
|
|
AddSearchSource();
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (_action == Action.Edit)
|
|
|
|
|
|
{
|
|
|
|
|
|
EditSearchSource();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2014-01-29 14:44:57 +00:00
|
|
|
|
|
2016-06-20 23:14:32 +00:00
|
|
|
|
private void AddSearchSource()
|
|
|
|
|
|
{
|
|
|
|
|
|
var keyword = _searchSource.ActionKeyword;
|
2025-02-21 05:25:31 +00:00
|
|
|
|
if (!_context.API.ActionKeywordAssigned(keyword))
|
2015-11-09 03:20:02 +00:00
|
|
|
|
{
|
2016-06-20 23:14:32 +00:00
|
|
|
|
var id = _context.CurrentPluginMetadata.ID;
|
2025-02-21 05:50:44 +00:00
|
|
|
|
_context.API.AddActionKeyword(id, keyword);
|
2016-06-20 23:14:32 +00:00
|
|
|
|
|
|
|
|
|
|
_searchSources.Add(_searchSource);
|
|
|
|
|
|
|
|
|
|
|
|
Close();
|
2015-11-09 03:20:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
else
|
2014-01-29 14:44:57 +00:00
|
|
|
|
{
|
2016-06-20 23:14:32 +00:00
|
|
|
|
var warning = _api.GetTranslation("newActionKeywordsHasBeenAssigned");
|
2024-11-27 04:21:34 +00:00
|
|
|
|
_context.API.ShowMsgBox(warning);
|
2014-02-02 08:15:34 +00:00
|
|
|
|
}
|
2016-06-20 23:14:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void EditSearchSource()
|
|
|
|
|
|
{
|
2016-07-21 13:50:37 +00:00
|
|
|
|
var newKeyword = _searchSource.ActionKeyword;
|
|
|
|
|
|
var oldKeyword = _oldSearchSource.ActionKeyword;
|
2025-02-21 05:25:31 +00:00
|
|
|
|
if (!_context.API.ActionKeywordAssigned(newKeyword) || oldKeyword == newKeyword)
|
2016-06-20 23:14:32 +00:00
|
|
|
|
{
|
|
|
|
|
|
var id = _context.CurrentPluginMetadata.ID;
|
2025-02-24 01:53:00 +00:00
|
|
|
|
_context.API.RemoveActionKeyword(id, oldKeyword);
|
|
|
|
|
|
_context.API.AddActionKeyword(id, newKeyword);
|
2015-11-06 02:29:32 +00:00
|
|
|
|
|
2016-06-20 23:14:32 +00:00
|
|
|
|
var index = _searchSources.IndexOf(_oldSearchSource);
|
2025-10-11 07:34:05 +00:00
|
|
|
|
|
|
|
|
|
|
// Only update if we found the item in the collection
|
|
|
|
|
|
if (index >= 0 && index < _searchSources.Count)
|
|
|
|
|
|
{
|
|
|
|
|
|
_searchSources[index] = _searchSource;
|
|
|
|
|
|
Close();
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
var warning = _api.GetTranslation("flowlauncher_plugin_websearch_edit_failed");
|
|
|
|
|
|
_context.API.ShowMsgBox(warning);
|
|
|
|
|
|
Close();
|
|
|
|
|
|
}
|
2016-06-20 23:14:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
var warning = _api.GetTranslation("newActionKeywordsHasBeenAssigned");
|
2024-11-27 04:21:34 +00:00
|
|
|
|
_context.API.ShowMsgBox(warning);
|
2016-06-20 23:14:32 +00:00
|
|
|
|
}
|
2020-08-11 22:23:10 +00:00
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(selectedNewIconImageFullPath))
|
|
|
|
|
|
{
|
|
|
|
|
|
_viewModel.UpdateIconAttributes(_searchSource, selectedNewIconImageFullPath);
|
|
|
|
|
|
|
|
|
|
|
|
_viewModel.CopyNewImageToUserDataDirectoryIfRequired(
|
|
|
|
|
|
_searchSource, selectedNewIconImageFullPath, _oldSearchSource.IconPath);
|
|
|
|
|
|
}
|
2014-01-29 14:44:57 +00:00
|
|
|
|
}
|
2014-02-06 14:22:02 +00:00
|
|
|
|
|
2025-04-15 05:22:22 +00:00
|
|
|
|
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD100:Avoid async void methods", Justification = "<Pending>")]
|
2022-10-30 19:23:04 +00:00
|
|
|
|
private async void OnSelectIconClick(object sender, RoutedEventArgs e)
|
2014-02-06 14:22:02 +00:00
|
|
|
|
{
|
2016-06-20 23:14:32 +00:00
|
|
|
|
const string filter = "Image files (*.jpg, *.jpeg, *.gif, *.png, *.bmp) |*.jpg; *.jpeg; *.gif; *.png; *.bmp";
|
2020-08-11 21:08:24 +00:00
|
|
|
|
var dialog = new OpenFileDialog {InitialDirectory = Main.CustomImagesDirectory, Filter = filter};
|
2014-02-06 14:22:02 +00:00
|
|
|
|
|
2016-06-20 23:14:32 +00:00
|
|
|
|
var result = dialog.ShowDialog();
|
|
|
|
|
|
if (result == true)
|
2014-02-06 14:22:02 +00:00
|
|
|
|
{
|
2020-08-11 22:23:10 +00:00
|
|
|
|
selectedNewIconImageFullPath = dialog.FileName;
|
2020-07-22 12:00:14 +00:00
|
|
|
|
|
2020-08-11 22:23:10 +00:00
|
|
|
|
if (!string.IsNullOrEmpty(selectedNewIconImageFullPath))
|
2015-11-12 19:53:43 +00:00
|
|
|
|
{
|
2020-08-11 22:23:10 +00:00
|
|
|
|
if (_viewModel.ShouldProvideHint(selectedNewIconImageFullPath))
|
2024-11-27 04:21:34 +00:00
|
|
|
|
_context.API.ShowMsgBox(_api.GetTranslation("flowlauncher_plugin_websearch_iconpath_hint"));
|
2020-08-11 22:23:10 +00:00
|
|
|
|
|
2022-10-30 19:23:04 +00:00
|
|
|
|
imgPreviewIcon.Source = await _viewModel.LoadPreviewIconAsync(selectedNewIconImageFullPath);
|
2015-11-12 19:53:43 +00:00
|
|
|
|
}
|
2014-02-06 14:22:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-04-11 04:23:17 +00:00
|
|
|
|
|
|
|
|
|
|
//Block Space Input
|
|
|
|
|
|
private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (e.Key == Key.Space)
|
|
|
|
|
|
{
|
|
|
|
|
|
e.Handled = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
private void TextBox_Pasting(object sender, DataObjectPastingEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (e.DataObject.GetDataPresent(DataFormats.Text))
|
|
|
|
|
|
{
|
|
|
|
|
|
string text = e.DataObject.GetData(DataFormats.Text) as string;
|
|
|
|
|
|
if (!string.IsNullOrEmpty(text) && text.Any(char.IsWhiteSpace))
|
|
|
|
|
|
{
|
|
|
|
|
|
e.CancelCommand();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
e.CancelCommand();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2014-01-28 03:19:27 +00:00
|
|
|
|
}
|
2016-06-20 23:14:32 +00:00
|
|
|
|
|
|
|
|
|
|
public enum Action
|
|
|
|
|
|
{
|
|
|
|
|
|
Add,
|
|
|
|
|
|
Edit
|
|
|
|
|
|
}
|
2022-10-30 19:23:04 +00:00
|
|
|
|
}
|