/*
 * vdr-podcatcher - A plugin for the Linux Video Disk Recorder
 * Copyright (c) 2006 Tobias Grimm <vdr@e-tobi.net>
 *
 * 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
 *
 * $Id: podcatcherplugin.cc 3219 2006-04-16 15:06:35Z svntobi $
 *
 */

#include "podcatcherplugin.h"
#include <iostream>
#include <getopt.h>
#include "i18n.h"
#include "version.h"
#include "feedupdater.h"
#include "errorlogger.h"
#include "downloadcache.h"

using namespace std;

PodcatcherPlugin::PodcatcherPlugin()
:cacheDirName("/var/cache/podcatcher"), sourcesFileName(""), maxCacheAge(30)
{
}

void PodcatcherPlugin::SetServiceLocator(
  ref_ptr<IServiceLocator> serviceLocator)
{
    this->serviceLocator = serviceLocator;
}

const char* PodcatcherPlugin::Version(void)
{
    return VERSION;
}

const char* PodcatcherPlugin::Description(void)
{
    return tr("Download and play podcasts");
}

const char* PodcatcherPlugin::MainMenuEntry(void)
{
    return tr("Podcatcher");
}

bool PodcatcherPlugin::Initialize(void)
{
    RegisterI18n(Phrases);

    const char* configDir = ConfigDirectory();

    if (configDir)
    {
        sourcesFileName = string(configDir) + "/podcatchersources.conf";
    }

    if (CacheDirIsAccessible())
    {
        return true;
    }
    else
    {
        serviceLocator->GetErrorLogger()->Log(
          "Unable to access cache directory `" + GetCacheDirName() + "`");
        return false;
    }
}

const char* PodcatcherPlugin::CommandLineHelp(void)
{
    return "  -c, --cache=CACHE_DIR    specify cache dir, defaults to "
      "/var/cache/podcatcher";
}


bool PodcatcherPlugin::ProcessArgs(int argc, char* argv[])
{
    static struct option longOptions[] =
    {
        { "cache", required_argument, NULL, 'c'},
        { NULL}
    };

    optind = 0;
    opterr = 0;

    int optionChar;
    int optionIndex = 0;
    while ((optionChar = getopt_long(argc, argv, "c:", longOptions,
      &optionIndex)) != -1)
    {
        switch (optionChar)
        {
            case 'c':
            {
                cacheDirName = optarg;
                break;
            }
            default:
            {
                cerr << argv[0] << ": " << "invalid option " <<  argv[optind-1] << endl;
                return false;
            }
        }
    }
    return true;
}

string PodcatcherPlugin::GetCacheDirName()
{
    return cacheDirName;
}

string PodcatcherPlugin::GetSourcesFileName()
{
    return sourcesFileName;
}

cOsdObject* PodcatcherPlugin::MainMenuAction()
{
    return serviceLocator->CreateFeedMenu();
}

bool PodcatcherPlugin::Start()
{
    serviceLocator->GetDownloadThread()->Start();

    serviceLocator->GetFeedUpdater()->Update();
    return true;
}

void PodcatcherPlugin::Housekeeping()
{
    serviceLocator->GetFeedUpdater()->Update();
    serviceLocator->GetDownloadCache()->CleanUp(24 * GetMaxCacheAge());
}

bool PodcatcherPlugin::CacheDirIsAccessible()
{
    if (0 == access((GetCacheDirName()+"/.").c_str(), W_OK))
    {
        return true;
    }
    else
    {
        return false;
    }
}

bool PodcatcherPlugin::SetupParse(const char *Name, const char *Value)
{
    if (string(Name) == "MaxCacheAge")
    {
        maxCacheAge = atoi(Value);
        return true;
    }
    else
    {
        return false;
    }
}

int PodcatcherPlugin::GetMaxCacheAge()
{
    return maxCacheAge;
}

void PodcatcherPlugin::SetMaxCacheAge(int age)
{
    maxCacheAge = age;
}

cMenuSetupPage* PodcatcherPlugin::SetupMenu()
{
    return serviceLocator->CreateSetupMenu();
}
