/*
 * 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: downloadqueue.cc 2514 2006-01-28 18:52:39Z svntobi $
 *
 */

#include "downloadqueue.h"

using namespace std;

void DownloadQueue::AddDownload(ref_ptr<Download> download)
{
    DownloadDeque::iterator i = LocateDownload(download->GetUrl());

    if (i == downloads.end())
    {
        downloads.push_front(download);
    }
}

ref_ptr<Download> DownloadQueue::GetNextDownload()
{
    if (downloads.empty())
    {
        return ref_ptr<Download>();
    }
    else
    {
        return downloads.back();
    }
}


ref_ptr<Download> DownloadQueue::GetDownloadByUrl(string url)
{
    DownloadDeque::iterator i = LocateDownload(url);

    if (i != downloads.end())
    {
        return *LocateDownload(url);
    }
    else
    {
        return ref_ptr<Download>();
    }
}

void DownloadQueue::RemoveDownload(ref_ptr<Download> download)
{
    DownloadDeque::iterator i = LocateDownload(download->GetUrl());

    if (i != downloads.end())
    {
        downloads.erase(i);
    }
}

DownloadQueue::DownloadDeque::iterator DownloadQueue::LocateDownload(string url)
{
    DownloadDeque::iterator i;

    for (i = downloads.begin(); i != downloads.end(); i++)
    {
        if ((*i)->GetUrl() == url)
        {
            break;
        }
    }

    return i;
}
