buypeeb-cs/MainWindow.cs

193 lines
5.6 KiB
C#
Raw Normal View History

2020-08-31 14:02:41 +00:00
/*
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;
2020-09-01 10:41:29 +00:00
using System.Collections.Generic;
2020-09-01 09:58:35 +00:00
using System.Text.RegularExpressions;
2020-09-03 05:23:23 +00:00
// using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Gtk;
namespace Buypeeb {
2020-09-03 05:23:23 +00:00
public struct ItemColumns {
public static int Name = 0;
public static int PriceYen = 1;
public static int PriceAUD = 2;
public static int Ending = 3;
public static int Id = 4;
2020-08-31 14:20:56 +00:00
}
class MainWindow : Window {
2020-08-31 14:20:56 +00:00
2020-09-02 03:21:32 +00:00
private ListStore items;
private Settings settings;
2020-08-31 14:20:56 +00:00
2020-09-03 05:23:23 +00:00
static SemaphoreSlim tasklimit = new SemaphoreSlim(4);
2020-09-01 09:58:35 +00:00
public MainWindow() : this(new Builder("main.glade")) { }
private MainWindow(Builder builder) : base(builder.GetObject("wndMain").Handle) {
2020-09-02 03:21:32 +00:00
this.settings = new Settings();
2020-09-02 04:12:56 +00:00
this.settings.Save();
2020-08-31 14:20:56 +00:00
this.Title = "Buypeeb";
2020-09-03 05:23:23 +00:00
builder.Autoconnect(this);
2020-09-02 03:21:32 +00:00
this.items = (ListStore)builder.GetObject("ListItems");
this.RenderList();
2020-09-03 05:23:23 +00:00
2020-09-02 03:21:32 +00:00
foreach (object[] row in this.items) {
2020-09-03 05:23:23 +00:00
Console.WriteLine(row[ItemColumns.Name]);
2020-08-31 14:20:56 +00:00
}
DeleteEvent += Window_Shutdown;
}
private void Window_Shutdown(object sender, DeleteEventArgs args) {
Application.Quit();
}
2020-09-01 10:41:29 +00:00
// general behaviour
2020-09-03 05:23:23 +00:00
private void UpdateThread(string id) {
var item = this.settings.watchlist[id];
Console.WriteLine($"Updating {id}...");
item.ready = false;
Thread.Sleep(3000);
string html = "Heeenlo";
item.name = html;
item.ready = true;
Console.WriteLine($"{id} updated.");
Gtk.Application.Invoke(delegate {
// this.RenderListItem(id);\
this.RenderList();
});
}
private void UpdateItem(string id) {
tasklimit.Wait();
var t = Task.Factory.StartNew(() => {
this.UpdateThread(id);
}).ContinueWith(task => { tasklimit.Release(); });
}
private void UpdateItems() {
var t = Task.Factory.StartNew(() => {
foreach (var item in this.settings.watchlist) {
this.UpdateItem(item.Key);
}
});
}
private (Boolean accepted, string response) EntryDialogue(string title = "Buypeeb", string message = "Hi there!") {
Dialog ed = new Dialog(title, null, Gtk.DialogFlags.DestroyWithParent, "Cancel", ResponseType.Cancel, "OK", ResponseType.Ok);
ed.DefaultResponse = ResponseType.Ok;
Label edLabel = new Label(message);
Entry edEntry = new Entry();
edEntry.ActivatesDefault = true;
ed.ContentArea.PackStart(edLabel, true, true, 2);
edLabel.Show();
ed.ContentArea.PackStart(edEntry, true, true, 10);
edEntry.Show();
ResponseType accepted = (ResponseType)ed.Run();
string response = edEntry.Text;
ed.Dispose();
return (accepted == ResponseType.Ok, response);
}
2020-09-02 03:21:32 +00:00
private void RenderList() {
this.items.Clear();
foreach (KeyValuePair<string, Listing> entry in settings.watchlist) {
string[] values = new[] { entry.Value.name, entry.Value.PriceJPY(), entry.Value.PriceAUD(), "whenever", entry.Value.id };
this.items.AppendValues(values);
}
}
// button handlers
private void ButtonAddClicked(object sender, EventArgs a) {
2020-09-01 09:58:35 +00:00
// Console.WriteLine("ButtonAddClicked");
AddItemDialogue aid = new AddItemDialogue();
ResponseType accepted = (ResponseType)aid.Run();
string url = aid.GetURL();
string name = aid.GetName();
aid.Dispose();
// vry simpl url validation for simpol creachers
Regex rx = new Regex(@"^http.+yahoo.+");
if (rx.IsMatch(url)) {
Console.WriteLine("{0} will be added", url);
2020-09-02 03:21:32 +00:00
this.settings.Watch(url, name);
this.RenderList();
2020-09-01 09:58:35 +00:00
}
else {
Console.WriteLine("{0} is an invalid url", url);
}
}
private void ButtonUpdateAllClicked(object sender, EventArgs a) {
Console.WriteLine("ButtonUpdateAllClicked");
2020-09-03 05:23:23 +00:00
this.UpdateItems();
}
private void ButtonClearEndedClicked(object sender, EventArgs a) {
Console.WriteLine("ButtonClearEndedClicked");
}
private void ButtonClearAllClicked(object sender, EventArgs a) {
Console.WriteLine("ButtonClearAllClicked");
}
private void ButtonSaveClicked(object sender, EventArgs a) {
Console.WriteLine("ButtonSaveClicked");
}
private void ButtonQuitClicked(object sender, EventArgs a) {
MessageDialog md = new MessageDialog(null, DialogFlags.DestroyWithParent, MessageType.Question, ButtonsType.OkCancel, "Are you sure you want to quit?");
ResponseType response = (ResponseType)md.Run();
md.Dispose();
if (response == ResponseType.Ok) {
Application.Quit();
}
}
private void tveItemsSelectionChanged(object sender, EventArgs a) {
Console.WriteLine("tveItemsSelectionChanged");
}
private void ButtonViewBuyeeClicked(object sender, EventArgs a) {
Console.WriteLine("ButtonViewBuyeeClicked");
}
private void ButtonViewYahooClicked(object sender, EventArgs a) {
Console.WriteLine("ButtonViewYahooClicked");
}
private void ButtonSelectedRemoveClicked(object sender, EventArgs a) {
Console.WriteLine("ButtonSelectedRemoveClicked");
}
private void ButtonSelectedRenameClicked(object sender, EventArgs a) {
Console.WriteLine("ButtonSelectedRenameClicked");
}
}
}