public SearchSourceSettingWindow(IList <SearchSource> sources, PluginInitContext context, SearchSource old)
 {
     _oldSearchSource = old;
     _viewModel       = new SearchSourceViewModel {
         SearchSource = old.DeepCopy()
     };
     Initilize(sources, context, Action.Edit);
 }
 private void Initilize(IList <SearchSource> sources, PluginInitContext context, Action action)
 {
     InitializeComponent();
     DataContext    = _viewModel;
     _searchSource  = _viewModel.SearchSource;
     _searchSources = sources;
     _action        = action;
     _context       = context;
     _api           = _context.API;
 }
Beispiel #3
0
        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);
        }
Beispiel #4
0
        public List <Result> Query(Query query)
        {
            _updateSource?.Cancel();
            _updateSource = new CancellationTokenSource();
            _updateToken  = _updateSource.Token;

            SearchSource searchSource =
                _settings.SearchSources.FirstOrDefault(o => o.ActionKeyword == query.ActionKeyword && o.Enabled);

            if (searchSource != null)
            {
                string keyword  = query.Search;
                string title    = keyword;
                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  = searchSource.IconPath
                    };
                    return(new List <Result> {
                        result
                    });
                }
                else
                {
                    var results = new List <Result>();
                    var result  = new Result
                    {
                        Title    = title,
                        SubTitle = subtitle,
                        Score    = 6,
                        IcoPath  = searchSource.IconPath,
                        Action   = c =>
                        {
                            Process.Start(searchSource.Url.Replace("{q}", Uri.EscapeDataString(keyword)));
                            return(true);
                        }
                    };
                    results.Add(result);
                    UpdateResultsFromSuggestion(results, keyword, subtitle, searchSource, query);
                    return(results);
                }
            }
            else
            {
                return(new List <Result>());
            }
        }
Beispiel #5
0
        private void UpdateResultsFromSuggestion(List <Result> results, string keyword, string subtitle,
                                                 SearchSource searchSource, Query query)
        {
            if (_settings.EnableSuggestion)
            {
                const int waittime = 300;
                Task      task     = Task.Run(async() => {
                    IEnumerable <Result> suggestions = await Suggestions(keyword, subtitle, searchSource);
                    results.AddRange(suggestions);
                }, _updateToken);

                if (!task.Wait(waittime))
                {
                    task.ContinueWith(_ => ResultsUpdated?.Invoke(this, new ResultUpdatedEventArgs {
                        Results = results,
                        Query   = query
                    }), _updateToken);
                }
            }
        }
Beispiel #6
0
        private void UpdateResultsFromSuggestion(List<Result> results, string keyword, string subtitle,
            SearchSource searchSource, Query query)
        {
            if (_settings.EnableSuggestion)
            {
                const int waittime = 300;
                var task = Task.Run(async () =>
                {
                    var suggestions = await Suggestions(keyword, subtitle, searchSource);
                    results.AddRange(suggestions);
                }, _updateToken);

                if (!task.Wait(waittime))
                {
                    task.ContinueWith(_ => ResultsUpdated?.Invoke(this, new ResultUpdatedEventArgs
                    {
                        Results = results,
                        Query = query
                    }), _updateToken);
                }
            }
        }
Beispiel #7
0
        Suggestions(string keyword, string subtitle, SearchSource searchSource)
        {
            SuggestionSource source = _settings.SelectedSuggestion;

            if (source != null)
            {
                List <string> suggestions = await source.Suggestions(keyword);

                IEnumerable <Result> resultsFromSuggestion = suggestions.Select(o => new Result {
                    Title    = o,
                    SubTitle = subtitle,
                    Score    = 5,
                    IcoPath  = searchSource.IconPath,
                    Action   = c => {
                        Process.Start(searchSource.Url.Replace("{q}", Uri.EscapeDataString(o)));
                        return(true);
                    }
                });
                return(resultsFromSuggestion);
            }

            return(new List <Result>());
        }
Beispiel #8
0
        private async Task <IEnumerable <Result> > Suggestions(string keyword, string subtitle, SearchSource searchSource)
        {
            var source = _settings.SelectedSuggestion;

            if (source != null)
            {
                var suggestions = await source.Suggestions(keyword);

                var resultsFromSuggestion = suggestions.Select(o => new Result
                {
                    Title    = o,
                    SubTitle = subtitle,
                    Score    = 5,
                    IcoPath  = searchSource.IconPath,
                    ActionKeywordAssigned = searchSource.ActionKeyword == SearchSourceGlobalPluginWildCardSign ? string.Empty : searchSource.ActionKeyword,
                    Action = c =>
                    {
                        if (_settings.OpenInNewBrowser)
                        {
                            searchSource.Url.Replace("{q}", Uri.EscapeDataString(o)).NewBrowserWindow(_settings.BrowserPath);
                        }
                        else
                        {
                            searchSource.Url.Replace("{q}", Uri.EscapeDataString(o)).NewTabInBrowser(_settings.BrowserPath);
                        }

                        return(true);
                    }
                });
                return(resultsFromSuggestion);
            }
            return(new List <Result>());
        }
 private void Initilize(IList<SearchSource> sources, PluginInitContext context, Action action)
 {
     InitializeComponent();
     DataContext = _viewModel;
     _searchSource = _viewModel.SearchSource;
     _searchSources = sources;
     _action = action;
     _context = context;
     _api = _context.API;
 }
 public SearchSourceSettingWindow(IList<SearchSource> sources, PluginInitContext context, SearchSource old)
 {
     _oldSearchSource = old;
     _viewModel = new SearchSourceViewModel {SearchSource = old.DeepCopy()};
     Initilize(sources, context, Action.Edit);
 }
Beispiel #11
0
 private async Task<IEnumerable<Result>> Suggestions(string keyword, string subtitle, SearchSource searchSource)
 {
     var source = _settings.SelectedSuggestion;
     if (source != null)
     {
         var suggestions = await source.Suggestions(keyword);
         var resultsFromSuggestion = suggestions.Select(o => new Result
         {
             Title = o,
             SubTitle = subtitle,
             Score = 5,
             IcoPath = searchSource.IconPath,
             Action = c =>
             {
                 Process.Start(searchSource.Url.Replace("{q}", Uri.EscapeDataString(o)));
                 return true;
             }
         });
         return resultsFromSuggestion;
     }
     return new List<Result>();
 }
Beispiel #12
0
        private async Task <IEnumerable <Result> > Suggestions(string keyword, string subtitle, SearchSource searchSource)
        {
            var source = _settings.SelectedSuggestion;

            if (source != null)
            {
                var suggestions = await source.Suggestions(keyword);

                var resultsFromSuggestion = suggestions.Select(o => new Result
                {
                    Title    = o,
                    SubTitle = subtitle,
                    Score    = 5,
                    IcoPath  = searchSource.IconPath,
                    Action   = c =>
                    {
                        string browserPath = GetBrowserPath();
                        if (browserPath.Length == 0)
                        {
                            Process.Start(searchSource.Url.Replace("{q}", Uri.EscapeDataString(o)));
                        }
                        else
                        {
                            Process.Start(browserPath, searchSource.Url.Replace("{q}", Uri.EscapeDataString(o)));
                        }

                        return(true);
                    }
                });
                return(resultsFromSuggestion);
            }
            return(new List <Result>());
        }