2013-12-30 01:17:11 +00:00
using System ;
using System.Collections ;
2013-10-25 00:59:34 +00:00
using System.Collections.Generic ;
using System.Linq ;
2014-05-03 01:33:12 +00:00
using Newtonsoft.Json ;
2013-10-25 00:59:34 +00:00
namespace BizHawk.Client.Common
{
2014-05-03 01:33:12 +00:00
[JsonObject]
2013-10-25 00:59:34 +00:00
public class RecentFiles : IEnumerable
{
2017-05-17 18:18:26 +00:00
// ReSharper disable once FieldCanBeMadeReadOnly.Local
2017-04-15 21:06:40 +00:00
private List < string > recentlist ;
2017-04-14 19:59:01 +00:00
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 ; }
2014-05-03 01:33:12 +00:00
public bool AutoLoad { get ; set ; }
2013-10-25 00:59:34 +00:00
2014-08-02 22:49:27 +00:00
/// <summary>
/// If true, the list can not change, or be cleared
/// </summary>
public bool Frozen { get ; set ; }
2014-05-03 01:33:12 +00:00
[JsonIgnore]
2017-04-14 19:59:01 +00:00
public bool Empty = > ! recentlist . Any ( ) ;
2013-10-25 00:59:34 +00:00
2017-04-15 20:37:30 +00:00
[JsonIgnore]
2017-04-14 19:59:01 +00:00
public int Count = > recentlist . Count ;
2013-10-25 00:59:34 +00:00
2017-04-15 20:37:30 +00:00
[JsonIgnore]
2017-05-10 11:45:23 +00:00
public string MostRecent = > recentlist . Any ( ) ? recentlist [ 0 ] : "" ;
2014-05-03 01:33:12 +00:00
2017-04-15 20:37:30 +00:00
public string this [ int index ]
2014-05-03 01:33:12 +00:00
{
get
{
if ( recentlist . Any ( ) )
2013-10-25 00:59:34 +00:00
{
2014-05-03 01:33:12 +00:00
return recentlist [ index ] ;
2013-10-25 00:59:34 +00:00
}
2014-05-03 01:33:12 +00:00
2017-05-10 11:45:23 +00:00
return "" ;
2013-10-25 00:59:34 +00:00
}
2014-05-03 01:33:12 +00:00
}
public IEnumerator < string > GetEnumerator ( )
{
return recentlist . GetEnumerator ( ) ;
}
IEnumerator IEnumerable . GetEnumerator ( )
{
return GetEnumerator ( ) ;
}
public void Clear ( )
{
2014-08-02 22:49:27 +00:00
if ( ! Frozen )
{
recentlist . Clear ( ) ;
}
2014-05-03 01:33:12 +00:00
}
2013-12-30 01:17:11 +00:00
2014-05-03 01:33:12 +00:00
public void Add ( string newFile )
{
2014-08-02 22:49:27 +00:00
if ( ! Frozen )
2013-10-25 00:59:34 +00:00
{
2014-08-02 22:49:27 +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 )
{
2014-08-02 22:49:27 +00:00
if ( ! Frozen )
2013-10-25 00:59:34 +00:00
{
2014-08-02 22:49:27 +00:00
var removed = false ;
foreach ( var recent in recentlist . ToList ( ) )
2013-10-25 00:59:34 +00:00
{
2014-08-02 22:49:27 +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
}
2014-08-02 22:49:27 +00:00
return removed ;
2013-10-25 00:59:34 +00:00
}
2013-12-30 01:17:11 +00:00
2014-08-02 22:49:27 +00:00
return false ;
2013-10-25 00:59:34 +00:00
}
public void ToggleAutoLoad ( )
{
AutoLoad ^ = true ;
}
}
}