aboutsummaryrefslogtreecommitdiff
path: root/ui/view-login.go
blob: 7fb61eaddae61785d0f14049713cde9781b4290a (plain)
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
// gomuks - A terminal Matrix client written in Go.
// Copyright (C) 2019 Tulir Asokan
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

package ui

import (
	"math"

	"maunium.net/go/tcell"

	"maunium.net/go/mautrix"
	"maunium.net/go/mauview"

	"maunium.net/go/gomuks/config"
	"maunium.net/go/gomuks/debug"
	"maunium.net/go/gomuks/interface"
)

type LoginView struct {
	*mauview.Form

	container *mauview.Centerer

	homeserverLabel *mauview.TextField
	usernameLabel   *mauview.TextField
	passwordLabel   *mauview.TextField

	homeserver *mauview.InputField
	username   *mauview.InputField
	password   *mauview.InputField
	error      *mauview.TextView

	loginButton *mauview.Button
	quitButton  *mauview.Button

	loading bool

	matrix ifc.MatrixContainer
	config *config.Config
	parent *GomuksUI
}

func (ui *GomuksUI) NewLoginView() mauview.Component {
	view := &LoginView{
		Form: mauview.NewForm(),

		usernameLabel:   mauview.NewTextField().SetText("Username"),
		passwordLabel:   mauview.NewTextField().SetText("Password"),
		homeserverLabel: mauview.NewTextField().SetText("Homeserver"),

		username:   mauview.NewInputField(),
		password:   mauview.NewInputField(),
		homeserver: mauview.NewInputField(),

		loginButton: mauview.NewButton("Login"),
		quitButton:  mauview.NewButton("Quit"),

		matrix: ui.gmx.Matrix(),
		config: ui.gmx.Config(),
		parent: ui,
	}

	hs := ui.gmx.Config().HS
	view.homeserver.SetText(hs)
	view.username.SetText(ui.gmx.Config().UserID)
	view.password.SetMaskCharacter('*')

	view.quitButton.SetOnClick(func() { ui.gmx.Stop(true) }).SetBackgroundColor(tcell.ColorDarkCyan)
	view.loginButton.SetOnClick(view.Login).SetBackgroundColor(tcell.ColorDarkCyan)

	view.SetColumns([]int{1, 10, 1, 9, 1, 9, 1, 10, 1})
	view.SetRows([]int{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1})
	view.AddFormItem(view.username, 3, 1, 5, 1).
		AddFormItem(view.password, 3, 3, 5, 1).
		AddFormItem(view.homeserver, 3, 5, 5, 1).
		AddFormItem(view.loginButton, 5, 7, 3, 1).
		AddFormItem(view.quitButton, 1, 7, 3, 1).
		AddComponent(view.usernameLabel, 1, 1, 1, 1).
		AddComponent(view.passwordLabel, 1, 3, 1, 1).
		AddComponent(view.homeserverLabel, 1, 5, 1, 1)
	view.FocusNextItem()
	ui.loginView = view

	view.container = mauview.Center(mauview.NewBox(view).SetTitle("Log in to Matrix"), 45, 11)
	view.container.SetAlwaysFocusChild(true)
	return view.container
}

func (view *LoginView) Error(err string) {
	if len(err) == 0 && view.error != nil {
		debug.Print("Hiding error")
		view.RemoveComponent(view.error)
		view.container.SetHeight(11)
		view.SetRows([]int{1, 1, 1, 1, 1, 1, 1, 1, 1})
		view.error = nil
	} else if len(err) > 0 {
		debug.Print("Showing error", err)
		if view.error == nil {
			view.error = mauview.NewTextView().SetTextColor(tcell.ColorRed)
			view.AddComponent(view.error, 1, 9, 7, 1)
		}
		view.error.SetText(err)
		errorHeight := int(math.Ceil(float64(mauview.StringWidth(err)) / 45))
		view.container.SetHeight(12 + errorHeight)
		view.SetRow(9, errorHeight)
	}

	view.parent.Render()
}

func (view *LoginView) actuallyLogin(hs, mxid, password string) {
	debug.Printf("Logging into %s as %s...", hs, mxid)
	view.config.HS = hs
	err := view.matrix.InitClient()
	if err != nil {
		debug.Print("Init error:", err)
		view.Error(err.Error())
		return
	}
	err = view.matrix.Login(mxid, password)
	if err != nil {
		if httpErr, ok := err.(mautrix.HTTPError); ok {
			if httpErr.RespError != nil {
				view.Error(httpErr.RespError.Err)
			} else {
				view.Error(httpErr.Message)
			}
		} else {
			view.Error(err.Error())
		}
		debug.Print("Login error:", err)
	}
	view.loading = false
	view.loginButton.SetText("Login")
}

func (view *LoginView) Login() {
	if view.loading {
		return
	}
	hs := view.homeserver.GetText()
	mxid := view.username.GetText()
	password := view.password.GetText()

	view.loading = true
	view.loginButton.SetText("Logging in...")
	go view.actuallyLogin(hs, mxid, password)
}