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
{
2014-05-03 01:33:12 +00:00
private List < string > recentlist ;
2013-10-25 00:59:34 +00:00
public RecentFiles ( ) : this ( 8 ) { }
public RecentFiles ( int max )
{
recentlist = new List < string > ( ) ;
MAX_RECENT_FILES = max ;
}
2014-05-03 01:33:12 +00:00
public int MAX_RECENT_FILES { get ; private set ; }
public bool AutoLoad { get ; set ; }
2013-10-25 00:59:34 +00:00
2014-05-03 01:33:12 +00:00
[JsonIgnore]
2013-10-25 00:59:34 +00:00
public bool Empty
{
2014-03-26 00:34:43 +00:00
get { return ! recentlist . Any ( ) ; }
2013-10-25 00:59:34 +00:00
}
2014-05-03 01:33:12 +00:00
[JsonIgnore]
2013-10-25 00:59:34 +00:00
public int Count
{
get { return recentlist . Count ; }
}
2014-05-03 01:33:12 +00:00
[JsonIgnore]
public string MostRecent
2013-10-25 00:59:34 +00:00
{
2014-05-03 01:33:12 +00:00
get
2013-10-25 00:59:34 +00:00
{
2014-05-03 01:33:12 +00:00
return recentlist . Any ( ) ? recentlist [ 0 ] : string . Empty ;
}
}
public string this [ int index ]
{
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
return string . Empty ;
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 ( )
{
recentlist . Clear ( ) ;
}
2013-12-30 01:17:11 +00:00
2014-05-03 01:33:12 +00:00
public void Add ( string newFile )
{
Remove ( newFile ) ;
2013-10-25 00:59:34 +00:00
recentlist . Insert ( 0 , newFile ) ;
2014-05-03 01:33:12 +00:00
2013-10-25 00:59:34 +00:00
if ( recentlist . Count > MAX_RECENT_FILES )
{
2014-05-03 01:33:12 +00:00
recentlist . Remove ( recentlist . Last ( ) ) ;
2013-10-25 00:59:34 +00:00
}
}
public bool Remove ( string newFile )
{
2013-12-30 01:17:11 +00:00
var removed = false ;
2014-05-03 01:33:12 +00:00
foreach ( var recent in recentlist . ToList ( ) )
2013-10-25 00:59:34 +00:00
{
2014-05-03 01:33:12 +00:00
if ( string . Compare ( newFile , recent , StringComparison . CurrentCultureIgnoreCase ) = = 0 )
2013-10-25 00:59:34 +00:00
{
2013-12-30 01:17:11 +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-12-30 01:17:11 +00:00
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 void ToggleAutoLoad ( )
{
AutoLoad ^ = true ;
}
}
}