-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
302 lines (262 loc) · 7.88 KB
/
main.go
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package main
import (
"bufio"
"fmt"
"io"
"log"
"net/url"
"os"
"path/filepath"
"time"
"github.com/gotk3/gotk3/glib"
"github.com/gotk3/gotk3/gtk"
)
const appId = "com.github-135e2.annie-gtk"
var (
DestFolder string
URL string
exPath string
)
type outputBuffer struct {
reader *bufio.Reader
textview *gtk.TextView
}
func main() {
app, err := gtk.ApplicationNew(appId, glib.APPLICATION_FLAGS_NONE)
if err != nil {
log.Fatal("Could not create application.", err)
}
// Get exec path
ex, err := os.Executable()
errorCheck(err)
exPath = filepath.Dir(ex) + string(os.PathSeparator)
// Init i18n
glib.InitI18n("annie-gtk", exPath+"i18n")
app.Connect("activate", func() {
onActivate(app)
})
app.Run(os.Args)
}
func onActivate(application *gtk.Application) {
// Create a new application window.
win := setupWindow("annie-gtk", application)
mainBox := setupBox(gtk.ORIENTATION_VERTICAL)
win.Add(mainBox)
// Setup menuBar
menuBar := setupMenuBar()
menuItem1 := setupMenuItem(glib.Local("About"))
menuBar.Append(menuItem1)
mainBox.PackStart(menuBar, false, true, 0)
// Setup box1
box1 := setupBox(gtk.ORIENTATION_HORIZONTAL)
label1 := setupLabel(glib.Local("Video Link"))
box1.PackStart(label1, false, true, 0)
entry1, err := gtk.EntryNew()
errorCheck(err)
box1.PackStart(entry1, true, true, 0)
mainBox.PackStart(box1, false, true, 0)
// Setup box2
box2 := setupBox(gtk.ORIENTATION_HORIZONTAL)
label2 := setupLabel(glib.Local("Target Dir"))
box2.PackStart(label2, false, true, 0)
fileButton, err := gtk.FileChooserButtonNew(glib.Local("Choose Dir"), gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER)
box2.PackStart(fileButton, false, true, 0)
startButton, err := gtk.ButtonNewWithLabel(glib.Local("Start"))
errorCheck(err)
startButton.SetMarginStart(256)
startButton.SetMarginEnd(256) // Set Margin to 256
box2.PackStart(startButton, true, true, 0)
mainBox.PackStart(box2, false, true, 0)
// Setup box3
box3 := setupBox(gtk.ORIENTATION_HORIZONTAL)
label3 := setupLabel(glib.Local("Download Progress"))
box3.PackStart(label3, false, true, 0)
progressBar, err := gtk.ProgressBarNew()
progressBar.SetShowText(true)
box3.PackStart(progressBar, true, true, 0)
mainBox.PackStart(box3, false, true, 0)
// Setup scrolled window
scrolledWindow, err := gtk.ScrolledWindowNew(nil, nil)
errorCheck(err)
textview := setupTextView()
scrolledWindow.Add(textview)
mainBox.PackStart(scrolledWindow, true, true, 0)
// Set Cur Folder
fileButton.SetCurrentFolder(exPath)
// Deal with signals
fileButton.Connect("current-folder-changed", func() {
DestFolder, err = fileButton.GetCurrentFolder()
errorCheck(err)
})
startButton.Connect("clicked", func() {
URL, err = entry1.GetText()
errorCheck(err)
DestFolder, err = fileButton.GetCurrentFolder()
errorCheck(err)
if checkURL(URL) {
AddText(textview, glib.Local("Download started"))
progressBar.SetFraction(0) // Reset ProgressBar
AddText(textview, fmt.Sprintf(glib.Local("annie-gtk is now downloading %s => %s"), URL, DestFolder))
// TODO: Download progress
output := &outputBuffer{
reader: nil,
textview: textview,
}
savedStdout := os.Stdout
r, w, _ := os.Pipe()
output.attachReader(r, textview)
os.Stdout = w
defaultDownloader, data, err := setupDownloader(nil, URL)
if err != nil {
AddText(textview, "annie-backend got error while setting up downloader: "+err.Error())
}
Site, Title, Type, Size, FileNameLength, stream, err := GetInfo(defaultDownloader, data)
if err != nil {
AddText(textview, "annie-backend got error: "+err.Error())
}
go func() {
var savedSize int64
for {
if len(stream.Parts) == 1 {
savedSize, err = GetSize(defaultDownloader, data, Title, FileNameLength, stream.Parts[0])
if err != nil {
AddText(textview, "GetSize error:"+err.Error())
}
} else {
savedSize = 0
for index, part := range stream.Parts {
partFileName := fmt.Sprintf("%s[%d]", Title, index)
partSize, err := GetSize(defaultDownloader, data, partFileName, FileNameLength, part)
if err != nil {
AddText(textview, "GetSize error (multi parts):"+err.Error())
}
// AddText(textview, fmt.Sprintf("partSize[%d]: %.2f MiB", index, float64(partSize)/(1024*1024)))
savedSize += partSize
}
}
if savedSize < Size {
// AddText(textview, fmt.Sprintf("Downloaded %.2f MiB/%.2f MiB", float64(savedSize)/(1024*1024), float64(Size)/(1024*1024)))
progressBar.SetFraction(float64(savedSize) / float64(Size))
progressBar.SetText(glib.Local("Downloaded") + fmt.Sprintf(" %.2f MiB/%.2f MiB", float64(savedSize)/(1024*1024), float64(Size)/(1024*1024)))
time.Sleep(500 * time.Millisecond)
}
// line, err := output.reader.ReadString('\n')
if err == nil || err == io.EOF {
// if line != "" {
// AddText(textview, "DEBUG: "+line)
// }
if err == io.EOF {
break
}
} else {
AddText(textview, err.Error())
break
}
// fmt.Fprint(savedStdout, line)
}
AddText(textview, "Download completed")
progressBar.SetText("Download completed")
progressBar.SetFraction(1)
}()
go func() {
AddText(textview, glib.Local("Downloading from: ")+Site)
AddText(textview, glib.Local("File title: ")+Title)
AddText(textview, glib.Local("File type: ")+Type)
AddText(textview, glib.Local("File size: ")+fmt.Sprintf("%.2f MiB (%d Bytes)\n", float64(Size)/(1024*1024), Size))
if err := Download(defaultDownloader, data); err != nil {
AddText(textview, "Got error while downloading: "+err.Error())
}
err := w.Close()
if err != nil {
AddText(textview, "Got error while closing the output: "+err.Error())
}
os.Stdout = savedStdout
}()
} else {
AddText(textview, glib.Local("You typed something. but not valid URL!"))
}
})
menuItem1.Connect("select", func(menuitem1 *gtk.MenuItem) {
about := About(exPath)
about.SetTransientFor(win)
about.Run()
about.Destroy()
})
// Launch the application
win.ShowAll()
}
// Modified from fanaticscripter/annie-mingui
func (b *outputBuffer) attachReader(r io.Reader, textview *gtk.TextView) {
// b.reader = bufio.NewReaderSize(r, bufio.MaxScanTokenSize)
b.reader = bufio.NewReader(r)
b.textview = textview
}
func setupWindow(title string, application *gtk.Application) *gtk.ApplicationWindow {
win, err := gtk.ApplicationWindowNew(application)
if err != nil {
log.Fatal("Unable to create window:", err)
}
win.SetTitle(title)
win.SetPosition(gtk.WIN_POS_CENTER)
win.SetDefaultSize(800, 600)
return win
}
func setupBox(orient gtk.Orientation) *gtk.Box {
box, err := gtk.BoxNew(orient, 2)
if err != nil {
log.Fatal("Unable to create box:", err)
}
return box
}
func setupLabel(text string) *gtk.Label {
label, err := gtk.LabelNew(text)
if err != nil {
log.Fatal("Unable to create label:", err)
}
return label
}
func setupMenuBar() *gtk.MenuBar {
menubar, err := gtk.MenuBarNew()
if err != nil {
log.Fatal("Unable to create MenuBar:", err)
}
return menubar
}
func setupMenuItem(label string) *gtk.MenuItem {
menuitem, err := gtk.MenuItemNewWithLabel(label)
if err != nil {
log.Fatal("Unable to create MenuItem:", err)
}
return menuitem
}
func setupTextView() *gtk.TextView {
tv, err := gtk.TextViewNew()
if err != nil {
log.Fatal("Unable to create TextView:", err)
}
return tv
}
func GetBuffer(tv *gtk.TextView) *gtk.TextBuffer {
buffer, err := tv.GetBuffer()
if err != nil {
log.Fatal("Unable to get buffer:", err)
}
return buffer
}
func AddText(tv *gtk.TextView, text string) {
// Add \n at the end of the message
text = time.Now().Format("15:04:05 ") + text + "\n"
buffer := GetBuffer(tv)
endIter := buffer.GetEndIter()
buffer.Insert(endIter, text)
}
func errorCheck(e error) {
if e != nil {
// panic for any errors.
log.Panic(e)
}
}
func checkURL(URL string) bool {
_, err := url.ParseRequestURI(URL)
return err == nil
}