/*
 * Softcam plugin to VDR (C++)
 *
 * This code 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 code 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.
 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
 */

#include "stdafx.h"

#include "vdr/tools.h"

#include "sc.h"
#include "system.h"
#include "smartcard.h"
#include "opts.h"

// --- cOpt --------------------------------------------------------------------

cOpt::cOpt(const char *Name, const char *Title)
{
	name=Name; title=Title;
	fullname=0; persistant=true;
}

cOpt::~cOpt()
{
	free(fullname);
}

// --- cOptInt -----------------------------------------------------------------

cOptInt::cOptInt(const char *Name, const char *Title, int *Storage, int Min, int Max)
:cOpt(Name,Title)
{
	storage=Storage; min=Min; max=Max;
}

// --- cOptSel -----------------------------------------------------------------

cOptSel::cOptSel(const char *Name, const char *Title, int *Storage, int NumStr, const char * const *Strings)
:cOptInt(Name,Title,Storage,0,NumStr)
{
	strings=Strings;
	trStrings=0;
}

cOptSel::~cOptSel()
{
	free(trStrings);
}

// --- cOptBool -----------------------------------------------------------------

cOptBool::cOptBool(const char *Name, const char *Title, int *Storage)
:cOptInt(Name,Title,Storage,0,1)
{}

// --- cOptStr -----------------------------------------------------------------

cOptStr::cOptStr(const char *Name, const char *Title, char *Storage, int Size, const char *Allowed)
:cOpt(Name,Title)
{
	storage=Storage; size=Size; allowed=Allowed;
	value=MALLOC(char,size);
}

cOptStr::~cOptStr()
{
	free(value);
}

// --- cOpts -------------------------------------------------------------------

cOpts::cOpts(const char *PreStr, int NumOpts)
{
	preStr=PreStr;
	numOpts=NumOpts; numAdd=0;
	if((opts=MALLOC(cOpt *,numOpts))) memset(opts,0,sizeof(cOpt *)*numOpts);
}

cOpts::~cOpts()
{
	if(opts) {
		for(int i=0; i<numOpts; i++) delete opts[i];
		free(opts);
	}
}

void cOpts::Add(cOpt *opt)
{
	if(opts && numAdd<numOpts) opts[numAdd++]=opt;
}

// --- cSoftCAM ---------------------------------------------------------------

int cSoftCAM::foundKeys=0;
int cSoftCAM::newKeys=0;

bool cSoftCAM::Load(const char *cfgdir)
{
	ecmcache.Load();
	if(!keys.Load(cfgdir)) printf("no keys loaded for softcam!\n");
	if(!cSystems::Init(cfgdir)) return false;
	smartcards.LoadData(cfgdir);
	cLoaders::LoadCache(cfgdir);
	Save();
	return true;
}

void cSoftCAM::Save(void)
{
	cLoaders::SaveCache();
}