diff --git a/Application/FileConverter/Application.xaml.cs b/Application/FileConverter/Application.xaml.cs index 54ea771..782ee0d 100644 --- a/Application/FileConverter/Application.xaml.cs +++ b/Application/FileConverter/Application.xaml.cs @@ -211,16 +211,7 @@ namespace FileConverter // Retrieve arguments. Debug.Log("Retrieve arguments..."); string[] args = Environment.GetCommandLineArgs(); - -#if (DEBUG) - { - System.Array.Resize(ref args, 3); - args[1] = "--settings"; - args[2] = "--verbose"; - } - -#endif - + // Log arguments. for (int index = 0; index < args.Length; index++) { @@ -240,6 +231,8 @@ namespace FileConverter ISettingsService settingsService = SimpleIoc.Default.GetInstance(); // Parse arguments. + bool quitAfterStartup = false; + int quitExitCode = 0; List filePaths = new List(); string conversionPresetName = null; for (int index = 1; index < args.Length; index++) @@ -258,14 +251,65 @@ namespace FileConverter switch (parameterTitle) { case "post-install-init": - settingsService.PostInstallationInitialization(); - Dispatcher.BeginInvoke((Action)(() => Application.Current.Shutdown())); - return; + if (!settingsService.PostInstallationInitialization()) + { + quitExitCode = 0x0F; + Debug.LogError(quitExitCode, $"Failed to execute post install initialization."); + } + + quitAfterStartup = true; + break; + + case "register-shell-extension": + { + quitAfterStartup = true; + + if (index >= args.Length - 1) + { + quitExitCode = 0x0B; + Debug.LogError(quitExitCode, $"Invalid format."); + break; + } + + string shellExtensionPath = args[index + 1]; + index++; + + if (!Helpers.RegisterShellExtension(shellExtensionPath)) + { + quitExitCode = 0x0C; + Debug.LogError(quitExitCode, $"Failed to register shell extension {shellExtensionPath}."); + } + + break; + } + + case "unregister-shell-extension": + { + quitAfterStartup = true; + + if (index >= args.Length - 1) + { + quitExitCode = 0x0D; + Debug.LogError(quitExitCode, $"Invalid format."); + break; + } + + string shellExtensionPath = args[index + 1]; + index++; + + if (!Helpers.UnregisterExtension(shellExtensionPath)) + { + quitExitCode = 0x0E; + Debug.LogError(quitExitCode, $"Failed to unregister shell extension {shellExtensionPath}."); + } + + break; + } case "version": - Console.Write(ApplicationVersion.ToString()); - Dispatcher.BeginInvoke((Action)(() => Application.Current.Shutdown())); - return; + Console.WriteLine(ApplicationVersion.ToString()); + quitAfterStartup = true; + break; case "settings": this.showSettings = true; @@ -273,20 +317,21 @@ namespace FileConverter case "apply-settings": settingsService.ApplyTemporarySettings(); - Dispatcher.BeginInvoke((Action)(() => Application.Current.Shutdown())); - return; + quitAfterStartup = true; + break; case "conversion-preset": if (index >= args.Length - 1) { - Debug.LogError("Invalid format. (code 0x01)"); - Dispatcher.BeginInvoke((Action)(() => Application.Current.Shutdown())); - return; + quitAfterStartup = true; + quitExitCode = 0x01; + Debug.LogError(quitExitCode, $"Invalid format."); + break; } conversionPresetName = args[index + 1]; index++; - continue; + break; case "verbose": { @@ -308,10 +353,18 @@ namespace FileConverter if (settingsService.Settings == null) { - Diagnostics.Debug.LogError("The application will now shutdown. If you want to fix the problem yourself please edit or delete the file: C:\\Users\\UserName\\AppData\\Local\\FileConverter\\Settings.user.xml."); - Dispatcher.BeginInvoke((Action)(() => Application.Current.Shutdown())); + Debug.LogError("Can't load File Converter settings. The application will now shutdown, if you want to fix the problem yourself please edit or delete the file: C:\\Users\\UserName\\AppData\\Local\\FileConverter\\Settings.user.xml."); + quitAfterStartup = true; + quitExitCode = 13; + } + + if (quitAfterStartup) + { + Dispatcher.BeginInvoke((Action)(() => Application.Current.Shutdown(quitExitCode))); return; } + + Debug.Assert(quitExitCode == 0, "An error happened during the initialization."); // Check for upgrade. if (settingsService.Settings.CheckUpgradeAtStartup) diff --git a/Application/FileConverter/FileConverter.csproj b/Application/FileConverter/FileConverter.csproj index 20ddd59..eba559f 100644 --- a/Application/FileConverter/FileConverter.csproj +++ b/Application/FileConverter/FileConverter.csproj @@ -106,8 +106,13 @@ False ..\..\Middleware\Ripper.dll + + ..\..\packages\SharpShell.2.7.1\lib\net40-client\SharpShell.dll + + + ..\..\packages\MvvmLightLibs.5.4.1.1\lib\net45\System.Windows.Interactivity.dll @@ -335,7 +340,9 @@ - + + Designer + @@ -379,7 +386,12 @@ - + + + {0c44ca69-42d6-4357-bdfd-83069d1aba2f} + FileConverterExtension + + copy /Y "$(SolutionDir)Middleware\ffmpeg\$(PlatformName)\ffmpeg.exe" "$(TargetDir)ffmpeg.exe" diff --git a/Application/FileConverter/Helpers.cs b/Application/FileConverter/Helpers.cs index 4549209..b9808de 100644 --- a/Application/FileConverter/Helpers.cs +++ b/Application/FileConverter/Helpers.cs @@ -1,4 +1,4 @@ -// // License: http://www.gnu.org/licenses/gpl.html GPL version 3. +// License: http://www.gnu.org/licenses/gpl.html GPL version 3. namespace FileConverter { @@ -6,19 +6,104 @@ namespace FileConverter using System.Collections.Generic; using System.Globalization; using System.IO; - using System.Linq; using System.Reflection; using System.Threading; + using System.ComponentModel.Composition.Hosting; using FileConverter.ConversionJobs; using FileConverter.Services; + using SharpShell; + using SharpShell.ServerRegistration; using GalaSoft.MvvmLight.Ioc; using Microsoft.Win32; public static class Helpers { + public static bool RegisterShellExtension(string shellExtensionPath) + { + if (!Application.IsInAdmininstratorPrivileges) + { + Diagnostics.Debug.LogError("File Converter needs administrator privileges to register the shell extension."); + return false; + } + + if (!File.Exists(shellExtensionPath)) + { + Diagnostics.Debug.LogError($"Shell extension {shellExtensionPath} does not exists."); + return false; + } + + Diagnostics.Debug.Log($"Install and register shell extension: {shellExtensionPath}."); + + try + { + var catalog = new AssemblyCatalog(shellExtensionPath); + var container = new CompositionContainer(catalog); + var server = container.GetExport().Value; + + RegistrationType registrationType = RegistrationType.OS64Bit; +#if BUILD32 + registrationType = RegistrationType.OS32Bit; +#endif + + ServerRegistrationManager.InstallServer(server, registrationType, true); + Diagnostics.Debug.Log($"Shell extension has been installed correctly."); + + ServerRegistrationManager.RegisterServer(server, registrationType); + Diagnostics.Debug.Log($"Shell extension has been registered correctly."); + } + catch (Exception exception) + { + Diagnostics.Debug.LogError($"An exception has been thrown during shell extension {shellExtensionPath} registration.\n{exception}"); + return false; + } + + return true; + } + + public static bool UnregisterExtension(string shellExtensionPath) + { + if (!Application.IsInAdmininstratorPrivileges) + { + Diagnostics.Debug.LogError("File Converter needs administrator privileges to unregister the shell extension."); + return false; + } + + if (!File.Exists(shellExtensionPath)) + { + Diagnostics.Debug.LogError($"Shell extension {shellExtensionPath} does not exists."); + return false; + } + + Diagnostics.Debug.Log($"Unregister and uninstall shell extension: {shellExtensionPath}."); + + try + { + var catalog = new AssemblyCatalog(shellExtensionPath); + var container = new CompositionContainer(catalog); + var server = container.GetExport().Value; + + RegistrationType registrationType = RegistrationType.OS64Bit; +#if BUILD32 + registrationType = RegistrationType.OS32Bit; +#endif + + ServerRegistrationManager.UnregisterServer(server, registrationType); + Diagnostics.Debug.Log($"Shell extension has been successfully unregistered."); + ServerRegistrationManager.UninstallServer(server, registrationType); + Diagnostics.Debug.Log($"Shell extension has been successfully uninstalled."); + } + catch (Exception exception) + { + Diagnostics.Debug.LogError($"An exception has been thrown during shell extension {shellExtensionPath} unregistration.\n{exception}"); + return false; + } + + return true; + } + public static IEnumerable GetSupportedCultures() { // Get all cultures. diff --git a/Application/FileConverter/Services/ISettingsService.cs b/Application/FileConverter/Services/ISettingsService.cs index acbf016..87c3eea 100644 --- a/Application/FileConverter/Services/ISettingsService.cs +++ b/Application/FileConverter/Services/ISettingsService.cs @@ -9,7 +9,7 @@ namespace FileConverter.Services get; } - void PostInstallationInitialization(); + bool PostInstallationInitialization(); void ApplyTemporarySettings(); diff --git a/Application/FileConverter/Services/SettingsService.cs b/Application/FileConverter/Services/SettingsService.cs index c2085f2..de4b0eb 100644 --- a/Application/FileConverter/Services/SettingsService.cs +++ b/Application/FileConverter/Services/SettingsService.cs @@ -72,8 +72,10 @@ namespace FileConverter.Services } } - public void PostInstallationInitialization() + public bool PostInstallationInitialization() { + Debug.Log("Execute post installation initialization."); + Settings defaultSettings = null; // Load the default settings. @@ -85,12 +87,14 @@ namespace FileConverter.Services } catch (Exception exception) { - Debug.LogError("Fail to load file converter default settings. {0}", exception.Message); + Debug.LogError($"Fail to load file converter default settings. {exception.Message}"); + return false; } } else { Debug.LogError("Default settings not found at path {0}. You should try to reinstall the application.", this.DefaultSettingsFilePath); + return false; } // Load user settings if exists. @@ -131,7 +135,7 @@ namespace FileConverter.Services } Settings settings = userSettings != null ? userSettings.Merge(defaultSettings) : defaultSettings; - this.Save(settings); + return this.Save(settings); } public void ApplyTemporarySettings() @@ -148,7 +152,7 @@ namespace FileConverter.Services RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(@"Software\FileConverter"); if (registryKey == null) { - Debug.LogError("Can't apply settings in registry. (code 0x03)"); + Debug.LogError(0x03, "Can't apply settings in registry."); return; } @@ -241,7 +245,7 @@ namespace FileConverter.Services return settings; } - private void Save(Settings settings) + private bool Save(Settings settings) { if (settings == null) { @@ -279,7 +283,8 @@ namespace FileConverter.Services } else { - MessageBox.Show("Can't apply settings in registry. (code 0x09)", "Error", MessageBoxButton.OK, MessageBoxImage.Error); + Debug.LogError(0x09, "Can't apply settings in registry."); + return false; } } } @@ -289,6 +294,8 @@ namespace FileConverter.Services File.Copy(this.UserSettingsTemporaryFilePath, this.UserSettingsFilePath, true); File.Delete(this.UserSettingsTemporaryFilePath); } + + return true; } private void RunSaveInAdminMode() @@ -374,7 +381,7 @@ namespace FileConverter.Services RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(@"Software\FileConverter", RegistryKeyPermissionCheck.ReadWriteSubTree); if (registryKey == null) { - MessageBox.Show("Can't apply settings in registry. (code 0x05)", "Error", MessageBoxButton.OK, MessageBoxImage.Error); + Debug.LogError(0x05, "Can't find File Converter registry entry."); return false; } @@ -388,7 +395,7 @@ namespace FileConverter.Services } catch (Exception) { - MessageBox.Show("Can't apply settings in registry. (code 0x06)", "Error", MessageBoxButton.OK, MessageBoxImage.Error); + Debug.LogError(0x06, "Can't apply settings in registry."); return false; } } @@ -419,7 +426,7 @@ namespace FileConverter.Services if (subKey == null) { - MessageBox.Show("Can't apply settings in registry. (code 0x07)", "Error", MessageBoxButton.OK, MessageBoxImage.Error); + Debug.LogError(0x07, "Can't apply settings in registry."); return false; } diff --git a/Application/FileConverter/packages.config b/Application/FileConverter/packages.config index 4458e25..7023713 100644 --- a/Application/FileConverter/packages.config +++ b/Application/FileConverter/packages.config @@ -8,5 +8,6 @@ + \ No newline at end of file