1package gui
2
3import (
4	"github.com/coyim/coyim/otr_client"
5)
6
7type executable interface {
8	execute(u *gtkUI)
9}
10
11type connectAccountCmd struct{ a *account }
12type disconnectAccountCmd struct{ a *account }
13type connectionInfoCmd struct{ a *account }
14type editAccountCmd struct{ a *account }
15type removeAccountCmd struct{ a *account }
16type toggleAutoConnectCmd struct{ a *account }
17type toggleAlwaysEncryptCmd struct{ a *account }
18
19func (u *gtkUI) ExecuteCmd(c interface{}) {
20	u.commands <- c
21}
22
23func (c connectAccountCmd) execute(u *gtkUI) {
24	doInUIThread(func() {
25		u.connectAccount(c.a)
26	})
27}
28
29func (c disconnectAccountCmd) execute(u *gtkUI) {
30	go c.a.session.Close()
31}
32
33func (c connectionInfoCmd) execute(u *gtkUI) {
34	doInUIThread(func() {
35		u.connectionInfoDialog(c.a)
36	})
37}
38
39func (c editAccountCmd) execute(u *gtkUI) {
40	doInUIThread(func() {
41		u.editAccount(c.a)
42	})
43}
44
45func (c removeAccountCmd) execute(u *gtkUI) {
46	doInUIThread(func() {
47		u.removeAccount(c.a)
48	})
49}
50
51func (c toggleAutoConnectCmd) execute(u *gtkUI) {
52	go u.toggleAutoConnectAccount(c.a)
53}
54
55func (c toggleAlwaysEncryptCmd) execute(u *gtkUI) {
56	go u.toggleAlwaysEncryptAccount(c.a)
57}
58
59func (u *gtkUI) watchCommands() {
60	for command := range u.commands {
61		switch c := command.(type) {
62		case executable:
63			c.execute(u)
64		case otr_client.AuthorizeFingerprintCmd:
65			c.Account.AuthorizeFingerprint(c.Peer.String(), c.Fingerprint)
66			u.ExecuteCmd(otr_client.SaveApplicationConfigCmd{})
67		case otr_client.SaveInstanceTagCmd:
68			account := c.Account
69			account.InstanceTag = c.InstanceTag
70			u.ExecuteCmd(otr_client.SaveApplicationConfigCmd{})
71		case otr_client.SaveApplicationConfigCmd:
72			u.SaveConfig()
73		}
74	}
75}
76