/**
 * setup_favourites.c: Setup submenu for creating/deleting favourite entries
 * 
 * Copyright: See COPYING file that comes with this distribution
 *
 * Author: Matthias Feistel <hitman_47@users.sourceforge.net>, (C) 2006, 2007
 */

#include <vdr/menuitems.h>
#include "setup_favourites.h"
#include "settings.h"
#include "utils.h" // store not case sensitive

cSetupFavourites::cSetupFavourites(	std::list<std::string> &FavouriteList )
:cOsdMenu(tr("Favourites"),20)
,m_FavouriteList(FavouriteList)
{
	strncpy( newfavourite, "", 511 );

	// OSD-Elemente aufbauen
	BuildItems( );
}

void cSetupFavourites::BuildItems( void )
{
	Clear( );
	
	SetHelp( 0, 0, tr( "Remove" ) );
	
	Add ( new cMenuEditStrItem ( tr( "New entry" ),
			newfavourite, sizeof(newfavourite), FileNameChars) );
	
	for( std::list<std::string>::iterator it = m_FavouriteList.begin( );
		it != m_FavouriteList.end( );
		it++ )
	{
		std::string it_string = *it;
		Add( new cOsdItem(it_string.c_str() ) );
		
		Display( );
	}
	
}

eOSState cSetupFavourites::ProcessKey( eKeys Key )
{
	eOSState state = cOsdMenu::ProcessKey( Key );
	
	if( state == osUnknown )
	{
		switch( Key & ~k_Repeat )
		{
			
			case kOk:
				if( strcmp( newfavourite, "" ) )
				{
					m_FavouriteList.push_back( newfavourite );
					// sort favouritelist
					// data will be sorted here, because of new entered items by user;
					// these items have not been stored in setup.conf, yet.
					m_FavouriteList.sort( cUtils::compare_nocase );
				}
				
				return osBack;
				
			case kYellow:
				m_FavouriteList.remove( Get( Current() )->Text( ) );
				
				Del( Current( )); /// Im aktuellen OSD ebenfalls entfernen
				Display( );
				
				return osContinue;
				
			default:
				state = osContinue;
				break;
		}
	}
	return state;
	
}
