/*$cb
 *    This file is a part of OpenMHP VDR plugin.
 *
 *    Copyright (C) 2005 Mikko Novaro <miknov@gmail.com>
 *
 *    See README file for licensing and contact information.
 $ce*/

#include "menu.h"
#include <vdr/plugin.h>
#include <vdr/device.h>
#include "osd.h"
#include "macros.h"
#include "openmhp.h"

// ---- class cOpenmhpMenu -----------

cOpenmhpMenu::cOpenmhpMenu(cPluginOpenmhp *OPENMHP):cOsdMenu("OpenMHP") {
  openmhp = OPENMHP;
  cXletFactory *factory = new cXletFactory(openmhp->setup->MhpCacheDir, cDevice::CurrentChannel());
  cXlet *xlet = NULL;
  while ((xlet = factory->Next())) Add(new cXletMenuItem(xlet));
  delete factory;
}

cOpenmhpMenu::~cOpenmhpMenu() {}

eOSState cOpenmhpMenu::Start(void) {
  cXletMenuItem *xmi = (cXletMenuItem *)Get(Current());
  cXlet *x = xmi->GetXlet();

  openmhp->xlet = new cXlet(x->GetName(), x->GetClass(), x->GetPath());
  
  CloseSubMenu();
  return osPlugin; // causes a call to cPluginsOpenmhp::MainMenuAction()
}



eOSState cOpenmhpMenu::ProcessKey(eKeys Key)
{

  eOSState state = cOsdMenu::ProcessKey(Key);

  if (state == osUnknown) {
    switch (Key) {
    case kOk:
      return Start();
    default:
      break;
    }
  }
  return state;
}


// ---- class cXletMenuItem -----------

cXletMenuItem::cXletMenuItem(cXlet *x) : cOsdItem(x->GetName()) {
  xlet = x;
}

cXletMenuItem::~cXletMenuItem() {
  delete xlet;
  xlet = NULL;
}

cXlet *cXletMenuItem::GetXlet(void) { return xlet; }

