From 4b8221ae352dd2e3517bd9e7c781f680bcf271c0 Mon Sep 17 00:00:00 2001 From: adnan29979 <148310766+adnan29979@users.noreply.github.com> Date: Tue, 2 Jul 2024 00:31:04 +0530 Subject: [PATCH] clickable headers to sort files and show date modified for files/folders --- src/FileViewer.Designer.cs | 23 ++++---- src/FileViewer.cs | 109 ++++++++++++++++++++++++++++++++++--- src/FileViewer.resx | 26 ++++----- 3 files changed, 124 insertions(+), 34 deletions(-) diff --git a/src/FileViewer.Designer.cs b/src/FileViewer.Designer.cs index 94f5207..55143b2 100644 --- a/src/FileViewer.Designer.cs +++ b/src/FileViewer.Designer.cs @@ -61,6 +61,7 @@ namespace MeshCentralRouter this.rightListView = new System.Windows.Forms.ListView(); this.columnHeader3 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); this.columnHeader4 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); + this.columnHeader5 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); this.remoteContextMenuStrip = new System.Windows.Forms.ContextMenuStrip(this.components); this.renameToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.compressToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); @@ -74,6 +75,7 @@ namespace MeshCentralRouter this.leftListView = new System.Windows.Forms.ListView(); this.columnHeader1 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); this.columnHeader2 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); + this.columnHeader6 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); this.localContextMenuStrip = new System.Windows.Forms.ContextMenuStrip(this.components); this.renameToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem(); this.deleteToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem(); @@ -81,8 +83,6 @@ namespace MeshCentralRouter this.refreshToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem(); this.leftTopPanel = new System.Windows.Forms.Panel(); this.localLabel = new System.Windows.Forms.Label(); - this.columnHeader5 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); - this.columnHeader6 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); this.topPanel.SuspendLayout(); this.statusStrip.SuspendLayout(); this.mainTableLayoutPanel.SuspendLayout(); @@ -298,7 +298,6 @@ namespace MeshCentralRouter this.rightListView.ContextMenuStrip = this.remoteContextMenuStrip; resources.ApplyResources(this.rightListView, "rightListView"); this.rightListView.FullRowSelect = true; - this.rightListView.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Nonclickable; this.rightListView.HideSelection = false; this.rightListView.Name = "rightListView"; this.rightListView.SmallImageList = this.fileIconImageList; @@ -319,6 +318,10 @@ namespace MeshCentralRouter // resources.ApplyResources(this.columnHeader4, "columnHeader4"); // + // columnHeader5 + // + resources.ApplyResources(this.columnHeader5, "columnHeader5"); + // // remoteContextMenuStrip // this.remoteContextMenuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { @@ -404,7 +407,6 @@ namespace MeshCentralRouter this.leftListView.ContextMenuStrip = this.localContextMenuStrip; resources.ApplyResources(this.leftListView, "leftListView"); this.leftListView.FullRowSelect = true; - this.leftListView.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Nonclickable; this.leftListView.HideSelection = false; this.leftListView.Name = "leftListView"; this.leftListView.SmallImageList = this.fileIconImageList; @@ -425,6 +427,10 @@ namespace MeshCentralRouter // resources.ApplyResources(this.columnHeader2, "columnHeader2"); // + // columnHeader6 + // + resources.ApplyResources(this.columnHeader6, "columnHeader6"); + // // localContextMenuStrip // this.localContextMenuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { @@ -475,14 +481,6 @@ namespace MeshCentralRouter resources.ApplyResources(this.localLabel, "localLabel"); this.localLabel.Name = "localLabel"; // - // columnHeader5 - // - resources.ApplyResources(this.columnHeader5, "columnHeader5"); - // - // columnHeader6 - // - resources.ApplyResources(this.columnHeader6, "columnHeader6"); - // // FileViewer // resources.ApplyResources(this, "$this"); @@ -566,4 +564,3 @@ namespace MeshCentralRouter private ColumnHeader columnHeader6; } } - diff --git a/src/FileViewer.cs b/src/FileViewer.cs index 2cd15a3..bf22a2d 100644 --- a/src/FileViewer.cs +++ b/src/FileViewer.cs @@ -43,6 +43,8 @@ namespace MeshCentralRouter private static string rndString = getRandomString(12); private bool skipExistingFiles = false; private FileDialogMsgForm msgForm = null; + private bool localSortAscending = true; + private bool remoteSortAscending = true; // Stats public long bytesIn = 0; @@ -93,9 +95,100 @@ namespace MeshCentralRouter // Load the local path from the registry string lp = Settings.GetRegValue("LocalPath", ""); if((lp != "") && (Directory.Exists(lp))) { localFolder = new DirectoryInfo(lp); } + + // Add ColumnClick event handlers + leftListView.ColumnClick += new ColumnClickEventHandler(LeftListView_ColumnClick); + rightListView.ColumnClick += new ColumnClickEventHandler(RightListView_ColumnClick); } - public bool updateLocalFileView() + private void LeftListView_ColumnClick(object sender, ColumnClickEventArgs e) + { + if (localFolder == null) return; + + if (e.Column == 1) // Size column + { + localSortAscending = !localSortAscending; + leftListView.ListViewItemSorter = new ListViewItemComparer(e.Column, localSortAscending, isNumeric: true); + } + else if (e.Column == 2) // Date column + { + localSortAscending = !localSortAscending; + leftListView.ListViewItemSorter = new ListViewItemComparer(e.Column, localSortAscending, isDate: true); + } + else // Name column or other columns + { + localSortAscending = !localSortAscending; + leftListView.ListViewItemSorter = new ListViewItemComparer(e.Column, localSortAscending); + } + } + + private void RightListView_ColumnClick(object sender, ColumnClickEventArgs e) + { + if (remoteFolderList == null) return; + + if (e.Column == 1) // Size column + { + remoteSortAscending = !remoteSortAscending; + rightListView.ListViewItemSorter = new ListViewItemComparer(e.Column, remoteSortAscending, isNumeric: true); + } + else if (e.Column == 2) // Date column + { + remoteSortAscending = !remoteSortAscending; + rightListView.ListViewItemSorter = new ListViewItemComparer(e.Column, remoteSortAscending, isDate: true); + } + else // Name column or other columns + { + remoteSortAscending = !remoteSortAscending; + rightListView.ListViewItemSorter = new ListViewItemComparer(e.Column, remoteSortAscending); + } + } + + public class ListViewItemComparer : IComparer + { + private int col; + private bool ascending; + private bool isNumeric; + private bool isDate; + + public ListViewItemComparer(int column, bool ascending, bool isNumeric = false, bool isDate = false) + { + this.col = column; + this.ascending = ascending; + this.isNumeric = isNumeric; + this.isDate = isDate; + } + + public int Compare(object x, object y) + { + int returnVal = 0; + ListViewItem item1 = (ListViewItem)x; + ListViewItem item2 = (ListViewItem)y; + + if (isNumeric) + { + long size1 = long.Parse(item1.SubItems[col].Text == "" ? "0" : item1.SubItems[col].Text); + long size2 = long.Parse(item2.SubItems[col].Text == "" ? "0" : item2.SubItems[col].Text); + returnVal = size1.CompareTo(size2); + } + else if (isDate) + { + DateTime date1 = DateTime.Parse(item1.SubItems[col].Text); + DateTime date2 = DateTime.Parse(item2.SubItems[col].Text); + returnVal = date1.CompareTo(date2); + } + else + { + returnVal = String.Compare(item1.SubItems[col].Text, item2.SubItems[col].Text); + } + + if (!ascending) returnVal = -returnVal; + + return returnVal; + } + } + + + public bool updateLocalFileView() { // Save the list of selected items List selectedItems = new List(); @@ -136,7 +229,7 @@ namespace MeshCentralRouter string[] si = new string[3]; si[0] = directory.Name; si[1] = ""; // Skipping size of directory because it is very compute consuming - si[2] = directory.LastWriteTime.ToString("dd-MM-yyyy hh:mm:ss tt"); // Add the date information + si[2] = directory.LastWriteTime.ToString(System.Globalization.CultureInfo.CurrentCulture); // Add the date information ListViewItem x = new ListViewItem(si, 1); x.Tag = directory; leftListView.Items.Add(x); @@ -148,7 +241,7 @@ namespace MeshCentralRouter string[] si = new string[3]; si[0] = file.Name; si[1] = "" + file.Length; - si[2] = file.LastWriteTime.ToString("dd-MM-yyyy hh:mm:ss tt"); // Add the date information + si[2] = file.LastWriteTime.ToString(System.Globalization.CultureInfo.CurrentCulture); // Add the date information ListViewItem x = new ListViewItem(si, 2); x.Tag = file; leftListView.Items.Add(x); @@ -245,16 +338,16 @@ namespace MeshCentralRouter { string[] si = new string[3]; si[0] = fileName; - si[1] = ""; // Skipping size of directory because it is very compute consuming - si[2] = fileDate != null ? DateTime.TryParse(fileDate, out DateTime parsedDate) ? parsedDate.ToString("dd-MM-yyyy hh:mm:ss tt") : "" : ""; + si[1] = ""; // Skipping size of directory because it is very compute consuming + si[2] = fileDate != null ? DateTime.TryParse(fileDate, out DateTime parsedDate) ? parsedDate.ToString(System.Globalization.CultureInfo.CurrentCulture) : "" : ""; // Add the date information sortlist.Add(new ListViewItem(si, 0)); // Drive } else if(fileIcon == 2) { string[] si = new string[3]; si[0] = fileName; - si[1] = ""; // Skipping size of directory because it is very compute consuming - si[2] = fileDate != null ? DateTime.TryParse(fileDate, out DateTime parsedDate) ? parsedDate.ToString("dd-MM-yyyy hh:mm:ss tt") : "" : ""; + si[1] = ""; // Skipping size of directory because it is very compute consuming + si[2] = fileDate != null ? DateTime.TryParse(fileDate, out DateTime parsedDate) ? parsedDate.ToString(System.Globalization.CultureInfo.CurrentCulture) : "" : ""; // Add the date information sortlist.Add(new ListViewItem(si, 1)); // Folder } } @@ -284,7 +377,7 @@ namespace MeshCentralRouter string[] si = new string[3]; si[0] = fileName; si[1] = "" + fileSize; - si[2] = fileDate != null ? DateTime.TryParse(fileDate, out DateTime parsedDate) ? parsedDate.ToString("dd-MM-yyyy hh:mm:ss tt") : "" : ""; // Add the date information + si[2] = fileDate != null ? DateTime.TryParse(fileDate, out DateTime parsedDate) ? parsedDate.ToString(System.Globalization.CultureInfo.CurrentCulture) : "" : ""; // Add the date information sortlist.Add(new ListViewItem(si, 2)); // File } } diff --git a/src/FileViewer.resx b/src/FileViewer.resx index 9a5ad92..a91a2d5 100644 --- a/src/FileViewer.resx +++ b/src/FileViewer.resx @@ -779,7 +779,7 @@ AAEAAAD/////AQAAAAAAAAAMAgAAAFdTeXN0ZW0uV2luZG93cy5Gb3JtcywgVmVyc2lvbj00LjAuMC4w LCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODkFAQAAACZTeXN0 ZW0uV2luZG93cy5Gb3Jtcy5JbWFnZUxpc3RTdHJlYW1lcgEAAAAERGF0YQcCAgAAAAkDAAAADwMAAAC+ - CQAAAk1TRnQBSQFMAgEBAwEAAcgBAAHIAQABEAEAARABAAT/AREBAAj/AUIBTQE2BwABNgMAASgDAAFA + CQAAAk1TRnQBSQFMAgEBAwEAAdABAAHQAQABEAEAARABAAT/AREBAAj/AUIBTQE2BwABNgMAASgDAAFA AwABEAMAAQEBAAEQBgABCDoAATABcwHKAWYBygFmAbUBexIAARABQgEQAUIBEAFCARABQgEQAUIBEAFC ARABQgEQAUIBEAFCARABQgEQAUIBEAFCARABQkYAAcoBZgHKAWYBygFmAcoBZgHKAWYBygFmAcoBZgHK AWYBygFmAcoBZgHKAWYBygFmATABcwYAARABQgHeAXsB3gF7Ad4BewHeAXsB3gF7Ad4BewHeAXsB3gF7 @@ -1587,6 +1587,12 @@ System.Windows.Forms.ColumnHeader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + columnHeader5 + + + System.Windows.Forms.ColumnHeader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + renameToolStripMenuItem @@ -1635,6 +1641,12 @@ System.Windows.Forms.ColumnHeader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + columnHeader6 + + + System.Windows.Forms.ColumnHeader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + renameToolStripMenuItem1 @@ -1659,18 +1671,6 @@ System.Windows.Forms.ToolStripMenuItem, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - columnHeader5 - - - System.Windows.Forms.ColumnHeader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - columnHeader6 - - - System.Windows.Forms.ColumnHeader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - FileViewer