2011-01-18 03:40:53 +00:00
using System.Collections.Generic ;
2011-01-17 01:58:19 +00:00
using System.Text ;
namespace BizHawk.MultiClient
{
2011-01-18 03:40:53 +00:00
public class RecentFiles : IConfigSerializable
2011-01-17 01:58:19 +00:00
{
private int MAX_RECENT_FILES ; //Maximum number of files
private List < string > recentlist ; //List of recent files
2011-01-18 03:40:53 +00:00
public RecentFiles ( ) : this ( 8 ) { }
2011-01-17 02:11:31 +00:00
public RecentFiles ( int max )
2011-01-17 01:58:19 +00:00
{
recentlist = new List < string > ( ) ;
MAX_RECENT_FILES = max ;
}
2011-01-17 02:11:31 +00:00
public void Clear ( )
2011-01-17 01:58:19 +00:00
{
recentlist . Clear ( ) ;
}
2011-01-17 02:11:31 +00:00
public bool IsEmpty ( )
2011-01-17 01:58:19 +00:00
{
if ( recentlist . Count = = 0 )
return true ;
else
return false ;
}
2011-01-17 02:34:52 +00:00
public int Length ( )
{
return recentlist . Count ;
}
2011-01-17 02:11:31 +00:00
public void Add ( string newFile )
2011-01-17 01:58:19 +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
}
}
2011-01-18 03:56:31 +00:00
recentlist . Insert ( 0 , newFile ) ;
2011-01-17 02:40:08 +00:00
if ( recentlist . Count > MAX_RECENT_FILES )
2011-01-18 04:03:46 +00:00
recentlist . Remove ( recentlist [ recentlist . Count - 1 ] ) ;
2011-01-17 01:58:19 +00:00
}
2011-01-17 02:11:31 +00:00
public bool Remove ( string newFile )
2011-01-17 01:58:19 +00:00
{
bool removed = false ;
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
removed = true ;
}
2011-01-18 03:56:31 +00:00
}
2011-01-17 01:58:19 +00:00
return removed ;
}
2011-01-17 02:11:31 +00:00
public List < string > GetRecentList ( )
2011-01-17 01:58:19 +00:00
{
return recentlist ;
}
2011-01-17 02:11:31 +00:00
public List < string > GetRecentListTruncated ( int length )
2011-01-17 01:58:19 +00:00
{
//iterate through list, truncating each item to length, and return the result in a List<string>
List < string > temp = new List < string > ( ) ;
for ( int x = 0 ; x < recentlist . Count ; x + + )
{
2011-01-17 01:59:03 +00:00
temp . Add ( recentlist [ x ] . Substring ( 0 , length ) ) ;
2011-01-17 01:58:19 +00:00
}
return temp ;
}
2011-01-17 02:11:31 +00:00
public string GetRecentFileByPosition ( int position )
2011-01-17 01:58:19 +00:00
{
return recentlist [ position ] ;
}
2011-01-18 03:40:53 +00:00
public override string ToString ( )
{
var sb = new StringBuilder ( ) ;
sb . Append ( MAX_RECENT_FILES ) ;
sb . Append ( "@" ) ;
foreach ( string file in recentlist )
sb . AppendFormat ( "\"{0}\"|" , file ) ;
return sb . ToString ( ) ;
}
public void Deserialize ( string str )
{
var sections = str . Split ( '@' ) ;
MAX_RECENT_FILES = int . Parse ( sections [ 0 ] ) ;
var files = sections [ 1 ] . Split ( '|' ) ;
recentlist . Clear ( ) ;
foreach ( string file in files )
if ( string . IsNullOrEmpty ( file ) = = false )
recentlist . Add ( file . Replace ( "\"" , "" ) ) ;
}
2011-01-17 01:58:19 +00:00
}
}