mirror of
https://github.com/chylex/TweetDuck.git
synced 2025-04-30 23:34:09 +02:00
Refactor WindowsUtils.OpenAssociatedProgram & update installer code
This commit is contained in:
parent
458eeeccda
commit
f1b7cd633e
@ -1,9 +1,21 @@
|
|||||||
using System.Diagnostics;
|
using System;
|
||||||
|
using System.Diagnostics;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using TweetLib.Core.Application;
|
using TweetLib.Core.Application;
|
||||||
|
|
||||||
namespace TweetDuck.Application{
|
namespace TweetDuck.Application{
|
||||||
class SystemHandler : IAppSystemHandler{
|
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){
|
void IAppSystemHandler.OpenFileExplorer(string path){
|
||||||
if (File.Exists(path)){
|
if (File.Exists(path)){
|
||||||
using(Process.Start("explorer.exe", "/select,\"" + path.Replace('/', '\\') + "\"")){}
|
using(Process.Start("explorer.exe", "/select,\"" + path.Replace('/', '\\') + "\"")){}
|
||||||
|
@ -44,7 +44,7 @@ public bool IsWaiting{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public string UpdateInstallerPath { get; private set; }
|
public UpdateInstaller UpdateInstaller { get; private set; }
|
||||||
private bool ignoreUpdateCheckError;
|
private bool ignoreUpdateCheckError;
|
||||||
|
|
||||||
public AnalyticsFile AnalyticsFile => analytics?.File ?? AnalyticsFile.Dummy;
|
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){
|
private void FormBrowser_FormClosed(object sender, FormClosedEventArgs e){
|
||||||
if (isLoaded && UpdateInstallerPath == null){
|
if (isLoaded && UpdateInstaller == null){
|
||||||
updateBridge.Cleanup();
|
updateBridge.Cleanup();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -311,7 +311,7 @@ void OnFinished(){
|
|||||||
UpdateDownloadStatus status = update.DownloadStatus;
|
UpdateDownloadStatus status = update.DownloadStatus;
|
||||||
|
|
||||||
if (status == UpdateDownloadStatus.Done){
|
if (status == UpdateDownloadStatus.Done){
|
||||||
UpdateInstallerPath = update.InstallerPath;
|
UpdateInstaller = new UpdateInstaller(update.InstallerPath);
|
||||||
ForceClose();
|
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)){
|
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)){
|
||||||
|
@ -192,14 +192,10 @@ private static void Main(){
|
|||||||
Resources.Initialize(mainForm);
|
Resources.Initialize(mainForm);
|
||||||
Win.Application.Run(mainForm);
|
Win.Application.Run(mainForm);
|
||||||
|
|
||||||
if (mainForm.UpdateInstallerPath != null){
|
if (mainForm.UpdateInstaller != null){
|
||||||
ExitCleanup();
|
ExitCleanup();
|
||||||
|
|
||||||
// ProgramPath has a trailing backslash
|
if (mainForm.UpdateInstaller.Launch()){
|
||||||
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)){
|
|
||||||
Win.Application.Exit();
|
Win.Application.Exit();
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
|
@ -54,6 +54,7 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="Plugins\PluginSchemeFactory.cs" />
|
<Compile Include="Plugins\PluginSchemeFactory.cs" />
|
||||||
|
<Compile Include="Updates\UpdateInstaller.cs" />
|
||||||
<Compile Include="Version.cs" />
|
<Compile Include="Version.cs" />
|
||||||
<Compile Include="Configuration\Arguments.cs" />
|
<Compile Include="Configuration\Arguments.cs" />
|
||||||
<Compile Include="Configuration\ConfigManager.cs" />
|
<Compile Include="Configuration\ConfigManager.cs" />
|
||||||
|
37
Updates/UpdateInstaller.cs
Normal file
37
Updates/UpdateInstaller.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -9,6 +9,7 @@
|
|||||||
using TweetDuck.Configuration;
|
using TweetDuck.Configuration;
|
||||||
using TweetDuck.Dialogs;
|
using TweetDuck.Dialogs;
|
||||||
using TweetDuck.Management;
|
using TweetDuck.Management;
|
||||||
|
using TweetLib.Core;
|
||||||
using TweetLib.Core.Features.Twitter;
|
using TweetLib.Core.Features.Twitter;
|
||||||
|
|
||||||
namespace TweetDuck.Utils{
|
namespace TweetDuck.Utils{
|
||||||
@ -105,7 +106,7 @@ public static void OpenExternalBrowser(string url){
|
|||||||
string browserPath = Config.BrowserPath;
|
string browserPath = Config.BrowserPath;
|
||||||
|
|
||||||
if (browserPath == null || !File.Exists(browserPath)){
|
if (browserPath == null || !File.Exists(browserPath)){
|
||||||
WindowsUtils.OpenAssociatedProgram(url);
|
App.SystemHandler.OpenAssociatedProgram(url);
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
string quotedUrl = '"' + url + '"';
|
string quotedUrl = '"' + url + '"';
|
||||||
|
@ -9,6 +9,7 @@
|
|||||||
using TweetDuck.Browser.Data;
|
using TweetDuck.Browser.Data;
|
||||||
using TweetDuck.Dialogs;
|
using TweetDuck.Dialogs;
|
||||||
using TweetDuck.Management;
|
using TweetDuck.Management;
|
||||||
|
using TweetLib.Core;
|
||||||
using TweetLib.Core.Features.Twitter;
|
using TweetLib.Core.Features.Twitter;
|
||||||
using TweetLib.Core.Utils;
|
using TweetLib.Core.Utils;
|
||||||
using Cookie = CefSharp.Cookie;
|
using Cookie = CefSharp.Cookie;
|
||||||
@ -44,7 +45,7 @@ public static void ViewImage(string url, ImageQuality quality){
|
|||||||
string ext = Path.GetExtension(path);
|
string ext = Path.GetExtension(path);
|
||||||
|
|
||||||
if (ImageUrl.ValidExtensions.Contains(ext)){
|
if (ImageUrl.ValidExtensions.Contains(ext)){
|
||||||
WindowsUtils.OpenAssociatedProgram(path);
|
App.SystemHandler.OpenAssociatedProgram(path);
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
FormMessage.Error("Image Download", "Invalid file extension " + ext, FormMessage.OK);
|
FormMessage.Error("Image Download", "Invalid file extension " + ext, FormMessage.OK);
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.ComponentModel;
|
|
||||||
using System.Diagnostics;
|
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using Microsoft.Win32;
|
using Microsoft.Win32;
|
||||||
@ -18,24 +16,6 @@ private static bool OSVersionEquals(int major, int minor){
|
|||||||
return ver.Major == major && ver.Minor == 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){
|
public static bool TrySleepUntil(Func<bool> test, int timeoutMillis, int timeStepMillis){
|
||||||
for(int waited = 0; waited < timeoutMillis; waited += timeStepMillis){
|
for(int waited = 0; waited < timeoutMillis; waited += timeStepMillis){
|
||||||
if (test()){
|
if (test()){
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
namespace TweetLib.Core.Application{
|
namespace TweetLib.Core.Application{
|
||||||
public interface IAppSystemHandler{
|
public interface IAppSystemHandler{
|
||||||
|
void OpenAssociatedProgram(string path);
|
||||||
void OpenFileExplorer(string path);
|
void OpenFileExplorer(string path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user