1
0
mirror of https://github.com/chylex/TweetDuck.git synced 2025-04-22 00:15:48 +02:00

Refactor WindowsUtils.OpenAssociatedProgram & update installer code

This commit is contained in:
chylex 2020-06-02 23:56:03 +02:00
parent 458eeeccda
commit f1b7cd633e
9 changed files with 61 additions and 32 deletions

View File

@ -1,9 +1,21 @@
using System.Diagnostics;
using System;
using System.Diagnostics;
using System.IO;
using TweetLib.Core.Application;
namespace TweetDuck.Application{
class SystemHandler : IAppSystemHandler{
void IAppSystemHandler.OpenAssociatedProgram(string path){
try{
using(Process.Start(new ProcessStartInfo{
FileName = path,
ErrorDialog = true
})){}
}catch(Exception e){
Program.Reporter.HandleException("Error Opening Program", "Could not open the associated program for " + path, true, e);
}
}
void IAppSystemHandler.OpenFileExplorer(string path){
if (File.Exists(path)){
using(Process.Start("explorer.exe", "/select,\"" + path.Replace('/', '\\') + "\"")){}

View File

@ -44,7 +44,7 @@ public bool IsWaiting{
}
}
public string UpdateInstallerPath { get; private set; }
public UpdateInstaller UpdateInstaller { get; private set; }
private bool ignoreUpdateCheckError;
public AnalyticsFile AnalyticsFile => analytics?.File ?? AnalyticsFile.Dummy;
@ -233,7 +233,7 @@ private void FormBrowser_FormClosing(object sender, FormClosingEventArgs e){
}
private void FormBrowser_FormClosed(object sender, FormClosedEventArgs e){
if (isLoaded && UpdateInstallerPath == null){
if (isLoaded && UpdateInstaller == null){
updateBridge.Cleanup();
}
}
@ -311,7 +311,7 @@ void OnFinished(){
UpdateDownloadStatus status = update.DownloadStatus;
if (status == UpdateDownloadStatus.Done){
UpdateInstallerPath = update.InstallerPath;
UpdateInstaller = new UpdateInstaller(update.InstallerPath);
ForceClose();
}
else if (status != UpdateDownloadStatus.Canceled && FormMessage.Error("Update Has Failed", "Could not automatically download the update: " + (update.DownloadError?.Message ?? "unknown error") + "\n\nWould you like to open the website and try downloading the update manually?", FormMessage.Yes, FormMessage.No)){

View File

@ -192,14 +192,10 @@ private static void Main(){
Resources.Initialize(mainForm);
Win.Application.Run(mainForm);
if (mainForm.UpdateInstallerPath != null){
if (mainForm.UpdateInstaller != null){
ExitCleanup();
// ProgramPath has a trailing backslash
string updaterArgs = "/SP- /SILENT /FORCECLOSEAPPLICATIONS /UPDATEPATH=\"" + ProgramPath + "\" /RUNARGS=\"" + Arguments.GetCurrentForInstallerCmd() + "\"" + (IsPortable ? " /PORTABLE=1" : "");
bool runElevated = !IsPortable || !FileUtils.CheckFolderWritePermission(ProgramPath);
if (WindowsUtils.OpenAssociatedProgram(mainForm.UpdateInstallerPath, updaterArgs, runElevated)){
if (mainForm.UpdateInstaller.Launch()){
Win.Application.Exit();
}
else{

View File

@ -54,6 +54,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Plugins\PluginSchemeFactory.cs" />
<Compile Include="Updates\UpdateInstaller.cs" />
<Compile Include="Version.cs" />
<Compile Include="Configuration\Arguments.cs" />
<Compile Include="Configuration\ConfigManager.cs" />

View File

@ -0,0 +1,37 @@
using System;
using System.ComponentModel;
using System.Diagnostics;
using TweetDuck.Configuration;
using TweetLib.Core.Utils;
namespace TweetDuck.Updates{
sealed class UpdateInstaller{
public string Path { get; }
public UpdateInstaller(string path){
this.Path = path;
}
public bool Launch(){
// ProgramPath has a trailing backslash
string arguments = "/SP- /SILENT /FORCECLOSEAPPLICATIONS /UPDATEPATH=\"" + Program.ProgramPath + "\" /RUNARGS=\"" + Arguments.GetCurrentForInstallerCmd() + "\"" + (Program.IsPortable ? " /PORTABLE=1" : "");
bool runElevated = !Program.IsPortable || !FileUtils.CheckFolderWritePermission(Program.ProgramPath);
try{
using(Process.Start(new ProcessStartInfo{
FileName = Path,
Arguments = arguments,
Verb = runElevated ? "runas" : string.Empty,
ErrorDialog = true
})){
return true;
}
}catch(Win32Exception e) when (e.NativeErrorCode == 0x000004C7){ // operation canceled by the user
return false;
}catch(Exception e){
Program.Reporter.HandleException("Update Installer Error", "Could not launch update installer.", true, e);
return false;
}
}
}
}

View File

@ -9,6 +9,7 @@
using TweetDuck.Configuration;
using TweetDuck.Dialogs;
using TweetDuck.Management;
using TweetLib.Core;
using TweetLib.Core.Features.Twitter;
namespace TweetDuck.Utils{
@ -105,7 +106,7 @@ public static void OpenExternalBrowser(string url){
string browserPath = Config.BrowserPath;
if (browserPath == null || !File.Exists(browserPath)){
WindowsUtils.OpenAssociatedProgram(url);
App.SystemHandler.OpenAssociatedProgram(url);
}
else{
string quotedUrl = '"' + url + '"';

View File

@ -9,6 +9,7 @@
using TweetDuck.Browser.Data;
using TweetDuck.Dialogs;
using TweetDuck.Management;
using TweetLib.Core;
using TweetLib.Core.Features.Twitter;
using TweetLib.Core.Utils;
using Cookie = CefSharp.Cookie;
@ -44,7 +45,7 @@ public static void ViewImage(string url, ImageQuality quality){
string ext = Path.GetExtension(path);
if (ImageUrl.ValidExtensions.Contains(ext)){
WindowsUtils.OpenAssociatedProgram(path);
App.SystemHandler.OpenAssociatedProgram(path);
}
else{
FormMessage.Error("Image Download", "Invalid file extension " + ext, FormMessage.OK);

View File

@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Threading;
using Microsoft.Win32;
@ -18,24 +16,6 @@ private static bool OSVersionEquals(int major, int minor){
return ver.Major == major && ver.Minor == minor;
}
public static bool OpenAssociatedProgram(string file, string arguments = "", bool runElevated = false){
try{
using(Process.Start(new ProcessStartInfo{
FileName = file,
Arguments = arguments,
Verb = runElevated ? "runas" : string.Empty,
ErrorDialog = true
})){
return true;
}
}catch(Win32Exception e) when (e.NativeErrorCode == 0x000004C7){ // operation canceled by the user
return false;
}catch(Exception e){
Program.Reporter.HandleException("Error Opening Program", "Could not open the associated program for " + file, true, e);
return false;
}
}
public static bool TrySleepUntil(Func<bool> test, int timeoutMillis, int timeStepMillis){
for(int waited = 0; waited < timeoutMillis; waited += timeStepMillis){
if (test()){

View File

@ -1,5 +1,6 @@
namespace TweetLib.Core.Application{
public interface IAppSystemHandler{
void OpenAssociatedProgram(string path);
void OpenFileExplorer(string path);
}
}