summaryrefslogtreecommitdiffstats
path: root/krecipes/src/importers/rezkonvimporter.cpp
blob: 2f7f4289746251fa659099f0f693dcd447b28e50 (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
/***************************************************************************
*   Copyright (C) 2003 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 "rezkonvimporter.h"

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

#include <qfile.h>
#include <qregexp.h>
#include <qtextstream.h>

#include "datablocks/mixednumber.h"

RezkonvImporter::RezkonvImporter() : BaseImporter()
{}

RezkonvImporter::~RezkonvImporter()
{}

void RezkonvImporter::parseFile( const QString &filename )
{
	QFile input( filename );

	if ( input.open( IO_ReadOnly ) ) {
		QTextStream stream( &input );
		stream.skipWhiteSpace();

		QString line;

		while ( !stream.atEnd() ) {
			line = stream.readLine();

			if ( line.contains( QRegExp( "^=====.*REZKONV.*" ) ) ) {
				QStringList raw_recipe;
				while ( !( line = stream.readLine() ).contains( QRegExp( "^=====\\s*$" ) ) && !stream.atEnd() )
					raw_recipe << line;

				readRecipe( raw_recipe );
			}
		}

		if ( fileRecipeCount() == 0 )
			setErrorMsg( i18n( "No recipes found in this file." ) );
	}
	else
		setErrorMsg( i18n( "Unable to open file." ) );
}

void RezkonvImporter::readRecipe( const QStringList &raw_recipe )
{
	kapp->processEvents(); //don't want the user to think its frozen... especially for files with thousands of recipes

	Recipe recipe;

	QStringList::const_iterator text_it = raw_recipe.begin();
	m_end_it = raw_recipe.end();

	//title (Titel)
	text_it++;
	recipe.title = ( *text_it ).mid( ( *text_it ).find( ":" ) + 1, ( *text_it ).length() ).stripWhiteSpace();
	kdDebug() << "Found title: " << recipe.title << endl;

	//categories (Kategorien):
	text_it++;
	QStringList categories = QStringList::split( ',', ( *text_it ).mid( ( *text_it ).find( ":" ) + 1, ( *text_it ).length() ) );
	for ( QStringList::const_iterator it = categories.begin(); it != categories.end(); ++it ) {
		Element new_cat;
		new_cat.name = QString( *it ).stripWhiteSpace();
		kdDebug() << "Found category: " << new_cat.name << endl;
		recipe.categoryList.append( new_cat );
	}

	//yield (Menge)
	text_it++;
	//get the number between the ":" and the next space after it
	QString yield_str = ( *text_it ).stripWhiteSpace();
	yield_str.remove( QRegExp( "^Menge:\\s*" ) );
	int sep_index = yield_str.find( ' ' );
	if ( sep_index != -1 )
		recipe.yield.type = yield_str.mid( sep_index+1 );
	readRange( yield_str.mid( 0, sep_index ), recipe.yield.amount, recipe.yield.amount_offset );
	kdDebug() << "Found yield: " << recipe.yield.amount << endl;

	bool is_sub = false;
	bool last_line_empty = false;
	text_it++;
	while ( text_it != raw_recipe.end() ) {
		if ( ( *text_it ).isEmpty() ) {
			last_line_empty = true;
			text_it++;
			continue;
		}

		if ( ( *text_it ).contains( QRegExp( "^=====.*=$" ) ) )  //is a header
		{
			if ( ( *text_it ).contains( "quelle", false ) )
			{
				loadReferences( text_it, recipe );
				break; //reference lines are the last before the instructions
			}
			else
				loadIngredientHeader( *text_it, recipe );
		}

		//if it has no more than two spaces followed by a non-digit
		//then we'll assume it is a direction line
		else if ( last_line_empty && ( *text_it ).contains( QRegExp( "^\\s{0,2}[^\\d\\s=]" ) ) )
			break;
		else
			loadIngredient( *text_it, recipe, is_sub );

		last_line_empty = false;
		text_it++;
	}

	loadInstructions( text_it, recipe );

	add
		( recipe );

	current_header = QString::null;
}

void RezkonvImporter::loadIngredient( const QString &string, Recipe &recipe, bool &is_sub )
{
	Ingredient new_ingredient;
	new_ingredient.amount = 0; //amount not required, so give default of 0

	QRegExp cont_test( "^-{1,2}" );
	if ( string.stripWhiteSpace().contains( cont_test ) ) {
		QString name = string.stripWhiteSpace();
		name.remove( cont_test );
		kdDebug() << "Appending to last ingredient: " << name << endl;
		if ( !recipe.ingList.isEmpty() )  //so it doesn't crash when the first ingredient appears to be a continuation of another
			( *recipe.ingList.at( recipe.ingList.count() - 1 ) ).name += " " + name;

		return ;
	}

	//amount
	if ( !string.mid( 0, 7 ).stripWhiteSpace().isEmpty() )
		readRange( string.mid( 0, 7 ), new_ingredient.amount, new_ingredient.amount_offset );

	//unit
	QString unit_str = string.mid( 8, 9 ).stripWhiteSpace();
	new_ingredient.units = Unit( unit_str, new_ingredient.amount );

	//name and preparation method
	new_ingredient.name = string.mid( 18, string.length() - 18 ).stripWhiteSpace();

	//separate out the preparation method
	QString name_and_prep = new_ingredient.name;
	int separator_index = name_and_prep.find( "," );
	if ( separator_index != -1 ) {
		new_ingredient.name = name_and_prep.mid( 0, separator_index ).stripWhiteSpace();
		new_ingredient.prepMethodList = ElementList::split(",",name_and_prep.mid( separator_index + 1, name_and_prep.length() ).stripWhiteSpace() );
	}

	//header (if present)
	new_ingredient.group = current_header;

	bool last_is_sub = is_sub;
	if ( new_ingredient.prepMethodList.last().name == "or" ) {
		new_ingredient.prepMethodList.pop_back();
		is_sub = true;
	}
	else
		is_sub = false;

	if ( last_is_sub )
		( *recipe.ingList.at( recipe.ingList.count() - 1 ) ).substitutes.append(new_ingredient);
	else
		recipe.ingList.append( new_ingredient );
}

void RezkonvImporter::loadIngredientHeader( const QString &string, Recipe &/*recipe*/ )
{
	QString header = string;
	header.remove( QRegExp( "^=*" ) ).remove( QRegExp( "=*$" ) );
	header = header.stripWhiteSpace();

	kdDebug() << "found ingredient header: " << header << endl;

	current_header = header;
}

void RezkonvImporter::loadInstructions( QStringList::const_iterator &text_it, Recipe &recipe )
{
	QString instr;
	QRegExp rx_title( "^:{0,1}\\s*O-Titel\\s*:" );
	QString line;
	while ( text_it != m_end_it ) {
		line = *text_it;

		//titles longer than the line width are rewritten here
		if ( line.contains( rx_title ) ) {
			line.remove( rx_title );
			recipe.title = line.stripWhiteSpace();

			QRegExp rx_line_cont( ":\\s*>{0,1}\\s*:" );
			while ( ( line = *text_it ).contains( rx_line_cont ) ) {
				line.remove( rx_line_cont );
				recipe.title += line;

				text_it++;
			}
			kdDebug() << "Found long title: " << recipe.title << endl;
		}
		else {
			if ( line.isEmpty() )
				instr += "\n\n";

			instr += line;
		}

		text_it++;
	}

	recipe.instructions = instr;
}

void RezkonvImporter::loadReferences( QStringList::const_iterator &text_it, Recipe &recipe )
{
	kdDebug() << "Found source header" << endl;

	while ( text_it != m_end_it ) {
		text_it++;
		QRegExp rx_line_begin( "^\\s*-{0,2}\\s*" );

		QRegExp rx_creation_date = QRegExp( "^\\s*-{0,2}\\s*Erfasst \\*RK\\*", false );
		if ( ( *text_it ).contains( rx_creation_date ) )  // date followed by typist
		{
			QString date = *text_it;
			date.remove( rx_creation_date ).remove( QRegExp( " von\\s*$" ) );

			// Date is given as DD.MM.YY
			QString s = date.section( ".", 0, 0 );
			int day = s.toInt();

			s = date.section( ".", 1, 1 );
			int month = s.toInt();

			s = date.section( ".", 2, 2 );
			int year = s.toInt();
			year += 1900;
			if ( year < 1970 )
				year += 100; //we'll assume nothing has been created before 1970 (y2k issues :p)

			recipe.ctime = QDate(year,month,day);

			#if 0
			//typist
			text_it++;
			QString typist = = *text_it;
			typist.remove( rx_line_begin );
			#endif

		}
		else //everything else is an author
		{
			if ( ( *text_it ).contains( rx_line_begin ) ) {
				QString author = *text_it;
				author.remove( rx_line_begin );

				recipe.authorList.append( Element( author ) );
			}
			else
				break;
		}
	}
}

void RezkonvImporter::readRange( const QString &range_str, double &amount, double &amount_offset )
{
	QString from = range_str.section( '-', 0, 0 );
	QString to   = range_str.section( '-', 1, 1 );

	amount = MixedNumber::fromString( from ).toDouble();

	if ( !to.stripWhiteSpace().isEmpty() )
		amount_offset = MixedNumber::fromString( to ).toDouble() - amount;
}