forked from biblica/tools-paratext-plugin-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParatextPluginManagerPlugin.cs
176 lines (158 loc) · 7.63 KB
/
ParatextPluginManagerPlugin.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
Copyright © 2021 by Biblica, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using AddInSideViews;
using PpmMain.Util;
using System;
using System.AddIn;
using System.AddIn.Pipeline;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace PpmMain
{
/// <summary>
/// Paratext Plugin Manager plugin root class.
/// </summary>
[AddIn(MainConsts.PluginName, Description = MainConsts.PluginDescription, Version = MainConsts.PluginVersion, Publisher = MainConsts.PluginPublisher)]
[QualificationData(PluginMetaDataKeys.menuText, MainConsts.PluginName)]
[QualificationData(PluginMetaDataKeys.insertAfterMenuName, "Tools|")]
[QualificationData(PluginMetaDataKeys.enableWhen, WhenToEnable.always)]
[QualificationData(PluginMetaDataKeys.multipleInstances, CreateInstanceRule.always)]
public class ParatextPluginManagerPlugin : IParatextAddIn2
{
/// <summary>
/// No-op, to fulfill IParatextAddIn2 contract.
/// </summary>
public Dictionary<string, IPluginDataFileMergeInfo> DataFileKeySpecifications => null;
/// <summary>
/// No-op, to fulfill IParatextAddIn2 contract.
///
/// Should never by invoked when CreateInstanceRule.always setting in place (above).
/// </summary>
/// <param name="activeProjectName">Active Paratext project name.</param>
public void Activate(string activeProjectName)
{
}
/// <summary>
/// Called when plugin is requested to shut down.
///
/// Terminates process, since plugins are standalone processes (not in-process libraries).
/// </summary>
public void RequestShutdown()
{
Environment.Exit(0);
}
/// <summary>
/// Overridable utility method to show message boxes.
/// </summary>
/// <param name="messageText">Message box text (required).</param>
/// <param name="messageButtons">Message box buttons (required).</param>
/// <param name="messageIcon">Message box icon (required).</param>
/// <returns>Result from message box call (e.g., "Cancel").</returns>
public virtual DialogResult ShowMessageBox(string messageText, MessageBoxButtons messageButtons, MessageBoxIcon messageIcon)
{
return MessageBox.Show(messageText, "Notice...", messageButtons, messageIcon);
}
/// <summary>
/// Entry point method.
/// </summary>
/// <param name="host">Host interface, providing access to Paratext services.</param>
/// <param name="activeProjectName">Active Paratext project name.</param>
public void Run(IHost host, string activeProjectName)
{
lock (this)
{
// track host & plugin reference for static error utilities
HostUtil.Instance.Host = host;
HostUtil.Instance.TypesettingPreviewPlugin = this;
try
{
// Create main thread & delegate
Application.EnableVisualStyles();
var uiThread = new Thread(() =>
{
#if DEBUG
// Provided because plugins are separate processes that may only be attached to,
// once instantiated (can't run Paratext and automatically attach, as with shared libraries).
ShowMessageBox($"Attach debugger now to PID {Process.GetCurrentProcess().Id}, if needed!",
MessageBoxButtons.OK, MessageBoxIcon.Information);
#endif
try
{
Application.Run(new PluginManagerMainForm());
}
catch (Exception ex)
{
// Variables for tracking error information.
IDictionary<string, string> errorDetails = new Dictionary<string, string>();
string message = ex.Message;
// Report the error
ReportErrorWithDetails(message, errorDetails);
}
finally
{
// Exit process (terminate plugin) once complete, no matter what.
Environment.Exit(0);
}
})
{ IsBackground = false };
// Execute main thread.
uiThread.SetApartmentState(ApartmentState.STA);
uiThread.Start();
}
catch (Exception ex)
{
// Log any errors that make it this far and re-throw to give Paratext a heads-up.
HostUtil.Instance.ReportError(null, ex);
throw;
}
}
}
/// <summary>
/// Function for normalizing how we print errors.
/// </summary>
/// <param name="message">The error message. (required)</param>
/// <param name="details">The error details. (optional)</param>
/// <param name="printException">Whether to print the exception or not. True: print the exception; False: don't print the exception. Default: false</param>
/// <param name="ex">The error's associated exception. (required if <c>printException</c> is <c>true</c>)</param>
public static void ReportErrorWithDetails(
string message,
IDictionary<string, string> details = null,
bool printException = false,
Exception ex = null
)
{
// validate required inputs
_ = message ?? throw new ArgumentNullException(nameof(message));
if (printException)
{
_ = ex ?? throw new ArgumentNullException(nameof(ex));
}
// initialize string builder with error message
StringBuilder msgSb = new StringBuilder($"{message}\r\n");
// add the details of the error the message string builder
if (details != null)
{
foreach (KeyValuePair<string, string> item in details)
{
msgSb.AppendLine($" {item.Key}: {item.Value}");
}
}
// report the prettified error
if (printException)
{
HostUtil.Instance.ReportError(msgSb.ToString(), ex);
}
else
{
HostUtil.Instance.ReportError(msgSb.ToString(), null);
}
}
}
}