PowerToys/Wox/SettingWindow.xaml.cs

330 lines
11 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
2014-07-04 15:12:22 +08:00
using System.Diagnostics;
using System.IO;
using System.Linq;
2014-07-18 20:00:55 +08:00
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Navigation;
2015-01-14 22:19:44 +08:00
using Microsoft.Win32;
2016-02-23 05:43:37 +08:00
using NHotkey;
using NHotkey.Wpf;
using Wox.Core;
2015-10-31 07:17:34 +08:00
using Wox.Core.Plugin;
using Wox.Core.Resource;
2016-02-23 05:43:37 +08:00
using Wox.Infrastructure.Hotkey;
2016-06-19 23:18:43 +08:00
using Wox.Infrastructure.UserSettings;
2015-10-31 07:17:34 +08:00
using Wox.Plugin;
2016-02-23 05:43:37 +08:00
using Wox.ViewModel;
2014-01-29 18:33:24 +08:00
namespace Wox
{
2016-05-10 05:45:20 +08:00
public partial class SettingWindow
{
2016-05-19 02:35:34 +08:00
private const string StartupPath = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
2016-02-12 15:22:01 +08:00
public readonly IPublicAPI _api;
private Settings _settings;
private SettingWindowViewModel _viewModel;
2016-05-22 05:44:27 +08:00
public SettingWindow(IPublicAPI api, SettingWindowViewModel viewModel)
{
InitializeComponent();
2016-05-22 05:44:27 +08:00
_settings = viewModel.Settings;
DataContext = viewModel;
_viewModel = viewModel;
_api = api;
}
2015-02-07 21:27:48 +08:00
#region General
2016-05-22 06:16:32 +08:00
void OnLanguageChanged(object sender, SelectionChangedEventArgs e)
2015-01-02 16:16:09 +08:00
{
2016-05-22 06:16:32 +08:00
var language = (Language)e.AddedItems[0];
InternationalizationManager.Instance.ChangeLanguage(language);
2015-01-02 16:16:09 +08:00
}
2016-05-22 06:16:32 +08:00
private void OnAutoStartupChecked(object sender, RoutedEventArgs e)
{
2016-05-19 02:35:34 +08:00
SetStartup();
}
2016-05-22 06:16:32 +08:00
private void OnAutoStartupUncheck(object sender, RoutedEventArgs e)
{
2016-05-19 02:35:34 +08:00
RemoveStartup();
2015-01-14 22:19:44 +08:00
}
2016-05-19 02:35:34 +08:00
public static void SetStartup()
2015-01-14 22:19:44 +08:00
{
2016-05-19 02:35:34 +08:00
using (var key = Registry.CurrentUser.OpenSubKey(StartupPath, true))
{
2016-05-19 02:38:43 +08:00
key?.SetValue(Infrastructure.Constant.Wox, Infrastructure.Constant.ExecutablePath);
}
2015-01-14 22:19:44 +08:00
}
2014-03-19 01:20:51 +08:00
2016-05-19 02:35:34 +08:00
private void RemoveStartup()
2015-01-14 22:19:44 +08:00
{
2016-05-19 02:35:34 +08:00
using (var key = Registry.CurrentUser.OpenSubKey(StartupPath, true))
2015-01-14 22:19:44 +08:00
{
2016-05-19 02:38:43 +08:00
key?.DeleteValue(Infrastructure.Constant.Wox, false);
2015-01-14 22:19:44 +08:00
}
}
2016-05-19 02:35:34 +08:00
public static bool StartupSet()
{
2016-05-19 02:35:34 +08:00
using (var key = Registry.CurrentUser.OpenSubKey(StartupPath, true))
2015-01-14 22:19:44 +08:00
{
2016-05-22 06:16:32 +08:00
var path = key?.GetValue(Infrastructure.Constant.Wox) as string;
2016-05-19 02:35:34 +08:00
if (path != null)
{
2016-05-19 02:38:43 +08:00
return path == Infrastructure.Constant.ExecutablePath;
2016-05-19 02:35:34 +08:00
}
else
{
return false;
}
2015-01-14 22:19:44 +08:00
}
}
2016-05-22 06:16:32 +08:00
private void OnSelectPythonDirectoryClick(object sender, RoutedEventArgs e)
{
2016-05-10 05:45:20 +08:00
var dlg = new System.Windows.Forms.FolderBrowserDialog
2016-05-07 04:25:53 +08:00
{
SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)
};
var result = dlg.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK)
{
string pythonDirectory = dlg.SelectedPath;
if (!string.IsNullOrEmpty(pythonDirectory))
{
var pythonPath = Path.Combine(pythonDirectory, PluginsLoader.PythonExecutable);
if (File.Exists(pythonPath))
{
_settings.PluginSettings.PythonDirectory = pythonDirectory;
MessageBox.Show("Remember to restart Wox use new Python path");
}
else
{
MessageBox.Show("Can't find python in given directory");
}
}
}
}
2016-05-09 09:41:46 +08:00
2015-02-07 21:27:48 +08:00
#endregion
#region Hotkey
private void OnHotkeyControlLoaded(object sender, RoutedEventArgs e)
{
HotkeyControl.SetHotkey(_viewModel.Settings.Hotkey, false);
}
void OnHotkeyChanged(object sender, EventArgs e)
2014-02-22 15:52:20 +08:00
{
2016-05-22 02:04:52 +08:00
if (HotkeyControl.CurrentHotkeyAvailable)
2014-02-22 15:52:20 +08:00
{
SetHotkey(HotkeyControl.CurrentHotkey, (o, args) =>
2016-02-12 15:22:01 +08:00
{
if (!Application.Current.MainWindow.IsVisible)
2016-02-12 15:22:01 +08:00
{
Application.Current.MainWindow.Visibility = Visibility.Visible;
2016-02-12 15:22:01 +08:00
}
else
{
Application.Current.MainWindow.Visibility = Visibility.Hidden;
2016-02-12 15:22:01 +08:00
}
});
RemoveHotkey(_settings.Hotkey);
2016-05-22 02:04:52 +08:00
_settings.Hotkey = HotkeyControl.CurrentHotkey.ToString();
2014-02-22 15:52:20 +08:00
}
}
2016-02-12 15:22:01 +08:00
void SetHotkey(HotkeyModel hotkey, EventHandler<HotkeyEventArgs> action)
{
string hotkeyStr = hotkey.ToString();
try
{
HotkeyManager.Current.AddOrReplace(hotkeyStr, hotkey.CharKey, hotkey.ModifierKeys, action);
}
catch (Exception)
{
2016-05-09 09:41:46 +08:00
string errorMsg =
string.Format(InternationalizationManager.Instance.GetTranslation("registerHotkeyFailed"), hotkeyStr);
2016-02-12 15:22:01 +08:00
MessageBox.Show(errorMsg);
}
}
void RemoveHotkey(string hotkeyStr)
{
if (!string.IsNullOrEmpty(hotkeyStr))
{
HotkeyManager.Current.Remove(hotkeyStr);
}
}
private void OnDeleteCustomHotkeyClick(object sender, RoutedEventArgs e)
2014-02-22 15:52:20 +08:00
{
var item = _viewModel.SelectedCustomPluginHotkey;
2015-01-02 16:16:09 +08:00
if (item == null)
{
2015-01-21 23:00:56 +08:00
MessageBox.Show(InternationalizationManager.Instance.GetTranslation("pleaseSelectAnItem"));
2015-01-02 16:16:09 +08:00
return;
}
2016-05-09 09:41:46 +08:00
string deleteWarning =
string.Format(InternationalizationManager.Instance.GetTranslation("deleteCustomHotkeyWarning"),
item.Hotkey);
if (
MessageBox.Show(deleteWarning, InternationalizationManager.Instance.GetTranslation("delete"),
MessageBoxButton.YesNo) == MessageBoxResult.Yes)
2014-02-22 15:52:20 +08:00
{
_settings.CustomPluginHotkeys.Remove(item);
2016-02-12 15:22:01 +08:00
RemoveHotkey(item.Hotkey);
2014-02-22 15:52:20 +08:00
}
}
private void OnnEditCustomHotkeyClick(object sender, RoutedEventArgs e)
2014-02-22 15:52:20 +08:00
{
var item = _viewModel.SelectedCustomPluginHotkey;
2014-02-22 15:52:20 +08:00
if (item != null)
{
CustomQueryHotkeySetting window = new CustomQueryHotkeySetting(this, _settings);
2014-02-22 15:52:20 +08:00
window.UpdateItem(item);
window.ShowDialog();
}
else
{
2015-01-21 23:00:56 +08:00
MessageBox.Show(InternationalizationManager.Instance.GetTranslation("pleaseSelectAnItem"));
2014-02-22 15:52:20 +08:00
}
}
private void OnAddCustomeHotkeyClick(object sender, RoutedEventArgs e)
2014-02-22 15:52:20 +08:00
{
new CustomQueryHotkeySetting(this, _settings).ShowDialog();
2014-02-22 15:52:20 +08:00
}
#endregion
2015-02-07 21:27:48 +08:00
#region Plugin
2014-03-26 17:34:19 +08:00
private void OnPluginToggled(object sender, RoutedEventArgs e)
{
var id = _viewModel.SelectedPlugin.PluginPair.Metadata.ID;
_settings.PluginSettings.Plugins[id].Disabled = _viewModel.SelectedPlugin.PluginPair.Metadata.Disabled;
}
private void OnPluginActionKeywordsClick(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left)
{
var id = _viewModel.SelectedPlugin.PluginPair.Metadata.ID;
ActionKeywords changeKeywordsWindow = new ActionKeywords(id, _settings);
changeKeywordsWindow.ShowDialog();
}
}
2014-07-04 15:12:22 +08:00
private void OnPluginNameClick(object sender, MouseButtonEventArgs e)
2014-07-04 15:12:22 +08:00
{
if (e.ChangedButton == MouseButton.Left)
2014-07-04 15:12:22 +08:00
{
2016-07-30 23:16:28 +08:00
var website = _viewModel.SelectedPlugin.PluginPair.Metadata.Website;
if (!string.IsNullOrEmpty(website))
2014-07-04 15:12:22 +08:00
{
2016-07-30 23:16:28 +08:00
var uri = new Uri(website);
if (Uri.CheckSchemeName(uri.Scheme))
2014-07-04 15:12:22 +08:00
{
2016-07-30 23:16:28 +08:00
Process.Start(website);
2014-07-04 15:12:22 +08:00
}
}
}
}
private void OnPluginDirecotyClick(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left)
{
var directory = _viewModel.SelectedPlugin.PluginPair.Metadata.PluginDirectory;
if (!string.IsNullOrEmpty(directory) && Directory.Exists(directory))
{
Process.Start(directory);
}
}
}
2015-02-07 21:27:48 +08:00
#endregion
#region Proxy
2016-05-09 09:41:46 +08:00
2016-05-23 05:49:41 +08:00
private void OnTestProxyClick(object sender, RoutedEventArgs e)
2014-07-18 20:00:55 +08:00
{
2016-06-19 23:18:43 +08:00
if (string.IsNullOrEmpty(_settings.Proxy.Server))
2014-07-18 20:00:55 +08:00
{
2015-01-21 23:00:56 +08:00
MessageBox.Show(InternationalizationManager.Instance.GetTranslation("serverCantBeEmpty"));
2014-07-18 20:00:55 +08:00
return;
}
2016-06-19 23:18:43 +08:00
if (_settings.Proxy.Port <= 0)
2014-07-18 20:00:55 +08:00
{
2015-01-21 23:00:56 +08:00
MessageBox.Show(InternationalizationManager.Instance.GetTranslation("portCantBeEmpty"));
2014-07-18 20:00:55 +08:00
return;
}
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Infrastructure.Constant.Repository);
2016-06-19 23:18:43 +08:00
if (string.IsNullOrEmpty(_settings.Proxy.UserName) || string.IsNullOrEmpty(_settings.Proxy.Password))
2014-07-18 20:00:55 +08:00
{
2016-06-19 23:18:43 +08:00
request.Proxy = new WebProxy(_settings.Proxy.Server, _settings.Proxy.Port);
2014-07-18 20:00:55 +08:00
}
else
{
2016-06-19 23:18:43 +08:00
request.Proxy = new WebProxy(_settings.Proxy.Server, _settings.Proxy.Port)
2016-05-23 05:49:41 +08:00
{
2016-06-19 23:18:43 +08:00
Credentials = new NetworkCredential(_settings.Proxy.UserName, _settings.Proxy.Password)
2016-05-23 05:49:41 +08:00
};
2014-07-18 20:00:55 +08:00
}
try
{
2016-05-23 05:49:41 +08:00
var response = (HttpWebResponse)request.GetResponse();
2014-07-18 20:00:55 +08:00
if (response.StatusCode == HttpStatusCode.OK)
{
2015-01-21 23:00:56 +08:00
MessageBox.Show(InternationalizationManager.Instance.GetTranslation("proxyIsCorrect"));
2014-07-18 20:00:55 +08:00
}
else
{
2015-01-21 23:00:56 +08:00
MessageBox.Show(InternationalizationManager.Instance.GetTranslation("proxyConnectFailed"));
2014-07-18 20:00:55 +08:00
}
}
catch
{
2015-01-21 23:00:56 +08:00
MessageBox.Show(InternationalizationManager.Instance.GetTranslation("proxyConnectFailed"));
2014-07-18 20:00:55 +08:00
}
}
2014-12-16 22:25:22 +08:00
2015-02-07 21:27:48 +08:00
#endregion
private async void OnCheckUpdates(object sender, RoutedEventArgs e)
2016-05-09 09:41:46 +08:00
{
2017-03-06 09:54:06 +08:00
await Updater.UpdateApp();
2016-05-09 09:41:46 +08:00
}
private void OnRequestNavigate(object sender, RequestNavigateEventArgs e)
{
Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
e.Handled = true;
}
2016-05-23 07:36:48 +08:00
private void OnClosed(object sender, EventArgs e)
{
_viewModel.Save();
PluginManager.Save();
2016-05-23 07:36:48 +08:00
}
private void OnCloseExecuted(object sender, ExecutedRoutedEventArgs e)
{
Close();
}
}
}