Raspberry BASIC

Author Topic: GUI Login  (Read 798 times)

John Spikowski

  • BASIC Developer
  • ***
  • Posts: 156
    • View Profile
    • ScriptBasic
Re: GUI Login
« Reply #15 on: January 03, 2020, 02:38:56 AM »
Nim Update

I had to rename the db_sqlite open/close procs to sqlopen/sqlclose to prevent a naming conflict with niup.

Code: Text
  1. # Nim 1.0.4 / IUP 3.28 - Login - JRS
  2.  
  3. import niup
  4. import niupext
  5. import db_sqlite
  6. import os
  7. import md5
  8.  
  9. var initdb:bool = existsFile("user.db")
  10. let usrdb = sqlopen("user.db", "", "", "")
  11. if initdb == false:
  12.   usrdb.exec(sql"CREATE TABLE user (UserID VARCHAR NOT NULL, Password VARCHAR NOT NULL, CONSTRAINT user_pk PRIMARY KEY (UserID));")
  13.  
  14. var uidtxt,showpwd,pwdtxt1,pwdtxt2,errlbl,zbtxt,newusr,loginbut:PIhandle
  15. var newusr_cb:string
  16.  
  17. proc Init_Form() =
  18.   Hide(errlbl)
  19.   SetAttribute(uidtxt, "VALUE", "")
  20.   SetAttribute(pwdtxt1, "VALUE", "")
  21.   SetAttribute(pwdtxt2, "VALUE", "")
  22.   SetAttribute(showpwd, "VALUE", "OFF")
  23.   SetAttribute(newusr, "VALUE", "OFF")
  24.   SetAttribute(zbtxt, "VALUE", "pwdtxt1")
  25.   SetAttribute(loginbut, "TITLE", "Login")
  26.   newusr_cb = "OFF"
  27.   SetFocus(uidtxt)
  28.  
  29. proc login_clicked(ih:PIhandle):cint {.cdecl.}=
  30.   var uid:cstring
  31.   var pswd:cstring
  32.   uid = GetAttribute(uidtxt,"VALUE")
  33.   if GetAttribute(showpwd, "VALUE") == "OFF":
  34.     pswd = GetAttribute(pwdtxt1,"VALUE")
  35.   else:
  36.     pswd = GetAttribute(pwdtxt2,"VALUE")
  37.   # *** NEW USER ***
  38.   if newusr_cb == "ON":
  39.     if uid != "" and pswd != "":
  40.       let rtnmsg = usrdb.tryInsertId(sql"INSERT INTO user (UserID, Password) VALUES (?, ?)", uid, getMD5($pswd))
  41.       if rtnmsg == -1:
  42.         SetAttribute(errlbl, "TITLE", "Duplicate User ID")
  43.         Show(errlbl)
  44.         SetFocus(uidtxt)
  45.         return IUP_DEFAULT
  46.       Message("Login",  "User Added")
  47.       Init_Form()
  48.       return
  49.     else:
  50.       Show(errlbl)
  51.       SetFocus(uidtxt)
  52.       return IUP_DEFAULT
  53.  
  54.   # *** LOGIN VERIFY ***
  55.   SetAttribute(errlbl, "TITLE", "User ID / Password Incorrect")
  56.   let dbpw = usrdb.getValue(sql"SELECT Password FROM user WHERE UserID=?", uid)
  57.   if dbpw != "":
  58.     if dbpw == getMD5($pswd):
  59.       Hide(errlbl)
  60.       Message("Login",  "Login Successful")
  61.       Init_Form()
  62.       return IUP_DEFAULT
  63.   Show(errlbl)
  64.   SetFocus(uidtxt)
  65.   return IUP_DEFAULT
  66.  
  67. proc show_password(ih:PIhandle; state: bool):cint {.cdecl.}=
  68.   var pwd1,pwd2:cstring
  69.   if state:
  70.     pwd1 = GetAttribute(pwdtxt1,"VALUE")
  71.     SetAttribute(pwdtxt2, "VALUE", pwd1)
  72.     SetAttribute(zbtxt, "VALUE", "pwdtxt2")
  73.   else:
  74.     pwd2 = GetAttribute(pwdtxt2,"VALUE")
  75.     SetAttribute(pwdtxt1, "VALUE", pwd2)
  76.     SetAttribute(zbtxt, "VALUE", "pwdtxt1")
  77.     return IUP_DEFAULT
  78.  
  79. proc new_user(ih:PIhandle; state: bool):cint {.cdecl.}=
  80.   if state:
  81.     newusr_cb = "ON"
  82.     SetAttribute(loginbut, "TITLE", "Add User")
  83.   else:
  84.     newusr_cb = "OFF"
  85.     SetAttribute(loginbut, "TITLE", "Login")
  86.   return IUP_DEFAULT
  87.  
  88. # *** MAIN ***
  89.  
  90. Open()
  91.  
  92. # *** DIALOG ***
  93. var dlg = Create("dialog")
  94. SetAttributes(dlg,
  95.   "TITLE=\"Login\", " &
  96.   "SIZE=200x200, " &
  97.   "MAXBOX=NO, " &
  98.   "MINBOX=NO, " &
  99.   "RESIZE=NO, " &
  100.   "DEFAULTENTER=\"loginbut\"")
  101.  
  102. # *** CONTAINER ***
  103. var vb = Create("vbox")
  104.  
  105. # *** IMAGE ***
  106. var hb1 = Create("hbox")
  107. var piclbl = Create("label")
  108. SetAttributes(piclbl,
  109.   "IMAGE=\"login.png\", " &
  110.   "EXPAND=HORIZONTAL, " &
  111.   "ALIGNMENT=ACENTER:ATOP")
  112. discard Append(hb1, piclbl)
  113. discard Append(vb, hb1)
  114.  
  115. # *** ERROR ***
  116. var hb2 = Create("hbox")
  117. errlbl = Create("label")
  118. SetAttributes(errlbl,
  119.   "TITLE=\"User ID / Password Incorrect\", " &
  120.   "FGCOLOR=\"#ff0000\", " &
  121.   "EXPAND=HORIZONTAL, " &
  122.   "ALIGNMENT=ACENTER")
  123. Hide(errlbl)
  124. discard Append(hb2, errlbl)
  125. discard Append(vb, hb2)
  126.  
  127. # *** ENTRY ***
  128. var hb3 = Create("hbox")
  129. SetAttributes(hb3,
  130.   "MARGIN=20x10, " &
  131.   "GAP=5")
  132. var vb1 = Create("vbox")
  133. SetAttribute(vb1, "GAP", "15")
  134. var uidlbl = Create("label")
  135. SetAttribute(uidlbl, "TITLE", "User ID")
  136. discard Append(vb1, uidlbl)
  137. var pwdlbl = Create("label")
  138. SetAttribute(pwdlbl, "TITLE", "Password")
  139. discard Append(vb1, pwdlbl)
  140. discard Append(hb3, vb1)
  141. var vb2 = Create("vbox")
  142. uidtxt = Create("text")
  143. SetAttribute(uidtxt, "SIZE", "85x")
  144. discard Append(vb2, uidtxt)
  145. pwdtxt1 = Create("text")
  146. SetAttributes(pwdtxt1,
  147.   "PASSWORD=YES, " &
  148.   "SIZE=85x")
  149. pwdtxt2 = Create("text")
  150. SetAttribute(pwdtxt2, "SIZE", "85x")
  151. SetHandle("pwdtxt1", pwdtxt1)
  152. SetHandle("pwdtxt2", pwdtxt2)
  153. zbtxt = Zbox(pwdtxt1, pwdtxt2, nil)
  154. SetHandle("zbtxt", zbtxt)
  155. discard Append(vb2, zbtxt)
  156. discard Append(hb3, vb2)
  157. discard Append(vb, hb3)
  158.  
  159. # *** SHOW/HIDE ***
  160. var hb4 = Create("hbox")
  161. SetAttributes(hb4,
  162.   "MARGIN=35x, " &
  163.   "GAP=40")
  164. showpwd = Create("toggle")
  165. SetAttributes(showpwd,
  166.   "TITLE=\"Show Password\", " &
  167.   "CANFOCUS=NO")
  168. newusr = Create("toggle")
  169. SetAttributes(newusr,
  170.   "TITLE=\"New User\", " &
  171.   "CANFOCUS=NO")
  172. discard Append(hb4, showpwd)
  173. discard Append(hb4, newusr)
  174. discard Append(vb, hb4)
  175.  
  176. # *** LOGIN ***
  177. var hb5 = Create("hbox")
  178. SetAttribute(hb5, "MARGIN", "40x10")
  179. loginbut = Create("button")
  180. SetAttributes(loginbut,
  181.   "TITLE=\"Login\", " &
  182.   "EXPAND=HORIZONTAL")
  183. SetHandle("loginbut", loginbut)
  184. discard Append(hb5, loginbut)
  185. discard Append(vb, hb5)
  186.  
  187. # *** ATTACH CONTAINER ***
  188. discard Append(dlg, vb)
  189.  
  190. # *** CALLBACKS ***
  191. SetCallback(loginbut, "ACTION", login_clicked)
  192. SetCallback(showpwd, "ACTION", show_password)
  193. SetCallback(newusr, "ACTION", new_user)
  194.  
  195. # *** PROCESS ***
  196. Show(dlg)
  197. MainLoop()
  198. sqlclose(usrdb)
  199. Close()
  200.  


ubuntu@rpi4b:~/guilc/nim-dev$ scriba showdb.sb
John   -   0b7efaa91a48ccd6c1dd58e3d69ea1a0
AIR   -   4de6ff4050222c488aa16286c39a605b
ubuntu@rpi4b:~/guilc/nim-dev$


Note: The screenshots are the same as the latest ScriptBasic submission.

« Last Edit: January 04, 2020, 03:13:29 AM by John Spikowski »
ScriptBasic Project Manager/Facilitator