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

158 lines
5.5 KiB
C#
Raw Normal View History

using System;
2014-09-19 16:18:54 +08:00
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows;
using System.Windows.Media.Imaging;
using Microsoft.Win32;
namespace Wox.Plugin.WebSearch
{
public partial class WebSearchSetting : Window
{
private string defaultWebSearchImageDirectory = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Images\\websearch");
2014-03-29 16:13:36 +08:00
private WebSearchesSetting settingWindow;
2014-02-02 16:15:34 +08:00
private bool update;
private WebSearch updateWebSearch;
2015-01-07 18:45:55 +08:00
private PluginInitContext context;
2014-02-02 16:15:34 +08:00
2015-01-07 18:45:55 +08:00
public WebSearchSetting(WebSearchesSetting settingWidow,PluginInitContext context)
{
2015-01-07 18:45:55 +08:00
this.context = context;
2014-03-20 02:26:00 +08:00
this.settingWindow = settingWidow;
InitializeComponent();
}
public void UpdateItem(WebSearch webSearch)
2014-02-02 16:15:34 +08:00
{
updateWebSearch = WebSearchStorage.Instance.WebSearches.FirstOrDefault(o => o == webSearch);
2014-02-02 16:15:34 +08:00
if (updateWebSearch == null || string.IsNullOrEmpty(updateWebSearch.Url))
{
2015-01-07 18:45:55 +08:00
string warning = context.API.GetTranslation("wox_plugin_websearch_invalid_web_search");
MessageBox.Show(warning);
2014-02-02 16:15:34 +08:00
Close();
return;
}
update = true;
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)
{
try
{
imgIcon.Source = new BitmapImage(new Uri(path));
}
catch (Exception)
{
}
}
private void BtnCancel_OnClick(object sender, RoutedEventArgs e)
{
Close();
}
private void btnAdd_OnClick(object sender, RoutedEventArgs e)
{
string title = tbTitle.Text;
if (string.IsNullOrEmpty(title))
{
2015-01-07 18:45:55 +08:00
string warning = context.API.GetTranslation("wox_plugin_websearch_input_title");
MessageBox.Show(warning);
return;
}
string url = tbUrl.Text;
if (string.IsNullOrEmpty(url))
{
2015-01-07 18:45:55 +08:00
string warning = context.API.GetTranslation("wox_plugin_websearch_input_url");
MessageBox.Show(warning);
return;
}
string action = tbActionword.Text;
if (string.IsNullOrEmpty(action))
{
2015-01-07 18:45:55 +08:00
string warning = context.API.GetTranslation("wox_plugin_websearch_input_action_keyword");
MessageBox.Show(warning);
return;
}
2014-02-02 16:15:34 +08:00
if (!update)
{
if (WebSearchStorage.Instance.WebSearches.Exists(o => o.ActionKeyword == action))
2014-02-02 16:15:34 +08:00
{
2015-01-07 18:45:55 +08:00
string warning = context.API.GetTranslation("wox_plugin_websearch_action_keyword_exist");
MessageBox.Show(warning);
2014-02-02 16:15:34 +08:00
return;
}
WebSearchStorage.Instance.WebSearches.Add(new WebSearch()
2014-02-02 16:15:34 +08:00
{
ActionKeyword = action,
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
});
context.CurrentPluginMetadata.ActionKeywords.Add(action);
2015-01-07 18:45:55 +08:00
string msg = context.API.GetTranslation("wox_plugin_websearch_succeed");
MessageBox.Show(msg);
}
2014-02-02 16:15:34 +08:00
else
{
if (WebSearchStorage.Instance.WebSearches.Exists(o => o.ActionKeyword == action && o != updateWebSearch))
2014-03-19 20:16:20 +08:00
{
2015-01-07 18:45:55 +08:00
string warning = context.API.GetTranslation("wox_plugin_websearch_action_keyword_exist");
MessageBox.Show(warning);
2014-03-19 20:16:20 +08:00
return;
}
updateWebSearch.ActionKeyword = action;
updateWebSearch.IconPath = tbIconPath.Text;
2014-02-02 16:15:34 +08:00
updateWebSearch.Enabled = cbEnable.IsChecked ?? false;
updateWebSearch.Url = url;
updateWebSearch.Title= title;
context.CurrentPluginMetadata.ActionKeywords.Add(action);
2015-01-07 18:45:55 +08:00
string msg = context.API.GetTranslation("wox_plugin_websearch_succeed");
MessageBox.Show(msg);
2014-02-02 16:15:34 +08:00
}
WebSearchStorage.Instance.Save();
2014-03-20 02:26:00 +08:00
settingWindow.ReloadWebSearchView();
2014-02-02 16:15:34 +08:00
Close();
}
private void BtnSelectIcon_OnClick(object sender, RoutedEventArgs e)
{
2014-09-19 16:18:54 +08:00
if(!Directory.Exists(defaultWebSearchImageDirectory))
{
defaultWebSearchImageDirectory =
Path.GetDirectoryName(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
2014-09-19 16:18:54 +08:00
}
var dlg = new OpenFileDialog
{
2014-09-19 16:18:54 +08:00
InitialDirectory = defaultWebSearchImageDirectory,
Filter ="Image files (*.jpg, *.jpeg, *.gif, *.png, *.bmp) |*.jpg; *.jpeg; *.gif; *.png; *.bmp"
};
bool? result = dlg.ShowDialog();
if (result == true)
{
string filename = dlg.FileName;
tbIconPath.Text = filename;
ShowIcon(filename);
}
}
}
}