github.com/benoitkugler/goacve@v0.0.0-20201217100549-151ce6e55dc8/client/GUI2/app/loggin.go (about)

     1  package app
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"net/http"
     7  	"sort"
     8  	"time"
     9  
    10  	"fyne.io/fyne"
    11  	"fyne.io/fyne/container"
    12  	"fyne.io/fyne/dialog"
    13  	"fyne.io/fyne/widget"
    14  	"github.com/benoitkugler/goACVE/client/GUI2/basic"
    15  	"github.com/benoitkugler/goACVE/client/controllers"
    16  	rd "github.com/benoitkugler/goACVE/server/core/rawdata"
    17  )
    18  
    19  type Launcher struct {
    20  	*widget.ScrollContainer
    21  
    22  	Parent     fyne.Window
    23  	Controller *controllers.MainController
    24  	// users      *widget.QScrollArea
    25  }
    26  
    27  func (l *Launcher) SetUsers(users []rd.ClientUser, autologs map[int64]string) *widget.ScrollContainer {
    28  	layUs := container.NewHBox()
    29  	// 	f := basic.Frame()
    30  	// 	layUs := widget.NewQHBoxLayout2(f)
    31  	sort.Slice(users, func(i, j int) bool {
    32  		return users[i].Label > users[j].Label
    33  	})
    34  	sort.SliceStable(users, func(i, j int) bool {
    35  		return users[i].IsAdmin || !users[j].IsAdmin
    36  	})
    37  	for _, us := range users {
    38  		us := us // attention au closure dans les boucles
    39  		btn := widget.NewButtonWithIcon(us.Label, basic.Icons.User, func() {
    40  			l.onClick(us.Id, autologs[us.Id])
    41  		})
    42  		layUs.Add(btn)
    43  		// 		b.SetIconSize(core.NewQSize2(100, 50))
    44  		// 		b.SetObjectName("user")
    45  		// 		b.SetProperty("admin", core.NewQVariant9(us.IsAdmin))
    46  	}
    47  
    48  	out := container.NewHScroll(container.NewCenter(layUs))
    49  	out.SetMinSize(fyne.NewSize(600, 100))
    50  	// l.SetMinSize()
    51  	// 	l.SetMinimumWidth(1000)
    52  	// 	l.users.SetWidget(f)
    53  	// 	if l.Exec() == 0 {
    54  	// 		keep = controllers.Quit
    55  	// 	}
    56  	// return
    57  	return out
    58  }
    59  
    60  func (l *Launcher) onClick(userId int64, autologPassword string) (outPassword string, outModules *rd.Modules, keep controllers.LoadMode) {
    61  	fmt.Println("licked")
    62  
    63  	f := newPasswordForm(userId, l.Controller, l.Parent)
    64  	f.password.SetText(autologPassword)
    65  	f.autolog.SetChecked(true)
    66  	if autologPassword != "" {
    67  		time.Sleep(20 * time.Millisecond)
    68  		f.loggin()
    69  	}
    70  	f.Show()
    71  	// dialog.NewCustom()
    72  	// if f.Exec() == 0 {
    73  	// 	return "", &rd.Modules{}, -1
    74  	// }
    75  
    76  	// if f.outError != nil {
    77  	// 	if ShowStartupError("Connexion impossible", fmt.Sprintf("La vérification du mot de passe a échoué : <br/><i>%s</i>", f.outError)) {
    78  	// 		keep = controllers.LoadLocal
    79  	// 	}
    80  	// 	return
    81  	// }
    82  	// outModules, keep = f.outModules, controllers.LoadRemote
    83  	// if f.autolog.IsChecked() {
    84  	// 	outPassword = f.password.Text()
    85  	// }
    86  	return
    87  }
    88  
    89  type passwordForm struct {
    90  	dialog.Dialog
    91  
    92  	controller *controllers.MainController
    93  	userId     int64
    94  
    95  	valid    *widget.Button
    96  	password *widget.Entry
    97  	retour   *widget.Label
    98  	autolog  *widget.Check
    99  
   100  	outModules *rd.Modules
   101  	outError   error
   102  
   103  	tapped int
   104  }
   105  
   106  func newPasswordForm(userId int64, ct *controllers.MainController, w fyne.Window) passwordForm {
   107  	// QDialog: basic.Dialog("Authentification"),
   108  	p := passwordForm{controller: ct, userId: userId}
   109  	// p.SetWindowFlag(core.Qt__WindowMinMaxButtonsHint, false)
   110  	p.password = widget.NewPasswordEntry()
   111  	p.password.SetPlaceHolder("Mot de passe...")
   112  	// p.password.focus
   113  	p.retour = widget.NewLabel("")
   114  	// p.retour.S (40)
   115  	// p.retour.SetObjectName("loggin-retour")
   116  	// p.retour.SetAlignment(core.Qt__AlignCenter)
   117  	p.autolog = widget.NewCheck("Se connecter automatiquement", nil)
   118  
   119  	// p.valid.SetObjectName(basic.ONAction)
   120  	// p.valid.SetFixedHeight(30)
   121  	// p.valid.ConnectClicked(func(_ bool) {
   122  	// 	p.loggin()
   123  	// })
   124  	// p.valid.SetObjectName(basic.ONAction)
   125  
   126  	container := container.NewVBox(
   127  		container.NewHBox(widget.NewLabel("Merci de renseigner votre mot de passe:"), p.password),
   128  		p.autolog,
   129  		p.retour,
   130  	)
   131  	p.Dialog = dialog.NewCustomConfirm("Authentification", "Se connecter", "Retour", container, func(b bool) {
   132  		if b {
   133  			p := dialog.NewProgressInfinite("Authentification", "Vérification", w)
   134  			p.Show()
   135  			re, _ := http.Get("http://acve.fr/directeurs")
   136  			b, _ := ioutil.ReadAll(re.Body)
   137  			p.Hide()
   138  			dialog.NewInformation("Authentification", fmt.Sprintf("%d", len(b)), w)
   139  		}
   140  	}, w)
   141  	w.Canvas().Focus(p.password)
   142  	// p.PopUp = widget.NewPopUp(content, w.Canvas())
   143  	// p.PopUp.Move(fyne.NewPos(w.Canvas().Size().Width/2, w.Canvas().Size().Height/2))
   144  	// p.Layout().SetContentsMargins(15, 15, 15, 15)
   145  	// p.Layout().AddWidget(basic.Label("Merci de renseigner votre <b>mot de passe</b> :"))
   146  	// p.Layout().AddWidget(p.password)
   147  	// p.Layout().AddWidget(p.autolog)
   148  	// p.Layout().AddWidget(p.retour)
   149  	// p.Layout().AddWidget(p.valid)
   150  	return p
   151  }
   152  
   153  func (p *passwordForm) loggin() {
   154  	if p.valid.Disabled() {
   155  		return
   156  	}
   157  	p.tapped++
   158  	fmt.Println(p.tapped)
   159  	p.valid.Disable()
   160  	p.valid.Refresh()
   161  	p.valid.SetText("Vérification du mot de passe...")
   162  	// p.outModules, p.outError = p.controller.CheckPassword(p.userId, p.password.Text)
   163  	fmt.Println("checing")
   164  	re, _ := http.Get("http://www.youtube.fr")
   165  	b, _ := ioutil.ReadAll(re.Body)
   166  	fmt.Println(len(b))
   167  	p.valid.SetText("Se connecter")
   168  	p.Hide()
   169  	// if p.outError == nil && p.outModules == nil {
   170  	// 	p.retour.SetText("Mot de passe incorrect.")
   171  	// 	p.valid.Enable()
   172  	// 	// p.valid.OnTapped = p.loggin
   173  	// } else {
   174  	// 	p.Hide()
   175  	// }
   176  }