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

155 lines
5.1 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
2014-09-19 16:18:54 +08:00
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
2014-02-02 16:15:34 +08:00
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Wox.Infrastructure;
2014-03-23 16:17:41 +08:00
using Wox.Infrastructure.Storage;
using Wox.Infrastructure.Storage.UserSettings;
2014-02-02 16:15:34 +08:00
using MessageBox = System.Windows.MessageBox;
2014-03-29 16:13:36 +08:00
namespace Wox.Plugin.SystemPlugins
{
public partial class WebSearchSetting : Window
{
2014-09-19 16:18:54 +08:00
private string defaultWebSearchImageDirectory = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath), "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;
2014-03-29 16:13:36 +08:00
public WebSearchSetting(WebSearchesSetting settingWidow)
{
2014-03-20 02:26:00 +08:00
this.settingWindow = settingWidow;
InitializeComponent();
}
2014-02-02 16:15:34 +08:00
public void UpdateItem(WebSearch webSearch)
{
2014-03-23 16:17:41 +08:00
updateWebSearch = UserSettingStorage.Instance.WebSearches.FirstOrDefault(o => o == webSearch);
2014-02-02 16:15:34 +08:00
if (updateWebSearch == null || string.IsNullOrEmpty(updateWebSearch.Url))
{
MessageBox.Show("Invalid web search");
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.ActionWord;
}
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))
{
MessageBox.Show("Please input Title field");
return;
}
string url = tbUrl.Text;
if (string.IsNullOrEmpty(url))
{
MessageBox.Show("Please input URL field");
return;
}
string action = tbActionword.Text;
if (string.IsNullOrEmpty(action))
{
MessageBox.Show("Please input ActionWord field");
return;
}
2014-02-02 16:15:34 +08:00
if (!update)
{
2014-03-23 16:17:41 +08:00
if (UserSettingStorage.Instance.WebSearches.Exists(o => o.ActionWord == action))
2014-02-02 16:15:34 +08:00
{
MessageBox.Show("ActionWord has existed, please input a new one.");
return;
}
2014-03-23 16:17:41 +08:00
UserSettingStorage.Instance.WebSearches.Add(new WebSearch()
2014-02-02 16:15:34 +08:00
{
ActionWord = action,
Enabled = cbEnable.IsChecked ?? false,
IconPath = tbIconPath.Text,
2014-02-02 16:15:34 +08:00
Url = url,
Title = title
});
MessageBox.Show(string.Format("Add {0} web search successfully!", title));
}
2014-02-02 16:15:34 +08:00
else
{
2014-03-23 16:17:41 +08:00
if (UserSettingStorage.Instance.WebSearches.Exists(o => o.ActionWord == action && o != updateWebSearch))
2014-03-19 20:16:20 +08:00
{
MessageBox.Show("ActionWord has existed, please input a new one.");
return;
}
2014-02-02 16:15:34 +08:00
updateWebSearch.ActionWord = action;
updateWebSearch.IconPath = tbIconPath.Text;
2014-02-02 16:15:34 +08:00
updateWebSearch.Enabled = cbEnable.IsChecked ?? false;
updateWebSearch.Url = url;
updateWebSearch.Title= title;
MessageBox.Show(string.Format("Update {0} web search successfully!", title));
}
2014-03-23 16:17:41 +08:00
UserSettingStorage.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 =
System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath);
}
var dlg = new Microsoft.Win32.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);
}
}
}
}