EliteBGS/LoadEntriesWindow.xaml.cs

78 lines
2.3 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using Microsoft.Win32;
using EDJournal;
using EliteBGS.Util;
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;
Config config = new Config();
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();
}
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) {
}
}
}
}