/*
 *
 * Copyright (C) 2002 George Staikos <staikos@kde.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */


#ifndef _CHANNELSTORE_H
#define _CHANNELSTORE_H

#include <qstring.h>
#include <qptrlist.h>
#include <qobject.h>

#include "channelstoreiface.h"

class Channel;
class QtVision;

template<class T>
class ChannelPtrList : public QPtrList<T> {
protected:
	virtual int compareItems(QPtrCollection::Item item1, QPtrCollection::Item item2) {
	Channel* i1 = static_cast<Channel*>(item1);
	Channel* i2 = static_cast<Channel*>(item2);
        if (i1 && i2)
	if (i1->number() < i2->number()) {
		return -1;
        } else if (i1->number() > i2->number()) {
		return 1;
        }
        return 0;
        }
};

typedef ChannelPtrList<Channel> ChannelList;

/**
 * A suite of channels.
 */
class ChannelStore : public QObject, public virtual QtVisionChannelStoreIface 
{
    Q_OBJECT
public:
    ChannelStore( QtVision *qtv, QObject *parent, const char *name=0);
    virtual ~ChannelStore();

    //ChannelList *channels() { return &_channels; }

    uint count() const { return _channels.count(); }
    bool isEmpty() const { return count() ? false : true; }

    bool load( const QString &filename=QString::null, const char *fmt=0 );
    bool save( const QString &filename=QString::null, const char *fmt=0 );

    const QString& fileName() const { return _file; }

    DCOPRef channelIfaceAt( int idx );

    Channel *addChannel( ulong freq );
    Channel *channelAt( int idx ) { return _channels.at(idx); }
    Channel *channelNumber( int n );

    Channel *channelAfter( Channel *channel );
    Channel *channelBefore( Channel *channel );

public slots:
    void setName(const QString &filename) { _file = filename; save(); }
    bool reload();

    void addChannel( Channel *channel );

    int removeChannel(int idx);
    int removeChannelNumber(int n);
    int removeChannel(Channel *channel);

    /** Adds channels from @p nstore to the end of this ChannelStore. */
    void addChannels( const ChannelStore& nstore);
    void clear();

    void renumber( int start );
    /** Renumbers the channels from 2 */
    void renumber();

signals:
    void channelAdded(Channel *x);
    void channelRemoved(Channel *x);
    void aboutToReload();
    void loaded();
    void saved();

private:
    ChannelList _channels;
    QString _file;
    bool silentModifications;
    QtVision *_qtv;
};


#endif





