-
Notifications
You must be signed in to change notification settings - Fork 1
/
FileChooserWindow.go
62 lines (56 loc) · 1.82 KB
/
FileChooserWindow.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
package main
import (
"github.com/carylorrk/goline-gotk3/res/glade"
"github.com/conformal/gotk3/gtk"
"github.com/mattn/go-gtk/glib"
"os"
"path"
)
type FileChooserWindow struct {
*LineWindow
parent *ChatWindow
fileChooser *gtk.FileChooserWidget
save *gtk.Button
cancel *gtk.Button
}
func NewFileChooserWindow(parent *ChatWindow, msgId, url, ext string) *FileChooserWindow {
window := &FileChooserWindow{
parent: parent,
LineWindow: NewLineWindow(glade.FileChooserWindow)}
window.setupWidgets()
window.setupDefult(msgId, url, ext)
return window
}
func (self *FileChooserWindow) setupWidgets() {
self.fileChooser = self.getIObjectWithType("FileChooser", >k.FileChooserWidget{}).(*gtk.FileChooserWidget)
self.save = self.getIObjectWithType("Save", >k.Button{}).(*gtk.Button)
self.cancel = self.getIObjectWithType("Cancel", >k.Button{}).(*gtk.Button)
}
func (self *FileChooserWindow) setupDefult(msgId, url, ext string) {
self.Connect(self.save, "clicked", func() {
dirname := self.fileChooser.GetFilename()
if dirname != "" {
go func() {
glib.IdleAdd(self.window.Destroy)
downloadingWindow := NewDownloadingWindow(msgId)
glib.IdleAdd(downloadingWindow.window.ShowAll)
defer glib.IdleAdd(downloadingWindow.window.Destroy)
content, err := downloadContentToTemp(msgId, url, ext)
if err != nil {
goline.LoggerPrintln(err)
glib.IdleAdd(func() { RunAlertMessage(self.parent.window, "Failed to download file: %s", err) })
return
}
err = os.Rename(content, path.Join(dirname, path.Base(content)))
if err != nil {
goline.LoggerPrintln(err)
glib.IdleAdd(func() { RunAlertMessage(self.parent.window, "Failed to move file: %s", err) })
return
}
}()
}
})
self.Connect(self.cancel, "clicked", func() {
glib.IdleAdd(self.window.Destroy)
})
}