// // rhlibrary // // The Radio Helsinki Rivendell Library // // // Copyright (C) 2016 Christian Pointner // // 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 . // package main import ( "sort" "sync" "code.helsinki.at/rhrd-go/player" "code.helsinki.at/rhrd-go/rddb" "github.com/gotk3/gotk3/glib" "github.com/gotk3/gotk3/gtk" ) var ( poolsMutex sync.Mutex pools rddb.PoolList poolsComboBox *gtk.ComboBoxText poolsCurrentGroup string ) func fetchPools(db *rddb.DBChan) (err error) { poolsMutex.Lock() defer poolsMutex.Unlock() if pools, err = db.GetPoolList(rhuser.Username); err != nil { return err } return } func updatePools(db *rddb.DBChan) (err error) { if err = fetchPools(db); err != nil { return } var keys []string for k, _ := range pools { keys = append(keys, k) } sort.Strings(keys) poolsComboBox.RemoveAll() for _, group := range keys { poolsComboBox.Append(group, pools[group].Description) } if !poolsComboBox.SetActiveID(poolsCurrentGroup) { poolsComboBox.SetActive(0) } return } func selectPool() rddb.PoolListEntry { id := poolsComboBox.GetActiveID() if id == "" { return rddb.PoolListEntry{} } poolsCurrentGroup = id poolsMutex.Lock() defer poolsMutex.Unlock() return pools[poolsCurrentGroup] } func addPoolComboBox(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("Musik-Pool auswählen"); err != nil { return } box.PackStart(label, false, false, 0) if poolsComboBox, err = gtk.ComboBoxTextNew(); err != nil { return } if err = updatePools(db); err != nil { return err } poolsComboBox.Connect("changed", func(_ *gtk.ComboBoxText) { pool := selectPool() if pool.Group == "" { return } glib.IdleAdd(func() { if err = updatePoolCartListRows(pool, db, p); err != nil { rhdl.Printf("error loading pool carts for %s: %v", pool.Group, err) return } }) rhdl.Printf("pool %s is now selected: %s", pool.Group, pool.Description) }) box.PackStart(poolsComboBox, true, true, 0) box.SetHExpand(true) grid.Attach(box, 1, 1, 1, 1) return } func updatePoolCartListRows(pool rddb.PoolListEntry, db *rddb.DBChan, p *player.PlayerChan) (err error) { carts, err := db.GetPoolCartList(pool) if err != nil { return err } for num, cart := range carts { rhdl.Printf("Pool Cart [%d]: %+v", num, cart) } return } func getPoolsSelectorWidget(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 = addPoolComboBox(grid, db, p); err != nil { return nil, err } // if err = addPoolCartList(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 }