/***************************************************************************
 *   Copyright (C) 2005 by Reel Multimedia                                 *
 *                                                                         *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "userIf.h"
#include "browserStatus.h"
#include "stringtools.h"
#include "def.h"

#include "vdr/interface.h"

#include <time.h>

//-------------------------------------------------------------
//----------------cMenuBrowserBaseIf---------------------------
//-------------------------------------------------------------

void cMenuBrowserBaseIf::EnterState(cMenuFileBrowser *menu, cMenuBrowserBaseIf *lastIf)  
{
    SetHelp(menu);
}

void cMenuBrowserBaseIf::SetHelp(cMenuFileBrowser *menu)
{
    menu->setHelp.SetRed("");
    menu->setHelp.SetGreen("");
    menu->setHelp.SetYellow("");
    menu->setHelp.SetBlue("");
    menu->setHelp.FlushButtons(false); 
}

//-------------------------------------------------------------
//-----------------cMenuBrowserBlockedIf-----------------------
//-------------------------------------------------------------

cMenuBrowserBlockedIf cMenuBrowserBlockedIf::instance;

eOSState cMenuBrowserBlockedIf::AnyKey(cMenuFileBrowser *menu, eKeys Key)
{
   menu->GetFinishedTasks();
   if(!menu->HasUnfinishedTasks())
   {
       menu->userIf_ = cMenuBrowserStandardIf::Instance();
       menu->userIf_->EnterState(menu, this);
   }
   return osContinue;
}

//-------------------------------------------------------------
//----------------cMenuBrowserNavigateIf-----------------------
//-------------------------------------------------------------

eOSState cMenuBrowserNavigateIf::OkKey(cMenuFileBrowser *menu)  
{
    return menu->OpenCurrent(); //do not change state
}

eOSState cMenuBrowserNavigateIf::BackKey(cMenuFileBrowser *menu)  
{
    return menu->ExitDir();
}

eOSState cMenuBrowserNavigateIf::InfoKey(cMenuFileBrowser *menu)  
{
    return menu->OpenCurrentInfo(); //do not change state
}

eOSState cMenuBrowserNavigateIf::AnyKey(cMenuFileBrowser *menu, eKeys Key)
{
    eOSState state = menu->StandardKeyHandling(Key);
    if(!menu->GetBrowserSubMenu())
    {
        SetHelp(menu);
#ifndef BLOCK_DURING_BKG_ACTIVITY
        //get finished background tasks
        menu->GetFinishedTasks();
#endif
       if(menu->CurrentChanged())
       {
           menu->UpdateCurrent();
       }
    }
    return state;
}

//-------------------------------------------------------------
//----------------cMenuBrowserRequestIf------------------------
//-------------------------------------------------------------

eOSState cMenuBrowserRequestIf::AnyKey(cMenuFileBrowser *menu, eKeys Key)
{
    eOSState state = menu->StandardKeyHandling(Key);
    if(Interface->Confirm(GetRequestString().c_str()))
    {
        Action(menu);
        state = LeaveState(menu, true);
    }
    else
    {
        state = LeaveState(menu, false);
    } 
    return state;
}

//-------------------------------------------------------------
//----------------cMenuBrowserDeleteRequestIf------------------
//-------------------------------------------------------------

cMenuBrowserDeleteRequestIf cMenuBrowserDeleteRequestIf::instance;

void cMenuBrowserDeleteRequestIf::EnterState(cMenuFileBrowser *menu, cMenuBrowserBaseIf *lastIf)
{
    SetHelp(menu);
    menu->SetTitleString(menu->GetTitleString());
    menu->Display();
}

void cMenuBrowserDeleteRequestIf::Action(cMenuFileBrowser *menu)
{
#ifdef APPLY_COMMANDS_ONLY_TO_MARKED_ENTRIES
    menu->DeleteMarkedEntries();
#else
    menu->DeleteMarkedOrCurrent();
#endif
}

eOSState cMenuBrowserDeleteRequestIf::LeaveState(cMenuFileBrowser *menu, bool ok)
{
    if(menu->GetMode() == autostart)
    {
        menu->userIf_ = cMenuBrowserAutoStartIf::Instance();
    }
    else
    {
        menu->userIf_ = cMenuBrowserStandardIf::Instance();
    }
    if(ok)
    {
#ifdef BLOCK_DURING_BKG_ACTIVITY
        //enter the blocked state
        menu->userIf_ = cMenuBrowserBlockedIf::Instance(); 
#endif
        Skins.Message(mtInfo, tr("Deleting"));
    }
    menu->userIf_->EnterState(menu, this);
    return osContinue;
}

std::string cMenuBrowserDeleteRequestIf::GetRequestString()
{
    return tr("Really Delete?");
}

//-------------------------------------------------------------
//----------------cMenuBrowserShutdownRequestIf----------------
//-------------------------------------------------------------

cMenuBrowserShutdownRequestIf cMenuBrowserShutdownRequestIf::instance;

eOSState cMenuBrowserShutdownRequestIf::LeaveState(cMenuFileBrowser *menu, bool ok)
{
    if(ok)
    {
        menu->userIf_ = cMenuBrowserShutdownOptionsIf::Instance();
        menu->userIf_->EnterState(menu, this);
        return osContinue;
    }
    return osBack;
}

std::string cMenuBrowserShutdownRequestIf::GetRequestString()
{
    return tr("Save/copy current playlist?");
}

//-------------------------------------------------------------
//----------------cMenuBrowserRequestAndNavigateIf-------------
//-------------------------------------------------------------

eOSState cMenuBrowserRequestAndNavigateIf::RedKey(cMenuFileBrowser *menu)
{
    LeaveState(menu, false);
    return osContinue;
}

eOSState cMenuBrowserRequestAndNavigateIf::GreenKey(cMenuFileBrowser *menu)
{
    Action(menu);
    LeaveState(menu, true);
    return osContinue;
}

void cMenuBrowserRequestAndNavigateIf::LeaveState(cMenuFileBrowser *menu, bool ok)
{
    if(menu->GetMode() == autostart)
    {
        menu->userIf_ = cMenuBrowserAutoStartIf::Instance();
    }
    else
    {
        menu->userIf_ = cMenuBrowserStandardIf::Instance();
    }
    if(ok)
    {
#ifdef BLOCK_DURING_BKG_ACTIVITY
        //enter the blocked state
        menu->userIf_ = cMenuBrowserBlockedIf::Instance(); 
#endif
        SetMessage();
    }
    menu->userIf_->EnterState(menu, this);
}

void cMenuBrowserRequestAndNavigateIf::SetMessage()
{
    Skins.Message(mtInfo, tr("Working"));
}
//-------------------------------------------------------------
//----------------cMenuBrowserCopyRequestIf--------------------
//-------------------------------------------------------------

cMenuBrowserCopyRequestIf cMenuBrowserCopyRequestIf::instance;

eOSState cMenuBrowserCopyRequestIf::YellowKey(cMenuFileBrowser *menu)
{
    menu->userIf_ = cMenuBrowserCreateDirIf::Instance();
    menu->userIf_->EnterState(menu, this);
    return osContinue;
}

void cMenuBrowserCopyRequestIf::EnterState(cMenuFileBrowser *menu, cMenuBrowserBaseIf *lastIf)
{
    SetHelp(menu);
    menu->SetTitleString(menu->GetTitleString());
    menu->Display();
    Skins.Message(mtInfo, tr("Paste selected entries"));
}

void cMenuBrowserCopyRequestIf::SetHelp(cMenuFileBrowser *menu)
{
    menu->setHelp.SetRed("Cancel");
    menu->setHelp.SetGreen("Insert");
    menu->setHelp.SetYellow("Create dir");
    menu->setHelp.SetBlue("");
    menu->setHelp.FlushButtons(false);
}

void cMenuBrowserCopyRequestIf::Action(cMenuFileBrowser *menu)
{
    menu->InsertCopiedEntries();
}

void cMenuBrowserCopyRequestIf::SetMessage()
{
    Skins.Message(mtInfo, tr("Copying"));
}

//----------------------------------------------------------------
//----------------cMenuBrowserMoveRequestIf-----------------------
//----------------------------------------------------------------

cMenuBrowserMoveRequestIf cMenuBrowserMoveRequestIf::instance;

void cMenuBrowserMoveRequestIf::Action(cMenuFileBrowser *menu)
{
    menu->InsertMovedEntries();
}

void cMenuBrowserMoveRequestIf::SetMessage()
{
    Skins.Message(mtInfo, tr("Moving"));
}

//-------------------------------------------------------------
//----------------cMenuBrowserCopyToHdRequestIf--------------------
//-------------------------------------------------------------

cMenuBrowserCopyToHdRequestIf cMenuBrowserCopyToHdRequestIf::instance;

eOSState cMenuBrowserCopyToHdRequestIf::BlueKey(cMenuFileBrowser *menu)
{
    menu->SetMode(expert);
    menu->Refresh();
    return osContinue;
}

void cMenuBrowserCopyToHdRequestIf::SetHelp(cMenuFileBrowser *menu)
{
    menu->setHelp.SetRed("Cancel");
    menu->setHelp.SetGreen("Insert");
    menu->setHelp.SetYellow("Create dir");
    menu->setHelp.SetBlue("Expert mode");
    menu->setHelp.FlushButtons(false);
}

void cMenuBrowserCopyToHdRequestIf::EnterState(cMenuFileBrowser *menu, cMenuBrowserBaseIf *lastIf)
{
    SetHelp(menu);
    menu->SetTitleString(menu->GetTitleString());
    if(lastIf == cMenuBrowserAutoStartIf::Instance())
    {
        menu->ChangeDir(HD_DIR);
    }
    menu->Display();
    Skins.Message(mtInfo, tr("Paste selected entries"));
}

//-------------------------------------------------------------
//----------------cMenuBrowserMoveToHdRequestIf--------------------
//-------------------------------------------------------------

cMenuBrowserMoveToHdRequestIf cMenuBrowserMoveToHdRequestIf::instance;

void cMenuBrowserMoveToHdRequestIf::SetMessage()
{
    Skins.Message(mtInfo, tr("Moving"));
}

//----------------------------------------------------------------
//----------------cMenuBrowserStandardIf--------------------------
//----------------------------------------------------------------

cMenuBrowserStandardIf cMenuBrowserStandardIf::instance;

eOSState cMenuBrowserStandardIf::RedKey(cMenuFileBrowser *menu)
{
     menu->MarkUnmarkCurrentItem();
     SetHelp(menu);
     return osContinue;  
}

eOSState cMenuBrowserStandardIf::GreenKey(cMenuFileBrowser *menu)
{
    menu->MarkUnmarkAllFiles();
    SetHelp(menu);
    return osContinue;
}

eOSState cMenuBrowserStandardIf::YellowKey(cMenuFileBrowser *menu)
{
   //Insert entries in playlist 
#ifdef APPLY_COMMANDS_ONLY_TO_MARKED_ENTRIES
    bool success = menu->InsertMarkedEntriesInPlayList();
#else
    bool success = menu->InsertMarkedOrCurrentInPlayList();
#endif
    if(!success)
    {
        Skins.Message(mtError, tr("Too many entries for playlist!"));
    }
    return menu->EnterPlayList();
}

eOSState cMenuBrowserStandardIf::BlueKey(cMenuFileBrowser *menu)
{
     //enter command mode 
#ifdef USE_KEYS_FOR_COMMANDS
     menu->userIf_ = cMenuBrowserCommandIf::Instance();
#else
     menu->userIf_ = cMenuBrowserCmdOptionsIf::Instance();
#endif
     menu->userIf_->EnterState(menu, this);
     return osContinue;
}

void cMenuBrowserStandardIf::SetHelp(cMenuFileBrowser *menu)
{
    SetHelpMarkUnmark(menu);
    menu->setHelp.SetYellow("Playlist");
    menu->setHelp.SetBlue("Commands");
    menu->setHelp.FlushButtons(false);
}

void cMenuBrowserStandardIf::EnterState(cMenuFileBrowser *menu, cMenuBrowserBaseIf *lastIf)
{ 
    ChangeIfType(if_standard);
    menu->SetTitleString(menu->GetTitleString());
    SetHelp(menu);
    menu->Display();
}

void cMenuBrowserStandardIf::SetHelpMarkUnmark(cMenuFileBrowser *menu)
{
    if(menu->NoFiles())
    {
	menu->setHelp.SetGreen("");
    }
    else if(menu->AllFilesAreMarked())
    {
	menu->setHelp.SetGreen("Deselect all");
    }
    else
    {
	menu->setHelp.SetGreen("Select all"); 
    }

    cMenuBrowserItem *item = menu->GetCurrentItem();
    if(item && menu->IsMarked(item))
    {
	menu->setHelp.SetRed("Deselect");
    }
    else
    {
	menu->setHelp.SetRed("Select");
    }
}

void cMenuBrowserStandardIf::SetMode(cMenuFileBrowser *menu, browseMode mode)
{
    if(mode == restricted)
    {
        menu->userIf_ = cMenuBrowserRestrictedIf::Instance(); 
        menu->userIf_->EnterState(menu, this);
    }
    else if(mode == browseplaylist)
    {
        menu->userIf_ = cMenuBrowserBrowsePlayListIf::Instance();
        menu->userIf_->EnterState(menu, this);
    }
}

//----------------------------------------------------------------
//----------------cMenuBrowserRestrictedIf--------------------------
//----------------------------------------------------------------

cMenuBrowserRestrictedIf cMenuBrowserRestrictedIf::instance;

eOSState cMenuBrowserRestrictedIf::OkKey(cMenuFileBrowser *menu)
{
    cMenuDirEntryItem *currentItem = menu->GetCurrentDirEntryItem();
    bool playlistEntered = false;
    eOSState state = osContinue;
    if(currentItem && currentItem->IsDir() && !menu->IsMarked(currentItem))
    {
        //state = menu->OpenCurrent(false);
        state = menu->PlayCurrent();
    }
    else if(menu->NothingIsMarked() && currentItem && currentItem->IsFile())
    {
        state = menu->OpenCurrent(false);
    }
    else
    {
        state =  menu->OpenMarked(playlistEntered);
    }
    if(playlistEntered)
    {
        menu->SetMode(browseplaylist);
    }
    return state;
}

eOSState cMenuBrowserRestrictedIf::YellowKey(cMenuFileBrowser *menu)
{
    return osContinue;
}

void cMenuBrowserRestrictedIf::EnterState(cMenuFileBrowser *menu, cMenuBrowserBaseIf *lastIf)
{
    ChangeIfType(if_restricted);
    menu->SetTitleString(menu->GetTitleString());
    SetHelp(menu);
    menu->Display();
}

void cMenuBrowserRestrictedIf::SetHelp(cMenuFileBrowser *menu)
{
    SetHelpMarkUnmark(menu);
    menu->setHelp.SetYellow("");
    menu->setHelp.SetBlue("Commands");
    menu->setHelp.FlushButtons(false);
}

void cMenuBrowserRestrictedIf::SetMode(cMenuFileBrowser *menu, browseMode mode)
{
    if(mode == expert)
    {
        menu->userIf_ = cMenuBrowserStandardIf::Instance(); 
        menu->userIf_->EnterState(menu, this);
    }
    else if(mode == browseplaylist)
    {
        menu->userIf_ = cMenuBrowserBrowsePlayListIf::Instance();
        menu->userIf_->EnterState(menu, this);
    }
}

//----------------------------------------------------------------
//----------------cMenuBrowserBrowsePlaylistIf--------------------
//----------------------------------------------------------------

cMenuBrowserBrowsePlayListIf cMenuBrowserBrowsePlayListIf::instance;

eOSState cMenuBrowserBrowsePlayListIf::OkKey(cMenuFileBrowser *menu)
{
    cMenuDirEntryItem *currentItem = menu->GetCurrentDirEntryItem();
    bool playlistEntered = false;
    eOSState state = osContinue;
    if(currentItem && currentItem->IsDir() && !menu->IsMarked(currentItem))
    {
        return menu->OpenCurrent(false);
    }
    else if(menu->NothingIsMarked() && currentItem && currentItem->IsFile())
    {
        menu->InsertCurrentInPlayList();
        if(GetCaller() == if_xine)
        {
            state = menu->OpenCurrent(true);
        }
        else if(GetCaller() == if_playlistmenu)
        {
            //go back to playlistmenu
            state = menu->EnterPlayList();
        }
    }
    else
    {
        if(GetCaller() == if_xine)
        {
            //go back to xine 
            state =  menu->OpenMarked(playlistEntered);
        }
        else if(GetCaller() == if_playlistmenu)
        {
            //go back to playlistmenu
            menu->InsertMarkedEntriesInPlayList(); 
            state = menu->EnterPlayList();
        }
    }
    return state;
}

eOSState cMenuBrowserBrowsePlayListIf::BackKey(cMenuFileBrowser *menu)
{
    eOSState state = menu->ExitDir();
    if(state == osBack && !menu->PlayListIsEmpty()) //exit in browseplaylist mode?
    {
        menu->userIf_ = cMenuBrowserShutdownRequestIf::Instance();
        menu->userIf_->EnterState(menu, this);
        return osContinue;
    }
    return state;
}

eOSState cMenuBrowserBrowsePlayListIf::YellowKey(cMenuFileBrowser *menu)
{
    if(GetCaller() == if_xine)
    {
       //go back to xine
       return menu->ShowPlayListWithXine();
    }
    else if(GetCaller() == if_playlistmenu)
    {
       //go back to playlistmenu
       return menu->EnterPlayList();
    }
    return osContinue;
}

void cMenuBrowserBrowsePlayListIf::SetHelp(cMenuFileBrowser *menu)
{
    SetHelpMarkUnmark(menu);
    if(GetCaller() == if_xine)
    {
        menu->setHelp.SetYellow("Back to xine");
    }
    else if(GetCaller() == if_playlistmenu)
    {
        menu->setHelp.SetYellow("Back to playlist");
    }
    menu->setHelp.FlushButtons(false);
}

void cMenuBrowserBrowsePlayListIf::EnterState(cMenuFileBrowser *menu, cMenuBrowserBaseIf *lastIf)
{
    cUserIfBase::PrintIfTypes();
    ChangeIfType(if_playlistbrowser);
    menu->SetTitleString(menu->GetTitleString() + ": Insert in playlist" );
    SetHelp(menu);
    menu->Display();
}

void cMenuBrowserBrowsePlayListIf::SetMode(cMenuFileBrowser *menu, browseMode mode)
{
    if(mode == expert)
    {
        menu->userIf_ = cMenuBrowserStandardIf::Instance(); 
        menu->userIf_->EnterState(menu, this);
    }
    else if(mode == restricted)
    {
        menu->userIf_ = cMenuBrowserRestrictedIf::Instance();
        menu->userIf_->EnterState(menu, this);
    }
}

//----------------------------------------------------------------
//----------------cMenuBrowserAutoStartIf--------------------------
//----------------------------------------------------------------

cMenuBrowserAutoStartIf cMenuBrowserAutoStartIf::instance;

eOSState cMenuBrowserAutoStartIf::YellowKey(cMenuFileBrowser *menu)
{
    menu->CopyMarkedOrCurrent(); 
    menu->userIf_ = cMenuBrowserCopyToHdRequestIf::Instance();
    menu->userIf_->EnterState(menu, this);
    return osContinue;
}

void cMenuBrowserAutoStartIf::SetHelp(cMenuFileBrowser *menu)
{
    SetHelpMarkUnmark(menu);
    menu->setHelp.SetYellow("Copy");
    menu->setHelp.SetBlue("Commands");
    menu->setHelp.FlushButtons(false);
}

//----------------------------------------------------------------
//----------------cMenuBrowserOptionsIf---------------------------
//----------------------------------------------------------------

cMenuBrowserOptionsIf cMenuBrowserOptionsIf::instance;

eOSState cMenuBrowserOptionsIf::OkKey(cMenuFileBrowser *menu)
{
    std::string option = menu->Get(menu->Current())->Text();
    menu->Hide(false);
    return ExecuteOptions(menu, option);
}

eOSState cMenuBrowserOptionsIf::BackKey(cMenuFileBrowser *menu)
{
    menu->Hide(false); 
    LeaveStateDefault(menu);
    return osContinue;
}

void cMenuBrowserOptionsIf::EnterState(cMenuFileBrowser *menu, cMenuBrowserBaseIf *lastIf)
{
    lastIf_ = lastIf;
    CreateOptions(menu);
    menu->Hide(true);
    AddOptions(menu);
    SetHelp(menu);
    menu->Display();
}

void cMenuBrowserOptionsIf::LeaveStateDefault(cMenuFileBrowser *menu)
{
    if(lastIf_)
    {
        menu->userIf_ = lastIf_;
    }
    else
    {
        menu->userIf_ = cMenuBrowserStandardIf::Instance();
    }
    menu->userIf_->EnterState(menu, this);
}

void cMenuBrowserOptionsIf::AddOptions(cMenuFileBrowser *menu)
{
    char buf[256];
    //TODO: use stringstreams
    for(uint i = 0; i < options_.size(); ++i)
    {
        sprintf(buf, "%d %s", i + 1, options_[i].c_str());
        menu->Add(new cOsdItem(buf, osUnknown, true));
    }
}

//----------------------------------------------------------------
//----------------cMenuBrowserCmdOptionsIf------------------------
//----------------------------------------------------------------

cMenuBrowserCmdOptionsIf cMenuBrowserCmdOptionsIf::instance;

eOSState cMenuBrowserCmdOptionsIf::ExecuteOptions(cMenuFileBrowser *menu, std::string option)
{;
    if(option.find(tr("Copy")) == 2)
    {
        return Copy(menu);
    }
    else if(option.find(tr("Move")) == 2)
    {
        return Move(menu);
    }
    else if(option.find(tr("Delete")) == 2)
    {
        return Delete(menu);
    }
    else if(option.find(tr("Rename entry")) == 2)
    {
        return Rename(menu);
    }
    else if(option.find(tr("Create directory")) == 2)
    {
        return CreateDir(menu);
    }
    else if(option.find(tr("Insert in playlist")) == 2)
    {
        return InsertInPlayList(menu);
    }
    else if(option.find(tr("Show playlist")) == 2)
    {
        return ShowPlayList(menu);
    }
    return osContinue;
}

void cMenuBrowserCmdOptionsIf::CreateOptions(cMenuFileBrowser *menu)
{
    menu->SetTitleString(tr("Filebrowser Commands"));
    options_.clear();
#ifdef APPLY_COMMANDS_ONLY_TO_MARKED_ENTRIES
    if(menu->HasMarkedDirEntryItems())
#else
    if(menu->GetCurrentDirEntryItem() && GetLast(menu->GetCurrentDirEntryItem()->GetPath()) != ".." || menu->HasMarkedDirEntryItems())
#endif
    {
        options_.push_back(tr("Copy"));
        options_.push_back(tr("Move"));
        options_.push_back(tr("Delete"));
    }
#ifdef APPLY_COMMANDS_ONLY_TO_MARKED_ENTRIES
    if(menu->GetSingleMarkedDirEntryItem()) 
#else 
    if(menu->GetSingleMarkedDirEntryItem()
    || menu->GetCurrentDirEntryItem() && GetLast(menu->GetCurrentDirEntryItem()->GetPath()) != ".." && menu->NothingIsMarked())
#endif
    {
        options_.push_back(tr("Rename entry"));
    }
    options_.push_back(tr("Create directory"));
#ifdef APPLY_COMMANDS_ONLY_TO_MARKED_ENTRIES 
    if(menu->HasMarkedDirEntryItems())
#else
    if(menu->GetCurrentDirEntryItem() && GetLast(menu->GetCurrentDirEntryItem()->GetPath()) != ".." || menu->HasMarkedDirEntryItems())
#endif
    { 
        options_.push_back(tr("Insert in playlist"));
    }
    options_.push_back(tr("Show playlist"));
}

eOSState cMenuBrowserCmdOptionsIf::Copy(cMenuFileBrowser *menu)
{
    //Copy entries
#ifdef APPLY_COMMANDS_ONLY_TO_MARKED_ENTRIES
    menu->CopyMarkedEntries();
#else
    menu->CopyMarkedOrCurrent();
#endif
    if(menu->GetMode() == autostart)
    {
        menu->userIf_ = cMenuBrowserCopyToHdRequestIf::Instance();
    }
    else
    {
        menu->userIf_ = cMenuBrowserCopyRequestIf::Instance();
    }
    menu->userIf_->EnterState(menu, lastIf_);
    return osContinue;
}

eOSState cMenuBrowserCmdOptionsIf::Move(cMenuFileBrowser *menu)
{
    //Copy entries (for move)
#ifdef APPLY_COMMANDS_ONLY_TO_MARKED_ENTRIES
    menu->CopyMarkedEntries();
#else
    menu->CopyMarkedOrCurrent(); 
#endif
    if(menu->GetMode() == autostart)
    {
        menu->userIf_ = cMenuBrowserMoveToHdRequestIf::Instance();
    }
    else
    {
        menu->userIf_ = cMenuBrowserMoveRequestIf::Instance();
    }
    menu->userIf_->EnterState(menu, lastIf_);
    return osContinue;
}

eOSState cMenuBrowserCmdOptionsIf::Delete(cMenuFileBrowser *menu)
{
    //Delete entries
    menu->userIf_ = cMenuBrowserDeleteRequestIf::Instance();
    menu->userIf_->EnterState(menu, lastIf_);
    return osContinue;
}

eOSState cMenuBrowserCmdOptionsIf::Rename(cMenuFileBrowser *menu)
{
    //Rename Entry
    menu->userIf_ = cMenuBrowserRenameEntryIf::Instance();
    menu->userIf_->EnterState(menu, lastIf_);
    return osContinue;
}

eOSState cMenuBrowserCmdOptionsIf::CreateDir(cMenuFileBrowser *menu)
{
    //Create Directory 
    menu->userIf_ = cMenuBrowserCreateDirIf::Instance();
    menu->userIf_->EnterState(menu, lastIf_);
    return osContinue;
}

eOSState cMenuBrowserCmdOptionsIf::InsertInPlayList(cMenuFileBrowser *menu)
{
    //Insert entries in playlist 
#ifdef APPLY_COMMANDS_ONLY_TO_MARKED_ENTRIES
    bool success = menu->InsertMarkedEntriesInPlayList();
#else
    bool success = menu->InsertMarkedOrCurrentInPlayList();
#endif
    if(!success)
    {
        Skins.Message(mtError, tr("Too many entries for playlist!"));
    }
    LeaveStateDefault(menu);
    return menu->EnterPlayList();
}

eOSState cMenuBrowserCmdOptionsIf::ShowPlayList(cMenuFileBrowser *menu)
{
    //Open playlist without inserting items
    LeaveStateDefault(menu);
    return menu->EnterPlayList();
}

//----------------------------------------------------------------
//----------------cMenuBrowserShutdownOptionsIf-------------------
//----------------------------------------------------------------

cMenuBrowserShutdownOptionsIf cMenuBrowserShutdownOptionsIf::instance;

eOSState cMenuBrowserShutdownOptionsIf::BackKey(cMenuFileBrowser *menu)
{
    return osEnd;
}

eOSState cMenuBrowserShutdownOptionsIf::ExecuteOptions(cMenuFileBrowser *menu, std::string option)
{
    //printf("------cMenuBrowserShutdownOptionsIf::ExecuteOptions: %s ------\n", option.c_str());    
    if(option.find(tr("Save playlist to disk")) == 2)
    {
        return SavePlayList(menu);
    }
    else if(option.find(tr("Copy playlist to usb stick")) == 2)
    {
        return CopyPlayListToUSB(menu);
    }
    else if(option.find(tr("Copy playlist to CD")) == 2)
    {
        return CopyPlayListToDVD(menu);
    }
    return osContinue;
}

void cMenuBrowserShutdownOptionsIf::CreateOptions(cMenuFileBrowser *menu)
{
    menu->SetTitleString(tr("Playlist actions"));
    options_.clear();
    {
        options_.push_back(tr("Save playlist to disk"));
        //options_.push_back(tr("Copy playlist to usb stick")); 
        options_.push_back(tr("Copy playlist to CD"));
    }
}

eOSState cMenuBrowserShutdownOptionsIf::KeepPlayList(cMenuFileBrowser *menu)
{
    return osEnd;
}

eOSState cMenuBrowserShutdownOptionsIf::DiscardPlayList(cMenuFileBrowser *menu)
{
    menu->ClearPlayList();
    return osEnd;
}

eOSState cMenuBrowserShutdownOptionsIf::SavePlayList(cMenuFileBrowser *menu)
{
    menu->SavePlayList();
    return osEnd;
}

eOSState cMenuBrowserShutdownOptionsIf::CopyPlayListToUSB(cMenuFileBrowser *menu)
{
    menu->CopyPlayListToUsb();
    return osEnd;
}

eOSState cMenuBrowserShutdownOptionsIf::CopyPlayListToDVD(cMenuFileBrowser *menu)
{
    menu->CopyPlayListToDvd();
    return osEnd;
}

//----------------------------------------------------------------
//----------------cMenuBrowserCreateDirIf-------------------------
//----------------------------------------------------------------

cMenuBrowserCreateDirIf cMenuBrowserCreateDirIf::instance;

eOSState cMenuBrowserCreateDirIf::OkKey(cMenuFileBrowser *menu)
{
    if(!CreateDir(menu))
    {
        Skins.Message(mtInfo, tr("Directory already exists"));
    }
    menu->Hide(false);
    LeaveStateDefault(menu);
    return osContinue;
}

void cMenuBrowserCreateDirIf::CreateOptions(cMenuFileBrowser *menu)
{ 
    menu->SetTitleString(tr("Create directory"));
    const char *const defEntryName = "Directory name";
    strcpy(name_, tr(defEntryName));
}

void cMenuBrowserCreateDirIf::AddOptions(cMenuFileBrowser *menu)
{
    menu->Add(new cMenuBrowserEditStrItem(menu,"", name_, sizeof(name_), trVDR(FileNameChars)));
}

bool cMenuBrowserCreateDirIf::CreateDir(cMenuFileBrowser *menu)
{
    return menu->CreateDirectory(name_);
}

//----------------------------------------------------------------
//----------------cMenuBrowserRenameEntryIf-----------------------
//----------------------------------------------------------------

cMenuBrowserRenameEntryIf cMenuBrowserRenameEntryIf::instance;

eOSState cMenuBrowserRenameEntryIf::OkKey(cMenuFileBrowser *menu)
{
    if(!RenameEntry(menu))
    {
        Skins.Message(mtInfo, tr("Entry already exists"));
    }
    menu->Hide(false);
    LeaveStateDefault(menu);
    return osContinue;
}

void cMenuBrowserRenameEntryIf::CreateOptions(cMenuFileBrowser *menu)
{
    menu->SetTitleString(tr("Rename entry"));
    if(menu->GetSingleMarkedDirEntryItem())
    {
        oldname_ = GetLast(menu->GetSingleMarkedDirEntryItem()->GetPath()).c_str();
    }
#ifndef APPLY_COMMANDS_ONLY_TO_MARKED_ENTRIES
    else if(menu->GetCurrentDirEntryItem())
    {
        oldname_ = GetLast(menu->GetCurrentDirEntryItem()->GetPath()).c_str();
    }
#endif
    strcpy(name_, oldname_.c_str());
}

void cMenuBrowserRenameEntryIf::AddOptions(cMenuFileBrowser *menu)
{
    menu->Add(new cMenuBrowserEditStrItem(menu,"", name_, sizeof(name_), trVDR(FileNameChars)));
}

bool cMenuBrowserRenameEntryIf::RenameEntry(cMenuFileBrowser *menu)
{
    return menu->RenameEntry(oldname_, name_);
}


