1010using Flow . Launcher . Infrastructure . UserSettings ;
1111using Flow . Launcher . Plugin ;
1212
13- namespace Flow . Launcher . Helper ;
13+ namespace Flow . Launcher . Core . Plugin ;
1414
1515/// <summary>
1616/// Helper class for installing, updating, and uninstalling plugins.
1717/// </summary>
18- public static class PluginInstallationHelper
18+ public static class PluginInstaller
1919{
20- private static readonly string ClassName = nameof ( PluginInstallationHelper ) ;
20+ private static readonly string ClassName = nameof ( PluginInstaller ) ;
2121
2222 private static readonly Settings Settings = Ioc . Default . GetRequiredService < Settings > ( ) ;
2323
24+ // We should not initialize API in static constructor because it will create another API instance
25+ private static IPublicAPI api = null ;
26+ private static IPublicAPI API => api ??= Ioc . Default . GetRequiredService < IPublicAPI > ( ) ;
27+
2428 public static async Task InstallPluginAndCheckRestartAsync ( UserPlugin newPlugin )
2529 {
26- if ( App . API . ShowMsgBox (
30+ if ( API . ShowMsgBox (
2731 string . Format (
28- App . API . GetTranslation ( "InstallPromptSubtitle" ) ,
32+ API . GetTranslation ( "InstallPromptSubtitle" ) ,
2933 newPlugin . Name , newPlugin . Author , Environment . NewLine ) ,
30- App . API . GetTranslation ( "InstallPromptTitle" ) ,
34+ API . GetTranslation ( "InstallPromptTitle" ) ,
3135 button : MessageBoxButton . YesNo ) != MessageBoxResult . Yes ) return ;
3236
3337 try
@@ -44,7 +48,7 @@ public static async Task InstallPluginAndCheckRestartAsync(UserPlugin newPlugin)
4448 if ( ! newPlugin . IsFromLocalInstallPath )
4549 {
4650 await DownloadFileAsync (
47- $ "{ App . API . GetTranslation ( "DownloadingPlugin" ) } { newPlugin . Name } ",
51+ $ "{ API . GetTranslation ( "DownloadingPlugin" ) } { newPlugin . Name } ",
4852 newPlugin . UrlDownload , filePath , cts ) ;
4953 }
5054 else
@@ -63,7 +67,7 @@ await DownloadFileAsync(
6367 throw new FileNotFoundException ( $ "Plugin { newPlugin . ID } zip file not found at { filePath } ", filePath ) ;
6468 }
6569
66- App . API . InstallPlugin ( newPlugin , filePath ) ;
70+ API . InstallPlugin ( newPlugin , filePath ) ;
6771
6872 if ( ! newPlugin . IsFromLocalInstallPath )
6973 {
@@ -72,21 +76,21 @@ await DownloadFileAsync(
7276 }
7377 catch ( Exception e )
7478 {
75- App . API . LogException ( ClassName , "Failed to install plugin" , e ) ;
76- App . API . ShowMsgError ( App . API . GetTranslation ( "ErrorInstallingPlugin" ) ) ;
79+ API . LogException ( ClassName , "Failed to install plugin" , e ) ;
80+ API . ShowMsgError ( API . GetTranslation ( "ErrorInstallingPlugin" ) ) ;
7781 return ; // don’t restart on failure
7882 }
7983
8084 if ( Settings . AutoRestartAfterChanging )
8185 {
82- App . API . RestartApp ( ) ;
86+ API . RestartApp ( ) ;
8387 }
8488 else
8589 {
86- App . API . ShowMsg (
87- App . API . GetTranslation ( "installbtn" ) ,
90+ API . ShowMsg (
91+ API . GetTranslation ( "installbtn" ) ,
8892 string . Format (
89- App . API . GetTranslation (
93+ API . GetTranslation (
9094 "InstallSuccessNoRestart" ) ,
9195 newPlugin . Name ) ) ;
9296 }
@@ -108,17 +112,17 @@ public static async Task InstallPluginAndCheckRestartAsync(string filePath)
108112 }
109113 catch ( Exception e )
110114 {
111- App . API . LogException ( ClassName , "Failed to validate zip file" , e ) ;
112- App . API . ShowMsgError ( App . API . GetTranslation ( "ZipFileNotHavePluginJson" ) ) ;
115+ API . LogException ( ClassName , "Failed to validate zip file" , e ) ;
116+ API . ShowMsgError ( API . GetTranslation ( "ZipFileNotHavePluginJson" ) ) ;
113117 return ;
114118 }
115119
116120 if ( Settings . ShowUnknownSourceWarning )
117121 {
118122 if ( ! InstallSourceKnown ( plugin . Website )
119- && App . API . ShowMsgBox ( string . Format (
120- App . API . GetTranslation ( "InstallFromUnknownSourceSubtitle" ) , Environment . NewLine ) ,
121- App . API . GetTranslation ( "InstallFromUnknownSourceTitle" ) ,
123+ && API . ShowMsgBox ( string . Format (
124+ API . GetTranslation ( "InstallFromUnknownSourceSubtitle" ) , Environment . NewLine ) ,
125+ API . GetTranslation ( "InstallFromUnknownSourceTitle" ) ,
122126 MessageBoxButton . YesNo ) == MessageBoxResult . No )
123127 return ;
124128 }
@@ -128,51 +132,51 @@ public static async Task InstallPluginAndCheckRestartAsync(string filePath)
128132
129133 public static async Task UninstallPluginAndCheckRestartAsync ( PluginMetadata oldPlugin )
130134 {
131- if ( App . API . ShowMsgBox (
135+ if ( API . ShowMsgBox (
132136 string . Format (
133- App . API . GetTranslation ( "UninstallPromptSubtitle" ) ,
137+ API . GetTranslation ( "UninstallPromptSubtitle" ) ,
134138 oldPlugin . Name , oldPlugin . Author , Environment . NewLine ) ,
135- App . API . GetTranslation ( "UninstallPromptTitle" ) ,
139+ API . GetTranslation ( "UninstallPromptTitle" ) ,
136140 button : MessageBoxButton . YesNo ) != MessageBoxResult . Yes ) return ;
137141
138- var removePluginSettings = App . API . ShowMsgBox (
139- App . API . GetTranslation ( "KeepPluginSettingsSubtitle" ) ,
140- App . API . GetTranslation ( "KeepPluginSettingsTitle" ) ,
142+ var removePluginSettings = API . ShowMsgBox (
143+ API . GetTranslation ( "KeepPluginSettingsSubtitle" ) ,
144+ API . GetTranslation ( "KeepPluginSettingsTitle" ) ,
141145 button : MessageBoxButton . YesNo ) == MessageBoxResult . No ;
142146
143147 try
144148 {
145- await App . API . UninstallPluginAsync ( oldPlugin , removePluginSettings ) ;
149+ await API . UninstallPluginAsync ( oldPlugin , removePluginSettings ) ;
146150 }
147151 catch ( Exception e )
148152 {
149- App . API . LogException ( ClassName , "Failed to uninstall plugin" , e ) ;
150- App . API . ShowMsgError ( App . API . GetTranslation ( "ErrorUninstallingPlugin" ) ) ;
153+ API . LogException ( ClassName , "Failed to uninstall plugin" , e ) ;
154+ API . ShowMsgError ( API . GetTranslation ( "ErrorUninstallingPlugin" ) ) ;
151155 return ; // don’t restart on failure
152156 }
153157
154158 if ( Settings . AutoRestartAfterChanging )
155159 {
156- App . API . RestartApp ( ) ;
160+ API . RestartApp ( ) ;
157161 }
158162 else
159163 {
160- App . API . ShowMsg (
161- App . API . GetTranslation ( "uninstallbtn" ) ,
164+ API . ShowMsg (
165+ API . GetTranslation ( "uninstallbtn" ) ,
162166 string . Format (
163- App . API . GetTranslation (
167+ API . GetTranslation (
164168 "UninstallSuccessNoRestart" ) ,
165169 oldPlugin . Name ) ) ;
166170 }
167171 }
168172
169173 public static async Task UpdatePluginAndCheckRestartAsync ( UserPlugin newPlugin , PluginMetadata oldPlugin )
170174 {
171- if ( App . API . ShowMsgBox (
175+ if ( API . ShowMsgBox (
172176 string . Format (
173- App . API . GetTranslation ( "UpdatePromptSubtitle" ) ,
177+ API . GetTranslation ( "UpdatePromptSubtitle" ) ,
174178 oldPlugin . Name , oldPlugin . Author , Environment . NewLine ) ,
175- App . API . GetTranslation ( "UpdatePromptTitle" ) ,
179+ API . GetTranslation ( "UpdatePromptTitle" ) ,
176180 button : MessageBoxButton . YesNo ) != MessageBoxResult . Yes ) return ;
177181
178182 try
@@ -184,7 +188,7 @@ public static async Task UpdatePluginAndCheckRestartAsync(UserPlugin newPlugin,
184188 if ( ! newPlugin . IsFromLocalInstallPath )
185189 {
186190 await DownloadFileAsync (
187- $ "{ App . API . GetTranslation ( "DownloadingPlugin" ) } { newPlugin . Name } ",
191+ $ "{ API . GetTranslation ( "DownloadingPlugin" ) } { newPlugin . Name } ",
188192 newPlugin . UrlDownload , filePath , cts ) ;
189193 }
190194 else
@@ -198,25 +202,25 @@ await DownloadFileAsync(
198202 return ;
199203 }
200204
201- await App . API . UpdatePluginAsync ( oldPlugin , newPlugin , filePath ) ;
205+ await API . UpdatePluginAsync ( oldPlugin , newPlugin , filePath ) ;
202206 }
203207 catch ( Exception e )
204208 {
205- App . API . LogException ( ClassName , "Failed to update plugin" , e ) ;
206- App . API . ShowMsgError ( App . API . GetTranslation ( "ErrorUpdatingPlugin" ) ) ;
209+ API . LogException ( ClassName , "Failed to update plugin" , e ) ;
210+ API . ShowMsgError ( API . GetTranslation ( "ErrorUpdatingPlugin" ) ) ;
207211 return ; // don’t restart on failure
208212 }
209213
210214 if ( Settings . AutoRestartAfterChanging )
211215 {
212- App . API . RestartApp ( ) ;
216+ API . RestartApp ( ) ;
213217 }
214218 else
215219 {
216- App . API . ShowMsg (
217- App . API . GetTranslation ( "updatebtn" ) ,
220+ API . ShowMsg (
221+ API . GetTranslation ( "updatebtn" ) ,
218222 string . Format (
219- App . API . GetTranslation (
223+ API . GetTranslation (
220224 "UpdateSuccessNoRestart" ) ,
221225 newPlugin . Name ) ) ;
222226 }
@@ -230,7 +234,7 @@ private static async Task DownloadFileAsync(string prgBoxTitle, string downloadU
230234 if ( showProgress )
231235 {
232236 var exceptionHappened = false ;
233- await App . API . ShowProgressBoxAsync ( prgBoxTitle ,
237+ await API . ShowProgressBoxAsync ( prgBoxTitle ,
234238 async ( reportProgress ) =>
235239 {
236240 if ( reportProgress == null )
@@ -242,18 +246,18 @@ await App.API.ShowProgressBoxAsync(prgBoxTitle,
242246 }
243247 else
244248 {
245- await App . API . HttpDownloadAsync ( downloadUrl , filePath , reportProgress , cts . Token ) . ConfigureAwait ( false ) ;
249+ await API . HttpDownloadAsync ( downloadUrl , filePath , reportProgress , cts . Token ) . ConfigureAwait ( false ) ;
246250 }
247251 } , cts . Cancel ) ;
248252
249253 // if exception happened while downloading and user does not cancel downloading,
250254 // we need to redownload the plugin
251255 if ( exceptionHappened && ( ! cts . IsCancellationRequested ) )
252- await App . API . HttpDownloadAsync ( downloadUrl , filePath , token : cts . Token ) . ConfigureAwait ( false ) ;
256+ await API . HttpDownloadAsync ( downloadUrl , filePath , token : cts . Token ) . ConfigureAwait ( false ) ;
253257 }
254258 else
255259 {
256- await App . API . HttpDownloadAsync ( downloadUrl , filePath , token : cts . Token ) . ConfigureAwait ( false ) ;
260+ await API . HttpDownloadAsync ( downloadUrl , filePath , token : cts . Token ) . ConfigureAwait ( false ) ;
257261 }
258262 }
259263
@@ -275,7 +279,7 @@ private static bool InstallSourceKnown(string url)
275279 if ( ! Uri . TryCreate ( url , UriKind . Absolute , out var uri ) || uri . Host != acceptedHost )
276280 return false ;
277281
278- return App . API . GetAllPlugins ( ) . Any ( x =>
282+ return API . GetAllPlugins ( ) . Any ( x =>
279283 ! string . IsNullOrEmpty ( x . Metadata . Website ) &&
280284 x . Metadata . Website . StartsWith ( constructedUrlPart )
281285 ) ;
0 commit comments