-
Notifications
You must be signed in to change notification settings - Fork 0
/
Thoth.cs
184 lines (156 loc) · 5.12 KB
/
Thoth.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using Eto.Forms;
using Eto.Drawing;
namespace ThothGui
{
class ThothDownloadThread
{
public bool Done = false;
private string _title = "";
private string _author = "";
private Bitmap _cover;
private IEnumerable<string> _urls;
public ThothDownloadThread(IEnumerable<URL> urls, string title, string author, Bitmap cover)
{
_title = title;
_author = author;
_cover = cover;
_urls = urls.Select(x => x.Href);
}
public void Down()
{
if (_cover != null)
{
File.WriteAllBytes("Cover.png", _cover.ToByteArray(ImageFormat.Jpeg));
}
var stringUrls = _urls;
Download.CheckAndDeleteDirectory("temp");
if (_cover == null)
{
Process.EbookFromListDefault(false, _title, _author, "", stringUrls);
}
else
{
Process.EbookFromListCoverIsFile(false, _title, _author, "cover.xhtml", stringUrls);
}
Done = true;
Download.CheckAndDelete("Cover.png");
Console.WriteLine("Done!");
}
}
class Thoth : Form
{
private Clipboard _clipboard;
private PasteBox _pastebox;
private CoverView _cover;
private ThothConsole _console;
private MetadataInput _input;
private Button _downloadButton;
private ButtonToolItem _toolbarPaste;
private Dialog<string> _test;
private bool _downloadRunning = false;
public Thoth()
{
ClientSize = new Size(500, 400);
_clipboard = new Clipboard();
_pastebox = new PasteBox();
_cover = new CoverView();
_console = new ThothConsole();
_downloadButton = new Button { Text = "Download" };
_input = new MetadataInput();
_test = new Dialog<string>();
_toolbarPaste = new ButtonToolItem { Text = "Paste" };
Console.SetOut(_console.Writer);
//toolbar
ToolBar = new ToolBar
{
Items = {
_toolbarPaste,
new SeparatorToolItem()
}
};
var layout = new TableLayout();
layout.Rows.Add(new TableRow(
new TableLayout {
Rows = {
_pastebox,
_input,
new TableRow(new TableCell(_downloadButton, false)) } },
new TableLayout
{
Rows =
{
_cover,
_console
}
}
));
layout.Rows.Add(null);
Content = layout;
BindHandlers();
}
private void _test_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
_test.Result = "hi";
}
private void BindHandlers()
{
KeyDown += PasteEvent;
_toolbarPaste.Click += _toolbarPaste_Click;
_downloadButton.Click += _downloadButton_Click;
}
private void _toolbarPaste_Click(object sender, EventArgs e)
{
HandlePaste();
}
private void _downloadButton_Click(object sender, EventArgs e)
{
if (!_downloadRunning)
{
_downloadButton.Enabled = false;
var download = new ThothDownloadThread(_pastebox.GetUrls(), _input.GetTitle(), _input.GetAuthor(), _cover.GetImage());
ThreadStart start = download.Down;
start += () =>
{
_downloadRunning = false;
Application.Instance.Invoke(() => _downloadButton.Enabled = true);
};
_downloadRunning = true;
var caller = new Thread(start);
caller.Start();
}
}
private void HandlePaste()
{
foreach(var thing in _clipboard.Types)
{
Console.WriteLine(thing);
}
if (_clipboard.Types.Contains("HTML Format"))
{
_pastebox.ExtractPasteData(_clipboard.Html);
}
if (_clipboard.Types.Contains("Bitmap") || _clipboard.Types.Contains("DeviceIndependentBitmap"))
{
if(_clipboard.Image is Bitmap)
{
var image = (Bitmap)_clipboard.Image;
_cover.ExtractPasteData(image);
}
}
}
private void PasteEvent(object sender, KeyEventArgs e)
{
if(e.Key == Keys.V && e.Modifiers == Keys.Control)
{
HandlePaste();
}
}
}
}