Exemple #1
0
        public Result Result(string query, IPublicAPI api)
        {
            if (api == null)
            {
                throw new ArgumentNullException(nameof(api));
            }

            var score = Score(query);

            if (score <= 0)
            { // no need to create result if this is zero
                return(null);
            }

            if (!HasArguments)
            {
                var noArgumentScoreModifier = 5;
                score += noArgumentScoreModifier;
            }
            else
            {
                // Filter Web Applications when searching for the main application
                if (FilterWebApplication(query))
                {
                    return(null);
                }
            }

            // NOTE: This is to display run commands only when there is an exact match, like in start menu
            if (!QueryEqualsNameForRunCommands(query))
            {
                return(null);
            }

            var result = new Result
            {
                SubTitle    = SetSubtitle(),
                IcoPath     = IcoPath,
                Score       = score,
                ContextData = this,
                Action      = e =>
                {
                    var info = new ProcessStartInfo
                    {
                        FileName         = LnkResolvedPath ?? FullPath,
                        WorkingDirectory = ParentDirectory,
                        UseShellExecute  = true,
                    };

                    Main.StartProcess(Process.Start, info);

                    return(true);
                },
            };

            // To set the title for the result to always be the name of the application
            result.Title = Name;
            result.TitleHighlightData = StringMatcher.FuzzySearch(query, Name).MatchData;

            var toolTipTitle = string.Format(CultureInfo.CurrentCulture, "{0}: {1}", Properties.Resources.powertoys_run_plugin_program_file_name, result.Title);
            var toolTipText  = string.Format(CultureInfo.CurrentCulture, "{0}: {1}", Properties.Resources.powertoys_run_plugin_program_file_path, FullPath);

            result.ToolTipData = new ToolTipData(toolTipTitle, toolTipText);

            return(result);
        }
Exemple #2
0
            public Result Result(string query, IPublicAPI api)
            {
                string      title;
                MatchResult matchResult;

                // We suppose Name won't be null
                if (!Main._settings.EnableDescription || Description == null || Name.StartsWith(Description))
                {
                    title       = Name;
                    matchResult = StringMatcher.FuzzySearch(query, title);
                }
                else if (Description.StartsWith(Name))
                {
                    title       = Description;
                    matchResult = StringMatcher.FuzzySearch(query, Description);
                }
                else
                {
                    title = $"{Name}: {Description}";
                    var nameMatch       = StringMatcher.FuzzySearch(query, Name);
                    var desciptionMatch = StringMatcher.FuzzySearch(query, Description);
                    if (desciptionMatch.Score > nameMatch.Score)
                    {
                        for (int i = 0; i < desciptionMatch.MatchData.Count; i++)
                        {
                            desciptionMatch.MatchData[i] += Name.Length + 2; // 2 is ": "
                        }
                        matchResult = desciptionMatch;
                    }
                    else
                    {
                        matchResult = nameMatch;
                    }
                }

                if (!matchResult.Success)
                {
                    return(null);
                }

                var result = new Result
                {
                    Title              = title,
                    SubTitle           = Main._settings.HideAppsPath ? string.Empty : Package.Location,
                    Icon               = Logo,
                    Score              = matchResult.Score,
                    TitleHighlightData = matchResult.MatchData,
                    ContextData        = this,
                    Action             = e =>
                    {
                        var elevated = (
                            e.SpecialKeyState.CtrlPressed &&
                            e.SpecialKeyState.ShiftPressed &&
                            !e.SpecialKeyState.AltPressed &&
                            !e.SpecialKeyState.WinPressed
                            );

                        if (elevated && CanRunElevated)
                        {
                            LaunchElevated();
                        }
                        else
                        {
                            Launch(api);

                            if (elevated)
                            {
                                var title   = "Plugin: Program";
                                var message = api.GetTranslation("flowlauncher_plugin_program_run_as_administrator_not_supported_message");
                                api.ShowMsg(title, message, string.Empty);
                            }
                        }

                        return(true);
                    }
                };


                return(result);
            }
 public MatchResult FuzzySearch(string query, string stringToCompare) => StringMatcher.FuzzySearch(query, stringToCompare);
Exemple #4
0
        private List <Result> ResultForInstallPlugin(Query query)
        {
            var results    = new List <Result>();
            var pluginName = query.SecondSearch;

            if (string.IsNullOrEmpty(pluginName))
            {
                return(results);
            }
            string json;

            try
            {
                json = Http.Get(pluginSearchUrl + pluginName).Result;
            }
            catch (WebException e)
            {
                //todo add option in log to decide give user prompt or not
                context.API.ShowMsg("PluginManagement.ResultForInstallPlugin: Can't connect to Wox plugin website, check your conenction");
                Logger.WoxError("Can't connect to Wox plugin website, check your conenction", e);
                return(new List <Result>());
            }

            List <WoxPluginResult> searchedPlugins;

            try
            {
                searchedPlugins = JsonConvert.DeserializeObject <List <WoxPluginResult> >(json);
            }
            catch (JsonSerializationException e)
            {
                context.API.ShowMsg("PluginManagement.ResultForInstallPlugin: Coundn't parse api search results, Please update your Wox!");
                Logger.WoxError("Coundn't parse api search results, Please update your Wox!", e);
                return(results);
            }

            foreach (var r in searchedPlugins)
            {
                var r1 = r;
                results.Add(new Result
                {
                    Title                 = r.name,
                    SubTitle              = r.description,
                    IcoPath               = "Images\\plugin.png",
                    TitleHighlightData    = StringMatcher.FuzzySearch(query.SecondSearch, r.name).MatchData,
                    SubTitleHighlightData = StringMatcher.FuzzySearch(query.SecondSearch, r.description).MatchData,
                    Action                = c =>
                    {
                        var result = MessageBox.Show("Are you sure you wish to install the \'" + r.name + "\' plugin",
                                                     "Install plugin", MessageBoxButton.YesNo);

                        if (result == MessageBoxResult.Yes)
                        {
                            var folder = Path.Combine(Path.GetTempPath(), "WoxPluginDownload");
                            if (!Directory.Exists(folder))
                            {
                                Directory.CreateDirectory(folder);
                            }
                            var filePath = Path.Combine(folder, Guid.NewGuid() + ".wox");

                            var pluginUrl = APIBASE + "/media/" + r1.plugin_file;

                            try
                            {
                                Http.Download(pluginUrl, filePath);
                            }
                            catch (WebException e)
                            {
                                context.API.ShowMsg($"PluginManagement.ResultForInstallPlugin: download failed for <{r.name}>");
                                Logger.WoxError($"download failed for <{r.name}>", e);
                                return(false);
                            }

                            context.API.InstallPlugin(filePath);
                        }

                        return(false);
                    }
                });
            }

            return(results);
        }
Exemple #5
0
        public Result Result(string query, IPublicAPI api)
        {
            var score = Score(query);

            if (score <= 0)
            { // no need to create result if this is zero
                return(null);
            }

            if (!hasArguments)
            {
                var noArgumentScoreModifier = 5;
                score += noArgumentScoreModifier;
            }
            else
            {
                // Filter Web Applications when searching for the main application
                if (FilterWebApplication(query))
                {
                    return(null);
                }
            }

            // NOTE: This is to display run commands only when there is an exact match, like in start menu
            if (!QueryEqualsNameForRunCommands(query))
            {
                return(null);
            }

            var result = new Result
            {
                SubTitle    = SetSubtitle(AppType, api),
                IcoPath     = IcoPath,
                Score       = score,
                ContextData = this,
                Action      = e =>
                {
                    var info = new ProcessStartInfo
                    {
                        FileName         = LnkResolvedPath ?? FullPath,
                        WorkingDirectory = ParentDirectory,
                        UseShellExecute  = true
                    };

                    Main.StartProcess(Process.Start, info);

                    return(true);
                }
            };

            if (Description.Length >= Name.Length &&
                Description.Substring(0, Name.Length) == Name)
            {
                result.Title = Description;
                result.TitleHighlightData = StringMatcher.FuzzySearch(query, Description).MatchData;
            }
            else
            {
                result.Title = Name;
                result.TitleHighlightData = StringMatcher.FuzzySearch(query, Name).MatchData;
            }

            return(result);
        }
        internal static Result CreateFileResult(string filePath, Query query, int score = 0, bool showIndexState = false, bool windowsIndexed = false)
        {
            var result = new Result
            {
                Title              = Path.GetFileName(filePath),
                SubTitle           = filePath,
                IcoPath            = filePath,
                AutoCompleteText   = GetPathWithActionKeyword(filePath, ResultType.File),
                TitleHighlightData = StringMatcher.FuzzySearch(query.Search, Path.GetFileName(filePath)).MatchData,
                Score              = score,
                Action             = c =>
                {
                    try
                    {
                        if (File.Exists(filePath) && c.SpecialKeyState.CtrlPressed && c.SpecialKeyState.ShiftPressed)
                        {
                            Task.Run(() =>
                            {
                                try
                                {
                                    Process.Start(new ProcessStartInfo
                                    {
                                        FileName        = filePath,
                                        UseShellExecute = true,
                                        Verb            = "runas",
                                    });
                                }
                                catch (Exception e)
                                {
                                    MessageBox.Show(e.Message, "Could not start " + filePath);
                                }
                            });
                        }
                        else if (c.SpecialKeyState.CtrlPressed)
                        {
                            Context.API.OpenDirectory(Path.GetDirectoryName(filePath), filePath);
                        }
                        else
                        {
                            FilesFolders.OpenPath(filePath);
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Could not start " + filePath);
                    }

                    return(true);
                },
                TitleToolTip    = Constants.ToolTipOpenContainingFolder,
                SubTitleToolTip = filePath,
                ContextData     = new SearchResult
                {
                    Type           = ResultType.File,
                    FullPath       = filePath,
                    ShowIndexState = showIndexState,
                    WindowsIndexed = windowsIndexed
                }
            };

            return(result);
        }
        public List <Result> Query(Query query)
        {
            var search   = query?.Search ?? string.Empty;
            var profiles = _terminalQuery.GetProfiles();

            var result = new List <Result>();

            foreach (var profile in profiles)
            {
                if (profile.Hidden && !_showHiddenProfiles)
                {
                    continue;
                }

                // Action keyword only or search query match
                if ((!string.IsNullOrWhiteSpace(query.ActionKeyword) && string.IsNullOrWhiteSpace(search)) || StringMatcher.FuzzySearch(search, profile.Name).Success)
                {
                    result.Add(new Result
                    {
                        Title    = profile.Name,
                        SubTitle = profile.Terminal.DisplayName,
                        Icon     = () => GetLogo(profile.Terminal),
                        Action   = _ =>
                        {
                            Launch(profile.Terminal.AppUserModelId, profile.Name);
                            return(true);
                        },
                        ContextData = profile,
                    });
                }
            }

            return(result);
        }
Exemple #8
0
        public Result Result(string query, IPublicAPI api)
        {
            string      title;
            MatchResult matchResult;

            // We suppose Name won't be null
            if (Description == null || Name.StartsWith(Description))
            {
                title       = Name;
                matchResult = StringMatcher.FuzzySearch(query, title);
            }
            else if (Description.StartsWith(Name))
            {
                title       = Description;
                matchResult = StringMatcher.FuzzySearch(query, Description);
            }
            else
            {
                title = $"{Name}: {Description}";
                var nameMatch       = StringMatcher.FuzzySearch(query, Name);
                var desciptionMatch = StringMatcher.FuzzySearch(query, Description);
                if (desciptionMatch.Score > nameMatch.Score)
                {
                    for (int i = 0; i < desciptionMatch.MatchData.Count; i++)
                    {
                        desciptionMatch.MatchData[i] += Name.Length + 2; // 2 is ": "
                    }
                    matchResult = desciptionMatch;
                }
                else
                {
                    matchResult = nameMatch;
                }
            }

            if (!matchResult.Success)
            {
                return(null);
            }


            var result = new Result
            {
                Title              = title,
                SubTitle           = FullPath,
                IcoPath            = IcoPath,
                Score              = matchResult.Score,
                TitleHighlightData = matchResult.MatchData,
                ContextData        = this,
                Action             = _ =>
                {
                    var info = new ProcessStartInfo
                    {
                        FileName         = FullPath,
                        WorkingDirectory = ParentDirectory,
                        UseShellExecute  = true
                    };

                    Main.StartProcess(Process.Start, info);

                    return(true);
                }
            };

            return(result);
        }
        public Result Result(string query, string queryArguments, IPublicAPI api)
        {
            if (api == null)
            {
                throw new ArgumentNullException(nameof(api));
            }

            var score = Score(query);

            if (score <= 0)
            { // no need to create result if this is zero
                return(null);
            }

            if (!HasArguments)
            {
                var noArgumentScoreModifier = 5;
                score += noArgumentScoreModifier;
            }
            else
            {
                // Filter Web Applications when searching for the main application
                if (FilterWebApplication(query))
                {
                    return(null);
                }
            }

            // NOTE: This is to display run commands only when there is an exact match, like in start menu
            if (!QueryEqualsNameForRunCommands(query))
            {
                return(null);
            }

            var result = new Result
            {
                // To set the title for the result to always be the name of the application
                Title            = !string.IsNullOrEmpty(LocalizedName) ? LocalizedName : Name,
                SubTitle         = GetSubtitle(),
                IcoPath          = IcoPath,
                Score            = score,
                ContextData      = this,
                ProgramArguments = queryArguments,
                Action           = e =>
                {
                    var info = GetProcessStartInfo(queryArguments);

                    Main.StartProcess(Process.Start, info);

                    return(true);
                },
            };

            result.TitleHighlightData = StringMatcher.FuzzySearch(query, result.Title).MatchData;

            // Using CurrentCulture since this is user facing
            var toolTipTitle = string.Format(CultureInfo.CurrentCulture, "{0}: {1}", Properties.Resources.powertoys_run_plugin_program_file_name, result.Title);
            var toolTipText  = string.Format(CultureInfo.CurrentCulture, "{0}: {1}", Properties.Resources.powertoys_run_plugin_program_file_path, FullPath);

            result.ToolTipData = new ToolTipData(toolTipTitle, toolTipText);

            return(result);
        }
        private Result CreateResult(string keyword, SearchResult searchResult)
        {
            var path = searchResult.FullPath;

            string workingDir = null;

            if (_settings.UseLocationAsWorkingDir)
            {
                workingDir = Path.GetDirectoryName(path);
            }

            var r = new Result
            {
                Title              = Path.GetFileName(path),
                SubTitle           = path,
                IcoPath            = path,
                TitleHighlightData = StringMatcher.FuzzySearch(keyword, Path.GetFileName(path)).MatchData,
                Action             = c =>
                {
                    bool hide;
                    try
                    {
                        switch (searchResult.Type)
                        {
                        case ResultType.Folder:
                            Process.Start(_settings.ExplorerPath,
                                          _settings.ExplorerArgs.Replace(Settings.DirectoryPathPlaceHolder, $"\"{path}\""));
                            break;

                        case ResultType.Volume:
                        case ResultType.File:
                            Process.Start(new ProcessStartInfo
                            {
                                FileName         = path,
                                UseShellExecute  = true,
                                WorkingDirectory = workingDir
                            });
                            break;

                        default:
                            break;
                        }

                        hide = true;
                    }
                    catch (Win32Exception)
                    {
                        var name    = $"Plugin: {_context.CurrentPluginMetadata.Name}";
                        var message = "Can't open this file";
                        _context.API.ShowMsg(name, message, string.Empty);
                        hide = false;
                    }

                    return(hide);
                },
                ContextData           = searchResult,
                SubTitleHighlightData = StringMatcher.FuzzySearch(keyword, path).MatchData
            };

            return(r);
        }
Exemple #11
0
        public List <Result> Query(Query query)
        {
            var results = new List <Result>();

            if (!string.IsNullOrEmpty(query.Search))
            {
                var keyword = query.Search;
                if (_settings.MaxSearchCount <= 0)
                {
                    _settings.MaxSearchCount = 50;
                }

                try
                {
                    var searchList = _api.Search(keyword, maxCount: _settings.MaxSearchCount).ToList();
                    foreach (var s in searchList)
                    {
                        var path = s.FullPath;

                        string workingDir = null;
                        if (_settings.UseLocationAsWorkingDir)
                        {
                            workingDir = Path.GetDirectoryName(path);
                        }

                        Result r = new Result();
                        r.Title              = Path.GetFileName(path);
                        r.SubTitle           = path;
                        r.IcoPath            = path;
                        r.TitleHighlightData = StringMatcher.FuzzySearch(keyword, Path.GetFileName(path)).MatchData;
                        r.Action             = c =>
                        {
                            bool hide;
                            try
                            {
                                Process.Start(new ProcessStartInfo
                                {
                                    FileName         = path,
                                    UseShellExecute  = true,
                                    WorkingDirectory = workingDir
                                });
                                hide = true;
                            }
                            catch (Win32Exception)
                            {
                                var name    = $"Plugin: {_context.CurrentPluginMetadata.Name}";
                                var message = "Can't open this file";
                                _context.API.ShowMsg(name, message, string.Empty);
                                hide = false;
                            }
                            return(hide);
                        };
                        r.ContextData           = s;
                        r.SubTitleHighlightData = StringMatcher.FuzzySearch(keyword, path).MatchData;
                        results.Add(r);
                    }
                }
                catch (IPCErrorException)
                {
                    results.Add(new Result
                    {
                        Title   = _context.API.GetTranslation("wox_plugin_everything_is_not_running"),
                        IcoPath = "Images\\warning.png"
                    });
                }
                catch (Exception e)
                {
                    results.Add(new Result
                    {
                        Title    = _context.API.GetTranslation("wox_plugin_everything_query_error"),
                        SubTitle = e.Message,
                        Action   = _ =>
                        {
                            Clipboard.SetText(e.Message + "\r\n" + e.StackTrace);
                            _context.API.ShowMsg(_context.API.GetTranslation("wox_plugin_everything_copied"), null, string.Empty);
                            return(false);
                        },
                        IcoPath = "Images\\error.png"
                    });
                }
            }

            _api.Reset();

            return(results);
        }