PowerToys/Wox.Plugin.SystemPlugins/Program/ProgramSourceSetting.xaml.cs

120 lines
4.4 KiB
C#
Raw Normal View History

2014-03-19 20:16:20 +08:00
using System;
2014-03-29 14:42:43 +08:00
using System.ComponentModel;
2014-03-19 20:16:20 +08:00
using System.Linq;
using System.Windows;
using System.Windows.Controls;
2014-03-23 16:17:41 +08:00
using Wox.Infrastructure.Storage.UserSettings;
2014-03-19 20:16:20 +08:00
using MessageBox = System.Windows.MessageBox;
2014-03-30 11:16:44 +08:00
namespace Wox.Plugin.SystemPlugins.Program
2014-03-19 20:16:20 +08:00
{
public partial class ProgramSourceSetting : Window
{
2014-03-29 14:42:43 +08:00
private ProgramSetting settingWindow;
2014-03-19 20:16:20 +08:00
private bool update;
private ProgramSource updateProgramSource;
2014-03-29 14:42:43 +08:00
public ProgramSourceSetting(ProgramSetting settingWidow)
2014-03-19 20:16:20 +08:00
{
2014-03-20 02:26:00 +08:00
this.settingWindow = settingWidow;
2014-03-19 20:16:20 +08:00
InitializeComponent();
2014-03-30 11:16:44 +08:00
this.cbType.ItemsSource = Programs.SourceTypes.Select(o => o.Key).ToList();
2014-03-19 20:16:20 +08:00
}
public void UpdateItem(ProgramSource programSource)
{
2014-03-23 16:17:41 +08:00
updateProgramSource = UserSettingStorage.Instance.ProgramSources.FirstOrDefault(o => o == programSource);
2014-03-19 20:16:20 +08:00
if (updateProgramSource == null)
{
MessageBox.Show("Invalid program source");
Close();
return;
}
update = true;
lblAdd.Text = "Update";
cbEnable.IsChecked = programSource.Enabled;
cbType.SelectedItem = programSource.Type;
cbType.IsEnabled = false;
tbLocation.Text = programSource.Location;
tbBonusPoints.Text = programSource.BonusPoints.ToString();
}
private void BtnCancel_OnClick(object sender, RoutedEventArgs e)
{
Close();
}
private void btnAdd_OnClick(object sender, RoutedEventArgs e)
{
string location = tbLocation.Text;
if (this.tbLocation.IsEnabled == true && string.IsNullOrEmpty(location))
{
MessageBox.Show("Please input Type field");
return;
}
string type = cbType.SelectedItem as string;
if (string.IsNullOrEmpty(type))
{
MessageBox.Show("Please input Type field");
return;
}
int bonusPoint = 0;
int.TryParse(this.tbBonusPoints.Text, out bonusPoint);
if (!update)
{
ProgramSource p = new ProgramSource()
{
Location = this.tbLocation.IsEnabled ? location : null,
Enabled = cbEnable.IsChecked ?? false,
Type = type,
BonusPoints = bonusPoint
};
2014-03-23 16:17:41 +08:00
if (UserSettingStorage.Instance.ProgramSources.Exists(o => o.ToString() == p.ToString() && o != p))
2014-03-19 20:16:20 +08:00
{
MessageBox.Show("Program source already exists!");
return;
}
2014-03-23 16:17:41 +08:00
UserSettingStorage.Instance.ProgramSources.Add(p);
2014-03-19 20:16:20 +08:00
MessageBox.Show(string.Format("Add {0} program source successfully!", p.ToString()));
}
else
{
2014-03-23 16:17:41 +08:00
if (UserSettingStorage.Instance.ProgramSources.Exists(o => o.ToString() == updateProgramSource.ToString() && o != updateProgramSource))
2014-03-19 20:16:20 +08:00
{
MessageBox.Show("Program source already exists!");
return;
}
updateProgramSource.Location = this.tbLocation.IsEnabled ? location : null;
updateProgramSource.Type = type;
updateProgramSource.Enabled = cbEnable.IsChecked ?? false;
updateProgramSource.BonusPoints = bonusPoint;
MessageBox.Show(string.Format("Update {0} program source successfully!", updateProgramSource.ToString()));
}
2014-03-23 16:17:41 +08:00
UserSettingStorage.Instance.Save();
2014-03-20 02:26:00 +08:00
settingWindow.ReloadProgramSourceView();
2014-03-19 20:16:20 +08:00
Close();
}
private void cbType_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string item = cbType.SelectedItem as String;
Type type;
2014-03-30 11:16:44 +08:00
if (item != null && Programs.SourceTypes.TryGetValue(item, out type))
2014-03-19 20:16:20 +08:00
{
2014-03-29 14:42:43 +08:00
var attrs = type.GetCustomAttributes(typeof(BrowsableAttribute), false);
if (attrs.Length > 0 && (attrs[0] as BrowsableAttribute).Browsable == false)
2014-03-19 20:16:20 +08:00
{
this.tbLocation.IsEnabled = false;
return;
}
}
this.tbLocation.IsEnabled = true;
}
}
}