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

#include <kdebug.h>
#include <ksavefile.h>

#include "channelioformat.h"

//
// ChannelIOFormat
//

ChannelIOFormat::ChannelIOFormat( const char *name, int f )
    : fmtName( name ), flags( f )
{
}

bool ChannelIOFormat::canRead( const char *fmt ) const
{
    if ( !(flags & FormatRead) )
	return false;
    if ( fmt && (fmtName != fmt) )
	return false;

    return true;
}

bool ChannelIOFormat::canWrite( const char *fmt ) const
{
    if ( !(flags & FormatWrite) )
	return false;
    if ( fmt && (fmtName != fmt) )
	return false;

    return true;
}

bool ChannelIOFormat::handlesFile( const QString& filename, int rflags) const
{
    if ( (flags & rflags) != rflags )
	return false;
    return filename.endsWith(fmtName);
}

bool ChannelIOFormat::load( ChannelStore *store, const QString &filename, const char *fmt )
{
    QFile file( filename );
    file.open( IO_ReadWrite );

    if ( !load( store, &file, fmt ) ) {
	kdDebug() << "ChannelIOFormat::load(...) sub-class load method failed" << endl;
	return false;
    }

    kdDebug() << "ChannelIOFormat::load(...) sub-class load ok" << endl;
    file.close();
    return true;
}

bool ChannelIOFormat::save( ChannelStore *store, const QString &filename, const char *fmt )
{
#if 1

    QFile file( filename );
    file.open( IO_WriteOnly );

    if ( !save( store, &file, fmt ) ) {
	kdDebug() << "ChannelIOFormat::save(...) sub-class save method failed" << endl;
	return false;
    }

    kdDebug() << "ChannelIOFormat::save(...) sub-class save ok" << endl;
    file.close();
    return true;

#else // maybe KSaveFile is broken?

    KSaveFile file( filename );
    if ( file.status() ) {
	kdDebug() << "ChannelIOFormat::save(...) Could not create KSaveFile" << endl;
	return false;
    }

    if ( !save( store, &file, fmt ) ) {
	kdDebug() << "ChannelIOFormat::save(...) sub-class save method failed" << endl;
	return false;
    }

    if ( file.status() ) {
	kdDebug() << "ChannelIOFormat::save(...) KSaveFile status is error" << endl;
	return false;
    }

    file.close();

    kdDebug() << "ChannelIOFormat::save(...) success" << endl;
    return true;

#endif
}

ChannelFileMetaInfo ChannelIOFormat::getMetaInfo( const QString &filename, const char *fmt ) {

    QFile file( filename );
    file.open( IO_ReadOnly );

    ChannelFileMetaInfo x = getMetaInfo( &file, fmt );

    file.close();
    return x;
}


ChannelFileMetaInfo ChannelIOFormat::getMetaInfo( QIODevice* /*file*/, const char* /*fmt*/ ) {
return ChannelFileMetaInfo();
}


bool ChannelIOFormat::load( ChannelStore *, QIODevice *, const char * )
{
    return false;
}

bool ChannelIOFormat::save( ChannelStore *, QIODevice *, const char * ) 
{
    return false;
}


