-
Notifications
You must be signed in to change notification settings - Fork 1
/
PopupMenu.go
93 lines (79 loc) · 2.01 KB
/
PopupMenu.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
package main
import (
"os/exec"
"github.com/conformal/gotk3/glib"
"github.com/conformal/gotk3/gtk"
)
type PopupMenu struct {
parent *ChatWindow
menu *gtk.Menu
view *gtk.MenuItem
download *gtk.MenuItem
}
func NewPopupMenu(parent *ChatWindow, msgId, url, ext string) *PopupMenu {
menu := &PopupMenu{parent: parent}
menu.setupWidgets()
menu.setupDefault(msgId, url, ext)
return menu
}
func (self *PopupMenu) setupWidgets() {
var err error
self.menu, err = gtk.MenuNew()
if err != nil {
goline.LoggerPanicln(err)
}
self.view, err = gtk.MenuItemNew()
if err != nil {
goline.LoggerPanicln(err)
}
self.view.SetLabel("View")
self.download, err = gtk.MenuItemNew()
if err != nil {
goline.LoggerPanicln(err)
}
self.download.SetLabel("Download")
self.menu.Append(self.view)
self.menu.Append(self.download)
self.menu.ShowAll()
}
func (self *PopupMenu) setupDefault(msgId, url, ext string) {
_, err := self.view.Connect("activate", func() {
self.viewFile(msgId, url, ext)
})
if err != nil {
goline.LoggerPanicln(err)
}
_, err = self.download.Connect("activate", func() {
self.downloadFile(msgId, url, ext)
})
if err != nil {
goline.LoggerPanicln(err)
}
}
func (self *PopupMenu) downloadFile(msgId, url, ext string) {
go func() {
glib.IdleAdd(func() {
NewFileChooserWindow(self.parent, msgId, url, ext).window.ShowAll()
})
}()
}
func (self *PopupMenu) viewFile(msgId, url, ext string) {
go func() {
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
}
cmd := exec.Command("xdg-open", content)
err = cmd.Start()
if err != nil {
goline.LoggerPrintln(err)
glib.IdleAdd(func() { RunAlertMessage(self.parent.window, "Failed to open file.") })
return
}
}()
}