Reworked close/auto save functions

This commit is contained in:
Alexander 2019-10-13 14:59:01 +03:00
parent c0c806560d
commit 4cdb97be4c

View file

@ -1,4 +1,4 @@
using Crypto_Notepad.Properties; using Crypto_Notepad.Properties;
using System; using System;
using System.ComponentModel; using System.ComponentModel;
using System.Diagnostics; using System.Diagnostics;
@ -8,7 +8,6 @@
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Reflection; using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
@ -19,7 +18,7 @@ public partial class MainForm : Form
{ {
Settings settings = Settings.Default; Settings settings = Settings.Default;
readonly string[] args = Environment.GetCommandLineArgs(); readonly string[] args = Environment.GetCommandLineArgs();
bool preventExit = false; bool cancelPressed = false;
string filePath = ""; string filePath = "";
string argsPath = ""; string argsPath = "";
int findPos = 0; int findPos = 0;
@ -258,80 +257,36 @@ private void DeleteUpdateFiles()
} }
} }
private void SaveConfirm(bool exit) private void SaveConfirm()
{ {
if (WindowState == FormWindowState.Normal) if (richTextBox.Modified)
{ {
settings.windowSize = Size; if (PublicVar.openFileName == string.Empty | PublicVar.openFileName == null)
settings.windowLocation = Location;
settings.windowState = WindowState;
}
if (WindowState == FormWindowState.Maximized)
{
settings.windowState = WindowState;
}
settings.Save();
if (!richTextBox.Modified)
{
if (exit)
{
trayIcon.Visible = false;
Application.Exit();
}
}
else
{
if (PublicVar.openFileName == null)
{ {
PublicVar.openFileName = "Unnamed.cnp"; PublicVar.openFileName = "Unnamed.cnp";
} }
string messageBoxText;
if (PublicVar.openFileName == String.Empty) if (!PublicVar.keyChanged)
{ {
PublicVar.openFileName = "Unnamed.cnp"; messageBoxText = "Save file: " + "\"" + PublicVar.openFileName + "\"" + " ? ";
}
else
{
messageBoxText = "Save file: " + "\"" + PublicVar.openFileName + "\"" + " with a new key? ";
} }
if (richTextBox.Text != "") using (new CenterWinDialog(this))
{ {
string messageBoxText;
if (!PublicVar.keyChanged)
{
messageBoxText = "Save file: " + "\"" + PublicVar.openFileName + "\"" + " ? ";
}
else
{
messageBoxText = "Save file: " + "\"" + PublicVar.openFileName + "\"" + " with a new key? ";
}
using (new CenterWinDialog(this))
{
DialogResult res = MessageBox.Show(this, messageBoxText, PublicVar.appName, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); DialogResult res = MessageBox.Show(this, messageBoxText, PublicVar.appName, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if (res == DialogResult.Yes) if (res == DialogResult.Yes)
{ {
SaveMainMenu_Click(this, new EventArgs()); trayIcon.Visible = false;
if (exit)
{
trayIcon.Visible = false;
SaveMainMenu_Click(this, new EventArgs()); SaveMainMenu_Click(this, new EventArgs());
}
}
if (res == DialogResult.No)
{
if (exit)
{
trayIcon.Visible = false;
Application.Exit();
}
}
if (res == DialogResult.Cancel)
{
preventExit = true;
}
} }
if (res == DialogResult.Cancel)
{
cancelPressed = true;
}
} }
} }
} }
@ -414,7 +369,7 @@ private async void StatusPanelMessage(string type)
{ {
string ready = "Ready"; string ready = "Ready";
if (statusLabel.Text == "New version is available") if (statusPanelLabel.Text == "New version is available")
{ {
ready = "New version is available"; ready = "New version is available";
} }
@ -485,10 +440,10 @@ private void LockFile()
{ {
try try
{ {
TypedPassword.Value = txtKey.Text; TypedPassword.Value = fileLockedKeyTextBox.Text;
string opnfile = File.ReadAllText(filePath); string opnfile = File.ReadAllText(filePath);
string de = AES.Decrypt(opnfile, TypedPassword.Value, null, settings.HashAlgorithm, Convert.ToInt32(settings.PasswordIterations), Convert.ToInt32(settings.KeySize)); string de = AES.Decrypt(opnfile, TypedPassword.Value, null, settings.HashAlgorithm, Convert.ToInt32(settings.PasswordIterations), Convert.ToInt32(settings.KeySize));
pnlEnterKey.Visible = false; fileLockedPanel.Visible = false;
richTextBox.Focus(); richTextBox.Focus();
richTextBox.Text = de; richTextBox.Text = de;
richTextBox.SelectionStart = caretPos; richTextBox.SelectionStart = caretPos;
@ -525,7 +480,7 @@ private void LoadSettings()
if (settings.editorRightToLeft) if (settings.editorRightToLeft)
{ {
richTextBox.RightToLeft = RightToLeft.Yes; richTextBox.RightToLeft = RightToLeft.Yes;
RTBLineNumbers.Dock = DockStyle.Right; lineNumbers.Dock = DockStyle.Right;
rightToLeftContextMenu.Checked = true; rightToLeftContextMenu.Checked = true;
} }
else else
@ -733,28 +688,69 @@ private void MainWindow_Activated(object sender, EventArgs e)
private void MainWindow_FormClosing(object sender, FormClosingEventArgs e) private void MainWindow_FormClosing(object sender, FormClosingEventArgs e)
{ {
if (settings.closeToTray) if (WindowState == FormWindowState.Normal)
{ {
if (settings.autoLock & PublicVar.encryptionKey.Get() != null) settings.windowSize = Size;
{ settings.windowLocation = Location;
pnlEnterKey.Visible = true; settings.windowState = WindowState;
}
Hide();
e.Cancel = true;
return;
} }
SaveConfirm(true); if (WindowState == FormWindowState.Maximized)
if (preventExit)
{ {
settings.windowState = WindowState;
}
settings.Save();
if (settings.closeToTray)
{
if (e.CloseReason == CloseReason.UserClosing)
{
if (settings.autoLock & PublicVar.encryptionKey.Get() != null)
{
fileLockedPanel.Visible = true;
}
Hide();
e.Cancel = true;
return;
}
}
if (richTextBox.Modified)
{
if (PublicVar.openFileName == string.Empty | PublicVar.openFileName == null)
{
PublicVar.openFileName = "Unnamed.cnp";
}
if (richTextBox.Text != "")
{
string messageBoxText;
if (!PublicVar.keyChanged)
{
messageBoxText = "Save file: " + "\"" + PublicVar.openFileName + "\"" + " ? ";
}
else
{
messageBoxText = "Save file: " + "\"" + PublicVar.openFileName + "\"" + " with a new key? ";
}
using (new CenterWinDialog(this))
{
DialogResult res = MessageBox.Show(this, messageBoxText, PublicVar.appName, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); DialogResult res = MessageBox.Show(this, messageBoxText, PublicVar.appName, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if (res == DialogResult.Yes) if (res == DialogResult.Yes)
{ {
trayIcon.Visible = false; trayIcon.Visible = false;
SaveMainMenu_Click(this, new EventArgs()); SaveMainMenu_Click(this, new EventArgs());
e.Cancel = true; }
}
if (res == DialogResult.Cancel)
{
e.Cancel = true;
}
}
}
}
} }
private void MainForm_Shown(object sender, EventArgs e) private void MainForm_Shown(object sender, EventArgs e)
@ -873,7 +869,12 @@ private void RichTextBox_LinkClicked(object sender, LinkClickedEventArgs e)
private void RichTextBox_DragDrop(object sender, DragEventArgs e) private void RichTextBox_DragDrop(object sender, DragEventArgs e)
{ {
SaveConfirm(false); SaveConfirm();
if (cancelPressed)
{
cancelPressed = false;
return;
}
string[] FileList = (string[])e.Data.GetData(DataFormats.FileDrop, false); string[] FileList = (string[])e.Data.GetData(DataFormats.FileDrop, false);
foreach (string file in FileList) openFileDialog.FileName = file; foreach (string file in FileList) openFileDialog.FileName = file;
object fname = e.Data.GetData("FileDrop"); object fname = e.Data.GetData("FileDrop");
@ -883,12 +884,6 @@ private void RichTextBox_DragDrop(object sender, DragEventArgs e)
var list = fname as string[]; var list = fname as string[];
if (list != null && !string.IsNullOrWhiteSpace(list[0])) if (list != null && !string.IsNullOrWhiteSpace(list[0]))
{ {
if (preventExit)
{
preventExit = false;
return;
}
if (!openFileDialog.FileName.Contains(".cnp")) if (!openFileDialog.FileName.Contains(".cnp"))
{ {
string opnfile = File.ReadAllText(openFileDialog.FileName); string opnfile = File.ReadAllText(openFileDialog.FileName);
@ -913,21 +908,21 @@ private void RichTextBox_CursorPositionChanged(object sender, EventArgs e)
StatusPanelTextInfo(); StatusPanelTextInfo();
} }
private void StatusLabel_TextChanged(object sender, EventArgs e) private void StatusPanelLabel_TextChanged(object sender, EventArgs e)
{ {
if (statusLabel.Text == "New version is available") if (statusPanelLabel.Text == "New version is available")
{ {
statusLabel.IsLink = true; statusPanelLabel.IsLink = true;
} }
else else
{ {
statusLabel.IsLink = false; statusPanelLabel.IsLink = false;
} }
} }
private void StatusLabel_Click(object sender, EventArgs e) private void StatusPanelLabel_Click(object sender, EventArgs e)
{ {
if (statusLabel.Text == "New version is available") if (statusPanelLabel.Text == "New version is available")
{ {
string exePath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + @"\"; string exePath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + @"\";
using (new CenterWinDialog(this)) using (new CenterWinDialog(this))
@ -953,7 +948,12 @@ private void StatusLabel_Click(object sender, EventArgs e)
/*File*/ /*File*/
private void NewMainMenu_Click(object sender, EventArgs e) private void NewMainMenu_Click(object sender, EventArgs e)
{ {
SaveConfirm(false); SaveConfirm();
if (cancelPressed)
{
cancelPressed = false;
return;
}
PublicVar.openFileName = "Unnamed.cnp"; PublicVar.openFileName = "Unnamed.cnp";
EnterKeyForm enterKeyForm = new EnterKeyForm EnterKeyForm enterKeyForm = new EnterKeyForm
{ {
@ -995,15 +995,13 @@ private void NewMainMenu_Click(object sender, EventArgs e)
private void OpenMainMenu_Click(object sender, EventArgs e) private void OpenMainMenu_Click(object sender, EventArgs e)
{ {
openFileDialog.FileName = ""; SaveConfirm();
SaveConfirm(false); if (cancelPressed)
if (preventExit)
{ {
preventExit = false; cancelPressed = false;
return; return;
} }
openFileDialog.FileName = "";
if (openFileDialog.ShowDialog() != DialogResult.OK) return; if (openFileDialog.ShowDialog() != DialogResult.OK) return;
{ {
PublicVar.openFileName = Path.GetFileName(openFileDialog.FileName); PublicVar.openFileName = Path.GetFileName(openFileDialog.FileName);
@ -1118,11 +1116,7 @@ private void DeleteFileToolStripMenuItem_Click(object sender, EventArgs e)
private void ExitMainMenu_Click(object sender, EventArgs e) private void ExitMainMenu_Click(object sender, EventArgs e)
{ {
SaveConfirm(true); Application.Exit();
if (settings.closeToTray)
{
Environment.Exit(0);
}
} }
private void fileMainMenu_DropDownOpened(object sender, EventArgs e) private void fileMainMenu_DropDownOpened(object sender, EventArgs e)
@ -1667,7 +1661,7 @@ private void FileLockedPanel_VisibleChanged(object sender, EventArgs e)
toolbarPanel.Enabled = true; toolbarPanel.Enabled = true;
searchPanel.Enabled = true; searchPanel.Enabled = true;
mainMenu.Enabled = true; mainMenu.Enabled = true;
txtKey.Text = null; fileLockedKeyTextBox.Text = null;
} }
} }