bool Model::parseContainer ( KTar& archive, QDateTime& creationDate, QString& name, QString& type ) { if (archive.open(QIODevice::ReadOnly)) { const KArchiveDirectory *d = archive.directory(); if (d) { const KArchiveFile *entry = dynamic_cast<const KArchiveFile*>(d->entry("metadata.xml")); if (entry) { QDomDocument doc; doc.setContent(entry->data()); QDomElement rootElem = doc.documentElement(); QDomElement nameElem = rootElem.firstChildElement("name"); QDomElement typeElem = rootElem.firstChildElement("type"); QDomElement dateElem = rootElem.firstChildElement("creationDate"); if (!nameElem.isNull() && !dateElem.isNull()) { creationDate = QDateTime::fromString(dateElem.text(), Qt::ISODate); name = nameElem.text(); type = typeElem.text(); return true; } else kDebug() << "Elements 0"; } else kDebug() << "Entry invalid"; } else kDebug() << "Directory invalid"; } else kDebug() << "Couldn't open tar"; return false; }
void KreImporter::parseFile( const QString &filename ) { QFile * file = 0; bool unlink = false; kDebug() << "loading file:" << filename ; if ( filename.right( 4 ) == ".kre" ) { file = new QFile( filename ); kDebug() << "file is an archive" ; KTar* kre = new KTar( filename, "application/x-gzip" ); kre->open( QIODevice::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.isEmpty() ) { kDebug() << "error: Archive doesn't contain a valid Krecipes file" ; setErrorMsg( i18n( "Archive does not contain a valid Krecipes file" ) ); return ; } QString tmp_dir = KStandardDirs::locateLocal( "tmp", "" ); dir->copyTo( tmp_dir ); file = new QFile( tmp_dir + name ); kre->close(); unlink = true; //remove file after import } else { file = new QFile( filename ); } if ( file->open( QIODevice::ReadOnly ) ) { kDebug() << "file opened" ; QDomDocument doc; QString error; int line; int column; if ( !doc.setContent( file, &error, &line, &column ) ) { kDebug() << QString( "error: \"%1\" at line %2, column %3" ).arg( error ).arg( line ).arg( column ) ; setErrorMsg( i18n( "\"%1\" at line %2, column %3" , error , line, column ) ); return ; } QDomElement kreml = doc.documentElement(); if ( kreml.tagName() != "krecipes" ) { setErrorMsg( i18n( "This file does not appear to be a *.kreml file" ) ); return ; } // TODO Check if there are changes between versions QString kreVersion = kreml.attribute( "version" ); kDebug() << "KreML version" << kreVersion ; QDomNodeList r = kreml.childNodes(); QDomElement krecipe; for ( int z = 0; z < r.count(); z++ ) { krecipe = r.item( z ).toElement(); QDomNodeList l = krecipe.childNodes(); if ( krecipe.tagName() == "krecipes-recipe" ) { Recipe recipe; for ( int i = 0; i < l.count(); i++ ) { QDomElement el = l.item( i ).toElement(); if ( el.tagName() == "krecipes-description" ) { readDescription( el.childNodes(), &recipe ); } if ( el.tagName() == "krecipes-ingredients" ) { readIngredients( el.childNodes(), &recipe ); } if ( el.tagName() == "krecipes-instructions" ) { recipe.instructions = el.text().trimmed(); } if ( el.tagName() == "krecipes-ratings" ) { readRatings( el.childNodes(), &recipe ); } } add ( recipe ); } else if ( krecipe.tagName() == "krecipes-category-structure" ) { CategoryTree * tree = new CategoryTree; readCategoryStructure( l, tree ); setCategoryStructure( tree ); } } } if ( unlink ) { file->remove (); } }
bool KigPlugin::readInfo( KFileMetaInfo& metainfo, uint /*what*/ ) { KFileMetaInfoGroup metagroup = appendGroup( metainfo, "KigInfo"); QString sfile = metainfo.path(); bool iscompressed = false; QFile f( sfile ); if ( !sfile.endsWith( ".kig", false ) ) { iscompressed = true; QString tempdir = KGlobal::dirs()->saveLocation( "tmp" ); if ( tempdir.isEmpty() ) return false; QString tempname = sfile.section( '/', -1 ); if ( sfile.endsWith( ".kigz", false ) ) { tempname.remove( QRegExp( "\\.[Kk][Ii][Gg][Zz]$" ) ); } else return false; // reading compressed file KTar* ark = new KTar( sfile, "application/x-gzip" ); ark->open( IO_ReadOnly ); const KArchiveDirectory* dir = ark->directory(); QStringList entries = dir->entries(); QStringList kigfiles = entries.grep( QRegExp( "\\.kig$" ) ); if ( kigfiles.count() != 1 ) return false; const KArchiveEntry* kigz = dir->entry( kigfiles[0] ); if ( !kigz->isFile() ) return false; dynamic_cast<const KArchiveFile*>( kigz )->copyTo( tempdir ); f.setName( tempdir + kigz->name() ); } if ( !f.open( IO_ReadOnly ) ) return false; QDomDocument doc( "KigDocument" ); if ( !doc.setContent( &f ) ) return false; f.close(); // removing temp file if ( iscompressed ) f.remove(); QDomElement main = doc.documentElement(); // reading the version... QString version = main.attribute( "Version" ); if ( version.isEmpty() ) version = main.attribute( "version" ); if ( version.isEmpty() ) version = i18n( "Translators: Not Available", "n/a" ); appendItem( metagroup, "Version", version ); // reading the compatibility version... QString compatversion = main.attribute( "CompatibilityVersion" ); if ( compatversion.isEmpty() ) compatversion = i18n( "%1 represents Kig version", "%1 (as the version)" ).arg( version ); appendItem( metagroup, "CompatVersion", compatversion ); // reading the Coordinate System... QCString coordsystem; for ( QDomNode n = main.firstChild(); ! n.isNull(); n = n.nextSibling() ) { QDomElement e = n.toElement(); if ( e.isNull() ) continue; if ( e.tagName() == "CoordinateSystem" ) coordsystem = e.text().latin1(); } appendItem( metagroup, "CoordSystem", coordsystem ); // has Kig document the grid? bool btmp = true; QString stmp = main.attribute( "grid" ); if ( !( stmp.isEmpty() || ( stmp != "0" ) ) ) btmp = ( stmp != "0" ); QString stmp2 = btmp ? i18n( "Yes" ) : i18n( "No" ); appendItem( metagroup, "Grid", stmp2 ); // has Kig document the axes? btmp = true; stmp = main.attribute( "axes" ); if ( !( stmp.isEmpty() || ( stmp != "0" ) ) ) btmp = ( stmp != "0" ); stmp2 = btmp ? i18n( "Yes" ) : i18n( "No" ); appendItem( metagroup, "Axes", stmp2 ); stmp2 = iscompressed ? i18n( "Yes" ) : i18n( "No" ); appendItem( metagroup, "Compressed", stmp2 ); return true; }