buypeeb-cs/MainWindow.cs

977 lines
31 KiB
C#
Executable File

/*
buypeeb - a program to track yahoo jp auctions of peebus.
Copyright (C) 2020 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.IO;
using System.Text.RegularExpressions;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using System.Net;
using System.Linq;
using System.Diagnostics;
using CsvHelper;
using Gtk;
namespace Buypeeb {
class MainWindow : Window {
private string location;
private JsonSerializerOptions jsonOptions;
private ListStore items;
private Settings settings;
private TreeView itemTreeView;
private Builder builder;
// TODO: whenever we get something from the builder, cache it for later
// that way we don't need to constantly do "builder.GetObject"s
// when that is done, you can use the cache array to replace everything from here...
private Box selectionViewBox;
private Label endingLabel;
private bool queueActive;
private SearchEntry searchEntry;
private Dictionary<string, CheckButton> filterChecks = new Dictionary<string, CheckButton>();
// ...to here.
static SemaphoreSlim taskLimit = new SemaphoreSlim(6);
private Queue<string> updateQueue = new Queue<string>();
private IEnumerable<YahooAuctionsItem> filterQuery {
get {
// father forgive me for i have lynned
return
from item in this.settings.watchlist.Values.ToList()
where (item.favourite != this.filterChecks["Favourites"].Active ||
item.favourite == this.filterChecks["NonFavourites"].Active) &&
(item.available != this.filterChecks["Active"].Active ||
item.available == this.filterChecks["Ended"].Active) &&
(item.endingToday != this.filterChecks["EndingToday"].Active ||
item.endingToday == this.filterChecks["EndingAfterToday"].Active) &&
(item.hasWinPrice != this.filterChecks["WithWinPrice"].Active ||
item.hasWinPrice == this.filterChecks["WithNoWinPrice"].Active) &&
(String.IsNullOrWhiteSpace(this.searchEntry.Text) ||
item.name.ToLower().Contains(this.searchEntry.Text.ToLower()) ||
item.originalName.ToLower().Contains(this.searchEntry.Text.ToLower()))
select item;
}
}
private IEnumerable<YahooAuctionsItem> outdatedItemQuery {
get {
// 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
return
from item in this.settings.watchlist.Values.ToList()
where item.ready && this.settings.ItemNotUpdatedSinceInterval(item)
select item;
}
}
private YahooAuctionsItem selectedItem {
get {
if (this.itemTreeView.Selection.CountSelectedRows() == 0) {
// avoids incurring the wrath of Gtk-CRITICAL **
return null;
}
this.itemTreeView.Selection.GetSelected(out TreeIter iter);
return (YahooAuctionsItem)this.itemTreeView.Model.GetValue(iter, 0);
}
}
public MainWindow() : this(new Builder("main.glade")) { }
private MainWindow(Builder builder) : base(builder.GetObject("wndMain").Handle) {
this.jsonOptions = new JsonSerializerOptions {
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.Create(System.Text.Unicode.UnicodeRanges.All)
};
if (Environment.OSVersion.Platform == PlatformID.Win32NT) {
// C:\Users\Beebus\AppData\Roaming\Lynnear Software\buypeeb
this.location = System.IO.Path.Combine(Environment.ExpandEnvironmentVariables("%APPDATA%"), "Lynnear Software", "buypeeb");
}
else {
// ~/.config/Lynnear Software/buypeeb
this.location = System.IO.Path.Combine(Environment.GetEnvironmentVariable("HOME"), ".config", "Lynnear Software", "buypeeb");
}
string userdata = System.IO.Path.Combine(location, "userdata.json");
if (File.Exists(userdata)) {
try {
string j = File.ReadAllText(userdata);
this.settings = JsonSerializer.Deserialize<Settings>(j);
}
catch {
// ???
Console.WriteLine("oops");
Application.Quit();
}
}
else {
this.settings = new Settings();
}
this.SaveSettings();
this.Title = "Buypeeb";
this.builder = builder;
builder.Autoconnect(this);
var menuButtonFilter = (MenuButton)builder.GetObject("MenuButtonFilter");
var menuButtonSort = (MenuButton)builder.GetObject("MenuButtonSort");
menuButtonFilter.Child = (Image)builder.GetObject("ImageFilter");
menuButtonSort.Child = (Image)builder.GetObject("ImageSort");
this.selectionViewBox = (Box)builder.GetObject("SelectionViewBox");
this.endingLabel = (Label)builder.GetObject("LabelSelectedEnding");
this.searchEntry = (SearchEntry)builder.GetObject("FilterSearchEntry");
foreach (var name in new List<string> { "Favourites", "NonFavourites", "Active", "Ended", "EndingToday", "EndingAfterToday", "WithWinPrice", "WithNoWinPrice" }) {
this.filterChecks.Add(name, (CheckButton)builder.GetObject($"CheckButtonFilter{name}"));
this.filterChecks[name].Active = false;
}
// bind treeview columns to watchlist instead of needing to manually sync its liststore
this.itemTreeView = (TreeView)builder.GetObject("TreeViewItems");
this.items = new ListStore(typeof(YahooAuctionsItem));
var filteredItems = new TreeModelFilter(this.items, null);
filteredItems.VisibleFunc = this.ItemFilter;
this.itemTreeView.Model = filteredItems;
TreeCellDataFunc[] funcs = {
new TreeCellDataFunc(this.RenderColumnFavourite),
new TreeCellDataFunc(this.RenderColumnName),
new TreeCellDataFunc(this.RenderColumnPriceYen),
new TreeCellDataFunc(this.RenderColumnPriceAUD),
new TreeCellDataFunc(this.RenderColumnEnding)
};
for (int i = 0; i < this.itemTreeView.Columns.Length; i++) {
var c = this.itemTreeView.Columns[i];
c.SetCellDataFunc(c.Cells[0], funcs[i]);
}
this.RenderList();
this.UpdateItems();
GLib.Timeout.Add(1000, new GLib.TimeoutHandler(UpdateSelectionEndTime));
GLib.Timeout.Add(10000, new GLib.TimeoutHandler(AutoUpdateItems));
DeleteEvent += WindowShutdown;
}
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
TreeIter iter;
this.itemTreeView.Model.GetIterFirst(out iter);
var m = (TreeModelFilter)this.itemTreeView.Model;
for (int i = 0; i < this.itemTreeView.Model.IterNChildren(); i++) {
var x = (YahooAuctionsItem)this.itemTreeView.Model.GetValue(iter, 0);
if (x.id == id) {
return (m.ConvertPathToChildPath(m.GetPath(iter)), m.ConvertIterToChildIter(iter));
}
else {
this.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() {
string j = JsonSerializer.Serialize(this.settings, this.jsonOptions);
string p = System.IO.Path.Combine(this.location, "userdata.json");
Console.WriteLine(j);
if (!Directory.Exists(this.location)) {
Directory.CreateDirectory(this.location);
}
using (StreamWriter 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 void UpdateThread(string id) {
var item = this.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's currently stored
item.ready = false;
Gtk.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 pathAndIter = this.GetRow(id);
if (pathAndIter.path != null) {
this.items.EmitRowChanged(pathAndIter.path, pathAndIter.iter);
}
});
using (WebClient client = new WebClient()) {
// TODO: download should have timeout
item.Update(client.DownloadString(item.url));
// Thread.Sleep(5000);
// item.Update(File.ReadAllText("yahoo.html"));
}
Gtk.Application.Invoke(delegate {
var pathAndIter = this.GetRow(id);
if (pathAndIter.path != null) {
this.items.EmitRowChanged(pathAndIter.path, pathAndIter.iter);
}
if (item == this.selectedItem) {
// if the user has this item selected and it just became ready, enable the selection box and redraw the info
this.selectionViewBox.Sensitive = true;
this.UpdateSelectionView();
}
});
item.ready = true;
// Console.WriteLine($"{id} updated.");
}
/// <summary>
/// recursively processes the update queue. this is a blocking function.
/// </summary>
private void ProcessUpdateQueue() {
this.queueActive = true;
this.UpdateItem(this.updateQueue.Dequeue());
if (this.updateQueue.TryPeek(out string _)) {
this.ProcessUpdateQueue();
}
else {
this.queueActive = false;
}
}
/// <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) {
var item = this.settings.watchlist[id];
if (item.updatedRecently) {
// the item has been updated recently, and force is not true
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();
var t = Task.Factory.StartNew(() => {
this.UpdateThread(id);
}).ContinueWith(task => {
taskLimit.Release();
if (renderListWhenDone) {
Gtk.Application.Invoke(delegate {
this.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 (this.queueActive) {
return;
}
this.selectionViewBox.Sensitive = false;
foreach (var item in this.settings.watchlist) {
if (!this.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;
this.updateQueue.Enqueue(item.Key);
}
}
if (!this.updateQueue.TryPeek(out string _)) {
// queue is empty
return;
}
this.itemTreeView.QueueDraw();
var t = Task.Factory.StartNew(() => {
this.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 = this.selectedItem;
var infobox = (Box)this.builder.GetObject("SelectionInfoBox");
if (item == null) {
this.selectionViewBox.Sensitive = false;
infobox.Visible = false;
(this.builder.GetObject("LabelSelectedName") as Label).Text = "buypeeb";
return;
}
this.selectionViewBox.Sensitive = item.ready;
infobox.Visible = true;
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", $"Last updated: {(item.ready ? item.lastUpdated.ToString("MMM dd, HH:mm:ss") : "Right now!")}" }
};
foreach (var row in info) {
(this.builder.GetObject($"LabelSelected{row.Key}") as Label).Text = row.Value;
}
this.UpdateSelectionEndTime();
var noteBuffer = (TextBuffer)this.builder.GetObject("TextBufferSelectedNotes");
noteBuffer.Clear();
if (!String.IsNullOrWhiteSpace(item.notes)) {
noteBuffer.Text = item.notes;
}
(this.builder.GetObject("ButtonSelectedFavourite") as ToggleButton).Active = item.favourite;
}
/// <summary>
/// opens a URL in the user's browser.
/// </summary>
/// <param name="url">the url to open</param>
private void OpenUrl(string url) {
// https://github.com/dotnet/runtime/issues/17938
if (Environment.OSVersion.Platform == PlatformID.Win32NT) {
ProcessStartInfo psi = new ProcessStartInfo {
FileName = url,
UseShellExecute = true
};
Process.Start(psi);
}
else {
// let's hope you have xdg-open installed
Process.Start("xdg-open", url);
}
}
/// <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(
parent_window: this,
flags: DialogFlags.DestroyWithParent | DialogFlags.Modal,
type: MessageType.Question,
bt: buttonsType,
format: message
);
md.KeepAbove = true;
md.Resizable = false;
md.FocusOnMap = true;
md.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 (Boolean accepted, string response) EntryDialogue(string title = "Buypeeb", string message = "Hi there!", string prefill = null) {
Dialog ed = new Dialog(
title: title,
parent: this,
flags: DialogFlags.DestroyWithParent | DialogFlags.Modal,
/* button_data: */ "Cancel", ResponseType.Cancel, "OK", ResponseType.Ok
);
ed.DefaultResponse = ResponseType.Ok;
ed.KeepAbove = true;
Label edLabel = new Label(message);
Entry 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;
ResponseType accepted = (ResponseType)ed.Run();
string 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)this.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 (this.selectedItem != null) {
id = this.selectedItem.id;
}
this.items.Clear();
var values = this.settings.watchlist.Values;
IOrderedEnumerable<YahooAuctionsItem> sorted;
var type = this.GetSortType();
if (type == "NameDescending") {
sorted = values.OrderByDescending(item => item.name);
}
else if (type == "NameAscending") {
sorted = values.OrderBy(item => item.name);
}
else if (type == "PriceDescending") {
sorted = values.OrderByDescending(item => item.price);
}
else if (type == "PriceAscending") {
sorted = values.OrderBy(item => item.price);
}
else if (type == "EndingDescending") {
sorted = values.OrderByDescending(item => item.endDate);
}
else {
sorted = 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);
}
}
(this.itemTreeView.Model as TreeModelFilter).Refilter();
if (id != null) {
// attempt to reselect the item we were just looking at
var pathAndIter = this.GetRow(id);
if (pathAndIter.path != null) {
this.itemTreeView.Selection.SelectPath(pathAndIter.path);
}
}
}
// event handlers
private void ButtonAddClicked(object sender, EventArgs a) {
// Console.WriteLine("ButtonAddClicked");
AddItemDialogue aid = new AddItemDialogue();
aid.Title = "Buypeeb";
ResponseType accepted = (ResponseType)aid.Run();
string url = aid.GetURL();
string name = aid.GetName();
aid.Dispose();
// vry simpl url validation for simpol creachers
// TODO: better. do better.
Regex rx = new Regex(@"^http.+yahoo.+");
if (rx.IsMatch(url)) {
this.UpdateItem(this.settings.Watch(url, name).id, true);
this.RenderList();
}
else {
var md = this.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) {
this.UpdateItems();
}
private void ButtonClearEndedClicked(object sender, EventArgs a) {
var md = this.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 item in this.settings.watchlist) {
if (!item.Value.available) {
removeMe.Add(item.Key);
}
}
foreach (var id in removeMe) {
this.settings.watchlist.Remove(id);
}
this.RenderList();
}
private void ButtonClearAllClicked(object sender, EventArgs a) {
var md = this.MsgBox("Are you sure you want to clear ALL items?");
if (md.Run() == (int)ResponseType.Ok) {
this.settings.watchlist.Clear();
this.RenderList();
}
md.Dispose();
}
private void ButtonOpenClicked(object sender, EventArgs a) {
var od = new FileChooserDialog(
title: "Open userdata.json",
parent: this,
action: FileChooserAction.Open,
"Cancel", ResponseType.Cancel, "Open", ResponseType.Accept
);
var odf = new FileFilter();
odf.Name = "JSON files";
odf.AddMimeType("application/json");
odf.AddPattern("*.json");
od.AddFilter(odf);
if (od.Run() == (int)ResponseType.Accept) {
try {
string j = File.ReadAllText(od.Filename);
this.settings = JsonSerializer.Deserialize<Settings>(j);
this.RenderList();
this.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) {
this.SaveSettings();
}
private void ButtonSaveAsClicked(object sender, EventArgs a) {
var sd = new FileChooserDialog(
title: "Save userdata.json",
parent: this,
action: FileChooserAction.Save,
"Cancel", ResponseType.Cancel, "Save", ResponseType.Accept
);
sd.CurrentName = "userdata.json";
var sdf = new FileFilter();
sdf.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 (StreamWriter fs = File.CreateText(sd.Filename)) {
fs.Write(JsonSerializer.Serialize(this.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 this.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(
title: "Export watchlist as CSV",
parent: this,
action: FileChooserAction.Save,
"Cancel", ResponseType.Cancel, "Save", ResponseType.Accept
);
sd.CurrentName = "buypeeb.csv";
var sdf = new FileFilter();
sdf.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, System.Globalization.CultureInfo.InvariantCulture)) {
csv.WriteRecords(this.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 this.settings.watchlist) {
Console.WriteLine(item);
}
Console.WriteLine("---\nFilter results:");
foreach (var item in this.filterQuery) {
Console.WriteLine(item);
}
Console.WriteLine("---\nListstore contents:");
foreach (object[] item in this.items) {
Console.WriteLine(item[0]);
}
}
private void ButtonQuitClicked(object sender, EventArgs a) {
var md = this.MsgBox("Are you sure you want to quit?");
ResponseType response = (ResponseType)md.Run();
md.Dispose();
if (response == ResponseType.Ok) {
this.SaveSettings();
Application.Quit();
}
}
private void ButtonSettingsClicked(object sender, EventArgs args) {
var win = new SettingsWindow(this.settings);
Application.AddWindow(win);
win.DeleteEvent += this.WindowSettingsClosed;
win.Show();
}
private void TreeViewItemsSelectionChanged(object sender, EventArgs a) {
this.UpdateSelectionView();
}
private void ButtonViewBuyeeClicked(object sender, EventArgs a) {
this.OpenUrl(this.selectedItem.buyeeUrl);
}
private void ButtonViewYahooClicked(object sender, EventArgs a) {
this.OpenUrl(this.selectedItem.url);
}
private void ButtonSelectedRemoveClicked(object sender, EventArgs a) {
var item = this.selectedItem;
var md = this.MsgBox($"Are you sure you want to remove the item \"{item.name}\"?"); // TODO: this looks bad being all on one line
ResponseType response = (ResponseType)md.Run();
md.Dispose();
if (response == ResponseType.Ok) {
this.settings.watchlist.Remove(item.id);
this.RenderList();
}
}
private void ButtonSelectedRenameClicked(object sender, EventArgs a) {
var item = this.selectedItem;
(bool accepted, string response) = this.EntryDialogue("Rename item", $"Enter a new name for the item \"{item.name}\".", item.name);
if (accepted) {
item.name = response;
this.UpdateSelectionView();
}
}
private void ButtonSelectedUpdateClicked(object sender, EventArgs args) {
this.selectionViewBox.Sensitive = false;
if (this.updateQueue.Contains(this.selectedItem.id)) {
// the item is already waiting to be updated
return;
}
this.UpdateItem(this.selectedItem.id);
}
private void ButtonSelectedFavouriteToggled(object sender, EventArgs args) {
ToggleButton s = (ToggleButton)sender;
this.selectedItem.favourite = s.Active;
if (this.settings.showFavouritesAtTopOfList) {
var id = this.selectedItem.id;
this.RenderList();
}
else {
// i don't know why this is necessary
var pathAndIter = this.GetRow(this.selectedItem.id);
if (pathAndIter.path != null) {
this.items.EmitRowChanged(pathAndIter.path, pathAndIter.iter);
}
}
}
private void ButtonSelectedNotesClearClicked(object sender, EventArgs args) {
var item = this.selectedItem;
var md = this.MsgBox($"Are you sure you want to clear the notes for \"{item.name}\"?");
if (md.Run() == (int)ResponseType.Ok) {
var noteBuffer = (TextBuffer)this.builder.GetObject("TextBufferSelectedNotes");
noteBuffer.Clear();
this.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)this.builder.GetObject("TextBufferSelectedNotes");
if (this.selectedItem != null) {
this.selectedItem.notes = String.IsNullOrWhiteSpace(noteBuffer.Text) ? null : noteBuffer.Text;
}
}
private void SortMenuClosed(object sender, EventArgs args) {
this.RenderList();
}
private void WindowSettingsClosed(object sender, EventArgs args) {
this.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 (!this.selectionViewBox.IsSensitive) {
return true;
}
var item = this.selectedItem;
if (!item.ready) {
return true;
}
string 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";
}
this.endingLabel.Text = ending;
return true;
}
/// <summary>
/// updates all items that need updating. runs every ten seconds.
/// </summary>
/// <returns>true</returns>
private bool AutoUpdateItems() {
if (this.queueActive) {
// don't autoupdate if the queue is active
return true;
}
foreach (var item in this.outdatedItemQuery) {
updateQueue.Enqueue(item.id);
}
if (updateQueue.TryPeek(out string _)) {
// there's at least one item in the queue
this.ProcessUpdateQueue();
}
return true;
}
// column renderers
private void RenderColumnFavourite(Gtk.TreeViewColumn column, Gtk.CellRenderer cell, Gtk.ITreeModel model, Gtk.TreeIter iter) {
YahooAuctionsItem item = (YahooAuctionsItem)model.GetValue(iter, 0);
(cell as Gtk.CellRendererText).Text = item.favourite ? "♥" : "";
}
private void RenderColumnName(Gtk.TreeViewColumn column, Gtk.CellRenderer cell, Gtk.ITreeModel model, Gtk.TreeIter iter) {
YahooAuctionsItem item = (YahooAuctionsItem)model.GetValue(iter, 0);
(cell as Gtk.CellRendererText).Text = item.name ?? "Loading...";
}
private void RenderColumnPriceYen(Gtk.TreeViewColumn column, Gtk.CellRenderer cell, Gtk.ITreeModel model, Gtk.TreeIter iter) {
YahooAuctionsItem item = (YahooAuctionsItem)model.GetValue(iter, 0);
(cell as Gtk.CellRendererText).Text = item.ready ? item.priceJPY : "...";
}
private void RenderColumnPriceAUD(Gtk.TreeViewColumn column, Gtk.CellRenderer cell, Gtk.ITreeModel model, Gtk.TreeIter iter) {
YahooAuctionsItem item = (YahooAuctionsItem)model.GetValue(iter, 0);
(cell as Gtk.CellRendererText).Text = item.ready ? item.priceAUD : "...";
}
private void RenderColumnEnding(Gtk.TreeViewColumn column, Gtk.CellRenderer cell, Gtk.ITreeModel model, Gtk.TreeIter iter) {
YahooAuctionsItem item = (YahooAuctionsItem)model.GetValue(iter, 0);
string 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 (this.settings.showSecondsInListView) {
// add the seconds on to the end
ending += end.ToString(":ss");
}
}
}
(cell as Gtk.CellRendererText).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 this.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 this.filterQuery.Contains(item);
}
private void RunFilter(object sender, EventArgs a) {
(this.itemTreeView.Model as TreeModelFilter).Refilter();
}
}
}