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

155 lines
5.1 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.Imaging;
using Microsoft.Win32;
2016-01-08 10:18:19 +08:00
using Wox.Infrastructure;
using Wox.Infrastructure.Exception;
namespace Wox.Plugin.WebSearch
{
public partial class WebSearchSetting : Window
{
private const string _imageDirectoryName = "Images";
2016-01-08 10:18:19 +08:00
private string _pluginDirectory = WoxDirectroy.Executable;
private readonly WebSearchesSetting _settingWindow;
private bool _isUpdate;
private WebSearch _updateWebSearch;
private readonly PluginInitContext _context;
private readonly WebSearchPlugin _plguin;
public WebSearchSetting(WebSearchesSetting settingWidow)
{
_plguin = settingWidow.Plugin;
_context = settingWidow.Context;
_settingWindow = settingWidow;
InitializeComponent();
}
public void UpdateItem(WebSearch webSearch)
2014-02-02 16:15:34 +08:00
{
_updateWebSearch = WebSearchStorage.Instance.WebSearches.FirstOrDefault(o => o == webSearch);
if (_updateWebSearch == null || string.IsNullOrEmpty(_updateWebSearch.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;
2014-02-02 16:15:34 +08:00
lblAdd.Text = "Update";
tbIconPath.Text = webSearch.IconPath;
ShowIcon(webSearch.IconPath);
2014-02-02 16:15:34 +08:00
cbEnable.IsChecked = webSearch.Enabled;
tbTitle.Text = webSearch.Title;
tbUrl.Text = webSearch.Url;
tbActionword.Text = webSearch.ActionKeyword;
2014-02-02 16:15:34 +08:00
}
private void ShowIcon(string path)
{
imgIcon.Source = new BitmapImage(new Uri(Path.Combine(_pluginDirectory, path), UriKind.Absolute));
}
private void BtnCancel_OnClick(object sender, RoutedEventArgs e)
{
Close();
}
/// <summary>
/// Confirm button for both add and update
/// </summary>
private void btnConfirm_OnClick(object sender, RoutedEventArgs e)
{
string title = tbTitle.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 = tbUrl.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 = tbActionword.Text.Trim();
if (_isUpdate)
{
try
{
_plguin.NotifyActionKeywordsUpdated(_updateWebSearch.ActionKeyword, newActionKeyword);
}
catch (WoxPluginException exception)
{
MessageBox.Show(exception.Message);
return;
}
2014-02-02 16:15:34 +08:00
_updateWebSearch.ActionKeyword = newActionKeyword;
_updateWebSearch.IconPath = tbIconPath.Text;
_updateWebSearch.Enabled = cbEnable.IsChecked ?? false;
_updateWebSearch.Url = url;
_updateWebSearch.Title = title;
}
else
{
try
{
_plguin.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;
}
2016-01-07 05:34:42 +08:00
WebSearchStorage.Instance.WebSearches.Add(new WebSearch
2014-02-02 16:15:34 +08:00
{
ActionKeyword = newActionKeyword,
2014-02-02 16:15:34 +08:00
Enabled = cbEnable.IsChecked ?? false,
IconPath = tbIconPath.Text,
2014-02-02 16:15:34 +08:00
Url = url,
Title = title
});
}
WebSearchStorage.Instance.Save();
_settingWindow.ReloadWebSearchView();
2014-02-02 16:15:34 +08:00
Close();
}
private void BtnSelectIcon_OnClick(object sender, RoutedEventArgs e)
{
if (!Directory.Exists(_pluginDirectory))
2014-09-19 16:18:54 +08:00
{
_pluginDirectory =
2016-01-08 10:18:19 +08:00
Path.GetDirectoryName(WoxDirectroy.Executable);
2014-09-19 16:18:54 +08:00
}
var dlg = new OpenFileDialog
{
InitialDirectory = Path.Combine(_pluginDirectory, _imageDirectoryName),
Filter = "Image files (*.jpg, *.jpeg, *.gif, *.png, *.bmp) |*.jpg; *.jpeg; *.gif; *.png; *.bmp"
};
bool? result = dlg.ShowDialog();
if (result == true)
{
string filename = dlg.FileName;
if (filename != null)
{
tbIconPath.Text = Path.Combine(_imageDirectoryName, Path.GetFileName(filename));
ShowIcon(tbIconPath.Text);
}
}
}
}
}