summaryrefslogtreecommitdiffstats
path: root/src/importers/baseimporter.h
blob: 944f50eae34d9338f5e42b5e91a127063ce6dce7 (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
/***************************************************************************
*   Copyright (C) 2003-2005 by                                            *
*   Unai Garro <ugarro@users.sourceforge.net>                             *
*   Cyril Bosselut (bosselut@b1project.com)                               *
*   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.                                   *
***************************************************************************/

#ifndef BASEIMPORTER_H
#define BASEIMPORTER_H

#include <tdelocale.h>

#include <tqstring.h>
#include <tqstringlist.h>

#include "datablocks/recipelist.h"
#include "datablocks/elementlist.h"
#include "datablocks/unitratiolist.h"

class Recipe;
class RecipeDB;
class CategoryTree;
class IngredientData;

class KProgressDialog;

/** @brief Subclass this class to create an importer for a specific file type.
  *
  * Subclasses should take the file name of the file to import in their constructor
  * and then parse the file.  For every recipe found in the file, a Recipe object should
  * be created and added to the importer using the @ref add() function.
  *
  * @author Jason Kivlighn
  */
class BaseImporter
{
public:
	BaseImporter();
	virtual ~BaseImporter();

	TQString getMessages() const
	{
		return m_master_error + m_master_warning;
	}
	TQString getErrorMsg() const
	{
		return m_master_error;
	}
	TQString getWarningMsg() const
	{
		return m_master_warning;
	}

	void parseFiles( const TQStringList &filenames );

	/** Import all the recipes into the given database.  These recipes are the
	  * recipes added to this class by a subclass using the @ref add() method.
	  */
	void import( RecipeDB *db, bool automatic = false );

	RecipeList recipeList() const { return *m_recipe_list; }
	void setRecipeList( const RecipeList &list ) { *m_recipe_list = list; }

	const CategoryTree *categoryStructure() const { return m_cat_structure; }

protected:
	virtual void parseFile( const TQString &filename ) = 0;
	
	void importRecipes( RecipeList &selected_recipes, RecipeDB *db, KProgressDialog *progess_dialog );

	/** Add a recipe to be imported into the database */
	void add( const Recipe &recipe );
	void add( const RecipeList &recipe_list );

	void setCategoryStructure( CategoryTree *cat_structure );
	void setUnitRatioInfo( UnitRatioList &ratioList, UnitList &unitList );

	int totalCount() const
	{
		return m_recipe_list->count();
	}
	int fileRecipeCount() const
	{
		return file_recipe_count;
	}

	void setErrorMsg( const TQString & s )
	{
		m_error_msgs.append( s );
	}
	void addWarningMsg( const TQString & s )
	{
		m_warning_msgs.append( s );
	}

private:
	void importCategoryStructure( RecipeDB *, const CategoryTree * );
	void importUnitRatios( RecipeDB * );
	void importIngredient( IngredientData &ing, RecipeDB *db, KProgressDialog *progress_dialog );

	void processMessages( const TQString &file );

	RecipeList *m_recipe_list;
	CategoryTree *m_cat_structure;
	UnitRatioList m_ratioList;
	UnitList m_unitList;

	TQStringList m_warning_msgs;
	TQStringList m_error_msgs;
	TQString m_master_warning;
	TQString m_master_error;

	int file_recipe_count;
	bool direct;

	RecipeDB *m_database;
	KProgressDialog *m_progress_dialog;
	TQStringList m_filenames;
};

#endif //BASEIMPORTER_H