BizHawk/BizHawk.Client.Common/RecentFiles.cs

114 lines
2.4 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;
namespace BizHawk.Client.Common
{
[Newtonsoft.Json.JsonObject]
2013-10-25 00:59:34 +00:00
public class RecentFiles : IEnumerable
{
public int MAX_RECENT_FILES { get; private set; } // Maximum number of files
public List<string> recentlist { get; private set; } // List of recent files
2013-10-25 00:59:34 +00:00
public bool AutoLoad = false;
public RecentFiles() : this(8) { }
public RecentFiles(int max)
{
recentlist = new List<string>();
MAX_RECENT_FILES = max;
}
public IEnumerator<string> GetEnumerator()
{
return recentlist.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public void Clear()
{
recentlist.Clear();
}
public bool Empty
{
get { return !recentlist.Any(); }
2013-10-25 00:59:34 +00:00
}
public int Count
{
get { return recentlist.Count; }
}
public void Add(string newFile)
{
for (int i = 0; i < recentlist.Count; i++)
2013-10-25 00:59:34 +00:00
{
if (String.Compare(newFile, recentlist[i], StringComparison.CurrentCultureIgnoreCase) == 0)
2013-10-25 00:59:34 +00:00
{
recentlist.Remove(newFile); // intentionally keeps iterating after this to remove duplicate instances, though those should never exist in the first place
2013-10-25 00:59:34 +00:00
}
}
2013-10-25 00:59:34 +00:00
recentlist.Insert(0, newFile);
if (recentlist.Count > MAX_RECENT_FILES)
{
recentlist.Remove(recentlist[recentlist.Count - 1]);
}
}
public bool Remove(string newFile)
{
var removed = false;
for (int i = 0; i < recentlist.Count; i++)
2013-10-25 00:59:34 +00:00
{
if (String.Compare(newFile, recentlist[i], StringComparison.CurrentCultureIgnoreCase) == 0)
2013-10-25 00:59:34 +00:00
{
recentlist.Remove(newFile); // intentionally keeps iterating after this to remove duplicate instances, though those should never exist in the first place
2013-10-25 00:59:34 +00:00
removed = true;
}
}
2013-10-25 00:59:34 +00:00
return removed;
}
public List<string> GetRecentListTruncated(int length)
{
return recentlist.Select(t => t.Substring(0, length)).ToList();
}
public string this[int index]
{
get
{
if (recentlist.Any())
{
return recentlist[index];
}
else
{
return String.Empty;
2013-10-25 00:59:34 +00:00
}
}
}
public void ToggleAutoLoad()
{
AutoLoad ^= true;
}
public string MostRecent
{
get
{
return recentlist.Any() ? recentlist[0] : string.Empty;
}
}
2013-10-25 00:59:34 +00:00
}
}