-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMain.cs
153 lines (140 loc) · 4.97 KB
/
Main.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows.Controls;
using Flow.Launcher.Plugin;
using Flow.Launcher.Plugin.WinSCP.ViewModels;
namespace Flow.Launcher.Plugin.WinSCP
{
/// <summary>
/// Plugin for WinSCP
/// </summary>
public class WinSCP : IPlugin, ISettingProvider, IPluginI18n, IContextMenu
{
private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
private PluginInitContext _context;
private Settings _settings;
private WinSCPService _service;
/// <inheritdoc/>
public void Init(PluginInitContext context)
{
_context = context;
_settings = context.API.LoadSettingJsonStorage<Settings>();
_service = new WinSCPService(_context, _settings);
}
/// <inheritdoc/>
public List<Result> Query(Query query)
{
if (string.IsNullOrEmpty(_settings.WinSCPExePath))
{
string errorTitle = _context.API.GetTranslation(
"flowlauncher_plugin_winscp_pathNotSet"
);
string errorMsg = _context.API.GetTranslation(
"flowlauncher_plugin_winscp_configPlugin"
);
_context.API.ShowMsg(errorTitle, errorMsg, "");
return new();
}
var querySearch = query.Search;
if (string.IsNullOrEmpty(querySearch))
{
return _service.GetAll().ConvertAll(e => MakeResult(e));
}
return _service
.GetAll()
.Where(entry =>
entry.Title.ToLowerInvariant().Contains(querySearch.ToLowerInvariant())
)
.ToList()
.ConvertAll(entry => MakeResult(entry));
}
/// <inheritdoc/>
public Control CreateSettingPanel()
{
return new PluginSettings(_context, _settings, new SettingsViewModel(_context));
}
/// <inheritdoc/>
public string GetTranslatedPluginTitle()
{
return _context.API.GetTranslation("flowlauncher_plugin_winscp_plugin_title");
}
/// <inheritdoc/>
public string GetTranslatedPluginDescription()
{
return _context.API.GetTranslation("flowlauncher_plugin_winscp_plugin_description");
}
/// <inheritdoc/>
public List<Result> LoadContextMenus(Result selectedResult)
{
return new()
{
MakeActionResult((SessionEntry)selectedResult.ContextData, "Run"),
MakeActionResult(
(SessionEntry)selectedResult.ContextData,
"Run new instance",
"/newinstance"
)
};
}
private Result MakeResult(SessionEntry sessionEntry)
{
return new()
{
Title = sessionEntry.Title,
IcoPath = "icon.png",
Score = 50,
SubTitle = sessionEntry.ToString(),
ContextData = sessionEntry,
Action = _context => LaunchWinSCPSession(sessionEntry),
};
}
private Result MakeActionResult(
SessionEntry sessionEntry,
string title,
string additionalArgs = null,
string subTitle = null
)
{
return new()
{
Title = title,
SubTitle = subTitle,
IcoPath = "icon.png",
Score = 50,
Action = _context => LaunchWinSCPSession(sessionEntry, additionalArgs),
};
}
private bool LaunchWinSCPSession(SessionEntry sessionEntry, string additionalArgs = null)
{
try
{
var winScpPath = _settings.WinSCPExePath;
var p = new Process { StartInfo = { FileName = winScpPath } };
p.StartInfo.Arguments = '"' + sessionEntry.Identifier + '"';
if (additionalArgs != null)
{
p.StartInfo.Arguments += " " + additionalArgs;
}
p.Start();
return true;
}
catch (Exception ex)
{
string trError = _context.API.GetTranslation("flowlauncher_plugin_winscp_error");
_context.API.ShowMsg(
trError
+ ": "
+ sessionEntry?.Identifier
+ " ("
+ _settings.WinSCPExePath
+ ") ",
ex.Message,
""
);
return false;
}
}
}
}