summaryrefslogtreecommitdiffstats
path: root/krecipes/src/exporters/cookmlexporter.cpp
blob: 047d586cbd84de6cc3463ab86eced334361e746d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/***************************************************************************
*   Copyright (C) 2003 by                                                 *
*   Jason Kivlighn ([email protected])                                  *
*                                                                         *
*   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 "cookmlexporter.h"

#include <ntqbuffer.h>
#include <ntqdom.h>
#include <ntqimage.h>
#include <ntqpixmap.h>
#include <ntqfile.h>

#include <kconfig.h>
#include <kdebug.h>
#include <klocale.h>
#include <ktempfile.h>
#include <kmdcodec.h>
#include <kglobal.h>
#include <kstandarddirs.h>

#include "backends/recipedb.h"

CookMLExporter::CookMLExporter( const TQString& filename, const TQString &format ) :
		BaseExporter( filename, format )
{}


CookMLExporter::~CookMLExporter()
{}

int CookMLExporter::supportedItems() const
{
	return RecipeDB::All ^ RecipeDB::Ratings;
}

TQString CookMLExporter::createHeader( const RecipeList& )
{
	TQString xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
	xml += "<!DOCTYPE cookml PUBLIC \"-\" \"cookml.dtd\">";
	xml += "<cookml version=\"1.0.13\" prog=\"Krecipes\" progver=\""+krecipes_version()+"\">";
	return xml;
}

TQString CookMLExporter::createFooter()
{
	return "</cookml>";
}

TQString CookMLExporter::createContent( const RecipeList& recipes )
{
	TQString xml;
	TQDomDocument doc;

	RecipeList::const_iterator recipe_it;
	for ( recipe_it = recipes.begin(); recipe_it != recipes.end(); ++recipe_it ) {
		TQDomElement recipe_tag = doc.createElement( "recipe" );
		recipe_tag.setAttribute( "lang", ( KGlobal::locale() ) ->language() );

		//cookml_tag.appendChild( recipe_tag );

		TQDomElement head_tag = doc.createElement( "head" );
		head_tag.setAttribute( "title", ( *recipe_it ).title );
		head_tag.setAttribute( "servingqty", ( *recipe_it ).yield.amount );
		head_tag.setAttribute( "servingtype", ( *recipe_it ).yield.type );
		head_tag.setAttribute( "rid", i18n( "" ) ); //FIXME:what's this...recipe ID??
		recipe_tag.appendChild( head_tag );

		for ( ElementList::const_iterator cat_it = ( *recipe_it ).categoryList.begin(); cat_it != ( *recipe_it ).categoryList.end(); ++cat_it ) {
			TQDomElement cat_tag = doc.createElement( "cat" );
			cat_tag.appendChild( doc.createTextNode( ( *cat_it ).name ) );
			head_tag.appendChild( cat_tag );
		}

		for ( ElementList::const_iterator author_it = ( *recipe_it ).authorList.begin(); author_it != ( *recipe_it ).authorList.end(); ++author_it ) {
			TQDomElement sourceline_tag = doc.createElement( "sourceline" );
			sourceline_tag.appendChild( doc.createTextNode( ( *author_it ).name ) );
			head_tag.appendChild( sourceline_tag );
		}

		TQDomElement picbin_tag = doc.createElement( "picbin" );
		picbin_tag.setAttribute( "format", "JPG" );

		TQByteArray data;
		TQBuffer buffer( data );
		buffer.open( IO_WriteOnly );
		TQImageIO iio( &buffer, "JPEG" );
		iio.setImage( ( *recipe_it ).photo.convertToImage() );
		iio.write();
		//( *recipe_it ).photo.save( &buffer, "JPEG" ); don't need TQImageIO in QT 3.2

		picbin_tag.appendChild( doc.createTextNode( KCodecs::base64Encode( data, true ) ) );
		head_tag.appendChild( picbin_tag );

		TQDomElement part_tag = doc.createElement( "part" );
		for ( IngredientList::const_iterator ing_it = ( *recipe_it ).ingList.begin(); ing_it != ( *recipe_it ).ingList.end(); ++ing_it ) {
			TQDomElement ingredient_tag = doc.createElement( "ingredient" );
			ingredient_tag.setAttribute( "qty", TQString::number( ( *ing_it ).amount ) );
			ingredient_tag.setAttribute( "unit", ( ( *ing_it ).amount > 1 ) ? ( *ing_it ).units.plural : ( *ing_it ).units.name );
			ingredient_tag.setAttribute( "item", ( *ing_it ).name );
			ingredient_tag.setAttribute( "preparation", ( *ing_it ).prepMethodList.join(",") );
			part_tag.appendChild( ingredient_tag );
		}
		recipe_tag.appendChild( part_tag );

		TQDomElement preparation_tag = doc.createElement( "preparation" );
		recipe_tag.appendChild( preparation_tag );

		TQDomElement text_tag = doc.createElement( "text" );
		preparation_tag.appendChild( text_tag );
		text_tag.appendChild( doc.createTextNode( ( *recipe_it ).instructions ) );

		xml += recipe_tag.text();
	}

	return xml;
}