2013-08-01 00:37:18 +00:00
using System.Collections.Generic ;
2013-04-15 02:14:14 +00:00
using System.Linq ;
2011-01-17 01:58:19 +00:00
namespace BizHawk.MultiClient
{
2011-06-19 23:39:25 +00:00
public class RecentFiles
{
2013-04-15 02:14:14 +00:00
private readonly int MAX_RECENT_FILES ; //Maximum number of files
2013-08-01 00:37:18 +00:00
private readonly List < string > recentlist ; //List of recent files
2011-01-17 01:58:19 +00:00
2013-08-01 00:37:18 +00:00
public RecentFiles ( ) : this ( 8 ) { }
2011-06-19 23:39:25 +00:00
public RecentFiles ( int max )
{
2013-08-01 00:37:18 +00:00
recentlist = new List < string > ( ) ;
2011-06-19 23:39:25 +00:00
MAX_RECENT_FILES = max ;
}
2011-01-17 01:58:19 +00:00
2011-06-19 23:39:25 +00:00
public void Clear ( )
{
2013-08-01 00:37:18 +00:00
recentlist . Clear ( ) ;
2011-06-19 23:39:25 +00:00
}
2011-01-17 01:58:19 +00:00
2013-07-16 01:59:59 +00:00
public bool Empty
2011-06-19 23:39:25 +00:00
{
2013-08-01 00:37:18 +00:00
get { return recentlist . Count = = 0 ; }
2011-06-19 23:39:25 +00:00
}
2011-01-17 02:34:52 +00:00
2013-03-10 22:42:54 +00:00
public int Count
2011-06-19 23:39:25 +00:00
{
2013-08-01 00:37:18 +00:00
get { return recentlist . Count ; }
2011-06-19 23:39:25 +00:00
}
2011-01-17 01:58:19 +00:00
2011-06-19 23:39:25 +00:00
public void Add ( string newFile )
{
2013-08-01 00:37:18 +00:00
for ( int x = 0 ; x < recentlist . Count ; x + + )
{
if ( string . Compare ( newFile , recentlist [ x ] ) = = 0 )
{
recentlist . Remove ( newFile ) ; //intentionally keeps iterating after this to remove duplicate instances, though those should never exist in the first place
}
}
recentlist . Insert ( 0 , newFile ) ;
if ( recentlist . Count > MAX_RECENT_FILES )
2013-03-10 22:42:54 +00:00
{
2013-08-01 00:37:18 +00:00
recentlist . Remove ( recentlist [ recentlist . Count - 1 ] ) ;
2013-03-10 22:42:54 +00:00
}
2011-06-19 23:39:25 +00:00
}
2011-01-17 01:58:19 +00:00
2011-06-19 23:39:25 +00:00
public bool Remove ( string newFile )
{
bool removed = false ;
2013-08-01 00:37:18 +00:00
for ( int x = 0 ; x < recentlist . Count ; x + + )
2011-06-19 23:39:25 +00:00
{
2013-08-01 00:37:18 +00:00
if ( string . Compare ( newFile , recentlist [ x ] ) = = 0 )
2011-06-19 23:39:25 +00:00
{
2013-08-01 00:37:18 +00:00
recentlist . Remove ( newFile ) ; //intentionally keeps iterating after this to remove duplicate instances, though those should never exist in the first place
2011-06-19 23:39:25 +00:00
removed = true ;
}
}
return removed ;
}
2011-01-17 01:58:19 +00:00
2011-06-19 23:39:25 +00:00
public List < string > GetRecentListTruncated ( int length )
{
//iterate through list, truncating each item to length, and return the result in a List<string>
2013-08-01 00:37:18 +00:00
return recentlist . Select ( t = > t . Substring ( 0 , length ) ) . ToList ( ) ;
2011-06-19 23:39:25 +00:00
}
public string GetRecentFileByPosition ( int position )
{
2013-08-01 00:37:18 +00:00
if ( recentlist . Count > 0 )
2013-03-10 22:42:54 +00:00
{
2013-08-01 00:37:18 +00:00
return recentlist [ position ] ;
2013-03-10 22:42:54 +00:00
}
else
{
2012-04-04 02:23:47 +00:00
return "" ;
2013-03-10 22:42:54 +00:00
}
2011-06-19 23:39:25 +00:00
}
}
2011-01-17 01:58:19 +00:00
}