Example #1
0
        /// <summary>
        /// Execute the selected action
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The event arguments</param>
        private void btnOK_Click(object sender, EventArgs e)
        {
            ThreadAction action;

            txtInfo.Text = null;

            try
            {
                HelpLibraryManager hlm = new HelpLibraryManager(viewerVersion);

                // Can't do anything if the Help Library Manager is already running
                if (Process.GetProcessesByName(Path.GetFileNameWithoutExtension(hlm.HelpLibraryManagerPath)).Length > 0)
                {
                    throw new HelpLibraryManagerException(viewerVersion,
                                                          HelpLibraryManagerException.HelpLibraryManagerAlreadyRunning);
                }
            }
            catch (Exception ex)
            {
                txtInfo.AppendText("Problem: " + ex.Message + "\r\n");
                return;
            }

            cboHelpViewerVersion.Enabled = grpOptions.Enabled = btnOK.Enabled = false;

            if (rbOpenCurrent.Checked)
            {
                action = ThreadAction.OpenCurrent;
            }
            else
            if (rbInstall.Checked)
            {
                action = ThreadAction.Install;
            }
            else
            if (rbRemove.Checked)
            {
                action = ThreadAction.Remove;
            }
            else
            {
                action = ThreadAction.OpenContentManager;
            }

            lblAction.Text = "Please wait...";
            pbWait.Visible = lblAction.Visible = true;
            actionThread.RunWorkerAsync(action);
        }
        /// <summary>
        /// This is used to determine the state of the help content and set the form options when a help viewer
        /// version is selected.
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The event arguments</param>
        private void cboHelpViewerVersion_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtInfo.Text = null;

            grpOptions.Enabled = rbInstall.Enabled = true;
            lastVersionSelected = cboHelpViewerVersion.SelectedIndex;

            // If there are substitution tags present, have a go at resolving them
            if(helpFilePath.IndexOf("{@", StringComparison.Ordinal) != -1)
            {
                try
                {
                    var bp = new SandcastleBuilder.Utils.BuildEngine.BuildProcess(project);
                    helpFilePath = bp.SubstitutionTags.TransformText(helpFilePath);
                    setupFile = Path.ChangeExtension(helpFilePath, ".msha");
                }
                catch
                {
                    // Ignore errors
                    txtInfo.AppendText("The help filename appears to contain substitution tags but they could " +
                        "not be resolved to determine the actual file to use for installation.  Building " +
                        "website output and viewing it can be used to work around this issue.\r\n\r\n");
                    rbInstall.Enabled = false;
                }
            }

            if(rbInstall.Enabled && (!File.Exists(helpFilePath) || !File.Exists(setupFile)))
            {
                txtInfo.AppendText("A copy of the help file does not appear to exist yet.  It may need to be built.\r\n\r\n");
                rbInstall.Enabled = false;
            }

            try
            {
                viewerVersion = new Version((string)cboHelpViewerVersion.SelectedItem);

                HelpLibraryManager hlm = new HelpLibraryManager(viewerVersion);

                // Can't do anything if the Help Library Manager is not installed
                if(hlm.HelpLibraryManagerPath == null)
                    throw new HelpLibraryManagerException(viewerVersion,
                        HelpLibraryManagerException.HelpLibraryManagerNotFound);

                // Can't do anything if the Help Library Manager is already running
                if(Process.GetProcessesByName(Path.GetFileNameWithoutExtension(hlm.HelpLibraryManagerPath)).Length > 0)
                    throw new HelpLibraryManagerException(viewerVersion,
                        HelpLibraryManagerException.HelpLibraryManagerAlreadyRunning);

                // Can't do anything if the local store is not initialized
                if(!hlm.LocalStoreInitialized)
                    throw new HelpLibraryManagerException(viewerVersion,
                        HelpLibraryManagerException.LocalStoreNotInitialized);

                if(hlm.HelpContentFileInstalled(helpFilePath))
                    rbOpenCurrent.Enabled = rbRemove.Enabled = true;
                else
                {
                    txtInfo.AppendText("The help file does not appear to be installed yet.\r\n");
                    rbOpenCurrent.Enabled = rbRemove.Enabled = false;
                }
            }
            catch(Exception ex)
            {
                txtInfo.AppendText("Problem: " + ex.Message + "\r\n");
                rbOpenCurrent.Enabled = rbRemove.Enabled = false;
            }

            if(rbOpenCurrent.Enabled)
                rbOpenCurrent.Checked = true;
            else
                if(rbInstall.Enabled)
                    rbInstall.Checked = true;
                else
                    rbLaunchContentManager.Checked = true;

            if(!rbOpenCurrent.Enabled && !rbInstall.Enabled && !rbRemove.Enabled)
                txtInfo.AppendText("\r\nNo action can be taken with the help content.");

            // Determine the catalog name here as it's used in a lot of places and varies by version if not
            // defined in the project.
            catalogName = !String.IsNullOrWhiteSpace(project.CatalogName) ? project.CatalogName :
                HelpLibraryManager.DefaultCatalogName(viewerVersion);

            // If it looks like a default value, warn the user if it doesn't match.  It may need to be cleared.
            if(!String.IsNullOrWhiteSpace(project.CatalogName) && project.CatalogName.StartsWith("VisualStudio",
              StringComparison.Ordinal) && project.CatalogName != HelpLibraryManager.DefaultCatalogName(viewerVersion))
                txtInfo.AppendText("\r\n\r\nWARNING:  The project's catalog name property is set to '" +
                    project.CatalogName + "' which does not match the default catalog name for the selected " +
                    "version of the help viewer.  If necessary, clear the catalog name property value.");
        }
        //=====================================================================

        /// <summary>
        /// This performs the requested task
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The event arguments</param>
        private void actionThread_DoWork(object sender, DoWorkEventArgs e)
        {
            ThreadAction action = (ThreadAction)e.Argument;
            string arguments, contentSetupFile;
            int errorCode;

            runningThread = Thread.CurrentThread;

            try
            {
                HelpLibraryManager hlm = new HelpLibraryManager(viewerVersion);

                // Remove old content.  We'll remove it if installing to be sure that the latest copy is
                // installed.
                if(action == ThreadAction.Install || action == ThreadAction.Remove)
                {
                    if(action == ThreadAction.Install)
                        actionThread.ReportProgress(0, (int)ThreadState.RemovingOldContent);
                    else
                        actionThread.ReportProgress(0, (int)ThreadState.RemovingContent);

                    if(viewerVersion.Major == 1)
                        arguments = String.Format(CultureInfo.InvariantCulture,
                            "/product \"{0}\" /version \"{1}\" /locale {2} /uninstall /silent /vendor " +
                            "\"{3}\" /productName \"{4}\" /mediaBookList \"{5}\"",
                            project.CatalogProductId, project.CatalogVersion, project.Language.Name,
                            !String.IsNullOrEmpty(project.VendorName) ? project.VendorName : "Vendor Name",
                            !String.IsNullOrEmpty(project.ProductTitle) ? project.ProductTitle : project.HelpTitle,
                            project.HelpTitle);
                    else
                        arguments = String.Format(CultureInfo.InvariantCulture,
                            "/catalogName \"{0}\" /locale {1} /wait 0 /operation uninstall /vendor \"{2}\" " +
                            "/productName \"{3}\" /bookList \"{4}\" ", catalogName, project.Language.Name,
                            !String.IsNullOrEmpty(project.VendorName) ? project.VendorName : "Vendor Name",
                            !String.IsNullOrEmpty(project.ProductTitle) ? project.ProductTitle : project.HelpTitle,
                            project.HelpTitle);

                    // If there are substitution tags present, have a go at resolving them
                    if(arguments.IndexOf("{@", StringComparison.Ordinal) != -1)
                    {
                        try
                        {
                            var bp = new BuildProcess(project);
                            arguments = bp.SubstitutionTags.TransformText(arguments);
                        }
                        catch(Exception ex)
                        {
                            throw new InvalidOperationException("Unable to transform substitution tags: " +
                                ex.Message, ex);
                        }
                    }

                    // This doesn't have to run as an administrator
                    errorCode = hlm.RunAsNormalUser(arguments, ProcessWindowStyle.Minimized);

                    // Ignore it if not found and we are installing
                    if(errorCode != HelpLibraryManagerException.Success &&
                      (errorCode != HelpLibraryManagerException.NoBooksToInstall || action == ThreadAction.Remove))
                        throw new HelpLibraryManagerException(viewerVersion, errorCode);
                }

                if(action == ThreadAction.Install)
                {
                    // Install the new content
                    actionThread.ReportProgress(0, (int)ThreadState.InstallingContent);

                    // Copy the MSHA file to the required name
                    contentSetupFile = Path.Combine(Path.GetDirectoryName(setupFile), "HelpContentSetup.msha");
                    File.Copy(setupFile, contentSetupFile, true);

                    if(viewerVersion.Major == 1)
                        arguments = String.Format(CultureInfo.InvariantCulture, "/product \"{0}\" " +
                            "/version \"{1}\" /locale {2} /sourceMedia \"{3}\"", project.CatalogProductId,
                            project.CatalogVersion, project.Language.Name, contentSetupFile);
                    else
                        arguments = String.Format(CultureInfo.InvariantCulture, "/catalogName \"{0}\" " +
                            "/locale {1} /wait 0 /operation install /sourceUri \"{2}\"", catalogName,
                            project.Language.Name, contentSetupFile);

                    // Always interactive and must run as administrator.  We can't run silently as we don't have
                    // a signed cabinet file.
                    errorCode = hlm.RunAsAdministrator(arguments, ProcessWindowStyle.Normal);

                    if(errorCode != HelpLibraryManagerException.Success)
                        throw new HelpLibraryManagerException(viewerVersion, errorCode);

                    // Open it if installed successfully
                    action = ThreadAction.OpenCurrent;
                }

                if(action == ThreadAction.OpenCurrent)
                {
                    arguments = null;

                    if(msHelpViewer == null)
                    {
                        msHelpViewer = hlm.HelpViewerPath;

                        if(msHelpViewer == null)
                            msHelpViewer = "ms-xhelp:///?method=page&id=-1";
                        else
                            if(viewerVersion.Major == 2)
                                arguments = "/catalogname \"" + catalogName + "\"";
                    }

                    actionThread.ReportProgress(0, (int)ThreadState.OpeningContent);
                    System.Diagnostics.Process.Start(msHelpViewer, arguments);
                }

                if(action == ThreadAction.OpenContentManager)
                {
                    actionThread.ReportProgress(0, (int)ThreadState.OpeningContentManager);

                    // Can't do anything if the Help Library Manager is not installed
                    if(hlm.HelpLibraryManagerPath == null)
                        throw new HelpLibraryManagerException(viewerVersion,
                            HelpLibraryManagerException.HelpLibraryManagerNotFound);

                    if(viewerVersion.Major == 1)
                        hlm.LaunchInteractive(String.Format(CultureInfo.InvariantCulture,
                            "/product \"{0}\" /version \"{1}\" /locale {2}", project.CatalogProductId,
                            project.CatalogVersion, project.Language.Name));
                    else
                        hlm.LaunchInteractive(String.Format(CultureInfo.InvariantCulture,
                            "/catalogName \"{0}\" /locale {1} /manage", catalogName, project.Language.Name));
                }
            }
            catch(ThreadAbortException)
            {
                // Ignore thread abort exceptions
            }
        }
        /// <summary>
        /// Launch the Launch Help Viewer 2.x Content Manager for interactive use based on the current project's
        /// settings.
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The event arguments</param>
        protected override void LaunchContentMgrExecuteHandler(object sender, EventArgs e)
        {
            Version version;

            try
            {
                SandcastleProject project = CurrentSandcastleProject;

                if(project != null)
                {
                    if(project.CatalogName == "VisualStudio11")
                        version = new Version(2, 0);
                    else
                        version = new Version(2, 1);

                    HelpLibraryManager hlm = new HelpLibraryManager(version);

                    hlm.LaunchInteractive(String.Format(CultureInfo.InvariantCulture,
                        "/catalogName \"{0}\" /locale {1} /manage", project.CatalogName, project.Language.Name));
                }
            }
            catch(Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());
                Utility.ShowMessageBox(OLEMSGICON.OLEMSGICON_CRITICAL, "Unable to launch Help Viewer 2.x " +
                    "Content Manager.  Reason:\r\n{0}\r\n\r\nIs the catalog name correct in the project?",
                    ex.Message);
            }
        }
        /// <summary>
        /// Launch the Help Library Manager 1.0 for interactive use based on the current project's settings
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The event arguments</param>
        protected override void LaunchHelpLibMgrExecuteHandler(object sender, EventArgs e)
        {
            try
            {
                SandcastleProject project = CurrentSandcastleProject;

                if(project != null)
                {
                    HelpLibraryManager hlm = new HelpLibraryManager();

                    hlm.LaunchInteractive(String.Format(CultureInfo.InvariantCulture,
                        "/product \"{0}\" /version \"{1}\" /locale {2}", project.CatalogProductId,
                        project.CatalogVersion, project.Language.Name));
                }
            }
            catch(Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());
                Utility.ShowMessageBox(OLEMSGICON.OLEMSGICON_CRITICAL,
                    "Unable to launch Help Library Manager.  Reason:\r\n{0}", ex.Message);
            }
        }
        /// <summary>
        /// This is used to determine the state of the help content and set the form options when a help viewer
        /// version is selected.
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The event arguments</param>
        private void cboHelpViewerVersion_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtInfo.Text = null;

            grpOptions.Enabled  = rbInstall.Enabled = true;
            lastVersionSelected = cboHelpViewerVersion.SelectedIndex;

            // If there are substitution tags present, have a go at resolving them
            if (helpFilePath.IndexOf("{@", StringComparison.Ordinal) != -1)
            {
                try
                {
                    var bp = new SandcastleBuilder.Utils.BuildEngine.BuildProcess(project);
                    helpFilePath = bp.TransformText(helpFilePath);
                    setupFile    = Path.ChangeExtension(helpFilePath, ".msha");
                }
                catch
                {
                    // Ignore errors
                    txtInfo.AppendText("The help filename appears to contain substitution tags but they could " +
                                       "not be resolved to determine the actual file to use for installation.  Building " +
                                       "website output and viewing it can be used to work around this issue.\r\n\r\n");
                    rbInstall.Enabled = false;
                }
            }

            if (rbInstall.Enabled && (!File.Exists(helpFilePath) || !File.Exists(setupFile)))
            {
                txtInfo.AppendText("A copy of the help file does not appear to exist yet.  It may need to be built.\r\n\r\n");
                rbInstall.Enabled = false;
            }

            try
            {
                viewerVersion = new Version((string)cboHelpViewerVersion.SelectedItem);

                HelpLibraryManager hlm = new HelpLibraryManager(viewerVersion);

                // Can't do anything if the Help Library Manager is not installed
                if (hlm.HelpLibraryManagerPath == null)
                {
                    throw new HelpLibraryManagerException(viewerVersion,
                                                          HelpLibraryManagerException.HelpLibraryManagerNotFound);
                }

                // Can't do anything if the Help Library Manager is already running
                if (Process.GetProcessesByName(Path.GetFileNameWithoutExtension(hlm.HelpLibraryManagerPath)).Length > 0)
                {
                    throw new HelpLibraryManagerException(viewerVersion,
                                                          HelpLibraryManagerException.HelpLibraryManagerAlreadyRunning);
                }

                // Can't do anything if the local store is not initialized
                if (!hlm.LocalStoreInitialized)
                {
                    throw new HelpLibraryManagerException(viewerVersion,
                                                          HelpLibraryManagerException.LocalStoreNotInitialized);
                }

                if (hlm.HelpContentFileInstalled(helpFilePath))
                {
                    rbOpenCurrent.Enabled = rbRemove.Enabled = true;
                }
                else
                {
                    txtInfo.AppendText("The help file does not appear to be installed yet.\r\n");
                    rbOpenCurrent.Enabled = rbRemove.Enabled = false;
                }

                if (rbOpenCurrent.Enabled)
                {
                    rbOpenCurrent.Checked = true;
                }
                else
                if (rbInstall.Enabled)
                {
                    rbInstall.Checked = true;
                }

                btnOK.Enabled = (rbOpenCurrent.Enabled || rbInstall.Enabled || rbRemove.Enabled);
            }
            catch (Exception ex)
            {
                txtInfo.AppendText("Problem: " + ex.Message + "\r\n");
                btnOK.Enabled = false;
            }

            if (!btnOK.Enabled)
            {
                txtInfo.AppendText("\r\nNo action can be taken.");
            }
        }
        //=====================================================================

        /// <summary>
        /// This performs the requested task
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The event arguments</param>
        private void actionThread_DoWork(object sender, DoWorkEventArgs e)
        {
            ThreadAction action = (ThreadAction)e.Argument;
            string       arguments, contentSetupFile;
            int          errorCode;

            runningThread = Thread.CurrentThread;

            try
            {
                HelpLibraryManager hlm = new HelpLibraryManager(viewerVersion);

                // Remove old content.  We'll remove it if installing to be sure that
                // the latest copy is installed.
                if (action == ThreadAction.Install || action == ThreadAction.Remove)
                {
                    if (action == ThreadAction.Install)
                    {
                        actionThread.ReportProgress(0, (int)ThreadState.RemovingOldContent);
                    }
                    else
                    {
                        actionThread.ReportProgress(0, (int)ThreadState.RemovingContent);
                    }

                    if (viewerVersion.Major == 1)
                    {
                        arguments = String.Format(CultureInfo.InvariantCulture,
                                                  "/product \"{0}\" /version \"{1}\" /locale {2} /uninstall /silent /vendor " +
                                                  "\"{3}\" /productName \"{4}\" /mediaBookList \"{5}\"",
                                                  project.CatalogProductId, project.CatalogVersion, project.Language.Name,
                                                  !String.IsNullOrEmpty(project.VendorName) ? project.VendorName : "Vendor Name",
                                                  !String.IsNullOrEmpty(project.ProductTitle) ? project.ProductTitle : project.HelpTitle,
                                                  project.HelpTitle);
                    }
                    else
                    {
                        arguments = String.Format(CultureInfo.InvariantCulture,
                                                  "/catalogName \"{0}\" /locale {1} /wait 0 /operation uninstall /vendor \"{2}\" " +
                                                  "/productName \"{3}\" /bookList \"{4}\" ",
                                                  project.CatalogName, project.Language.Name,
                                                  !String.IsNullOrEmpty(project.VendorName) ? project.VendorName : "Vendor Name",
                                                  !String.IsNullOrEmpty(project.ProductTitle) ? project.ProductTitle : project.HelpTitle,
                                                  project.HelpTitle);
                    }

                    // This doesn't have to run as an administrator
                    errorCode = hlm.RunAsNormalUser(arguments, ProcessWindowStyle.Minimized);

                    // Ignore it if not found and we are installing
                    if (errorCode != HelpLibraryManagerException.Success &&
                        (errorCode != HelpLibraryManagerException.NoBooksToInstall || action == ThreadAction.Remove))
                    {
                        throw new HelpLibraryManagerException(viewerVersion, errorCode);
                    }
                }

                if (action == ThreadAction.Install)
                {
                    // Install the new content
                    actionThread.ReportProgress(0, (int)ThreadState.InstallingContent);

                    // Copy the MSHA file to the required name
                    contentSetupFile = Path.Combine(Path.GetDirectoryName(setupFile), "HelpContentSetup.msha");
                    File.Copy(setupFile, contentSetupFile, true);

                    if (viewerVersion.Major == 1)
                    {
                        arguments = String.Format(CultureInfo.InvariantCulture, "/product \"{0}\" " +
                                                  "/version \"{1}\" /locale {2} /sourceMedia \"{3}\"", project.CatalogProductId,
                                                  project.CatalogVersion, project.Language.Name, contentSetupFile);
                    }
                    else
                    {
                        arguments = String.Format(CultureInfo.InvariantCulture, "/catalogName \"{0}\" " +
                                                  "/locale {1} /wait 0 /operation install /sourceUri \"{2}\"", project.CatalogName,
                                                  project.Language.Name, contentSetupFile);
                    }

                    // Always interactive and must run as administrator.  We can't run silently as we don't have
                    // a signed cabinet file.
                    errorCode = hlm.RunAsAdministrator(arguments, ProcessWindowStyle.Normal);

                    if (errorCode != HelpLibraryManagerException.Success)
                    {
                        throw new HelpLibraryManagerException(viewerVersion, errorCode);
                    }

                    // Open it if installed successfully
                    action = ThreadAction.OpenCurrent;
                }

                if (action == ThreadAction.OpenCurrent)
                {
                    arguments = null;

                    if (msHelpViewer == null)
                    {
                        msHelpViewer = hlm.HelpViewerPath;

                        if (msHelpViewer == null)
                        {
                            msHelpViewer = "ms-xhelp:///?method=page&id=-1";
                        }
                        else
                        if (viewerVersion.Major == 2)
                        {
                            arguments = "/catalogname \"" + project.CatalogName + "\"";
                        }
                    }

                    actionThread.ReportProgress(0, (int)ThreadState.OpeningContent);
                    System.Diagnostics.Process.Start(msHelpViewer, arguments);
                }
            }
            catch (ThreadAbortException)
            {
                // Ignore thread abort exceptions
            }
        }
        /// <summary>
        /// This is used to determine the state of the help content and set the form options when a help viewer
        /// version is selected.
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The event arguments</param>
        private void cboHelpViewerVersion_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtInfo.Text = null;

            grpOptions.Enabled = rbInstall.Enabled = true;
            lastVersionSelected = cboHelpViewerVersion.SelectedIndex;

            // If there are substitution tags present, have a go at resolving them
            if(helpFilePath.IndexOf("{@", StringComparison.Ordinal) != -1)
            {
                try
                {
                    var bp = new SandcastleBuilder.Utils.BuildEngine.BuildProcess(project);
                    helpFilePath = bp.TransformText(helpFilePath);
                    setupFile = Path.ChangeExtension(helpFilePath, ".msha");
                }
                catch
                {
                    // Ignore errors
                    txtInfo.AppendText("The help filename appears to contain substitution tags but they could " +
                        "not be resolved to determine the actual file to use for installation.  Building " +
                        "website output and viewing it can be used to work around this issue.\r\n\r\n");
                    rbInstall.Enabled = false;
                }
            }

            if(rbInstall.Enabled && (!File.Exists(helpFilePath) || !File.Exists(setupFile)))
            {
                txtInfo.AppendText("A copy of the help file does not appear to exist yet.  It may need to be built.\r\n\r\n");
                rbInstall.Enabled = false;
            }

            try
            {
                viewerVersion = new Version((string)cboHelpViewerVersion.SelectedItem);

                HelpLibraryManager hlm = new HelpLibraryManager(viewerVersion);

                // Can't do anything if the Help Library Manager is not installed
                if(hlm.HelpLibraryManagerPath == null)
                    throw new HelpLibraryManagerException(viewerVersion,
                        HelpLibraryManagerException.HelpLibraryManagerNotFound);

                // Can't do anything if the Help Library Manager is already running
                if(Process.GetProcessesByName(Path.GetFileNameWithoutExtension(hlm.HelpLibraryManagerPath)).Length > 0)
                    throw new HelpLibraryManagerException(viewerVersion,
                        HelpLibraryManagerException.HelpLibraryManagerAlreadyRunning);

                // Can't do anything if the local store is not initialized
                if(!hlm.LocalStoreInitialized)
                    throw new HelpLibraryManagerException(viewerVersion,
                        HelpLibraryManagerException.LocalStoreNotInitialized);

                if(hlm.HelpContentFileInstalled(helpFilePath))
                    rbOpenCurrent.Enabled = rbRemove.Enabled = true;
                else
                {
                    txtInfo.AppendText("The help file does not appear to be installed yet.\r\n");
                    rbOpenCurrent.Enabled = rbRemove.Enabled = false;
                }

                if(rbOpenCurrent.Enabled)
                    rbOpenCurrent.Checked = true;
                else
                    if(rbInstall.Enabled)
                        rbInstall.Checked = true;

                btnOK.Enabled = (rbOpenCurrent.Enabled || rbInstall.Enabled || rbRemove.Enabled);
            }
            catch(Exception ex)
            {
                txtInfo.AppendText("Problem: " + ex.Message + "\r\n");
                btnOK.Enabled = false;
            }

            if(!btnOK.Enabled)
                txtInfo.AppendText("\r\nNo action can be taken.");
        }
Example #9
0
        /// <summary>
        /// Launch Help Library Manager 1.0 or Help Viewer 2.0 content manager for interactive use based on the
        /// current project's settings
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The event arguments</param>
        private void miLaunchHlm_Click(object sender, EventArgs e)
        {
            Version version;

            try
            {
                if(sender == miLaunchHlm)
                    version = new Version(1, 0);
                else
                    if(project.CatalogName == "VisualStudio11")
                        version = new Version(2, 0);
                    else
                        version = new Version(2, 1);

                HelpLibraryManager hlm = new HelpLibraryManager(version);

                if(sender == miLaunchHlm)
                    hlm.LaunchInteractive(String.Format(CultureInfo.InvariantCulture,
                        "/product \"{0}\" /version \"{1}\" /locale {2}", project.CatalogProductId,
                        project.CatalogVersion, project.Language.Name));
                else
                    hlm.LaunchInteractive(String.Format(CultureInfo.InvariantCulture,
                        "/catalogName \"{0}\" /locale {1} /manage", project.CatalogName, project.Language.Name));
            }
            catch(Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());

                MessageBox.Show(String.Format(CultureInfo.CurrentCulture, "Unable to launch help library " +
                    "content manager.  Reason:\r\n{0}\r\n\r\nIs the catalog name correct in the project?",
                    ex.Message), Constants.AppName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
Example #10
0
        /// <summary>
        /// This is used to determine the state of the help content and set the form options when a help viewer
        /// version is selected.
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The event arguments</param>
        private void cboHelpViewerVersion_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtInfo.Text = null;

            grpOptions.Enabled  = rbInstall.Enabled = true;
            lastVersionSelected = cboHelpViewerVersion.SelectedIndex;

            // If there are substitution tags present, have a go at resolving them
            if (helpFilePath.IndexOf("{@", StringComparison.Ordinal) != -1)
            {
                try
                {
                    var bp = new BuildProcess(project);
                    helpFilePath = bp.SubstitutionTags.TransformText(helpFilePath);
                    setupFile    = Path.ChangeExtension(helpFilePath, ".msha");
                }
                catch
                {
                    // Ignore errors
                    txtInfo.AppendText("The help filename appears to contain substitution tags but they could " +
                                       "not be resolved to determine the actual file to use for installation.  Building " +
                                       "website output and viewing it can be used to work around this issue.\r\n\r\n");
                    rbInstall.Enabled = false;
                }
            }

            if (rbInstall.Enabled && (!File.Exists(helpFilePath) || !File.Exists(setupFile)))
            {
                txtInfo.AppendText("A copy of the help file does not appear to exist yet.  It may need to be built.\r\n\r\n");
                rbInstall.Enabled = false;
            }

            try
            {
                viewerVersion = new Version((string)cboHelpViewerVersion.SelectedItem);

                HelpLibraryManager hlm = new HelpLibraryManager(viewerVersion);

                // Can't do anything if the Help Library Manager is not installed
                if (hlm.HelpLibraryManagerPath == null)
                {
                    throw new HelpLibraryManagerException(viewerVersion,
                                                          HelpLibraryManagerException.HelpLibraryManagerNotFound);
                }

                // Can't do anything if the Help Library Manager is already running
                if (Process.GetProcessesByName(Path.GetFileNameWithoutExtension(hlm.HelpLibraryManagerPath)).Length > 0)
                {
                    throw new HelpLibraryManagerException(viewerVersion,
                                                          HelpLibraryManagerException.HelpLibraryManagerAlreadyRunning);
                }

                // Can't do anything if the local store is not initialized
                if (!hlm.LocalStoreInitialized)
                {
                    throw new HelpLibraryManagerException(viewerVersion,
                                                          HelpLibraryManagerException.LocalStoreNotInitialized);
                }

                if (hlm.HelpContentFileInstalled(helpFilePath))
                {
                    rbOpenCurrent.Enabled = rbRemove.Enabled = true;
                }
                else
                {
                    txtInfo.AppendText("The help file does not appear to be installed yet.\r\n");
                    rbOpenCurrent.Enabled = rbRemove.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                txtInfo.AppendText("Problem: " + ex.Message + "\r\n");
                rbOpenCurrent.Enabled = rbRemove.Enabled = false;
            }

            if (rbOpenCurrent.Enabled)
            {
                rbOpenCurrent.Checked = true;
            }
            else
            if (rbInstall.Enabled)
            {
                rbInstall.Checked = true;
            }
            else
            {
                rbLaunchContentManager.Checked = true;
            }

            if (!rbOpenCurrent.Enabled && !rbInstall.Enabled && !rbRemove.Enabled)
            {
                txtInfo.AppendText("\r\nNo action can be taken with the help content.");
            }

            // Determine the catalog name here as it's used in a lot of places and varies by version if not
            // defined in the project.
            catalogName = !String.IsNullOrWhiteSpace(project.CatalogName) ? project.CatalogName :
                          HelpLibraryManager.DefaultCatalogName(viewerVersion);

            // If it looks like a default value, warn the user if it doesn't match.  It may need to be cleared.
            if (!String.IsNullOrWhiteSpace(project.CatalogName) && project.CatalogName.StartsWith("VisualStudio",
                                                                                                  StringComparison.Ordinal) && project.CatalogName != HelpLibraryManager.DefaultCatalogName(viewerVersion))
            {
                txtInfo.AppendText("\r\n\r\nWARNING:  The project's catalog name property is set to '" +
                                   project.CatalogName + "' which does not match the default catalog name for the selected " +
                                   "version of the help viewer.  If necessary, clear the catalog name property value.");
            }
        }
        //=====================================================================
        /// <summary>
        /// This is the main program entry point
        /// </summary>
        /// <param name="args">The command line arguments</param>
        public static int Main(string[] args)
        {
            List<CommandLineArgument> allArgs = new List<CommandLineArgument>();
            List<string> execArgs = new List<string>();
            string product = null, version = null, locale = null, commandLine;
            int result = HelpLibraryManagerException.Success;
            bool isInstall = false, isSilent = false, showHelp = false;

            Assembly asm = Assembly.GetExecutingAssembly();

            FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(asm.Location);
            Console.WriteLine("{0}, version {1}\r\n{2}\r\nE-Mail: [email protected]\r\n",
                fvi.ProductName, fvi.ProductVersion, fvi.LegalCopyright);

            try
            {
                HelpLibraryManager hlm = new HelpLibraryManager();

                // Can't do anything if the Help Library Manager is not installed
                if(hlm.HelpLibraryManagerPath == null)
                    throw new HelpLibraryManagerException(HelpLibraryManagerException.HelpLibraryManagerNotFound);

                // Can't do anything if the Help Library Manager is already running
                if(Process.GetProcessesByName(Path.GetFileNameWithoutExtension(hlm.HelpLibraryManagerPath)).Length > 0)
                    throw new HelpLibraryManagerException(HelpLibraryManagerException.HelpLibraryManagerAlreadyRunning);

                // Can't do anything if the local store is not initialized
                if(!hlm.LocalStoreInitialized)
                    throw new HelpLibraryManagerException(HelpLibraryManagerException.LocalStoreNotInitialized);

                // Parse the command line arguments
                foreach(string arg in args)
                    allArgs.Add(new CommandLineArgument(arg));

                for(int idx = 0; idx < allArgs.Count; idx++)
                {
                    if(allArgs[idx].IsSwitch)
                    {
                        if(allArgs[idx].MatchesSwitch("product") && idx < allArgs.Count - 1)
                            product = allArgs[idx + 1].Value;

                        if(allArgs[idx].MatchesSwitch("version") && idx < allArgs.Count - 1)
                            version = allArgs[idx + 1].Value;

                        if(allArgs[idx].MatchesSwitch("locale") && idx < allArgs.Count - 1)
                            locale = allArgs[idx + 1].Value;

                        if(allArgs[idx].MatchesSwitch("install") || allArgs[idx].MatchesSwitch("sourceMedia"))
                            isInstall = true;

                        if(allArgs[idx].MatchesSwitch("silent"))
                            isSilent = true;

                        if(allArgs[idx].MatchesSwitch("help") || allArgs[idx].Value[1] == '?')
                            showHelp = true;
                    }

                    execArgs.Add(allArgs[idx].ToCommandLineOption());
                }

                if(allArgs.Count == 0 || showHelp)
                {
                    ShowHelp();
                    return HelpLibraryManagerException.MissingCommandLineArgument;
                }

                // These two are required
                if(String.IsNullOrEmpty(product))
                    throw new HelpLibraryManagerException(HelpLibraryManagerException.MissingCommandLineArgument,
                        "/product");

                if(String.IsNullOrEmpty(version))
                    throw new HelpLibraryManagerException(HelpLibraryManagerException.MissingCommandLineArgument,
                        "/version");

                // If not specified, try to figure out the default locale
                if(String.IsNullOrEmpty(locale))
                {
                    locale = hlm.FindLocaleFor(product, version);

                    if(locale == null)
                        throw new HelpLibraryManagerException(HelpLibraryManagerException.CatalogNotInstalled,
                            String.Format(CultureInfo.InvariantCulture, "Product: {0}  Version: {1}", product, version));

                    Console.WriteLine("No locale specified, the default locale '{0}' will be used", locale);

                    execArgs.Add("/locale");
                    execArgs.Add(locale);
                }

                // Execute the request
                Console.WriteLine("Running Help Library Manager to perform the requested action.  Please wait...");

                commandLine = String.Join(" ", execArgs.ToArray());

                try
                {
                    // If installing, we must always run as administrator.  Everything else can
                    // run as a normal user.
                    if(isInstall)
                    {
                        if(isSilent)
                            result = hlm.RunAsAdministrator(commandLine, ProcessWindowStyle.Minimized);
                        else
                            result = hlm.RunAsAdministrator(commandLine, ProcessWindowStyle.Normal);
                    }
                    else
                        result = hlm.RunAsNormalUser(commandLine, ProcessWindowStyle.Minimized);
                }
                catch(Exception ex)
                {
                    throw new HelpLibraryManagerException(HelpLibraryManagerException.UnknownError, String.Format(
                        CultureInfo.InvariantCulture, "Failed to execute \"{0}\" {1}.\r\nError: {2}",
                        hlm.HelpLibraryManagerPath, commandLine, ex.Message));
                }

                if(result != HelpLibraryManagerException.Success)
                    throw new HelpLibraryManagerException(result);

                Console.WriteLine("The operation completed successfully");
            }
            catch(HelpLibraryManagerException hlmEx)
            {
                result = hlmEx.ErrorCode;
                Console.WriteLine("\r\nERROR: The requested operation could not be performed.\r\nDetails: {0}",
                    hlmEx.Message);
            }
            catch(Exception ex)
            {
                result = HelpLibraryManagerException.UnknownError;
                Console.WriteLine("\r\nERROR: The requested operation could not be performed.\r\nDetails: {0}",
                    ex.ToString());
            }
            #if DEBUG
            if(System.Diagnostics.Debugger.IsAttached)
            {
                Console.WriteLine("Hit ENTER to exit...");
                Console.ReadLine();
            }
            #endif
            return result;
        }
        /// <summary>
        /// This is used to determine the state of the help content and set
        /// the form options.
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The event arguments</param>
        private void LaunchMSHelpViewerDlg_Load(object sender, EventArgs e)
        {
            if(!File.Exists(helpFilePath) || !File.Exists(setupFile))
            {
                txtInfo.AppendText("A copy of the help file does not appear to exist yet.  It may need to be built.\r\n");
                rbInstall.Enabled = false;
            }

            try
            {
                HelpLibraryManager hlm = new HelpLibraryManager();

                // Can't do anything if the Help Library Manager is not installed
                if(hlm.HelpLibraryManagerPath == null)
                    throw new HelpLibraryManagerException(HelpLibraryManagerException.HelpLibraryManagerNotFound);

                // Can't do anything if the Help Library Manager is already running
                if(Process.GetProcessesByName(Path.GetFileNameWithoutExtension(hlm.HelpLibraryManagerPath)).Length > 0)
                    throw new HelpLibraryManagerException(HelpLibraryManagerException.HelpLibraryManagerAlreadyRunning);

                // Can't do anything if the local store is not initialized
                if(!hlm.LocalStoreInitialized)
                    throw new HelpLibraryManagerException(HelpLibraryManagerException.LocalStoreNotInitialized);

                if(!hlm.HelpContentFileInstalled(helpFilePath))
                {
                    txtInfo.AppendText("The help file does not appear to be installed yet.\r\n");
                    rbOpenCurrent.Enabled = rbRemove.Enabled = false;
                }

                grpOptions.Enabled = true;
                btnOK.Enabled = (rbOpenCurrent.Enabled || rbInstall.Enabled || rbRemove.Enabled);
            }
            catch(Exception ex)
            {
                txtInfo.AppendText("Problem: " + ex.Message + "\r\n");
            }

            if(!btnOK.Enabled)
                txtInfo.AppendText("\r\nNo action can be taken.");
        }
        //=====================================================================
        /// <summary>
        /// This performs the requested task
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The event arguments</param>
        private void actionThread_DoWork(object sender, DoWorkEventArgs e)
        {
            ThreadAction action = (ThreadAction)e.Argument;
            string arguments, contentSetupFile;
            int errorCode;

            runningThread = Thread.CurrentThread;

            try
            {
                HelpLibraryManager hlm = new HelpLibraryManager();

                // Remove old content.  We'll remove it if installing to be sure that
                // the latest copy is installed.
                if(action == ThreadAction.Install || action == ThreadAction.Remove)
                {
                    if(action == ThreadAction.Install)
                        actionThread.ReportProgress(0, (int)ThreadState.RemovingOldContent);
                    else
                        actionThread.ReportProgress(0, (int)ThreadState.RemovingContent);

                    arguments = String.Format(CultureInfo.InvariantCulture,
                        "/product \"{0}\" /version \"{1}\" /locale {2} /uninstall /silent /vendor " +
                        "\"{3}\" /mediaBookList \"{4}\" /productName \"{5}\"",
                        project.CatalogProductId, project.CatalogVersion, project.Language.Name,
                        project.VendorName, project.HelpTitle, project.ProductTitle);

                    // This doesn't have to run as an administrator
                    errorCode = hlm.RunAsNormalUser(arguments, ProcessWindowStyle.Minimized);

                    // Ignore it if not found and we are installing
                    if(errorCode != HelpLibraryManagerException.Success &&
                      (errorCode != HelpLibraryManagerException.NoBooksToInstall || action == ThreadAction.Remove))
                        throw new HelpLibraryManagerException(errorCode);
                }

                if(action == ThreadAction.Install)
                {
                    // Install the new content
                    actionThread.ReportProgress(0, (int)ThreadState.InstallingContent);

                    // Copy the MSHA file to the required name
                    contentSetupFile = Path.Combine(Path.GetDirectoryName(setupFile), "HelpContentSetup.msha");
                    File.Copy(setupFile, contentSetupFile, true);

                    arguments = String.Format(CultureInfo.InvariantCulture,
                        "/product \"{0}\" /version \"{1}\" /locale {2} /brandingPackage Dev10.mshc " +
                        "/sourceMedia \"{3}", project.CatalogProductId, project.CatalogVersion, project.Language.Name,
                        contentSetupFile);

                    // Always interactive and must run as administrator.  We can't run
                    // silently as we don't have a signed cabinet file.
                    errorCode = hlm.RunAsAdministrator(arguments, ProcessWindowStyle.Normal);

                    if(errorCode != HelpLibraryManagerException.Success)
                        throw new HelpLibraryManagerException(errorCode);

                    // Open it if installed successfully
                    action = ThreadAction.OpenCurrent;
                }

                if(action == ThreadAction.OpenCurrent)
                {
                    actionThread.ReportProgress(0, (int)ThreadState.OpeningContent);
                    System.Diagnostics.Process.Start(msHelpViewer);
                }
            }
            catch(ThreadAbortException )
            {
                // Ignore thread abort exceptions
            }
        }
        /// <summary>
        /// Execute the selected action
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The event arguments</param>
        private void btnOK_Click(object sender, EventArgs e)
        {
            ThreadAction action;

            txtInfo.Text = null;

            try
            {
                HelpLibraryManager hlm = new HelpLibraryManager(viewerVersion);

                // Can't do anything if the Help Library Manager is already running
                if(Process.GetProcessesByName(Path.GetFileNameWithoutExtension(hlm.HelpLibraryManagerPath)).Length > 0)
                    throw new HelpLibraryManagerException(viewerVersion,
                        HelpLibraryManagerException.HelpLibraryManagerAlreadyRunning);
            }
            catch(Exception ex)
            {
                txtInfo.AppendText("Problem: " + ex.Message + "\r\n");
                return;
            }

            cboHelpViewerVersion.Enabled = grpOptions.Enabled = btnOK.Enabled = false;

            if(rbOpenCurrent.Checked)
                action = ThreadAction.OpenCurrent;
            else
                if(rbInstall.Checked)
                    action = ThreadAction.Install;
                else
                    if(rbRemove.Checked)
                        action = ThreadAction.Remove;
                    else
                        action = ThreadAction.OpenContentManager;

            lblAction.Text = "Please wait...";
            pbWait.Visible = lblAction.Visible = true;
            actionThread.RunWorkerAsync(action);
        }
        //=====================================================================

        /// <summary>
        /// This is the main program entry point
        /// </summary>
        /// <param name="args">The command line arguments</param>
        /// <returns>An exit code that indicates the success or failure of the process</returns>
        public static int Main(string[] args)
        {
            List <CommandLineArgument> allArgs = new List <CommandLineArgument>();
            List <string> execArgs = new List <string>();
            string        product = null, version = null, locale = null, catalogName = null, commandLine;
            int           result = HelpLibraryManagerException.Success;
            bool          isInstall = false, isSilent = false, showHelp = false;
            Version       viewerVersion = new Version(1, 0);

            Assembly asm = Assembly.GetExecutingAssembly();

            FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(asm.Location);

            Console.WriteLine("{0}, version {1}\r\n{2}\r\nE-Mail: [email protected]\r\n",
                              fvi.ProductName, fvi.ProductVersion, fvi.LegalCopyright);

            try
            {
                // Parse the command line arguments
                foreach (string arg in args)
                {
                    allArgs.Add(new CommandLineArgument(arg));
                }

                for (int idx = 0; idx < allArgs.Count; idx++)
                {
                    if (allArgs[idx].IsSwitch)
                    {
                        // This is used internally and isn't passed on
                        if (allArgs[idx].MatchesSwitch("viewerVersion") && idx < allArgs.Count - 1)
                        {
                            idx++;
                            viewerVersion = new Version(allArgs[idx].Value);
                            continue;
                        }

                        if (allArgs[idx].MatchesSwitch("catalogName") && idx < allArgs.Count - 1)
                        {
                            catalogName = allArgs[idx + 1].Value;
                        }

                        if (allArgs[idx].MatchesSwitch("product") && idx < allArgs.Count - 1)
                        {
                            product = allArgs[idx + 1].Value;
                        }

                        if (allArgs[idx].MatchesSwitch("version") && idx < allArgs.Count - 1)
                        {
                            version = allArgs[idx + 1].Value;
                        }

                        if (allArgs[idx].MatchesSwitch("locale") && idx < allArgs.Count - 1)
                        {
                            locale = allArgs[idx + 1].Value;
                        }

                        if (allArgs[idx].MatchesSwitch("install") || allArgs[idx].MatchesSwitch("sourceMedia") ||
                            allArgs[idx].MatchesSwitch("sourceUri"))
                        {
                            isInstall = true;
                        }

                        if (allArgs[idx].MatchesSwitch("silent"))
                        {
                            isSilent = true;
                        }

                        if (allArgs[idx].MatchesSwitch("help") || allArgs[idx].Value[1] == '?')
                        {
                            showHelp = true;
                        }
                    }

                    execArgs.Add(allArgs[idx].ToCommandLineOption());
                }

                if (allArgs.Count == 0 || showHelp)
                {
                    ShowHelp();
                    return(HelpLibraryManagerException.MissingCommandLineArgument);
                }

                if (viewerVersion.Major == 1)
                {
                    // These two are required for Help Viewer 1.0
                    if (String.IsNullOrEmpty(product))
                    {
                        throw new HelpLibraryManagerException(viewerVersion,
                                                              HelpLibraryManagerException.MissingCommandLineArgument, "/product");
                    }

                    if (String.IsNullOrEmpty(version))
                    {
                        throw new HelpLibraryManagerException(viewerVersion,
                                                              HelpLibraryManagerException.MissingCommandLineArgument, "/version");
                    }

                    // This is only used by Help Viewer 2.0
                    if (!String.IsNullOrEmpty(catalogName))
                    {
                        throw new HelpLibraryManagerException(viewerVersion,
                                                              HelpLibraryManagerException.InvalidCmdArgs,
                                                              "/catalogName is only valid for Help Viewer 2.0");
                    }
                }
                else
                {
                    if (!String.IsNullOrEmpty(product))
                    {
                        throw new HelpLibraryManagerException(viewerVersion,
                                                              HelpLibraryManagerException.InvalidCmdArgs,
                                                              "/product is only valid for Help Viewer 1.0");
                    }

                    if (!String.IsNullOrEmpty(version))
                    {
                        throw new HelpLibraryManagerException(viewerVersion,
                                                              HelpLibraryManagerException.InvalidCmdArgs,
                                                              "/version is only valid for Help Viewer 1.0");
                    }

                    // If not specified, default the catalog name based on the viewer version
                    if (String.IsNullOrEmpty(catalogName))
                    {
                        catalogName = HelpLibraryManager.DefaultCatalogName(viewerVersion);

                        if (catalogName == null)
                        {
                            throw new HelpLibraryManagerException(viewerVersion,
                                                                  HelpLibraryManagerException.MissingCommandLineArgument, "/catalogName");
                        }

                        Console.WriteLine("Catalog name not specified, the default catalog name '{0}' will " +
                                          "be used.", catalogName);
                    }
                }

                HelpLibraryManager hlm = new HelpLibraryManager(viewerVersion);

                // Can't do anything if the Help Library Manager is not installed
                if (hlm.HelpLibraryManagerPath == null)
                {
                    throw new HelpLibraryManagerException(viewerVersion,
                                                          HelpLibraryManagerException.HelpLibraryManagerNotFound);
                }

                // Can't do anything if the Help Library Manager is already running
                if (Process.GetProcessesByName(Path.GetFileNameWithoutExtension(hlm.HelpLibraryManagerPath)).Length > 0)
                {
                    throw new HelpLibraryManagerException(viewerVersion,
                                                          HelpLibraryManagerException.HelpLibraryManagerAlreadyRunning);
                }

                // Can't do anything if the local store is not initialized
                if (!hlm.LocalStoreInitialized)
                {
                    throw new HelpLibraryManagerException(viewerVersion,
                                                          HelpLibraryManagerException.LocalStoreNotInitialized);
                }

                // If not specified, try to figure out the default locale
                if (String.IsNullOrEmpty(locale))
                {
                    if (viewerVersion.Major == 1)
                    {
                        locale = hlm.FindLocaleFor(product, version);

                        if (locale == null)
                        {
                            throw new HelpLibraryManagerException(viewerVersion,
                                                                  HelpLibraryManagerException.CatalogNotInstalled, String.Format(
                                                                      CultureInfo.InvariantCulture, "Product: {0}  Version: {1}", product, version));
                        }
                    }
                    else
                    {
                        locale = hlm.FindLocaleFor(catalogName);

                        if (locale == null)
                        {
                            throw new HelpLibraryManagerException(viewerVersion,
                                                                  HelpLibraryManagerException.CatalogNotInstalled, String.Format(
                                                                      CultureInfo.InvariantCulture, "Catalog Name: {0}", catalogName));
                        }
                    }

                    Console.WriteLine("No locale specified, the default locale '{0}' will be used", locale);

                    execArgs.Add("/locale");
                    execArgs.Add(locale);
                }

                // Execute the request
                Console.WriteLine("Running Help Library Manager to perform the requested action.  Please wait...");

                commandLine = String.Join(" ", execArgs.ToArray());

                try
                {
                    // If installing, we must always run as administrator.  Everything else can run as a normal
                    // user.
                    if (isInstall)
                    {
                        if (isSilent)
                        {
                            result = hlm.RunAsAdministrator(commandLine, ProcessWindowStyle.Minimized);
                        }
                        else
                        {
                            result = hlm.RunAsAdministrator(commandLine, ProcessWindowStyle.Normal);
                        }
                    }
                    else
                    {
                        result = hlm.RunAsNormalUser(commandLine, ProcessWindowStyle.Minimized);

                        // For content manager for Help Viewer 2.0 returns almost immediately.  If there is not
                        // content to uninstall, a subsequent install attempt can fail because the other instance
                        // hasn't quite shutdown yet.  This works around the issue by pausing for a couple of
                        // seconds.
                        if (viewerVersion.Major == 2)
                        {
                            System.Threading.Thread.Sleep(2000);
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw new HelpLibraryManagerException(viewerVersion, HelpLibraryManagerException.UnknownError,
                                                          String.Format(CultureInfo.InvariantCulture, "Failed to execute \"{0}\" {1}.\r\nError: {2}",
                                                                        hlm.HelpLibraryManagerPath, commandLine, ex.Message));
                }

                if (result != HelpLibraryManagerException.Success)
                {
                    throw new HelpLibraryManagerException(viewerVersion, result);
                }

                Console.WriteLine("The operation completed successfully");
            }
            catch (HelpLibraryManagerException hlmEx)
            {
                result = hlmEx.ErrorCode;
                Console.WriteLine("\r\nERROR: The requested operation could not be performed.\r\nDetails: {0}",
                                  hlmEx.Message);
            }
            catch (Exception ex)
            {
                result = HelpLibraryManagerException.UnknownError;
                Console.WriteLine("\r\nERROR: The requested operation could not be performed.\r\nDetails: {0}",
                                  ex.ToString());
            }
#if DEBUG
            if (System.Diagnostics.Debugger.IsAttached)
            {
                Console.WriteLine("Hit ENTER to exit...");
                Console.ReadLine();
            }
#endif
            return(result);
        }
        //=====================================================================

        /// <summary>
        /// This is the main program entry point
        /// </summary>
        /// <param name="args">The command line arguments</param>
        /// <returns>An exit code that indicates the success or failure of the process</returns>
        public static int Main(string[] args)
        {
            List<CommandLineArgument> allArgs = new List<CommandLineArgument>();
            List<string> execArgs = new List<string>();
            string product = null, version = null, locale = null, catalogName = null, commandLine;
            int result = HelpLibraryManagerException.Success;
            bool isInstall = false, isSilent = false, showHelp = false;
            Version viewerVersion = new Version(1, 0);

            Assembly asm = Assembly.GetExecutingAssembly();

            FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(asm.Location);
            Console.WriteLine("{0}, version {1}\r\n{2}\r\nE-Mail: [email protected]\r\n",
                fvi.ProductName, fvi.ProductVersion, fvi.LegalCopyright);

            try
            {
                // Parse the command line arguments
                foreach(string arg in args)
                    allArgs.Add(new CommandLineArgument(arg));

                for(int idx = 0; idx < allArgs.Count; idx++)
                {
                    if(allArgs[idx].IsSwitch)
                    {
                        // This is used internally and isn't passed on
                        if(allArgs[idx].MatchesSwitch("viewerVersion") && idx < allArgs.Count - 1)
                        {
                            idx++;
                            viewerVersion = new Version(allArgs[idx].Value);
                            continue;
                        }

                        if(allArgs[idx].MatchesSwitch("catalogName") && idx < allArgs.Count - 1)
                            catalogName = allArgs[idx + 1].Value;

                        if(allArgs[idx].MatchesSwitch("product") && idx < allArgs.Count - 1)
                            product = allArgs[idx + 1].Value;

                        if(allArgs[idx].MatchesSwitch("version") && idx < allArgs.Count - 1)
                            version = allArgs[idx + 1].Value;

                        if(allArgs[idx].MatchesSwitch("locale") && idx < allArgs.Count - 1)
                            locale = allArgs[idx + 1].Value;

                        if(allArgs[idx].MatchesSwitch("install") || allArgs[idx].MatchesSwitch("sourceMedia") ||
                          allArgs[idx].MatchesSwitch("sourceUri"))
                            isInstall = true;

                        if(allArgs[idx].MatchesSwitch("silent"))
                            isSilent = true;

                        if(allArgs[idx].MatchesSwitch("help") || allArgs[idx].Value[1] == '?')
                            showHelp = true;
                    }

                    execArgs.Add(allArgs[idx].ToCommandLineOption());
                }

                if(allArgs.Count == 0 || showHelp)
                {
                    ShowHelp();
                    return HelpLibraryManagerException.MissingCommandLineArgument;
                }

                if(viewerVersion.Major == 1)
                {
                    // These two are required for Help Viewer 1.0
                    if(String.IsNullOrEmpty(product))
                        throw new HelpLibraryManagerException(viewerVersion,
                            HelpLibraryManagerException.MissingCommandLineArgument, "/product");

                    if(String.IsNullOrEmpty(version))
                        throw new HelpLibraryManagerException(viewerVersion,
                            HelpLibraryManagerException.MissingCommandLineArgument, "/version");

                    // This is only used by Help Viewer 2.0
                    if(!String.IsNullOrEmpty(catalogName))
                        throw new HelpLibraryManagerException(viewerVersion,
                            HelpLibraryManagerException.InvalidCmdArgs,
                            "/catalogName is only valid for Help Viewer 2.0");
                }
                else
                {
                    if(!String.IsNullOrEmpty(product))
                        throw new HelpLibraryManagerException(viewerVersion,
                            HelpLibraryManagerException.InvalidCmdArgs,
                            "/product is only valid for Help Viewer 1.0");

                    if(!String.IsNullOrEmpty(version))
                        throw new HelpLibraryManagerException(viewerVersion,
                            HelpLibraryManagerException.InvalidCmdArgs,
                            "/version is only valid for Help Viewer 1.0");

                    // If not specified, default the catalog name based on the viewer version
                    if(String.IsNullOrEmpty(catalogName))
                    {
                        catalogName = HelpLibraryManager.DefaultCatalogName(viewerVersion);

                        if(catalogName == null)
                            throw new HelpLibraryManagerException(viewerVersion,
                                HelpLibraryManagerException.MissingCommandLineArgument, "/catalogName");

                        Console.WriteLine("Catalog name not specified, the default catalog name '{0}' will " +
                            "be used.", catalogName);

                        execArgs.Add("/catalogName");
                        execArgs.Add(catalogName);
                    }
                }

                HelpLibraryManager hlm = new HelpLibraryManager(viewerVersion);

                // Can't do anything if the Help Library Manager is not installed
                if(hlm.HelpLibraryManagerPath == null)
                    throw new HelpLibraryManagerException(viewerVersion,
                        HelpLibraryManagerException.HelpLibraryManagerNotFound);

                // Can't do anything if the Help Library Manager is already running
                if(Process.GetProcessesByName(Path.GetFileNameWithoutExtension(hlm.HelpLibraryManagerPath)).Length > 0)
                    throw new HelpLibraryManagerException(viewerVersion,
                        HelpLibraryManagerException.HelpLibraryManagerAlreadyRunning);

                // Can't do anything if the local store is not initialized
                if(!hlm.LocalStoreInitialized)
                    throw new HelpLibraryManagerException(viewerVersion,
                        HelpLibraryManagerException.LocalStoreNotInitialized);

                // If not specified, try to figure out the default locale
                if(String.IsNullOrEmpty(locale))
                {
                    if(viewerVersion.Major == 1)
                    {
                        locale = hlm.FindLocaleFor(product, version);

                        if(locale == null)
                            throw new HelpLibraryManagerException(viewerVersion,
                                HelpLibraryManagerException.CatalogNotInstalled, String.Format(
                                CultureInfo.InvariantCulture, "Product: {0}  Version: {1}", product, version));
                    }
                    else
                    {
                        locale = hlm.FindLocaleFor(catalogName);

                        if(locale == null)
                            throw new HelpLibraryManagerException(viewerVersion,
                                HelpLibraryManagerException.CatalogNotInstalled, String.Format(
                                CultureInfo.InvariantCulture, "Catalog Name: {0}", catalogName));
                    }

                    Console.WriteLine("No locale specified, the default locale '{0}' will be used", locale);

                    execArgs.Add("/locale");
                    execArgs.Add(locale);
                }

                // Execute the request
                Console.WriteLine("Running Help Library Manager to perform the requested action.  Please wait...");

                commandLine = String.Join(" ", execArgs.ToArray());

                try
                {
                    // If installing, we must always run as administrator.  Everything else can run as a normal
                    // user.
                    if(isInstall)
                    {
                        if(isSilent)
                            result = hlm.RunAsAdministrator(commandLine, ProcessWindowStyle.Minimized);
                        else
                            result = hlm.RunAsAdministrator(commandLine, ProcessWindowStyle.Normal);
                    }
                    else
                    {
                        result = hlm.RunAsNormalUser(commandLine, ProcessWindowStyle.Minimized);

                        // For content manager for Help Viewer 2.0 returns almost immediately.  If there is not
                        // content to uninstall, a subsequent install attempt can fail because the other instance
                        // hasn't quite shutdown yet.  This works around the issue by pausing for a couple of
                        // seconds.
                        if(viewerVersion.Major == 2)
                            System.Threading.Thread.Sleep(2000);
                    }
                }
                catch(Exception ex)
                {
                    throw new HelpLibraryManagerException(viewerVersion, HelpLibraryManagerException.UnknownError,
                        String.Format(CultureInfo.InvariantCulture, "Failed to execute \"{0}\" {1}.\r\nError: {2}",
                        hlm.HelpLibraryManagerPath, commandLine, ex.Message));
                }

                if(result != HelpLibraryManagerException.Success)
                    throw new HelpLibraryManagerException(viewerVersion, result);

                Console.WriteLine("The operation completed successfully");
            }
            catch(HelpLibraryManagerException hlmEx)
            {
                result = hlmEx.ErrorCode;
                Console.WriteLine("\r\nERROR: The requested operation could not be performed.\r\nDetails: {0}",
                    hlmEx.Message);
            }
            catch(Exception ex)
            {
                result = HelpLibraryManagerException.UnknownError;
                Console.WriteLine("\r\nERROR: The requested operation could not be performed.\r\nDetails: {0}",
                    ex.ToString());
            }
#if DEBUG
            if(System.Diagnostics.Debugger.IsAttached)
            {
                Console.WriteLine("Hit ENTER to exit...");
                Console.ReadLine();
            }
#endif
            return result;
        }
Example #17
0
        /// <summary>
        /// Launch the Help Library Manager for interactive use based on the
        /// current project's settings.
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The event arguments</param>
        private void miLaunchHlm_Click(object sender, EventArgs e)
        {
            try
            {
                HelpLibraryManager hlm = new HelpLibraryManager();

                hlm.LaunchInteractive(String.Format(CultureInfo.InvariantCulture,
                    "/product \"{0}\" /version \"{1}\" /locale {2} /brandingPackage Dev10.mshc",
                    project.CatalogProductId, project.CatalogVersion, project.Language.Name));
            }
            catch(Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());

                MessageBox.Show(String.Format(CultureInfo.CurrentCulture,
                    "Unable to launch Help Library Manager.  Reason:\r\n{0}",
                    ex.Message), Constants.AppName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }