From 40b2b8db5bf1d63df9d4b2ff4859ba6ba7808060 Mon Sep 17 00:00:00 2001 From: yankejustin Date: Sun, 12 Apr 2015 22:19:08 -0400 Subject: [PATCH] Rewrote a method and improved behavior Rewrote the File Manager Form's 'lstDirectory_DoubleClick' method so it is more clear, to prevent unnecessary strings being recreated, and because it was making the same decision twice when it didn't need to. Also placed the Setting Form's 'btnListen_Click' method in a try-finally block so if anything goes wrong in the method, the state of it won't be frozen (before, you would have to click out of the Form, discard changes, then re-open. Perhaps it would be advisable to notify the user of an issue with the listening if it failed... --- Server/Forms/FrmFileManager.cs | 21 +++++++++------------ Server/Forms/FrmSettings.cs | 32 ++++++++++++++++++++++---------- 2 files changed, 31 insertions(+), 22 deletions(-) diff --git a/Server/Forms/FrmFileManager.cs b/Server/Forms/FrmFileManager.cs index 587cf61a..c14c50a0 100644 --- a/Server/Forms/FrmFileManager.cs +++ b/Server/Forms/FrmFileManager.cs @@ -60,14 +60,14 @@ private void lstDirectory_DoubleClick(object sender, EventArgs e) { if (lstDirectory.SelectedItems.Count != 0) { - if (lstDirectory.SelectedItems[0].Tag.ToString() == "dir" && lstDirectory.SelectedItems[0].SubItems[0].Text == "..") + if (lstDirectory.SelectedItems[0].Tag.ToString() == "dir" && _connectClient.Value != null) { - if (_connectClient.Value != null) + if (lstDirectory.SelectedItems[0].SubItems[0].Text == "..") { - if (!_currentDir.EndsWith(@"\")) - _currentDir = _currentDir + @"\"; - - _currentDir = _currentDir.Remove(_currentDir.Length - 1); + if (_currentDir.EndsWith(@"\")) + { + _currentDir = _currentDir.Remove(_currentDir.Length - 1); + } if (_currentDir.Length > 2) _currentDir = _currentDir.Remove(_currentDir.LastIndexOf(@"\")); @@ -78,17 +78,14 @@ private void lstDirectory_DoubleClick(object sender, EventArgs e) new Core.Packets.ServerPackets.Directory(_currentDir).Execute(_connectClient); _connectClient.Value.LastDirectorySeen = false; } - } - else if (lstDirectory.SelectedItems[0].Tag.ToString() == "dir") - { - if (_connectClient.Value != null) + else { if (_connectClient.Value.LastDirectorySeen) { if (_currentDir.EndsWith(@"\")) - _currentDir = _currentDir + lstDirectory.SelectedItems[0].SubItems[0].Text; + _currentDir += lstDirectory.SelectedItems[0].SubItems[0].Text; else - _currentDir = _currentDir + @"\" + lstDirectory.SelectedItems[0].SubItems[0].Text; + _currentDir += @"\" + lstDirectory.SelectedItems[0].SubItems[0].Text; new Core.Packets.ServerPackets.Directory(_currentDir).Execute(_connectClient); _connectClient.Value.LastDirectorySeen = false; diff --git a/Server/Forms/FrmSettings.cs b/Server/Forms/FrmSettings.cs index a14c98b6..0693c010 100644 --- a/Server/Forms/FrmSettings.cs +++ b/Server/Forms/FrmSettings.cs @@ -36,20 +36,32 @@ private void btnListen_Click(object sender, EventArgs e) { if (btnListen.Text == "Start listening" && !_listenServer.Listening) { - if (chkUseUpnp.Checked) - Core.Helper.UPnP.ForwardPort(ushort.Parse(ncPort.Value.ToString())); + try + { + if (chkUseUpnp.Checked) + Core.Helper.UPnP.ForwardPort(ushort.Parse(ncPort.Value.ToString())); + _listenServer.Listen(ushort.Parse(ncPort.Value.ToString())); + } + finally + { - _listenServer.Listen(ushort.Parse(ncPort.Value.ToString())); - btnListen.Text = "Stop listening"; - ncPort.Enabled = false; - txtPassword.Enabled = false; + btnListen.Text = "Stop listening"; + ncPort.Enabled = false; + txtPassword.Enabled = false; + } } else if (btnListen.Text == "Stop listening" && _listenServer.Listening) { - _listenServer.Disconnect(); - btnListen.Text = "Start listening"; - ncPort.Enabled = true; - txtPassword.Enabled = true; + try + { + _listenServer.Disconnect(); + } + finally + { + btnListen.Text = "Start listening"; + ncPort.Enabled = true; + txtPassword.Enabled = true; + } } }