-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
681 lines (593 loc) · 16.2 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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
package main
import (
"bufio"
"errors"
"fmt"
"github.com/gotk3/gotk3/glib"
"github.com/gotk3/gotk3/gtk"
"gopkg.in/yaml.v3"
"log"
"net/http"
"os"
"path/filepath"
"time"
)
const (
ColumnName = iota
ColumnSize
ColumnProgress
)
const appId = "com.github.gotk3.gotk3-examples.glade"
type OutFile struct {
Name string
Iter *gtk.TreeIter
IsDone bool
}
var files = make([]OutFile, 0)
var win *gtk.ApplicationWindow
var treeStore *gtk.ListStore
var buttonConnect *gtk.Button
var buttonCancel *gtk.Button
var buttonSettings *gtk.Button
var headerBar *gtk.HeaderBar
var config Config
type Config struct {
Client struct {
Host string `yaml:"host"`
Port string `yaml:"port"`
} `yaml:"client"`
Server struct {
Listen bool `yaml:"listen"`
Port string `yaml:"port"`
Directory string `yaml:"directory"`
} `yaml:"server"`
}
func main() {
loadConfig()
// Create a new application.
application, err := gtk.ApplicationNew(appId, glib.APPLICATION_FLAGS_NONE)
failOnError(err)
// Connect function to application startup event, this is not required.
_ = application.Connect("startup", func() {
log.Println("application startup")
})
// Connect function to application activate event
_ = application.Connect("activate", func() {
log.Println("application activate")
// Get the GtkBuilder UI definition in the glade file.
builder, err := gtk.BuilderNewFromFile("ui/sfn-main.ui")
failOnError(err)
// Map the handlers to callback functions, and connect the signals
// to the Builder.
signals := map[string]interface{}{
"on_main_window_destroy": onMainWindowDestroy,
}
builder.ConnectSignals(signals)
// Get the object with the id of "main_window".
obj, err := builder.GetObject("dialog_main")
failOnError(err)
// Verify that the object is a pointer to a gtk.ApplicationWindow.
win, err = isApplicationWindow(obj)
failOnError(err)
obj, err = builder.GetObject("header")
failOnError(err)
headerBar, err = isHeader(obj)
failOnError(err)
obj, err = builder.GetObject("button_connect")
failOnError(err)
buttonConnect, err = isButton(obj)
failOnError(err)
obj, err = builder.GetObject("button_cancel")
failOnError(err)
buttonCancel, err = isButton(obj)
failOnError(err)
obj, err = builder.GetObject("button_import")
failOnError(err)
buttonImport, err := isButton(obj)
failOnError(err)
obj, err = builder.GetObject("button_settings")
failOnError(err)
buttonSettings, err = isButton(obj)
failOnError(err)
obj, err = builder.GetObject("tree_files")
failOnError(err)
tree, err := isTreeView(obj)
failOnError(err)
tree.AppendColumn(createTextColumn("File Name", ColumnName))
tree.AppendColumn(createTextColumn("File Size", ColumnSize))
tree.AppendColumn(createProgressColumn("Progress", ColumnProgress))
// Creating a tree store. This is what holds the data that will be shown on our tree view.
treeStore, err = gtk.ListStoreNew(glib.TYPE_STRING, glib.TYPE_STRING, glib.TYPE_INT)
if err != nil {
log.Fatal("Unable to create tree store:", err)
}
tree.SetModel(treeStore)
_ = buttonConnect.Connect("clicked", func() {
builder, err := gtk.BuilderNewFromFile("ui/sfn-popover.ui")
failOnError(err)
obj, err = builder.GetObject("connect_popover")
failOnError(err)
popover, err := isPopover(obj)
failOnError(err)
obj, err = builder.GetObject("connect_host")
failOnError(err)
hostEntry, err := isEntry(obj)
failOnError(err)
hostEntry.SetText(config.Client.Host)
obj, err = builder.GetObject("connect_port")
failOnError(err)
portEntry, err := isEntry(obj)
failOnError(err)
portEntry.SetText(config.Client.Port)
obj, err = builder.GetObject("connect_button")
failOnError(err)
button, err := isButton(obj)
failOnError(err)
_ = button.Connect("clicked", func() {
host, err := hostEntry.GetText()
failOnError(err)
port, err := portEntry.GetText()
failOnError(err)
go func() {
config.Client.Host = host
config.Client.Port = port
saveConfig()
SwitchConnectionButton(true)
err := RunClient(host, port)
if err != nil {
showError("Failed to connect to %s:%s", host, port)
}
}()
})
validateFunc := func() {
log.Println("host/port changed")
h, err := hostEntry.GetText()
failOnError(err)
p, err := portEntry.GetText()
failOnError(err)
button.SetSensitive(len(h) > 0 && len(p) > 0)
}
validateFunc()
_ = hostEntry.Connect("changed", validateFunc)
_ = portEntry.Connect("changed", validateFunc)
popover.SetRelativeTo(buttonConnect)
popover.Show()
})
_ = buttonCancel.Connect("clicked", func() {
err := Disconnect()
if err != nil {
showError("Unable to disconnect")
return
}
SetSubtitle("")
SwitchConnectionButton(false)
StartServerAsync()
})
_ = buttonImport.Connect("clicked", func() {
dialog, err := gtk.FileChooserNativeDialogNew(
"Choose the file to send",
win,
gtk.FILE_CHOOSER_ACTION_OPEN,
"Open",
"Cancel",
)
failOnError(err)
dialog.SetModal(true)
dialog.SetSelectMultiple(true)
v := dialog.Run()
if v == int(gtk.RESPONSE_ACCEPT) {
list, err := dialog.GetFilenames()
if err != nil {
log.Println("unable to choose files")
return
}
for _, name := range list {
log.Println("open:", name)
base := filepath.Base(name)
stat, err := os.Stat(name)
if err != nil {
log.Println("unable to get file info")
return
}
iter := addRow(treeStore, base, ByteCountBinary(stat.Size()))
files = append(files, OutFile{Name: name, Iter: iter, IsDone: false})
}
}
})
_ = buttonSettings.Connect("clicked", func() {
builder, err := gtk.BuilderNewFromFile("ui/sfn-settings.ui")
failOnError(err)
obj, err = builder.GetObject("settings_popover")
failOnError(err)
popover, err := isPopover(obj)
failOnError(err)
obj, err = builder.GetObject("host_switch")
failOnError(err)
hostSwitch, err := isSwitch(obj)
failOnError(err)
hostSwitch.SetActive(config.Server.Listen)
obj, err = builder.GetObject("host_port")
failOnError(err)
portEntry, err := isEntry(obj)
failOnError(err)
portEntry.SetText(config.Server.Port)
portEntry.SetSensitive(config.Server.Listen)
obj, err = builder.GetObject("incoming_dir")
failOnError(err)
dirEntry, err := isEntry(obj)
failOnError(err)
dirEntry.SetText(config.Server.Directory)
obj, err = builder.GetObject("select_dir")
failOnError(err)
selectDirButton, err := isButton(obj)
failOnError(err)
_ = selectDirButton.Connect("clicked", func() {
dialog, err := gtk.FileChooserNativeDialogNew("Select incoming files directory", win, gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER, "Select", "Cancel")
failOnError(err)
dialog.SetModal(true)
v := dialog.Run()
if v == int(gtk.RESPONSE_ACCEPT) {
name := dialog.GetFilename()
log.Println("select dir:", name)
config.Server.Directory = name
dirEntry.SetText(config.Server.Directory)
go func() { saveConfig() }()
}
})
_ = hostSwitch.Connect("state-set", func() {
active := hostSwitch.GetActive()
portEntry.SetSensitive(active)
if !active {
go func() {
time.Sleep(250 * time.Millisecond)
popover.Hide()
}()
}
})
_ = popover.Connect("closed", func() {
log.Println("settings closed")
l := hostSwitch.GetActive()
p, err := portEntry.GetText()
failOnError(err)
dir, err := dirEntry.GetText()
failOnError(err)
config.Server.Directory = dir
if l != config.Server.Listen || p != config.Server.Port {
config.Server.Listen = l
config.Server.Port = p
go func() {
StopServer()
SwitchConnectionButton(false)
if config.Server.Listen {
StartServerAsync()
}
}()
}
go func() { saveConfig() }()
})
popover.SetRelativeTo(buttonSettings)
popover.Show()
})
failOnError(err)
// Show the Window and all of its components.
win.Show()
application.AddWindow(win)
StartServerAsync()
})
// Connect function to application shutdown event, this is not required.
_ = application.Connect("shutdown", func() {
log.Println("application shutdown")
})
// Launch the application
os.Exit(application.Run(os.Args))
}
func loadConfig() {
loaded := false
f, err := os.Open("config.yml")
if err == nil {
//noinspection GoUnhandledErrorResult
defer f.Close()
var cfg Config
decoder := yaml.NewDecoder(f)
err = decoder.Decode(&cfg)
if err == nil {
config = cfg
loaded = true
}
}
if !loaded {
ex, err := os.Executable()
if err != nil {
panic(err)
}
current := filepath.Dir(ex)
config.Client.Host = ""
config.Client.Port = "3214"
config.Server.Listen = false
config.Server.Port = "3214"
config.Server.Directory = current
}
}
func saveConfig() {
f, err := os.Create("config.yml")
if err != nil {
log.Println("unable to create config file")
return
}
//noinspection GoUnhandledErrorResult
defer f.Close()
encoder := yaml.NewEncoder(f)
err = encoder.Encode(config)
//noinspection GoUnhandledErrorResult
defer encoder.Close()
if err != nil {
log.Println("unable to save config file")
return
}
}
func StartServerAsync() {
go func() {
log.Println("server start")
for StartServer() {
log.Println("restart server")
}
log.Println("server stopped")
SetSubtitle("")
}()
}
func StartServer() bool {
if !config.Server.Listen {
return false
}
ip, err := GetIpAddr()
if err != nil {
ip = "Listening on port " + config.Server.Port
} else {
ip = "Listening on " + ip + ":" + config.Server.Port
}
log.Println("server ip", ip)
SetSubtitle(ip)
ip, err = Listen(config.Server.Port)
if err != nil {
log.Println("listening failed")
return false
}
ip = "Connected to " + ip
SetSubtitle(ip)
SwitchConnectionButton(true)
ReceiveFiles()
SendFiles()
_ = Disconnect()
StopServer()
SwitchConnectionButton(false)
return true
}
func StopServer() {
err := Disconnect()
if err != nil {
log.Println("unable to disconnect")
}
err = StopListen()
if err != nil {
log.Println("unable to stop listening")
}
}
func RunClient(host string, port string) error {
StopServer()
address := host + ":" + port
log.Println("connect to", address)
SetSubtitle("Connecting to " + address)
ip, err := Connect(address)
if err != nil {
log.Println("unable to connect")
} else {
ip = "Connected to " + address
SetSubtitle(ip)
SendFiles()
ReceiveFiles()
_ = Disconnect()
}
SwitchConnectionButton(false)
StartServerAsync()
return err
}
func GetIpAddr() (string, error) {
resp, err := http.Get("http://tomclaw.com/services/simple/getip.php")
if err != nil {
return "", err
}
//noinspection GoUnhandledErrorResult
defer resp.Body.Close()
reader := bufio.NewReader(resp.Body)
line, _, err := reader.ReadLine()
if err != nil {
return "", err
}
return string(line), nil
}
func ReceiveFiles() {
var iter *gtk.TreeIter
for {
more, err := ReadFile(config.Server.Directory, func(name string, size int64) {
iter = addRow(treeStore, name, ByteCountBinary(size))
}, func(p int) {
err := treeStore.SetValue(iter, ColumnProgress, p)
if err != nil {
log.Fatal("unable set value:", err)
}
})
if err != nil {
showError("File receiving error")
break
}
if !more {
log.Println("done receiving files")
break
}
log.Println("receive next file")
}
}
func SendFiles() {
var err error
if len(files) > 0 {
for _, outFile := range files {
if outFile.IsDone {
continue
}
err = SendFile(outFile.Name, func(p int) {
err := treeStore.SetValue(outFile.Iter, ColumnProgress, p)
if err != nil {
log.Fatal("unable set value:", err)
}
})
if err != nil {
break
}
outFile.IsDone = true
}
}
if err == nil {
err = SendDone()
}
if err != nil {
showError("File sending error")
}
}
func SwitchConnectionButton(connected bool) {
glib.IdleAdd(func() { buttonCancel.SetVisible(connected) })
glib.IdleAdd(func() { buttonConnect.SetVisible(!connected) })
glib.IdleAdd(func() { buttonSettings.SetVisible(!connected) })
}
func SetSubtitle(subtitle string) {
glib.IdleAdd(func() { headerBar.SetSubtitle(subtitle) })
}
func isApplicationWindow(obj glib.IObject) (*gtk.ApplicationWindow, error) {
// Make type assertion (as per gtk.go).
if win, ok := obj.(*gtk.ApplicationWindow); ok {
return win, nil
}
return nil, errors.New("not a *gtk.Window")
}
func isHeader(obj glib.IObject) (*gtk.HeaderBar, error) {
// Make type assertion (as per gtk.go).
if headerBar, ok := obj.(*gtk.HeaderBar); ok {
return headerBar, nil
}
return nil, errors.New("not a *gtk.HeaderBar")
}
func isPopover(obj glib.IObject) (*gtk.Popover, error) {
// Make type assertion (as per gtk.go).
if popover, ok := obj.(*gtk.Popover); ok {
return popover, nil
}
return nil, errors.New("not a *gtk.Popover")
}
func isSwitch(obj glib.IObject) (*gtk.Switch, error) {
// Make type assertion (as per gtk.go).
if switchWidget, ok := obj.(*gtk.Switch); ok {
return switchWidget, nil
}
return nil, errors.New("not a *gtk.Switch")
}
func isEntry(obj glib.IObject) (*gtk.Entry, error) {
// Make type assertion (as per gtk.go).
if entry, ok := obj.(*gtk.Entry); ok {
return entry, nil
}
return nil, errors.New("not a *gtk.Entry")
}
func isButton(obj glib.IObject) (*gtk.Button, error) {
// Make type assertion (as per gtk.go).
if button, ok := obj.(*gtk.Button); ok {
return button, nil
}
return nil, errors.New("not a *gtk.Button")
}
func isTreeView(obj glib.IObject) (*gtk.TreeView, error) {
// Make type assertion (as per gtk.go).
if tree, ok := obj.(*gtk.TreeView); ok {
return tree, nil
}
return nil, errors.New("not a *gtk.TreeView")
}
// Add a column to the tree view (during the initialization of the tree view)
// We need to distinct the type of data shown in either column.
func createTextColumn(title string, id int) *gtk.TreeViewColumn {
cellRenderer, err := gtk.CellRendererTextNew()
if err != nil {
log.Fatal("Unable to create text cell renderer:", err)
}
column, err := gtk.TreeViewColumnNewWithAttribute(title, cellRenderer, "text", id)
if err != nil {
log.Fatal("Unable to create cell column:", err)
}
return column
}
// Add a column to the tree view (during the initialization of the tree view)
// We need to distinct the type of data shown in either column.
func createProgressColumn(title string, id int) *gtk.TreeViewColumn {
// In this column we want to show text, hence create a text renderer
cellRenderer, err := gtk.CellRendererProgressNew()
if err != nil {
log.Fatal("Unable to create progress cell renderer:", err)
}
// Tell the renderer where to pick input from. Text renderer understands
// the "int" property.
column, err := gtk.TreeViewColumnNewWithAttribute(title, cellRenderer, "value", id)
if err != nil {
log.Fatal("Unable to create cell column:", err)
}
return column
}
// Append a toplevel row to the tree store for the tree view
func addRow(treeStore *gtk.ListStore, name string, size string) *gtk.TreeIter {
// Get an iterator for a new row at the end of the list store
i := treeStore.Append()
// Set the contents of the tree store row that the iterator represents
err := treeStore.SetValue(i, ColumnName, name)
if err != nil {
log.Fatal("Unable set value:", err)
}
err = treeStore.SetValue(i, ColumnSize, size)
if err != nil {
log.Fatal("Unable set value:", err)
}
err = treeStore.SetValue(i, ColumnProgress, 0)
if err != nil {
log.Fatal("Unable set value:", err)
}
return i
}
func failOnError(e error) {
if e != nil {
// panic for any errors.
log.Panic(e)
}
}
func showError(format string, a ...interface{}) {
glib.IdleAdd(func() {
dialog := gtk.MessageDialogNew(win, gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR, gtk.BUTTONS_CLOSE, format, a...)
_ = dialog.Connect("response", func() {
dialog.Hide()
})
dialog.Show()
})
}
// onMainWindowDestroy is the callback that is linked to the
// on_main_window_destroy handler. It is not required to map this,
// and is here to simply demo how to hook-up custom callbacks.
func onMainWindowDestroy() {
log.Println("onMainWindowDestroy")
}
func ByteCountBinary(b int64) string {
const unit = 1024
if b < unit {
return fmt.Sprintf("%d B", b)
}
div, exp := int64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %ciB", float64(b)/float64(div), "KMGTPE"[exp])
}