# Nim 1.0.4 / IUP 3.28 - Login - JRS
import niup
import niupext
import db_sqlite
import os
import md5
var initdb:bool = existsFile("user.db")
let usrdb = sqlopen("user.db", "", "", "")
if initdb == false:
usrdb.exec(sql"CREATE TABLE user (UserID VARCHAR NOT NULL, Password VARCHAR NOT NULL, CONSTRAINT user_pk PRIMARY KEY (UserID));")
var uidtxt,showpwd,pwdtxt1,pwdtxt2,errlbl,zbtxt,newusr,loginbut:PIhandle
var newusr_cb:string
proc Init_Form() =
Hide(errlbl)
SetAttribute(uidtxt, "VALUE", "")
SetAttribute(pwdtxt1, "VALUE", "")
SetAttribute(pwdtxt2, "VALUE", "")
SetAttribute(showpwd, "VALUE", "OFF")
SetAttribute(newusr, "VALUE", "OFF")
SetAttribute(zbtxt, "VALUE", "pwdtxt1")
SetAttribute(loginbut, "TITLE", "Login")
newusr_cb = "OFF"
SetFocus(uidtxt)
proc login_clicked(ih:PIhandle):cint {.cdecl.}=
var uid:cstring
var pswd:cstring
uid = GetAttribute(uidtxt,"VALUE")
if GetAttribute(showpwd, "VALUE") == "OFF":
pswd = GetAttribute(pwdtxt1,"VALUE")
else:
pswd = GetAttribute(pwdtxt2,"VALUE")
# *** NEW USER ***
if newusr_cb == "ON":
if uid != "" and pswd != "":
let rtnmsg = usrdb.tryInsertId(sql"INSERT INTO user (UserID, Password) VALUES (?, ?)", uid, getMD5($pswd))
if rtnmsg == -1:
SetAttribute(errlbl, "TITLE", "Duplicate User ID")
Show(errlbl)
SetFocus(uidtxt)
return IUP_DEFAULT
Message("Login", "User Added")
Init_Form()
return
else:
Show(errlbl)
SetFocus(uidtxt)
return IUP_DEFAULT
# *** LOGIN VERIFY ***
SetAttribute(errlbl, "TITLE", "User ID / Password Incorrect")
let dbpw = usrdb.getValue(sql"SELECT Password FROM user WHERE UserID=?", uid)
if dbpw != "":
if dbpw == getMD5($pswd):
Hide(errlbl)
Message("Login", "Login Successful")
Init_Form()
return IUP_DEFAULT
Show(errlbl)
SetFocus(uidtxt)
return IUP_DEFAULT
proc show_password(ih:PIhandle; state: bool):cint {.cdecl.}=
var pwd1,pwd2:cstring
if state:
pwd1 = GetAttribute(pwdtxt1,"VALUE")
SetAttribute(pwdtxt2, "VALUE", pwd1)
SetAttribute(zbtxt, "VALUE", "pwdtxt2")
else:
pwd2 = GetAttribute(pwdtxt2,"VALUE")
SetAttribute(pwdtxt1, "VALUE", pwd2)
SetAttribute(zbtxt, "VALUE", "pwdtxt1")
return IUP_DEFAULT
proc new_user(ih:PIhandle; state: bool):cint {.cdecl.}=
if state:
newusr_cb = "ON"
SetAttribute(loginbut, "TITLE", "Add User")
else:
newusr_cb = "OFF"
SetAttribute(loginbut, "TITLE", "Login")
return IUP_DEFAULT
# *** MAIN ***
Open()
# *** DIALOG ***
var dlg = Create("dialog")
SetAttributes(dlg,
"TITLE=\"Login\", " &
"SIZE=200x200, " &
"MAXBOX=NO, " &
"MINBOX=NO, " &
"RESIZE=NO, " &
"DEFAULTENTER=\"loginbut\"")
# *** CONTAINER ***
var vb = Create("vbox")
# *** IMAGE ***
var hb1 = Create("hbox")
var piclbl = Create("label")
SetAttributes(piclbl,
"IMAGE=\"login.png\", " &
"EXPAND=HORIZONTAL, " &
"ALIGNMENT=ACENTER:ATOP")
discard Append(hb1, piclbl)
discard Append(vb, hb1)
# *** ERROR ***
var hb2 = Create("hbox")
errlbl = Create("label")
SetAttributes(errlbl,
"TITLE=\"User ID / Password Incorrect\", " &
"FGCOLOR=\"#ff0000\", " &
"EXPAND=HORIZONTAL, " &
"ALIGNMENT=ACENTER")
Hide(errlbl)
discard Append(hb2, errlbl)
discard Append(vb, hb2)
# *** ENTRY ***
var hb3 = Create("hbox")
SetAttributes(hb3,
"MARGIN=20x10, " &
"GAP=5")
var vb1 = Create("vbox")
SetAttribute(vb1, "GAP", "15")
var uidlbl = Create("label")
SetAttribute(uidlbl, "TITLE", "User ID")
discard Append(vb1, uidlbl)
var pwdlbl = Create("label")
SetAttribute(pwdlbl, "TITLE", "Password")
discard Append(vb1, pwdlbl)
discard Append(hb3, vb1)
var vb2 = Create("vbox")
uidtxt = Create("text")
SetAttribute(uidtxt, "SIZE", "85x")
discard Append(vb2, uidtxt)
pwdtxt1 = Create("text")
SetAttributes(pwdtxt1,
"PASSWORD=YES, " &
"SIZE=85x")
pwdtxt2 = Create("text")
SetAttribute(pwdtxt2, "SIZE", "85x")
SetHandle("pwdtxt1", pwdtxt1)
SetHandle("pwdtxt2", pwdtxt2)
zbtxt = Zbox(pwdtxt1, pwdtxt2, nil)
SetHandle("zbtxt", zbtxt)
discard Append(vb2, zbtxt)
discard Append(hb3, vb2)
discard Append(vb, hb3)
# *** SHOW/HIDE ***
var hb4 = Create("hbox")
SetAttributes(hb4,
"MARGIN=35x, " &
"GAP=40")
showpwd = Create("toggle")
SetAttributes(showpwd,
"TITLE=\"Show Password\", " &
"CANFOCUS=NO")
newusr = Create("toggle")
SetAttributes(newusr,
"TITLE=\"New User\", " &
"CANFOCUS=NO")
discard Append(hb4, showpwd)
discard Append(hb4, newusr)
discard Append(vb, hb4)
# *** LOGIN ***
var hb5 = Create("hbox")
SetAttribute(hb5, "MARGIN", "40x10")
loginbut = Create("button")
SetAttributes(loginbut,
"TITLE=\"Login\", " &
"EXPAND=HORIZONTAL")
SetHandle("loginbut", loginbut)
discard Append(hb5, loginbut)
discard Append(vb, hb5)
# *** ATTACH CONTAINER ***
discard Append(dlg, vb)
# *** CALLBACKS ***
SetCallback(loginbut, "ACTION", login_clicked)
SetCallback(showpwd, "ACTION", show_password)
SetCallback(newusr, "ACTION", new_user)
# *** PROCESS ***
Show(dlg)
MainLoop()
sqlclose(usrdb)
Close()