/
m_file.go
248 lines (209 loc) · 5.67 KB
/
m_file.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
package mcontent
import (
"context"
"errors"
"fmt"
"html"
"log"
"strings"
"github.com/diamondburned/gotk4/pkg/core/glib"
"github.com/diamondburned/gotk4/pkg/gtk/v4"
"github.com/diamondburned/gotk4/pkg/pango"
"github.com/diamondburned/gotkit/app/locale"
"github.com/diamondburned/gotkit/gtkutil/cssutil"
"github.com/diamondburned/gotktrix/internal/components/filepick"
"github.com/diamondburned/gotktrix/internal/components/progress"
"github.com/diamondburned/gotktrix/internal/gotktrix"
"github.com/diamondburned/gotrix/event"
"github.com/dustin/go-humanize"
)
// linkContent is a lighter version of fileContent.
type linkContent struct {
*gtk.Label
}
func (c *linkContent) content() {}
type fileContent struct {
*gtk.Box
ctx context.Context
info *fileInfo
brev *gtk.Revealer
url string
name string
size int
}
type fileInfo struct {
*gtk.Box
icon *gtk.Image
right struct {
*gtk.Box
name *gtk.Label
size *gtk.Label
}
action *gtk.Button
click glib.SignalHandle
}
var fileCSS = cssutil.Applier("mcontent-file", `
.mcontent-file {
padding: 4px 6px;
}
.mcontent-file-info > image {
padding-right: 6px;
}
.mcontent-file-size {
font-size: 0.85rem;
margin-top: -2px;
}
`)
func newFileContent(ctx context.Context, msg *event.RoomMessageEvent) contentPart {
client := gotktrix.FromContext(ctx)
url, _ := client.MediaDownloadURL(msg.URL, true, "")
info, err := msg.FileInfo()
if err != nil {
// File has no info for some reason. Just render the URL as a fallback.
c := linkContent{}
c.Label = gtk.NewLabel(fmt.Sprintf(
`<a href="%s">%s</a>`,
html.EscapeString(url), html.EscapeString(msg.Body),
))
c.Label.SetUseMarkup(true)
c.Label.SetEllipsize(pango.EllipsizeMiddle)
c.Label.SetXAlign(0)
c.AddCSSClass("mcontent-file-fallback")
return &c
}
c := fileContent{
ctx: ctx,
url: url,
name: msg.Body,
size: info.Size,
}
c.info = newFileInfo(info, c.name, c.url)
c.info.setAction(fileDownload(c.download))
c.brev = gtk.NewRevealer()
c.brev.AddCSSClass("mcontent-file-progress-revealer")
c.brev.SetTransitionType(gtk.RevealerTransitionTypeSlideDown)
c.brev.SetRevealChild(false)
c.Box = gtk.NewBox(gtk.OrientationVertical, 0)
c.AddCSSClass("frame")
c.SetHAlign(gtk.AlignStart)
c.Append(c.info)
c.Append(c.brev)
fileCSS(c.Box)
return &c
}
func (c *fileContent) download() {
chooser := filepick.New(
c.ctx, "Download File",
gtk.FileChooserActionSave,
locale.S(c.ctx, "Download"),
locale.S(c.ctx, "Cancel"),
)
chooser.SetCurrentName(c.name)
chooser.ConnectAccept(func() {
if path := chooser.File().Path(); path != "" {
c.downloadTo(path)
}
})
chooser.Show()
}
func (c *fileContent) downloadTo(path string) {
bar := progress.NewBar()
bar.SetMax(int64(c.size))
bar.SetLabelFunc(func(n, max int64) string {
return fmt.Sprintf(
"%s (%.0f%%)",
humanize.Bytes(uint64(n)), float64(n)/float64(max)*100,
)
})
c.brev.SetChild(bar)
c.brev.SetRevealChild(true)
ctx, cancel := context.WithCancel(c.ctx)
c.info.setAction(fileStopDownload(cancel))
go func() {
err := progress.Download(ctx, c.url, path, bar)
cancel()
glib.IdleAdd(func() {
c.info.setAction(fileDownload(c.download))
// Pretend that cancelling the context is not an error.
if err == nil || errors.Is(err, context.Canceled) {
c.brev.SetRevealChild(false)
bar.Unparent()
}
})
}()
}
func newFileInfo(info event.FileInfo, name, url string) *fileInfo {
icon := "text-x-generic-symbolic"
if info.MimeType != "" {
switch strings.Split(info.MimeType, "/")[0] {
case "application":
icon = "package-x-generic-symbolic"
case "image":
icon = "image-x-generic-symbolic"
case "video":
icon = "video-x-generic-symbolic"
case "audio":
icon = "audio-x-generic-symbolic"
}
}
inf := fileInfo{}
inf.icon = gtk.NewImageFromIconName(icon)
inf.icon.SetIconSize(gtk.IconSizeLarge)
inf.right.name = gtk.NewLabel(fmt.Sprintf(
`<a href="%s">%s</a>`,
html.EscapeString(url), html.EscapeString(name),
))
inf.right.name.SetUseMarkup(true)
inf.right.name.AddCSSClass("mcontent-file-name")
inf.right.name.SetEllipsize(pango.EllipsizeMiddle)
inf.right.name.SetXAlign(0)
inf.right.Box = gtk.NewBox(gtk.OrientationVertical, 0)
inf.right.Box.SetHExpand(true)
inf.right.Box.SetVAlign(gtk.AlignCenter)
inf.right.Box.Append(inf.right.name)
if info.Size > 0 {
inf.right.size = gtk.NewLabel(humanize.Bytes(uint64(info.Size)))
inf.right.size.AddCSSClass("mcontent-file-size")
inf.right.size.SetEllipsize(pango.EllipsizeMiddle)
inf.right.size.SetXAlign(0)
inf.right.Box.Append(inf.right.size)
}
inf.action = gtk.NewButton()
inf.action.SetVAlign(gtk.AlignCenter)
inf.action.SetSensitive(false)
inf.Box = gtk.NewBox(gtk.OrientationHorizontal, 0)
inf.AddCSSClass("mcontent-file-info")
inf.SetSizeRequest(maxWidth, -1)
inf.Append(inf.icon)
inf.Append(inf.right)
inf.Append(inf.action)
return &inf
}
type fileAction interface{ _action() }
type fileDownload func()
type fileStopDownload context.CancelFunc
func (f fileDownload) _action() {}
func (f fileStopDownload) _action() {}
func (inf *fileInfo) setAction(action fileAction) {
if inf.click > 0 {
inf.action.HandlerDisconnect(inf.click)
inf.click = 0
}
inf.action.SetSensitive(true)
switch action := action.(type) {
case fileDownload:
inf.action.SetTooltipText("Download")
inf.action.SetIconName("folder-download-symbolic")
inf.click = inf.action.ConnectClicked(action)
case fileStopDownload:
inf.action.SetTooltipText("Stop")
inf.action.SetIconName("process-stop-symbolic")
inf.click = inf.action.ConnectClicked(func() {
action()
inf.action.SetSensitive(false)
})
default:
log.Panicf("unknown action %T", action)
}
}
func (c *fileContent) content() {}