Skip to content

Commit

Permalink
Code cleanup, some refactors, nothing important.
Browse files Browse the repository at this point in the history
Added UnitTests for fun.

Signed-off-by: stevenstrike <[email protected]>
  • Loading branch information
stevenstrike committed Mar 30, 2019
1 parent b814b0b commit 167bc5a
Show file tree
Hide file tree
Showing 16 changed files with 285 additions and 84 deletions.
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
################################################################################

/.vs/StartMenuCleaner/v15
/StartMenuCleaner/bin/Debug
/StartMenuCleaner/obj/Debug
/StartMenuCleaner/bin
/StartMenuCleaner/obj
/StartMenuCleaner/StartMenuCleaner.csproj.user
/StartMenuCleaner_UnitTestProject/obj
/StartMenuCleaner_UnitTestProject/bin
/packages
13 changes: 11 additions & 2 deletions StartMenuCleaner.sln
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.23107.0
# Visual Studio 15
VisualStudioVersion = 15.0.28307.539
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StartMenuCleaner", "StartMenuCleaner\StartMenuCleaner.csproj", "{9D3FDFD0-445E-4F32-8661-DB02DD44C8A5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StartMenuCleaner_UnitTestProject", "StartMenuCleaner_UnitTestProject\StartMenuCleaner_UnitTestProject.csproj", "{787897BF-429E-4893-B4F8-CC72332D83E2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -15,8 +17,15 @@ Global
{9D3FDFD0-445E-4F32-8661-DB02DD44C8A5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9D3FDFD0-445E-4F32-8661-DB02DD44C8A5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9D3FDFD0-445E-4F32-8661-DB02DD44C8A5}.Release|Any CPU.Build.0 = Release|Any CPU
{787897BF-429E-4893-B4F8-CC72332D83E2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{787897BF-429E-4893-B4F8-CC72332D83E2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{787897BF-429E-4893-B4F8-CC72332D83E2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{787897BF-429E-4893-B4F8-CC72332D83E2}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {0F9041B5-7905-4B94-9972-AD70F06C28EF}
EndGlobalSection
EndGlobal
25 changes: 8 additions & 17 deletions StartMenuCleaner/Classes/FormTools/ListBoxLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ public sealed class ListBoxLog : IDisposable

private bool _disposed;
private ListBox _listBox;
private string _messageFormat;
private int _maxEntriesInListBox;
private readonly string _messageFormat;
private readonly int _maxEntriesInListBox;
private bool _canAdd;
private bool _paused;

private void OnHandleCreated(object sender, EventArgs e)
{
Expand All @@ -33,10 +32,8 @@ private void DrawItemHandler(object sender, DrawItemEventArgs e)
e.DrawBackground();
e.DrawFocusRectangle();

LogEvent logEvent = ((ListBox)sender).Items[e.Index] as LogEvent;

// SafeGuard against wrong configuration of list box
if (logEvent == null)
if (!(((ListBox)sender).Items[e.Index] is LogEvent logEvent))
{
logEvent = new LogEvent(Enums.LogLevel.Critical, ((ListBox)sender).Items[e.Index].ToString());
}
Expand Down Expand Up @@ -84,8 +81,7 @@ private void CopyMenuOnClickHandler(object sender, EventArgs e)
}
private void CopyMenuPopupHandler(object sender, EventArgs e)
{
ContextMenu menu = sender as ContextMenu;
if (menu != null)
if (sender is ContextMenu menu)
{
menu.MenuItems[0].Enabled = (_listBox.SelectedItems.Count > 0);
}
Expand All @@ -101,10 +97,10 @@ public LogEvent(Enums.LogLevel level, string message)
}

public readonly DateTime EventTime;

public readonly Enums.LogLevel Level;
public readonly string Message;
}

private void WriteEvent(LogEvent logEvent)
{
if ((logEvent != null) && (_canAdd))
Expand All @@ -122,7 +118,7 @@ private void AddALogEntry(object item)
_listBox.Items.RemoveAt(0);
}

if (!_paused) _listBox.TopIndex = _listBox.Items.Count - 1;
if (!Paused) _listBox.TopIndex = _listBox.Items.Count - 1;
}
private string LevelName(Enums.LogLevel level)
{
Expand Down Expand Up @@ -171,7 +167,6 @@ private void CopyToClipboard()
System.Diagnostics.Debug.WriteLine(selectedItemsAsRTFText.ToString());
Clipboard.SetData(DataFormats.Rtf, selectedItemsAsRTFText.ToString());
}

}

public ListBoxLog(ListBox listBox) : this(listBox, DEFAULT_MESSAGE_FORMAT, DEFAULT_MAX_LINES_IN_LISTBOX) { }
Expand All @@ -184,7 +179,7 @@ public ListBoxLog(ListBox listBox, string messageFormat, int maxLinesInListbox)
_messageFormat = messageFormat;
_maxEntriesInListBox = maxLinesInListbox;

_paused = false;
Paused = false;

_canAdd = listBox.IsHandleCreated;

Expand All @@ -210,11 +205,7 @@ public void Log(Enums.LogLevel level, string message)
WriteEvent(new LogEvent(level, message));
}

public bool Paused
{
get { return _paused; }
set { _paused = value; }
}
public bool Paused { get; set; }

~ListBoxLog()
{
Expand Down
31 changes: 24 additions & 7 deletions StartMenuCleaner/Classes/SMCleaner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace StartMenuCleaner
/// <summary>
/// Start Menu Cleaner Class.
/// </summary>
class SMCleaner
public class SMCleaner
{
#region Private Attributes
/// <summary>
Expand All @@ -35,6 +35,7 @@ class SMCleaner
public static List<FileInfo> EnumerateAllLnkFiles()
{
List<FileInfo> fileInfos = new List<FileInfo>();

try
{
foreach (FileInfo fi in StartMenuPath.SelectMany(x => new DirectoryInfo(x).EnumerateFiles("*.lnk", SearchOption.AllDirectories)))
Expand All @@ -43,9 +44,8 @@ public static List<FileInfo> EnumerateAllLnkFiles()
}
return fileInfos;
}
catch (System.Exception ex)
catch (Exception)
{
Console.WriteLine(ex.Message);
throw;
}
}
Expand All @@ -59,6 +59,7 @@ public static List<FileInfo> EnumerateAllLnkFiles()
public static List<string> NormalScanFilter(List<FileInfo> searchResult)
{
List<string> returnResults = new List<string>();

try
{
foreach (string wordToRemove in DefaultWordsToRemove)
Expand All @@ -73,9 +74,8 @@ public static List<string> NormalScanFilter(List<FileInfo> searchResult)
}
return returnResults;
}
catch (System.Exception excpt)
catch (Exception)
{
Console.WriteLine(excpt.Message);
throw;
}
}
Expand All @@ -91,6 +91,7 @@ public static List<string> CustomScanFilter(List<FileInfo> searchResult, string
{
List<string> returnResults = new List<string>();
List<string> customDictionnary = new List<string>();

try
{
customDictionnary = customSearchEntry.Split(';').ToList();
Expand All @@ -106,9 +107,25 @@ public static List<string> CustomScanFilter(List<FileInfo> searchResult, string
}
return returnResults;
}
catch (System.Exception excpt)
catch (Exception)
{
throw;
}
}

/// <summary>
/// Removes the shortcut file.
/// </summary>
/// <param name="filePath">The file path.</param>
public static bool RemoveShortcutFile(string filePath)
{
try
{
File.Delete(filePath);
return true;
}
catch(Exception)
{
Console.WriteLine(excpt.Message);
throw;
}
}
Expand Down
2 changes: 2 additions & 0 deletions StartMenuCleaner/MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

92 changes: 60 additions & 32 deletions StartMenuCleaner/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,34 @@ private void AddResultToCheckedListBox(List<string> list)
});
}
}
catch (System.Exception excpt)
catch (Exception ex)
{
Console.WriteLine(excpt.Message);
MyListBoxLog.Log(Enums.LogLevel.Error, excpt.Message);
Console.WriteLine(ex.Message);
MyListBoxLog.Log(Enums.LogLevel.Error, ex.Message);
}
}

/// <summary>
/// Removes the start menu entries.
/// </summary>
private void RemoveStartMenuShortcutEntry()
{
foreach (var item in resultsCheckedListBox.CheckedItems.OfType<string>().ToList())
{
try
{
//Remove file.
SMCleaner.RemoveShortcutFile(item.ToString());
//Remove item from listbox.
resultsCheckedListBox.Items.Remove(item);

MyListBoxLog.Log(Enums.LogLevel.Info, String.Format("Item {0} deleted.", item.ToString()));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
MyListBoxLog.Log(Enums.LogLevel.Error, ex.Message);
}
}
}

Expand All @@ -91,10 +115,10 @@ private void MainForm_Load(object sender, EventArgs e)

customSearchTextBox.Text = CustomSearchTextBoxDefaultText;
}
catch (System.Exception excpt)
catch (Exception ex)
{
Console.WriteLine(excpt.Message);
MyListBoxLog.Log(Enums.LogLevel.Error, excpt.Message);
Console.WriteLine(ex.Message);
MyListBoxLog.Log(Enums.LogLevel.Error, ex.Message);
}
}

Expand All @@ -114,10 +138,10 @@ private void scanButton_Click(object sender, EventArgs e)
}

var fileInfos = SMCleaner.EnumerateAllLnkFiles();
this.SearchResult = SMCleaner.NormalScanFilter(fileInfos);
AddResultToCheckedListBox(this.SearchResult);
SearchResult = SMCleaner.NormalScanFilter(fileInfos);
AddResultToCheckedListBox(SearchResult);

MyListBoxLog.Log(Enums.LogLevel.Info, String.Format("Regular Scan: Found {0} items.", this.SearchResult.Count()));
MyListBoxLog.Log(Enums.LogLevel.Info, String.Format("Regular Scan: Found {0} items.", SearchResult.Count()));
}
catch(Exception ex)
{
Expand Down Expand Up @@ -145,16 +169,16 @@ private void scanCustomButton_Click(object sender, EventArgs e)
}

var fileInfos = SMCleaner.EnumerateAllLnkFiles();
this.SearchResult = SMCleaner.CustomScanFilter(fileInfos, customSearchTextBox.Text);
AddResultToCheckedListBox(this.SearchResult);
SearchResult = SMCleaner.CustomScanFilter(fileInfos, customSearchTextBox.Text);
AddResultToCheckedListBox(SearchResult);

MyListBoxLog.Log(Enums.LogLevel.Info, String.Format("Custom Scan: Found {0} items.", this.SearchResult.Count()));
MyListBoxLog.Log(Enums.LogLevel.Info, String.Format("Custom Scan: Found {0} items.", SearchResult.Count()));
}
}
catch (System.Exception excpt)
catch (Exception ex)
{
Console.WriteLine(excpt.Message);
MyListBoxLog.Log(Enums.LogLevel.Error, excpt.Message);
Console.WriteLine(ex.Message);
MyListBoxLog.Log(Enums.LogLevel.Error, ex.Message);
}
}

Expand All @@ -175,29 +199,15 @@ private void removeButton_Click(object sender, EventArgs e)
MessageBoxButtons.YesNo);
if (confirmResult == DialogResult.Yes)
{
foreach (var item in resultsCheckedListBox.CheckedItems.OfType<string>().ToList())
{
try
{
File.Delete(item.ToString());
resultsCheckedListBox.Items.Remove(item);

MyListBoxLog.Log(Enums.LogLevel.Info, String.Format("Item {0} deleted.", item.ToString()));
}
catch (System.Exception ex)
{
Console.WriteLine(ex.Message);
MyListBoxLog.Log(Enums.LogLevel.Error, ex.Message);
}
}
RemoveStartMenuShortcutEntry();
}
else
{
MyListBoxLog.Log(Enums.LogLevel.Info, "User cancelled the operation.");
}
}
}
catch (System.Exception ex)
catch (Exception ex)
{
Console.WriteLine(ex.Message);
MyListBoxLog.Log(Enums.LogLevel.Error, ex.Message);
Expand Down Expand Up @@ -271,7 +281,25 @@ private void customSearchTextBox_Leave(object sender, EventArgs e)
{
customSearchTextBox.Text = CustomSearchTextBoxDefaultText;
}
}
}

/// <summary>
/// Handles the SelectedIndexChanged event of the resultsCheckedListBox control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
private void resultsCheckedListBox_SelectedIndexChanged(object sender, EventArgs e)
{
// Enable the remove button when at least one item is selected.
if (resultsCheckedListBox.CheckedItems.Count > 0)
{
removeButton.Enabled = true;
}
else
{
removeButton.Enabled = false;
}
}
#endregion
}
}
4 changes: 2 additions & 2 deletions StartMenuCleaner/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ static class Program
static void Main()
{
//Get App name + version (+fixed name and current build date) from assembly
string[] applicationInfo = new string[4] { Application.ProductName, Application.ProductVersion, "StevenStrike", Properties.Resources.BuildDate };
string[] applicationInfo = new string[4] { Application.ProductName, Application.ProductVersion, "StevenStrike", Properties.Resources.BuildDate.Trim() };

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

//Overload the Main Form to change its text dynamically (version, date..)
MainForm mf1 = new MainForm
{
Text = String.Format("{0} {1} {2} {3}", applicationInfo[0], applicationInfo[1], applicationInfo[2], applicationInfo[3])
Text = String.Format("{0} - v{1} by {2} on {3}.", applicationInfo[0], applicationInfo[1], applicationInfo[2], applicationInfo[3])
};
mf1.ShowDialog();
}
Expand Down
2 changes: 1 addition & 1 deletion StartMenuCleaner/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@
[assembly: AssemblyVersion("1.0.0.3")]
[assembly: AssemblyFileVersion("1.0.0.3")]
[assembly: NeutralResourcesLanguage("en-US")]

[assembly: InternalsVisibleTo("StartMenuCleaner_UnitTestProject")]
Loading

0 comments on commit 167bc5a

Please sign in to comment.