-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMainWindow.xaml.cs
462 lines (391 loc) · 17.4 KB
/
MainWindow.xaml.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Reflection;
//using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;
using System.Xml.Xsl;
namespace FailedRequestTraceViewer2
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private readonly SynchronizationContext _syncContext;
WatchFolders _foldernotificationhelper = new WatchFolders();
bool fFinishedImporting = true; // false change to true
public int iD = 1;
public bool fWebviewVisible = true;
bool _captureTraffic = true;
string sFrebXSL;
bool _IE_XSL_Mode = false;
bool CaptureTraffic {
get { return _captureTraffic; }
set { _captureTraffic = value;
if (value == true)
_foldernotificationhelper.RefreshIISFolders();
}
}
Object lockAddToObservableCollection;
FRTUtil frtfolders = new FRTUtil();
public ObservableCollection<FailedRequestTraceFile> FailedRequestTracesInGrid { get; set; }
//Dictionary<string, FileSystemWatcher> watchedFolders = new Dictionary<string, FileSystemWatcher>();
//= new ObservableCollection<FailedRequestTraceFile>();
// Define the event handlers.
private void WatchOnChanged(object source, FileSystemEventArgs e)
{
if (CaptureTraffic == false)
{
return;
}
// Specify what is done when a file is changed, created, or deleted.
lock (lockAddToObservableCollection)
{
if (!fFinishedImporting)
{
frtfolders.ImportFailedRequestTraceFile(e.FullPath);
}
else
{
if (AddFailedRequestTraceFileToGrid(e.FullPath))
{
}
// add to observable collection :)
}
}
//Console.WriteLine($"File: {e.FullPath} {e.ChangeType}");
}
public void resize_controls()
{
var a = wpfStackBorder.ActualHeight;
var b = this.ActualWidth;
var c = this.Height;
var d = this.Width;
//stacker_panell.Height = a;
var desiredheight = fWebviewVisible?(a - wpfMenu.ActualHeight) / 2: (a - wpfMenu.ActualHeight); // - System.Windows.SystemParameters.WindowCaptionHeight*22/39) / 2;
wpfGrid.Height = desiredheight;
wpfBrowserBorder.Height = fWebviewVisible? desiredheight:0;
//wbSample.Height = double.NaN; // (a - wpfMenu.ActualHeight) / 2;
wbSample.Height = fWebviewVisible? desiredheight:0;
}
// returns true if an item was added
// caller is responsible for locking the array.
public void update_size(object sender, RoutedEventArgs e)
{
resize_controls();
}
public bool AddFailedRequestTraceFileToGrid(string sPathToFile)
{
FailedRequestTraceFile frtTraceFile;
if (frtfolders.GenerateFRTObjectFromFile(sPathToFile, out frtTraceFile))
{
// this needs to be done on the UI thread!
frtTraceFile.sID = iD.ToString();
iD++;
_syncContext.Post(o => FailedRequestTracesInGrid.Add(frtTraceFile), null);
//Application.Current.Dispatcher.Invoke(new Action(() =>
//{
// FailedRequestTracesInGrid.Add(frtTraceFile);
// // item was added... need to refresh
// //this.frtdata.Items.Refresh();
//}));
return true;
}
return false;
}
/* public void WatchThisPath(string sPath, bool fWatch)
{
if (!fWatch)
{
if (watchedFolders.ContainsKey(sPath))
{
watchedFolders.Remove(sPath);
}
// otherwise do nothing
return;
}
if (fWatch)
{
if (watchedFolders.ContainsKey(sPath))
return;
FileSystemWatcher watch = new FileSystemWatcher();
watch.Path = sPath;
// Watch for changes in LastAccess and LastWrite times, and
// the renaming of files or directories.
watch.NotifyFilter = NotifyFilters.LastAccess
| NotifyFilters.LastWrite
| NotifyFilters.FileName
| NotifyFilters.DirectoryName;
// Only watch text files.
watch.Filter = "*.xml";
// Add event handlers.
//watch.Changed += OnChanged;
watch.Created += WatchOnChanged;
//watch.Deleted += OnChanged;
//watch.Renamed += OnRenamed;
// Begin watching.
watch.EnableRaisingEvents = true;
watchedFolders.Add(sPath, watch);
}
return;
}
*/
private string TraceItemToFile(FailedRequestTraceFile item)
{
string sTempFile;
string filecontent;
if (_IE_XSL_Mode)
{
filecontent = item.sFileContents;
sTempFile = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".xml";
}
else
{
filecontent = TransformXMLToHTML(item.sFileContents);
sTempFile = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".html";
}
//string sTempFile = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".xml";
File.WriteAllText(sTempFile, filecontent);
return sTempFile;
}
private void Row_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string sTempFile = (e.AddedItems.Count > 0)?TraceItemToFile((FailedRequestTraceFile)e.AddedItems[0]):"about:blank";
wbSample.Navigate(sTempFile);
}
private string TransformXMLToHTML(string inputXml)//, string xsltString)
{
if (string.IsNullOrEmpty(sFrebXSL))
{
try
{
sFrebXSL = File.ReadAllText(System.IO.Path.GetTempPath() + "\\freb.xsl");
} catch(Exception eeek)
{
return "";
}
}
XslCompiledTransform transform = new XslCompiledTransform();
using (XmlReader reader = XmlReader.Create(new StringReader(sFrebXSL)))
{
transform.Load(reader);
}
StringWriter results = new StringWriter();
using (XmlReader reader = XmlReader.Create(new StringReader(inputXml)))
{
transform.Transform(reader, null, results);
}
return results.ToString();
}
private void Row_Select(object sender, MouseButtonEventArgs e)
{
DataGridRow row = sender as DataGridRow;
string sTempFile = TraceItemToFile((FailedRequestTraceFile)row.Item);
wbSample.Navigate(sTempFile);
}
private void Row_DoubleClick(object sender, MouseButtonEventArgs e)
{
DataGridRow row = sender as DataGridRow;
string sTempFile = TraceItemToFile((FailedRequestTraceFile)row.Item);
System.Diagnostics.Process.Start(sTempFile);
// Some operations with this row
}
private void mnuCaptureTraffic_Click(object sender, RoutedEventArgs e)
{
CaptureTraffic = ((MenuItem)e.Source).IsChecked;
//MessageBox.Show("Not Implemented!");
}
private void mnuImportFolder_Click(object sender, RoutedEventArgs e)
{
SortedDictionary<DateTime, string> filesSortedByDate = new SortedDictionary<DateTime, string>();
Ookii.Dialogs.Wpf.VistaFolderBrowserDialog folderToAdd = new Ookii.Dialogs.Wpf.VistaFolderBrowserDialog();
folderToAdd.ShowNewFolderButton = true;
folderToAdd.Description = "Select folder to import Failed Request Trace logs...";
// Default to the My Documents folder.
folderToAdd.RootFolder = Environment.SpecialFolder.MyComputer;
bool? result = folderToAdd.ShowDialog();
if (result == true)
{
var xmlFiles = Directory.EnumerateFiles(folderToAdd.SelectedPath, "*.xml", SearchOption.TopDirectoryOnly);
foreach (string currentFile in xmlFiles)
{
FailedRequestTraceFile frtTraceFile;
FRTUtil boo = new FRTUtil();
if (boo.GenerateFRTObjectFromFile(currentFile, out frtTraceFile))
{
DateTime creationtime = frtTraceFile.sTime; // DateTime.Parse(frtTraceFile.sTime);
// Resolve conflict if two different requests have the same time stamp
// Note that this is only important during initial import...
// once historical files are imported, new files will be added sequentially.
while (filesSortedByDate.ContainsKey(creationtime))
{
creationtime += new TimeSpan(0, 0, 0, 0, 1);
}
filesSortedByDate.Add(creationtime, frtTraceFile.sFilePath);
}
}
foreach (DateTime abc in filesSortedByDate.Keys)
{
AddFailedRequestTraceFileToGrid(filesSortedByDate[abc]);
}
//lock (lockAddToObservableCollection)
//{
// AddFailedRequestTraceFileToGrid(currentFile);
//}
// if we don't have a freb.xsl in the user's temp folder,
// and if one exists in this path,
// copy to user's temp folder
if (!File.Exists(System.IO.Path.Combine(System.IO.Path.GetTempPath(), "freb.xsl")))
{
if (File.Exists(System.IO.Path.Combine(folderToAdd.SelectedPath, "freb.xsl")))
{
// copy freb.xsl over
File.Copy(System.IO.Path.Combine(folderToAdd.SelectedPath, "freb.xsl"), System.IO.Path.Combine(System.IO.Path.GetTempPath(), "freb.xsl"));
}
}
}
}
private void mnuExit_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Not Implemented!");
}
private void mnuClearView_Click(object sender, RoutedEventArgs e)
{
lock (lockAddToObservableCollection)
{
_syncContext.Post(o => FailedRequestTracesInGrid.Clear(), null);
}
// MessageBox.Show("Not Implemented!");
}
private void mnuEditColumns_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Not Implemented!");
}
private void mnuViewWebview_Click(object sender, RoutedEventArgs e)
{
fWebviewVisible = ((MenuItem)e.Source).IsChecked;
//wpfBrowserBorder.Visibility = fWebviewVisible ? Visibility.Visible : Visibility.Hidden;
//MessageBox.Show("Not Implemented!");
resize_controls();
}
//mnuIE_XSL_Mode_Click
private void mnuIE_XSL_Mode_Click(object sender, RoutedEventArgs e)
{
_IE_XSL_Mode = ((MenuItem)e.Source).IsChecked;
resize_controls();
}
private void mnuViewFolders_Click(object sender, RoutedEventArgs e)
{
wpfMonitoredFolders.Visibility = ((MenuItem)e.Source).IsChecked?Visibility.Visible:Visibility.Collapsed;
wpfMonitorFolderList.Text = "";
//foreach (string s in watchedFolders.Keys)
//{
// wpfMonitorFolderList.Text = watchedFolders[s].Path + "\n" + wpfMonitorFolderList.Text;
//}
if (wpfMonitoredFolders.Visibility == Visibility.Visible) {
List<string> folders = _foldernotificationhelper.GetWatchedFolders();
foreach (string s in folders)
{
wpfMonitorFolderList.Text = s + "\n" + wpfMonitorFolderList.Text;
}
//stacker_panel2.Visibility = Visibility.Collapsed;
} else
{
//stacker_panel2.Visibility = Visibility.Visible;
}
resize_controls();
}
public MainWindow()
{
_syncContext = SynchronizationContext.Current;
_foldernotificationhelper.WatchCallback = WatchOnChanged;
lockAddToObservableCollection = new Object();
InitializeComponent();
//bool fBreakPoint = true;
// [begin] turn off script errors on web browser
//dynamic activeX = this.wbSample.GetType().InvokeMember("ActiveXInstance",
// System.Reflection.BindingFlags.GetProperty | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic,
// null, this.wbSample, new object[] { });
//activeX.Silent = true;
// [end] turn off script errors on web browser
//HideScriptErrors(wbSample, true);
//wbSample.Navigate("http://www.wpf-tutorial.com");
FailedRequestTracesInGrid = new ObservableCollection<FailedRequestTraceFile>();
wpfGrid.DataContext = this;
//wpfGrid.SetBinding()
_foldernotificationhelper.RefreshIISFolders();
/* if (!frtfolders.GetFailedRequestLocations())
{
Close();
return;
}
// start watching for changes
// Create a new FileSystemWatcher and set its properties.
foreach (string spath in frtfolders.frtIISFolderPaths)
{
WatchThisPath(spath, true);
// also watch immediate subfolders where the files actually get written
var frqsubdirs = Directory.EnumerateDirectories(spath, "*", SearchOption.TopDirectoryOnly);
foreach (string currentDir in frqsubdirs)
{
WatchThisPath(currentDir, true);
}
}
frtfolders.ImportAllTraceFiles();
lock (lockAddToObservableCollection)
{
fFinishedImporting = true;
foreach (DateTime abc in frtfolders.filesSortedByDate.Keys)
{
FailedRequestTraceFile item = frtfolders.initialRequestFiles[frtfolders.filesSortedByDate[abc]];
item.sID = iD.ToString();
FailedRequestTracesInGrid.Add(item);
iD++;
}
}
*/
//frtdata.ItemsSource = null;
//frtdata.ItemsSource = FailedRequestTracesInGrid;
//fBreakPoint = false;
//frtfolders.ImportFailedRequestTraceFile("c:\\Users\\brianc\\Desktop\\15.xml");
}
public void HideScriptErrors(WebBrowser wb, bool Hide)
{
System.Reflection.FieldInfo fiComWebBrowser = typeof(WebBrowser)
.GetField("_axIWebBrowser2",
System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
if (fiComWebBrowser == null) return;
object objComWebBrowser = fiComWebBrowser.GetValue(wb);
if (objComWebBrowser == null) return;
objComWebBrowser.GetType().InvokeMember(
"Silent", System.Reflection.BindingFlags.SetProperty, null, objComWebBrowser,
new object[] { Hide });
}
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
}
private void wbSample_Navigating(object sender, NavigatingCancelEventArgs e)
{
dynamic activeX = this.wbSample.GetType().InvokeMember("ActiveXInstance",
BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.NonPublic,
null, this.wbSample, new object[] { });
activeX.Silent = true;
}
}
}