10
10
using System . Runtime . InteropServices ;
11
11
using System . Text . RegularExpressions ;
12
12
using System . Text ;
13
+ using System . Linq ;
13
14
14
15
namespace SafeNotes
15
16
{
@@ -21,6 +22,13 @@ private async void MainForm_Load(object sender, EventArgs e)
21
22
{
22
23
_settings = SettingsManager . LoadSettings ( ) ;
23
24
25
+ // Reset the IsRestartingForUpdate flag on application start
26
+ if ( _settings . IsRestartingForUpdate )
27
+ {
28
+ _settings . IsRestartingForUpdate = false ;
29
+ SettingsManager . SaveSettings ( _settings ) ;
30
+ }
31
+
24
32
if ( _settings . UserPassword == String . Empty )
25
33
{
26
34
PasswordStrength . Visible = true ;
@@ -248,6 +256,12 @@ private void EncryptEntriesButton_Click(object sender, System.EventArgs e)
248
256
249
257
private void MainForm_FormClosing ( object sender , FormClosingEventArgs e )
250
258
{
259
+ if ( _settings . IsRestartingForUpdate )
260
+ {
261
+ // Skip saving settings if the application is restarting for an update
262
+ return ;
263
+ }
264
+
251
265
if ( EntriesEncryptedButtonClicked == false )
252
266
{
253
267
_settings . EntryText = JournalEntryBox . Text ;
@@ -259,8 +273,16 @@ private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
259
273
260
274
try
261
275
{
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
+ }
264
286
265
287
// Check if decryption is in progress
266
288
if ( DecryptionStatusLabel . Text == "Decrypting..." )
@@ -284,25 +306,20 @@ private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
284
306
285
307
if ( ! string . IsNullOrWhiteSpace ( NotepadTextBox . Text ) && ! shouldExit )
286
308
{
287
- if ( ! string . IsNullOrWhiteSpace ( NotepadTitle . Text ) && NotepadTitle . Text != null )
309
+ if ( ! string . IsNullOrWhiteSpace ( NotepadTitle . Text ) )
288
310
{
289
311
DialogResult dialogResult = MessageBox . Show ( "Do you want to save your notepad before closing the application?" , "Save notepad" , MessageBoxButtons . YesNo , MessageBoxIcon . Question ) ;
290
312
if ( dialogResult == DialogResult . Yes )
291
313
{
292
314
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
299
316
{
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
+ } ;
306
323
307
324
if ( saveFileDialog . ShowDialog ( ) == DialogResult . OK )
308
325
{
@@ -313,14 +330,13 @@ private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
313
330
}
314
331
else
315
332
{
316
- // User canceled the save operation, clear the notepad and close the application
317
333
NotepadTextBox . Text = null ;
318
334
shouldExit = true ;
319
335
Close ( ) ;
320
336
}
321
337
return ;
322
338
}
323
- if ( dialogResult == DialogResult . No )
339
+ else if ( dialogResult == DialogResult . No )
324
340
{
325
341
NotepadTextBox . Text = null ;
326
342
shouldExit = true ;
@@ -334,27 +350,31 @@ private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
334
350
}
335
351
}
336
352
337
- if ( EntriesListBox . Items . Count != 0 )
353
+ if ( EntriesListBox . Items . Count != 0 && _settings . IsUserLoggedIn )
338
354
{
339
355
string [ ] entries = new string [ EntriesListBox . Items . Count ] ;
340
356
for ( int i = 0 ; i < EntriesListBox . Items . Count ; i ++ )
341
357
{
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
+
343
364
entries [ i ] = EncryptString ( entryText , ConvertToUnsecureString ( securePassword ) ) ;
344
365
}
345
- _settings . Entries = string . Join ( "," , entries ) ;
366
+ _settings . Entries = string . Join ( "," , entries . Where ( entry => ! string . IsNullOrWhiteSpace ( entry ) ) ) ;
346
367
SettingsManager . SaveSettings ( _settings ) ;
347
368
}
348
369
349
- // Check if the application is actually exiting
350
370
if ( e . CloseReason == CloseReason . UserClosing || e . CloseReason == CloseReason . WindowsShutDown )
351
371
{
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
+ }
358
378
}
359
379
360
380
DecryptionStatusLabel . Visible = false ;
@@ -363,16 +383,13 @@ private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
363
383
{
364
384
MessageBox . Show ( $ "An error occurred while closing the application: { ex . Message } ", "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
365
385
}
366
- } else
386
+ }
387
+ else if ( _settings . IsUserLoggedIn )
367
388
{
368
- // If the EncryptEntriesButtonClicked is true close the application
369
- // Set the logged-in setting to false
370
389
_settings . IsUserLoggedIn = false ;
371
390
SettingsManager . SaveSettings ( _settings ) ;
372
- // Clear the password from memory
373
391
ClearInMemoryPassword ( ) ;
374
392
DecryptionStatusLabel . Visible = false ;
375
- // Close the application
376
393
Application . Exit ( ) ;
377
394
}
378
395
}
@@ -381,15 +398,16 @@ private void ResetAccountCheckbox_CheckedChanged(object sender, System.EventArgs
381
398
{
382
399
if ( ResetAccountCheckbox . Checked == true )
383
400
{
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 ) ;
385
402
if ( ResetAccount == DialogResult . Yes )
386
403
{
404
+ // Clear all user data
387
405
UserPassword . Text = string . Empty ;
388
406
UserConfirmPassword . Text = string . Empty ;
389
407
390
408
_settings . UserPassword = string . Empty ;
391
409
_settings . Entries = string . Empty ;
392
- _settings . YourName = string . Empty ;
410
+ _settings . YourName = null ;
393
411
_settings . EntryText = string . Empty ;
394
412
_settings . NotepadSaveText = string . Empty ;
395
413
_settings . SaveDate = true ;
@@ -399,19 +417,26 @@ private void ResetAccountCheckbox_CheckedChanged(object sender, System.EventArgs
399
417
_settings . RequirePinCode = false ;
400
418
_settings . PinCode = null ;
401
419
420
+ // Reset the IsRestartingForUpdate flag to ensure it doesn't interfere
421
+ _settings . IsRestartingForUpdate = false ;
422
+
423
+ // Save the updated settings
402
424
SettingsManager . SaveSettings ( _settings ) ;
403
425
426
+ // Clear in-memory secure password
404
427
securePassword ? . Dispose ( ) ;
405
428
securePassword = null ;
406
429
430
+ // Update UI to reflect reset state
407
431
UserLoginButton . Text = "Register" ;
408
432
UserConfirmPassword . Visible = true ;
409
433
UserPassword . Location = new System . Drawing . Point ( 300 , 100 ) ;
410
434
PasswordGenBox . Visible = true ;
411
435
RegenPassButton . Visible = true ;
412
436
UsePassButton . Visible = true ;
413
437
PasswordLengthSlider . Visible = true ;
414
-
438
+
439
+ // Restart the application
415
440
Application . Restart ( ) ;
416
441
}
417
442
else
@@ -666,6 +691,8 @@ private void UserLoginButton_Click(object sender, EventArgs e)
666
691
// return;
667
692
//}
668
693
694
+ PasswordStrength . Visible = false ;
695
+
669
696
// Checks if any of the supported password managers are installed
670
697
string [ ] supportedManagers = { "Bitwarden" , "KeePass Password Safe 2" , "1Password" , "LastPass" , "ProtonPass" , "NordPass" } ;
671
698
string managerToOpen = null ;
@@ -927,7 +954,7 @@ private void LeftMenuNav_AfterSelect(object sender, TreeViewEventArgs e)
927
954
928
955
private void ExportEntriesButton_Click ( object sender , System . EventArgs e )
929
956
{
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 ) ;
931
958
// Export the entries to a file.
932
959
// Also ask the user where to export it to.
933
960
SaveFileDialog saveFileDialog = new SaveFileDialog ( ) ;
@@ -1383,22 +1410,33 @@ private void EditEntryButton_Click(object sender, EventArgs e)
1383
1410
{
1384
1411
// Programmatically switch to the "Journal" tab
1385
1412
TabControl . SelectedTab = JournalEntryPage ;
1413
+
1386
1414
// Get the selected item
1387
1415
ListViewItem selectedItem = EntriesListBox . SelectedItems [ 0 ] ;
1388
- // Populate journalEntryBox with the text of the selected entry (without date and time)
1389
1416
string selectedText = selectedItem . Text ;
1417
+
1418
+ // Check if the entry contains a date prefix
1390
1419
int index = selectedText . IndexOf ( " - " ) ;
1391
1420
if ( index != - 1 )
1392
1421
{
1422
+ // Extract the text after the date and time
1393
1423
JournalEntryBox . Text = selectedText . Substring ( index + 3 ) ;
1394
1424
}
1425
+ else
1426
+ {
1427
+ // If no date prefix, use the entire text
1428
+ JournalEntryBox . Text = selectedText ;
1429
+ }
1395
1430
1396
1431
// Set focus to the journalEntryBox
1397
1432
JournalEntryBox . Focus ( ) ;
1433
+
1398
1434
// Hide the editEntryButton
1399
1435
EditEntryButton . Hide ( ) ;
1436
+
1400
1437
// Disable the tabControl
1401
1438
JournalTabSelector . Enabled = false ;
1439
+
1402
1440
// Change the button text to indicate editing
1403
1441
SaveEntryButton . Text = "Save Edit" ;
1404
1442
EditEntryButton . Size = new Size ( 107 , 36 ) ;
@@ -1608,6 +1646,11 @@ private bool VerifyPassword(string enteredPassword, string storedHash)
1608
1646
1609
1647
private void SaveEntries ( )
1610
1648
{
1649
+ if ( _settings . Entries == null )
1650
+ {
1651
+ _settings . Entries = string . Empty ;
1652
+ }
1653
+
1611
1654
if ( EntriesListBox . Items . Count != 0 )
1612
1655
{
1613
1656
string [ ] entries = new string [ EntriesListBox . Items . Count ] ;
@@ -1616,7 +1659,7 @@ private void SaveEntries()
1616
1659
string entryText = EntriesListBox . Items [ i ] . ToString ( ) . Replace ( "ListViewItem: {" , "" ) . Replace ( "}" , "" ) ;
1617
1660
entries [ i ] = EncryptString ( entryText . Trim ( ) , ConvertToUnsecureString ( securePassword ) ) ;
1618
1661
}
1619
- _settings . Entries = string . Join ( "," , entries ) ; // Use comma as a delimiter instead of newline
1662
+ _settings . Entries = string . Join ( "," , entries ) ;
1620
1663
}
1621
1664
else
1622
1665
{
0 commit comments