// -*- c++ -*-

/*
 *
 * Copyright (C) 2002 Richard Moore <rich@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 CHANNELIOFORMAT_H
#define CHANNELIOFORMAT_H

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

#include <pluginfactory.h>

class QIODevice;
class KSaveFile;
class ChannelStore;
class QtVision;

class ChannelIOFormatPrivate;
class ChannelFileMetaInfoPrivate;

class ChannelFileMetaInfo {
	public:
		const QString& name() const { return _name; }
		const QString& norm() const { return _norm; }
		void setName(QString x) { _name = x; }
		void setNorm(QString x) { _norm = x; }

	private:
		QString _name, _norm;
		ChannelFileMetaInfoPrivate *d;
};


/**
 * Base-class of all channel format implementations.
 *
 * @author Richard Moore, <rich@kde.org>
 */
class ChannelIOFormat
{
    friend class PluginFactory;
public:
    /**
     * Flags indicating the level of support provided by the handler.
     */
    enum Flags {
	FormatRead = 0x1,
	FormatWrite = 0x2,
	FormatReadWrite = FormatRead | FormatWrite
    };

    ChannelIOFormat( const char *name, int flags = FormatReadWrite );
    virtual ~ChannelIOFormat() {}

    /**
     * The name of the format.
     */
    const char *name() const { return fmtName.data(); }

    /**
     * Returns true iff this object can read the specified format. If
     * no format is specified, then it is assumed that the user wants
     * to know about the one returned by @ref name() .
     */
    bool canRead( const char *fmt=0 ) const;

    /**
     * Returns true iff this object can write the specified format.
     * @see canRead() .
     */
    bool canWrite( const char *fmt=0 ) const;

    /**
     * Returns true if this object can handle the specified file.
     * This function should do a quick test about the file, so the proper
     * format could be easily selected.
     * The default implementation only checks, if a filename ends with the
     * name of the format. If thats not appropriate, then you should override
     * this method.
     * @p rflags the requested flags
     */
    virtual bool handlesFile( const QString& filename, int rflags) const;

    /**
     * Load the specified file into the specified store. Returns true
     * on success, false otherwise.
     */
    bool load( ChannelStore *store, const QString &filename, const char *fmt);

    /**
     * Save the specified store into the specified file. Returns true
     * on success, false otherwise.
     */
    bool save( ChannelStore *store, const QString &filename, const char *fmt);

    /**
     * Load the contents of the specified QIODevice into the specified store. 
     * Returns true on success, false otherwise.
     */
    virtual bool load( ChannelStore *store, QIODevice *file, const char *fmt );

    /**
     * Save the contents of the specified store into the specified QIODevice. 
     * Returns true on success, false otherwise.
     */
    virtual bool save( ChannelStore *store, QIODevice *file, const char *fmt );

    /**
     * Load the metainfo for the given file
     */
    ChannelFileMetaInfo getMetaInfo( const QString &filename, const char *fmt );
    virtual ChannelFileMetaInfo getMetaInfo( QIODevice *file, const char *fmt );

protected:
    PluginDesc _description;
    ChannelFileMetaInfo metaInfo;
    QtVision *_qtv;

private:
    QCString fmtName;
    int flags;
    ChannelIOFormatPrivate *d;
};

typedef QPtrList<ChannelIOFormat> ChannelIOFormatList;


#endif // CHANNELIOFORMAT_H
