// -*- 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.
 */

#include <kdebug.h>

#include "channel.h"
#include "channelstore.h"
#include "channelio.h"

#include "qtvision.h"

#include "pluginfactory.h"

//
// ChannelIO
//

static ChannelIOFormatList *channel_io_formats = 0;

void ChannelIO::registerFormats(QtVision *qtv)
{
QPtrList<PluginDesc>& channelPlugins(qtv->pluginFactory()->channelPlugins());

    for (PluginDesc *plug = channelPlugins.first(); plug;
                             plug = channelPlugins.next()) {
        kdDebug() << "Found a plugin:" << endl;
        kdDebug() << plug->name << endl;
        kdDebug() << plug->author << endl;
        kdDebug() << plug->comment << endl;
        ChannelIOFormat *f = qtv->pluginFactory()->getChannelPlugin(plug);
        if (f)
            registerFormat(qtv, f);
    }
}

ChannelIOFormat* ChannelIO::guessFormat( QtVision *qtv, const QString &filename, int flags )
{
    FormatList *formatList = formats(qtv);

    for ( Format *io = formatList->first(); io; io = formatList->next() ) {
	if (io->handlesFile(filename, flags))
	    return io;
    }
    kdDebug() << "ChannelIO::guessFormat(...) returning csv for format" << endl;
    return findFormat(qtv, "csv", flags);
}

void ChannelIO::registerFormat( QtVision *qtv, ChannelIOFormat *format )
{
    kdDebug() << "ChannelIO::registeringFormat(...) " << format->name() << endl;
    formats(qtv)->append( format );
}

ChannelIOFormatList *ChannelIO::formats(QtVision* qtv)
{
    if ( !channel_io_formats ) {
	kdDebug() << "ChannelIO::formats() Creating format list" << endl;

	channel_io_formats = new FormatList;
	channel_io_formats->setAutoDelete( true );
	registerFormats(qtv);
    }
    return channel_io_formats;
}

ChannelIOFormat *ChannelIO::findFormat( QtVision *qtv, const char *fmt, int flags )
{
    kdDebug() << "ChannelIO::findFormat(...) " << fmt << ", flags=" << flags << endl;

    //
    // Search for a matching handler
    //
    FormatList *formatList = formats(qtv);

    for ( Format *io = formatList->first(); io; io = formatList->next() ) {
	if ( flags & Format::FormatRead ) {
	    if ( io->canRead(fmt) ) {
		kdDebug() << "ChannelIO::findFormat(...) io " << io->name() << " can read " << fmt << endl;
		return io;
	    }
	}
	if ( flags & Format::FormatWrite ) {
	    if ( io->canWrite(fmt) ) {
		kdDebug() << "ChannelIO::findFormat(...) io " << io->name() << " can write " << fmt << endl;
		return io;
	    }
	}
    }

    kdDebug() << "ChannelIO::findFormat(...) failed for " << fmt << endl;
    return 0;
}

bool ChannelIO::load( QtVision *qtv, ChannelStore *store, const QString &filename, const char *fmt )
{
    kdDebug() << "ChannelIO::load(...) file='" << filename << "' format='" << fmt << "'" << endl;

    Format *io = (fmt ? findFormat( qtv, fmt, Format::FormatRead )
	    : guessFormat(qtv, filename, Format::FormatRead));
    if ( !io )
	return false;

    kdDebug() << "ChannelIO::load(...) using format '" << io->name() << "'" << endl;
    return io->load( store, filename, fmt );
}

bool ChannelIO::save( QtVision *qtv, ChannelStore *store, const QString &filename, const char *fmt )
{
    kdDebug() << "ChannelIO::save(...) file='" << filename << "' format='" << fmt << "'" << endl;

    Format *io = (fmt ? findFormat( qtv, fmt, Format::FormatWrite )
	    : guessFormat(qtv, filename, Format::FormatWrite));
    if ( !io )
	return false;

    kdDebug() << "ChannelIO::save(...) using format '" << io->name() << "'" << endl;
    return io->save( store, filename, fmt );
}


ChannelFileMetaInfo ChannelIO::getMetaInfo( QtVision *qtv, const QString& filename, const char *fmt ) {
    ChannelFileMetaInfo x;
    kdDebug() << "Getting meta info for file " << filename << endl;
    Format *io = (fmt ? findFormat( qtv, fmt, Format::FormatWrite )
	    : guessFormat(qtv, filename, Format::FormatWrite));
    if ( !io )
	return x;

    x = io->getMetaInfo(filename, fmt);

    return x;
}



