~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/AddIns/Misc/HelpViewer/Source/Core/HelpLibraryManager.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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)
 
3
 
 
4
using System;
 
5
using System.Diagnostics;
 
6
using System.Globalization;
 
7
using System.IO;
 
8
using System.Text.RegularExpressions;
 
9
using ICSharpCode.Core;
 
10
 
 
11
namespace MSHelpSystem.Core
 
12
{
 
13
        public sealed class HelpLibraryManager
 
14
        {
 
15
                HelpLibraryManager()
 
16
                {
 
17
                }
 
18
 
 
19
                public static bool IsRunning
 
20
                {
 
21
                        get
 
22
                        {
 
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;                             
 
26
                        }
 
27
                }
 
28
 
 
29
                public static string Manager
 
30
                {
 
31
                        get
 
32
                        {
 
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;
 
37
                        }
 
38
                }
 
39
 
 
40
                #region InitializeLocaleStore
 
41
 
 
42
                public static void InitializeLocaleStore(string productCode, string productVersion)
 
43
                {
 
44
                        InitializeLocaleStore(productCode, productVersion, CultureInfo.CurrentCulture.Name.ToUpper(), string.Empty);
 
45
                }
 
46
 
 
47
                public static void InitializeLocaleStore(string productCode, string productVersion, string locale)
 
48
                {
 
49
                        InitializeLocaleStore(productCode, productVersion, locale, string.Empty);
 
50
                }
 
51
 
 
52
                public static void InitializeLocaleStore(string productCode, string productVersion, string locale, string brandingPackage)
 
53
                {
 
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"); }
 
60
 
 
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);
 
63
 
 
64
                        LoggingService.Debug(string.Format("Help 3.0: Initializing local store with \"{0}\"", arguments));
 
65
                        HelpLibManagerProcessRunner(arguments);
 
66
                }
 
67
 
 
68
                #endregion
 
69
 
 
70
                #region InstallHelpDocumentsFromLocalSource
 
71
 
 
72
                public static void InstallHelpDocumentsFromLocalSource(string productCode, string productVersion, string locale, string sourceMedia)
 
73
                {
 
74
                        InstallHelpDocumentsFromLocalSource(productCode, productVersion, locale, sourceMedia, string.Empty);
 
75
                }
 
76
 
 
77
                public static void InstallHelpDocumentsFromLocalSource(string productCode, string productVersion, string locale, string sourceMedia, string brandingPackage)
 
78
                {
 
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"); }              
 
86
 
 
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);
 
90
 
 
91
                        LoggingService.Debug(string.Format("Help 3.0: Installing local help documents with \"{0}\"", arguments));
 
92
                        HelpLibManagerProcessRunner(arguments);
 
93
                }
 
94
 
 
95
                #endregion
 
96
 
 
97
                #region InstallHelpDocumentsFromWebSource
 
98
 
 
99
                public static void InstallHelpDocumentsFromWebSource(string productCode, string productVersion, string locale, string sourceWeb)
 
100
                {
 
101
                        InstallHelpDocumentsFromWebSource(productCode, productVersion, locale, sourceWeb, string.Empty);
 
102
                }
 
103
 
 
104
                public static void InstallHelpDocumentsFromWebSource(string productCode, string productVersion, string locale, string sourceWeb, string brandingPackage)
 
105
                {
 
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"); }              
 
112
                
 
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);
 
116
 
 
117
                        LoggingService.Debug(string.Format("Help 3.0: Installing help documents from web with \"{0}\"", arguments));
 
118
                        HelpLibManagerProcessRunner(arguments);
 
119
                }
 
120
 
 
121
                #endregion
 
122
 
 
123
                #region UninstallHelpDocuments
 
124
 
 
125
                public static void UninstallHelpDocuments(string productCode, string productVersion, string locale, string vendor, string productName, string mediaBookList)
 
126
                {
 
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"); }                                      
 
136
 
 
137
                        string arguments = string.Format("/product {0} /version {1} /locale {2} /vendor \"{3}\" /productName \"{4}\" /mediaBookList {5} /uninstall", productCode, productVersion, locale, vendor, productName, mediaBookList);
 
138
 
 
139
                        LoggingService.Debug(string.Format("Help 3.0: Uninstalling help documents with \"{0}\"", arguments));
 
140
                        HelpLibManagerProcessRunner(arguments);
 
141
                }
 
142
                                                         
 
143
                #endregion
 
144
 
 
145
                static int HelpLibManagerProcessRunner(string arguments)
 
146
                {
 
147
                        return HelpLibManagerProcessRunner(arguments, false);
 
148
                }
 
149
 
 
150
                static int HelpLibManagerProcessRunner(string arguments, bool silent)
 
151
                {
 
152
                        if (string.IsNullOrEmpty(Manager) || string.IsNullOrEmpty(Help3Environment.AppRoot)) return -10;
 
153
                        Stop();
 
154
                        
 
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;
 
160
                        psi.Verb = "runas";
 
161
                        psi.WindowStyle = ProcessWindowStyle.Normal;
 
162
 
 
163
                        try {
 
164
                                Process p = Process.Start(psi);
 
165
                                p.WaitForExit();
 
166
                                return p.ExitCode;
 
167
                        }
 
168
                        catch (Exception ex) {
 
169
                                LoggingService.Error(string.Format("Help 3.0: {0}", ex.ToString()));
 
170
                        }
 
171
                        return -1;
 
172
                }
 
173
 
 
174
 
 
175
                public static bool Start()
 
176
                {
 
177
                        if (IsRunning) return true;
 
178
                        if (string.IsNullOrEmpty(Manager)) {
 
179
                                throw new ArgumentNullException("Manager");
 
180
                        }
 
181
                        if (Help3Service.ActiveCatalog == null) {
 
182
                                throw new ArgumentNullException("Help3Service.ActiveCatalog");
 
183
                        }
 
184
                        ProcessStartInfo psi = new ProcessStartInfo();
 
185
                        psi.FileName = Manager;
 
186
                        psi.WorkingDirectory = Help3Environment.AppRoot;
 
187
                        psi.Arguments = Help3Service.ActiveCatalog.AsCmdLineParam;
 
188
                        psi.UseShellExecute = true;
 
189
                        psi.Verb = "runas";
 
190
                        psi.WindowStyle = ProcessWindowStyle.Normal;
 
191
                        try {
 
192
                                Process p = Process.Start(psi);
 
193
                                p.WaitForInputIdle();
 
194
                                LoggingService.Info("Help 3.0: Help library manager started");
 
195
                                return IsRunning;
 
196
                        }
 
197
                        catch (Exception ex) {
 
198
                                LoggingService.Error(string.Format("Help 3.0: {0}", ex.ToString()));
 
199
                        }
 
200
                        return false;
 
201
                }
 
202
 
 
203
                public static bool Stop()
 
204
                {
 
205
                        return Stop(true);
 
206
                }
 
207
 
 
208
                public static bool Stop(bool waitForExit)
 
209
                {
 
210
                        try {
 
211
                                Process[] managers = Process.GetProcessesByName("HelpLibManager");
 
212
 
 
213
                                foreach (Process manager in managers) {
 
214
                                        manager.Kill();
 
215
                                        if (waitForExit) manager.WaitForExit();
 
216
                                }
 
217
                                LoggingService.Debug(string.Format("Help 3.0: {0} {1} of HelpLibraryManager.exe stopped", managers.Length, (managers.Length == 1)?"process":"processes"));
 
218
                        }
 
219
                        catch (Exception ex) {
 
220
                                LoggingService.Error(string.Format("Help 3.0: {0}", ex.ToString()));
 
221
                        }                       
 
222
                        return true;
 
223
                }
 
224
        }
 
225
}