github.com/benoitkugler/goacve@v0.0.0-20201217100549-151ce6e55dc8/client/GUI/app/loggin.go (about) 1 package app 2 3 import ( 4 "fmt" 5 "sort" 6 "time" 7 8 rd "github.com/benoitkugler/goACVE/server/core/rawdata" 9 10 "github.com/benoitkugler/goACVE/client/GUI/basic" 11 "github.com/benoitkugler/goACVE/client/controllers" 12 "github.com/therecipe/qt/core" 13 "github.com/therecipe/qt/widgets" 14 ) 15 16 type Launcher struct { 17 *widgets.QDialog 18 19 Controller *controllers.MainController 20 users *widgets.QScrollArea 21 } 22 23 func (l Launcher) ShowError(err error) { 24 basic.ShowError(err) 25 } 26 27 func (l Launcher) InitRessources() { 28 InitRessources() 29 } 30 31 func (l Launcher) ShowUpdate(ct *controllers.MainController) controllers.LoadMode { 32 dial := initUpdateMonitor() 33 var out controllers.LoadMode 34 basic.Delay(func() { 35 restart, err := ct.CheckUpdate(dial.monitor) 36 if err != nil { 37 keep := ShowStartupError("Mise à jour impossible", fmt.Sprintf("Erreur pendant la mise à jour : %s", err)) 38 if keep { 39 out = controllers.LoadLocal 40 dial.Accept() 41 return 42 } 43 out = controllers.Quit 44 dial.Accept() 45 return 46 } 47 if !restart { 48 out = controllers.LoadRemote 49 dial.Accept() 50 return 51 } 52 dial.showSuccesUpdate() 53 if err := ct.Restart(); err != nil { 54 l.ShowError(fmt.Errorf("Le redémarrage semble impossible : %s. <br/> "+ 55 "Le logiciel va quitter. Veuillez redémarrer manuellement.", err)) 56 } 57 out = controllers.Quit 58 dial.Accept() 59 }) 60 dial.Exec() 61 time.Sleep(100 * time.Millisecond) 62 return out 63 } 64 65 func (l Launcher) ShowChangelog() { 66 showChangelog() 67 } 68 69 func (l *Launcher) InitLoggin(controller *controllers.MainController) { 70 l.QDialog = basic.Dialog("Connexion") 71 setupShortcuts(l, controller, false) 72 title := basic.Label("Bienvenue sur le client ACVE !") 73 title.SetObjectName("loggin-title") 74 title.SetAlignment(core.Qt__AlignCenter) 75 l.users = widgets.NewQScrollArea(nil) 76 l.users.SetAlignment(core.Qt__AlignCenter) 77 l.users.SetWidget(basic.Label("<i>Chargement des utilisateurs...</i>")) 78 l.users.SetMinimumHeight(200) 79 l.users.SetMinimumWidth(900) 80 l.Layout().AddWidget(title) 81 l.Layout().AddWidget(l.users) 82 l.SetModal(true) 83 l.Show() 84 } 85 86 func (l Launcher) ShowLogginError(cause string) bool { 87 return ShowStartupError("Erreur de connexion", cause) 88 } 89 90 func ShowStartupError(title, cause string) (loadLocal bool) { 91 di := basic.Dialog2(title) 92 msg := fmt.Sprintf(`<b>%s</b><br/><br/> 93 Vous pouvez démarrer en mode <b>hors-connexion</b> (accès en lecture seule aux dernières 94 données sauvegardées sur le disque) ou vérifier votre connexion et <b>redémarrer</b>.`, cause) 95 96 goOffline := basic.Button("Continuer hors-connexion") 97 goOffline.SetObjectName(basic.ONAction) 98 goOffline.ConnectClicked(func(_ bool) { 99 di.Accept() 100 }) 101 102 quit := basic.Button("Quitter") 103 quit.ConnectClicked(func(_ bool) { 104 di.Reject() 105 }) 106 107 lay := widgets.NewQGridLayout(di) 108 lab := basic.Label(msg) 109 lab.SetWordWrap(true) 110 lab.SetMinimumWidth(800) 111 lay.AddWidget3(lab, 0, 0, 1, 3, 0) 112 lay.AddWidget2(goOffline, 1, 0, 0) 113 lay.AddWidget2(quit, 1, 2, 0) 114 return di.Exec() > 0 115 } 116 117 func (l *Launcher) SetUsersAndChoose(users []rd.ClientUser, autologs map[int64]string) (userId int64, password string, mods *rd.Modules, keep controllers.LoadMode) { 118 f := basic.Frame() 119 layUs := widgets.NewQHBoxLayout2(f) 120 sort.Slice(users, func(i, j int) bool { 121 return users[i].Label > users[j].Label 122 }) 123 sort.SliceStable(users, func(i, j int) bool { 124 return users[i].IsAdmin || !users[j].IsAdmin 125 }) 126 for _, us := range users { 127 us := us 128 b := widgets.NewQToolButton(nil) 129 b.SetToolButtonStyle(core.Qt__ToolButtonTextUnderIcon) 130 b.SetText(us.Label) 131 b.SetIcon(basic.Icons.User) 132 b.SetIconSize(core.NewQSize2(100, 50)) 133 b.SetObjectName("user") 134 b.ConnectClicked(func(_ bool) { 135 password, mods, keep = l.onClick(us.Id, autologs[us.Id]) 136 userId = us.Id 137 if keep >= 0 { 138 l.Accept() 139 } 140 }) 141 b.SetProperty("admin", core.NewQVariant9(us.IsAdmin)) 142 layUs.AddWidget(b, 1, 0) 143 } 144 l.SetMinimumWidth(1000) 145 l.users.SetWidget(f) 146 if l.Exec() == 0 { 147 keep = controllers.Quit 148 } 149 return 150 } 151 152 func (l *Launcher) onClick(userId int64, autologPassword string) (outPassword string, outModules *rd.Modules, keep controllers.LoadMode) { 153 f := newPasswordForm(userId, l.Controller) 154 f.password.SetText(autologPassword) 155 f.autolog.SetChecked(true) 156 if autologPassword != "" { 157 basic.Delay(f.loggin) 158 } 159 if f.Exec() == 0 { 160 return "", &rd.Modules{}, -1 161 } 162 163 if f.outError != nil { 164 if ShowStartupError("Connexion impossible", fmt.Sprintf("La vérification du mot de passe a échoué : <br/><i>%s</i>", f.outError)) { 165 keep = controllers.LoadLocal 166 } 167 return 168 } 169 outModules, keep = f.outModules, controllers.LoadRemote 170 if f.autolog.IsChecked() { 171 outPassword = f.password.Text() 172 } 173 return 174 } 175 176 type passwordForm struct { 177 *widgets.QDialog 178 179 controller *controllers.MainController 180 userId int64 181 182 valid *widgets.QPushButton 183 password *widgets.QLineEdit 184 retour *widgets.QLabel 185 autolog *widgets.QCheckBox 186 187 outModules *rd.Modules 188 outError error 189 } 190 191 func newPasswordForm(userId int64, ct *controllers.MainController) *passwordForm { 192 p := passwordForm{QDialog: basic.Dialog("Authentification"), controller: ct, userId: userId} 193 p.SetWindowFlag(core.Qt__WindowMinMaxButtonsHint, false) 194 p.password = widgets.NewQLineEdit(nil) 195 p.password.SetPlaceholderText("Mot de passe...") 196 p.password.SetFocus2() 197 p.retour = basic.Label(".") 198 p.retour.SetFixedHeight(40) 199 p.retour.SetObjectName("loggin-retour") 200 p.retour.SetAlignment(core.Qt__AlignCenter) 201 p.autolog = widgets.NewQCheckBox2("Se connecter automatiquement", nil) 202 p.valid = basic.Button("Se connecter") 203 p.valid.SetObjectName(basic.ONAction) 204 p.valid.SetFixedHeight(30) 205 p.valid.ConnectClicked(func(_ bool) { 206 p.loggin() 207 }) 208 p.valid.SetObjectName(basic.ONAction) 209 210 p.Layout().SetContentsMargins(15, 15, 15, 15) 211 p.Layout().AddWidget(basic.Label("Merci de renseigner votre <b>mot de passe</b> :")) 212 p.Layout().AddWidget(p.password) 213 p.Layout().AddWidget(p.autolog) 214 p.Layout().AddWidget(p.retour) 215 p.Layout().AddWidget(p.valid) 216 return &p 217 } 218 219 func (p *passwordForm) loggin() { 220 p.valid.SetEnabled(false) 221 p.valid.SetText("Vérification du mot de passe...") 222 p.outModules, p.outError = p.controller.CheckPassword(p.userId, p.password.Text()) 223 p.valid.SetText("Se connecter") 224 p.valid.SetEnabled(true) 225 if p.outError == nil { 226 if p.outModules == nil { 227 p.retour.SetText("Mot de passe incorrect.") 228 } else { 229 p.Accept() 230 } 231 } else { 232 p.Accept() 233 } 234 }