BizHawk/BizHawk.Client.Common/RecentFiles.cs

113 lines
2.0 KiB
C#
Raw Normal View History

using System;
using System.Collections;
2013-10-25 00:59:34 +00:00
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
2013-10-25 00:59:34 +00:00
namespace BizHawk.Client.Common
{
[JsonObject]
2013-10-25 00:59:34 +00:00
public class RecentFiles : IEnumerable
{
2017-04-15 21:06:40 +00:00
private List<string> recentlist;
public RecentFiles()
: this(8)
{
}
2013-10-25 00:59:34 +00:00
public RecentFiles(int max)
{
recentlist = new List<string>();
MAX_RECENT_FILES = max;
}
2014-05-07 00:41:13 +00:00
public int MAX_RECENT_FILES { get; set; }
public bool AutoLoad { get; set; }
2013-10-25 00:59:34 +00:00
/// <summary>
/// If true, the list can not change, or be cleared
/// </summary>
public bool Frozen { get; set; }
[JsonIgnore]
public bool Empty => !recentlist.Any();
2013-10-25 00:59:34 +00:00
2017-04-15 20:37:30 +00:00
[JsonIgnore]
public int Count => recentlist.Count;
2013-10-25 00:59:34 +00:00
2017-04-15 20:37:30 +00:00
[JsonIgnore]
public string MostRecent => recentlist.Any() ? recentlist[0] : string.Empty;
2017-04-15 20:37:30 +00:00
public string this[int index]
{
get
{
if (recentlist.Any())
2013-10-25 00:59:34 +00:00
{
return recentlist[index];
2013-10-25 00:59:34 +00:00
}
return string.Empty;
2013-10-25 00:59:34 +00:00
}
}
public IEnumerator<string> GetEnumerator()
{
return recentlist.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public void Clear()
{
if (!Frozen)
{
recentlist.Clear();
}
}
public void Add(string newFile)
{
if (!Frozen)
2013-10-25 00:59:34 +00:00
{
Remove(newFile);
recentlist.Insert(0, newFile);
if (recentlist.Count > MAX_RECENT_FILES)
{
recentlist.Remove(recentlist.Last());
}
2013-10-25 00:59:34 +00:00
}
}
public bool Remove(string newFile)
{
if (!Frozen)
2013-10-25 00:59:34 +00:00
{
var removed = false;
foreach (var recent in recentlist.ToList())
2013-10-25 00:59:34 +00:00
{
if (string.Compare(newFile, recent, StringComparison.CurrentCultureIgnoreCase) == 0)
{
recentlist.Remove(newFile); // intentionally keeps iterating after this to remove duplicate instances, though those should never exist in the first place
removed = true;
}
2013-10-25 00:59:34 +00:00
}
return removed;
2013-10-25 00:59:34 +00:00
}
return false;
2013-10-25 00:59:34 +00:00
}
public void ToggleAutoLoad()
{
AutoLoad ^= true;
}
}
}