コード例 #1
0
ファイル: GoogleSuggest.cs プロジェクト: jameshy/else
        private List<Result> Query(Query query, ITokenSource cancelToken)
        {
            if (query.KeywordComplete && query.HasArguments) {
                // check the cache for a matching result
                var cacheKey = query.Arguments;
                var cachedSuggestions = MemoryCache.Default.Get(cacheKey) as List<string>;

                // if cached result is found, return it.
                if (cachedSuggestions != null) {
                    // convert the list of suggestion strings to a List<Result>
                    if (cachedSuggestions.Any()) {
                        var results = cachedSuggestions.Select(suggestion => new Result
                        {
                            Title = suggestion,
                            Icon = _icon,
                            SubTitle = "Search google for " + suggestion,
                            Launch = query1 =>
                            {
                                Process.Start($"http://google.co.uk/search?q={suggestion}");
                                AppCommands.HideWindow();

                            }
                        }).ToList();
                        return results;
                    }
                    // no suggestions were received from the server
                    return new List<Result>
                    {
                        new Result
                        {
                            Title = "No search suggestions found.",
                            Icon = _icon
                        }
                    };
                }

                // Cache miss, begin the background query to fill the cache
                // create a local cancel token for passing to httpclient..
                var cancellable = new CancellationToken();
                cancellable.Register(() =>
                {
                    cancelToken.Cancel();
                    cancelToken.Dispose();
                });
                var x = GetSuggestionsAsync(query.Arguments, cancellable);
                return new List<Result>
                {
                    new Result
                    {
                        Title = "Retrieving search suggestions...",
                        Icon = _icon
                    }
                };
            }
            // otherwise the query has not been provided yet, running the action will autocomplete the query
            return new List<Result>
            {
                new Result
                {
                    Title = "Search Google",
                    SubTitle = "Search Google with Suggestions",
                    Icon = _icon,
                    Launch = query1 => AppCommands.RewriteQuery(Keyword + ' ')
                }
            };
        }