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;
|
|
|
|
|
using System.Windows;
|
2016-04-26 09:40:23 +08:00
|
|
|
|
using System.Windows.Media;
|
2014-01-28 11:19:27 +08:00
|
|
|
|
using System.Windows.Media.Imaging;
|
2015-01-03 15:20:34 +08:00
|
|
|
|
using Microsoft.Win32;
|
2016-01-08 10:18:19 +08:00
|
|
|
|
using Wox.Infrastructure;
|
2015-11-09 11:20:02 +08:00
|
|
|
|
using Wox.Infrastructure.Exception;
|
2016-04-26 09:40:23 +08:00
|
|
|
|
using Wox.Infrastructure.Image;
|
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
|
|
|
|
|
{
|
2016-04-26 09:40:23 +08:00
|
|
|
|
private const string ImageDirectory = "Images";
|
|
|
|
|
private const string DefaultIcon = "web_search.png";
|
|
|
|
|
private readonly string _pluginDirectory;
|
2015-11-09 11:20:02 +08:00
|
|
|
|
private readonly WebSearchesSetting _settingWindow;
|
|
|
|
|
private bool _isUpdate;
|
2016-04-26 09:40:23 +08:00
|
|
|
|
private WebSearch _webSearch;
|
2015-11-09 11:20:02 +08:00
|
|
|
|
private readonly PluginInitContext _context;
|
2016-03-11 08:39:41 +08:00
|
|
|
|
private readonly WebSearchPlugin _plugin;
|
2016-04-26 09:40:23 +08:00
|
|
|
|
private readonly Settings _settings;
|
2015-11-09 11:20:02 +08:00
|
|
|
|
|
2016-04-21 08:53:21 +08:00
|
|
|
|
public WebSearchSetting(WebSearchesSetting settingWidow, Settings settings)
|
2014-01-28 11:19:27 +08:00
|
|
|
|
{
|
2016-04-26 09:40:23 +08:00
|
|
|
|
InitializeComponent();
|
2016-03-11 08:39:41 +08:00
|
|
|
|
_plugin = settingWidow.Plugin;
|
2015-11-09 11:20:02 +08:00
|
|
|
|
_context = settingWidow.Context;
|
|
|
|
|
_settingWindow = settingWidow;
|
2016-03-28 10:09:57 +08:00
|
|
|
|
_settings = settings;
|
2016-04-26 09:40:23 +08:00
|
|
|
|
_pluginDirectory = _settingWindow.Context.CurrentPluginMetadata.PluginDirectory;
|
2014-01-28 11:19:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-01-26 22:50:38 +08:00
|
|
|
|
public void UpdateItem(WebSearch webSearch)
|
2014-02-02 16:15:34 +08:00
|
|
|
|
{
|
2016-04-26 09:40:23 +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
|
|
|
|
|
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;
|
2016-04-26 09:40:23 +08:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2016-04-26 09:40:23 +08:00
|
|
|
|
public void AddItem(WebSearch websearch)
|
2014-02-06 22:22:02 +08:00
|
|
|
|
{
|
2016-04-26 09:40:23 +08:00
|
|
|
|
_webSearch = websearch;
|
|
|
|
|
if (string.IsNullOrEmpty(_webSearch.IconPath))
|
|
|
|
|
{
|
|
|
|
|
_webSearch.IconPath = DefaultIcon;
|
|
|
|
|
WebSearchIcon.Source = LoadIcon(_webSearch.IconPath);
|
|
|
|
|
}
|
2014-02-06 22:22:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-04-26 09:40:23 +08:00
|
|
|
|
private void CancelButtonOnClick(object sender, RoutedEventArgs e)
|
2014-01-28 11:19:27 +08:00
|
|
|
|
{
|
|
|
|
|
Close();
|
|
|
|
|
}
|
2014-01-29 22:44:57 +08:00
|
|
|
|
|
2016-04-26 09:40:23 +08:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-09 11:20:02 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Confirm button for both add and update
|
|
|
|
|
/// </summary>
|
2016-04-26 09:40:23 +08:00
|
|
|
|
private void ConfirmButtonOnClick(object sender, RoutedEventArgs e)
|
2014-01-29 22:44:57 +08:00
|
|
|
|
{
|
2016-04-26 09:40:23 +08:00
|
|
|
|
string title = WebSearchName.Text;
|
2014-01-29 22:44:57 +08:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-26 09:40:23 +08:00
|
|
|
|
string url = Url.Text;
|
2014-01-29 22:44:57 +08:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-26 09:40:23 +08:00
|
|
|
|
string newActionKeyword = Actionword.Text.Trim();
|
|
|
|
|
|
2015-11-09 11:20:02 +08:00
|
|
|
|
if (_isUpdate)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2016-04-26 09:40:23 +08:00
|
|
|
|
_plugin.NotifyActionKeywordsUpdated(_webSearch.ActionKeyword, newActionKeyword);
|
2015-11-09 11:20:02 +08:00
|
|
|
|
}
|
|
|
|
|
catch (WoxPluginException exception)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(exception.Message);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
2014-01-29 22:44:57 +08:00
|
|
|
|
{
|
2015-11-09 11:20:02 +08:00
|
|
|
|
try
|
|
|
|
|
{
|
2016-03-11 08:39:41 +08:00
|
|
|
|
_plugin.NotifyActionKeywordsAdded(newActionKeyword);
|
2015-11-09 11:20:02 +08:00
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
}
|
2016-04-26 09:40:23 +08:00
|
|
|
|
|
|
|
|
|
_settings.WebSearches.Add(_webSearch);
|
2014-02-02 16:15:34 +08:00
|
|
|
|
}
|
2015-11-06 10:29:32 +08:00
|
|
|
|
|
2016-04-26 09:40:23 +08:00
|
|
|
|
_webSearch.ActionKeyword = newActionKeyword;
|
|
|
|
|
_webSearch.Enabled = EnableCheckBox.IsChecked ?? false;
|
|
|
|
|
_webSearch.Url = url;
|
|
|
|
|
_webSearch.Title = title;
|
|
|
|
|
|
2015-11-09 11:20:02 +08:00
|
|
|
|
_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
|
|
|
|
|
2016-04-26 09:40:23 +08:00
|
|
|
|
private void SelectIconButtonOnClick(object sender, RoutedEventArgs e)
|
2014-02-06 22:22:02 +08:00
|
|
|
|
{
|
2015-01-03 15:20:34 +08:00
|
|
|
|
var dlg = new OpenFileDialog
|
2014-02-06 22:22:02 +08:00
|
|
|
|
{
|
2016-04-26 09:40:23 +08:00
|
|
|
|
InitialDirectory = Path.Combine(_pluginDirectory, ImageDirectory),
|
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();
|
2016-04-26 09:40:23 +08:00
|
|
|
|
if (result != null && result == true)
|
2014-02-06 22:22:02 +08:00
|
|
|
|
{
|
2016-04-26 09:40:23 +08:00
|
|
|
|
string fullpath = dlg.FileName;
|
|
|
|
|
if (fullpath != null)
|
2015-11-13 03:53:43 +08:00
|
|
|
|
{
|
2016-04-26 09:40:23 +08:00
|
|
|
|
WebSearchIcon.Source = LoadIcon(fullpath);
|
2015-11-13 03:53:43 +08:00
|
|
|
|
}
|
2014-02-06 22:22:02 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-01-28 11:19:27 +08:00
|
|
|
|
}
|
|
|
|
|
}
|