2014-01-28 11:19:27 +08:00
|
|
|
|
using System;
|
2014-09-19 16:18:54 +08:00
|
|
|
|
using System.IO;
|
2014-01-28 11:19:27 +08:00
|
|
|
|
using System.Linq;
|
2015-01-03 15:20:34 +08:00
|
|
|
|
using System.Reflection;
|
2014-01-28 11:19:27 +08:00
|
|
|
|
using System.Windows;
|
|
|
|
|
using System.Windows.Media.Imaging;
|
2015-01-03 15:20:34 +08:00
|
|
|
|
using Microsoft.Win32;
|
2015-11-09 11:20:02 +08:00
|
|
|
|
using Wox.Infrastructure.Exception;
|
2014-01-28 11:19:27 +08:00
|
|
|
|
|
2015-01-03 15:20:34 +08:00
|
|
|
|
namespace Wox.Plugin.WebSearch
|
2014-01-28 11:19:27 +08:00
|
|
|
|
{
|
|
|
|
|
public partial class WebSearchSetting : Window
|
|
|
|
|
{
|
2015-11-13 03:53:43 +08:00
|
|
|
|
private const string _imageDirectoryName = "Images";
|
|
|
|
|
private string _pluginDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
|
2015-11-09 11:20:02 +08:00
|
|
|
|
private readonly WebSearchesSetting _settingWindow;
|
|
|
|
|
private bool _isUpdate;
|
|
|
|
|
private WebSearch _updateWebSearch;
|
|
|
|
|
private readonly PluginInitContext _context;
|
|
|
|
|
private readonly WebSearchPlugin _plguin;
|
|
|
|
|
|
|
|
|
|
public WebSearchSetting(WebSearchesSetting settingWidow)
|
2014-01-28 11:19:27 +08:00
|
|
|
|
{
|
2015-11-09 11:20:02 +08:00
|
|
|
|
_plguin = settingWidow.Plugin;
|
|
|
|
|
_context = settingWidow.Context;
|
|
|
|
|
_settingWindow = settingWidow;
|
2014-01-28 11:19:27 +08:00
|
|
|
|
InitializeComponent();
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-26 22:50:38 +08:00
|
|
|
|
public void UpdateItem(WebSearch webSearch)
|
2014-02-02 16:15:34 +08:00
|
|
|
|
{
|
2015-11-09 11:20:02 +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
|
|
|
|
|
2015-11-09 11:20:02 +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;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-09 11:20:02 +08:00
|
|
|
|
_isUpdate = true;
|
2014-02-02 16:15:34 +08:00
|
|
|
|
lblAdd.Text = "Update";
|
2014-02-06 22:22:02 +08:00
|
|
|
|
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;
|
2015-11-05 06:49:40 +08:00
|
|
|
|
tbActionword.Text = webSearch.ActionKeyword;
|
2014-02-02 16:15:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-06 22:22:02 +08:00
|
|
|
|
private void ShowIcon(string path)
|
|
|
|
|
{
|
2015-11-13 03:53:43 +08:00
|
|
|
|
imgIcon.Source = new BitmapImage(new Uri(Path.Combine(_pluginDirectory, path), UriKind.Absolute));
|
2014-02-06 22:22:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-28 11:19:27 +08:00
|
|
|
|
private void BtnCancel_OnClick(object sender, RoutedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
Close();
|
|
|
|
|
}
|
2014-01-29 22:44:57 +08:00
|
|
|
|
|
2015-11-09 11:20:02 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Confirm button for both add and update
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void btnConfirm_OnClick(object sender, RoutedEventArgs e)
|
2014-01-29 22:44:57 +08:00
|
|
|
|
{
|
|
|
|
|
string title = tbTitle.Text;
|
|
|
|
|
if (string.IsNullOrEmpty(title))
|
|
|
|
|
{
|
2015-11-09 11:20:02 +08:00
|
|
|
|
string warning = _context.API.GetTranslation("wox_plugin_websearch_input_title");
|
2015-01-07 18:45:55 +08:00
|
|
|
|
MessageBox.Show(warning);
|
2014-01-29 22:44:57 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string url = tbUrl.Text;
|
|
|
|
|
if (string.IsNullOrEmpty(url))
|
|
|
|
|
{
|
2015-11-09 11:20:02 +08:00
|
|
|
|
string warning = _context.API.GetTranslation("wox_plugin_websearch_input_url");
|
2015-01-07 18:45:55 +08:00
|
|
|
|
MessageBox.Show(warning);
|
2014-01-29 22:44:57 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-09 11:20:02 +08:00
|
|
|
|
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
|
|
|
|
|
2015-11-09 11:20:02 +08:00
|
|
|
|
_updateWebSearch.ActionKeyword = newActionKeyword;
|
|
|
|
|
_updateWebSearch.IconPath = tbIconPath.Text;
|
|
|
|
|
_updateWebSearch.Enabled = cbEnable.IsChecked ?? false;
|
|
|
|
|
_updateWebSearch.Url = url;
|
|
|
|
|
_updateWebSearch.Title = title;
|
|
|
|
|
}
|
|
|
|
|
else
|
2014-01-29 22:44:57 +08:00
|
|
|
|
{
|
2015-11-09 11:20:02 +08:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_plguin.NotifyActionKeywordsAdded(newActionKeyword);
|
|
|
|
|
}
|
|
|
|
|
catch (WoxPluginException exception)
|
2014-02-02 16:15:34 +08:00
|
|
|
|
{
|
2015-11-09 11:20:02 +08:00
|
|
|
|
MessageBox.Show(exception.Message);
|
2014-02-02 16:15:34 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
2015-01-26 22:50:38 +08:00
|
|
|
|
WebSearchStorage.Instance.WebSearches.Add(new WebSearch()
|
2014-02-02 16:15:34 +08:00
|
|
|
|
{
|
2015-11-09 11:20:02 +08:00
|
|
|
|
ActionKeyword = newActionKeyword,
|
2014-02-02 16:15:34 +08:00
|
|
|
|
Enabled = cbEnable.IsChecked ?? false,
|
2014-02-06 22:22:02 +08:00
|
|
|
|
IconPath = tbIconPath.Text,
|
2014-02-02 16:15:34 +08:00
|
|
|
|
Url = url,
|
|
|
|
|
Title = title
|
|
|
|
|
});
|
|
|
|
|
}
|
2015-11-06 10:29:32 +08:00
|
|
|
|
|
2015-11-09 11:20:02 +08:00
|
|
|
|
WebSearchStorage.Instance.Save();
|
|
|
|
|
_settingWindow.ReloadWebSearchView();
|
2014-02-02 16:15:34 +08:00
|
|
|
|
Close();
|
2014-01-29 22:44:57 +08:00
|
|
|
|
}
|
2014-02-06 22:22:02 +08:00
|
|
|
|
|
|
|
|
|
private void BtnSelectIcon_OnClick(object sender, RoutedEventArgs e)
|
|
|
|
|
{
|
2015-11-13 03:53:43 +08:00
|
|
|
|
if (!Directory.Exists(_pluginDirectory))
|
2014-09-19 16:18:54 +08:00
|
|
|
|
{
|
2015-11-13 03:53:43 +08:00
|
|
|
|
_pluginDirectory =
|
2015-01-03 15:20:34 +08:00
|
|
|
|
Path.GetDirectoryName(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
|
2014-09-19 16:18:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-01-03 15:20:34 +08:00
|
|
|
|
var dlg = new OpenFileDialog
|
2014-02-06 22:22:02 +08:00
|
|
|
|
{
|
2015-11-13 03:53:43 +08:00
|
|
|
|
InitialDirectory = Path.Combine(_pluginDirectory, _imageDirectoryName),
|
2015-11-09 11:20:02 +08:00
|
|
|
|
Filter = "Image files (*.jpg, *.jpeg, *.gif, *.png, *.bmp) |*.jpg; *.jpeg; *.gif; *.png; *.bmp"
|
2014-02-06 22:22:02 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
bool? result = dlg.ShowDialog();
|
|
|
|
|
if (result == true)
|
|
|
|
|
{
|
|
|
|
|
string filename = dlg.FileName;
|
2015-11-13 03:53:43 +08:00
|
|
|
|
if (filename != null)
|
|
|
|
|
{
|
|
|
|
|
tbIconPath.Text = Path.Combine(_imageDirectoryName, Path.GetFileName(filename));
|
|
|
|
|
ShowIcon(tbIconPath.Text);
|
|
|
|
|
}
|
2014-02-06 22:22:02 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-01-28 11:19:27 +08:00
|
|
|
|
}
|
|
|
|
|
}
|