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

177 lines
5.7 KiB
C#
Raw Normal View History

using System;
2014-09-19 16:18:54 +08:00
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Microsoft.Win32;
2016-01-08 10:18:19 +08:00
using Wox.Infrastructure;
using Wox.Infrastructure.Exception;
using Wox.Infrastructure.Image;
namespace Wox.Plugin.WebSearch
{
public partial class WebSearchSetting : Window
{
private const string ImageDirectory = "Images";
private const string DefaultIcon = "web_search.png";
private readonly string _pluginDirectory;
private readonly WebSearchesSetting _settingWindow;
private bool _isUpdate;
private WebSearch _webSearch;
private readonly PluginInitContext _context;
2016-03-11 08:39:41 +08:00
private readonly WebSearchPlugin _plugin;
private readonly Settings _settings;
public WebSearchSetting(WebSearchesSetting settingWidow, Settings settings)
{
InitializeComponent();
2016-03-11 08:39:41 +08:00
_plugin = settingWidow.Plugin;
_context = settingWidow.Context;
_settingWindow = settingWidow;
_settings = settings;
_pluginDirectory = _settingWindow.Context.CurrentPluginMetadata.PluginDirectory;
}
public void UpdateItem(WebSearch webSearch)
2014-02-02 16:15:34 +08:00
{
_webSearch = _settings.WebSearches.FirstOrDefault(o => o == webSearch);
if (_webSearch == null || string.IsNullOrEmpty(_webSearch.Url))
2014-02-02 16:15:34 +08:00
{
2015-01-07 18:45:55 +08:00
string warning = _context.API.GetTranslation("wox_plugin_websearch_invalid_web_search");
2015-01-07 18:45:55 +08:00
MessageBox.Show(warning);
2014-02-02 16:15:34 +08:00
Close();
return;
}
_isUpdate = true;
ConfirmButton.Content = "Update";
WebSearchIcon.Source = LoadIcon(webSearch.IconPath);
EnableCheckBox.IsChecked = webSearch.Enabled;
WebSearchName.Text = webSearch.Title;
Url.Text = webSearch.Url;
Actionword.Text = webSearch.ActionKeyword;
2014-02-02 16:15:34 +08:00
}
public void AddItem(WebSearch websearch)
{
_webSearch = websearch;
if (string.IsNullOrEmpty(_webSearch.IconPath))
{
_webSearch.IconPath = DefaultIcon;
WebSearchIcon.Source = LoadIcon(_webSearch.IconPath);
}
}
private void CancelButtonOnClick(object sender, RoutedEventArgs e)
{
Close();
}
private ImageSource LoadIcon(string path)
{
if (path != null)
{
var releativePath = Path.Combine(_pluginDirectory, ImageDirectory, Path.GetFileName(path));
if (File.Exists(releativePath))
{
_webSearch.IconPath = path;
var source = ImageLoader.Load(releativePath);
return source;
}
else
{
_webSearch.IconPath = path;
var source = ImageLoader.Load(path);
return source;
}
}
else
{
var source = ImageLoader.Load(Path.Combine(_pluginDirectory, ImageDirectory, DefaultIcon));
return source;
}
}
/// <summary>
/// Confirm button for both add and update
/// </summary>
private void ConfirmButtonOnClick(object sender, RoutedEventArgs e)
{
string title = WebSearchName.Text;
if (string.IsNullOrEmpty(title))
{
string warning = _context.API.GetTranslation("wox_plugin_websearch_input_title");
2015-01-07 18:45:55 +08:00
MessageBox.Show(warning);
return;
}
string url = Url.Text;
if (string.IsNullOrEmpty(url))
{
string warning = _context.API.GetTranslation("wox_plugin_websearch_input_url");
2015-01-07 18:45:55 +08:00
MessageBox.Show(warning);
return;
}
string newActionKeyword = Actionword.Text.Trim();
if (_isUpdate)
{
try
{
_plugin.NotifyActionKeywordsUpdated(_webSearch.ActionKeyword, newActionKeyword);
}
catch (WoxPluginException exception)
{
MessageBox.Show(exception.Message);
return;
}
}
else
{
try
{
2016-03-11 08:39:41 +08:00
_plugin.NotifyActionKeywordsAdded(newActionKeyword);
}
catch (WoxPluginException exception)
2014-02-02 16:15:34 +08:00
{
MessageBox.Show(exception.Message);
2014-02-02 16:15:34 +08:00
return;
}
_settings.WebSearches.Add(_webSearch);
2014-02-02 16:15:34 +08:00
}
_webSearch.ActionKeyword = newActionKeyword;
_webSearch.Enabled = EnableCheckBox.IsChecked ?? false;
_webSearch.Url = url;
_webSearch.Title = title;
_settingWindow.ReloadWebSearchView();
2014-02-02 16:15:34 +08:00
Close();
}
private void SelectIconButtonOnClick(object sender, RoutedEventArgs e)
{
var dlg = new OpenFileDialog
{
InitialDirectory = Path.Combine(_pluginDirectory, ImageDirectory),
Filter = "Image files (*.jpg, *.jpeg, *.gif, *.png, *.bmp) |*.jpg; *.jpeg; *.gif; *.png; *.bmp"
};
bool? result = dlg.ShowDialog();
if (result != null && result == true)
{
string fullpath = dlg.FileName;
if (fullpath != null)
{
WebSearchIcon.Source = LoadIcon(fullpath);
}
}
}
}
}