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
|
//
// rhlibrary
//
// The Radio Helsinki Rivendell Library
//
//
// Copyright (C) 2016 Christian Pointner <equinox@helsinki.at>
//
// This file is part of rhlibrary.
//
// rhlibrary is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// rhlibrary is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with rhlibrary. If not, see <http://www.gnu.org/licenses/>.
//
package main
import (
"fmt"
"sort"
"sync"
"time"
"code.helsinki.at/rhrd-go/player"
"code.helsinki.at/rhrd-go/rddb"
"github.com/gotk3/gotk3/glib"
"github.com/gotk3/gotk3/gtk"
)
var (
jinglesMutex sync.Mutex
jingles rddb.JingleList
jinglesComboBox *gtk.ComboBoxText
jinglesCurrentGroup string
jinglesCutListBin *gtk.Bin
jinglesCutList *gtk.Grid
)
func fetchJingles(db *rddb.DBChan) (err error) {
jinglesMutex.Lock()
defer jinglesMutex.Unlock()
if jingles, err = db.GetJingleList(rhuser.Username); err != nil {
return err
}
return
}
func updateJingles(db *rddb.DBChan) (err error) {
if err = fetchJingles(db); err != nil {
return
}
var keys []string
for k, _ := range jingles {
keys = append(keys, k)
}
sort.Strings(keys)
jinglesComboBox.RemoveAll()
for _, group := range keys {
jinglesComboBox.Append(group, jingles[group].Description)
}
if !jinglesComboBox.SetActiveID(jinglesCurrentGroup) {
jinglesComboBox.SetActive(0)
}
return
}
func selectJingle() rddb.JingleListEntry {
id := jinglesComboBox.GetActiveID()
if id == "" {
return rddb.JingleListEntry{}
}
jinglesCurrentGroup = id
jinglesMutex.Lock()
defer jinglesMutex.Unlock()
return jingles[jinglesCurrentGroup]
}
func addJingleComboBox(grid *gtk.Grid, db *rddb.DBChan, p *player.PlayerChan) (err error) {
var box *gtk.Box
if box, err = gtk.BoxNew(gtk.ORIENTATION_HORIZONTAL, 10); err != nil {
return
}
var label *gtk.Label
if label, err = gtk.LabelNew("Jingle Gruppe auswählen"); err != nil {
return
}
box.PackStart(label, false, false, 0)
if jinglesComboBox, err = gtk.ComboBoxTextNew(); err != nil {
return
}
if err = updateJingles(db); err != nil {
return err
}
jinglesComboBox.Connect("changed", func(_ *gtk.ComboBoxText) {
jingle := selectJingle()
if jingle.Group == "" {
return
}
glib.IdleAdd(func() {
if err = updateJingleCutListRows(jingle, db, p); err != nil {
rhdl.Printf("error loading jingle cuts for %s", jingle.Group)
return
}
})
rhdl.Printf("jingle group %s is now selected: %s", jingle.Group, jingle.Description)
})
box.PackStart(jinglesComboBox, true, true, 0)
box.SetHExpand(true)
grid.Attach(box, 1, 1, 1, 1)
return
}
func getJingleCutListLabel(caption, css string) (label *gtk.Label, err error) {
if label, err = gtk.LabelNew(caption); err != nil {
return
}
if err = setCssStyle(&label.Widget, css); err != nil {
return
}
return
}
func getJingleCutListEntryLabel(caption string, idx int) (label *gtk.Label, err error) {
if (idx & 1) == 1 {
return getJingleCutListLabel(caption, ".label { background-color: #D2D2D2; padding: 7px 15px; }")
} else {
return getJingleCutListLabel(caption, ".label { padding: 7px 15px; }")
}
}
func getJingleCutListHeadLabel(caption string) (label *gtk.Label, err error) {
return getJingleCutListLabel(caption, `
.label {
border-bottom: 2px solid;
padding: 8px 15px;
font-weight: bold;
}`)
}
func addJingleCutListHead(idx int) (err error) {
var lcut *gtk.Label
if lcut, err = getJingleCutListHeadLabel("Cut #"); err != nil {
return
}
jinglesCutList.Attach(lcut, 1, idx, 1, 1)
var ltitle *gtk.Label
if ltitle, err = getJingleCutListHeadLabel("Titel"); err != nil {
return
}
ltitle.SetHExpand(true)
jinglesCutList.Attach(ltitle, 2, idx, 1, 1)
var lduration *gtk.Label
if lduration, err = getJingleCutListHeadLabel("Länge"); err != nil {
return
}
jinglesCutList.Attach(lduration, 3, idx, 1, 1)
var limported *gtk.Label
if limported, err = getJingleCutListHeadLabel("importiert"); err != nil {
return
}
jinglesCutList.Attach(limported, 4, idx, 1, 1)
var lplay *gtk.Label
if lplay, err = getJingleCutListHeadLabel("Play"); err != nil {
return
}
jinglesCutList.Attach(lplay, 5, idx, 1, 1)
return
}
func getJingleCutListEntryPlayButton(cart uint, cut rddb.CutListEntry, p *player.PlayerChan, idx int) (gtk.IWidget, error) {
btn, err := gtk.ButtonNewFromIconName("media-playback-start", gtk.ICON_SIZE_SMALL_TOOLBAR)
if err != nil {
return nil, err
}
btn.SetSizeRequest(35, 35)
var frame *gtk.Frame
if frame, err = gtk.FrameNew(""); err != nil {
return nil, err
}
css := ".frame { border: 0; padding: 4px 15px; }"
if (idx & 1) == 1 {
css = ".frame { border: 0; background-color: #D2D2D2; padding: 4px 15px; }"
}
if err = setCssStyle(&frame.Widget, css); err != nil {
return nil, err
}
frame.Add(btn)
btn.Connect("clicked", func(_ *gtk.Button) {
if err := p.Load(cart, cut.Number); err != nil {
rhdl.Println(err) // TODO: feedback at GUI?
return
}
if err := p.Play(); err != nil {
rhdl.Println(err) // TODO: feedback at GUI?
}
})
return frame, nil
}
func addJingleCutListEntry(idx int, cart uint, cut rddb.CutListEntry, p *player.PlayerChan) (err error) {
var lcut *gtk.Label
if lcut, err = getJingleCutListEntryLabel(fmt.Sprintf("%06d_%03d", cart, cut.Number), idx); err != nil {
return
}
jinglesCutList.Attach(lcut, 1, idx, 1, 1)
var ltitle *gtk.Label
if ltitle, err = getJingleCutListEntryLabel(cut.Description, idx); err != nil {
return
}
ltitle.SetHExpand(true)
jinglesCutList.Attach(ltitle, 2, idx, 1, 1)
duration := time.Unix(0, 0).UTC().Add(cut.Duration).Format("15:04:05.0")
imported := "-"
if cut.Imported.Valid {
imported = cut.Imported.Time.Format("Mon 02.01.2006 15:04:05")
}
var lduration *gtk.Label
if lduration, err = getJingleCutListEntryLabel(duration, idx); err != nil {
return
}
jinglesCutList.Attach(lduration, 3, idx, 1, 1)
var limported *gtk.Label
if limported, err = getJingleCutListEntryLabel(imported, idx); err != nil {
return
}
jinglesCutList.Attach(limported, 4, idx, 1, 1)
var btn gtk.IWidget
if btn, err = getJingleCutListEntryPlayButton(cart, cut, p, idx); err != nil {
return
}
jinglesCutList.Attach(btn, 5, idx, 1, 1)
return
}
func addJingleCutListSpacer(idx int) (err error) {
var ltitle *gtk.Label
if ltitle, err = getJingleCutListHeadLabel("deaktivirt"); err != nil {
return
}
ltitle.SetHExpand(true)
jinglesCutList.Attach(ltitle, 1, idx, 5, 1)
return
}
func updateJingleCutListRows(jingle rddb.JingleListEntry, db *rddb.DBChan, p *player.PlayerChan) (err error) {
if jinglesCutList != nil {
jinglesCutListBin.Remove(jinglesCutList)
}
if jinglesCutList, err = gtk.GridNew(); err != nil {
return
}
jinglesCutList.SetHExpand(true)
jinglesCutList.SetVExpand(true)
activeCart, inactiveCart, err := db.GetJingleCutList(jingle)
if err != nil {
return err
}
idx := 1
addJingleCutListHead(idx)
for _, cut := range activeCart.Cuts {
idx++
addJingleCutListEntry(idx, activeCart.Number, cut, p)
}
if len(inactiveCart.Cuts) > 0 {
idx++
addJingleCutListSpacer(idx)
for _, cut := range inactiveCart.Cuts {
idx++
addJingleCutListEntry(idx, inactiveCart.Number, cut, p)
}
}
jinglesCutListBin.Add(jinglesCutList)
jinglesCutList.ShowAll()
return
}
func addJingleCutList(grid *gtk.Grid, db *rddb.DBChan, p *player.PlayerChan) (err error) {
var frame *gtk.Frame
if frame, err = gtk.FrameNew(""); err != nil {
return
}
jinglesCutListBin = &frame.Bin
if err = setCssStyle(&frame.Widget, ".frame { border: 0; }"); err != nil {
return
}
var sw *gtk.ScrolledWindow
if sw, err = gtk.ScrolledWindowNew(nil, nil); err != nil {
return
}
sw.SetHExpand(true)
sw.SetVExpand(true)
sw.Add(frame)
grid.Attach(sw, 1, 2, 1, 1)
return
}
func getJinglesSelectorWidget(db *rddb.DBChan, p *player.PlayerChan) (gtk.IWidget, error) {
grid, err := gtk.GridNew()
if err != nil {
return nil, err
}
grid.SetRowSpacing(25)
grid.SetHExpand(true)
grid.SetVExpand(true)
if err = addJingleComboBox(grid, db, p); err != nil {
return nil, err
}
if err = addJingleCutList(grid, db, p); err != nil {
return nil, err
}
var frame *gtk.Frame
if frame, err = gtk.FrameNew(""); err != nil {
return nil, err
}
if err = setCssStyle(&frame.Widget, ".frame { border: 0; padding: 25px 42px; }"); err != nil {
return nil, err
}
frame.Add(grid)
return frame, nil
}
|