1010using System . Runtime . InteropServices ;
1111using System . Text . RegularExpressions ;
1212using System . Text ;
13+ using System . Linq ;
1314
1415namespace SafeNotes
1516{
@@ -21,6 +22,13 @@ private async void MainForm_Load(object sender, EventArgs e)
2122 {
2223 _settings = SettingsManager . LoadSettings ( ) ;
2324
25+ // Reset the IsRestartingForUpdate flag on application start
26+ if ( _settings . IsRestartingForUpdate )
27+ {
28+ _settings . IsRestartingForUpdate = false ;
29+ SettingsManager . SaveSettings ( _settings ) ;
30+ }
31+
2432 if ( _settings . UserPassword == String . Empty )
2533 {
2634 PasswordStrength . Visible = true ;
@@ -248,6 +256,12 @@ private void EncryptEntriesButton_Click(object sender, System.EventArgs e)
248256
249257 private void MainForm_FormClosing ( object sender , FormClosingEventArgs e )
250258 {
259+ if ( _settings . IsRestartingForUpdate )
260+ {
261+ // Skip saving settings if the application is restarting for an update
262+ return ;
263+ }
264+
251265 if ( EntriesEncryptedButtonClicked == false )
252266 {
253267 _settings . EntryText = JournalEntryBox . Text ;
@@ -259,8 +273,16 @@ private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
259273
260274 try
261275 {
262- EntriesEncryptedButtonClicked = false ;
263- SaveEntries ( ) ;
276+ if ( _settings . IsUserLoggedIn )
277+ {
278+ if ( securePassword == null )
279+ {
280+ MessageBox . Show ( "Secure password is not set. Please log in again." , "Error" , MessageBoxButtons . OK , MessageBoxIcon . Warning ) ;
281+ return ;
282+ }
283+
284+ SaveEntries ( ) ;
285+ }
264286
265287 // Check if decryption is in progress
266288 if ( DecryptionStatusLabel . Text == "Decrypting..." )
@@ -284,25 +306,20 @@ private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
284306
285307 if ( ! string . IsNullOrWhiteSpace ( NotepadTextBox . Text ) && ! shouldExit )
286308 {
287- if ( ! string . IsNullOrWhiteSpace ( NotepadTitle . Text ) && NotepadTitle . Text != null )
309+ if ( ! string . IsNullOrWhiteSpace ( NotepadTitle . Text ) )
288310 {
289311 DialogResult dialogResult = MessageBox . Show ( "Do you want to save your notepad before closing the application?" , "Save notepad" , MessageBoxButtons . YesNo , MessageBoxIcon . Question ) ;
290312 if ( dialogResult == DialogResult . Yes )
291313 {
292314 e . Cancel = true ;
293- // Ask the user where to save the notepad file before closing the application
294- SaveFileDialog saveFileDialog = new SaveFileDialog ( ) ;
295- saveFileDialog . Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*" ;
296- saveFileDialog . Title = "Save your notepad file" ;
297- string fileName = NotepadTitle . Text . Replace ( "Opened File: " , "" ) ;
298- if ( ! fileName . EndsWith ( ".txt" ) )
315+ SaveFileDialog saveFileDialog = new SaveFileDialog
299316 {
300- fileName + = ".txt" ;
301- }
302- saveFileDialog . FileName = fileName ;
303- saveFileDialog . DefaultExt = ".txt" ;
304- // I want initial directory to be the desktop
305- saveFileDialog . InitialDirectory = Environment . GetFolderPath ( Environment . SpecialFolder . Desktop ) ;
317+ Filter = "Text files (* .txt)|*.txt|All files (*.*)|*.*" ,
318+ Title = "Save your notepad file" ,
319+ FileName = NotepadTitle . Text . Replace ( "Opened File: " , "" ) + ".txt" ,
320+ DefaultExt = ".txt" ,
321+ InitialDirectory = Environment . GetFolderPath ( Environment . SpecialFolder . Desktop )
322+ } ;
306323
307324 if ( saveFileDialog . ShowDialog ( ) == DialogResult . OK )
308325 {
@@ -313,14 +330,13 @@ private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
313330 }
314331 else
315332 {
316- // User canceled the save operation, clear the notepad and close the application
317333 NotepadTextBox . Text = null ;
318334 shouldExit = true ;
319335 Close ( ) ;
320336 }
321337 return ;
322338 }
323- if ( dialogResult == DialogResult . No )
339+ else if ( dialogResult == DialogResult . No )
324340 {
325341 NotepadTextBox . Text = null ;
326342 shouldExit = true ;
@@ -334,27 +350,31 @@ private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
334350 }
335351 }
336352
337- if ( EntriesListBox . Items . Count != 0 )
353+ if ( EntriesListBox . Items . Count != 0 && _settings . IsUserLoggedIn )
338354 {
339355 string [ ] entries = new string [ EntriesListBox . Items . Count ] ;
340356 for ( int i = 0 ; i < EntriesListBox . Items . Count ; i ++ )
341357 {
342- string entryText = EntriesListBox . Items [ i ] . ToString ( ) . Replace ( "ListViewItem: {" , "" ) . Replace ( "}" , "" ) ;
358+ var item = EntriesListBox . Items [ i ] ;
359+ if ( item == null ) continue ;
360+
361+ string entryText = item . ToString ( ) . Replace ( "ListViewItem: {" , "" ) . Replace ( "}" , "" ) . Trim ( ) ;
362+ if ( string . IsNullOrWhiteSpace ( entryText ) ) continue ;
363+
343364 entries [ i ] = EncryptString ( entryText , ConvertToUnsecureString ( securePassword ) ) ;
344365 }
345- _settings . Entries = string . Join ( "," , entries ) ;
366+ _settings . Entries = string . Join ( "," , entries . Where ( entry => ! string . IsNullOrWhiteSpace ( entry ) ) ) ;
346367 SettingsManager . SaveSettings ( _settings ) ;
347368 }
348369
349- // Check if the application is actually exiting
350370 if ( e . CloseReason == CloseReason . UserClosing || e . CloseReason == CloseReason . WindowsShutDown )
351371 {
352- // Set the logged-in setting to false
353- _settings . IsUserLoggedIn = false ;
354- SettingsManager . SaveSettings ( _settings ) ;
355-
356- // Clear the password from memory
357- ClearInMemoryPassword ( ) ;
372+ if ( _settings . IsUserLoggedIn )
373+ {
374+ _settings . IsUserLoggedIn = false ;
375+ SettingsManager . SaveSettings ( _settings ) ;
376+ ClearInMemoryPassword ( ) ;
377+ }
358378 }
359379
360380 DecryptionStatusLabel . Visible = false ;
@@ -363,16 +383,13 @@ private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
363383 {
364384 MessageBox . Show ( $ "An error occurred while closing the application: { ex . Message } ", "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
365385 }
366- } else
386+ }
387+ else if ( _settings . IsUserLoggedIn )
367388 {
368- // If the EncryptEntriesButtonClicked is true close the application
369- // Set the logged-in setting to false
370389 _settings . IsUserLoggedIn = false ;
371390 SettingsManager . SaveSettings ( _settings ) ;
372- // Clear the password from memory
373391 ClearInMemoryPassword ( ) ;
374392 DecryptionStatusLabel . Visible = false ;
375- // Close the application
376393 Application . Exit ( ) ;
377394 }
378395 }
@@ -381,15 +398,16 @@ private void ResetAccountCheckbox_CheckedChanged(object sender, System.EventArgs
381398 {
382399 if ( ResetAccountCheckbox . Checked == true )
383400 {
384- DialogResult ResetAccount = MessageBox . Show ( "Are you sure you wish to reset all of your accounts data?" , "Reset Account" , MessageBoxButtons . YesNo , MessageBoxIcon . Question ) ;
401+ DialogResult ResetAccount = MessageBox . Show ( "Are you sure you wish to reset all of your account's data?" , "Reset Account" , MessageBoxButtons . YesNo , MessageBoxIcon . Question ) ;
385402 if ( ResetAccount == DialogResult . Yes )
386403 {
404+ // Clear all user data
387405 UserPassword . Text = string . Empty ;
388406 UserConfirmPassword . Text = string . Empty ;
389407
390408 _settings . UserPassword = string . Empty ;
391409 _settings . Entries = string . Empty ;
392- _settings . YourName = string . Empty ;
410+ _settings . YourName = null ;
393411 _settings . EntryText = string . Empty ;
394412 _settings . NotepadSaveText = string . Empty ;
395413 _settings . SaveDate = true ;
@@ -399,19 +417,26 @@ private void ResetAccountCheckbox_CheckedChanged(object sender, System.EventArgs
399417 _settings . RequirePinCode = false ;
400418 _settings . PinCode = null ;
401419
420+ // Reset the IsRestartingForUpdate flag to ensure it doesn't interfere
421+ _settings . IsRestartingForUpdate = false ;
422+
423+ // Save the updated settings
402424 SettingsManager . SaveSettings ( _settings ) ;
403425
426+ // Clear in-memory secure password
404427 securePassword ? . Dispose ( ) ;
405428 securePassword = null ;
406429
430+ // Update UI to reflect reset state
407431 UserLoginButton . Text = "Register" ;
408432 UserConfirmPassword . Visible = true ;
409433 UserPassword . Location = new System . Drawing . Point ( 300 , 100 ) ;
410434 PasswordGenBox . Visible = true ;
411435 RegenPassButton . Visible = true ;
412436 UsePassButton . Visible = true ;
413437 PasswordLengthSlider . Visible = true ;
414-
438+
439+ // Restart the application
415440 Application . Restart ( ) ;
416441 }
417442 else
@@ -666,6 +691,8 @@ private void UserLoginButton_Click(object sender, EventArgs e)
666691 // return;
667692 //}
668693
694+ PasswordStrength . Visible = false ;
695+
669696 // Checks if any of the supported password managers are installed
670697 string [ ] supportedManagers = { "Bitwarden" , "KeePass Password Safe 2" , "1Password" , "LastPass" , "ProtonPass" , "NordPass" } ;
671698 string managerToOpen = null ;
@@ -927,7 +954,7 @@ private void LeftMenuNav_AfterSelect(object sender, TreeViewEventArgs e)
927954
928955 private void ExportEntriesButton_Click ( object sender , System . EventArgs e )
929956 {
930- MessageBox . Show ( "To import the after exporting the entries, you need to have the same password used to log into SafeNotes during this export process to import the exported entries." , "Password Mismatch Warning" , MessageBoxButtons . OK , MessageBoxIcon . Warning ) ;
957+ MessageBox . Show ( "To import after exporting the entries, you need to have the same password used to log into SafeNotes during this export process to import the exported entries." , "Password Mismatch Warning" , MessageBoxButtons . OK , MessageBoxIcon . Warning ) ;
931958 // Export the entries to a file.
932959 // Also ask the user where to export it to.
933960 SaveFileDialog saveFileDialog = new SaveFileDialog ( ) ;
@@ -1383,22 +1410,33 @@ private void EditEntryButton_Click(object sender, EventArgs e)
13831410 {
13841411 // Programmatically switch to the "Journal" tab
13851412 TabControl . SelectedTab = JournalEntryPage ;
1413+
13861414 // Get the selected item
13871415 ListViewItem selectedItem = EntriesListBox . SelectedItems [ 0 ] ;
1388- // Populate journalEntryBox with the text of the selected entry (without date and time)
13891416 string selectedText = selectedItem . Text ;
1417+
1418+ // Check if the entry contains a date prefix
13901419 int index = selectedText . IndexOf ( " - " ) ;
13911420 if ( index != - 1 )
13921421 {
1422+ // Extract the text after the date and time
13931423 JournalEntryBox . Text = selectedText . Substring ( index + 3 ) ;
13941424 }
1425+ else
1426+ {
1427+ // If no date prefix, use the entire text
1428+ JournalEntryBox . Text = selectedText ;
1429+ }
13951430
13961431 // Set focus to the journalEntryBox
13971432 JournalEntryBox . Focus ( ) ;
1433+
13981434 // Hide the editEntryButton
13991435 EditEntryButton . Hide ( ) ;
1436+
14001437 // Disable the tabControl
14011438 JournalTabSelector . Enabled = false ;
1439+
14021440 // Change the button text to indicate editing
14031441 SaveEntryButton . Text = "Save Edit" ;
14041442 EditEntryButton . Size = new Size ( 107 , 36 ) ;
@@ -1608,6 +1646,11 @@ private bool VerifyPassword(string enteredPassword, string storedHash)
16081646
16091647 private void SaveEntries ( )
16101648 {
1649+ if ( _settings . Entries == null )
1650+ {
1651+ _settings . Entries = string . Empty ;
1652+ }
1653+
16111654 if ( EntriesListBox . Items . Count != 0 )
16121655 {
16131656 string [ ] entries = new string [ EntriesListBox . Items . Count ] ;
@@ -1616,7 +1659,7 @@ private void SaveEntries()
16161659 string entryText = EntriesListBox . Items [ i ] . ToString ( ) . Replace ( "ListViewItem: {" , "" ) . Replace ( "}" , "" ) ;
16171660 entries [ i ] = EncryptString ( entryText . Trim ( ) , ConvertToUnsecureString ( securePassword ) ) ;
16181661 }
1619- _settings . Entries = string . Join ( "," , entries ) ; // Use comma as a delimiter instead of newline
1662+ _settings . Entries = string . Join ( "," , entries ) ;
16201663 }
16211664 else
16221665 {
0 commit comments