/*
 * menu_open.c: A plugin for the Video Disk Recorder
 *
 * See the README file for copyright information and how to reach the author.
 *
 * $Id$
 */

#include "viewer.h"
#include "menu_open.h"
#include "menu_syslog.h"
#include "tools.h"
#include <vdr/interface.h>
#include <vdr/menu.h>
#include <vdr/plugin.h>
#include <vdr/menu.h>
#include "menu_vwfilebrowser.h"

// --- cMenuViewerOpen ---------------------------------------------------------

cMenuViewerOpen::cMenuViewerOpen(const char *Title, char *filename, char *FileNames[MAX_VIEWER_FILES], char *syslog, bool DirectCall)
:cOsdMenu(Title)
{
  strn0cpy(Filename, filename, MaxFileName);
  pFilename=filename;
  Syslog = syslog;
  directCall = DirectCall;
  SetHasHotkeys();
  bSubMenuOk=false;

  for(int i=0;i<MAX_VIEWER_FILES;i++)
     if(strlen(FileNames[i])>0)
        Add(new cOsdItem(hk(FileNames[i])));

  SetHelp(tr("Open"), (strlen(Syslog)>0)? tr("Syslog-config"):NULL, NULL, NULL);
}

cMenuViewerOpen::~cMenuViewerOpen()
{
}

eOSState cMenuViewerOpen::HandleFilebrowser()
{
  bSubMenuOk=false;
  return AddSubMenu(new cMenuViewerFilebrowser(Filename, &bSubMenuOk));
}

eOSState cMenuViewerOpen::ProcessKey(eKeys Key)
{
  bool bHadSubMenu = HasSubMenu();
  eOSState state = cOsdMenu::ProcessKey(Key);

  // jump back to calling menu, if a command was called directly with key '1' .. '9'
  if (directCall && bHadSubMenu && !HasSubMenu())
     return osBack;

  if (state == osUnknown) {
     switch (Key) {
        case kRed:   return HandleFilebrowser();
                     break;
        case kGreen: if(strlen(Syslog))
                        return AddSubMenu(new cMenuViewerSyslog(tr("Select a syslog entry ..."), Filename, Syslog, &bSubMenuOk));
                     break;
	case kOk:    if (Count() > 0)
                        strn0cpy(pFilename, UnindentMenuItem(Get(Current())->Text()), MaxFileName);
		     return osBack;
        default:     break;
     }
  }
  if (bHadSubMenu && !HasSubMenu() && bSubMenuOk) {// return from submenu
     strn0cpy(pFilename, Filename, MaxFileName);
isyslog("cMenuViewerOpen::ProcessKey, %s", pFilename);
     bSubMenuOk=false;
     return osBack;
  }
  return state;
}
