/**********************************************************************
** Copyright (C) 2000-2001 Trolltech AS.  All rights reserved.
**
** This file is part of Qt Linguist.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.trolltech.com/gpl/ for GPL licensing information.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/

#include <qregexp.h>
#include <qstring.h>
#include <qtranslator.h>

#include <metatranslator.h>

typedef QValueList<QTranslatorMessage> TML;

/*
  This utility converts unstripped message files (.qm files) into source message
  files (.ts files).  The .qm files to convert are expected as command-line
  arguments.
*/

int main( int argc, char **argv )
{
    if ( argc < 2 ) {
	qWarning( "Usage: qm2ts file.qm..." );
	return 1;
    }

    for ( int i = 1; i < argc; i++ ) {
	QTranslator tor( 0 );
	if ( tor.load(argv[i], ".") ) {
	    MetaTranslator metator;
	    TML all = tor.messages();
	    TML::Iterator it;
	    for ( it = all.begin(); it != all.end(); ++it ) {
		metator.insert( MetaTranslatorMessage((*it).context(),
				(*it).sourceText(), (*it).comment(),
				(*it).translation(), FALSE,
				MetaTranslatorMessage::Finished) );
	    }

	    QString g = argv[i];
	    g.replace( QRegExp(QString(".qm$")), QString::null );
	    g += QString( ".ts" );
	    if ( !metator.save(g) )
		qWarning( "qm2ts warning: For some reason, I cannot save '%s'",
			  g.latin1() );
	} else {
	    qWarning( "qm2ts warning: For some reason, I cannot load '%s'",
		      argv[i] );
	}
    }
}
