EliteBGS/LoadEntriesWindow.xaml.cs

111 lines
3.3 KiB
C#
Raw Normal View History

2022-01-23 15:52:19 +01:00
using System;
using System.Linq;
2022-01-23 15:52:19 +01:00
using System.Collections.Generic;
2022-01-29 16:13:56 +01:00
using System.IO;
2022-01-23 15:52:19 +01:00
using System.Windows;
2022-01-29 16:13:56 +01:00
using Microsoft.Win32;
2022-01-23 15:52:19 +01:00
using EDJournal;
using EliteBGS.BGS;
2022-01-29 16:13:56 +01:00
using EliteBGS.Util;
2022-01-23 15:52:19 +01:00
namespace EliteBGS {
/// <summary>
/// Interaction logic for LoadEntriesWindow.xaml
/// </summary>
public partial class LoadEntriesWindow : Window {
public delegate void EntriesLoadedDelegate(List<Entry> entries);
public event EntriesLoadedDelegate EntriesLoaded;
2022-01-29 16:13:56 +01:00
Config config = new Config();
2022-01-23 15:52:19 +01:00
public LoadEntriesWindow() {
InitializeComponent();
}
private void Load_Click(object sender, RoutedEventArgs e) {
string lines = Lines.Text.Trim();
if (lines.Length <= 0) {
return;
}
try {
List<Entry> entries = new List<Entry>();
foreach (string line in lines.Split('\n')) {
Entry entry = Entry.Parse(line);
entries.Add(entry);
}
if (entries.Count > 0) {
EntriesLoaded?.Invoke(entries);
}
} catch (Exception exception) {
MessageBox.Show(string.Format("There was an error while parsing the JSON: {0}",
exception.ToString()));
}
}
private void Clear_Click(object sender, RoutedEventArgs e) {
Lines.Clear();
}
2022-01-29 16:13:56 +01:00
private void LoadFile_Click(object sender, RoutedEventArgs e) {
OpenFileDialog dialog = new OpenFileDialog();
dialog.DefaultExt = ".log";
dialog.Filter = "Log files (*.log)|*.log|All files (*.*)|*";
var location = config.Global.DefaultJournalLocation;
if (Directory.Exists(location)) {
dialog.InitialDirectory = location;
}
bool result = dialog.ShowDialog(this) ?? false;
if (!result) {
return;
}
try {
using (FileStream stream = File.OpenRead(dialog.FileName)) {
using (StreamReader reader = new StreamReader(stream)) {
Lines.Text = reader.ReadToEnd();
}
}
} catch (Exception) {
}
}
private void DeleteUnimportant_Click(object sender, RoutedEventArgs e) {
string lines = Lines.Text.Trim();
if (lines.Length <= 0) {
return;
}
try {
List<Entry> entries = new List<Entry>();
foreach (string line in lines.Split('\n')) {
Entry entry = Entry.Parse(line);
if (Report.IsRelevant(entry)) {
entries.Add(entry);
}
}
if (entries.Count <= 0) {
return;
}
string[] text = entries
.ConvertAll(x => x.JSON.ToString(Newtonsoft.Json.Formatting.None))
.ToArray()
;
Lines.Text = string.Join("\n", text).Trim();
} catch (Exception exception) {
MessageBox.Show(string.Format("There was an error while parsing the JSON: {0}",
exception.ToString()));
}
}
2022-01-23 15:52:19 +01:00
}
}