summaryrefslogtreecommitdiffstats
path: root/krecipes/src/importers/recipemlimporter.cpp
blob: d32e3524b6f2cc10f84561a6f331c40ce175a791 (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/***************************************************************************
*   Copyright (C) 2003 by                                                 *
*   Richard Lärkäng                                                       *
*                                                                         *
*   Copyright (C) 2003-2005 by                                            *
*   Jason Kivlighn (jkivlighn@gmail.com)                                  *
*                                                                         *
*   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 "recipemlimporter.h"

#include <qfile.h>
#include <qdatetime.h>

#include <klocale.h>
#include <kdebug.h>

#include "datablocks/recipe.h"
#include "datablocks/mixednumber.h"

RecipeMLImporter::RecipeMLImporter() : BaseImporter()
{}

void RecipeMLImporter::parseFile( const QString& file )
{
	QFile input( file );
	if ( input.open( IO_ReadOnly ) ) {
		QDomDocument doc;
		QString error;
		int line;
		int column;
		if ( !doc.setContent( &input, &error, &line, &column ) ) {
			setErrorMsg( QString( i18n( "\"%1\" at line %2, column %3.  This may not be a RecipeML file." ) ).arg( error ).arg( line ).arg( column ) );
			return ;
		}

		QDomElement recipeml = doc.documentElement();

		if ( recipeml.tagName() != "recipeml" ) {
			setErrorMsg( i18n( "This file does not appear to be a valid RecipeML archive." ) );
			return ;
		}

		QDomNodeList l = recipeml.childNodes();

		for ( unsigned i = 0 ; i < l.count(); i++ ) {
			QDomElement el = l.item( i ).toElement();
			QString tagName = el.tagName();

			if ( tagName == "meta" )
				continue;
			else if ( tagName == "recipe" )
				readRecipemlRecipe( el );
			else if ( tagName == "menu" )
				readRecipemlMenu( el );
			else
				kdDebug() << "Unknown tag within <recipeml>: " << tagName << endl;
		}
	}
	else
		setErrorMsg( i18n( "Unable to open file." ) );
}

RecipeMLImporter::~RecipeMLImporter()
{}

void RecipeMLImporter::readRecipemlRecipe( const QDomElement& recipe_element )
{
	recipe.empty();

	QDomNodeList l = recipe_element.childNodes();

	for ( unsigned i = 0; i < l.count(); i++ ) {
		QDomElement el = l.item( i ).toElement();
		QString tagName = el.tagName();

		if ( tagName == "head" )
			readRecipemlHead( el );
		else if ( tagName == "ingredients" )
			readRecipemlIngs( el );
		else if ( tagName == "directions" )
			readRecipemlDirections( el );
	else if ( tagName == "description" ) {} //TODO: what do we do with this?
		else if ( tagName == "equipment" ) {} //TODO: what do we do with this?
		else if ( tagName == "nutrition" ) {} //TODO: what do we do with this?
		else if ( tagName == "diet-exchanges" ) {} //TODO: what do we do with this?
		else
			kdDebug() << "Unknown tag within <recipe>: " << el.tagName() << endl;
	}

	add
		( recipe );
}

void RecipeMLImporter::readRecipemlHead( const QDomElement& head )
{
	QDomNodeList l = head.childNodes();
	for ( unsigned i = 0 ; i < l.count(); i++ ) {
		QDomElement el = l.item( i ).toElement();
		QString tagName = el.tagName();

		if ( tagName == "title" )
			recipe.title = el.text().stripWhiteSpace();
		else if ( tagName == "subtitle" )
			recipe.title += ": " + el.text().stripWhiteSpace();
	else if ( tagName == "version" ) {} //TODO: what do we do with this?
		else if ( tagName == "source" )
			readRecipemlSrcItems( el );
		else if ( tagName == "categories" ) {
			QDomNodeList categories = el.childNodes();
			for ( unsigned j = 0; j < categories.count(); j++ ) {
				QDomElement c = categories.item( j ).toElement();
				if ( c.tagName() == "cat" ) {
					recipe.categoryList.append( Element( c.text() ) );
				}
			}
		}
		else if ( tagName == "description" )
			recipe.instructions += "\n\nDescription: " + el.text().stripWhiteSpace();
		else if ( tagName == "preptime" )
			readRecipemlPreptime( el );
		else if ( tagName == "yield" ) {
			QDomNodeList yieldChildren = el.childNodes();
			for ( unsigned j = 0; j < yieldChildren.count(); j++ ) {
				QDomElement y = yieldChildren.item( j ).toElement();
				QString tagName = y.tagName();
				if ( tagName == "range" )
					readRecipemlRange( y, recipe.yield.amount, recipe.yield.amount_offset );
				else if ( tagName == "unit" )
					recipe.yield.type = y.text();
				else
					kdDebug() << "Unknown tag within <yield>: " << y.tagName() << endl;
			}
		}
		else
			kdDebug() << "Unknown tag within <head>: " << el.tagName() << endl;
	}
}

void RecipeMLImporter::readRecipemlIngs( const QDomElement& ings )
{
	QDomNodeList l = ings.childNodes();
	for ( unsigned i = 0 ; i < l.count(); i++ ) {
		QDomElement el = l.item( i ).toElement();
		QString tagName = el.tagName();

		if ( tagName == "ing" )
			readRecipemlIng( el );
		else if ( tagName == "ing-div" )  //NOTE: this can have the "type" attribute
		{
			QString header;
			QDomNodeList ingDiv = el.childNodes();
			for ( unsigned j = 0; j < ingDiv.count(); j++ )
			{
				QDomElement cEl = ingDiv.item( j ).toElement();
				if ( cEl.tagName() == "title" )
					header = cEl.text().stripWhiteSpace();
				else if ( cEl.tagName() == "description" ) {} //TODO: what do we do with this?
				else if ( cEl.tagName() == "ing" )
					readRecipemlIng( cEl, 0, header );
				else if ( tagName == "note" ) {} //TODO: what do we do with this?
				else
					kdDebug() << "Unknown tag within <ing-div>: " << cEl.tagName() << endl;
			}
		}
	else if ( tagName == "note" ) {} //TODO: what do we do with this?
		else
			kdDebug() << "Unknown tag within <ingredients>: " << el.tagName() << endl;
	}
}

void RecipeMLImporter::readRecipemlIng( const QDomElement& ing, Ingredient *ing_parent, const QString &header )
{
	Ingredient new_ing;

	QDomNodeList ingChilds = ing.childNodes();

	QString name, unit, size, prep_method;
	Ingredient quantity;
	quantity.amount = 1;// default quantity assumed by RecipeML DTD

	for ( unsigned j = 0; j < ingChilds.count(); j++ ) {
		QDomElement ingChild = ingChilds.item( j ).toElement();
		QString tagName = ingChild.tagName();

		if ( tagName == "amt" ) {
			QDomNodeList amtChilds = ingChild.childNodes();

			for ( unsigned k = 0; k < amtChilds.count(); k++ ) {
				QDomElement amtChild = amtChilds.item( k ).toElement();

				if ( amtChild.tagName() == "qty" )
					readRecipemlQty( amtChild, quantity );
				else if ( amtChild.tagName() == "size" )
					size = amtChild.text().stripWhiteSpace();
				else if ( amtChild.tagName() == "unit" )
					unit = amtChild.text().stripWhiteSpace();
				else
					kdDebug() << "Unknown tag within <amt>: " << amtChild.tagName() << endl;
			}
		}
		else if ( tagName == "item" ) {
			name = ingChild.text().stripWhiteSpace();
			if ( ing.attribute( "optional", "no" ) == "yes" )
				prep_method = "(optional)";
		}
		else if ( tagName == "prep" ) { //FIXME: this overwrite the optional attribute
			prep_method = ingChild.text().stripWhiteSpace();
		}
		else if ( tagName == "alt-ing" )
			readRecipemlIng( ingChild, &new_ing, header );
		else
			kdDebug() << "Unknown tag within <ing>: " << ingChild.tagName() << endl;
	}

	if ( !size.isEmpty() )
		unit.prepend( size + " " );

	new_ing.name = name;
	new_ing.units = Unit( unit, quantity.amount+quantity.amount_offset );
	new_ing.amount = quantity.amount;
	new_ing.amount_offset = quantity.amount_offset;
	new_ing.group = header;
	new_ing.prepMethodList = ElementList::split(",",prep_method);

	if ( !ing_parent )
		recipe.ingList.append(new_ing);
	else
		ing_parent->substitutes.append( new_ing );
}

void RecipeMLImporter::readRecipemlDirections( const QDomElement& dirs )
{
	QDomNodeList l = dirs.childNodes();

	QStringList directions;

	for ( unsigned i = 0 ; i < l.count(); i++ ) {
		QDomElement el = l.item( i ).toElement();

		if ( el.tagName() == "step" )
			directions.append( el.text().stripWhiteSpace() );
		else
			kdDebug() << "Unknown tag within <directions>: " << el.tagName() << endl;
	}

	QString directionsText;

	if ( directions.count() > 1 ) {
		for ( unsigned i = 1; i <= directions.count(); i++ ) {
			if ( i != 1 ) {
				directionsText += "\n\n";
			}

			QString sWith = QString( "%1. " ).arg( i );
			QString text = directions[ i - 1 ];
			if ( !text.stripWhiteSpace().startsWith( sWith ) )
				directionsText += sWith;
			directionsText += text;
		}
	}
	else
		directionsText = directions[ 0 ];

	recipe.instructions = directionsText;
}

void RecipeMLImporter::readRecipemlMenu( const QDomElement& menu_el )
{
	QDomNodeList l = menu_el.childNodes();
	for ( unsigned i = 0 ; i < l.count(); i++ ) {
		QDomElement el = l.item( i ).toElement();
		QString tagName = el.tagName();

		if ( tagName == "head" )
			readRecipemlHead( el );
	else if ( tagName == "description" ) {} //TODO: what do we do with this?
		else if ( tagName == "recipe" )
			readRecipemlRecipe( el );
		else
			kdDebug() << "Unknown tag within <menu>: " << tagName << endl;
	}
}

void RecipeMLImporter::readRecipemlSrcItems( const QDomElement& sources )
{
	QDomNodeList l = sources.childNodes();
	for ( unsigned i = 0 ; i < l.count(); i++ ) {
		QDomElement srcitem = l.item( i ).toElement();
		QString tagName = srcitem.tagName();

		if ( tagName == "srcitem" )
			recipe.authorList.append( Element( srcitem.text().stripWhiteSpace() ) );
		else
			kdDebug() << "Unknown tag within <source>: " << tagName << endl;
	}
}

void RecipeMLImporter::readRecipemlPreptime( const QDomElement &preptime )
{
	QDomNodeList l = preptime.childNodes();
	for ( unsigned i = 0 ; i < l.count(); i++ ) {
		QDomElement el = l.item( i ).toElement();
		QString tagName = el.tagName();

		if ( tagName == "time" ) {
			int qty = 0;
			QString timeunit;

			QDomNodeList time_l = el.childNodes();
			for ( unsigned i = 0 ; i < time_l.count(); i++ ) {
				QDomElement time_el = time_l.item( i ).toElement();
				QString time_tagName = time_el.tagName();

				if ( time_tagName == "qty" )
					qty = time_el.text().toInt();
				else if ( time_tagName == "timeunit" )
					timeunit = time_el.text();
				else
					kdDebug() << "Unknown tag within <time>: " << time_tagName << endl;
			}

			int minutes = 0;
			int hours = 0;
			if ( timeunit == "minutes" )
				minutes = qty;
			else if ( timeunit == "hours" )
				hours = qty;
			else
				kdDebug() << "Unknown timeunit: " << timeunit << endl;

			recipe.prepTime = QTime( hours + minutes / 60, minutes % 60 );
		}
		else
			kdDebug() << "Unknown tag within <preptime>: " << tagName << endl;
	}
}

void RecipeMLImporter::readRecipemlQty( const QDomElement &qty, Ingredient &ing )
{
	QDomNodeList qtyChilds = qty.childNodes();

	for ( unsigned i = 0; i < qtyChilds.count(); i++ ) {
		QDomElement qtyChild = qtyChilds.item( i ).toElement();
		QString tagName = qtyChild.tagName();
		if ( tagName == "range" )
			readRecipemlRange( qtyChild, ing.amount, ing.amount_offset );
		else if ( tagName.isEmpty() )
			ing.amount = MixedNumber::fromString( qty.text() ).toDouble();
		else
			kdDebug() << "Unknown tag within <qty>: " << tagName << endl;
	}
}

void RecipeMLImporter::readRecipemlRange( const QDomElement& range, double &amount, double &amount_offset )
{
	QDomNodeList rangeChilds = range.childNodes();
	double q1 = 1, q2 = 0; // default quantity assumed by RecipeML DTD
	for ( unsigned j = 0; j < rangeChilds.count(); j++ ) {
		QDomElement rangeChild = rangeChilds.item( j ).toElement();
		QString subTagName = rangeChild.tagName();
		if ( subTagName == "q1" )
			q1 = MixedNumber::fromString( rangeChild.text() ).toDouble();
		else if ( subTagName == "q2" )
			q2 = MixedNumber::fromString( rangeChild.text() ).toDouble();
		else
			kdDebug() << "Unknown tag within <range>: " << subTagName << endl;
	}

	amount = q1;
	amount_offset = q2-q1;
}