Archived
1
0
This repository has been archived on 2021-10-19. You can view files and clone it, but cannot push or open issues or pull requests.
nonabgs/MainWindow.xaml.cs

244 lines
7.0 KiB
C#

using System;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Ookii.Dialogs.Wpf;
using NonaBGS.Journal;
using NonaBGS.BGS;
using NonaBGS.Util;
using NonaBGS.EDDB;
using NonaBGS.UI;
namespace NonaBGS {
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window {
private PlayerJournal journal = null;
private Report report = new Report();
private Config config = new Config();
private API api = null;
private PopulatedSystems systems_db = null;
private Stations stations_db = null;
public Config Config => config;
public Report Report => report;
public MainWindow() {
InitializeComponent();
try {
config.LoadGlobal();
} catch (Exception) {
/* ignored */
}
report.OnLog += Report_OnLog;
api = new API(config.ConfigPath);
journal = new PlayerJournal(config.Global.JournalLocation);
// Set both to now
startdate.SelectedDate = DateTime.Now;
enddate.SelectedDate = DateTime.Now;
journallocation.Text = Config.Global.JournalLocation;
useeddb.IsChecked = Config.Global.UseEDDB;
try {
config.LoadObjectives(Report);
RefreshObjectives();
} catch (Exception e) {
Log(e.Message);
}
api.SystemsAvailable += Api_SystemsAvailable;
api.StationsAvailable += Api_StationsAvailable;
try {
SyncDatabases();
} catch (Exception e) {
Log(e.Message);
}
}
private void Api_StationsAvailable() {
try {
stations_db = api.MakeStations();
} catch (Exception e) {
Log(e.Message);
}
}
private void Api_SystemsAvailable() {
try {
systems_db = api.MakePopulatedSystems();
system.Provider = new SystemSuggestionProvider(systems_db);
} catch (Exception e) {
Log(e.Message);
}
}
private void Report_OnLog(string message) {
StringBuilder builder = new StringBuilder();
builder.Append(DateTime.Now.ToString());
builder.Append(": ");
builder.Append(message);
builder.Append("\n");
log.AppendText(builder.ToString());
}
private void Log(string message) {
Report_OnLog(message);
}
private void SyncDatabases() {
if (!config.Global.UseEDDB) {
return;
}
api.Download();
}
private void RefreshObjectives() {
entries.Items.Clear();
if (report.Objectives == null) {
return;
}
foreach (var obj in report.Objectives) {
var item_objective = new TreeViewItem {
Header = obj.ToString(),
Tag = obj
};
foreach (var log in obj.LogEntries) {
var log_objective = new TreeViewItem {
Header = log.ToString(),
Tag = log
};
item_objective.Items.Add(log_objective);
}
item_objective.IsExpanded = true;
entries.Items.Add(item_objective);
}
}
private void ParseJournal_Click(object sender, RoutedEventArgs e) {
journal.Open(); // Load all files
var start = startdate.SelectedDate ?? DateTime.Now;
var end = startdate.SelectedDate ?? DateTime.Now;
report.Scan(journal, start, end);
RefreshObjectives();
}
private void AddObjective() {
Objective objective = new Objective {
System = system.Text,
Faction = faction.Text,
Station = station.Text
};
if (!objective.IsValid) {
return;
}
if (report.AddObjective(objective)) {
RefreshObjectives();
config.SaveObjectives(Report);
}
}
private void AddFilter_Click(object sender, RoutedEventArgs e) {
AddObjective();
}
private void GenerateDiscord_Click(object sender, RoutedEventArgs e) {
NonaDiscordLog discord = new NonaDiscordLog();
string report = discord.GenerateDiscordLog(Report);
DiscordLog.Text = report;
}
private void RemoveCurrentObjective() {
if (entries.SelectedItem == null) {
return;
}
TreeViewItem item = entries.SelectedItem as TreeViewItem;
var obj = item.Tag;
bool removed = false;
if (obj.GetType() == typeof(Objective)) {
removed = report.Objectives.Remove(obj as Objective);
} else if (obj.GetType() == typeof(LogEntry) ||
obj.GetType().IsSubclassOf(typeof(LogEntry))) {
var parent = (item.Parent as TreeViewItem).Tag as Objective;
removed = parent.LogEntries.Remove(obj as LogEntry);
}
if (removed) {
RefreshObjectives();
config.SaveObjectives(Report);
}
}
private void entries_KeyUp(object sender, KeyEventArgs e) {
if (e.Key == Key.Delete) {
RemoveCurrentObjective();
}
}
private void browsejournallocation_Click(object sender, RoutedEventArgs e) {
var dialog = new VistaFolderBrowserDialog();
if ((bool)!dialog.ShowDialog()) {
return;
}
Config.Global.JournalLocation = dialog.SelectedPath;
journallocation.Text = Config.Global.JournalLocation;
journal = new PlayerJournal(config.Global.JournalLocation);
}
private void useeddb_Click(object sender, RoutedEventArgs e) {
Config.Global.UseEDDB = (bool)useeddb.IsChecked;
SyncDatabases();
}
private void Filter_KeyDown(object sender, KeyEventArgs e) {
if (e.Key == Key.Enter) {
AddObjective();
}
}
private void station_GotFocus(object sender, RoutedEventArgs e) {
try {
if (stations_db == null || systems_db == null) {
return;
}
var sys = system.Text;
if (sys == null || sys.Length <= 0) {
return;
}
int system_id = systems_db.ToId(sys);
station.Provider = new StationSuggestionProvider(stations_db, system_id);
} catch (Exception exc) {
Log(exc.Message);
}
}
}
}