From 1f9bcaf902c6d9c317c058125ed6fe4d04cc52a1 Mon Sep 17 00:00:00 2001 From: Jaex Date: Mon, 17 Jun 2019 04:14:04 +0300 Subject: [PATCH] Added "Show stats" button to history form --- ShareX.HelpersLib/Extensions/Extensions.cs | 15 ++++ .../Forms/HistoryForm.Designer.cs | 22 +++++ ShareX.HistoryLib/Forms/HistoryForm.cs | 85 ++++++++++++++++++- ShareX.HistoryLib/Forms/HistoryForm.resx | 78 +++++++++++++++-- .../Forms/CustomUploaderSettingsForm.cs | 16 ++-- 5 files changed, 197 insertions(+), 19 deletions(-) diff --git a/ShareX.HelpersLib/Extensions/Extensions.cs b/ShareX.HelpersLib/Extensions/Extensions.cs index 6af01f27d..7da18aa6a 100644 --- a/ShareX.HelpersLib/Extensions/Extensions.cs +++ b/ShareX.HelpersLib/Extensions/Extensions.cs @@ -694,5 +694,20 @@ public static void DoubleBuffered(this DataGridView dgv, bool value) PropertyInfo pi = dgv.GetType().GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic); pi.SetValue(dgv, value, null); } + + public static void AppendLine(this RichTextBox rtb, string value = "") + { + rtb.AppendText(value + Environment.NewLine); + } + + public static void SetFontRegular(this RichTextBox rtb) + { + rtb.SelectionFont = new Font(rtb.Font, FontStyle.Regular); + } + + public static void SetFontBold(this RichTextBox rtb) + { + rtb.SelectionFont = new Font(rtb.Font, FontStyle.Bold); + } } } \ No newline at end of file diff --git a/ShareX.HistoryLib/Forms/HistoryForm.Designer.cs b/ShareX.HistoryLib/Forms/HistoryForm.Designer.cs index ddfd6b571..45a64296f 100644 --- a/ShareX.HistoryLib/Forms/HistoryForm.Designer.cs +++ b/ShareX.HistoryLib/Forms/HistoryForm.Designer.cs @@ -31,12 +31,14 @@ private void InitializeComponent() { System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(HistoryForm)); this.scMain = new ShareX.HelpersLib.SplitContainerCustomSplitter(); + this.rtbStats = new System.Windows.Forms.RichTextBox(); this.lvHistory = new ShareX.HelpersLib.MyListView(); this.chIcon = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); this.chDateTime = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); this.chFilename = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); this.chURL = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); this.panel1 = new System.Windows.Forms.Panel(); + this.btnShowStats = new System.Windows.Forms.Button(); this.pbThumbnail = new ShareX.HelpersLib.MyPictureBox(); this.gbFilters = new System.Windows.Forms.GroupBox(); this.lblURLFilter = new System.Windows.Forms.Label(); @@ -70,13 +72,22 @@ private void InitializeComponent() // // scMain.Panel1 // + this.scMain.Panel1.Controls.Add(this.rtbStats); this.scMain.Panel1.Controls.Add(this.lvHistory); // // scMain.Panel2 // this.scMain.Panel2.Controls.Add(this.panel1); + this.scMain.SplitterColor = System.Drawing.Color.White; + this.scMain.SplitterLineColor = System.Drawing.Color.FromArgb(((int)(((byte)(189)))), ((int)(((byte)(189)))), ((int)(((byte)(189))))); this.scMain.SplitterMoved += new System.Windows.Forms.SplitterEventHandler(this.scMain_SplitterMoved); // + // rtbStats + // + this.rtbStats.BorderStyle = System.Windows.Forms.BorderStyle.None; + resources.ApplyResources(this.rtbStats, "rtbStats"); + this.rtbStats.Name = "rtbStats"; + // // lvHistory // this.lvHistory.AllowColumnSort = true; @@ -117,11 +128,19 @@ private void InitializeComponent() // // panel1 // + this.panel1.Controls.Add(this.btnShowStats); this.panel1.Controls.Add(this.pbThumbnail); this.panel1.Controls.Add(this.gbFilters); resources.ApplyResources(this.panel1, "panel1"); this.panel1.Name = "panel1"; // + // btnShowStats + // + resources.ApplyResources(this.btnShowStats, "btnShowStats"); + this.btnShowStats.Name = "btnShowStats"; + this.btnShowStats.UseVisualStyleBackColor = true; + this.btnShowStats.Click += new System.EventHandler(this.BtnShowStats_Click); + // // pbThumbnail // resources.ApplyResources(this.pbThumbnail, "pbThumbnail"); @@ -130,6 +149,7 @@ private void InitializeComponent() this.pbThumbnail.DrawCheckeredBackground = true; this.pbThumbnail.FullscreenOnClick = true; this.pbThumbnail.Name = "pbThumbnail"; + this.pbThumbnail.PictureBoxBackColor = System.Drawing.SystemColors.Control; this.pbThumbnail.ShowImageSizeLabel = true; // // gbFilters @@ -286,5 +306,7 @@ private void InitializeComponent() private System.Windows.Forms.Label lblFilenameFilter; private System.Windows.Forms.Label lblURLFilter; private System.Windows.Forms.TextBox txtURLFilter; + private System.Windows.Forms.Button btnShowStats; + private System.Windows.Forms.RichTextBox rtbStats; } } \ No newline at end of file diff --git a/ShareX.HistoryLib/Forms/HistoryForm.cs b/ShareX.HistoryLib/Forms/HistoryForm.cs index 615fc1926..20a36c043 100644 --- a/ShareX.HistoryLib/Forms/HistoryForm.cs +++ b/ShareX.HistoryLib/Forms/HistoryForm.cs @@ -44,6 +44,7 @@ public partial class HistoryForm : Form private HistoryItemManager him; private HistoryItem[] allHistoryItems; private string defaultTitle; + private bool showingStats; public HistoryForm(string historyPath, HistorySettings settings, Action uploadFile = null, Action editImage = null) { @@ -53,7 +54,7 @@ public HistoryForm(string historyPath, HistorySettings settings, Action InitializeComponent(); Icon = ShareXResources.Icon; defaultTitle = Text; - UpdateTitle(); + //UpdateTitle(); // Mark the Date column as having a date; used for sorting chDateTime.Tag = new DateTime(); @@ -86,6 +87,63 @@ private void RefreshHistoryItems() ApplyFiltersAndAdd(); } + private void OutputStats(HistoryItem[] historyItems) + { + rtbStats.ResetText(); + + rtbStats.SetFontBold(); + rtbStats.AppendLine("History item counts:"); + rtbStats.SetFontRegular(); + rtbStats.AppendLine("Total: " + historyItems.Length); + + IEnumerable types = historyItems. + Select(x => x.Type). + GroupBy(x => x). + OrderByDescending(x => x.Count()). + Select(x => string.Format("{0}: {1}", x.Key, x.Count())); + + rtbStats.AppendLine(string.Join(Environment.NewLine, types)); + + rtbStats.AppendLine(); + rtbStats.SetFontBold(); + rtbStats.AppendLine("Yearly usages:"); + rtbStats.SetFontRegular(); + + IEnumerable yearlyUsages = historyItems. + GroupBy(x => x.DateTime.Year). + OrderByDescending(x => x.Key). + Select(x => string.Format("{0}: {1} ({2:N0}%)", x.Key, x.Count(), x.Count() / (float)historyItems.Length * 100)); + + rtbStats.AppendLine(string.Join(Environment.NewLine, yearlyUsages)); + + rtbStats.AppendLine(); + rtbStats.SetFontBold(); + rtbStats.AppendLine("File extensions:"); + rtbStats.SetFontRegular(); + + IEnumerable fileExtensions = historyItems. + Where(x => !string.IsNullOrEmpty(x.Filename) && !x.Filename.EndsWith(")")). + Select(x => Helpers.GetFilenameExtension(x.Filename)). + GroupBy(x => x). + OrderByDescending(x => x.Count()). + Select(x => string.Format("{0} ({1})", x.Key, x.Count())); + + rtbStats.AppendLine(string.Join(Environment.NewLine, fileExtensions)); + + rtbStats.AppendLine(); + rtbStats.SetFontBold(); + rtbStats.AppendLine("Hosts:"); + rtbStats.SetFontRegular(); + + IEnumerable hosts = historyItems. + Select(x => x.Host). + GroupBy(x => x). + OrderByDescending(x => x.Count()). + Select(x => string.Format("{0} ({1})", x.Key, x.Count())); + + rtbStats.AppendLine(string.Join(Environment.NewLine, hosts)); + } + private HistoryItem[] him_GetHistoryItems() { return lvHistory.SelectedItems.Cast().Select(x => x.Tag as HistoryItem).ToArray(); @@ -177,7 +235,7 @@ private void AddHistoryItems(HistoryItem[] historyItems) { Cursor = Cursors.WaitCursor; - UpdateTitle(historyItems); + //UpdateTitle(historyItems); lvHistory.Items.Clear(); @@ -340,6 +398,29 @@ private void btnRemoveFilters_Click(object sender, EventArgs e) AddHistoryItems(allHistoryItems); } + private void BtnShowStats_Click(object sender, EventArgs e) + { + if (showingStats) + { + lvHistory.Visible = true; + rtbStats.Visible = false; + // TODO: Translate + btnShowStats.Text = "Show stats"; + showingStats = false; + } + else + { + rtbStats.Visible = true; + lvHistory.Visible = false; + // TODO: Translate + btnShowStats.Text = "Hide stats"; + Cursor = Cursors.WaitCursor; + OutputStats(allHistoryItems); + Cursor = Cursors.Default; + showingStats = true; + } + } + private void lvHistory_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e) { if (e.IsSelected) diff --git a/ShareX.HistoryLib/Forms/HistoryForm.resx b/ShareX.HistoryLib/Forms/HistoryForm.resx index 7219222fa..7d6ef63ba 100644 --- a/ShareX.HistoryLib/Forms/HistoryForm.resx +++ b/ShareX.HistoryLib/Forms/HistoryForm.resx @@ -125,10 +125,43 @@ 0, 0 + + Fill + + + Arial, 9.75pt + + + 0, 0 + + + 550, 641 + + + + 1 + + + + + + False + + + rtbStats + + + System.Windows.Forms.RichTextBox, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + scMain.Panel1 + + + 0 + - 24 @@ -166,13 +199,13 @@ lvHistory - ShareX.HelpersLib.MyListView, ShareX.HelpersLib, Version=12.3.0.0, Culture=neutral, PublicKeyToken=null + ShareX.HelpersLib.MyListView, ShareX.HelpersLib, Version=13.0.0.0, Culture=neutral, PublicKeyToken=null scMain.Panel1 - 0 + 1 scMain.Panel1 @@ -189,6 +222,33 @@ 100 + + Bottom, Left + + + 8, 608 + + + 144, 24 + + + 5 + + + Show stats + + + btnShowStats + + + System.Windows.Forms.Button, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + panel1 + + + 0 + Top, Bottom, Left, Right @@ -196,7 +256,7 @@ 8, 8 - 408, 384 + 408, 352 3 @@ -205,13 +265,13 @@ pbThumbnail - ShareX.HelpersLib.MyPictureBox, ShareX.HelpersLib, Version=12.3.0.0, Culture=neutral, PublicKeyToken=null + ShareX.HelpersLib.MyPictureBox, ShareX.HelpersLib, Version=13.0.0.0, Culture=neutral, PublicKeyToken=null panel1 - 0 + 1 Bottom, Left, Right @@ -601,7 +661,7 @@ 14 - 8, 400 + 8, 368 408, 234 @@ -622,7 +682,7 @@ panel1 - 1 + 2 Fill @@ -679,7 +739,7 @@ scMain - ShareX.HelpersLib.SplitContainerCustomSplitter, ShareX.HelpersLib, Version=12.3.0.0, Culture=neutral, PublicKeyToken=null + ShareX.HelpersLib.SplitContainerCustomSplitter, ShareX.HelpersLib, Version=13.0.0.0, Culture=neutral, PublicKeyToken=null $this diff --git a/ShareX.UploadersLib/Forms/CustomUploaderSettingsForm.cs b/ShareX.UploadersLib/Forms/CustomUploaderSettingsForm.cs index f2039cbb3..d84a417d7 100644 --- a/ShareX.UploadersLib/Forms/CustomUploaderSettingsForm.cs +++ b/ShareX.UploadersLib/Forms/CustomUploaderSettingsForm.cs @@ -768,32 +768,32 @@ private void UpdateResponseInfoTextBox(ResponseInfo responseInfo, bool includeRe { rtbResponseInfo.ResetText(); - rtbResponseInfo.SelectionFont = new Font(rtbResponseInfo.Font, FontStyle.Bold); + rtbResponseInfo.SetFontBold(); rtbResponseInfo.AppendText("Status code:\r\n"); - rtbResponseInfo.SelectionFont = new Font(rtbResponseInfo.Font, FontStyle.Regular); + rtbResponseInfo.SetFontRegular(); rtbResponseInfo.AppendText($"({(int)responseInfo.StatusCode}) {responseInfo.StatusDescription}"); if (!string.IsNullOrEmpty(responseInfo.ResponseURL)) { - rtbResponseInfo.SelectionFont = new Font(rtbResponseInfo.Font, FontStyle.Bold); + rtbResponseInfo.SetFontBold(); rtbResponseInfo.AppendText("\r\n\r\nResponse URL:\r\n"); - rtbResponseInfo.SelectionFont = new Font(rtbResponseInfo.Font, FontStyle.Regular); + rtbResponseInfo.SetFontRegular(); rtbResponseInfo.AppendText(responseInfo.ResponseURL); } if (responseInfo.Headers != null && responseInfo.Headers.Count > 0) { - rtbResponseInfo.SelectionFont = new Font(rtbResponseInfo.Font, FontStyle.Bold); + rtbResponseInfo.SetFontBold(); rtbResponseInfo.AppendText("\r\n\r\nHeaders:\r\n"); - rtbResponseInfo.SelectionFont = new Font(rtbResponseInfo.Font, FontStyle.Regular); + rtbResponseInfo.SetFontRegular(); rtbResponseInfo.AppendText(responseInfo.Headers.ToString().TrimEnd('\r', '\n')); } if (includeResponseText && !string.IsNullOrEmpty(responseInfo.ResponseText)) { - rtbResponseInfo.SelectionFont = new Font(rtbResponseInfo.Font, FontStyle.Bold); + rtbResponseInfo.SetFontBold(); rtbResponseInfo.AppendText("\r\n\r\nResponse text:\r\n"); - rtbResponseInfo.SelectionFont = new Font(rtbResponseInfo.Font, FontStyle.Regular); + rtbResponseInfo.SetFontRegular(); rtbResponseInfo.AppendText(responseInfo.ResponseText); } }