1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
5
using System.Diagnostics;
6
using System.Globalization;
8
using System.Text.RegularExpressions;
9
using ICSharpCode.Core;
11
namespace MSHelpSystem.Core
13
public sealed class HelpLibraryManager
19
public static bool IsRunning
23
Process[] managers = Process.GetProcessesByName("HelpLibManager");
24
LoggingService.Debug(string.Format("Help 3.0: {0} {1} of HelpLibraryManager.exe found", managers.Length, (managers.Length == 1)?"process":"processes"));
25
return managers.Length > 0;
29
public static string Manager
33
if (string.IsNullOrEmpty(Help3Environment.AppRoot)) return string.Empty;
34
string manager = Path.Combine(Help3Environment.AppRoot, "HelpLibManager.exe");
35
LoggingService.Debug(string.Format("Help 3.0: Help library manager is \"{0}\"", manager));
36
return (File.Exists(manager)) ? manager : string.Empty;
40
#region InitializeLocaleStore
42
public static void InitializeLocaleStore(string productCode, string productVersion)
44
InitializeLocaleStore(productCode, productVersion, CultureInfo.CurrentCulture.Name.ToUpper(), string.Empty);
47
public static void InitializeLocaleStore(string productCode, string productVersion, string locale)
49
InitializeLocaleStore(productCode, productVersion, locale, string.Empty);
52
public static void InitializeLocaleStore(string productCode, string productVersion, string locale, string brandingPackage)
54
if (Help3Environment.IsLocalStoreInitialized) { return; }
55
if (string.IsNullOrEmpty(productCode)) { throw new ArgumentNullException("productCode"); }
56
if (string.IsNullOrEmpty(productVersion)) { throw new ArgumentNullException("productVersion"); }
57
if (string.IsNullOrEmpty(locale)) { throw new ArgumentNullException("locale"); }
58
if (!Regex.IsMatch(productVersion, @"^\d{3}$", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase)) { throw new ArgumentOutOfRangeException("productVersion"); }
59
if (!Regex.IsMatch(locale, @"^\w{2}-\w{2}", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase)) { throw new ArgumentOutOfRangeException("locale"); }
61
string brandingSwitch = (!string.IsNullOrEmpty(brandingPackage)) ? string.Format("/brandingPackage \"{0}\"", brandingPackage):"";
62
string arguments = string.Format("/product {0} /version {1} /locale {2} /content \"{3}\" {4}", productCode, productVersion, locale, Help3Environment.BuildLocalStoreFolder, brandingSwitch);
64
LoggingService.Debug(string.Format("Help 3.0: Initializing local store with \"{0}\"", arguments));
65
HelpLibManagerProcessRunner(arguments);
70
#region InstallHelpDocumentsFromLocalSource
72
public static void InstallHelpDocumentsFromLocalSource(string productCode, string productVersion, string locale, string sourceMedia)
74
InstallHelpDocumentsFromLocalSource(productCode, productVersion, locale, sourceMedia, string.Empty);
77
public static void InstallHelpDocumentsFromLocalSource(string productCode, string productVersion, string locale, string sourceMedia, string brandingPackage)
79
if (string.IsNullOrEmpty(productCode)) { throw new ArgumentNullException("productCode"); }
80
if (string.IsNullOrEmpty(productVersion)) { throw new ArgumentNullException("productVersion"); }
81
if (string.IsNullOrEmpty(locale)) { throw new ArgumentNullException("locale"); }
82
if (string.IsNullOrEmpty(sourceMedia)) { throw new ArgumentNullException("sourceMedia"); }
83
if (!File.Exists(sourceMedia)) { throw new FileNotFoundException(); }
84
if (!Regex.IsMatch(productVersion, @"^\d{3}$", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase)) { throw new ArgumentOutOfRangeException("productVersion"); }
85
if (!Regex.IsMatch(locale, @"^\w{2}-\w{2}", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase)) { throw new ArgumentOutOfRangeException("locale"); }
87
string initLS = (!Help3Environment.IsLocalStoreInitialized) ? string.Format("/content \"{0}\"", Help3Environment.BuildLocalStoreFolder):"";
88
string brandingSwitch = (!string.IsNullOrEmpty(brandingPackage)) ? string.Format("/brandingPackage \"{0}\"", brandingPackage):"";
89
string arguments = string.Format("/product {0} /version {1} /locale {2} /sourceMedia \"{3}\" {4} {5}", productCode, productVersion, locale, sourceMedia, initLS, brandingSwitch);
91
LoggingService.Debug(string.Format("Help 3.0: Installing local help documents with \"{0}\"", arguments));
92
HelpLibManagerProcessRunner(arguments);
97
#region InstallHelpDocumentsFromWebSource
99
public static void InstallHelpDocumentsFromWebSource(string productCode, string productVersion, string locale, string sourceWeb)
101
InstallHelpDocumentsFromWebSource(productCode, productVersion, locale, sourceWeb, string.Empty);
104
public static void InstallHelpDocumentsFromWebSource(string productCode, string productVersion, string locale, string sourceWeb, string brandingPackage)
106
if (string.IsNullOrEmpty(productCode)) { throw new ArgumentNullException("productCode"); }
107
if (string.IsNullOrEmpty(productVersion)) { throw new ArgumentNullException("productVersion"); }
108
if (string.IsNullOrEmpty(locale)) { throw new ArgumentNullException("locale"); }
109
if (string.IsNullOrEmpty(sourceWeb)) { throw new ArgumentNullException("sourceWeb"); }
110
if (!Regex.IsMatch(productVersion, @"^\d{3}$", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase)) { throw new ArgumentOutOfRangeException("productVersion"); }
111
if (!Regex.IsMatch(locale, @"^\w{2}-\w{2}", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase)) { throw new ArgumentOutOfRangeException("locale"); }
113
string initLS = (!Help3Environment.IsLocalStoreInitialized) ? string.Format("/content \"{0}\"", Help3Environment.BuildLocalStoreFolder):"";
114
string brandingSwitch = (!string.IsNullOrEmpty(brandingPackage)) ? string.Format("/brandingPackage \"{0}\"", brandingPackage):"";
115
string arguments = string.Format("/product {0} /version {1} /locale {2} /sourceWeb \"{3}\" {4} {5}", productCode, productVersion, locale, sourceWeb, initLS, brandingSwitch);
117
LoggingService.Debug(string.Format("Help 3.0: Installing help documents from web with \"{0}\"", arguments));
118
HelpLibManagerProcessRunner(arguments);
123
#region UninstallHelpDocuments
125
public static void UninstallHelpDocuments(string productCode, string productVersion, string locale, string vendor, string productName, string mediaBookList)
127
if (!Help3Environment.IsLocalStoreInitialized) { return; }
128
if (string.IsNullOrEmpty(productCode)) { throw new ArgumentNullException("productCode"); }
129
if (string.IsNullOrEmpty(productVersion)) { throw new ArgumentNullException("productVersion"); }
130
if (string.IsNullOrEmpty(locale)) { throw new ArgumentNullException("locale"); }
131
if (string.IsNullOrEmpty(vendor)) { throw new ArgumentNullException("vendor"); }
132
if (string.IsNullOrEmpty(productName)) { throw new ArgumentNullException("productName"); }
133
if (string.IsNullOrEmpty(mediaBookList)) { throw new ArgumentNullException("mediaBookList"); }
134
if (!Regex.IsMatch(productVersion, @"^\d{3}$", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase)) { throw new ArgumentOutOfRangeException("productVersion"); }
135
if (!Regex.IsMatch(locale, @"^\w{2}-\w{2}", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase)) { throw new ArgumentOutOfRangeException("locale"); }
137
string arguments = string.Format("/product {0} /version {1} /locale {2} /vendor \"{3}\" /productName \"{4}\" /mediaBookList {5} /uninstall", productCode, productVersion, locale, vendor, productName, mediaBookList);
139
LoggingService.Debug(string.Format("Help 3.0: Uninstalling help documents with \"{0}\"", arguments));
140
HelpLibManagerProcessRunner(arguments);
145
static int HelpLibManagerProcessRunner(string arguments)
147
return HelpLibManagerProcessRunner(arguments, false);
150
static int HelpLibManagerProcessRunner(string arguments, bool silent)
152
if (string.IsNullOrEmpty(Manager) || string.IsNullOrEmpty(Help3Environment.AppRoot)) return -10;
155
ProcessStartInfo psi = new ProcessStartInfo();
156
psi.FileName = Manager;
157
psi.WorkingDirectory = Help3Environment.AppRoot;
158
psi.Arguments = string.Format("{0} {1}", arguments, (silent)?"/silent":string.Empty);
159
psi.UseShellExecute = true;
161
psi.WindowStyle = ProcessWindowStyle.Normal;
164
Process p = Process.Start(psi);
168
catch (Exception ex) {
169
LoggingService.Error(string.Format("Help 3.0: {0}", ex.ToString()));
175
public static bool Start()
177
if (IsRunning) return true;
178
if (string.IsNullOrEmpty(Manager)) {
179
throw new ArgumentNullException("Manager");
181
if (Help3Service.ActiveCatalog == null) {
182
throw new ArgumentNullException("Help3Service.ActiveCatalog");
184
ProcessStartInfo psi = new ProcessStartInfo();
185
psi.FileName = Manager;
186
psi.WorkingDirectory = Help3Environment.AppRoot;
187
psi.Arguments = Help3Service.ActiveCatalog.AsCmdLineParam;
188
psi.UseShellExecute = true;
190
psi.WindowStyle = ProcessWindowStyle.Normal;
192
Process p = Process.Start(psi);
193
p.WaitForInputIdle();
194
LoggingService.Info("Help 3.0: Help library manager started");
197
catch (Exception ex) {
198
LoggingService.Error(string.Format("Help 3.0: {0}", ex.ToString()));
203
public static bool Stop()
208
public static bool Stop(bool waitForExit)
211
Process[] managers = Process.GetProcessesByName("HelpLibManager");
213
foreach (Process manager in managers) {
215
if (waitForExit) manager.WaitForExit();
217
LoggingService.Debug(string.Format("Help 3.0: {0} {1} of HelpLibraryManager.exe stopped", managers.Length, (managers.Length == 1)?"process":"processes"));
219
catch (Exception ex) {
220
LoggingService.Error(string.Format("Help 3.0: {0}", ex.ToString()));