From 92b56299567e9790d39bb5dd5c345b65d43e3bd2 Mon Sep 17 00:00:00 2001 From: Tichau Date: Mon, 10 Oct 2016 18:42:20 +0200 Subject: [PATCH] Fixed: Microsoft office applications detection --- .../ConversionJobs/ConversionJob_Excel.cs | 11 ++- .../ConversionJobs/ConversionJob_Office.cs | 36 +++++++++- .../ConversionJob_PowerPoint.cs | 13 +++- .../ConversionJobs/ConversionJob_Word.cs | 11 ++- Application/FileConverter/Helpers.cs | 72 ++++++++++++------- .../Properties/Resources.Designer.cs | 27 +++++++ .../Properties/Resources.en.resx | 9 +++ .../Properties/Resources.fr.resx | 9 +++ .../FileConverter/Properties/Resources.resx | 9 +++ .../FileConverter/Windows/InputExtension.cs | 20 +++++- 10 files changed, 182 insertions(+), 35 deletions(-) diff --git a/Application/FileConverter/ConversionJobs/ConversionJob_Excel.cs b/Application/FileConverter/ConversionJobs/ConversionJob_Excel.cs index b0d6ec1..bd80a7b 100644 --- a/Application/FileConverter/ConversionJobs/ConversionJob_Excel.cs +++ b/Application/FileConverter/ConversionJobs/ConversionJob_Excel.cs @@ -6,9 +6,10 @@ namespace FileConverter.ConversionJobs using System.IO; using System.Threading.Tasks; - using Excel = Microsoft.Office.Interop.Excel; using FileConverter.Diagnostics; + using Excel = Microsoft.Office.Interop.Excel; + public class ConversionJob_Excel : ConversionJob_Office { private Excel.Workbook document; @@ -25,6 +26,14 @@ namespace FileConverter.ConversionJobs { } + protected override ApplicationName Application + { + get + { + return ApplicationName.Excel; + } + } + protected override int GetOuputFilesCount() { if (this.ConversionPreset.OutputType == OutputType.Pdf) diff --git a/Application/FileConverter/ConversionJobs/ConversionJob_Office.cs b/Application/FileConverter/ConversionJobs/ConversionJob_Office.cs index 088b27b..1519302 100644 --- a/Application/FileConverter/ConversionJobs/ConversionJob_Office.cs +++ b/Application/FileConverter/ConversionJobs/ConversionJob_Office.cs @@ -12,14 +12,44 @@ namespace FileConverter.ConversionJobs { } + public enum ApplicationName + { + None, + + Word, + Excel, + PowerPoint + } + + protected abstract ApplicationName Application + { + get; + } + protected override void Initialize() { base.Initialize(); - if (!Helpers.IsMicrosoftOfficeAvailable()) + if (!Helpers.IsMicrosoftOfficeApplicationAvailable(this.Application)) { - this.ConversionFailed(Properties.Resources.ErrorMicrosoftOfficeIsNotAvailable); - return; + switch (this.Application) + { + case ApplicationName.Word: + this.ConversionFailed(Properties.Resources.ErrorMicrosoftWordIsNotAvailable); + return; + + case ApplicationName.PowerPoint: + this.ConversionFailed(Properties.Resources.ErrorMicrosoftPowerPointIsNotAvailable); + return; + + case ApplicationName.Excel: + this.ConversionFailed(Properties.Resources.ErrorMicrosoftExcelIsNotAvailable); + return; + + default: + this.ConversionFailed(Properties.Resources.ErrorMicrosoftOfficeIsNotAvailable); + return; + } } } diff --git a/Application/FileConverter/ConversionJobs/ConversionJob_PowerPoint.cs b/Application/FileConverter/ConversionJobs/ConversionJob_PowerPoint.cs index 5d19d42..1c46344 100644 --- a/Application/FileConverter/ConversionJobs/ConversionJob_PowerPoint.cs +++ b/Application/FileConverter/ConversionJobs/ConversionJob_PowerPoint.cs @@ -6,9 +6,10 @@ namespace FileConverter.ConversionJobs using System.IO; using System.Threading.Tasks; - using Microsoft.Office.Core; - using PowerPoint = Microsoft.Office.Interop.PowerPoint; using FileConverter.Diagnostics; + using Microsoft.Office.Core; + + using PowerPoint = Microsoft.Office.Interop.PowerPoint; public class ConversionJob_PowerPoint : ConversionJob_Office { @@ -26,6 +27,14 @@ namespace FileConverter.ConversionJobs { } + protected override ApplicationName Application + { + get + { + return ApplicationName.PowerPoint; + } + } + protected override int GetOuputFilesCount() { if (this.ConversionPreset.OutputType == OutputType.Pdf) diff --git a/Application/FileConverter/ConversionJobs/ConversionJob_Word.cs b/Application/FileConverter/ConversionJobs/ConversionJob_Word.cs index dd7e7d2..c102c00 100644 --- a/Application/FileConverter/ConversionJobs/ConversionJob_Word.cs +++ b/Application/FileConverter/ConversionJobs/ConversionJob_Word.cs @@ -6,9 +6,10 @@ namespace FileConverter.ConversionJobs using System.IO; using System.Threading.Tasks; - using Word = Microsoft.Office.Interop.Word; using FileConverter.Diagnostics; + using Word = Microsoft.Office.Interop.Word; + public class ConversionJob_Word : ConversionJob_Office { private Word.Document document; @@ -25,6 +26,14 @@ namespace FileConverter.ConversionJobs { } + protected override ApplicationName Application + { + get + { + return ApplicationName.Word; + } + } + protected override int GetOuputFilesCount() { if (this.ConversionPreset.OutputType == OutputType.Pdf) diff --git a/Application/FileConverter/Helpers.cs b/Application/FileConverter/Helpers.cs index a7a71ee..a4c022b 100644 --- a/Application/FileConverter/Helpers.cs +++ b/Application/FileConverter/Helpers.cs @@ -10,23 +10,11 @@ namespace FileConverter using System.Reflection; using System.Threading; + using FileConverter.ConversionJobs; using Microsoft.Win32; public static class Helpers { - private static readonly string[] CompatibleOfficeVersions = new string[] - { - "16.0", // Office 2016 - "15.0", // Office 2013 - "14.0", // Office 2010 - "12.0", // Office 2007 - "11.0", // Office 2003 - "10.0", // Office XP - "9.0", // Office 2000 - "8.0", // Office 98 - "7.0", // Office 97 - }; - public static IEnumerable GetSupportedCultures() { // Get all cultures. @@ -184,42 +172,74 @@ namespace FileConverter /// /// Returns true if Office is installed on the computer. /// source: http://stackoverflow.com/questions/3266675/how-to-detect-installed-version-of-ms-office/3267832#3267832 - public static bool IsMicrosoftOfficeAvailable() + /// source: http://www.codeproject.com/Articles/26520/Getting-Office-s-Version + public static bool IsMicrosoftOfficeApplicationAvailable(ConversionJobs.ConversionJob_Office.ApplicationName application) { - string registryKeyPattern = @"Software\Microsoft\Office\{0}\Common"; - - for (int index = 0; index < Helpers.CompatibleOfficeVersions.Length; index++) + string registryKeyPattern = @"Software\Microsoft\Windows\CurrentVersion\App Paths\"; + switch (application) { - string registryKeyPath = string.Format(registryKeyPattern, Helpers.CompatibleOfficeVersions[index]); - RegistryKey officeRegistryKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(registryKeyPath); - if (officeRegistryKey != null) + case ConversionJob_Office.ApplicationName.Word: + registryKeyPattern += "winword.exe"; + break; + + case ConversionJob_Office.ApplicationName.PowerPoint: + registryKeyPattern += "powerpnt.exe"; + break; + + case ConversionJob_Office.ApplicationName.Excel: + registryKeyPattern += "excel.exe"; + break; + + case ConversionJob_Office.ApplicationName.None: + return false; + } + + // Looks inside CURRENT_USER. + RegistryKey winwordKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(registryKeyPattern, false); + if (winwordKey != null) + { + string winwordPath = winwordKey.GetValue(string.Empty).ToString(); + if (!string.IsNullOrEmpty(winwordPath)) + { + return true; + } + } + + // If not found, looks inside LOCAL_MACHINE. + winwordKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(registryKeyPattern, false); + if (winwordKey != null) + { + string winwordPath = winwordKey.GetValue(string.Empty).ToString(); + if (!string.IsNullOrEmpty(winwordPath)) { - Diagnostics.Debug.Log($"Registry key '{registryKeyPath}' found"); return true; } } - Diagnostics.Debug.Log("No compatible office version found."); return false; } - public static bool IsExtensionCompatibleWithOffice(string extension) + public static ConversionJob_Office.ApplicationName GetOfficeApplicationCompatibleWithExtension(string extension) { switch (extension) { case "doc": case "docx": + case "odt": + return ConversionJob_Office.ApplicationName.Word; + case "ppt": case "pptx": case "odp": + return ConversionJob_Office.ApplicationName.PowerPoint; + case "ods": - case "odt": case "xls": case "xlsx": - return true; + return ConversionJob_Office.ApplicationName.Excel; } - return false; + return ConversionJob_Office.ApplicationName.None; } public static class InputCategoryNames diff --git a/Application/FileConverter/Properties/Resources.Designer.cs b/Application/FileConverter/Properties/Resources.Designer.cs index 9f68979..6d7be31 100644 --- a/Application/FileConverter/Properties/Resources.Designer.cs +++ b/Application/FileConverter/Properties/Resources.Designer.cs @@ -537,6 +537,15 @@ namespace FileConverter.Properties { } } + /// + /// Looks up a localized string similar to Microsoft Excel must be installed in order to convert Excel documents.. + /// + public static string ErrorMicrosoftExcelIsNotAvailable { + get { + return ResourceManager.GetString("ErrorMicrosoftExcelIsNotAvailable", resourceCulture); + } + } + /// /// Looks up a localized string similar to Microsoft Office must be installed in order to convert Office documents.. /// @@ -546,6 +555,24 @@ namespace FileConverter.Properties { } } + /// + /// Looks up a localized string similar to Microsoft PowerPoint must be installed in order to convert PowerPoint documents.. + /// + public static string ErrorMicrosoftPowerPointIsNotAvailable { + get { + return ResourceManager.GetString("ErrorMicrosoftPowerPointIsNotAvailable", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Microsoft Word must be installed in order to convert Word documents.. + /// + public static string ErrorMicrosoftWordIsNotAvailable { + get { + return ResourceManager.GetString("ErrorMicrosoftWordIsNotAvailable", resourceCulture); + } + } + /// /// Looks up a localized string similar to Fail to open document with Microsoft Office (check if your licence/installation is valid).. /// diff --git a/Application/FileConverter/Properties/Resources.en.resx b/Application/FileConverter/Properties/Resources.en.resx index f27bf53..ca362ac 100644 --- a/Application/FileConverter/Properties/Resources.en.resx +++ b/Application/FileConverter/Properties/Resources.en.resx @@ -532,4 +532,13 @@ use maj for uppercase version Fail to open document with Microsoft Office (check if your licence/installation are valid). + + Microsoft Excel must be installed in order to convert Excel documents. + + + Microsoft PowerPoint must be installed in order to convert PowerPoint documents. + + + Microsoft Word must be installed in order to convert Word documents. + \ No newline at end of file diff --git a/Application/FileConverter/Properties/Resources.fr.resx b/Application/FileConverter/Properties/Resources.fr.resx index ea24a33..59dbeef 100644 --- a/Application/FileConverter/Properties/Resources.fr.resx +++ b/Application/FileConverter/Properties/Resources.fr.resx @@ -550,4 +550,13 @@ utilisez maj pour une version en majuscules Impossible d'ouvrir le document avec Microsoft Office (vérifiez que la licence/l'installation sont valides). + + Microsoft Excel doit être installé pour pouvoir convertir les documents Excel. + + + Microsoft PowerPoint doit être installé pour pouvoir convertir les documents PowerPoint. + + + Microsoft Word doit être installé pour pouvoir convertir les documents Word. + \ No newline at end of file diff --git a/Application/FileConverter/Properties/Resources.resx b/Application/FileConverter/Properties/Resources.resx index ad8c030..8857064 100644 --- a/Application/FileConverter/Properties/Resources.resx +++ b/Application/FileConverter/Properties/Resources.resx @@ -532,4 +532,13 @@ use maj for uppercase version Fail to open document with Microsoft Office (check if your licence/installation is valid). + + Microsoft Excel must be installed in order to convert Excel documents. + + + Microsoft PowerPoint must be installed in order to convert PowerPoint documents. + + + Microsoft Word must be installed in order to convert Word documents. + \ No newline at end of file diff --git a/Application/FileConverter/Windows/InputExtension.cs b/Application/FileConverter/Windows/InputExtension.cs index 1187fa6..fcebe97 100644 --- a/Application/FileConverter/Windows/InputExtension.cs +++ b/Application/FileConverter/Windows/InputExtension.cs @@ -7,6 +7,7 @@ namespace FileConverter using System.Windows.Media; using FileConverter.Annotations; + using FileConverter.ConversionJobs; public class InputExtension : INotifyPropertyChanged { @@ -21,14 +22,29 @@ namespace FileConverter { this.Name = name; - if (!Helpers.IsExtensionCompatibleWithOffice(name) || Helpers.IsMicrosoftOfficeAvailable()) + ConversionJob_Office.ApplicationName officeApplication = Helpers.GetOfficeApplicationCompatibleWithExtension(name); + + if (officeApplication == ConversionJob_Office.ApplicationName.None || Helpers.IsMicrosoftOfficeApplicationAvailable(officeApplication)) { this.ForegroundBrush = this.defaultBrush; } else { this.ForegroundBrush = this.errorBrush; - this.ToolTip = Properties.Resources.ErrorMicrosoftOfficeIsNotAvailable; + switch (officeApplication) + { + case ConversionJob_Office.ApplicationName.Word: + this.ToolTip = Properties.Resources.ErrorMicrosoftWordIsNotAvailable; + break; + + case ConversionJob_Office.ApplicationName.PowerPoint: + this.ToolTip = Properties.Resources.ErrorMicrosoftPowerPointIsNotAvailable; + break; + + case ConversionJob_Office.ApplicationName.Excel: + this.ToolTip = Properties.Resources.ErrorMicrosoftExcelIsNotAvailable; + break; + } } }