Flow.Launcher/Plugins/Flow.Launcher.Plugin.Sys/Main.cs

322 lines
13 KiB
C#
Raw Permalink Normal View History

2019-09-30 10:03:06 +00:00
using System;
using System.Collections.Generic;
2014-07-25 00:32:19 +00:00
using System.Diagnostics;
using System.IO;
2014-07-25 00:32:19 +00:00
using System.Runtime.InteropServices;
using System.Windows;
2016-01-06 06:45:08 +00:00
using System.Windows.Forms;
using System.Windows.Interop;
2020-04-21 09:12:17 +00:00
using Flow.Launcher.Infrastructure;
using Flow.Launcher.Infrastructure.UserSettings;
2021-03-04 08:51:56 +00:00
using Flow.Launcher.Plugin.SharedCommands;
2016-01-06 06:45:08 +00:00
using Application = System.Windows.Application;
2015-07-14 15:59:24 +00:00
using Control = System.Windows.Controls.Control;
2016-01-06 06:45:08 +00:00
using FormsApplication = System.Windows.Forms.Application;
using MessageBox = System.Windows.MessageBox;
2014-07-25 00:32:19 +00:00
2020-04-21 09:12:17 +00:00
namespace Flow.Launcher.Plugin.Sys
2014-07-25 00:32:19 +00:00
{
2016-05-07 02:55:09 +00:00
public class Main : IPlugin, ISettingProvider, IPluginI18n
2015-02-27 10:04:49 +00:00
{
2015-01-07 14:23:10 +00:00
private PluginInitContext context;
2014-07-25 00:32:19 +00:00
2015-02-27 10:04:49 +00:00
#region DllImport
2014-07-25 00:32:19 +00:00
internal const int EWX_LOGOFF = 0x00000000;
internal const int EWX_SHUTDOWN = 0x00000001;
internal const int EWX_REBOOT = 0x00000002;
internal const int EWX_FORCE = 0x00000004;
internal const int EWX_POWEROFF = 0x00000008;
internal const int EWX_FORCEIFHUNG = 0x00000010;
2014-07-25 00:32:19 +00:00
[DllImport("user32")]
private static extern bool ExitWindowsEx(uint uFlags, uint dwReason);
2014-07-25 00:32:19 +00:00
[DllImport("user32")]
private static extern void LockWorkStation();
[DllImport("Shell32.dll", CharSet = CharSet.Unicode)]
private static extern uint SHEmptyRecycleBin(IntPtr hWnd, uint dwFlags);
// http://www.pinvoke.net/default.aspx/Enums/HRESULT.html
private enum HRESULT : uint
{
S_FALSE = 0x0001,
S_OK = 0x0000
}
2015-02-27 10:04:49 +00:00
#endregion
2014-07-25 00:32:19 +00:00
2015-07-14 15:59:24 +00:00
public Control CreateSettingPanel()
2014-07-25 00:32:19 +00:00
{
var results = Commands();
return new SysSettings(results);
2014-07-25 00:32:19 +00:00
}
public List<Result> Query(Query query)
2014-07-25 00:32:19 +00:00
{
var commands = Commands();
var results = new List<Result>();
foreach (var c in commands)
2014-07-25 00:32:19 +00:00
{
2019-12-03 14:25:19 +00:00
var titleMatch = StringMatcher.FuzzySearch(query.Search, c.Title);
var subTitleMatch = StringMatcher.FuzzySearch(query.Search, c.SubTitle);
var score = Math.Max(titleMatch.Score, subTitleMatch.Score);
if (score > 0)
2014-07-25 00:32:19 +00:00
{
c.Score = score;
2019-12-03 14:25:19 +00:00
if (score == titleMatch.Score)
{
c.TitleHighlightData = titleMatch.MatchData;
}
else
2019-12-03 14:25:19 +00:00
{
c.SubTitleHighlightData = subTitleMatch.MatchData;
}
results.Add(c);
2014-07-25 00:32:19 +00:00
}
}
2014-07-25 00:32:19 +00:00
return results;
}
public void Init(PluginInitContext context)
2014-07-25 00:32:19 +00:00
{
2015-01-07 14:23:10 +00:00
this.context = context;
}
private List<Result> Commands()
2015-01-07 14:23:10 +00:00
{
var results = new List<Result>();
results.AddRange(new[]
{
new Result
{
Title = "Shutdown",
2020-04-21 12:54:41 +00:00
SubTitle = context.API.GetTranslation("flowlauncher_plugin_sys_shutdown_computer"),
IcoPath = "Images\\shutdown.png",
2016-01-06 21:34:42 +00:00
Action = c =>
{
var reuslt = MessageBox.Show(
context.API.GetTranslation("flowlauncher_plugin_sys_dlgtext_shutdown_computer"),
context.API.GetTranslation("flowlauncher_plugin_sys_shutdown_computer"),
MessageBoxButton.YesNo, MessageBoxImage.Warning);
if (reuslt == MessageBoxResult.Yes)
{
Process.Start("shutdown", "/s /t 0");
}
return true;
}
},
new Result
{
Title = "Restart",
2020-04-21 12:54:41 +00:00
SubTitle = context.API.GetTranslation("flowlauncher_plugin_sys_restart_computer"),
IcoPath = "Images\\restart.png",
2016-01-06 21:34:42 +00:00
Action = c =>
{
var result = MessageBox.Show(
context.API.GetTranslation("flowlauncher_plugin_sys_dlgtext_restart_computer"),
context.API.GetTranslation("flowlauncher_plugin_sys_restart_computer"),
MessageBoxButton.YesNo, MessageBoxImage.Warning);
if (result == MessageBoxResult.Yes)
{
Process.Start("shutdown", "/r /t 0");
}
return true;
}
},
new Result
{
Title = "Log Off",
2020-04-21 12:54:41 +00:00
SubTitle = context.API.GetTranslation("flowlauncher_plugin_sys_log_off"),
IcoPath = "Images\\logoff.png",
2016-01-06 21:34:42 +00:00
Action = c => ExitWindowsEx(EWX_LOGOFF, 0)
},
new Result
{
Title = "Lock",
2020-04-21 12:54:41 +00:00
SubTitle = context.API.GetTranslation("flowlauncher_plugin_sys_lock"),
IcoPath = "Images\\lock.png",
2016-01-06 21:34:42 +00:00
Action = c =>
{
LockWorkStation();
return true;
}
},
2015-07-14 15:59:24 +00:00
new Result
{
Title = "Sleep",
2020-04-21 12:54:41 +00:00
SubTitle = context.API.GetTranslation("flowlauncher_plugin_sys_sleep"),
2015-07-14 15:59:24 +00:00
IcoPath = "Images\\sleep.png",
2016-01-06 21:34:42 +00:00
Action = c => FormsApplication.SetSuspendState(PowerState.Suspend, false, false)
},
new Result
2019-09-30 10:03:06 +00:00
{
Title = "Hibernate",
2020-04-21 12:54:41 +00:00
SubTitle = context.API.GetTranslation("flowlauncher_plugin_sys_hibernate"),
2021-03-04 08:51:56 +00:00
IcoPath = "Images\\hibernate.png",
Action= c =>
{
var info = ShellCommand.SetProcessStartInfo("shutdown", arguments:"/h");
info.WindowStyle = ProcessWindowStyle.Hidden;
info.UseShellExecute = true;
Process.Start(info);
return true;
}
2019-09-30 10:03:06 +00:00
},
new Result
{
Title = "Empty Recycle Bin",
2020-04-21 12:54:41 +00:00
SubTitle = context.API.GetTranslation("flowlauncher_plugin_sys_emptyrecyclebin"),
IcoPath = "Images\\recyclebin.png",
2016-01-06 21:34:42 +00:00
Action = c =>
{
// http://www.pinvoke.net/default.aspx/shell32/SHEmptyRecycleBin.html
2018-12-22 06:23:28 +00:00
// FYI, couldn't find documentation for this but if the recycle bin is already empty, it will return -2147418113 (0x8000FFFF (E_UNEXPECTED))
// 0 for nothing
2021-01-17 08:05:28 +00:00
var result = SHEmptyRecycleBin(new WindowInteropHelper(Application.Current.MainWindow).Handle, 0);
if (result != (uint) HRESULT.S_OK && result != (uint) 0x8000FFFF)
{
MessageBox.Show($"Error emptying recycle bin, error code: {result}\n" +
"please refer to https://msdn.microsoft.com/en-us/library/windows/desktop/aa378137",
"Error",
MessageBoxButton.OK, MessageBoxImage.Error);
}
return true;
}
2015-07-14 15:59:24 +00:00
},
new Result
{
Title = "Exit",
2020-04-21 12:54:41 +00:00
SubTitle = context.API.GetTranslation("flowlauncher_plugin_sys_exit"),
IcoPath = "Images\\app.png",
2016-01-06 21:34:42 +00:00
Action = c =>
{
Application.Current.MainWindow.Close();
return true;
}
},
new Result
{
Title = "Save Settings",
2020-04-21 12:54:41 +00:00
SubTitle = context.API.GetTranslation("flowlauncher_plugin_sys_save_all_settings"),
IcoPath = "Images\\app.png",
Action = c =>
{
context.API.SaveAppAllSettings();
2020-04-21 12:54:41 +00:00
context.API.ShowMsg(context.API.GetTranslation("flowlauncher_plugin_sys_dlgtitle_success"),
context.API.GetTranslation("flowlauncher_plugin_sys_dlgtext_all_settings_saved"));
return true;
}
},
new Result
{
2020-04-22 10:26:09 +00:00
Title = "Restart Flow Launcher",
2020-04-21 12:54:41 +00:00
SubTitle = context.API.GetTranslation("flowlauncher_plugin_sys_restart"),
IcoPath = "Images\\app.png",
2016-01-06 21:34:42 +00:00
Action = c =>
{
context.API.RestartApp();
return false;
}
},
new Result
{
Title = "Settings",
2020-04-21 12:54:41 +00:00
SubTitle = context.API.GetTranslation("flowlauncher_plugin_sys_setting"),
IcoPath = "Images\\app.png",
2016-01-06 21:34:42 +00:00
Action = c =>
{
context.API.OpenSettingDialog();
return true;
}
},
new Result
{
Title = "Reload Plugin Data",
2020-04-21 12:54:41 +00:00
SubTitle = context.API.GetTranslation("flowlauncher_plugin_sys_reload_plugin_data"),
IcoPath = "Images\\app.png",
Action = c =>
{
// Hide the window first then show msg after done because sometimes the reload could take a while, so not to make user think it's frozen.
Application.Current.MainWindow.Hide();
context.API.ReloadAllPluginData().ContinueWith(_ =>
context.API.ShowMsg(
context.API.GetTranslation("flowlauncher_plugin_sys_dlgtitle_success"),
context.API.GetTranslation(
"flowlauncher_plugin_sys_dlgtext_all_applicableplugins_reloaded")));
return true;
}
},
new Result
{
Title = "Check For Update",
2021-03-30 19:28:25 +00:00
SubTitle = context.API.GetTranslation("flowlauncher_plugin_sys_check_for_update"),
IcoPath = "Images\\checkupdate.png",
Action = c =>
{
Application.Current.MainWindow.Hide();
context.API.CheckForNewUpdate();
return true;
}
},
new Result
{
Title = "Open Log Location",
2021-03-30 19:28:25 +00:00
SubTitle = context.API.GetTranslation("flowlauncher_plugin_sys_open_log_location"),
IcoPath = "Images\\app.png",
Action = c =>
{
var logPath = Path.Combine(DataLocation.DataDirectory(), "Logs", Constant.Version);
FilesFolders.OpenPath(logPath);
return true;
}
},
new Result
{
Title = "Flow Launcher Tips",
SubTitle = context.API.GetTranslation("flowlauncher_plugin_sys_open_docs_tips"),
IcoPath = "Images\\app.png",
Action = c =>
{
SearchWeb.NewTabInBrowser(Constant.Documentation);
return true;
}
},
new Result
{
Title = "Flow Launcher UserData Folder",
SubTitle = context.API.GetTranslation("flowlauncher_plugin_sys_open_userdata_location"),
IcoPath = "Images\\app.png",
Action = c =>
{
FilesFolders.OpenPath(DataLocation.DataDirectory());
return true;
}
}
2021-04-18 21:29:58 +00:00
});
return results;
2014-07-25 00:32:19 +00:00
}
2015-02-07 12:17:49 +00:00
public string GetTranslatedPluginTitle()
{
2020-04-21 12:54:41 +00:00
return context.API.GetTranslation("flowlauncher_plugin_sys_plugin_name");
2015-02-07 12:17:49 +00:00
}
public string GetTranslatedPluginDescription()
{
2020-04-21 12:54:41 +00:00
return context.API.GetTranslation("flowlauncher_plugin_sys_plugin_description");
2015-02-07 12:17:49 +00:00
}
2015-02-27 10:04:49 +00:00
}
}