System Plugin File Structure change.

This commit is contained in:
qianlifeng
2014-03-30 11:16:44 +08:00
parent ab6a29191d
commit 79b3b0fffc
18 changed files with 41 additions and 95 deletions

View File

@@ -0,0 +1,109 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Newtonsoft.Json;
using Wox.Infrastructure;
using Wox.Infrastructure.Storage;
using Wox.Infrastructure.Storage.UserSettings;
using Wox.Plugin.SystemPlugins.SuggestionSources;
namespace Wox.Plugin.SystemPlugins
{
public class WebSearchPlugin : BaseSystemPlugin, ISettingProvider
{
private PluginInitContext context;
protected override List<Result> QueryInternal(Query query)
{
List<Result> results = new List<Result>();
if (string.IsNullOrEmpty(query.ActionName)) return results;
WebSearch webSearch =
UserSettingStorage.Instance.WebSearches.FirstOrDefault(o => o.ActionWord == query.ActionName && o.Enabled);
if (webSearch != null)
{
string keyword = query.ActionParameters.Count > 0 ? query.GetAllRemainingParameter() : "";
string title = keyword;
string subtitle = "Search " + webSearch.Title;
if (string.IsNullOrEmpty(keyword))
{
title = subtitle;
subtitle = null;
}
context.PushResults(query, new List<Result>()
{
new Result()
{
Title = title,
SubTitle = subtitle,
Score = 6,
IcoPath = webSearch.IconPath,
Action = (c) =>
{
Process.Start(webSearch.Url.Replace("{q}", keyword));
return true;
}
}
});
if (!string.IsNullOrEmpty(keyword))
{
ISuggestionSource sugg = new Google();
var result = sugg.GetSuggestions(keyword);
if (result != null)
{
context.PushResults(query, result.Select(o => new Result()
{
Title = o,
SubTitle = subtitle,
Score = 5,
IcoPath = webSearch.IconPath,
Action = (c) =>
{
Process.Start(webSearch.Url.Replace("{q}", o));
return true;
}
}).ToList());
}
}
}
return results;
}
protected override void InitInternal(PluginInitContext context)
{
this.context = context;
if (UserSettingStorage.Instance.WebSearches == null)
UserSettingStorage.Instance.WebSearches = UserSettingStorage.Instance.LoadDefaultWebSearches();
}
public override string Name
{
get { return "Web Searches"; }
}
public override string IcoPath
{
get { return @"Images\app.png"; }
}
public override string Description
{
get { return base.Description; }
}
#region ISettingProvider Members
public System.Windows.Controls.Control CreateSettingPanel()
{
return new WebSearchesSetting();
}
#endregion
}
}

View File

@@ -0,0 +1,43 @@
<Window x:Class="Wox.Plugin.SystemPlugins.WebSearchSetting"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
ResizeMode="NoResize"
WindowStartupLocation="CenterScreen"
Title="WebSearchSetting" Height="350" Width="674.766">
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition Height="60"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="120"></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Margin="10" FontSize="14" Grid.Row="0" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Right">Title:</TextBlock>
<TextBox x:Name="tbTitle" Margin="10" Grid.Row="0" Width="400" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Left"></TextBox>
<TextBlock Margin="10" FontSize="14" Grid.Row="1" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Right">URL:</TextBlock>
<TextBox x:Name="tbUrl" Margin="10" Grid.Row="1" Width="400" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Left"></TextBox>
<TextBlock Margin="10" FontSize="14" Grid.Row="2" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Right">ActionWord:</TextBlock>
<TextBox x:Name="tbActionword" Margin="10" Grid.Row="2" Width="400" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Left"></TextBox>
<TextBlock Margin="10" FontSize="14" Grid.Row="3" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Right">Enable:</TextBlock>
<CheckBox x:Name="cbEnable" IsChecked="True" Margin="10" Grid.Row="3" Grid.Column="1" VerticalAlignment="Center"></CheckBox>
<TextBlock Margin="10" FontSize="14" Grid.Row="4" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Right">Icon:</TextBlock>
<StackPanel Orientation="Horizontal" Grid.Row="4" Grid.Column="1" Margin="10">
<Image x:Name="imgIcon" Width="24" Height="24" Margin="0 0 10 0" />
<Button x:Name="btnSelectIcon" Height="24" Click="BtnSelectIcon_OnClick">Select Icon</Button>
<TextBlock x:Name="tbIconPath" Visibility="Hidden"></TextBlock>
</StackPanel>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Grid.Row="5" Grid.Column="1">
<Button x:Name="btnCancel" Click="BtnCancel_OnClick" Margin="10 0 10 0" Width="80" Height="25">Cancel</Button>
<Button x:Name="btnAdd" Margin="10 0 10 0" Width="80" Height="25" Click="btnAdd_OnClick"><TextBlock x:Name="lblAdd">Add</TextBlock></Button>
</StackPanel>
</Grid>
</Window>

View File

@@ -0,0 +1,147 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Wox.Infrastructure;
using Wox.Infrastructure.Storage;
using Wox.Infrastructure.Storage.UserSettings;
using MessageBox = System.Windows.MessageBox;
namespace Wox.Plugin.SystemPlugins
{
public partial class WebSearchSetting : Window
{
private WebSearchesSetting settingWindow;
private bool update;
private WebSearch updateWebSearch;
public WebSearchSetting(WebSearchesSetting settingWidow)
{
this.settingWindow = settingWidow;
InitializeComponent();
}
public void UpdateItem(WebSearch webSearch)
{
updateWebSearch = UserSettingStorage.Instance.WebSearches.FirstOrDefault(o => o == webSearch);
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);
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;
}
if (!update)
{
if (UserSettingStorage.Instance.WebSearches.Exists(o => o.ActionWord == action))
{
MessageBox.Show("ActionWord has existed, please input a new one.");
return;
}
UserSettingStorage.Instance.WebSearches.Add(new WebSearch()
{
ActionWord = action,
Enabled = cbEnable.IsChecked ?? false,
IconPath = tbIconPath.Text,
Url = url,
Title = title
});
MessageBox.Show(string.Format("Add {0} web search successfully!", title));
}
else
{
if (UserSettingStorage.Instance.WebSearches.Exists(o => o.ActionWord == action && o != updateWebSearch))
{
MessageBox.Show("ActionWord has existed, please input a new one.");
return;
}
updateWebSearch.ActionWord = action;
updateWebSearch.IconPath = tbIconPath.Text;
updateWebSearch.Enabled = cbEnable.IsChecked ?? false;
updateWebSearch.Url = url;
updateWebSearch.Title= title;
MessageBox.Show(string.Format("Update {0} web search successfully!", title));
}
UserSettingStorage.Instance.Save();
settingWindow.ReloadWebSearchView();
Close();
}
private void BtnSelectIcon_OnClick(object sender, RoutedEventArgs e)
{
var dlg = new Microsoft.Win32.OpenFileDialog
{
DefaultExt = ".png",
Filter =
"JPEG Files (*.jpeg)|*.jpeg|PNG Files (*.png)|*.png|JPG Files (*.jpg)|*.jpg|GIF Files (*.gif)|*.gif"
};
bool? result = dlg.ShowDialog();
if (result == true)
{
string filename = dlg.FileName;
tbIconPath.Text = filename;
ShowIcon(filename);
}
}
}
}

View File

@@ -0,0 +1,39 @@
<UserControl x:Class="Wox.Plugin.SystemPlugins.WebSearchesSetting"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid Margin="10">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="50"/>
</Grid.RowDefinitions>
<ListView x:Name="webSearchView" Grid.Row="0">
<ListView.View>
<GridView>
<GridViewColumn Header="ActionWord" Width="180">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding ActionWord}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Url" Width="500">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Url}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
<StackPanel Grid.Row="1" HorizontalAlignment="Right" Orientation="Horizontal">
<Button x:Name="btnDeleteWebSearch" Click="btnDeleteWebSearch_OnClick" Width="100" Margin="10" Content="Delete"/>
<Button x:Name="btnEditWebSearch" Click="btnEditWebSearch_OnClick" Width="100" Margin="10" Content="Edit"/>
<Button x:Name="btnAddWebSearch" Click="btnAddWebSearch_OnClick" Width="100" Margin="10" Content="Add"/>
</StackPanel>
</Grid>
</UserControl>

View File

@@ -0,0 +1,79 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Wox.Infrastructure.Storage.UserSettings;
namespace Wox.Plugin.SystemPlugins
{
/// <summary>
/// Interaction logic for WebSearchesSetting.xaml
/// </summary>
public partial class WebSearchesSetting : UserControl
{
public WebSearchesSetting()
{
InitializeComponent();
Loaded += Setting_Loaded;
}
private void Setting_Loaded(object sender, RoutedEventArgs e)
{
webSearchView.ItemsSource = UserSettingStorage.Instance.WebSearches;
}
public void ReloadWebSearchView()
{
webSearchView.Items.Refresh();
}
private void btnAddWebSearch_OnClick(object sender, RoutedEventArgs e)
{
WebSearchSetting webSearch = new WebSearchSetting(this);
webSearch.ShowDialog();
}
private void btnDeleteWebSearch_OnClick(object sender, RoutedEventArgs e)
{
WebSearch seletedWebSearch = webSearchView.SelectedItem as WebSearch;
if (seletedWebSearch != null &&
MessageBox.Show("Are your sure to delete " + seletedWebSearch.Title, "Delete WebSearch",
MessageBoxButton.YesNo) == MessageBoxResult.Yes)
{
UserSettingStorage.Instance.WebSearches.Remove(seletedWebSearch);
webSearchView.Items.Refresh();
}
else
{
MessageBox.Show("Please select a web search");
}
}
private void btnEditWebSearch_OnClick(object sender, RoutedEventArgs e)
{
WebSearch seletedWebSearch = webSearchView.SelectedItem as WebSearch;
if (seletedWebSearch != null)
{
WebSearchSetting webSearch = new WebSearchSetting(this);
webSearch.UpdateItem(seletedWebSearch);
webSearch.ShowDialog();
}
else
{
MessageBox.Show("Please select a web search");
}
}
}
}