Files
lambda/gtk-helpers.go

452 lines
11 KiB
Go

package main
import (
"context"
"fmt"
"github.com/diamondburned/gotk4/pkg/gdk/v4"
"github.com/diamondburned/gotk4/pkg/glib/v2"
"github.com/diamondburned/gotk4/pkg/gtk/v4"
"github.com/diamondburned/gotk4/pkg/pango"
"gosrc.io/xmpp/stanza"
)
func scrollToBottomAfterUpdate(scrolledWindow *gtk.ScrolledWindow) {
glib.IdleAdd(func() bool {
vAdj := scrolledWindow.VAdjustment()
vAdj.SetValue(vAdj.Upper() - vAdj.PageSize())
return false // Return false to run only once
})
}
func createTab(jid string, isMuc bool) bool {
fmt.Println("Creating tab", jid, "isMuc:", isMuc)
_, ok := tabs.Load(jid)
_, uok := userdevices.Load(jid)
_, mok := mucmembers.Load(jid)
if !ok && !uok && !mok {
newTab := new(chatTab)
newTab.isMuc = isMuc
newTab.msgs = gtk.NewListBox()
newTab.msgs.SetVExpand(true)
newTab.msgs.SetShowSeparators(true)
newTab.msgs.Append(gtk.NewButtonWithLabel("Get past messages..."))
tabs.Store(jid, newTab)
return true
}
return false
}
func switchToTab(jid string, w *gtk.Window) {
current = jid
tab, ok := tabs.Load(current)
if !ok {
return
}
typed_tab := tab.(*chatTab)
scroller.SetChild(typed_tab.msgs)
if typed_tab.isMuc {
m, ok := mucmembers.Load(jid)
if !ok {
return
}
ma, ok := m.(mucUnit)
if !ok {
return
}
mm := ma.Members
gen := gtk.NewBox(gtk.OrientationVertical, 0)
i := 0
mm.Range(func(k, v any) bool {
i++
userbox := gtk.NewBox(gtk.OrientationHorizontal, 0)
u := v.(stanza.Presence)
var mu MucUser
var ocu OccupantID
u.Get(&mu)
u.Get(&ocu)
//id := ocu.ID
//if id == "" {
id := JidMustParse(u.From).Resource
//}
nick_label := gtk.NewLabel(JidMustParse(u.From).Resource)
nick_label.SetEllipsize(pango.EllipsizeEnd)
nick_label.AddCSSClass(mu.MucUserItem.Role)
if mu.MucUserItem.Role == "visitor" {
nick_label.SetOpacity(0.5)
}
userbox.SetTooltipText(fmt.Sprintf("%s\n%s\n%s\nClick for more information", u.From, mu.MucUserItem.Role, mu.MucUserItem.Affiliation))
userbox.Append(nick_label)
var hats Hats
ok := u.Get(&hats)
if ok {
for _, hat := range hats.Hats {
tag := gtk.NewImageFromPaintable(clientAssets["tag"])
tag.SetTooltipText(hat.Title)
userbox.Prepend(tag)
}
}
status := gtk.NewImageFromPaintable(clientAssets["status_"+string(u.Show)])
status.SetTooltipText(string(u.Show))
status.SetHAlign(gtk.AlignEnd)
// medal.SetHExpand(true)
userbox.Prepend(status)
medal := gtk.NewImageFromPaintable(clientAssets[mu.MucUserItem.Affiliation])
medal.SetTooltipText(mu.MucUserItem.Affiliation)
medal.SetHAlign(gtk.AlignEnd)
medal.SetHExpand(true)
userbox.Append(medal)
gesture := gtk.NewGestureClick()
gesture.SetButton(1)
mod_gesture := gtk.NewGestureClick()
mod_gesture.SetButton(3)
popover := gtk.NewPopover()
popover.SetHasArrow(false)
popover.SetParent(userbox)
rc_box := gtk.NewBox(gtk.OrientationVertical, 0)
bb := gtk.NewButtonWithLabel("Ban")
kb := gtk.NewButtonWithLabel("Kick")
ab := gtk.NewButtonWithLabel("Set affil")
rb := gtk.NewButtonWithLabel("Set role")
kb.ConnectClicked(func() {
client.SendRaw(fmt.Sprintf(`
<iq from='%s'
id='kick1'
to='%s'
type='set'>
<query xmlns='http://jabber.org/protocol/muc#admin'>
<item nick='%s' role='none'>
</item>
</query>
</iq>
`, clientroot.Session.BindJid, jid, JidMustParse(u.From).Resource))
})
bb.ConnectClicked(func() {
var mu MucUser
ok = u.Get(&mu)
if ok {
if mu.MucUserItem.JID != "" {
client.SendRaw(fmt.Sprintf(`
<iq from='%s'
id='ban1'
to='%s'
type='set'>
<query xmlns='http://jabber.org/protocol/muc#admin'>
<item affiliation='outcast' jid='%s'/>
</query>
</iq>
`, clientroot.Session.BindJid, jid, JidMustParse(mu.MucUserItem.JID).Bare()))
}
}
})
ab.ConnectClicked(func() {
var mu MucUser
ok = u.Get(&mu)
if ok {
if mu.MucUserItem.JID != "" {
win := gtk.NewWindow()
win.SetDefaultSize(400, 1)
win.SetResizable(false)
box := gtk.NewBox(gtk.OrientationVertical, 0)
box.Append(gtk.NewLabel("Set "+JidMustParse(u.From).Resource+"'s affiliation"))
the_entry := gtk.NewEntry()
the_entry.SetText(mu.MucUserItem.Affiliation)
submit := gtk.NewButtonWithLabel("Submit")
submit.ConnectClicked(func() {
client.SendRaw(fmt.Sprintf(`
<iq from='%s'
id='ba1'
to='%s'
type='set'>
<query xmlns='http://jabber.org/protocol/muc#admin'>
<item affiliation='%s' jid='%s'/>
</query>
</iq>
`, clientroot.Session.BindJid, jid, the_entry.Text(), JidMustParse(mu.MucUserItem.JID).Bare()))
win.SetVisible(false)
})
box.Append(the_entry)
box.Append(submit)
win.SetChild(box)
win.SetVisible(true)
}}
})
rb.ConnectClicked(func() {
var mu MucUser
ok = u.Get(&mu)
if ok {
if mu.MucUserItem.JID != "" {
win := gtk.NewWindow()
win.SetDefaultSize(400, 1)
win.SetResizable(false)
box := gtk.NewBox(gtk.OrientationVertical, 0)
box.Append(gtk.NewLabel("Set "+JidMustParse(u.From).Resource+"'s role"))
box.Append(gtk.NewLabel("Important: if you want this to be permanent, set their affiliation instead"))
the_entry := gtk.NewEntry()
the_entry.SetText(mu.MucUserItem.Role)
submit := gtk.NewButtonWithLabel("Submit")
submit.ConnectClicked(func() {
client.SendRaw(fmt.Sprintf(`
<iq from='%s'
id='kick1'
to='%s'
type='set'>
<query xmlns='http://jabber.org/protocol/muc#admin'>
<item nick='%s' role='%s'>
</item>
</query>
</iq>
`, clientroot.Session.BindJid, jid, JidMustParse(u.From).Resource, the_entry.Text()))
})
box.Append(the_entry)
box.Append(submit)
win.SetChild(box)
win.SetVisible(true)
}}
})
rc_box.Append(bb)
rc_box.Append(kb)
rc_box.Append(ab)
rc_box.Append(rb)
popover.SetChild(rc_box)
mod_gesture.Connect("pressed", func(n_press, x, y int) {
rect := gdk.NewRectangle(x, y, 1, 1)
popover.SetPointingTo(&rect)
popover.Popup()
})
gesture.Connect("pressed", func(n_press, x, y int) {
win := gtk.NewWindow()
win.SetDefaultSize(400, 400)
profile_box := gtk.NewBox(gtk.OrientationVertical, 0)
nick := gtk.NewLabel(JidMustParse(u.From).Resource)
ver_text := gtk.NewLabel("Getting version...")
ver_text.AddCSSClass("visitor")
win.SetTitle(JidMustParse(u.From).Resource)
nick.AddCSSClass("author")
profile_box.Append(nick)
profile_box.Append(ver_text)
fr := gtk.NewLabel(u.From)
fr.AddCSSClass("jid")
profile_box.Append(fr)
profile_box.Append(ver_text)
iqResp, err := stanza.NewIQ(stanza.Attrs{
Type: "get",
From: clientroot.Session.BindJid,
To: u.From,
Id: "vc2",
Lang: "en",
})
if err != nil {
panic(err)
}
iqResp.Payload = &stanza.Version{}
loading_version_text := gtk.NewLabel("...")
var hats Hats
ok := u.Get(&hats)
if ok {
for _, hat := range hats.Hats {
l := gtk.NewLabel(hat.Title)
l.AddCSSClass("hat")
profile_box.Append(l)
}
}
var mu MucUser
ok = u.Get(&mu)
if ok {
if mu.MucUserItem.JID != "" {
ji := (gtk.NewLabel(mu.MucUserItem.JID))
ji.AddCSSClass("jid")
profile_box.Append(ji)
}
profile_box.Append(gtk.NewLabel("Connected with role " + mu.MucUserItem.Role))
profile_box.Append(gtk.NewLabel("Affiliated as " + mu.MucUserItem.Affiliation))
}
go func() {
fmt.Println("Attempting to get Disco info")
myIQ, err := stanza.NewIQ(stanza.Attrs{
Type: "get",
From: clientroot.Session.BindJid,
To: u.From,
Id: "dicks",
Lang: "en",
})
if err != nil {
panic(err)
}
myIQ.Payload = &stanza.DiscoInfo{}
ctx := context.TODO()
mychan, err := client.SendIQ(ctx, myIQ)
if err == nil {
result := <-mychan
res, ok := result.Payload.(*stanza.DiscoInfo)
if ok {
idents := res.Identity
for i, ident := range idents {
profile_box.Append(gtk.NewLabel(fmt.Sprintf("%d: Name: %s, Category: %s, Type: %s", i+1, ident.Name, ident.Category, ident.Type)))
}
/*
s := fmt.Sprintf("%v", res.Features)
h := sha1.New()
h.Write([]byte(s))
sha1_hash := hex.EncodeToString(h.Sum(nil))
*/
sw := gtk.NewScrolledWindow()
s := ""
for _, feature := range res.Features {
s = s + feature.Var + "\n"
}
sw.SetChild(gtk.NewLabel(s))
profile_box.Append(sw)
}
}
}()
go func() {
ctx := context.TODO()
mychan, err := client.SendIQ(ctx, iqResp)
if err == nil {
result := <-mychan
ver, ok := result.Payload.(*stanza.Version)
if ok {
loading_version_text.SetVisible(false)
name := ver.Name
version := ver.Version
os := ver.OS
vr := fmt.Sprintf("%s %s %s", name, version, os)
if name == "" && version == "" && os == "" {
ver_text.SetText("Client responded with empty version")
} else {
ver_text.SetText(vr)
ver_text.RemoveCSSClass("visitor")
}
} else if result.Error != nil && result.Error.Type != "" {
ver_text.SetText("Got error trying to get version")
ver_text.SetTooltipText(result.Error.Reason + ": " + result.Error.Text)
ver_text.RemoveCSSClass("visitor")
ver_text.AddCSSClass("error")
}
}
}()
go func() {
mo, _ := mucmembers.Load(JidMustParse(u.From).Bare())
mm := mo.(mucUnit)
mmm := mm.Members
mmmm, ok := mmm.Load(id)
if ok {
pres := mmmm.(stanza.Presence)
var vu VCardUpdate
pres.Get(&vu)
if vu.Photo != "" {
im := getAvatar(u.From, vu.Photo)
im.SetPixelSize(80)
im.AddCSSClass("author_img")
profile_box.Prepend(im)
} else {
im := newImageFromPath("debug.png")
im.SetPixelSize(80)
im.AddCSSClass("author_img")
profile_box.Prepend(im)
}
} else {
im := newImageFromPath("debug.png")
im.SetPixelSize(80)
im.AddCSSClass("author_img")
profile_box.Prepend(im)
}
}()
win.SetChild(profile_box)
win.SetTransientFor(win)
win.Present()
})
userbox.AddController(gesture)
userbox.AddController(mod_gesture)
if mu.MucUserItem.Role == "moderator" {
gen.Prepend(userbox)
} else {
gen.Append(userbox)
}
return true
})
headerBox := gtk.NewBox(gtk.OrientationHorizontal, 0)
if i >= 500 {
headerBox.Append(gtk.NewImageFromPaintable(clientAssets["world"]))
} else if i >= 50 {
headerBox.Append(gtk.NewImageFromPaintable(clientAssets["large_group"]))
} else {
headerBox.Append(gtk.NewImageFromPaintable(clientAssets["group"]))
}
headerBox.Append(gtk.NewLabel(fmt.Sprintf("%d participant(s)", i)))
gen.Prepend(headerBox)
muci := getAvatar(jid, jid)
muci.SetPixelSize(80)
gen.Prepend(muci)
memberList.SetChild(gen)
} else {
memberList.SetChild(gtk.NewLabel(jid))
}
}
func showErrorDialog(err error) {
fmt.Println(err.Error())
}