diff --git a/Plugins/Wox.Plugin.WebSearch/Languages/de.xaml b/Plugins/Wox.Plugin.WebSearch/Languages/de.xaml index 1ff21cba4a..9cc70038b5 100644 --- a/Plugins/Wox.Plugin.WebSearch/Languages/de.xaml +++ b/Plugins/Wox.Plugin.WebSearch/Languages/de.xaml @@ -5,6 +5,7 @@ Löschen Bearbeiten Hinzufügen + Confirm Aktionsschlüsselwort URL Suche @@ -12,7 +13,7 @@ Bitte wähle einen Suchdienst Willst du wirklich {0} löschen? - + Titel Aktivieren diff --git a/Plugins/Wox.Plugin.WebSearch/Languages/en.xaml b/Plugins/Wox.Plugin.WebSearch/Languages/en.xaml index cdfc44cb39..f3d8135ca3 100644 --- a/Plugins/Wox.Plugin.WebSearch/Languages/en.xaml +++ b/Plugins/Wox.Plugin.WebSearch/Languages/en.xaml @@ -5,13 +5,14 @@ Delete Edit Add + Confirm Action Keyword URL Search Enable search suggestions Please select a web search Are you sure you want to delete {0}? - + Title Enable diff --git a/Plugins/Wox.Plugin.WebSearch/Languages/pl.xaml b/Plugins/Wox.Plugin.WebSearch/Languages/pl.xaml index f3bb831aca..fb6acf9410 100644 --- a/Plugins/Wox.Plugin.WebSearch/Languages/pl.xaml +++ b/Plugins/Wox.Plugin.WebSearch/Languages/pl.xaml @@ -5,13 +5,14 @@ Usuń Edytuj Dodaj + Confirm Wyzwalacz Adres URL Szukaj Pokazuj podpowiedzi wyszukiwania Musisz wybrać coś z listy Czy jesteś pewnie że chcesz usunąć {0}? - + Tytuł Aktywne diff --git a/Plugins/Wox.Plugin.WebSearch/Languages/zh-cn.xaml b/Plugins/Wox.Plugin.WebSearch/Languages/zh-cn.xaml index 8800481360..d580fb0f51 100644 --- a/Plugins/Wox.Plugin.WebSearch/Languages/zh-cn.xaml +++ b/Plugins/Wox.Plugin.WebSearch/Languages/zh-cn.xaml @@ -5,6 +5,7 @@ 删除 编辑 添加 + 确认 触发关键字 URL 搜索 diff --git a/Plugins/Wox.Plugin.WebSearch/Languages/zh-tw.xaml b/Plugins/Wox.Plugin.WebSearch/Languages/zh-tw.xaml index 9e665167c0..b0882d487c 100644 --- a/Plugins/Wox.Plugin.WebSearch/Languages/zh-tw.xaml +++ b/Plugins/Wox.Plugin.WebSearch/Languages/zh-tw.xaml @@ -5,6 +5,7 @@ 刪除 編輯 添加 + 确认 觸發關鍵字 URL 搜索 @@ -28,5 +29,5 @@ 網頁搜索 提供網頁搜索能力 - + \ No newline at end of file diff --git a/Plugins/Wox.Plugin.WebSearch/Main.cs b/Plugins/Wox.Plugin.WebSearch/Main.cs index 43f92fad15..ca3bedef0c 100644 --- a/Plugins/Wox.Plugin.WebSearch/Main.cs +++ b/Plugins/Wox.Plugin.WebSearch/Main.cs @@ -6,28 +6,26 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; using System.Windows.Controls; -using JetBrains.Annotations; using Wox.Infrastructure; using Wox.Infrastructure.Storage; -using Wox.Plugin.WebSearch.SuggestionSources; namespace Wox.Plugin.WebSearch { - public class Main : IPlugin, ISettingProvider, IPluginI18n, IMultipleActionKeywords, ISavable, IResultUpdated + public class Main : IPlugin, ISettingProvider, IPluginI18n, ISavable, IResultUpdated { - public PluginInitContext Context { get; private set; } + private PluginInitContext _context; - private PluginJsonStorage _storage; - private Settings _settings; + private readonly Settings _settings; + private readonly SettingsViewModel _viewModel; private CancellationTokenSource _updateSource; private CancellationToken _updateToken; public const string Images = "Images"; - public static string ImagesDirectory; + public static readonly string ImagesDirectory; public void Save() { - _storage.Save(); + _viewModel.Save(); } public List Query(Query query) @@ -36,21 +34,21 @@ namespace Wox.Plugin.WebSearch _updateSource = new CancellationTokenSource(); _updateToken = _updateSource.Token; - WebSearch webSearch = - _settings.WebSearches.FirstOrDefault(o => o.ActionKeyword == query.ActionKeyword && o.Enabled); + SearchSource searchSource = + _settings.SearchSources.FirstOrDefault(o => o.ActionKeyword == query.ActionKeyword && o.Enabled); - if (webSearch != null) + if (searchSource != null) { string keyword = query.Search; string title = keyword; - string subtitle = Context.API.GetTranslation("wox_plugin_websearch_search") + " " + webSearch.Title; + string subtitle = _context.API.GetTranslation("wox_plugin_websearch_search") + " " + searchSource.Title; if (string.IsNullOrEmpty(keyword)) { var result = new Result { Title = subtitle, SubTitle = string.Empty, - IcoPath = webSearch.IconPath + IcoPath = searchSource.IconPath }; return new List { result }; } @@ -62,15 +60,15 @@ namespace Wox.Plugin.WebSearch Title = title, SubTitle = subtitle, Score = 6, - IcoPath = webSearch.IconPath, + IcoPath = searchSource.IconPath, Action = c => { - Process.Start(webSearch.Url.Replace("{q}", Uri.EscapeDataString(keyword))); + Process.Start(searchSource.Url.Replace("{q}", Uri.EscapeDataString(keyword))); return true; } }; results.Add(result); - UpdateResultsFromSuggestion(results, keyword, subtitle, webSearch, query); + UpdateResultsFromSuggestion(results, keyword, subtitle, searchSource, query); return results; } } @@ -80,14 +78,15 @@ namespace Wox.Plugin.WebSearch } } - private void UpdateResultsFromSuggestion(List results, string keyword, string subtitle, WebSearch webSearch, Query query) + private void UpdateResultsFromSuggestion(List results, string keyword, string subtitle, + SearchSource searchSource, Query query) { - if (_settings.EnableWebSearchSuggestion) + if (_settings.EnableSuggestion) { const int waittime = 300; var task = Task.Run(async () => { - var suggestions = await Suggestions(keyword, subtitle, webSearch); + var suggestions = await Suggestions(keyword, subtitle, searchSource); results.AddRange(suggestions); }, _updateToken); @@ -102,21 +101,21 @@ namespace Wox.Plugin.WebSearch } } - private async Task> Suggestions(string keyword, string subtitle, WebSearch webSearch) + private async Task> Suggestions(string keyword, string subtitle, SearchSource searchSource) { - var source = SuggestionSource.GetSuggestionSource(_settings.WebSearchSuggestionSource); + var source = _settings.SelectedSuggestion; if (source != null) { - var suggestions = await source.GetSuggestions(keyword); + var suggestions = await source.Suggestions(keyword); var resultsFromSuggestion = suggestions.Select(o => new Result { Title = o, SubTitle = subtitle, Score = 5, - IcoPath = webSearch.IconPath, + IcoPath = searchSource.IconPath, Action = c => { - Process.Start(webSearch.Url.Replace("{q}", Uri.EscapeDataString(o))); + Process.Start(searchSource.Url.Replace("{q}", Uri.EscapeDataString(o))); return true; } }); @@ -127,18 +126,21 @@ namespace Wox.Plugin.WebSearch static Main() { - var plugins = Infrastructure.Constant.Plugins; + var plugins = Constant.Plugins; var assemblyName = typeof(Main).Assembly.GetName().Name; - var pluginDirectory = Path.Combine(Infrastructure.Constant.SettingsPath, plugins, assemblyName); + var pluginDirectory = Path.Combine(Constant.SettingsPath, plugins, assemblyName); ImagesDirectory = Path.Combine(pluginDirectory, Images); } + public Main() + { + _viewModel = new SettingsViewModel(); + _settings = _viewModel.Settings; + } + public void Init(PluginInitContext context) { - Context = context; - - _storage = new PluginJsonStorage(); - _settings = _storage.Load(); + _context = context; var pluginDirectory = context.CurrentPluginMetadata.PluginDirectory; var bundledImagesDirectory = Path.Combine(pluginDirectory, Images); @@ -149,43 +151,21 @@ namespace Wox.Plugin.WebSearch public Control CreateSettingPanel() { - return new WebSearchesSetting(this, _settings); + return new SettingsControl(_context, _viewModel); } #endregion public string GetTranslatedPluginTitle() { - return Context.API.GetTranslation("wox_plugin_websearch_plugin_name"); + return _context.API.GetTranslation("wox_plugin_websearch_plugin_name"); } public string GetTranslatedPluginDescription() { - return Context.API.GetTranslation("wox_plugin_websearch_plugin_description"); + return _context.API.GetTranslation("wox_plugin_websearch_plugin_description"); } - public bool IsInstantQuery(string query) => false; - - [NotifyPropertyChangedInvocator] - public void NotifyActionKeywordsUpdated(string oldActionKeywords, string newActionKeywords) - { - ActionKeywordsChanged?.Invoke(this, new ActionKeywordsChangedEventArgs - { - OldActionKeyword = oldActionKeywords, - NewActionKeyword = newActionKeywords - }); - } - - [NotifyPropertyChangedInvocator] - public void NotifyActionKeywordsAdded(string newActionKeywords) - { - ActionKeywordsChanged?.Invoke(this, new ActionKeywordsChangedEventArgs - { - NewActionKeyword = newActionKeywords - }); - } - - public event ActionKeywordsChangedEventHandler ActionKeywordsChanged; public event ResultUpdatedEventHandler ResultsUpdated; } -} +} \ No newline at end of file diff --git a/Plugins/Wox.Plugin.WebSearch/SearchSourceViewModel.cs b/Plugins/Wox.Plugin.WebSearch/SearchSourceViewModel.cs new file mode 100644 index 0000000000..6b343ae8fd --- /dev/null +++ b/Plugins/Wox.Plugin.WebSearch/SearchSourceViewModel.cs @@ -0,0 +1,7 @@ +namespace Wox.Plugin.WebSearch +{ + public class SearchSourceViewModel + { + public SearchSource SearchSource { get; set; } + } +} diff --git a/Plugins/Wox.Plugin.WebSearch/Settings.cs b/Plugins/Wox.Plugin.WebSearch/Settings.cs index aebec99c2e..ef5de0aed9 100644 --- a/Plugins/Wox.Plugin.WebSearch/Settings.cs +++ b/Plugins/Wox.Plugin.WebSearch/Settings.cs @@ -1,12 +1,23 @@ -using System.Collections.Generic; +using System; +using System.Collections.ObjectModel; +using Newtonsoft.Json; +using Wox.Plugin.WebSearch.SuggestionSources; namespace Wox.Plugin.WebSearch { - public class Settings + public class Settings : BaseModel { - public List WebSearches { get; set; } = new List + public Settings() + { + SelectedSuggestion = Suggestions[0]; + if (SearchSources.Count > 0) { - new WebSearch + SelectedSearchSource = SearchSources[0]; + } + } + public ObservableCollection SearchSources { get; set; } = new ObservableCollection + { + new SearchSource { Title = "Google", ActionKeyword = "g", @@ -14,7 +25,7 @@ namespace Wox.Plugin.WebSearch Url = "https://www.google.com/search?q={q}", Enabled = true }, - new WebSearch + new SearchSource { Title = "Google Scholar", ActionKeyword = "sc", @@ -22,7 +33,7 @@ namespace Wox.Plugin.WebSearch Url = "https://scholar.google.com/scholar?q={q}", Enabled = true }, - new WebSearch + new SearchSource { Title = "Wikipedia", ActionKeyword = "wiki", @@ -30,7 +41,7 @@ namespace Wox.Plugin.WebSearch Url = "https://en.wikipedia.org/wiki/{q}", Enabled = true }, - new WebSearch + new SearchSource { Title = "FindIcon", ActionKeyword = "findicon", @@ -38,7 +49,7 @@ namespace Wox.Plugin.WebSearch Url = "http://findicons.com/search/{q}", Enabled = true }, - new WebSearch + new SearchSource { Title = "Facebook", ActionKeyword = "facebook", @@ -46,7 +57,7 @@ namespace Wox.Plugin.WebSearch Url = "https://www.facebook.com/search/?q={q}", Enabled = true }, - new WebSearch + new SearchSource { Title = "Twitter", ActionKeyword = "twitter", @@ -54,7 +65,7 @@ namespace Wox.Plugin.WebSearch Url = "https://twitter.com/search?q={q}", Enabled = true }, - new WebSearch + new SearchSource { Title = "Google Maps", ActionKeyword = "maps", @@ -62,7 +73,7 @@ namespace Wox.Plugin.WebSearch Url = "https://maps.google.com/maps?q={q}", Enabled = true }, - new WebSearch + new SearchSource { Title = "Google Translate", ActionKeyword = "translate", @@ -70,7 +81,7 @@ namespace Wox.Plugin.WebSearch Url = "https://translate.google.com/#auto|en|{q}", Enabled = true }, - new WebSearch + new SearchSource { Title = "Duckduckgo", ActionKeyword = "duckduckgo", @@ -78,7 +89,7 @@ namespace Wox.Plugin.WebSearch Url = "https://duckduckgo.com/?q={q}", Enabled = true }, - new WebSearch + new SearchSource { Title = "Github", ActionKeyword = "github", @@ -86,7 +97,7 @@ namespace Wox.Plugin.WebSearch Url = "https://github.com/search?q={q}", Enabled = true }, - new WebSearch + new SearchSource { Title = "Github Gist", ActionKeyword = "gist", @@ -94,7 +105,7 @@ namespace Wox.Plugin.WebSearch Url = "https://gist.github.com/search?q={q}", Enabled = true }, - new WebSearch + new SearchSource { Title = "Gmail", ActionKeyword = "gmail", @@ -102,7 +113,7 @@ namespace Wox.Plugin.WebSearch Url = "https://mail.google.com/mail/ca/u/0/#apps/{q}", Enabled = true }, - new WebSearch + new SearchSource { Title = "Google Drive", ActionKeyword = "drive", @@ -110,7 +121,7 @@ namespace Wox.Plugin.WebSearch Url = "https://drive.google.com/?hl=en&tab=bo#search/{q}", Enabled = true }, - new WebSearch + new SearchSource { Title = "Wolframalpha", ActionKeyword = "wolframalpha", @@ -118,7 +129,7 @@ namespace Wox.Plugin.WebSearch Url = "https://www.wolframalpha.com/input/?i={q}", Enabled = true }, - new WebSearch + new SearchSource { Title = "Stackoverflow", ActionKeyword = "stackoverflow", @@ -126,7 +137,7 @@ namespace Wox.Plugin.WebSearch Url = "https://stackoverflow.com/search?q={q}", Enabled = true }, - new WebSearch + new SearchSource { Title = "I'm Feeling Lucky", ActionKeyword = "lucky", @@ -134,7 +145,7 @@ namespace Wox.Plugin.WebSearch Url = "https://google.com/search?q={q}&btnI=I", Enabled = true }, - new WebSearch + new SearchSource { Title = "Google Image", ActionKeyword = "image", @@ -142,7 +153,7 @@ namespace Wox.Plugin.WebSearch Url = "https://www.google.com/search?q={q}&tbm=isch", Enabled = true }, - new WebSearch + new SearchSource { Title = "Youtube", ActionKeyword = "youtube", @@ -150,7 +161,7 @@ namespace Wox.Plugin.WebSearch Url = "https://www.youtube.com/results?search_query={q}", Enabled = true }, - new WebSearch + new SearchSource { Title = "Bing", ActionKeyword = "bing", @@ -158,7 +169,7 @@ namespace Wox.Plugin.WebSearch Url = "https://www.bing.com/search?q={q}", Enabled = true }, - new WebSearch + new SearchSource { Title = "Yahoo", ActionKeyword = "yahoo", @@ -166,18 +177,50 @@ namespace Wox.Plugin.WebSearch Url = "https://www.search.yahoo.com/search?p={q}", Enabled = true }, - new WebSearch + new SearchSource { - Title= "Baidu", - ActionKeyword= "bd", - Icon= "baidu.png", - Url="https://www.baidu.com/#ie=UTF-8&wd={q}", - Enabled= true + Title = "Baidu", + ActionKeyword = "bd", + Icon = "baidu.png", + Url = "https://www.baidu.com/#ie=UTF-8&wd={q}", + Enabled = true } }; - public bool EnableWebSearchSuggestion { get; set; } + [JsonIgnore] + public SearchSource SelectedSearchSource { get; set; } + + public bool EnableSuggestion { get; set; } + + [JsonIgnore] + public SuggestionSource[] Suggestions { get; set; } = { + new Google(), + new Baidu() + }; + + [JsonIgnore] + public SuggestionSource SelectedSuggestion { get; set; } + + /// + /// used to store Settings.json only + /// + public string Suggestion + { + get + { + return SelectedSuggestion.ToString(); + } + set + { + foreach (var s in Suggestions) + { + if (string.Equals(s.ToString(), value, StringComparison.OrdinalIgnoreCase)) + { + SelectedSuggestion = s; + } + } + } + } - public string WebSearchSuggestionSource { get; set; } } } \ No newline at end of file diff --git a/Plugins/Wox.Plugin.WebSearch/SettingsViewModel.cs b/Plugins/Wox.Plugin.WebSearch/SettingsViewModel.cs new file mode 100644 index 0000000000..6a727a5ddb --- /dev/null +++ b/Plugins/Wox.Plugin.WebSearch/SettingsViewModel.cs @@ -0,0 +1,22 @@ +using Wox.Infrastructure.Storage; + +namespace Wox.Plugin.WebSearch +{ + public class SettingsViewModel + { + private readonly JsonStrorage _storage; + + public SettingsViewModel() + { + _storage = new PluginJsonStorage(); + Settings = _storage.Load(); + } + + public Settings Settings { get; set; } + + public void Save() + { + _storage.Save(); + } + } +} \ No newline at end of file diff --git a/Plugins/Wox.Plugin.WebSearch/SuggestionSources/Baidu.cs b/Plugins/Wox.Plugin.WebSearch/SuggestionSources/Baidu.cs index a2a5b62fe2..0c71d7a2b4 100644 --- a/Plugins/Wox.Plugin.WebSearch/SuggestionSources/Baidu.cs +++ b/Plugins/Wox.Plugin.WebSearch/SuggestionSources/Baidu.cs @@ -13,11 +13,9 @@ namespace Wox.Plugin.WebSearch.SuggestionSources { public class Baidu : SuggestionSource { - public override string Domain { get; set; } = "www.baidu.com"; + private readonly Regex _reg = new Regex("window.baidu.sug\\((.*)\\)"); - Regex reg = new Regex("window.baidu.sug\\((.*)\\)"); - - public override async Task> GetSuggestions(string query) + public override async Task> Suggestions(string query) { string result; @@ -30,12 +28,12 @@ namespace Wox.Plugin.WebSearch.SuggestionSources { Log.Warn("Can't get suggestion from baidu"); Log.Exception(e); - return new List(); ; + return new List(); + ; } if (string.IsNullOrEmpty(result)) return new List(); - - Match match = reg.Match(result); + Match match = _reg.Match(result); if (match.Success) { JContainer json; @@ -61,5 +59,10 @@ namespace Wox.Plugin.WebSearch.SuggestionSources return new List(); } + + public override string ToString() + { + return "Baidu"; + } } -} +} \ No newline at end of file diff --git a/Plugins/Wox.Plugin.WebSearch/SuggestionSources/Google.cs b/Plugins/Wox.Plugin.WebSearch/SuggestionSources/Google.cs index 95097f9a65..6d1f9eaa94 100644 --- a/Plugins/Wox.Plugin.WebSearch/SuggestionSources/Google.cs +++ b/Plugins/Wox.Plugin.WebSearch/SuggestionSources/Google.cs @@ -12,8 +12,7 @@ namespace Wox.Plugin.WebSearch.SuggestionSources { public class Google : SuggestionSource { - public override string Domain { get; set; } = "www.google.com"; - public override async Task> GetSuggestions(string query) + public override async Task> Suggestions(string query) { string result; try @@ -25,7 +24,8 @@ namespace Wox.Plugin.WebSearch.SuggestionSources { Log.Warn("Can't get suggestion from google"); Log.Exception(e); - return new List(); ; + return new List(); + ; } if (string.IsNullOrEmpty(result)) return new List(); JContainer json; @@ -48,5 +48,10 @@ namespace Wox.Plugin.WebSearch.SuggestionSources } return new List(); } + + public override string ToString() + { + return "Google"; + } } -} +} \ No newline at end of file diff --git a/Plugins/Wox.Plugin.WebSearch/SuggestionSources/ISuggestionSource.cs b/Plugins/Wox.Plugin.WebSearch/SuggestionSources/ISuggestionSource.cs index f7317b8b04..c56c892418 100644 --- a/Plugins/Wox.Plugin.WebSearch/SuggestionSources/ISuggestionSource.cs +++ b/Plugins/Wox.Plugin.WebSearch/SuggestionSources/ISuggestionSource.cs @@ -3,25 +3,9 @@ using System.Threading.Tasks; namespace Wox.Plugin.WebSearch.SuggestionSources { + //todo rename file public abstract class SuggestionSource { - public virtual string Domain { get; set; } - - public abstract Task> GetSuggestions(string query); - - public static SuggestionSource GetSuggestionSource(string name) - { - switch (name.ToLower()) - { - case "google": - return new Google(); - - case "baidu": - return new Baidu(); - - default: - return null; - } - } + public abstract Task> Suggestions(string query); } -} +} \ No newline at end of file diff --git a/Plugins/Wox.Plugin.WebSearch/WebSearch.cs b/Plugins/Wox.Plugin.WebSearch/WebSearch.cs index 0b645a990e..46cbc47970 100644 --- a/Plugins/Wox.Plugin.WebSearch/WebSearch.cs +++ b/Plugins/Wox.Plugin.WebSearch/WebSearch.cs @@ -1,40 +1,43 @@ using System.IO; +using System.Windows.Media; using JetBrains.Annotations; using Newtonsoft.Json; -using Newtonsoft.Json.Serialization; +using Wox.Infrastructure.Image; namespace Wox.Plugin.WebSearch { - public class WebSearch + public class SearchSource : BaseModel { public const string DefaultIcon = "web_search.png"; public string Title { get; set; } public string ActionKeyword { get; set; } - [NotNull] - private string _icon = DefaultIcon; [NotNull] - public string Icon - { - get { return _icon; } - set - { - _icon = value; - IconPath = Path.Combine(Main.ImagesDirectory, value); - } - } + public string Icon { private get; set; } = DefaultIcon; /// /// All icon should be put under Images directory /// [NotNull] [JsonIgnore] - internal string IconPath { get; private set; } = Path.Combine - ( - Main.ImagesDirectory, DefaultIcon - ); + internal string IconPath => Path.Combine(Main.ImagesDirectory, Icon); + [JsonIgnore] + public ImageSource Image => ImageLoader.Load(IconPath); public string Url { get; set; } public bool Enabled { get; set; } + + public SearchSource DeepCopy() + { + var webSearch = new SearchSource + { + Title = string.Copy(Title), + ActionKeyword = string.Copy(ActionKeyword), + Url = string.Copy(Url), + Icon = string.Copy(Icon), + Enabled = Enabled + }; + return webSearch; + } } } \ No newline at end of file diff --git a/Plugins/Wox.Plugin.WebSearch/WebSearchSetting.xaml b/Plugins/Wox.Plugin.WebSearch/WebSearchSetting.xaml index c83f9ae08a..00c2d13dc9 100644 --- a/Plugins/Wox.Plugin.WebSearch/WebSearchSetting.xaml +++ b/Plugins/Wox.Plugin.WebSearch/WebSearchSetting.xaml @@ -1,42 +1,60 @@ - + Title="Search Source Setting" Height="300" Width="500" + d:DataContext="{d:DesignInstance vm:SearchSourceViewModel}"> - - - - - - + + + + + + - - + + - - + + - - + + - - + + - - + + - + - - + +