PowerToys/Plugins/Wox.Plugin.WebSearch/SearchSourceSetting.xaml.cs

148 lines
5.0 KiB
C#
Raw Normal View History

using System.Collections.Generic;
2014-09-19 16:18:54 +08:00
using System.IO;
using System.Windows;
using Microsoft.Win32;
using Wox.Core.Plugin;
namespace Wox.Plugin.WebSearch
{
public partial class SearchSourceSettingWindow
{
private readonly SearchSource _oldSearchSource;
private SearchSource _searchSource;
private IList<SearchSource> _searchSources;
private Action _action;
private PluginInitContext _context;
private IPublicAPI _api;
private SearchSourceViewModel _viewModel;
public SearchSourceSettingWindow(IList<SearchSource> sources, PluginInitContext context, SearchSource old)
{
_oldSearchSource = old;
2016-06-21 07:18:35 +08:00
_viewModel = new SearchSourceViewModel {SearchSource = old.DeepCopy()};
Initilize(sources, context, Action.Edit);
}
public SearchSourceSettingWindow(IList<SearchSource> sources, PluginInitContext context)
2014-02-02 16:15:34 +08:00
{
2016-06-21 07:18:35 +08:00
_viewModel = new SearchSourceViewModel {SearchSource = new SearchSource()};
Initilize(sources, context, Action.Add);
2014-02-02 16:15:34 +08:00
}
private void Initilize(IList<SearchSource> sources, PluginInitContext context, Action action)
{
InitializeComponent();
DataContext = _viewModel;
_searchSource = _viewModel.SearchSource;
_searchSources = sources;
_action = action;
_context = context;
_api = _context.API;
}
private void OnCancelButtonClick(object sender, RoutedEventArgs e)
{
Close();
}
private void OnConfirmButtonClick(object sender, RoutedEventArgs e)
{
if (string.IsNullOrEmpty(_searchSource.Title))
{
var warning = _api.GetTranslation("wox_plugin_websearch_input_title");
2015-01-07 18:45:55 +08:00
MessageBox.Show(warning);
}
else if (string.IsNullOrEmpty(_searchSource.Url))
{
var warning = _api.GetTranslation("wox_plugin_websearch_input_url");
MessageBox.Show(warning);
}
else if (string.IsNullOrEmpty(_searchSource.ActionKeyword))
{
var warning = _api.GetTranslation("wox_plugin_websearch_input_action_keyword");
2015-01-07 18:45:55 +08:00
MessageBox.Show(warning);
}
else if (_action == Action.Add)
{
AddSearchSource();
}
else if (_action == Action.Edit)
{
EditSearchSource();
}
}
private void AddSearchSource()
{
var keyword = _searchSource.ActionKeyword;
if (!PluginManager.ActionKeywordRegistered(keyword))
{
var id = _context.CurrentPluginMetadata.ID;
PluginManager.AddActionKeyword(id, keyword);
_searchSources.Add(_searchSource);
2019-08-21 18:18:20 +08:00
var info = _api.GetTranslation("success");
MessageBox.Show(info);
Close();
}
else
{
var warning = _api.GetTranslation("newActionKeywordsHasBeenAssigned");
MessageBox.Show(warning);
2014-02-02 16:15:34 +08:00
}
}
private void EditSearchSource()
{
var newKeyword = _searchSource.ActionKeyword;
var oldKeyword = _oldSearchSource.ActionKeyword;
if (!PluginManager.ActionKeywordRegistered(newKeyword) || oldKeyword == newKeyword)
{
var id = _context.CurrentPluginMetadata.ID;
PluginManager.ReplaceActionKeyword(id, oldKeyword, newKeyword);
var index = _searchSources.IndexOf(_oldSearchSource);
_searchSources[index] = _searchSource;
2019-08-21 18:18:20 +08:00
var info = _api.GetTranslation("success");
MessageBox.Show(info);
Close();
}
else
{
var warning = _api.GetTranslation("newActionKeywordsHasBeenAssigned");
MessageBox.Show(warning);
}
}
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";
2016-06-21 07:18:35 +08:00
var dialog = new OpenFileDialog {InitialDirectory = directory, Filter = filter};
var result = dialog.ShowDialog();
if (result == true)
{
var fullpath = dialog.FileName;
if (!string.IsNullOrEmpty(fullpath))
{
_searchSource.Icon = Path.GetFileName(fullpath);
if (!File.Exists(_searchSource.IconPath))
{
_searchSource.Icon = SearchSource.DefaultIcon;
MessageBox.Show($"The file should be put under {directory}");
}
}
}
}
}
public enum Action
{
Add,
Edit
}
}