ShareX/ShareX.HistoryLib/Forms/HistoryForm.cs

480 lines
16 KiB
C#
Raw Normal View History

2013-11-03 23:53:49 +13:00
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
2019-01-02 20:43:52 +13:00
Copyright (c) 2007-2019 ShareX Team
2013-11-03 23:53:49 +13:00
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
2014-12-11 09:25:20 +13:00
using ShareX.HelpersLib;
using ShareX.HistoryLib.Properties;
2013-11-03 23:53:49 +13:00
using System;
using System.Collections.Generic;
using System.IO;
2013-11-03 23:53:49 +13:00
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
2013-11-03 23:53:49 +13:00
using System.Windows.Forms;
2014-12-11 09:25:20 +13:00
namespace ShareX.HistoryLib
2013-11-03 23:53:49 +13:00
{
public partial class HistoryForm : Form
2013-11-03 23:53:49 +13:00
{
public string HistoryPath { get; private set; }
2018-08-02 01:52:10 +12:00
public HistorySettings Settings { get; private set; }
2013-11-03 23:53:49 +13:00
private HistoryManager history;
private HistoryItemManager him;
private HistoryItem[] allHistoryItems;
private string defaultTitle;
private bool showingStats;
2013-11-03 23:53:49 +13:00
2018-08-02 01:52:10 +12:00
public HistoryForm(string historyPath, HistorySettings settings, Action<string> uploadFile = null, Action<string> editImage = null)
2013-11-03 23:53:49 +13:00
{
HistoryPath = historyPath;
2018-08-02 01:52:10 +12:00
Settings = settings;
2013-11-03 23:53:49 +13:00
InitializeComponent();
2016-03-20 04:44:24 +13:00
defaultTitle = Text;
2019-06-17 18:02:16 +12:00
UpdateTitle();
2013-11-03 23:53:49 +13:00
// Mark the Date column as having a date; used for sorting
chDateTime.Tag = new DateTime();
2016-03-01 15:03:39 +13:00
ImageList il = new ImageList();
il.ColorDepth = ColorDepth.Depth32Bit;
il.Images.Add(Resources.image);
il.Images.Add(Resources.notebook);
il.Images.Add(Resources.application_block);
il.Images.Add(Resources.globe);
lvHistory.SmallImageList = il;
him = new HistoryItemManager(uploadFile, editImage);
2013-11-03 23:53:49 +13:00
him.GetHistoryItems += him_GetHistoryItems;
2019-06-25 07:33:17 +12:00
lvHistory.ContextMenuStrip = him.cmsHistory;
2013-11-03 23:53:49 +13:00
pbThumbnail.Reset();
lvHistory.FillLastColumn();
2018-08-02 01:52:10 +12:00
if (Settings.SplitterDistance > 0)
{
2018-08-02 01:52:10 +12:00
scMain.SplitterDistance = Settings.SplitterDistance;
}
2018-08-02 01:52:10 +12:00
2019-06-25 07:33:17 +12:00
ShareXResources.ApplyTheme(this);
2018-08-02 01:52:10 +12:00
Settings.WindowState.AutoHandleFormState(this);
2013-11-03 23:53:49 +13:00
}
private void RefreshHistoryItems()
{
allHistoryItems = GetHistoryItems();
ApplyFiltersAndAdd();
}
private void OutputStats(HistoryItem[] historyItems)
{
rtbStats.ResetText();
rtbStats.SetFontBold();
2019-08-24 15:50:52 +12:00
rtbStats.AppendLine(Resources.HistoryItemCounts);
rtbStats.SetFontRegular();
2019-08-24 15:50:52 +12:00
rtbStats.AppendLine(Resources.HistoryStats_Total + " " + historyItems.Length);
IEnumerable<string> types = historyItems.
2019-06-17 18:02:16 +12:00
GroupBy(x => x.Type).
OrderByDescending(x => x.Count()).
2019-06-17 18:02:16 +12:00
Select(x => string.Format("{0}: {1} ({2:N0}%)", x.Key, x.Count(), x.Count() / (float)historyItems.Length * 100));
rtbStats.AppendLine(string.Join(Environment.NewLine, types));
rtbStats.AppendLine();
rtbStats.SetFontBold();
2019-08-24 15:50:52 +12:00
rtbStats.AppendLine(Resources.HistoryStats_YearlyUsages);
rtbStats.SetFontRegular();
IEnumerable<string> 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();
2019-08-24 15:50:52 +12:00
rtbStats.AppendLine(Resources.HistoryStats_FileExtensions);
rtbStats.SetFontRegular();
IEnumerable<string> 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();
2019-08-24 15:50:52 +12:00
rtbStats.AppendLine(Resources.HistoryStats_Hosts);
rtbStats.SetFontRegular();
IEnumerable<string> hosts = historyItems.
2019-06-17 18:02:16 +12:00
GroupBy(x => x.Host).
OrderByDescending(x => x.Count()).
Select(x => string.Format("{0} ({1})", x.Key, x.Count()));
rtbStats.AppendLine(string.Join(Environment.NewLine, hosts));
}
2013-11-03 23:53:49 +13:00
private HistoryItem[] him_GetHistoryItems()
{
return lvHistory.SelectedItems.Cast<ListViewItem>().Select(x => x.Tag as HistoryItem).ToArray();
}
private HistoryItem[] GetHistoryItems()
{
2018-08-02 01:12:18 +12:00
if (history == null)
{
history = new HistoryManager(HistoryPath);
}
2013-11-03 23:53:49 +13:00
IEnumerable<HistoryItem> tempHistoryItems = history.GetHistoryItems();
tempHistoryItems = tempHistoryItems.Reverse();
2013-11-03 23:53:49 +13:00
2018-08-02 01:52:10 +12:00
if (Settings.MaxItemCount > 0)
2013-11-03 23:53:49 +13:00
{
2018-08-02 01:52:10 +12:00
tempHistoryItems = tempHistoryItems.Take(Settings.MaxItemCount);
2013-11-03 23:53:49 +13:00
}
return tempHistoryItems.ToArray();
2013-11-03 23:53:49 +13:00
}
private void ApplyFiltersAndAdd()
{
if (allHistoryItems.Length > 0)
{
AddHistoryItems(ApplyFilters(allHistoryItems));
}
}
private HistoryItem[] ApplyFilters(HistoryItem[] historyItems)
{
if (!cbTypeFilter.Checked && !cbHostFilter.Checked && string.IsNullOrEmpty(txtFilenameFilter.Text) && string.IsNullOrEmpty(txtURLFilter.Text) && !cbDateFilter.Checked)
2016-03-02 01:05:26 +13:00
{
return historyItems;
}
2013-11-03 23:53:49 +13:00
IEnumerable<HistoryItem> result = historyItems.AsEnumerable();
if (cbTypeFilter.Checked)
{
string type = cbTypeFilterSelection.Text;
2013-11-03 23:53:49 +13:00
if (!string.IsNullOrEmpty(type))
2015-06-01 17:19:01 +12:00
{
result = result.Where(x => !string.IsNullOrEmpty(x.Type) && x.Type.Equals(type, StringComparison.InvariantCultureIgnoreCase));
2015-06-01 17:19:01 +12:00
}
2013-11-03 23:53:49 +13:00
}
if (cbHostFilter.Checked)
{
string host = cbHostFilterSelection.Text;
2013-11-03 23:53:49 +13:00
2015-06-01 17:19:01 +12:00
if (!string.IsNullOrEmpty(host))
{
2015-08-21 03:44:40 +12:00
result = result.Where(x => !string.IsNullOrEmpty(x.Host) && x.Host.Contains(host, StringComparison.InvariantCultureIgnoreCase));
2015-06-01 17:19:01 +12:00
}
2013-11-03 23:53:49 +13:00
}
string filenameFilter = txtFilenameFilter.Text;
if (!string.IsNullOrEmpty(filenameFilter))
2013-11-03 23:53:49 +13:00
{
string pattern = Regex.Escape(filenameFilter).Replace("\\?", ".").Replace("\\*", ".*");
Regex regex = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
result = result.Where(x => x.Filename != null && regex.IsMatch(x.Filename));
2013-11-03 23:53:49 +13:00
}
string urlFilter = txtURLFilter.Text;
if (!string.IsNullOrEmpty(urlFilter))
{
result = result.Where(x => x.URL != null && x.URL.Contains(urlFilter, StringComparison.InvariantCultureIgnoreCase));
}
2013-11-03 23:53:49 +13:00
if (cbDateFilter.Checked)
{
DateTime fromDate = dtpFilterFrom.Value.Date;
DateTime toDate = dtpFilterTo.Value.Date;
2016-03-01 23:29:47 +13:00
result = result.Where(x => x.DateTime.Date >= fromDate && x.DateTime.Date <= toDate);
2013-11-03 23:53:49 +13:00
}
return result.ToArray();
}
private void AddHistoryItems(HistoryItem[] historyItems)
{
Cursor = Cursors.WaitCursor;
2019-06-17 18:02:16 +12:00
UpdateTitle(historyItems);
2013-11-03 23:53:49 +13:00
lvHistory.Items.Clear();
ListViewItem[] listViewItems = new ListViewItem[historyItems.Length];
for (int i = 0; i < historyItems.Length; i++)
{
HistoryItem hi = historyItems[i];
2016-03-01 15:03:39 +13:00
ListViewItem lvi = listViewItems[i] = new ListViewItem();
if (hi.Type.Equals("Image", StringComparison.InvariantCultureIgnoreCase))
{
lvi.ImageIndex = 0;
}
else if (hi.Type.Equals("Text", StringComparison.InvariantCultureIgnoreCase))
{
lvi.ImageIndex = 1;
}
else if (hi.Type.Equals("File", StringComparison.InvariantCultureIgnoreCase))
{
lvi.ImageIndex = 2;
}
else
{
lvi.ImageIndex = 3;
}
lvi.SubItems.Add(hi.DateTime.ToString()).Tag = hi.DateTime;
2013-11-03 23:53:49 +13:00
lvi.SubItems.Add(hi.Filename);
lvi.SubItems.Add(hi.URL);
lvi.Tag = hi;
}
lvHistory.Items.AddRange(listViewItems);
lvHistory.FillLastColumn();
lvHistory.Focus();
Cursor = Cursors.Default;
2013-11-03 23:53:49 +13:00
}
private void UpdateTitle(HistoryItem[] historyItems = null)
2013-11-03 23:53:49 +13:00
{
string title = defaultTitle;
2013-11-03 23:53:49 +13:00
if (historyItems != null)
2013-11-03 23:53:49 +13:00
{
StringBuilder status = new StringBuilder();
2013-11-03 23:53:49 +13:00
status.Append(" (");
status.AppendFormat(Resources.HistoryForm_UpdateItemCount_Total___0_, allHistoryItems.Length.ToString("N0"));
2013-11-03 23:53:49 +13:00
if (allHistoryItems.Length > historyItems.Length)
{
status.AppendFormat(" - " + Resources.HistoryForm_UpdateItemCount___Filtered___0_, historyItems.Length.ToString("N0"));
}
2019-06-17 18:02:16 +12:00
IEnumerable<string> types = historyItems.
GroupBy(x => x.Type).
OrderByDescending(x => x.Count()).
Select(x => string.Format(" - {0}: {1}", x.Key, x.Count()));
foreach (string type in types)
{
status.Append(type);
}
status.Append(")");
title += status.ToString();
2013-11-03 23:53:49 +13:00
}
Text = title;
2013-11-03 23:53:49 +13:00
}
private void UpdateControls()
{
switch (him.RefreshInfo())
{
case HistoryRefreshInfoResult.Success:
UpdatePictureBox();
break;
case HistoryRefreshInfoResult.Invalid:
pbThumbnail.Reset();
break;
}
}
private void UpdatePictureBox()
{
pbThumbnail.Reset();
if (him != null)
{
if (him.IsImageFile)
{
pbThumbnail.LoadImageFromFileAsync(him.HistoryItem.Filepath);
}
else if (him.IsImageURL)
{
pbThumbnail.LoadImageFromURLAsync(him.HistoryItem.URL);
}
}
}
#region Form events
private void HistoryForm_Shown(object sender, EventArgs e)
{
Refresh();
2013-11-03 23:53:49 +13:00
RefreshHistoryItems();
if (lvHistory.Items.Count > 0)
{
lvHistory.Items[0].Selected = true;
cbTypeFilterSelection.Items.Clear();
cbTypeFilterSelection.Items.AddRange(allHistoryItems.Select(x => x.Type).Distinct().Where(x => !string.IsNullOrEmpty(x)).ToArray());
if (cbTypeFilterSelection.Items.Count > 0)
{
cbTypeFilterSelection.SelectedIndex = 0;
}
cbHostFilterSelection.Items.Clear();
cbHostFilterSelection.Items.AddRange(allHistoryItems.Select(x => x.Host).Distinct().Where(x => !string.IsNullOrEmpty(x)).ToArray());
}
this.ForceActivate();
2013-11-03 23:53:49 +13:00
}
private void HistoryForm_Resize(object sender, EventArgs e)
{
Refresh();
}
2013-11-03 23:53:49 +13:00
private void HistoryForm_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyData)
{
case Keys.F5:
RefreshHistoryItems();
e.Handled = true;
break;
}
}
private void scMain_SplitterMoved(object sender, SplitterEventArgs e)
{
2018-08-02 01:52:10 +12:00
Settings.SplitterDistance = scMain.SplitterDistance;
}
2013-11-03 23:53:49 +13:00
private void btnApplyFilters_Click(object sender, EventArgs e)
{
ApplyFiltersAndAdd();
}
private void btnRemoveFilters_Click(object sender, EventArgs e)
{
AddHistoryItems(allHistoryItems);
}
private void BtnShowStats_Click(object sender, EventArgs e)
{
if (showingStats)
{
lvHistory.Visible = true;
2019-06-17 18:02:16 +12:00
pStats.Visible = false;
2019-08-24 15:50:52 +12:00
btnShowStats.Text = Resources.BtnShowStats_ShowStats;
showingStats = false;
}
else
{
2019-06-17 18:02:16 +12:00
pStats.Visible = true;
lvHistory.Visible = false;
2019-08-24 15:50:52 +12:00
btnShowStats.Text = Resources.BtnShowStats_HideStats;
Cursor = Cursors.WaitCursor;
OutputStats(allHistoryItems);
Cursor = Cursors.Default;
showingStats = true;
}
}
2013-11-03 23:53:49 +13:00
private void lvHistory_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
if (e.IsSelected)
{
UpdateControls();
}
}
private void lvHistory_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (him != null && e.Button == MouseButtons.Left)
{
him.TryOpen();
}
}
private void lvHistory_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyData)
{
default:
return;
case Keys.Enter:
him.TryOpen();
break;
case Keys.Control | Keys.Enter:
him.OpenFile();
break;
case Keys.Control | Keys.C:
him.CopyURL();
break;
}
e.Handled = true;
}
private void lvHistory_ItemDrag(object sender, ItemDragEventArgs e)
{
List<string> selection = new List<string>();
foreach (ListViewItem item in lvHistory.SelectedItems)
{
HistoryItem hi = (HistoryItem)item.Tag;
if (File.Exists(hi.Filepath))
{
selection.Add(hi.Filepath);
}
}
if (selection.Count > 0)
{
DataObject data = new DataObject(DataFormats.FileDrop, selection.ToArray());
DoDragDrop(data, DragDropEffects.Copy);
}
}
2013-11-03 23:53:49 +13:00
#endregion Form events
}
}