LoginUI.go (3987B)
1 package ui 2 3 import ( 4 "sync" 5 6 "github.com/Nordgedanken/Morpheus/matrix" 7 "github.com/matrix-org/gomatrix" 8 log "github.com/sirupsen/logrus" 9 "github.com/therecipe/qt/core" 10 "github.com/therecipe/qt/widgets" 11 ) 12 13 // NewLoginUIStruct gives you a LoginUI struct with prefilled data 14 func NewLoginUIStruct(windowWidth, windowHeight int, window *widgets.QMainWindow) (loginUIStruct LoginUI) { 15 configStruct := config{ 16 windowWidth: windowWidth, 17 windowHeight: windowHeight, 18 } 19 loginUIStruct = LoginUI{ 20 config: configStruct, 21 window: window, 22 } 23 return 24 } 25 26 // NewLoginUIStructWithExistingConfig gives you a LoginUI struct with prefilled data and data from a previous Config 27 func NewLoginUIStructWithExistingConfig(configStruct config, window *widgets.QMainWindow) (loginUIStruct LoginUI) { 28 loginUIStruct = LoginUI{ 29 config: configStruct, 30 window: window, 31 } 32 return 33 } 34 35 // GetWidget gives you the widget of the LoginUI struct 36 func (l *LoginUI) GetWidget() (widget *widgets.QWidget) { 37 widget = l.widget 38 return 39 } 40 41 // NewUI initializes a new login Screen 42 func (l *LoginUI) NewUI() (err error) { 43 widget := widgets.NewQWidget(nil, 0) 44 widget.SetObjectName("LoginWrapper") 45 widget.SetStyleSheet("QWidget#LoginWrapper { border: 0px; };") 46 topLayout := widgets.NewQVBoxLayout() 47 48 formWidget := widgets.NewQWidget(nil, 0) 49 formWrapper := widgets.NewQHBoxLayout() 50 51 formLayout := widgets.NewQVBoxLayout() 52 formLayout.SetSpacing(20) 53 formLayout.SetContentsMargins(0, 0, 0, 30) 54 formWidget.SetLayout(formLayout) 55 56 formWrapper.AddStretch(1) 57 formWrapper.AddWidget(formWidget, 0, 0) 58 formWrapper.AddStretch(1) 59 60 // UsernameInput 61 usernameInput := widgets.NewQLineEdit(nil) 62 usernameInput.SetPlaceholderText("Insert MXID") 63 64 usernameLayout := widgets.NewQHBoxLayout() 65 usernameLayout.AddWidget(usernameInput, 0, core.Qt__AlignVCenter) 66 67 // PasswordInput 68 passwordInput := widgets.NewQLineEdit(nil) 69 passwordInput.SetPlaceholderText("Insert password") 70 passwordInput.SetEchoMode(widgets.QLineEdit__Password) 71 72 passwordLayout := widgets.NewQHBoxLayout() 73 passwordLayout.AddWidget(passwordInput, 0, core.Qt__AlignVCenter) 74 75 formLayout.AddLayout(usernameLayout, 0) 76 formLayout.AddLayout(passwordLayout, 0) 77 78 // loginButton 79 buttonLayout := widgets.NewQHBoxLayout() 80 buttonLayout.SetSpacing(0) 81 buttonLayout.SetContentsMargins(0, 0, 0, 30) 82 83 loginButton := widgets.NewQPushButton2("LOGIN", nil) 84 loginButton.SetMinimumSize2(350, 65) 85 86 buttonLayout.AddStretch(1) 87 buttonLayout.AddWidget(loginButton, 0, 0) 88 buttonLayout.AddStretch(1) 89 90 topLayout.AddStretch(1) 91 topLayout.AddLayout(formWrapper, 0) 92 topLayout.AddStretch(1) 93 topLayout.AddLayout(buttonLayout, 0) 94 topLayout.AddStretch(1) 95 96 widget.SetLayout(topLayout) 97 98 usernameInput.ConnectTextChanged(func(value string) { 99 l.username = value 100 }) 101 102 passwordInput.ConnectTextChanged(func(value string) { 103 l.password = value 104 }) 105 106 loginButton.ConnectClicked(func(_ bool) { 107 LoginErr := l.login() 108 if err != nil { 109 err = LoginErr 110 return 111 } 112 }) 113 114 widget.SetWindowTitle("Morpheus - Login") 115 116 l.widget = widget 117 return 118 } 119 120 func (l *LoginUI) login() (err error) { 121 //TODO register enter and show loader or so 122 123 var wg sync.WaitGroup 124 125 if l.username != "" && l.password != "" { 126 log.Infoln("Starting Login Sequenze in background") 127 results := make(chan *gomatrix.Client) 128 129 wg.Add(1) 130 go matrix.DoLogin(l.username, l.password, "", "", "", results, &wg) 131 132 go func() { 133 wg.Wait() // wait for each execTask to return 134 close(results) // then close the results channel 135 }() 136 137 //Show MainUI 138 for result := range results { 139 //TODO Don't switch screen on wrong login data. 140 l.cli = result 141 MainUIStruct := NewMainUIStructWithExistingConfig(l.config, l.window) 142 mainUIErr := MainUIStruct.NewUI() 143 if mainUIErr != nil { 144 err = mainUIErr 145 return 146 } 147 l.window.SetCentralWidget(MainUIStruct.GetWidget()) 148 l.window.Resize(l.widget.Size()) 149 } 150 } else { 151 log.Warningln("Username and/or password is empty. Do Nothing.") 152 } 153 return 154 }