-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSoylent.cs
executable file
·380 lines (321 loc) · 14.9 KB
/
Soylent.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Word = Microsoft.Office.Interop.Word;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Word;
using Microsoft.Office.Tools.Word.Extensions;
using System.Windows.Controls;
using System.Windows.Forms.Integration;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Deployment.Application;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using Soylent.Model;
using Soylent.Model.Shortn;
using Soylent.Model.Crowdproof;
using Soylent.Model.HumanMacro;
using Soylent.View;
using Soylent.View.Crowdproof;
using System.Text.RegularExpressions;
namespace Soylent
{
public partial class Soylent
{
public Microsoft.Office.Tools.CustomTaskPane HITView;
//public SoylentPanel soylent;
//private TurKitSocKit tksc;
private TurKitHTTP tkhttp;
public JobManager jobManager;
public Dictionary<Word.Document, SoylentPanel> soylentMap = new Dictionary<Word.Document,SoylentPanel>();
public Dictionary<int, Word.Document> jobToDoc = new Dictionary<int, Word.Document>();
public static bool DEBUG = true;
/// <summary>
/// Get the directory where the application stores its deployed data files (e.g., *.xml)
/// </summary>
/// <returns></returns>
public static string GetDataDirectory()
{
string result;
if (ApplicationDeployment.IsNetworkDeployed)
{
result = ApplicationDeployment.CurrentDeployment.DataDirectory;
}
else
{
result = AppDomain.CurrentDomain.BaseDirectory;
}
if (!result.EndsWith(Path.DirectorySeparatorChar.ToString()))
{
result = result + Path.DirectorySeparatorChar;
}
return result;
}
public static string GetAppDirectory()
{
string result = AppDomain.CurrentDomain.BaseDirectory;
if (!result.EndsWith(Path.DirectorySeparatorChar.ToString()))
{
result = result + Path.DirectorySeparatorChar;
}
return result;
}
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
//Globals.Ribbons.Ribbon.debug.Visible = true;
InitializeActiveHitsDirectory();
jobManager = new JobManager();
tkhttp = new TurKitHTTP();
tkhttp.Listen();
//For save/load of hits
this.Application.DocumentOpen += new Word.ApplicationEvents4_DocumentOpenEventHandler(Application_DocumentOpen);
this.Application.DocumentBeforeSave += new Word.ApplicationEvents4_DocumentBeforeSaveEventHandler(Application_DocumentBeforeSave);
this.Application.DocumentChange += new Word.ApplicationEvents4_DocumentChangeEventHandler(Application_DocumentChange);
this.Application.DocumentBeforeClose += new Word.ApplicationEvents4_DocumentBeforeCloseEventHandler(Application_DocumentBeforeClose);
/*
MessageBox.Show("AppDomain: " + AppDomain.CurrentDomain.BaseDirectory);
MessageBox.Show("Deploy: " + System.Deployment.Application.ApplicationDeployment.CurrentDeployment.DataDirectory);
MessageBox.Show("Assembly Location: " + System.Reflection.Assembly.GetExecutingAssembly().Location);
MessageBox.Show("Assembly CodeBase: " + System.Reflection.Assembly.GetExecutingAssembly().CodeBase);
*/
}
/// <summary>
/// Creates the active hits directory if it doesn't exist yet
/// </summary>
private void InitializeActiveHitsDirectory()
{
String root = GetDataDirectory();
String path = root + @"active-hits";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
File.Copy(GetAppDirectory()+@"turkit\active-hits\README.txt", path+@"\README.txt");
}
}
void Application_DocumentChange()
{
Word.Document doc = this.Application.ActiveDocument;
// If the document is already open, don't worry about it. If not (e.g. created a new document, loaded old), set up its panel
if (!soylentMap.Keys.Contains<Word.Document>(doc)){
SoylentPanel soylent = new SoylentPanel();
HITView = this.CustomTaskPanes.Add(soylent, "Soylent");
HITView.VisibleChanged += new EventHandler(hitviewVisibleChanged);
//HITView.Visible = true;
addDocToMap(soylent);
}
}
public void addDocToMap(SoylentPanel sp)
{
Word.Document doc = this.Application.ActiveDocument;
soylentMap[doc] = sp;
}
void Application_DocumentOpen(Word.Document doc)
{
SoylentPanel soylent = soylentMap[doc];
List<string> rawHITs = new List<string>();
//One problem: loads jobs in reverse order. This is easy to fix. But is either correct?
foreach (Microsoft.Office.Core.CustomXMLPart xmlPart in Globals.Soylent.Application.ActiveDocument.CustomXMLParts)
{
string xml = xmlPart.XML;
Regex typeRegex = new Regex("<job>(.*?)</job>"); //To filter out Soylent jobs from the xml parts Word automatically saves
Match regexResult = typeRegex.Match(xml);
string jobString = regexResult.ToString();
if (jobString.Length < 6) { continue; }
int job = Int32.Parse(jobString.Substring(5, jobString.Length - 11));
jobToDoc[job] = doc;
rawHITs.Add(xml);
}
rawHITs.Reverse();
foreach(string xml in rawHITs)
{
StringReader sr = new StringReader(xml);
XmlReader xr = XmlReader.Create(sr);
if (new Regex("</ShortnData>").Match(xml).Length > 0)
{
XmlSerializer serializer = new XmlSerializer(typeof(ShortnData));
object raw = serializer.Deserialize(xr);
ShortnData hit = raw as ShortnData;
// HACK: have to do this after deserialization because numParagraphs is 0 when the object is deserialized
foreach(StageData stage in hit.stages) {
stage.FixParagraphNumber(hit.numParagraphs);
}
Word.Bookmark a = Globals.Soylent.Application.ActiveDocument.Bookmarks["Soylent" + hit.job];
hit.range = a.Range;
if (hit.jobDone){
ShortnJob s = new ShortnJob(hit, hit.job, false);
//Use saved TurKit messages to recreate the results.
foreach (TurKitSocKit.TurKitStageComplete message in hit.stageCompletes)
{
hit.terminateStage(message);
}
foreach (TurKitSocKit.TurKitFindFixVerify message in hit.findFixVerifies){
hit.postProcessSocKitMessage(message);
}
}
else{
// This will work if you are restarting it on the same machine, where the
// TurKit javascript file still sits. Otherwise, it will restart the job.
ShortnJob s = new ShortnJob(hit, hit.job, true);
}
}
else if (new Regex("</CrowdproofData>").Match(xml).Length > 0)
{
XmlSerializer serializer = new XmlSerializer(typeof(CrowdproofData));
object raw = serializer.Deserialize(xr);
CrowdproofData hit = raw as CrowdproofData;
foreach (StageData stage in hit.stages)
{
stage.FixParagraphNumber(hit.numParagraphs);
}
Word.Bookmark a = Globals.Soylent.Application.ActiveDocument.Bookmarks["Soylent" + hit.job];
hit.range = a.Range;
//SoylentRibbon.setLastJob(hit.job);
if (hit.jobDone)
{
CrowdproofJob s = new CrowdproofJob(hit, hit.job, false);
//Use saved TurKit messages to recreate the results.
foreach (TurKitSocKit.TurKitStageComplete message in hit.stageCompletes)
{
hit.terminateStage(message);
}
foreach (TurKitSocKit.TurKitFindFixVerify message in hit.findFixVerifies)
{
hit.postProcessSocKitMessage(message);
}
}
else
{
CrowdproofJob s = new CrowdproofJob(hit, hit.job, true);
}
}
else if (new Regex("</HumanMacroData>").Match(xml).Length > 0)
{
XmlSerializer serializer = new XmlSerializer(typeof(HumanMacroData));
object raw = serializer.Deserialize(xr);
HumanMacroData hit = raw as HumanMacroData;
foreach (StageData stage in hit.stages)
{
stage.FixParagraphNumber(hit.numParagraphs);
}
Word.Bookmark a = Globals.Soylent.Application.ActiveDocument.Bookmarks["Soylent" + hit.job];
hit.range = a.Range;
//SoylentRibbon.setLastJob(hit.job);
if (hit.jobDone)
{
HumanMacroJob s = new HumanMacroJob(hit, hit.job, false);
//Use saved TurKit messages to recreate the results.
foreach (TurKitSocKit.TurKitHumanMacroResult message in hit.messages)
{
hit.postProcessSocKitMessage(message);
}
hit.finishStageData();
}
else
{
HumanMacroJob s = new HumanMacroJob(hit, hit.job, true);
}
}
}
}
void Application_DocumentBeforeSave(Word.Document Doc, ref bool SaveAsUI, ref bool Cancel)
{
foreach (Microsoft.Office.Core.CustomXMLPart xmlPart in Globals.Soylent.Application.ActiveDocument.CustomXMLParts)
{
if (!(xmlPart.BuiltIn))
{
xmlPart.Delete();
}
}
foreach (object obj in soylentMap[Doc].sidebar.jobs.Children)
{
if (!(obj is StackPanel)) { continue; }
StackPanel elem = obj as StackPanel;
foreach (object elem2 in elem.Children)
{
HITData raw;
if (elem2 is HITView)
{
raw = (elem2 as HITView).data;
}
else
{
raw = (elem2 as HITViewStub).data;
}
if (raw is ShortnData)
{
ShortnData hit = raw as ShortnData;
XmlSerializer x = new XmlSerializer(hit.GetType());
StringWriter sw = new StringWriter();
x.Serialize(sw, hit);
string xml = sw.ToString();
Microsoft.Office.Core.CustomXMLPart xmlPart = Globals.Soylent.Application.ActiveDocument.CustomXMLParts.Add(xml);
}
else if (raw is CrowdproofData)
{
CrowdproofData hit = raw as CrowdproofData;
XmlSerializer x = new XmlSerializer(hit.GetType());
StringWriter sw = new StringWriter();
x.Serialize(sw, hit);
string xml = sw.ToString();
Microsoft.Office.Core.CustomXMLPart xmlPart = Globals.Soylent.Application.ActiveDocument.CustomXMLParts.Add(xml);
}
else if (raw is HumanMacroData)
{
HumanMacroData hit = raw as HumanMacroData;
XmlSerializer x = new XmlSerializer(hit.GetType());
StringWriter sw = new StringWriter();
x.Serialize(sw, hit);
string xml = sw.ToString();
Microsoft.Office.Core.CustomXMLPart xmlPart = Globals.Soylent.Application.ActiveDocument.CustomXMLParts.Add(xml);
}
}
}
}
private void Application_DocumentBeforeClose(Word.Document doc, ref bool cancel)
{
// We need to stop the timer
foreach (object obj in soylentMap[doc].sidebar.jobs.Children)
{
if (!(obj is StackPanel)) { continue; }
StackPanel elem = obj as StackPanel;
foreach (object elem2 in elem.Children)
{
HITData raw;
if (elem2 is HITView)
{
raw = (elem2 as HITView).data;
}
else
{
raw = (elem2 as HITViewStub).data;
}
raw.tk.stopTurKitTimer();
}
}
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
void hitviewVisibleChanged(object sender, EventArgs e)
{
Globals.Ribbons.Ribbon.jobStatus.Checked = HITView.Visible;
}
#region VSTO generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
#endregion
}
}