Advertisement
Guest User

FormLogin

a guest
Mar 27th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 9.99 KB | None | 0 0
  1. unit FormLogin;
  2. interface
  3. uses
  4. Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  5. System.Classes, Vcl.Graphics,
  6. Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls,
  7. Vcl.ComCtrls, StdStyleActnCtrls;
  8. type
  9. TfrmLogin = class(TForm)
  10. btnExit: TButton;
  11. edtUsername: TEdit;
  12. lblLoginT: TLabel;
  13. shp1: TBevel;
  14. shp2: TBevel;
  15. lblUsername: TLabel;
  16. edtPass: TEdit;
  17. lblPassword: TLabel;
  18. lblSignUp: TLabel;
  19. lblUsernameS: TLabel; // ****** + "S" where S means
  20. edtUsernameS: TEdit; // the component is for signing up
  21. edtPassS: TEdit; // not for logging in.
  22. lblPasswordS: TLabel;
  23. edtRePassS: TEdit;
  24. lblRePasswordS: TLabel;
  25. btnSignUp: TButton;
  26. btnLogin: TButton;
  27. lblpassmatch: TLabel;
  28. lblUserTaken: TLabel;
  29. lblInvalidLogin: TLabel;
  30. procedure btnExitClick(Sender: TObject);
  31. procedure btnSignUpClick(Sender: TObject);
  32. procedure FormCreate(Sender: TObject);
  33. procedure edtUsernameSKeyUp(Sender: TObject; var Key: Word;
  34. Shift: TShiftState);
  35. procedure edtPassSKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
  36. procedure edtRePassSKeyUp(Sender: TObject; var Key: Word;
  37. Shift: TShiftState);
  38. procedure btnLoginClick(Sender: TObject);
  39. procedure FormShow(Sender: TObject);
  40. private
  41. { Private declarations }
  42. procedure ClearFields;
  43. Procedure UpDir(loc: String); overload; // debug version prints location
  44. // of call to console
  45. procedure UpDir; overload; // Go up a directory
  46. public
  47. { Public declarations }
  48. AccountNumber: Integer;
  49. AccountName: String;
  50. Procedure CheckAndCreateDirectories;
  51. end;
  52. type
  53. TAccountInfo = Record
  54. AccountName: String[25];
  55. AccountPass: String[25];
  56. AccountNumber: Integer;
  57. end;
  58. // type TAccountFile = File of TAccountInfo;
  59. var
  60. frmLogin: TfrmLogin;
  61. AccountFile: File of TAccountInfo;
  62. AccountInfoRecord: TAccountInfo;
  63. implementation
  64. {$R *.dfm}
  65. uses FormMain;
  66. Procedure TfrmLogin.UpDir(loc: String);
  67. begin
  68. writeln(loc + ': ' + GetCurrentDir);
  69. SetCurrentDir(ExtractFilePath(ExcludeTrailingPathDelimiter(GetCurrentDir)));
  70. writeln(loc + ': ' + GetCurrentDir);
  71. end;
  72. Procedure TfrmLogin.UpDir;
  73. begin
  74. SetCurrentDirectory
  75. (PChar(ExtractFilePath(ExcludeTrailingPathDelimiter(GetCurrentDir))));
  76. end;
  77. /// /////////////////////////////////////////////////////////////////////////////
  78. procedure CheckForAllCompleteFields;
  79. begin
  80. if (frmLogin.lblpassmatch.Caption = 'Passwords Match') and
  81. (frmLogin.edtUsernameS.Text <> '') and (frmLogin.edtPassS.Text <> '') and
  82. (frmLogin.edtRePassS.Text <> '') and not(frmLogin.lblUserTaken.Visible) then
  83. frmLogin.btnSignUp.Enabled := True
  84. else
  85. frmLogin.btnSignUp.Enabled := false;
  86. end;
  87. /// /////////////////////////////////////////////////////////////////////////////
  88. Procedure CheckIfPasswordsMatch;
  89. begin
  90. frmLogin.lblpassmatch.Visible := True;
  91. frmLogin.lblpassmatch.Font.Color := clred;
  92. if (frmLogin.edtPassS.Text <> '') and (frmLogin.edtRePassS.Text <> '') then
  93. begin // IF Start
  94. if (frmLogin.edtPassS.Text = frmLogin.edtRePassS.Text) then
  95. begin
  96. frmLogin.lblpassmatch.Caption := 'Passwords Match';
  97. frmLogin.lblpassmatch.Font.Color := clgreen;
  98. end
  99. else
  100. begin
  101. frmLogin.lblpassmatch.Caption := 'Passwords do not Match';
  102. frmLogin.lblpassmatch.Font.Color := clred;
  103. frmLogin.btnSignUp.Enabled := false;
  104. end;
  105. end; // ENDIF
  106. end;
  107. /// /////////////////////////////////////////////////////////////////////////////
  108. function FindNextAccountNumber(AccountInfo: TAccountInfo): Integer;
  109. var
  110. CurrentAccountNum: Integer;
  111. begin
  112. SetCurrentDir(GetCurrentDir + '\Main Files');
  113. AssignFile(AccountFile, 'AccountData.dat');
  114. Reset(AccountFile);
  115. while not Eof(AccountFile) do
  116. begin // while
  117. read(AccountFile, AccountInfo);
  118. end; // while
  119. Closefile(AccountFile);
  120. Result := AccountInfo.AccountNumber + 1;
  121. frmLogin.UpDir;
  122. end;
  123. /// /////////////////////////////////////////////////////////////////////////////
  124. procedure ReadNewAccountToFile(AccountInfo: TAccountInfo);
  125. begin
  126. SetCurrentDir(GetCurrentDir + '\Main Files');
  127. AssignFile(AccountFile, 'AccountData.dat');
  128. Reset(AccountFile);
  129. Seek(AccountFile, Filesize(AccountFile));
  130. Write(AccountFile, AccountInfo);
  131. Closefile(AccountFile);
  132. frmLogin.UpDir;
  133. end;
  134. /// /////////////////////////////////////////////////////////////////////////////
  135. procedure TfrmLogin.btnExitClick(Sender: TObject);
  136. begin
  137. close;
  138. end;
  139. /// /////////////////////////////////////////////////////////////////////////////
  140. Function EncryptPass(Password: String): String;
  141. var
  142. Mod1, Count: Integer;
  143. begin
  144. Result := '';
  145. Mod1 := Length(Password);
  146. for Count := Low(Password) to (Length(Password)) do
  147. begin
  148. Result := Result + Chr(Ord(Password[Count]) + Mod1);
  149. end;
  150. end;
  151. /// /////////////////////////////////////////////////////////////////////////////
  152. Function DecryptPass(EncPassword: String): String;
  153. var
  154. Mod1, Count: Integer;
  155. begin
  156. Result := '';
  157. Mod1 := Length(EncPassword);
  158. for Count := Low(EncPassword) to (Length(EncPassword)) do
  159. begin
  160. Result := Result + Chr(Ord(EncPassword[Count]) - Mod1);
  161. end;
  162. end;
  163. /// /////////////////////////////////////////////////////////////////////////////
  164. Procedure AssignAccountDataS( { Username : String; Password : String; }
  165. AccountInfo: TAccountInfo);
  166. begin
  167. AccountInfo.AccountName := frmLogin.edtUsernameS.Text;
  168. AccountInfo.AccountPass := frmLogin.edtPassS.Text;
  169. // AccountInfo.AccountNumber := AccountNum;
  170. end;
  171. /// /////////////////////////////////////////////////////////////////////////////
  172. procedure TfrmLogin.btnLoginClick(Sender: TObject);
  173. var
  174. AccountFound: Boolean;
  175. EncryptedPass: String;
  176. begin
  177. EncryptedPass := EncryptPass(edtPass.Text);
  178. AccountFound := false;
  179. SetCurrentDir(GetCurrentDir + '\Main Files');
  180. AssignFile(AccountFile, 'AccountData.dat');
  181. Reset(AccountFile);
  182. while not Eof(AccountFile) do
  183. begin // Start While //iterate through file until account is found
  184. Read(AccountFile, AccountInfoRecord);
  185. if (AccountInfoRecord.AccountName = edtUsername.Text) and
  186. (AccountInfoRecord.AccountPass = EncryptedPass) then
  187. begin
  188. // login
  189. frmLogin.AccountName := AccountInfoRecord.AccountName;
  190. frmLogin.AccountNumber := AccountInfoRecord.AccountNumber;
  191. frmLogin.Hide;
  192. AccountFound := True;
  193. break;
  194. end;
  195. end; // end While
  196. frmLogin.UpDir; // go back to parent directory
  197. if AccountFound then
  198. begin
  199. frmMain.Show; // Only show after finding account number;
  200. lblInvalidLogin.Visible := false; // remove it if the user had previous
  201. // //login attempt
  202. end
  203. else
  204. begin
  205. lblInvalidLogin.Visible := True;
  206. end;
  207. Closefile(AccountFile);
  208. end;
  209. /// /////////////////////////////////////////////////////////////////////////////
  210. procedure CheckIfUserTaken;
  211. begin
  212. SetCurrentDir(GetCurrentDir + '\Main Files');
  213. AssignFile(AccountFile, 'AccountData.dat');
  214. Reset(AccountFile);
  215. while not Eof(AccountFile) do
  216. begin // while
  217. read(AccountFile, AccountInfoRecord);
  218. if AccountInfoRecord.AccountName = frmLogin.edtUsernameS.Text then
  219. begin
  220. frmLogin.lblUserTaken.Visible := True;
  221. frmLogin.lblUserTaken.Caption := 'Username already taken';
  222. frmLogin.lblUserTaken.Font.Color := clred;
  223. break;
  224. end
  225. else
  226. frmLogin.lblUserTaken.Visible := false;
  227. end; // while
  228. Closefile(AccountFile);
  229. frmLogin.UpDir;
  230. end;
  231. procedure TfrmLogin.CheckAndCreateDirectories;
  232. begin
  233. if not DirectoryExists('Main Files') then
  234. CreateDir('Main Files');
  235. if not DirectoryExists('Flashcards') then
  236. CreateDir('Flashcards');
  237. if not DirectoryExists('Topic Tabs') then
  238. CreateDir('Topic Tabs');
  239. if not DirectoryExists('Notepads') then
  240. CreateDir('Notepads');
  241. if not DirectoryExists('Public Flashcards') then
  242. CreateDir('Public Flashcards');
  243. end;
  244. procedure TfrmLogin.btnSignUpClick(Sender: TObject);
  245. begin
  246. AssignAccountDataS(AccountInfoRecord);
  247. CheckIfUserTaken;
  248. CheckForAllCompleteFields;
  249. if btnSignUp.Enabled then
  250. begin
  251. AccountInfoRecord.AccountName := frmLogin.edtUsernameS.Text;
  252. AccountInfoRecord.AccountPass := EncryptPass(frmLogin.edtPassS.Text);
  253. AccountInfoRecord.AccountNumber := FindNextAccountNumber(AccountInfoRecord);
  254. ReadNewAccountToFile(AccountInfoRecord);
  255. frmLogin.AccountName := AccountInfoRecord.AccountName;
  256. frmLogin.AccountNumber := AccountInfoRecord.AccountNumber;
  257. frmMain.Show;
  258. frmLogin.ClearFields;
  259. end
  260. end;
  261. /// //////////Begin checks on user-entered data////////////////////
  262. procedure TfrmLogin.edtPassSKeyUp(Sender: TObject; var Key: Word;
  263. Shift: TShiftState);
  264. begin
  265. CheckIfPasswordsMatch;
  266. CheckForAllCompleteFields;
  267. end;
  268. procedure TfrmLogin.edtRePassSKeyUp(Sender: TObject; var Key: Word;
  269. Shift: TShiftState);
  270. begin
  271. CheckIfPasswordsMatch;
  272. CheckForAllCompleteFields;
  273. end;
  274. procedure TfrmLogin.edtUsernameSKeyUp(Sender: TObject; var Key: Word;
  275. Shift: TShiftState);
  276. begin
  277. CheckForAllCompleteFields;
  278. lblUserTaken.Visible := false;
  279. end;
  280. /// /////////End Checks//////////////////////////////////////////////
  281. procedure TfrmLogin.FormCreate(Sender: TObject);
  282. begin
  283. frmLogin.Height := Screen.WorkAreaHeight;
  284. frmLogin.Width := Screen.WorkAreaWidth;
  285. frmLogin.CheckAndCreateDirectories;
  286. /// //////////
  287. SetCurrentDir(GetCurrentDir + '\Main Files');
  288. {$I-}
  289. AssignFile(AccountFile, 'AccountData.dat');
  290. Reset(AccountFile);
  291. Closefile(AccountFile);
  292. {$I+}
  293. frmLogin.UpDir; // check to see if an old file exists
  294. if (IOResult <> 0) then // already
  295. begin
  296. SetCurrentDir(GetCurrentDir + '\Main Files');
  297. Rewrite(AccountFile);
  298. Closefile(AccountFile); // Create a new file for writing to
  299. frmLogin.UpDir;
  300. end;
  301. /// ////////
  302. // if (Screen.Width <> 1920) and (Screen.Height <> 1080) then
  303. frmLogin.VertScrollBar.Position := frmLogin.VertScrollBar.Range.MinValue;
  304. // this doesn't currently appear to work
  305. lblpassmatch.Visible := false;
  306. lblInvalidLogin.Visible := false;
  307. lblpassmatch.Caption := 'Passwords do not match';
  308. lblpassmatch.Font.Color := clred;
  309. btnSignUp.Enabled := false;
  310. lblUserTaken.Visible := false;
  311. end;
  312. procedure TfrmLogin.FormShow(Sender: TObject);
  313. begin
  314. frmLogin.ClearFields;
  315. end;
  316. procedure TfrmLogin.ClearFields; // Clears all edit boxes and labels
  317. begin
  318. frmLogin.edtPass.Clear;
  319. frmLogin.edtPassS.Clear;
  320. frmLogin.edtRePassS.Clear;
  321. frmLogin.edtUsername.Clear;
  322. frmLogin.edtUsernameS.Clear;
  323. frmLogin.lblpassmatch.Visible := false;
  324. frmLogin.lblUserTaken.Visible := false;
  325. frmLogin.lblInvalidLogin.Visible := false;
  326. end;
  327. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement