buypeeb-cs/MainWindow.cs
2021-10-24 03:42:39 +10:00

974 lines
30 KiB
C#
Executable file

/*
buypeeb - a program to track yahoo jp auctions of peebus.
Copyright (C) 2021 lynnesbian
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Text.Unicode;
using System.Threading;
using System.Threading.Tasks;
using CsvHelper;
using Gtk;
using Timeout = GLib.Timeout;
// ReSharper disable UnusedParameter.Local
namespace Buypeeb {
[SuppressMessage("ReSharper", "UnusedMember.Local")]
internal class MainWindow : Window {
private static readonly SemaphoreSlim TaskLimit = new(6);
private readonly Builder builder;
private readonly Label endingLabel;
private readonly Dictionary<string, CheckButton> filterChecks = new();
private readonly HttpClient httpClient;
private readonly ListStore items;
private readonly TreeView itemTreeView;
private readonly JsonSerializerOptions jsonOptions;
private readonly string location;
private readonly SearchEntry searchEntry;
// TODO: whenever we get something from the builder, cache it for later
// that way we don't need to constantly do "builder.GetObject"s
private readonly Box selectionViewBox;
private readonly Queue<string> updateQueue = new();
private bool queueActive;
private Settings settings;
public MainWindow() : this(new Builder("main.glade")) {
}
private MainWindow(Builder builder) : base(builder.GetObject("wndMain").Handle) {
jsonOptions = new JsonSerializerOptions {
// enable full unicode support to ensure that japanese characters are saved properly
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All),
};
if (Environment.OSVersion.Platform ==
PlatformID.Win32NT) // C:\Users\Beebus\AppData\Roaming\Lynnear Software\buypeeb
{
location = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"Lynnear Software", "buypeeb");
} else // ~/.config/Lynnear Software/buypeeb
{
location = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".config",
"Lynnear Software", "buypeeb");
}
// initialise http client
httpClient = new HttpClient();
var userdata = System.IO.Path.Combine(location, "userdata.json");
if (File.Exists(userdata)) {
try {
var j = File.ReadAllText(userdata);
settings = JsonSerializer.Deserialize<Settings>(j);
} catch {
// ???
Console.WriteLine("oops");
Application.Quit();
}
} else {
settings = new Settings();
}
SaveSettings();
Title = "Buypeeb";
// build main window UI
this.builder = builder;
builder.Autoconnect(this);
// hide the (currently non-functional) help button
((ToolButton) builder.GetObject("HelpButton")).Visible = false;
var menuButtonFilter = (MenuButton) builder.GetObject("MenuButtonFilter");
var menuButtonSort = (MenuButton) builder.GetObject("MenuButtonSort");
menuButtonFilter.Child = (Image) builder.GetObject("ImageFilter");
menuButtonSort.Child = (Image) builder.GetObject("ImageSort");
selectionViewBox = (Box) builder.GetObject("SelectionViewBox");
endingLabel = (Label) builder.GetObject("LabelSelectedEnding");
searchEntry = (SearchEntry) builder.GetObject("FilterSearchEntry");
// construct the list of filters, and ensure they're all disabled
foreach (var name in new List<string> {
"Favourites", "NonFavourites", "Active", "Ended", "EndingToday", "EndingAfterToday", "WithWinPrice",
"WithNoWinPrice",
}) {
filterChecks.Add(name, (CheckButton) builder.GetObject($"CheckButtonFilter{name}"));
filterChecks[name].Active = false;
}
// bind treeview columns to watchlist instead of needing to manually sync its liststore
itemTreeView = (TreeView) builder.GetObject("TreeViewItems");
items = new ListStore(typeof(YahooAuctionsItem));
var filteredItems = new TreeModelFilter(items, null) {VisibleFunc = ItemFilter};
itemTreeView.Model = filteredItems;
// functions for rendering text to the associated columns
TreeCellDataFunc[] funcs = {
RenderColumnFavourite,
RenderColumnName,
RenderColumnPriceYen,
RenderColumnPriceAUD,
RenderColumnEnding,
};
for (var i = 0; i < itemTreeView.Columns.Length; i++) {
var c = itemTreeView.Columns[i];
c.SetCellDataFunc(c.Cells[0], funcs[i]);
}
RenderList();
UpdateItems();
Timeout.Add(1000, UpdateSelectionEndTime);
Timeout.Add(10000, AutoUpdateItems);
DeleteEvent += WindowShutdown;
}
private IEnumerable<YahooAuctionsItem> filterQuery =>
// father forgive me for i have lynned
from item in settings.watchlist.Values.ToList()
where (item.favourite != filterChecks["Favourites"].Active ||
item.favourite == filterChecks["NonFavourites"].Active) &&
(item.Available != filterChecks["Active"].Active ||
item.Available == filterChecks["Ended"].Active) &&
(item.endingToday != filterChecks["EndingToday"].Active ||
item.endingToday == filterChecks["EndingAfterToday"].Active) &&
(item.hasWinPrice != filterChecks["WithWinPrice"].Active ||
item.hasWinPrice == filterChecks["WithNoWinPrice"].Active) &&
(string.IsNullOrWhiteSpace(searchEntry.Text) ||
item.name.ToLower().Contains(searchEntry.Text.ToLower()) ||
item.originalName.ToLower().Contains(searchEntry.Text.ToLower()))
select item;
private IEnumerable<YahooAuctionsItem> outdatedItemQuery =>
// only returns items that meet all of the following:
// - marked as "ready", as in, they aren't in the process of updating
// - not updated since the interval
// - hasn't already ended
from item in settings.watchlist.Values.ToList()
where item.Ready && settings.ItemNotUpdatedSinceInterval(item) && item.endDate.CompareTo(DateTime.UtcNow) > 0
select item;
private YahooAuctionsItem selectedItem {
get {
if (itemTreeView.Selection.CountSelectedRows() == 0) // avoids incurring the wrath of Gtk-CRITICAL **
{
return null;
}
itemTreeView.Selection.GetSelected(out var iter);
return (YahooAuctionsItem) itemTreeView.Model.GetValue(iter, 0);
}
}
private void WindowShutdown(object sender, DeleteEventArgs args) {
SaveSettings();
Application.Quit();
}
// general behaviour
/// <summary>
/// gets the path and iter for a given item id.
/// </summary>
/// <param name="id">the item id to find in the treeview</param>
/// <returns>a tuple of (TreePath, TreeIter)</returns>
private (TreePath path, TreeIter iter) GetRow(string id) {
// TODO: surely there's a better way to do this
itemTreeView.Model.GetIterFirst(out var iter);
var m = (TreeModelFilter) itemTreeView.Model;
for (var i = 0; i < itemTreeView.Model.IterNChildren(); i++) {
var x = (YahooAuctionsItem) itemTreeView.Model.GetValue(iter, 0);
if (x.id == id) {
return (m.ConvertPathToChildPath(m.GetPath(iter)), m.ConvertIterToChildIter(iter));
}
itemTreeView.Model.IterNext(ref iter);
}
Console.WriteLine($"Couldn't find {id}!");
return (null, iter);
}
/// <summary>
/// saves the settings to userdata.json.
/// </summary>
private void SaveSettings() {
var j = JsonSerializer.Serialize(settings, jsonOptions);
var p = System.IO.Path.Combine(location, "userdata.json");
Console.WriteLine(j);
if (!Directory.Exists(location)) {
Directory.CreateDirectory(location);
}
using var fs = File.CreateText(p);
fs.Write(j);
}
/// <summary>
/// updates the item with the given id. this method blocks and is intended to be run from a task.
/// </summary>
/// <param name="id">the id of the item to update</param>
private async void UpdateThread(string id) {
var item = settings.watchlist[id];
// Console.WriteLine($"Updating {id}...");
// set item.ready to false to show that it's still being updated
// this changes a few behaviours, such as displaying the price as "..." instead of whatever is currently stored
item.Ready = false;
Application.Invoke(delegate {
// TODO: find a way to not have to do this. i think we need to avoid actually modifying the items outside of the
// main thread :/
var (path, iter) = GetRow(id);
if (path != null) {
items.EmitRowChanged(path, iter);
}
});
try {
item.Update(await httpClient.GetStringAsync(item.url));
} catch (HttpRequestException e) {
if (e.StatusCode == HttpStatusCode.NotFound) {
item.AuctionEnded();
} else {
Console.WriteLine($"Failed to update item ${id}! Status code ${e.StatusCode}: ${e.Message}");
}
}
Application.Invoke(delegate {
var (path, iter) = GetRow(id);
if (path != null) {
items.EmitRowChanged(path, iter);
}
if (item == selectedItem) {
// if the user has this item selected and it just became ready, enable the selection box and redraw the info
selectionViewBox.Sensitive = true;
UpdateSelectionView();
}
});
item.Ready = true;
// Console.WriteLine($"{id} updated.");
}
/// <summary>
/// processes the update queue. this is a blocking function.
/// </summary>
private void ProcessUpdateQueue() {
queueActive = true;
while (queueActive) {
UpdateItem(updateQueue.Dequeue());
queueActive = updateQueue.TryPeek(out _);
}
}
/// <summary>
/// updates an item with the given id with a new task.
/// </summary>
/// <param name="id">the id of the task to update</param>
/// <param name="renderListWhenDone">whether or not to call this.RenderList() after updating the item</param>
private void UpdateItem(string id, bool renderListWhenDone = false) {
if (settings.watchlist[id].updatedRecently) {
return;
}
// don't start a new task if there are more than `tasklimit` tasks currently running
// this makes sure we don't make 1000 simultaneous requests to yahoo auctions if there are 1000 items on the
// watchlist
TaskLimit.Wait();
Task.Factory.StartNew(() => { UpdateThread(id); }).ContinueWith(task => {
TaskLimit.Release();
if (renderListWhenDone) {
Application.Invoke(delegate { RenderList(); });
}
});
}
/// <summary>
/// add every item in the watchlist to the update queue. if the update queue is already being processed
/// (this.queueActive), this will do nothing.
/// </summary>
private void UpdateItems() {
if (queueActive) {
return;
}
selectionViewBox.Sensitive = false;
foreach (var item in settings.watchlist) {
if (!updateQueue.Contains(item.Key)) {
// set everything to not ready first
// ensures other actions don't attempt to display the items even if UpdateItem hasn't started yet
item.Value.Ready = false;
updateQueue.Enqueue(item.Key);
}
}
if (!updateQueue.TryPeek(out var _)) {
// queue is empty
return;
}
itemTreeView.QueueDraw();
Task.Factory.StartNew(ProcessUpdateQueue);
}
/// <summary>
/// updates the selection view, displaying the id, name, etc. for the currently selected item.
/// </summary>
private void UpdateSelectionView() {
// get the currently selected item
var item = selectedItem;
var infobox = (Box) builder.GetObject("SelectionInfoBox");
if (item == null) {
selectionViewBox.Sensitive = false;
infobox.Visible = false;
((Label) builder.GetObject("LabelSelectedName")).Text = "buypeeb";
return;
}
selectionViewBox.Sensitive = item.Ready;
infobox.Visible = true;
var lastUpdated = item.LastUpdated.ToLocalTime().ToString("MMM dd, HH:mm:ss");
var info = new Dictionary<string, string> {
{"Name", item.name},
{"YahooName", item.originalName},
{"Price", item.priceJpy},
{"PriceAUD", item.priceAud},
{"Ending", "Please wait..."},
{"Bids", $"{item.Bids}"},
{"BuyItNow", item.WinPrice == 0 ? "No" : $"{item.winPriceJpy} ({item.winPriceAud})"},
{"AutoExtension", item.AutoExtension ? "Yes" : "No"}, {
"LastUpdated",
item.UpdateFailed
? $"Failed to update item on {lastUpdated}"
: $"Last updated: {(item.Ready ? lastUpdated : "Right now!")}"
},
};
foreach (var (key, value) in info) {
((Label) builder.GetObject($"LabelSelected{key}")).Text = value;
}
UpdateSelectionEndTime();
var noteBuffer = (TextBuffer) builder.GetObject("TextBufferSelectedNotes");
noteBuffer.Clear();
if (!string.IsNullOrWhiteSpace(item.notes)) {
noteBuffer.Text = item.notes;
}
((ToggleButton) builder.GetObject("ButtonSelectedFavourite")).Active = item.favourite;
}
/// <summary>
/// opens a URL in the user's browser.
/// </summary>
/// <param name="url">the url to open</param>
private static void OpenUrl(string url) {
var psi = new ProcessStartInfo {
FileName = url,
UseShellExecute = true,
};
Process.Start(psi);
}
/// <summary>
/// a simple MessageDialog constructor.
/// </summary>
/// <param name="message">the MessageDialog's format</param>
/// <param name="buttonsType">the MessageDialog's bt</param>
/// <returns></returns>
private MessageDialog MsgBox(string message, ButtonsType buttonsType = ButtonsType.OkCancel) {
var md = new MessageDialog(
this,
DialogFlags.DestroyWithParent | DialogFlags.Modal,
MessageType.Question,
buttonsType,
message
) {KeepAbove = true, Resizable = false, FocusOnMap = true, Title = "Buypeeb"};
return md;
}
/// <summary>
/// show a simple entry dialogue that allows the user to enter text and either cancel or submit it.
/// </summary>
/// <param name="title">the title of the entry dialogue</param>
/// <param name="message">the prompt that should be presented to the user</param>
/// <param name="prefill">a string to prefill the input box with</param>
/// <returns></returns>
private (bool accepted, string response) EntryDialogue(
string title = "Buypeeb", string message = "Hi there!", string prefill = null
) {
var ed = new Dialog(
title,
this,
DialogFlags.DestroyWithParent | DialogFlags.Modal,
/* button_data: */ "Cancel", ResponseType.Cancel, "OK", ResponseType.Ok
) {DefaultResponse = ResponseType.Ok, KeepAbove = true};
var edLabel = new Label(message);
var edEntry = new Entry();
if (!string.IsNullOrWhiteSpace(prefill)) {
edEntry.Text = prefill;
}
edEntry.ActivatesDefault = true;
ed.ContentArea.PackStart(edLabel, true, true, 2);
edLabel.Show();
ed.ContentArea.PackStart(edEntry, true, true, 10);
edEntry.Show();
ed.ContentArea.MarginBottom = 5;
ed.ContentArea.MarginTop = 5;
ed.ContentArea.MarginStart = 5;
ed.ContentArea.MarginEnd = 5;
ed.MarginBottom = 5;
var accepted = (ResponseType) ed.Run();
var response = edEntry.Text;
ed.Dispose();
return (accepted == ResponseType.Ok, response);
}
/// <summary>
/// gets the sort type selected by the user - "NameDescending", "EndingAscending", etc.
/// </summary>
/// <returns>the id of the radiobutton without the "Sort" prefix</returns>
private string GetSortType() {
foreach (var name in new List<string> {
"NameDescending", "NameAscending", "PriceDescending", "PriceAscending", "EndingDescending", "EndingAscending",
}) {
var radio = (RadioMenuItem) builder.GetObject($"Sort{name}");
if (radio.Active) {
return name;
}
}
return "NameAscending";
}
/// <summary>
/// clears the treeview's liststore and adds everything in the watchlist to it, obeying sort order. tries to
/// reselect the item that the user had selected, if possible.
/// </summary>
private void RenderList() {
string id = null;
if (selectedItem != null) {
id = selectedItem.id;
}
items.Clear();
var values = settings.watchlist.Values;
var type = GetSortType();
var sorted = type switch {
"NameDescending" => values.OrderByDescending(item => item.name),
"NameAscending" => values.OrderBy(item => item.name),
"PriceDescending" => values.OrderByDescending(item => item.Price),
"PriceAscending" => values.OrderBy(item => item.Price),
"EndingDescending" => values.OrderByDescending(item => item.endDate),
_ => values.OrderBy(item => item.endDate),
};
if (settings.showFavouritesAtTopOfList) {
foreach (var item in sorted.Where(item => item.favourite)) {
items.AppendValues(item);
}
foreach (var item in sorted.Where(item => !item.favourite)) {
items.AppendValues(item);
}
} else {
foreach (var item in sorted) {
items.AppendValues(item);
}
}
((TreeModelFilter) itemTreeView.Model).Refilter();
if (id == null) {
return;
}
// attempt to reselect the item we were just looking at
// ReSharper disable once UseDeconstruction
var pathAndIter = GetRow(id);
if (pathAndIter.path != null) {
itemTreeView.Selection.SelectPath(pathAndIter.path);
}
}
// event handlers
private void ButtonAddClicked(object sender, EventArgs a) {
// Console.WriteLine("ButtonAddClicked");
var aid = new AddItemDialogue {Title = "Buypeeb"};
aid.Run();
var url = aid.entryURL.Text;
var name = aid.entryName.Text;
aid.Dispose();
// vry simpl url validation for simpol creachers
// TODO: better. do better.
var rx = new Regex(@"^http.+yahoo.+");
if (rx.IsMatch(url)) {
UpdateItem(settings.Watch(url, name).id, true);
RenderList();
} else {
var md = MsgBox($"\"{url}\" is not a valid Buyee or Yahoo! Auctions Japan URL.", ButtonsType.Ok);
md.Run();
md.Dispose();
}
}
private void ButtonUpdateAllClicked(object sender, EventArgs a) {
UpdateItems();
}
private void ButtonClearEndedClicked(object sender, EventArgs a) {
var md = MsgBox("Are you sure you want to remove all ended auctions from the list?");
var r = (ResponseType) md.Run();
md.Dispose();
if (r != ResponseType.Ok) {
return;
}
var removeMe = new List<string>();
foreach (var (key, value) in settings.watchlist) {
if (!value.Available) {
removeMe.Add(key);
}
}
foreach (var id in removeMe) {
settings.watchlist.Remove(id);
}
RenderList();
}
private void ButtonClearAllClicked(object sender, EventArgs a) {
var md = MsgBox("Are you sure you want to clear ALL items?");
if (md.Run() == (int) ResponseType.Ok) {
settings.watchlist.Clear();
RenderList();
}
md.Dispose();
}
private void ButtonOpenClicked(object sender, EventArgs a) {
var od = new FileChooserDialog(
"Open userdata.json",
this,
FileChooserAction.Open,
"Cancel", ResponseType.Cancel, "Open", ResponseType.Accept
);
var odf = new FileFilter {Name = "JSON files"};
odf.AddMimeType("application/json");
odf.AddPattern("*.json");
od.AddFilter(odf);
if (od.Run() == (int) ResponseType.Accept) {
try {
var j = File.ReadAllText(od.Filename);
settings = JsonSerializer.Deserialize<Settings>(j);
RenderList();
UpdateItems();
} catch (Exception e) {
Console.WriteLine(e);
var md = MsgBox($"Failed to load {od.Filename}!\n{e.Message}", ButtonsType.Ok);
md.Run();
md.Dispose();
}
}
od.Dispose();
}
private void ButtonSaveClicked(object sender, EventArgs a) {
SaveSettings();
}
private void ButtonSaveAsClicked(object sender, EventArgs a) {
var sd = new FileChooserDialog(
"Save userdata.json",
this,
FileChooserAction.Save,
"Cancel", ResponseType.Cancel, "Save", ResponseType.Accept
) {CurrentName = "userdata.json"};
var sdf = new FileFilter {Name = "JSON files"};
sdf.AddMimeType("application/json");
sdf.AddPattern("*.json");
sd.AddFilter(sdf);
if (sd.Run() == (int) ResponseType.Accept) {
try {
if (!File.Exists(sd.Filename)) {
using var fs = File.CreateText(sd.Filename);
fs.Write(JsonSerializer.Serialize(settings, jsonOptions));
}
} catch (Exception e) {
Console.WriteLine(e);
var md = MsgBox($"Failed to write {sd.Filename}!\n{e.Message}.", ButtonsType.Ok);
md.Run();
md.Dispose();
}
}
sd.Dispose();
}
private void ButtonExportClicked(object sender, EventArgs a) {
var readyQuery = from item in settings.watchlist.Values.ToList() where !item.Ready select item;
if (readyQuery.Count() != 0) {
var md = MsgBox("Please wait for all items to update before exporting a CSV.", ButtonsType.Ok);
md.Run();
md.Dispose();
return;
}
var sd = new FileChooserDialog(
"Export watchlist as CSV",
this,
FileChooserAction.Save,
"Cancel", ResponseType.Cancel, "Save", ResponseType.Accept
) {CurrentName = "buypeeb.csv"};
var sdf = new FileFilter {Name = "CSV files"};
sdf.AddMimeType("text/csv");
sdf.AddPattern("*.csv");
sd.AddFilter(sdf);
if (sd.Run() == (int) ResponseType.Accept) {
try {
using var writer = new StreamWriter(sd.Filename);
using var csv = new CsvWriter(writer, CultureInfo.InvariantCulture);
csv.WriteRecords(settings.watchlist);
} catch (Exception e) {
Console.WriteLine(e);
var md = MsgBox($"Failed to write {sd.Filename}!\n{e.Message}.", ButtonsType.Ok);
md.Run();
md.Dispose();
}
}
sd.Dispose();
}
private void ButtonHelpClicked(object sender, EventArgs args) {
Console.WriteLine("Watchlist:");
foreach (var item in settings.watchlist) {
Console.WriteLine(item);
}
Console.WriteLine("---\nFilter results:");
foreach (var item in filterQuery) {
Console.WriteLine(item);
}
Console.WriteLine("---\nListstore contents:");
foreach (object[] item in items) {
Console.WriteLine(item[0]);
}
}
private void ButtonQuitClicked(object sender, EventArgs a) {
var md = MsgBox("Are you sure you want to quit?");
var response = (ResponseType) md.Run();
md.Dispose();
if (response == ResponseType.Ok) {
SaveSettings();
Application.Quit();
}
}
private void ButtonSettingsClicked(object sender, EventArgs args) {
var win = new SettingsWindow(settings);
Application.AddWindow(win);
win.DeleteEvent += WindowSettingsClosed;
win.Show();
}
private void TreeViewItemsSelectionChanged(object sender, EventArgs a) {
UpdateSelectionView();
}
private void ButtonViewBuyeeClicked(object sender, EventArgs a) {
OpenUrl(selectedItem.buyeeUrl);
}
private void ButtonViewYahooClicked(object sender, EventArgs a) {
OpenUrl(selectedItem.url);
}
private void ButtonSelectedRemoveClicked(object sender, EventArgs a) {
var item = selectedItem;
var md = MsgBox($"Are you sure you want to remove the item \"{item.name}\"?");
var response = (ResponseType) md.Run();
md.Dispose();
if (response == ResponseType.Ok) {
settings.watchlist.Remove(item.id);
RenderList();
}
}
private void ButtonSelectedRenameClicked(object sender, EventArgs a) {
var item = selectedItem;
var (accepted, response) = EntryDialogue(
"Rename item", $"Enter a new name for the item \"{item.name}\".", item.name
);
if (accepted) {
item.name = response;
UpdateSelectionView();
}
}
private void ButtonSelectedUpdateClicked(object sender, EventArgs args) {
selectionViewBox.Sensitive = false;
if (updateQueue.Contains(selectedItem.id)) {
// the item is already waiting to be updated
return;
}
UpdateItem(selectedItem.id);
}
private void ButtonSelectedFavouriteToggled(object sender, EventArgs args) {
var s = (ToggleButton) sender;
selectedItem.favourite = s.Active;
if (settings.showFavouritesAtTopOfList) {
RenderList();
} else {
// i don't know why this is necessary
var pathAndIter = GetRow(selectedItem.id);
if (pathAndIter.path != null) {
items.EmitRowChanged(pathAndIter.path, pathAndIter.iter);
}
}
}
private void ButtonSelectedNotesClearClicked(object sender, EventArgs args) {
var item = selectedItem;
var md = MsgBox($"Are you sure you want to clear the notes for \"{item.name}\"?");
if (md.Run() == (int) ResponseType.Ok) {
var noteBuffer = (TextBuffer) builder.GetObject("TextBufferSelectedNotes");
noteBuffer.Clear();
selectedItem.notes = null;
}
md.Dispose();
}
private void TextViewSelectedNotesFocusOut(object sender, FocusOutEventArgs args) {
// the "save" button does nothing, however, when you click the save button, you transfer focus to it, firing this
// event!
// how very sneaky
var noteBuffer = (TextBuffer) builder.GetObject("TextBufferSelectedNotes");
if (selectedItem != null) {
selectedItem.notes = string.IsNullOrWhiteSpace(noteBuffer.Text) ? null : noteBuffer.Text;
}
}
private void SortMenuClosed(object sender, EventArgs args) {
RenderList();
}
private void WindowSettingsClosed(object sender, EventArgs args) {
RenderList();
}
// timers
/// <summary>
/// updates the end time displayed in the selection box. runs every second to update the countdown timer.
/// </summary>
/// <returns>true</returns>
private bool UpdateSelectionEndTime() {
if (!selectionViewBox.IsSensitive) {
return true;
}
var item = selectedItem;
if (!item.Ready) {
return true;
}
var ending = "";
if (item.Available) {
var now = DateTime.Now;
var end = item.endDate.ToLocalTime();
var span = end.Subtract(now);
if (span.Days > 0) {
ending += span.ToString("dd' days, '"); // will format twelve days as "12 days, "
}
// timespan objects don't contain definitions for the time or date separators, so the colons need to be escaped
// `HH` doesn't exist, but `hh` behaves identically
// see https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-timespan-format-strings
ending += span.ToString(@"hh\:mm\:ss");
} else {
ending = "Auction has ended";
}
endingLabel.Text = ending;
return true;
}
/// <summary>
/// updates all items that need updating. runs every ten seconds.
/// </summary>
/// <returns>true</returns>
private bool AutoUpdateItems() {
if (queueActive) {
// don't autoupdate if the queue is active
return true;
}
foreach (var item in outdatedItemQuery) {
updateQueue.Enqueue(item.id);
}
if (updateQueue.TryPeek(out var _)) {
// there's at least one item in the queue
ProcessUpdateQueue();
}
return true;
}
// column renderers
private static void RenderColumnFavourite(
TreeViewColumn column, CellRenderer cell, ITreeModel model, TreeIter iter
) {
var item = (YahooAuctionsItem) model.GetValue(iter, 0);
((CellRendererText) cell).Text = item.favourite ? "♥" : "";
}
private static void RenderColumnName(TreeViewColumn column, CellRenderer cell, ITreeModel model, TreeIter iter) {
var item = (YahooAuctionsItem) model.GetValue(iter, 0);
((CellRendererText) cell).Text = item.name ?? "Loading...";
}
private static void RenderColumnPriceYen(
TreeViewColumn column, CellRenderer cell, ITreeModel model, TreeIter iter
) {
var item = (YahooAuctionsItem) model.GetValue(iter, 0);
((CellRendererText) cell).Text = item.Ready ? item.priceJpy : "...";
}
private static void RenderColumnPriceAUD(
TreeViewColumn column, CellRenderer cell, ITreeModel model, TreeIter iter
) {
var item = (YahooAuctionsItem) model.GetValue(iter, 0);
((CellRendererText) cell).Text = item.Ready ? item.priceAud : "...";
}
private void RenderColumnEnding(TreeViewColumn column, CellRenderer cell, ITreeModel model, TreeIter iter) {
var item = (YahooAuctionsItem) model.GetValue(iter, 0);
var ending = "";
if (item.Ready) {
if (!item.Available) {
ending = "Ended";
} else {
var now = DateTime.Now;
var end = item.endDate.ToLocalTime();
// TODO: should we show the year if the auction ends next year? 0uo
if (end.DayOfYear != now.DayOfYear) {
// the auction isn't ending today, so we should show the day it's ending on for clarity
ending += end.ToString("MMM d ");
}
ending += end.ToString("HH:mm");
if (settings.showSecondsInListView) {
// add the seconds on to the end
ending += end.ToString(":ss");
}
}
}
((CellRendererText) cell).Text = item.Ready ? ending : "...";
}
// tree filter
private bool ItemFilter(ITreeModel model, TreeIter iter) {
var item = (YahooAuctionsItem) model.GetValue(iter, 0);
if (item == null) {
return true;
}
if (item.Price == 0) {
// the item has only just been added, so we can't compare it against the filters, since it's missing most of its data
return true;
}
bool Filtered(string name) {
return filterChecks[name].Active;
}
// first, check to see if any filters are set that would exclude everything, such as hiding both active and ended auctions
// if so, there's no need to run the more expensive linq query
if (
Filtered("Favourites") && Filtered("NonFavourites") ||
Filtered("Active") && Filtered("Ended") ||
Filtered("EndingToday") && Filtered("EndingAfterToday") ||
Filtered("WithWinPrice") && Filtered("WithNoWinPrice")
) {
return false;
}
return filterQuery.Contains(item);
}
private void RunFilter(object sender, EventArgs a) {
((TreeModelFilter) itemTreeView.Model).Refilter();
}
}
}