code cleanup: name conventions, redundant code...

- use var instead of specific type names where applicable
- get user's home directory from Environment.SpecialFolder.UserProfile instead of reading the HOME environment variable, which isn't always set
- use object initialisers where possible
- remove redundant identifiers (e.g. "Gtk.Application.Invoke" becomes "Application.Invoke"), of which there were MANY
- remove unused local variables
- replace "variable as Class" with "(Class) variable"
- many other miscellaneous improvements

thanks rider 0u0
This commit is contained in:
Lynne Megido 2021-06-15 10:20:46 +10:00
parent cdf4fff8ff
commit b26326baf5
Signed by: lynnesbian
GPG Key ID: F0A184B5213D9F90
8 changed files with 363 additions and 360 deletions

View File

@ -0,0 +1,3 @@
<component name="ProjectDictionaryState">
<dictionary name="lynne" />
</component>

View File

@ -1,32 +1,30 @@
using Gtk; using Gtk;
namespace Buypeeb { namespace Buypeeb {
internal class AddItemDialogue : Dialog {
class AddItemDialogue : Dialog { private readonly Entry entryURL;
private Entry EntryURL; private readonly Entry entryName;
private Entry EntryName;
public AddItemDialogue() : this(new Builder("add.glade")) { } public AddItemDialogue() : this(new Builder("add.glade")) { }
private AddItemDialogue(Builder builder) : base(builder.GetObject("DialogueAdd").Handle) { private AddItemDialogue(Builder builder) : base(builder.GetObject("DialogueAdd").Handle) {
this.Title = "Add item"; Title = "Add item";
builder.Autoconnect(this); builder.Autoconnect(this);
this.EntryURL = (Entry)builder.GetObject("EntryAddURL"); entryURL = (Entry)builder.GetObject("EntryAddURL");
this.EntryName = (Entry)builder.GetObject("EntryAddName"); entryName = (Entry)builder.GetObject("EntryAddName");
DeleteEvent += Window_Shutdown; DeleteEvent += Window_Shutdown;
} }
private void Window_Shutdown(object sender, DeleteEventArgs args) { private static void Window_Shutdown(object sender, DeleteEventArgs args) {
Application.Quit(); Application.Quit();
} }
public string GetURL() { public string GetURL() {
return this.EntryURL.Text; return entryURL.Text;
} }
public string GetName() { public string GetName() {
return this.EntryName.Text; return entryName.Text;
} }
} }
} }

View File

@ -1,15 +1,16 @@
using System; using System;
using Gtk;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using GLib;
using Application = Gtk.Application;
namespace Buypeeb { namespace Buypeeb {
class BuypeebApp { internal class BuypeebApp {
[STAThread] [STAThread]
public static void Main(string[] args) { public static void Main(string[] args) {
Application.Init(); Application.Init();
var app = new Application("space.lynnesbian.Buypeeb", GLib.ApplicationFlags.None); var app = new Application("space.lynnesbian.Buypeeb", ApplicationFlags.None);
app.Register(GLib.Cancellable.Current); app.Register(Cancellable.Current);
var win = new MainWindow(); var win = new MainWindow();
app.AddWindow(win); app.AddWindow(win);
@ -18,13 +19,16 @@ namespace Buypeeb {
Application.Run(); Application.Run();
} }
// ReSharper disable once InconsistentNaming
// i prefer "IDFromURL" to "IdFromUrl". also i will never forgive microsoft for the name of XmlHTTPRequest and you
// may consider this an act of spite/revenge
public static string IDFromURL(string url) { public static string IDFromURL(string url) {
// TODO: handle invalid URLs better, or at all really // TODO: handle invalid URLs better, or at all really
Regex rx = new Regex(@"^([^?#]+)"); var rx = new Regex(@"^([^?#]+)");
url = rx.Match(url).Groups[1].Value; // remove query params (if any) url = rx.Match(url).Groups[1].Value; // remove query params (if any)
rx = new Regex(@".+\/(.+?)/?$"); rx = new Regex(@".+\/(.+?)/?$");
string id = rx.Match(url).Groups[1].Value; // extracts "k12345" from "https://buypeeb.biz/whatever/k12345/" var id = rx.Match(url).Groups[1].Value; // extracts "k12345" from "https://buypeeb.biz/whatever/k12345/"
return id; return id;
} }

6
Folder.DotSettings Normal file
View File

@ -0,0 +1,6 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=URL/@EntryIndexedValue">URL</s:String>
<s:Boolean x:Key="/Default/UserDictionary/Words/=autosave/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateInstanceFields/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateStaticFields/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;</s:String>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Buypeeb/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

View File

@ -18,19 +18,27 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO; using System.IO;
using System.Text.RegularExpressions; using System.Linq;
using System.Net;
using System.Text.Encodings.Web;
using System.Text.Json; using System.Text.Json;
using System.Text.RegularExpressions;
using System.Text.Unicode;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Net;
using System.Linq;
using System.Diagnostics;
using CsvHelper; using CsvHelper;
using Gtk; using Gtk;
using Timeout = GLib.Timeout;
// ReSharper disable UnusedParameter.Local
namespace Buypeeb { namespace Buypeeb {
class MainWindow : Window { [SuppressMessage("ReSharper", "UnusedMember.Local")]
internal class MainWindow : Window {
private string location; private string location;
private JsonSerializerOptions jsonOptions; private JsonSerializerOptions jsonOptions;
@ -52,24 +60,24 @@ namespace Buypeeb {
// ...to here. // ...to here.
static SemaphoreSlim taskLimit = new SemaphoreSlim(6); private static SemaphoreSlim taskLimit = new SemaphoreSlim(6);
private Queue<string> updateQueue = new Queue<string>(); private Queue<string> updateQueue = new Queue<string>();
private IEnumerable<YahooAuctionsItem> filterQuery { private IEnumerable<YahooAuctionsItem> filterQuery {
get { get {
// father forgive me for i have lynned // father forgive me for i have lynned
return return
from item in this.settings.watchlist.Values.ToList() from item in settings.watchlist.Values.ToList()
where (item.favourite != this.filterChecks["Favourites"].Active || where (item.favourite != filterChecks["Favourites"].Active ||
item.favourite == this.filterChecks["NonFavourites"].Active) && item.favourite == filterChecks["NonFavourites"].Active) &&
(item.available != this.filterChecks["Active"].Active || (item.Available != filterChecks["Active"].Active ||
item.available == this.filterChecks["Ended"].Active) && item.Available == filterChecks["Ended"].Active) &&
(item.endingToday != this.filterChecks["EndingToday"].Active || (item.endingToday != filterChecks["EndingToday"].Active ||
item.endingToday == this.filterChecks["EndingAfterToday"].Active) && item.endingToday == filterChecks["EndingAfterToday"].Active) &&
(item.hasWinPrice != this.filterChecks["WithWinPrice"].Active || (item.hasWinPrice != filterChecks["WithWinPrice"].Active ||
item.hasWinPrice == this.filterChecks["WithNoWinPrice"].Active) && item.hasWinPrice == filterChecks["WithNoWinPrice"].Active) &&
(String.IsNullOrWhiteSpace(this.searchEntry.Text) || (String.IsNullOrWhiteSpace(searchEntry.Text) ||
item.name.ToLower().Contains(this.searchEntry.Text.ToLower()) || item.name.ToLower().Contains(searchEntry.Text.ToLower()) ||
item.originalName.ToLower().Contains(this.searchEntry.Text.ToLower())) item.originalName.ToLower().Contains(searchEntry.Text.ToLower()))
select item; select item;
} }
} }
@ -79,45 +87,45 @@ namespace Buypeeb {
// - marked as "ready", as in, they aren't in the process of updating // - marked as "ready", as in, they aren't in the process of updating
// - not updated since the interval // - not updated since the interval
return return
from item in this.settings.watchlist.Values.ToList() from item in settings.watchlist.Values.ToList()
where item.ready && this.settings.ItemNotUpdatedSinceInterval(item) where item.Ready && settings.ItemNotUpdatedSinceInterval(item)
select item; select item;
} }
} }
private YahooAuctionsItem selectedItem { private YahooAuctionsItem selectedItem {
get { get {
if (this.itemTreeView.Selection.CountSelectedRows() == 0) { if (itemTreeView.Selection.CountSelectedRows() == 0) {
// avoids incurring the wrath of Gtk-CRITICAL ** // avoids incurring the wrath of Gtk-CRITICAL **
return null; return null;
} }
this.itemTreeView.Selection.GetSelected(out TreeIter iter); itemTreeView.Selection.GetSelected(out var iter);
return (YahooAuctionsItem)this.itemTreeView.Model.GetValue(iter, 0); return (YahooAuctionsItem)itemTreeView.Model.GetValue(iter, 0);
} }
} }
public MainWindow() : this(new Builder("main.glade")) { } public MainWindow() : this(new Builder("main.glade")) { }
private MainWindow(Builder builder) : base(builder.GetObject("wndMain").Handle) { private MainWindow(Builder builder) : base(builder.GetObject("wndMain").Handle) {
this.jsonOptions = new JsonSerializerOptions { jsonOptions = new JsonSerializerOptions {
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.Create(System.Text.Unicode.UnicodeRanges.All) Encoder = JavaScriptEncoder.Create(UnicodeRanges.All),
}; };
if (Environment.OSVersion.Platform == PlatformID.Win32NT) { if (Environment.OSVersion.Platform == PlatformID.Win32NT) {
// C:\Users\Beebus\AppData\Roaming\Lynnear Software\buypeeb // C:\Users\Beebus\AppData\Roaming\Lynnear Software\buypeeb
this.location = System.IO.Path.Combine(Environment.ExpandEnvironmentVariables("%APPDATA%"), "Lynnear Software", "buypeeb"); location = System.IO.Path.Combine(Environment.ExpandEnvironmentVariables("%APPDATA%"), "Lynnear Software", "buypeeb");
} }
else { else {
// ~/.config/Lynnear Software/buypeeb // ~/.config/Lynnear Software/buypeeb
this.location = System.IO.Path.Combine(Environment.GetEnvironmentVariable("HOME"), ".config", "Lynnear Software", "buypeeb"); location = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".config", "Lynnear Software", "buypeeb");
} }
string userdata = System.IO.Path.Combine(location, "userdata.json"); var userdata = System.IO.Path.Combine(location, "userdata.json");
if (File.Exists(userdata)) { if (File.Exists(userdata)) {
try { try {
string j = File.ReadAllText(userdata); var j = File.ReadAllText(userdata);
this.settings = JsonSerializer.Deserialize<Settings>(j); settings = JsonSerializer.Deserialize<Settings>(j);
} }
catch { catch {
// ??? // ???
@ -127,10 +135,10 @@ namespace Buypeeb {
} }
else { else {
this.settings = new Settings(); settings = new Settings();
} }
this.SaveSettings(); SaveSettings();
this.Title = "Buypeeb"; Title = "Buypeeb";
this.builder = builder; this.builder = builder;
builder.Autoconnect(this); builder.Autoconnect(this);
@ -140,38 +148,37 @@ namespace Buypeeb {
menuButtonFilter.Child = (Image)builder.GetObject("ImageFilter"); menuButtonFilter.Child = (Image)builder.GetObject("ImageFilter");
menuButtonSort.Child = (Image)builder.GetObject("ImageSort"); menuButtonSort.Child = (Image)builder.GetObject("ImageSort");
this.selectionViewBox = (Box)builder.GetObject("SelectionViewBox"); selectionViewBox = (Box)builder.GetObject("SelectionViewBox");
this.endingLabel = (Label)builder.GetObject("LabelSelectedEnding"); endingLabel = (Label)builder.GetObject("LabelSelectedEnding");
this.searchEntry = (SearchEntry)builder.GetObject("FilterSearchEntry"); searchEntry = (SearchEntry)builder.GetObject("FilterSearchEntry");
foreach (var name in new List<string> { "Favourites", "NonFavourites", "Active", "Ended", "EndingToday", "EndingAfterToday", "WithWinPrice", "WithNoWinPrice" }) { foreach (var name in new List<string> { "Favourites", "NonFavourites", "Active", "Ended", "EndingToday", "EndingAfterToday", "WithWinPrice", "WithNoWinPrice" }) {
this.filterChecks.Add(name, (CheckButton)builder.GetObject($"CheckButtonFilter{name}")); filterChecks.Add(name, (CheckButton)builder.GetObject($"CheckButtonFilter{name}"));
this.filterChecks[name].Active = false; filterChecks[name].Active = false;
} }
// bind treeview columns to watchlist instead of needing to manually sync its liststore // bind treeview columns to watchlist instead of needing to manually sync its liststore
this.itemTreeView = (TreeView)builder.GetObject("TreeViewItems"); itemTreeView = (TreeView)builder.GetObject("TreeViewItems");
this.items = new ListStore(typeof(YahooAuctionsItem)); items = new ListStore(typeof(YahooAuctionsItem));
var filteredItems = new TreeModelFilter(this.items, null); var filteredItems = new TreeModelFilter(items, null) {VisibleFunc = ItemFilter};
filteredItems.VisibleFunc = this.ItemFilter;
this.itemTreeView.Model = filteredItems; itemTreeView.Model = filteredItems;
TreeCellDataFunc[] funcs = { TreeCellDataFunc[] funcs = {
new TreeCellDataFunc(this.RenderColumnFavourite), RenderColumnFavourite,
new TreeCellDataFunc(this.RenderColumnName), RenderColumnName,
new TreeCellDataFunc(this.RenderColumnPriceYen), RenderColumnPriceYen,
new TreeCellDataFunc(this.RenderColumnPriceAUD), RenderColumnPriceAUD,
new TreeCellDataFunc(this.RenderColumnEnding) RenderColumnEnding,
}; };
for (int i = 0; i < this.itemTreeView.Columns.Length; i++) { for (var i = 0; i < itemTreeView.Columns.Length; i++) {
var c = this.itemTreeView.Columns[i]; var c = itemTreeView.Columns[i];
c.SetCellDataFunc(c.Cells[0], funcs[i]); c.SetCellDataFunc(c.Cells[0], funcs[i]);
} }
this.RenderList(); RenderList();
this.UpdateItems(); UpdateItems();
GLib.Timeout.Add(1000, new GLib.TimeoutHandler(UpdateSelectionEndTime)); Timeout.Add(1000, UpdateSelectionEndTime);
GLib.Timeout.Add(10000, new GLib.TimeoutHandler(AutoUpdateItems)); Timeout.Add(10000, AutoUpdateItems);
DeleteEvent += WindowShutdown; DeleteEvent += WindowShutdown;
} }
@ -191,17 +198,16 @@ namespace Buypeeb {
private (TreePath path, TreeIter iter) GetRow(string id) { private (TreePath path, TreeIter iter) GetRow(string id) {
// TODO: surely there's a better way to do this // TODO: surely there's a better way to do this
TreeIter iter; TreeIter iter;
this.itemTreeView.Model.GetIterFirst(out iter); itemTreeView.Model.GetIterFirst(out iter);
var m = (TreeModelFilter)this.itemTreeView.Model; var m = (TreeModelFilter)itemTreeView.Model;
for (int i = 0; i < this.itemTreeView.Model.IterNChildren(); i++) { for (var i = 0; i < itemTreeView.Model.IterNChildren(); i++) {
var x = (YahooAuctionsItem)this.itemTreeView.Model.GetValue(iter, 0); var x = (YahooAuctionsItem)itemTreeView.Model.GetValue(iter, 0);
if (x.id == id) { if (x.id == id) {
return (m.ConvertPathToChildPath(m.GetPath(iter)), m.ConvertIterToChildIter(iter)); return (m.ConvertPathToChildPath(m.GetPath(iter)), m.ConvertIterToChildIter(iter));
} }
else {
this.itemTreeView.Model.IterNext(ref iter); itemTreeView.Model.IterNext(ref iter);
}
} }
Console.WriteLine($"Couldn't find {id}!"); Console.WriteLine($"Couldn't find {id}!");
@ -212,14 +218,14 @@ namespace Buypeeb {
/// saves the settings to userdata.json. /// saves the settings to userdata.json.
/// </summary> /// </summary>
private void SaveSettings() { private void SaveSettings() {
string j = JsonSerializer.Serialize(this.settings, this.jsonOptions); var j = JsonSerializer.Serialize(settings, jsonOptions);
string p = System.IO.Path.Combine(this.location, "userdata.json"); var p = System.IO.Path.Combine(location, "userdata.json");
Console.WriteLine(j); Console.WriteLine(j);
if (!Directory.Exists(this.location)) { if (!Directory.Exists(location)) {
Directory.CreateDirectory(this.location); Directory.CreateDirectory(location);
} }
using (StreamWriter fs = File.CreateText(p)) { using (var fs = File.CreateText(p)) {
fs.Write(j); fs.Write(j);
} }
@ -230,40 +236,40 @@ namespace Buypeeb {
/// </summary> /// </summary>
/// <param name="id">the id of the item to update</param> /// <param name="id">the id of the item to update</param>
private void UpdateThread(string id) { private void UpdateThread(string id) {
var item = this.settings.watchlist[id]; var item = settings.watchlist[id];
// Console.WriteLine($"Updating {id}..."); // Console.WriteLine($"Updating {id}...");
// set item.ready to false to show that it's still being updated // 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 // this changes a few behaviours, such as displaying the price as "..." instead of whatever's currently stored
item.ready = false; item.Ready = false;
Gtk.Application.Invoke(delegate { 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 :/ // 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); var pathAndIter = GetRow(id);
if (pathAndIter.path != null) { if (pathAndIter.path != null) {
this.items.EmitRowChanged(pathAndIter.path, pathAndIter.iter); items.EmitRowChanged(pathAndIter.path, pathAndIter.iter);
} }
}); });
using (WebClient client = new WebClient()) { using (var client = new WebClient()) {
// TODO: download should have timeout // TODO: download should have timeout
item.Update(client.DownloadString(item.url)); item.Update(client.DownloadString(item.url));
// Thread.Sleep(5000); // Thread.Sleep(5000);
// item.Update(File.ReadAllText("yahoo.html")); // item.Update(File.ReadAllText("yahoo.html"));
} }
Gtk.Application.Invoke(delegate { Application.Invoke(delegate {
var pathAndIter = this.GetRow(id); var pathAndIter = GetRow(id);
if (pathAndIter.path != null) { if (pathAndIter.path != null) {
this.items.EmitRowChanged(pathAndIter.path, pathAndIter.iter); items.EmitRowChanged(pathAndIter.path, pathAndIter.iter);
} }
if (item == this.selectedItem) { if (item == selectedItem) {
// if the user has this item selected and it just became ready, enable the selection box and redraw the info // if the user has this item selected and it just became ready, enable the selection box and redraw the info
this.selectionViewBox.Sensitive = true; selectionViewBox.Sensitive = true;
this.UpdateSelectionView(); UpdateSelectionView();
} }
}); });
item.ready = true; item.Ready = true;
// Console.WriteLine($"{id} updated."); // Console.WriteLine($"{id} updated.");
} }
@ -271,13 +277,13 @@ namespace Buypeeb {
/// recursively processes the update queue. this is a blocking function. /// recursively processes the update queue. this is a blocking function.
/// </summary> /// </summary>
private void ProcessUpdateQueue() { private void ProcessUpdateQueue() {
this.queueActive = true; queueActive = true;
this.UpdateItem(this.updateQueue.Dequeue()); UpdateItem(updateQueue.Dequeue());
if (this.updateQueue.TryPeek(out string _)) { if (updateQueue.TryPeek(out var _)) {
this.ProcessUpdateQueue(); ProcessUpdateQueue();
} }
else { else {
this.queueActive = false; queueActive = false;
} }
} }
@ -287,7 +293,7 @@ namespace Buypeeb {
/// <param name="id">the id of the task to update</param> /// <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> /// <param name="renderListWhenDone">whether or not to call this.RenderList() after updating the item</param>
private void UpdateItem(string id, bool renderListWhenDone = false) { private void UpdateItem(string id, bool renderListWhenDone = false) {
var item = this.settings.watchlist[id]; var item = settings.watchlist[id];
if (item.updatedRecently) { if (item.updatedRecently) {
// the item has been updated recently, and force is not true // the item has been updated recently, and force is not true
return; return;
@ -296,13 +302,13 @@ namespace Buypeeb {
// don't start a new task if there are more than `tasklimit` tasks currently running // 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 // this makes sure we don't make 1000 simultaneous requests to yahoo auctions if there are 1000 items on the watchlist
taskLimit.Wait(); taskLimit.Wait();
var t = Task.Factory.StartNew(() => { Task.Factory.StartNew(() => {
this.UpdateThread(id); UpdateThread(id);
}).ContinueWith(task => { }).ContinueWith(task => {
taskLimit.Release(); taskLimit.Release();
if (renderListWhenDone) { if (renderListWhenDone) {
Gtk.Application.Invoke(delegate { Application.Invoke(delegate {
this.RenderList(); RenderList();
}); });
} }
}); });
@ -312,28 +318,28 @@ namespace Buypeeb {
/// add every item in the watchlist to the update queue. if the update queue is already being processed (this.queueActive), this will do nothing. /// 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> /// </summary>
private void UpdateItems() { private void UpdateItems() {
if (this.queueActive) { if (queueActive) {
return; return;
} }
this.selectionViewBox.Sensitive = false; selectionViewBox.Sensitive = false;
foreach (var item in this.settings.watchlist) { foreach (var item in settings.watchlist) {
if (!this.updateQueue.Contains(item.Key)) { if (!updateQueue.Contains(item.Key)) {
// set everything to not ready first // set everything to not ready first
// ensures other actions don't attempt to display the items even if UpdateItem hasn't started yet // ensures other actions don't attempt to display the items even if UpdateItem hasn't started yet
item.Value.ready = false; item.Value.Ready = false;
this.updateQueue.Enqueue(item.Key); updateQueue.Enqueue(item.Key);
} }
} }
if (!this.updateQueue.TryPeek(out string _)) { if (!updateQueue.TryPeek(out var _)) {
// queue is empty // queue is empty
return; return;
} }
this.itemTreeView.QueueDraw(); itemTreeView.QueueDraw();
var t = Task.Factory.StartNew(() => { Task.Factory.StartNew(() => {
this.ProcessUpdateQueue(); ProcessUpdateQueue();
}); });
} }
@ -343,43 +349,43 @@ namespace Buypeeb {
/// </summary> /// </summary>
private void UpdateSelectionView() { private void UpdateSelectionView() {
// get the currently selected item // get the currently selected item
var item = this.selectedItem; var item = selectedItem;
var infobox = (Box)this.builder.GetObject("SelectionInfoBox"); var infobox = (Box)builder.GetObject("SelectionInfoBox");
if (item == null) { if (item == null) {
this.selectionViewBox.Sensitive = false; selectionViewBox.Sensitive = false;
infobox.Visible = false; infobox.Visible = false;
(this.builder.GetObject("LabelSelectedName") as Label).Text = "buypeeb"; ((Label) builder.GetObject("LabelSelectedName")).Text = "buypeeb";
return; return;
} }
this.selectionViewBox.Sensitive = item.ready; selectionViewBox.Sensitive = item.Ready;
infobox.Visible = true; infobox.Visible = true;
var info = new Dictionary<string, string> { var info = new Dictionary<string, string> {
{ "Name", item.name }, { "Name", item.name },
{ "YahooName", item.originalName }, { "YahooName", item.originalName },
{ "Price", item.priceJPY }, { "Price", item.priceJpy },
{ "PriceAUD", item.priceAUD }, { "PriceAUD", item.priceAud },
{ "Ending", "Please wait..." }, { "Ending", "Please wait..." },
{ "Bids", $"{item.bids}" }, { "Bids", $"{item.Bids}" },
{ "BuyItNow", item.winPrice == 0 ? "No" : $"{item.winPriceJPY} ({item.winPriceAUD})" }, { "BuyItNow", item.WinPrice == 0 ? "No" : $"{item.winPriceJpy} ({item.winPriceAud})" },
{ "AutoExtension", item.autoExtension ? "Yes" : "No" }, { "AutoExtension", item.AutoExtension ? "Yes" : "No" },
{ "LastUpdated", $"Last updated: {(item.ready ? item.lastUpdated.ToString("MMM dd, HH:mm:ss") : "Right now!")}" } { "LastUpdated", $"Last updated: {(item.Ready ? item.LastUpdated.ToString("MMM dd, HH:mm:ss") : "Right now!")}" },
}; };
foreach (var row in info) { foreach (var row in info) {
(this.builder.GetObject($"LabelSelected{row.Key}") as Label).Text = row.Value; ((Label) builder.GetObject($"LabelSelected{row.Key}")).Text = row.Value;
} }
this.UpdateSelectionEndTime(); UpdateSelectionEndTime();
var noteBuffer = (TextBuffer)this.builder.GetObject("TextBufferSelectedNotes"); var noteBuffer = (TextBuffer)builder.GetObject("TextBufferSelectedNotes");
noteBuffer.Clear(); noteBuffer.Clear();
if (!String.IsNullOrWhiteSpace(item.notes)) { if (!String.IsNullOrWhiteSpace(item.notes)) {
noteBuffer.Text = item.notes; noteBuffer.Text = item.notes;
} }
(this.builder.GetObject("ButtonSelectedFavourite") as ToggleButton).Active = item.favourite; ((ToggleButton) builder.GetObject("ButtonSelectedFavourite")).Active = item.favourite;
} }
@ -390,9 +396,9 @@ namespace Buypeeb {
private void OpenUrl(string url) { private void OpenUrl(string url) {
// https://github.com/dotnet/runtime/issues/17938 // https://github.com/dotnet/runtime/issues/17938
if (Environment.OSVersion.Platform == PlatformID.Win32NT) { if (Environment.OSVersion.Platform == PlatformID.Win32NT) {
ProcessStartInfo psi = new ProcessStartInfo { var psi = new ProcessStartInfo {
FileName = url, FileName = url,
UseShellExecute = true UseShellExecute = true,
}; };
Process.Start(psi); Process.Start(psi);
} }
@ -432,7 +438,7 @@ namespace Buypeeb {
/// <param name="prefill">a string to prefill the input box with</param> /// <param name="prefill">a string to prefill the input box with</param>
/// <returns></returns> /// <returns></returns>
private (Boolean accepted, string response) EntryDialogue(string title = "Buypeeb", string message = "Hi there!", string prefill = null) { private (Boolean accepted, string response) EntryDialogue(string title = "Buypeeb", string message = "Hi there!", string prefill = null) {
Dialog ed = new Dialog( var ed = new Dialog(
title: title, title: title,
parent: this, parent: this,
flags: DialogFlags.DestroyWithParent | DialogFlags.Modal, flags: DialogFlags.DestroyWithParent | DialogFlags.Modal,
@ -442,8 +448,8 @@ namespace Buypeeb {
ed.DefaultResponse = ResponseType.Ok; ed.DefaultResponse = ResponseType.Ok;
ed.KeepAbove = true; ed.KeepAbove = true;
Label edLabel = new Label(message); var edLabel = new Label(message);
Entry edEntry = new Entry(); var edEntry = new Entry();
if (!String.IsNullOrWhiteSpace(prefill)) { if (!String.IsNullOrWhiteSpace(prefill)) {
edEntry.Text = prefill; edEntry.Text = prefill;
} }
@ -460,8 +466,8 @@ namespace Buypeeb {
ed.ContentArea.MarginEnd = 5; ed.ContentArea.MarginEnd = 5;
ed.MarginBottom = 5; ed.MarginBottom = 5;
ResponseType accepted = (ResponseType)ed.Run(); var accepted = (ResponseType)ed.Run();
string response = edEntry.Text; var response = edEntry.Text;
ed.Dispose(); ed.Dispose();
return (accepted == ResponseType.Ok, response); return (accepted == ResponseType.Ok, response);
} }
@ -472,7 +478,7 @@ namespace Buypeeb {
/// <returns>the id of the radiobutton without the "Sort" prefix</returns> /// <returns>the id of the radiobutton without the "Sort" prefix</returns>
private string GetSortType() { private string GetSortType() {
foreach (var name in new List<string> { "NameDescending", "NameAscending", "PriceDescending", "PriceAscending", "EndingDescending", "EndingAscending" }) { foreach (var name in new List<string> { "NameDescending", "NameAscending", "PriceDescending", "PriceAscending", "EndingDescending", "EndingAscending" }) {
var radio = (RadioMenuItem)this.builder.GetObject($"Sort{name}"); var radio = (RadioMenuItem)builder.GetObject($"Sort{name}");
if (radio.Active) { if (radio.Active) {
return name; return name;
} }
@ -486,14 +492,14 @@ namespace Buypeeb {
/// </summary> /// </summary>
private void RenderList() { private void RenderList() {
string id = null; string id = null;
if (this.selectedItem != null) { if (selectedItem != null) {
id = this.selectedItem.id; id = selectedItem.id;
} }
this.items.Clear(); items.Clear();
var values = this.settings.watchlist.Values; var values = settings.watchlist.Values;
IOrderedEnumerable<YahooAuctionsItem> sorted; IOrderedEnumerable<YahooAuctionsItem> sorted;
var type = this.GetSortType(); var type = GetSortType();
if (type == "NameDescending") { if (type == "NameDescending") {
sorted = values.OrderByDescending(item => item.name); sorted = values.OrderByDescending(item => item.name);
} }
@ -501,10 +507,10 @@ namespace Buypeeb {
sorted = values.OrderBy(item => item.name); sorted = values.OrderBy(item => item.name);
} }
else if (type == "PriceDescending") { else if (type == "PriceDescending") {
sorted = values.OrderByDescending(item => item.price); sorted = values.OrderByDescending(item => item.Price);
} }
else if (type == "PriceAscending") { else if (type == "PriceAscending") {
sorted = values.OrderBy(item => item.price); sorted = values.OrderBy(item => item.Price);
} }
else if (type == "EndingDescending") { else if (type == "EndingDescending") {
sorted = values.OrderByDescending(item => item.endDate); sorted = values.OrderByDescending(item => item.endDate);
@ -528,14 +534,15 @@ namespace Buypeeb {
} }
} }
(this.itemTreeView.Model as TreeModelFilter).Refilter(); ((TreeModelFilter) itemTreeView.Model).Refilter();
if (id != null) { if (id == null) { return; }
// attempt to reselect the item we were just looking at
var pathAndIter = this.GetRow(id); // attempt to reselect the item we were just looking at
if (pathAndIter.path != null) { // ReSharper disable once UseDeconstruction
this.itemTreeView.Selection.SelectPath(pathAndIter.path); var pathAndIter = GetRow(id);
} if (pathAndIter.path != null) {
itemTreeView.Selection.SelectPath(pathAndIter.path);
} }
} }
@ -543,33 +550,32 @@ namespace Buypeeb {
private void ButtonAddClicked(object sender, EventArgs a) { private void ButtonAddClicked(object sender, EventArgs a) {
// Console.WriteLine("ButtonAddClicked"); // Console.WriteLine("ButtonAddClicked");
AddItemDialogue aid = new AddItemDialogue(); var aid = new AddItemDialogue {Title = "Buypeeb"};
aid.Title = "Buypeeb"; aid.Run();
ResponseType accepted = (ResponseType)aid.Run(); var url = aid.GetURL();
string url = aid.GetURL(); var name = aid.GetName();
string name = aid.GetName();
aid.Dispose(); aid.Dispose();
// vry simpl url validation for simpol creachers // vry simpl url validation for simpol creachers
// TODO: better. do better. // TODO: better. do better.
Regex rx = new Regex(@"^http.+yahoo.+"); var rx = new Regex(@"^http.+yahoo.+");
if (rx.IsMatch(url)) { if (rx.IsMatch(url)) {
this.UpdateItem(this.settings.Watch(url, name).id, true); UpdateItem(settings.Watch(url, name).id, true);
this.RenderList(); RenderList();
} }
else { else {
var md = this.MsgBox($"\"{url}\" is not a valid Buyee or Yahoo! Auctions Japan URL.", ButtonsType.Ok); var md = MsgBox($"\"{url}\" is not a valid Buyee or Yahoo! Auctions Japan URL.", ButtonsType.Ok);
md.Run(); md.Run();
md.Dispose(); md.Dispose();
} }
} }
private void ButtonUpdateAllClicked(object sender, EventArgs a) { private void ButtonUpdateAllClicked(object sender, EventArgs a) {
this.UpdateItems(); UpdateItems();
} }
private void ButtonClearEndedClicked(object sender, EventArgs a) { 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 md = MsgBox("Are you sure you want to remove all ended auctions from the list?");
var r = (ResponseType)md.Run(); var r = (ResponseType)md.Run();
md.Dispose(); md.Dispose();
if (r != ResponseType.Ok) { if (r != ResponseType.Ok) {
@ -577,24 +583,24 @@ namespace Buypeeb {
} }
var removeMe = new List<string>(); var removeMe = new List<string>();
foreach (var item in this.settings.watchlist) { foreach (var (key, value) in settings.watchlist) {
if (!item.Value.available) { if (!value.Available) {
removeMe.Add(item.Key); removeMe.Add(key);
} }
} }
foreach (var id in removeMe) { foreach (var id in removeMe) {
this.settings.watchlist.Remove(id); settings.watchlist.Remove(id);
} }
this.RenderList(); RenderList();
} }
private void ButtonClearAllClicked(object sender, EventArgs a) { private void ButtonClearAllClicked(object sender, EventArgs a) {
var md = this.MsgBox("Are you sure you want to clear ALL items?"); var md = MsgBox("Are you sure you want to clear ALL items?");
if (md.Run() == (int)ResponseType.Ok) { if (md.Run() == (int)ResponseType.Ok) {
this.settings.watchlist.Clear(); settings.watchlist.Clear();
this.RenderList(); RenderList();
} }
md.Dispose(); md.Dispose();
} }
@ -615,10 +621,10 @@ namespace Buypeeb {
if (od.Run() == (int)ResponseType.Accept) { if (od.Run() == (int)ResponseType.Accept) {
try { try {
string j = File.ReadAllText(od.Filename); var j = File.ReadAllText(od.Filename);
this.settings = JsonSerializer.Deserialize<Settings>(j); settings = JsonSerializer.Deserialize<Settings>(j);
this.RenderList(); RenderList();
this.UpdateItems(); UpdateItems();
} }
catch (Exception e) { catch (Exception e) {
Console.WriteLine(e); Console.WriteLine(e);
@ -631,7 +637,7 @@ namespace Buypeeb {
} }
private void ButtonSaveClicked(object sender, EventArgs a) { private void ButtonSaveClicked(object sender, EventArgs a) {
this.SaveSettings(); SaveSettings();
} }
private void ButtonSaveAsClicked(object sender, EventArgs a) { private void ButtonSaveAsClicked(object sender, EventArgs a) {
@ -652,8 +658,8 @@ namespace Buypeeb {
if (sd.Run() == (int)ResponseType.Accept) { if (sd.Run() == (int)ResponseType.Accept) {
try { try {
if (!File.Exists(sd.Filename)) { if (!File.Exists(sd.Filename)) {
using (StreamWriter fs = File.CreateText(sd.Filename)) { using (var fs = File.CreateText(sd.Filename)) {
fs.Write(JsonSerializer.Serialize(this.settings, jsonOptions)); fs.Write(JsonSerializer.Serialize(settings, jsonOptions));
} }
} }
} }
@ -669,7 +675,7 @@ namespace Buypeeb {
} }
private void ButtonExportClicked(object sender, EventArgs a) { private void ButtonExportClicked(object sender, EventArgs a) {
var readyQuery = from item in this.settings.watchlist.Values.ToList() where !item.ready select item; var readyQuery = from item in settings.watchlist.Values.ToList() where !item.Ready select item;
if (readyQuery.Count() != 0) { if (readyQuery.Count() != 0) {
var md = MsgBox("Please wait for all items to update before exporting a CSV.", ButtonsType.Ok); var md = MsgBox("Please wait for all items to update before exporting a CSV.", ButtonsType.Ok);
@ -695,9 +701,9 @@ namespace Buypeeb {
if (sd.Run() == (int)ResponseType.Accept) { if (sd.Run() == (int)ResponseType.Accept) {
try { try {
using (var writer = new StreamWriter(sd.Filename)) using (var writer = new StreamWriter(sd.Filename))
using (var csv = new CsvWriter(writer, System.Globalization.CultureInfo.InvariantCulture)) { using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture)) {
csv.WriteRecords(this.settings.watchlist); csv.WriteRecords(settings.watchlist);
} }
} }
catch (Exception e) { catch (Exception e) {
@ -713,104 +719,103 @@ namespace Buypeeb {
private void ButtonHelpClicked(object sender, EventArgs args) { private void ButtonHelpClicked(object sender, EventArgs args) {
Console.WriteLine("Watchlist:"); Console.WriteLine("Watchlist:");
foreach (var item in this.settings.watchlist) { foreach (var item in settings.watchlist) {
Console.WriteLine(item); Console.WriteLine(item);
} }
Console.WriteLine("---\nFilter results:"); Console.WriteLine("---\nFilter results:");
foreach (var item in this.filterQuery) { foreach (var item in filterQuery) {
Console.WriteLine(item); Console.WriteLine(item);
} }
Console.WriteLine("---\nListstore contents:"); Console.WriteLine("---\nListstore contents:");
foreach (object[] item in this.items) { foreach (object[] item in items) {
Console.WriteLine(item[0]); Console.WriteLine(item[0]);
} }
} }
private void ButtonQuitClicked(object sender, EventArgs a) { private void ButtonQuitClicked(object sender, EventArgs a) {
var md = this.MsgBox("Are you sure you want to quit?"); var md = MsgBox("Are you sure you want to quit?");
ResponseType response = (ResponseType)md.Run(); var response = (ResponseType)md.Run();
md.Dispose(); md.Dispose();
if (response == ResponseType.Ok) { if (response == ResponseType.Ok) {
this.SaveSettings(); SaveSettings();
Application.Quit(); Application.Quit();
} }
} }
private void ButtonSettingsClicked(object sender, EventArgs args) { private void ButtonSettingsClicked(object sender, EventArgs args) {
var win = new SettingsWindow(this.settings); var win = new SettingsWindow(settings);
Application.AddWindow(win); Application.AddWindow(win);
win.DeleteEvent += this.WindowSettingsClosed; win.DeleteEvent += WindowSettingsClosed;
win.Show(); win.Show();
} }
private void TreeViewItemsSelectionChanged(object sender, EventArgs a) { private void TreeViewItemsSelectionChanged(object sender, EventArgs a) {
this.UpdateSelectionView(); UpdateSelectionView();
} }
private void ButtonViewBuyeeClicked(object sender, EventArgs a) { private void ButtonViewBuyeeClicked(object sender, EventArgs a) {
this.OpenUrl(this.selectedItem.buyeeUrl); OpenUrl(selectedItem.buyeeUrl);
} }
private void ButtonViewYahooClicked(object sender, EventArgs a) { private void ButtonViewYahooClicked(object sender, EventArgs a) {
this.OpenUrl(this.selectedItem.url); OpenUrl(selectedItem.url);
} }
private void ButtonSelectedRemoveClicked(object sender, EventArgs a) { private void ButtonSelectedRemoveClicked(object sender, EventArgs a) {
var item = this.selectedItem; var item = 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 var md = 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(); var response = (ResponseType)md.Run();
md.Dispose(); md.Dispose();
if (response == ResponseType.Ok) { if (response == ResponseType.Ok) {
this.settings.watchlist.Remove(item.id); settings.watchlist.Remove(item.id);
this.RenderList(); RenderList();
} }
} }
private void ButtonSelectedRenameClicked(object sender, EventArgs a) { private void ButtonSelectedRenameClicked(object sender, EventArgs a) {
var item = this.selectedItem; var item = selectedItem;
(bool accepted, string response) = this.EntryDialogue("Rename item", $"Enter a new name for the item \"{item.name}\".", item.name); (var accepted, var response) = EntryDialogue("Rename item", $"Enter a new name for the item \"{item.name}\".", item.name);
if (accepted) { if (accepted) {
item.name = response; item.name = response;
this.UpdateSelectionView(); UpdateSelectionView();
} }
} }
private void ButtonSelectedUpdateClicked(object sender, EventArgs args) { private void ButtonSelectedUpdateClicked(object sender, EventArgs args) {
this.selectionViewBox.Sensitive = false; selectionViewBox.Sensitive = false;
if (this.updateQueue.Contains(this.selectedItem.id)) { if (updateQueue.Contains(selectedItem.id)) {
// the item is already waiting to be updated // the item is already waiting to be updated
return; return;
} }
this.UpdateItem(this.selectedItem.id); UpdateItem(selectedItem.id);
} }
private void ButtonSelectedFavouriteToggled(object sender, EventArgs args) { private void ButtonSelectedFavouriteToggled(object sender, EventArgs args) {
ToggleButton s = (ToggleButton)sender; var s = (ToggleButton)sender;
this.selectedItem.favourite = s.Active; selectedItem.favourite = s.Active;
if (this.settings.showFavouritesAtTopOfList) { if (settings.showFavouritesAtTopOfList) {
var id = this.selectedItem.id; RenderList();
this.RenderList();
} }
else { else {
// i don't know why this is necessary // i don't know why this is necessary
var pathAndIter = this.GetRow(this.selectedItem.id); var pathAndIter = GetRow(selectedItem.id);
if (pathAndIter.path != null) { if (pathAndIter.path != null) {
this.items.EmitRowChanged(pathAndIter.path, pathAndIter.iter); items.EmitRowChanged(pathAndIter.path, pathAndIter.iter);
} }
} }
} }
private void ButtonSelectedNotesClearClicked(object sender, EventArgs args) { private void ButtonSelectedNotesClearClicked(object sender, EventArgs args) {
var item = this.selectedItem; var item = selectedItem;
var md = this.MsgBox($"Are you sure you want to clear the notes for \"{item.name}\"?"); var md = MsgBox($"Are you sure you want to clear the notes for \"{item.name}\"?");
if (md.Run() == (int)ResponseType.Ok) { if (md.Run() == (int)ResponseType.Ok) {
var noteBuffer = (TextBuffer)this.builder.GetObject("TextBufferSelectedNotes"); var noteBuffer = (TextBuffer)builder.GetObject("TextBufferSelectedNotes");
noteBuffer.Clear(); noteBuffer.Clear();
this.selectedItem.notes = null; selectedItem.notes = null;
} }
md.Dispose(); md.Dispose();
} }
@ -818,18 +823,18 @@ namespace Buypeeb {
private void TextViewSelectedNotesFocusOut(object sender, FocusOutEventArgs args) { 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! // the "save" button does nothing, however, when you click the save button, you transfer focus to it, firing this event!
// how very sneaky // how very sneaky
var noteBuffer = (TextBuffer)this.builder.GetObject("TextBufferSelectedNotes"); var noteBuffer = (TextBuffer)builder.GetObject("TextBufferSelectedNotes");
if (this.selectedItem != null) { if (selectedItem != null) {
this.selectedItem.notes = String.IsNullOrWhiteSpace(noteBuffer.Text) ? null : noteBuffer.Text; selectedItem.notes = String.IsNullOrWhiteSpace(noteBuffer.Text) ? null : noteBuffer.Text;
} }
} }
private void SortMenuClosed(object sender, EventArgs args) { private void SortMenuClosed(object sender, EventArgs args) {
this.RenderList(); RenderList();
} }
private void WindowSettingsClosed(object sender, EventArgs args) { private void WindowSettingsClosed(object sender, EventArgs args) {
this.RenderList(); RenderList();
} }
// timers // timers
@ -839,17 +844,17 @@ namespace Buypeeb {
/// </summary> /// </summary>
/// <returns>true</returns> /// <returns>true</returns>
private bool UpdateSelectionEndTime() { private bool UpdateSelectionEndTime() {
if (!this.selectionViewBox.IsSensitive) { if (!selectionViewBox.IsSensitive) {
return true; return true;
} }
var item = this.selectedItem; var item = selectedItem;
if (!item.ready) { if (!item.Ready) {
return true; return true;
} }
string ending = ""; var ending = "";
if (item.available) { if (item.Available) {
var now = DateTime.Now; var now = DateTime.Now;
var end = item.endDate.ToLocalTime(); var end = item.endDate.ToLocalTime();
var span = end.Subtract(now); var span = end.Subtract(now);
@ -866,7 +871,7 @@ namespace Buypeeb {
ending = "Auction has ended"; ending = "Auction has ended";
} }
this.endingLabel.Text = ending; endingLabel.Text = ending;
return true; return true;
} }
@ -875,18 +880,18 @@ namespace Buypeeb {
/// </summary> /// </summary>
/// <returns>true</returns> /// <returns>true</returns>
private bool AutoUpdateItems() { private bool AutoUpdateItems() {
if (this.queueActive) { if (queueActive) {
// don't autoupdate if the queue is active // don't autoupdate if the queue is active
return true; return true;
} }
foreach (var item in this.outdatedItemQuery) { foreach (var item in outdatedItemQuery) {
updateQueue.Enqueue(item.id); updateQueue.Enqueue(item.id);
} }
if (updateQueue.TryPeek(out string _)) { if (updateQueue.TryPeek(out var _)) {
// there's at least one item in the queue // there's at least one item in the queue
this.ProcessUpdateQueue(); ProcessUpdateQueue();
} }
return true; return true;
@ -894,31 +899,31 @@ namespace Buypeeb {
// column renderers // column renderers
private void RenderColumnFavourite(Gtk.TreeViewColumn column, Gtk.CellRenderer cell, Gtk.ITreeModel model, Gtk.TreeIter iter) { private void RenderColumnFavourite(TreeViewColumn column, CellRenderer cell, ITreeModel model, TreeIter iter) {
YahooAuctionsItem item = (YahooAuctionsItem)model.GetValue(iter, 0); var item = (YahooAuctionsItem)model.GetValue(iter, 0);
(cell as Gtk.CellRendererText).Text = item.favourite ? "♥" : ""; ((CellRendererText) cell).Text = item.favourite ? "♥" : "";
} }
private void RenderColumnName(Gtk.TreeViewColumn column, Gtk.CellRenderer cell, Gtk.ITreeModel model, Gtk.TreeIter iter) { private void RenderColumnName(TreeViewColumn column, CellRenderer cell, ITreeModel model, TreeIter iter) {
YahooAuctionsItem item = (YahooAuctionsItem)model.GetValue(iter, 0); var item = (YahooAuctionsItem)model.GetValue(iter, 0);
(cell as Gtk.CellRendererText).Text = item.name ?? "Loading..."; ((CellRendererText) cell).Text = item.name ?? "Loading...";
} }
private void RenderColumnPriceYen(Gtk.TreeViewColumn column, Gtk.CellRenderer cell, Gtk.ITreeModel model, Gtk.TreeIter iter) { private void RenderColumnPriceYen(TreeViewColumn column, CellRenderer cell, ITreeModel model, TreeIter iter) {
YahooAuctionsItem item = (YahooAuctionsItem)model.GetValue(iter, 0); var item = (YahooAuctionsItem)model.GetValue(iter, 0);
(cell as Gtk.CellRendererText).Text = item.ready ? item.priceJPY : "..."; ((CellRendererText) cell).Text = item.Ready ? item.priceJpy : "...";
} }
private void RenderColumnPriceAUD(Gtk.TreeViewColumn column, Gtk.CellRenderer cell, Gtk.ITreeModel model, Gtk.TreeIter iter) { private void RenderColumnPriceAUD(TreeViewColumn column, CellRenderer cell, ITreeModel model, TreeIter iter) {
YahooAuctionsItem item = (YahooAuctionsItem)model.GetValue(iter, 0); var item = (YahooAuctionsItem)model.GetValue(iter, 0);
(cell as Gtk.CellRendererText).Text = item.ready ? item.priceAUD : "..."; ((CellRendererText) cell).Text = item.Ready ? item.priceAud : "...";
} }
private void RenderColumnEnding(Gtk.TreeViewColumn column, Gtk.CellRenderer cell, Gtk.ITreeModel model, Gtk.TreeIter iter) { private void RenderColumnEnding(TreeViewColumn column, CellRenderer cell, ITreeModel model, TreeIter iter) {
YahooAuctionsItem item = (YahooAuctionsItem)model.GetValue(iter, 0); var item = (YahooAuctionsItem)model.GetValue(iter, 0);
string ending = ""; var ending = "";
if (item.ready) { if (item.Ready) {
if (!item.available) { if (!item.Available) {
ending = "Ended"; ending = "Ended";
} }
else { else {
@ -930,13 +935,13 @@ namespace Buypeeb {
ending += end.ToString("MMM d "); ending += end.ToString("MMM d ");
} }
ending += end.ToString("HH:mm"); ending += end.ToString("HH:mm");
if (this.settings.showSecondsInListView) { if (settings.showSecondsInListView) {
// add the seconds on to the end // add the seconds on to the end
ending += end.ToString(":ss"); ending += end.ToString(":ss");
} }
} }
} }
(cell as Gtk.CellRendererText).Text = item.ready ? ending : "..."; ((CellRendererText) cell).Text = item.Ready ? ending : "...";
} }
// tree filter // tree filter
@ -946,13 +951,13 @@ namespace Buypeeb {
if (item == null) { if (item == null) {
return true; return true;
} }
if (item.price == 0) { 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 // 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; return true;
} }
bool Filtered(string name) { bool Filtered(string name) {
return this.filterChecks[name].Active; 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 // first, check to see if any filters are set that would exclude everything, such as hiding both active and ended auctions
@ -966,11 +971,11 @@ namespace Buypeeb {
return false; return false;
} }
return this.filterQuery.Contains(item); return filterQuery.Contains(item);
} }
private void RunFilter(object sender, EventArgs a) { private void RunFilter(object sender, EventArgs a) {
(this.itemTreeView.Model as TreeModelFilter).Refilter(); ((TreeModelFilter) itemTreeView.Model).Refilter();
} }
} }
} }

View File

@ -2,8 +2,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
namespace Buypeeb { namespace Buypeeb {
internal class Settings {
class Settings {
public int updateInterval { get; set; } = 10 * 60; public int updateInterval { get; set; } = 10 * 60;
public int favouriteUpdateInterval { get; set; } = 5 * 60; public int favouriteUpdateInterval { get; set; } = 5 * 60;
public int updateIntervalCritical { get; set; } = 60; public int updateIntervalCritical { get; set; } = 60;
@ -17,40 +16,29 @@ namespace Buypeeb {
} }
public Settings() { public Settings() {
if (this.watchlist == null) { // create a new watchlist from an empty dictionary if it's null, which should only happen if either this is the
// either this is the first time the program has been run, or there's something wrong with userdata.json // first time the program has been run, or there's something wrong with userdata.json
this.watchlist = new Dictionary<string, YahooAuctionsItem>(); watchlist ??= new Dictionary<string, YahooAuctionsItem>();
}
} }
public YahooAuctionsItem Watch(string url, string name) { public YahooAuctionsItem Watch(string url, string name) {
string id = BuypeebApp.IDFromURL(url); var id = BuypeebApp.IDFromURL(url);
Console.WriteLine(id); Console.WriteLine(id);
this.watchlist[id] = new YahooAuctionsItem(id, name); watchlist[id] = new YahooAuctionsItem(id, name);
return this.watchlist[id]; return watchlist[id];
} }
// TRUE if the item hasn't been updated for at least interval seconds // TRUE if the item hasn't been updated for at least interval seconds
// for example, if the interval is 10, and the item hasn't been updated since 20 seconds ago, this will be TRUE // for example, if the interval is 10, and the item hasn't been updated since 20 seconds ago, this will be TRUE
public bool ItemNotUpdatedSinceInterval(YahooAuctionsItem item) { public bool ItemNotUpdatedSinceInterval(YahooAuctionsItem item) {
int seconds = 1000; int seconds;
if (item.favourite) { if (item.favourite) {
if (item.endingSoon) { seconds = item.endingSoon ? favouriteUpdateIntervalCritical : favouriteUpdateInterval;
seconds = this.favouriteUpdateIntervalCritical;
}
else {
seconds = this.favouriteUpdateInterval;
}
} }
else { else {
if (item.endingSoon) { seconds = item.endingSoon ? updateIntervalCritical : updateInterval;
seconds = this.updateIntervalCritical;
}
else {
seconds = this.updateInterval;
}
} }
var later = item.lastUpdated.AddSeconds(seconds); var later = item.LastUpdated.AddSeconds(seconds);
return DateTime.Compare(later, DateTime.UtcNow) < 0; return DateTime.Compare(later, DateTime.UtcNow) < 0;
} }
} }

View File

@ -1,44 +1,46 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using Gtk; using Gtk;
// ReSharper disable UnusedMember.Local
// ReSharper disable UnusedParameter.Local
namespace Buypeeb { namespace Buypeeb {
class SettingsWindow : Window { internal class SettingsWindow : Window {
private List<Switch> generalSwitches = new List<Switch>(); private readonly List<Switch> generalSwitches = new List<Switch>();
private List<Entry> updateIntervalEntries = new List<Entry>(); private readonly List<Entry> updateIntervalEntries = new List<Entry>();
private Settings settings; private readonly Settings settings;
private Builder builder; private readonly Builder builder;
private List<String> generalSwitchNames = new List<String> { "ShowSecondsInListView", "Autosave", "ShowFavouritesAtTopOfList" }; private readonly List<string> generalSwitchNames = new List<string> { "ShowSecondsInListView", "Autosave", "ShowFavouritesAtTopOfList" };
private List<String> updateIntervalEntryNames = new List<String> { "UpdateInterval", "UpdateIntervalCritical", "FavouriteUpdateInterval", "FavouriteUpdateIntervalCritical" }; private readonly List<string> updateIntervalEntryNames = new List<string> { "UpdateInterval", "UpdateIntervalCritical", "FavouriteUpdateInterval", "FavouriteUpdateIntervalCritical" };
public SettingsWindow(Settings settings) : this(new Builder("settings.glade"), settings) { } public SettingsWindow(Settings settings) : this(new Builder("settings.glade"), settings) { }
private SettingsWindow(Builder builder, Settings settings) : base(builder.GetObject("WindowSettings").Handle) { private SettingsWindow(Builder builder, Settings settings) : base(builder.GetObject("WindowSettings").Handle) {
this.Title = "Buypeeb - Settings"; Title = "Buypeeb - Settings";
this.settings = settings; this.settings = settings;
this.builder = builder; this.builder = builder;
builder.Autoconnect(this); builder.Autoconnect(this);
foreach (var name in this.generalSwitchNames) { foreach (var name in generalSwitchNames) {
var s = (Switch)builder.GetObject($"Switch{name}"); var s = (Switch)builder.GetObject($"Switch{name}");
this.generalSwitches.Add(s); generalSwitches.Add(s);
s.Active = GetSetting<bool>(this.PropertyName(name)); s.Active = GetSetting<bool>(PropertyName(name));
} }
foreach (var name in this.updateIntervalEntryNames) { foreach (var name in updateIntervalEntryNames) {
var e = (Entry)builder.GetObject($"Entry{name}"); var e = (Entry)builder.GetObject($"Entry{name}");
this.updateIntervalEntries.Add(e); updateIntervalEntries.Add(e);
e.Text = GetSetting<int>(this.PropertyName(name)).ToString(); e.Text = GetSetting<int>(PropertyName(name)).ToString();
} }
} }
private T GetSetting<T>(string property) { private T GetSetting<T>(string property) {
return (T)this.settings.GetType().GetProperty(property).GetValue(this.settings, null); return (T)settings.GetType().GetProperty(property).GetValue(settings, null);
} }
private void SetSetting<T>(string property, T value) { private void SetSetting<T>(string property, T value) {
this.settings.GetType().GetProperty(property).SetValue(this.settings, value); settings.GetType().GetProperty(property).SetValue(settings, value);
} }
private string PropertyName(string property) { private string PropertyName(string property) {
@ -48,10 +50,10 @@ namespace Buypeeb {
private void ButtonSaveClicked(object sender, EventArgs args) { private void ButtonSaveClicked(object sender, EventArgs args) {
// first, validate all the intervals // first, validate all the intervals
bool failed = false; var failed = false;
foreach (var name in this.updateIntervalEntryNames) { foreach (var name in updateIntervalEntryNames) {
var e = (Entry)builder.GetObject($"Entry{name}"); var e = (Entry)builder.GetObject($"Entry{name}");
if (!int.TryParse(e.Text, out int result)) { if (!int.TryParse(e.Text, out var result)) {
failed = true; failed = true;
} }
else { else {
@ -60,28 +62,30 @@ namespace Buypeeb {
} }
} }
if (failed) { if (!failed) {
var md = new MessageDialog(this, DialogFlags.Modal, MessageType.Error, ButtonsType.Ok, "Update intervals must be a whole number between 30 and 6000."); continue;
md.Run();
md.Dispose();
return;
} }
var md = new MessageDialog(this, DialogFlags.Modal, MessageType.Error, ButtonsType.Ok, "Update intervals must be a whole number between 30 and 6000.");
md.Run();
md.Dispose();
return;
} }
// validation success! // validation success!
foreach (var name in this.updateIntervalEntryNames) { foreach (var name in updateIntervalEntryNames) {
this.SetSetting<int>(PropertyName(name), int.Parse((builder.GetObject($"Entry{name}") as Entry).Text)); SetSetting(PropertyName(name), int.Parse(((Entry) builder.GetObject($"Entry{name}")).Text));
} }
foreach (var name in this.generalSwitchNames) { foreach (var name in generalSwitchNames) {
this.SetSetting<bool>(PropertyName(name), (builder.GetObject($"Switch{name}") as Switch).Active); SetSetting(PropertyName(name), ((Switch) builder.GetObject($"Switch{name}")).Active);
} }
this.Dispose(); Dispose();
} }
private void ButtonCancelClicked(object sender, EventArgs args) { private void ButtonCancelClicked(object sender, EventArgs args) {
this.Dispose(); Dispose();
} }
} }
} }

View File

@ -1,26 +1,18 @@
using System; using System;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization;
using System.Text.Json; using System.Text.Json;
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using CsvHelper.Configuration.Attributes; using CsvHelper.Configuration.Attributes;
namespace Buypeeb { namespace Buypeeb {
class YahooAuctionsItem { internal class YahooAuctionsItem {
[JsonIgnore] [JsonIgnore]
public string url { public string url => $"https://page.auctions.yahoo.co.jp/jp/auction/{id}";
get {
return $"https://page.auctions.yahoo.co.jp/jp/auction/{this.id}";
}
}
[JsonIgnore] [JsonIgnore]
public string buyeeUrl { public string buyeeUrl => $"https://buyee.jp/item/yahoo/auction/{id}";
get {
return $"https://buyee.jp/item/yahoo/auction/{this.id}";
}
}
// any items with a getter will be saved to userdata.json // any items with a getter will be saved to userdata.json
// anything that's configurable by the user, such as the custom name, should be saved // anything that's configurable by the user, such as the custom name, should be saved
@ -29,43 +21,50 @@ namespace Buypeeb {
// there's not really a need for it i guess but it's my program and i can do what i want // there's not really a need for it i guess but it's my program and i can do what i want
// anything with the attribute [Ignore] won't be put in the CSV, and things with [JsonIgnore] won't be put in userdata.json // anything with the attribute [Ignore] won't be put in the CSV, and things with [JsonIgnore] won't be put in userdata.json
[Ignore] [Ignore]
public string id { get; set; } public string id { get; }
public string name { get; set; } public string name { get; set; }
public int price = 0; public int Price;
public int winPrice; public int WinPrice;
public string originalName { get; set; } public string originalName { get; set; }
public string notes { get; set; } public string notes { get; set; }
public bool favourite { get; set; } = false; public bool favourite { get; set; }
public DateTime startDate; public DateTime StartDate;
public DateTime endDate { get; set; } public DateTime endDate { get; set; }
public DateTime lastUpdated; public DateTime LastUpdated;
public int bids; public int Bids;
public bool autoExtension; public bool AutoExtension;
public bool ready; public bool Ready;
public bool available; public bool Available;
[Ignore, JsonIgnore] [Ignore, JsonIgnore]
public bool updatedRecently { public bool updatedRecently {
get { get {
var later = this.lastUpdated.AddSeconds(15); var later = LastUpdated.AddSeconds(15);
return DateTime.Compare(later, this.lastUpdated) < 0; return DateTime.Compare(later, LastUpdated) < 0;
} }
} }
[JsonIgnore] [JsonIgnore]
public string priceJPY { get { return $"¥{this.price}"; } } public string priceJpy => $"¥{Price}";
[JsonIgnore] [JsonIgnore]
public string winPriceJPY { get { return $"¥{this.winPrice}"; } } public string winPriceJpy => $"¥{WinPrice}";
[Ignore, JsonIgnore] [Ignore, JsonIgnore]
public string priceAUD { get { return $"${(this.price / 75.0):f2}"; } } public string priceAud => $"${(Price / 75.0):f2}";
[Ignore, JsonIgnore] [Ignore, JsonIgnore]
public string winPriceAUD { get { return $"${(this.winPrice / 75.0):f2}"; } } public string winPriceAud => $"${(WinPrice / 75.0):f2}";
[Ignore, JsonIgnore] [Ignore, JsonIgnore]
public bool endingToday { get { return this.endDate.DayOfYear == DateTime.UtcNow.DayOfYear; } } public bool endingToday => endDate.DayOfYear == DateTime.UtcNow.DayOfYear;
[Ignore, JsonIgnore] [Ignore, JsonIgnore]
public bool hasWinPrice { get { return this.winPrice != 0; } } public bool hasWinPrice => WinPrice != 0;
[Ignore, JsonIgnore] [Ignore, JsonIgnore]
public bool endingSoon { get { return DateTime.Compare(DateTime.UtcNow.AddMinutes(10), this.endDate) > 0; } } public bool endingSoon => DateTime.Compare(DateTime.UtcNow.AddMinutes(10), endDate) > 0;
private bool success { get; set; } // TODO: custom setter that throws an exception if set to false or something idk private bool success { get; set; } // TODO: custom setter that throws an exception if set to false or something idk
public YahooAuctionsItem(string id, string name) { public YahooAuctionsItem(string id, string name) {
@ -81,52 +80,48 @@ namespace Buypeeb {
// TODO: handle all the parsing errors and weird interpretation that could possibly happen here // TODO: handle all the parsing errors and weird interpretation that could possibly happen here
var rx = new Regex(@"var pageData ?= ?(\{.+?\});", RegexOptions.Singleline); // TODO: maybe compile and match the regex in another thread var rx = new Regex(@"var pageData ?= ?(\{.+?\});", RegexOptions.Singleline); // TODO: maybe compile and match the regex in another thread
var m = rx.Match(html); var m = rx.Match(html);
if (m == null) {
Console.WriteLine("no sir i don't like it");
return;
}
Dictionary<string, Dictionary<string, string>> j_full; Dictionary<string, Dictionary<string, string>> jFull;
try { try {
// master forgive me, but i must go all out, just this once... // master forgive me, but i must go all out, just this once...
j_full = JsonSerializer.Deserialize<Dictionary<string, Dictionary<string, string>>>(m.Groups[1].Value); jFull = JsonSerializer.Deserialize<Dictionary<string, Dictionary<string, string>>>(m.Groups[1].Value);
} }
catch (Exception e) { catch {
Console.WriteLine("oh jeez oh man oh jeez oh man oh jeez oh man"); Console.WriteLine("oh jeez oh man oh jeez oh man oh jeez oh man");
Console.WriteLine(m.Groups[1].Value); Console.WriteLine(m.Groups[1].Value);
throw e; throw;
} }
var jst = TimeZoneInfo.CreateCustomTimeZone("JST", new TimeSpan(9, 0, 0), "Japan Standard Time", "Japen Standard Time"); var jst = TimeZoneInfo.CreateCustomTimeZone("JST", new TimeSpan(9, 0, 0), "Japan Standard Time", "Japen Standard Time");
var j = j_full["items"]; var j = jFull["items"];
this.originalName = j["productName"]; originalName = j["productName"];
this.startDate = TimeZoneInfo.ConvertTimeToUtc(DateTime.ParseExact(j["starttime"], "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture), jst); StartDate = TimeZoneInfo.ConvertTimeToUtc(DateTime.ParseExact(j["starttime"], "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture), jst);
this.endDate = TimeZoneInfo.ConvertTimeToUtc(DateTime.ParseExact(j["endtime"], "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture), jst); endDate = TimeZoneInfo.ConvertTimeToUtc(DateTime.ParseExact(j["endtime"], "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture), jst);
this.lastUpdated = DateTime.UtcNow; LastUpdated = DateTime.UtcNow;
this.available = j["isClosed"] == "0"; Available = j["isClosed"] == "0";
this.success = int.TryParse(j["price"], out this.price); success = int.TryParse(j["price"], out Price);
this.success = int.TryParse(j["winPrice"], out this.winPrice); success = int.TryParse(j["winPrice"], out WinPrice);
this.success = int.TryParse(j["bids"], out this.bids); success = int.TryParse(j["bids"], out Bids);
if (String.IsNullOrWhiteSpace(this.name)) { if (String.IsNullOrWhiteSpace(name)) {
this.name = this.originalName; name = originalName;
} }
// as far as i can tell, neither the `pageData` nor the `conf` variables in the html seem to store whether or not the auction uses automatic extension // as far as i can tell, neither the `pageData` nor the `conf` variables in the html seem to store whether or not the auction uses automatic extension
// the `conf` variable *does* store whether or not the auction has the "early end" feature enabled, in the key `earlyed`. // the `conf` variable *does* store whether or not the auction has the "early end" feature enabled, in the key `earlyed`.
// unfortunately, it seems like the only way to get the auto extension info is to scrape the page for the info column that displays the auto ext status // unfortunately, it seems like the only way to get the auto extension info is to scrape the page for the info column that displays the auto ext status
// and check whether or not it's equal to "ari" (japanese for "yes"). // and check whether or not it's equal to "ari" (japanese for "yes").
var autoExtensionCheck = new Regex(@"自動延長.+\n.+>(.+)<"); rx = new Regex(@"自動延長.+\n.+>(.+)<");
m = rx.Match(html); m = rx.Match(html);
if (m.Groups[1].Value != null) { if (m.Groups[1].Value != null) {
this.autoExtension = (m.Groups[1].Value == "あり"); AutoExtension = (m.Groups[1].Value == "あり");
} }
} }
public override string ToString() { public override string ToString() {
return $"{this.id}: {this.name}"; return $"{id}: {name}";
} }
} }
} }