/ src / modules / launcher / PowerLauncher / ReportWindow.xaml.cs
ReportWindow.xaml.cs
 1  // Copyright (c) Microsoft Corporation
 2  // The Microsoft Corporation licenses this file to you under the MIT license.
 3  // See the LICENSE file in the project root for more information.
 4  
 5  using System;
 6  using System.Globalization;
 7  using System.IO;
 8  using System.IO.Abstractions;
 9  using System.Linq;
10  using System.Text;
11  using System.Windows;
12  using System.Windows.Documents;
13  using System.Windows.Media.Imaging;
14  
15  using PowerLauncher.Helper;
16  using Wox.Infrastructure.Image;
17  using Wox.Plugin.Logger;
18  
19  namespace PowerLauncher
20  {
21      internal sealed partial class ReportWindow
22      {
23          private static readonly IFileSystem FileSystem = new FileSystem();
24          private static readonly IFile File = FileSystem.File;
25  
26          public ReportWindow(Exception exception)
27          {
28              InitializeComponent();
29              BitmapImage image = GetImageFromPath(ImageLoader.ErrorIconPath);
30              if (image != null)
31              {
32                  Icon = image;
33              }
34  
35              ErrorTextbox.Document.Blocks.FirstBlock.Margin = new Thickness(0);
36              SetException(exception);
37          }
38  
39          private void SetException(Exception exception)
40          {
41              string path = Log.CurrentLogDirectory;
42              var directory = new DirectoryInfo(path);
43              var log = directory.GetFiles().OrderByDescending(f => f.LastWriteTime).FirstOrDefault();
44  
45              LogFilePathBox.Text = log?.FullName;
46  
47              StringBuilder content = new StringBuilder();
48              content.AppendLine(ErrorReporting.RuntimeInfo());
49  
50              // Using CurrentCulture since this is displayed to user in the report window
51              content.AppendLine(CultureInfo.CurrentCulture, $"Date: {DateTime.Now.ToString(CultureInfo.CurrentCulture)}");
52              content.AppendLine("Exception:");
53              content.AppendLine(exception.ToString());
54              var paragraph = new Paragraph();
55              paragraph.Inlines.Add(content.ToString());
56              ErrorTextbox.Document.Blocks.Add(paragraph);
57          }
58  
59          // Function to get the Bitmap Image from the path
60          private static BitmapImage GetImageFromPath(string path)
61          {
62              if (File.Exists(path))
63              {
64                  MemoryStream memoryStream = new MemoryStream();
65  
66                  byte[] fileBytes = File.ReadAllBytes(path);
67                  memoryStream.Write(fileBytes, 0, fileBytes.Length);
68                  memoryStream.Position = 0;
69  
70                  var image = new BitmapImage();
71                  image.BeginInit();
72                  image.StreamSource = memoryStream;
73                  image.EndInit();
74                  return image;
75              }
76              else
77              {
78                  return null;
79              }
80          }
81  
82          private void RepositoryHyperlink_Click(object sender, RoutedEventArgs e)
83          {
84              var uri = (sender as Hyperlink).NavigateUri.ToString();
85              Wox.Infrastructure.Helper.OpenInShell(uri);
86          }
87      }
88  }