summaryrefslogtreecommitdiffstats
path: root/krecipes/src/importers/nycgenericimporter.cpp
blob: 4105eeab571a3198bb0272fbea836440510bc232 (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
/***************************************************************************
*   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 "nycgenericimporter.h"

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

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

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

NYCGenericImporter::NYCGenericImporter() : BaseImporter()
{}

void NYCGenericImporter::parseFile( const QString &file )
{
	first = true;

	m_recipe.empty();

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

		if ( !stream.atEnd() && stream.readLine().startsWith( "@@@@@" ) )
			importNYCGeneric( stream );
		else {
			setErrorMsg( i18n( "File does not appear to be a valid NYC export." ) );
			return ;
		}
	}
	else
		setErrorMsg( i18n( "Unable to open file." ) );
}

NYCGenericImporter::~NYCGenericImporter()
{}

void NYCGenericImporter::importNYCGeneric( QTextStream &stream )
{
	kapp->processEvents(); //don't want the user to think its frozen... especially for files with thousands of recipes

	QString current;

	stream.skipWhiteSpace();

	//title
	while ( !( current = stream.readLine() ).isEmpty() && !stream.atEnd() )
		m_recipe.title = current;

	//categories
	while ( !( current = stream.readLine() ).isEmpty() && !stream.atEnd() ) {
		if ( current[ 0 ].isNumber() ) {
			loadIngredientLine( current );
			break;
		} //oops, this is really an ingredient line (there was no category line)

		QStringList categories = QStringList::split( ',', current );

		if ( categories.count() > 0 && categories[ 0 ].upper() == "none" )  //there are no categories
			break;

		for ( QStringList::const_iterator it = categories.begin(); it != categories.end(); ++it ) {
			Element new_cat( QString( *it ).stripWhiteSpace() );
			kdDebug() << "Found category: " << new_cat.name << endl;
			m_recipe.categoryList.append( new_cat );
		}
	}

	//ingredients
	while ( !( current = stream.readLine() ).isEmpty() && !stream.atEnd() )
		loadIngredientLine( current );

	//everything else is the instructions with optional "contributor", "prep time" and "yield"
	bool found_next;
	while ( !( found_next = ( current = stream.readLine() ).startsWith( "@@@@@" ) ) && !stream.atEnd() ) {
		if ( current.startsWith( "Contributor:" ) ) {
			Element new_author( current.mid( current.find( ':' ) + 1, current.length() ).stripWhiteSpace() );
			kdDebug() << "Found author: " << new_author.name << endl;
			m_recipe.authorList.append( new_author );
		}
		else if ( current.startsWith( "Preparation Time:" ) ) {
			m_recipe.prepTime = QTime::fromString( current.mid( current.find( ':' ), current.length() ) );
		}
		else if ( current.startsWith( "Yield:" ) ) {
			int colon_index = current.find( ':' );
			int amount_type_sep_index = current.find(" ",colon_index+1);

			m_recipe.yield.amount = current.mid( colon_index+2, amount_type_sep_index-colon_index ).toDouble();
			m_recipe.yield.type = current.mid( amount_type_sep_index+3, current.length() );
		}
		else if ( current.startsWith( "NYC Nutrition Analysis (per serving or yield unit):" ) ) {
			//m_recipe.instructions += current + "\n";
		}
		else if ( current.startsWith( "NYC Nutrilink:" ) ) {
			//m_recipe.instructions += current + "\n";
		}
		else if ( !current.stripWhiteSpace().isEmpty() && !current.startsWith("** Exported from Now You're Cooking!") ) {
			m_recipe.instructions += current + "\n";
		}
	}

	m_recipe.instructions = m_recipe.instructions.stripWhiteSpace();
	putDataInRecipe();

	if ( found_next )
		importNYCGeneric( stream );
}

void NYCGenericImporter::putDataInRecipe()
{
	//put it in the recipe list
	add( m_recipe );

	//reset for the next recipe
	m_recipe.empty();
}

void NYCGenericImporter::loadIngredientLine( const QString &line )
{
	QString current = line;

	if ( current.contains( "-----" ) ) {
		current_header = current.stripWhiteSpace();
		kdDebug() << "Found ingredient header: " << current_header << endl;
		return ;
	}

	MixedNumber amount( 0, 0, 1 );
	QString unit;
	QString name;
	QString prep;

	QStringList ingredient_line = QStringList::split( ' ', current );

	bool found_amount = false;

	if ( !ingredient_line.empty() )  //probably an unnecessary check... but to be safe
	{
		bool ok;
		MixedNumber test_amount = MixedNumber::fromString( ingredient_line[ 0 ], &ok );
		if ( ok )
		{
			amount = amount + test_amount;
			ingredient_line.pop_front();
			found_amount = true;
		}
	}
	if ( !ingredient_line.empty() )  //probably an unnecessary check... but to be safe
	{
		bool ok;
		MixedNumber test_amount = MixedNumber::fromString( ingredient_line[ 0 ], &ok );
		if ( ok )
		{
			amount = amount + test_amount;
			ingredient_line.pop_front();
			found_amount = true;
		}
	}

	if ( found_amount ) {
		unit = ingredient_line[ 0 ];
		ingredient_line.pop_front();
	}

	//now join each separate part of ingredient (name, unit, amount)
	name = ingredient_line.join( " " );

	int prep_sep_index = name.find( QRegExp( "(--|,;;)" ) );
	if ( prep_sep_index == -1 )
		prep_sep_index = name.length();

	name = name.left( prep_sep_index ).stripWhiteSpace();
	prep = name.mid( prep_sep_index+1, name.length() ).stripWhiteSpace();

	Ingredient new_ingredient( name, amount.toDouble(), Unit( unit, amount.toDouble() ) );
	new_ingredient.group = current_header;
	new_ingredient.prepMethodList = ElementList::split(",",prep);
	m_recipe.ingList.append( new_ingredient );

}