<commit_before>/***************************************************************************
 *   Copyright (C) 2003 by krecipes.sourceforge.net authors                *
 *                                                                         *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/

#include "kreimporter.h"

#include <klocale.h>

#include <qfile.h>
#include <qstringlist.h>

#include "recipe.h"


KreImporter::KreImporter(const QString& filename)
{
  QFile* file = 0;
  bool unlink = false;
	qDebug("loading file: %s",filename.latin1());

  if(filename.right(6) == ".kreml"){
	  file = new QFile( filename );
  }
  else if(filename.right(4) == ".kre"){
    qDebug("file is an archive");
    KTar* kre = new KTar(filename, "application/x-gzip");
    kre->open( IO_ReadOnly );
    const KArchiveDirectory* dir = kre->directory();
    QString name;
    QStringList fileList = dir->entries();
    for ( QStringList::Iterator it = fileList.begin(); it != fileList.end(); ++it ) {
        if( (*it).right(6) == ".kreml" ){
          name = *it;
        }
    }
    if(name == QString::null){
			qDebug("error: Archive doesn't contain a valid krecipes file");
			setErrorMsg( i18n("error: Archive doesn't contain a valid krecipes file") );
			return;
    }
    dir->copyTo("/tmp/");
	  file = new QFile( "/tmp/"+name );
    kre->close();
    unlink = true; //remove file after import
  }

	if ( file->open( IO_ReadOnly ) )
	{
		qDebug("file opened");
		QDomDocument doc;
		QString error; int line; int column;
		if (!doc.setContent(file,&error,&line,&column))
		{
			qDebug("error: \"%s\" at line %d, column %d",error.latin1(),line,column);
			setErrorMsg( QString( i18n("\"%1\" at line %2, column %3") ).arg(error).arg(line).arg(column) );
			return;
		}

		QDomElement kreml = doc.documentElement();

		if (kreml.tagName() != "krecipes-recipe" /*|| kreml.attribute("source") != "MasterCook 5.0"*/)
		{
			setErrorMsg( i18n("This file doesn't appear to be a *.kreml file") );
			return;
		}

		// TODO Check if there are changes between versions
    QString version = kreml.attribute("version");

		QDomNodeList l = kreml.childNodes();<commit_msg>add kreml version debug output<commit_after>/***************************************************************************
 *   Copyright (C) 2003 by krecipes.sourceforge.net authors                *
 *                                                                         *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/

#include "kreimporter.h"

#include <klocale.h>

#include <qfile.h>
#include <qstringlist.h>

#include "recipe.h"


KreImporter::KreImporter(const QString& filename)
{
  QFile* file = 0;
  bool unlink = false;
	qDebug("loading file: %s",filename.latin1());

  if(filename.right(6) == ".kreml"){
	  file = new QFile( filename );
  }
  else if(filename.right(4) == ".kre"){
    qDebug("file is an archive");
    KTar* kre = new KTar(filename, "application/x-gzip");
    kre->open( IO_ReadOnly );
    const KArchiveDirectory* dir = kre->directory();
    QString name;
    QStringList fileList = dir->entries();
    for ( QStringList::Iterator it = fileList.begin(); it != fileList.end(); ++it ) {
        if( (*it).right(6) == ".kreml" ){
          name = *it;
        }
    }
    if(name == QString::null){
			qDebug("error: Archive doesn't contain a valid krecipes file");
			setErrorMsg( i18n("error: Archive doesn't contain a valid krecipes file") );
			return;
    }
    dir->copyTo("/tmp/");
	  file = new QFile( "/tmp/"+name );
    kre->close();
    unlink = true; //remove file after import
  }

	if ( file->open( IO_ReadOnly ) )
	{
		qDebug("file opened");
		QDomDocument doc;
		QString error; int line; int column;
		if (!doc.setContent(file,&error,&line,&column))
		{
			qDebug("error: \"%s\" at line %d, column %d",error.latin1(),line,column);
			setErrorMsg( QString( i18n("\"%1\" at line %2, column %3") ).arg(error).arg(line).arg(column) );
			return;
		}

		QDomElement kreml = doc.documentElement();

		if (kreml.tagName() != "krecipes-recipe")
		{
			setErrorMsg( i18n("This file doesn't appear to be a *.kreml file") );
			return;
		}

		// TODO Check if there are changes between versions
    QString kreVersion = kreml.attribute("version");
    qDebug(QString( i18n("KreML version %1") ).arg(kreVersion));

		QDomNodeList l = kreml.childNodes();
{'fraction_non_alphanumeric': 0.19593810444874274, 'fraction_numerical': 0.008704061895551257, 'mean_word_length': 2.657001414427157, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2736402', 'n_tokens_mistral': 1675, 'n_tokens_neox': 1554, 'n_words': 423}