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;
|
2015-01-03 15:20:34 +08:00
|
|
|
|
using Microsoft.Win32;
|
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
|
|
|
|
{
|
2016-05-04 04:18:26 +08:00
|
|
|
|
public partial class WebSearchSetting
|
2014-01-28 11:19:27 +08:00
|
|
|
|
{
|
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-05-07 10:55:09 +08:00
|
|
|
|
private readonly Main _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-05-06 07:29:27 +08:00
|
|
|
|
WebSearchName.Focus();
|
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;
|
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";
|
2016-05-04 04:18:26 +08:00
|
|
|
|
WebSearchIcon.Source = ImageLoader.Load(webSearch.IconPath);
|
2016-04-26 09:40:23 +08:00
|
|
|
|
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-05-04 04:18:26 +08:00
|
|
|
|
public void AddItem(WebSearch webSearch)
|
2014-02-06 22:22:02 +08:00
|
|
|
|
{
|
2016-05-04 04:18:26 +08:00
|
|
|
|
_webSearch = webSearch;
|
|
|
|
|
WebSearchIcon.Source = ImageLoader.Load(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
|
|
|
|
|
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
|
|
|
|
{
|
2016-05-08 02:16:13 +08:00
|
|
|
|
var directory = Path.Combine(Main.ImagesDirectory, Main.Images);
|
2015-01-03 15:20:34 +08:00
|
|
|
|
var dlg = new OpenFileDialog
|
2014-02-06 22:22:02 +08:00
|
|
|
|
{
|
2016-05-04 04:18:26 +08:00
|
|
|
|
InitialDirectory = directory,
|
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-05-04 04:18:26 +08:00
|
|
|
|
_webSearch.Icon = Path.GetFileName(fullpath);
|
|
|
|
|
if (File.Exists(_webSearch.IconPath))
|
|
|
|
|
{
|
|
|
|
|
WebSearchIcon.Source = ImageLoader.Load(_webSearch.IconPath);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_webSearch.Icon = WebSearch.DefaultIcon;
|
|
|
|
|
MessageBox.Show($"The file should be put under {directory}");
|
|
|
|
|
}
|
2015-11-13 03:53:43 +08:00
|
|
|
|
}
|
2014-02-06 22:22:02 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-01-28 11:19:27 +08:00
|
|
|
|
}
|
|
|
|
|
}
|