summaryrefslogtreecommitdiffstats
path: root/src/backends
diff options
context:
space:
mode:
Diffstat (limited to 'src/backends')
-rw-r--r--src/backends/CMakeLists.txt19
-rw-r--r--src/backends/PostgreSQL/CMakeLists.txt1
-rw-r--r--src/backends/krecipesdbiface.h26
-rw-r--r--src/backends/propertycalculator.cpp131
-rw-r--r--src/backends/propertycalculator.h24
5 files changed, 196 insertions, 5 deletions
diff --git a/src/backends/CMakeLists.txt b/src/backends/CMakeLists.txt
index 42ba0e8..2601e9d 100644
--- a/src/backends/CMakeLists.txt
+++ b/src/backends/CMakeLists.txt
@@ -1,6 +1,7 @@
include_directories(
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_BINARY_DIR}
+ ${CMAKE_SOURCE_DIR}/src
)
@@ -16,16 +17,24 @@ tde_conditional_add_subdirectory( WITH_SQLITE3 SQLite )
# backend libraries list
set( BACKEND_LIBRARIES "" )
if( WITH_MYSQL )
- list( APPEND BACKEND_LIBRARIES "krecmysql-static" )
+ list( APPEND BACKEND_LIBRARIES krecmysql-static )
endif( )
if( WITH_POSTGRESQL )
- list( APPEND BACKEND_LIBRARIES "krecpsql-static" )
+ list( APPEND BACKEND_LIBRARIES krecpsql-static )
endif( )
if( WITH_SQLITE3 )
- list( APPEND BACKEND_LIBRARIES "krecsqlite-static" )
+ list( APPEND BACKEND_LIBRARIES krecsqlite-static )
endif( )
tde_add_library( krecipesdbs STATIC_PIC AUTOMOC
- SOURCES recipedb.cpp qsqlrecipedb.cpp progressinterface.cpp
- LINK ${BACKEND_LIBRARIES}
+ SOURCES
+ recipedb.cpp qsqlrecipedb.cpp progressinterface.cpp propertycalculator.cpp
+ krecipesdbiface.skel
+ LINK
+ datablocks-static krecipesimporters-static ${BACKEND_LIBRARIES}
+ tdeio-shared
)
+
+# Increase the number of times the linker scan for circular dependencies
+# in static libraries (required for building tests)
+set_target_properties( krecipesdbs-static PROPERTIES LINK_INTERFACE_MULTIPLICITY 3 )
diff --git a/src/backends/PostgreSQL/CMakeLists.txt b/src/backends/PostgreSQL/CMakeLists.txt
index fa825e0..84f23f7 100644
--- a/src/backends/PostgreSQL/CMakeLists.txt
+++ b/src/backends/PostgreSQL/CMakeLists.txt
@@ -7,6 +7,7 @@ link_directories(
${LIBPQ_LIBRARY_DIRS}
)
+
##### krecpsql (static)
tde_add_library( krecpsql STATIC_PIC AUTOMOC
diff --git a/src/backends/krecipesdbiface.h b/src/backends/krecipesdbiface.h
new file mode 100644
index 0000000..f5447dc
--- /dev/null
+++ b/src/backends/krecipesdbiface.h
@@ -0,0 +1,26 @@
+/***************************************************************************
+* Copyright (C) 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. *
+***************************************************************************/
+
+#ifndef _KRECIPESDBIFACE_H_
+#define _KRECIPESDBIFACE_H_
+
+#include <dcopobject.h>
+
+class KrecipesDBIface : virtual public DCOPObject
+{
+ K_DCOP
+public:
+
+k_dcop:
+ virtual void emptyData() = 0;
+ virtual bool backup( const TQString &filename ) = 0;
+};
+
+#endif // _KRECIPESIFACE_H_
diff --git a/src/backends/propertycalculator.cpp b/src/backends/propertycalculator.cpp
new file mode 100644
index 0000000..8310b14
--- /dev/null
+++ b/src/backends/propertycalculator.cpp
@@ -0,0 +1,131 @@
+/***************************************************************************
+* Copyright (C) 2003 by Unai Garro *
+* ugarro@users.sourceforge.net *
+* *
+* 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 "propertycalculator.h"
+
+#include <math.h> // For fabs()
+
+#include <kdebug.h>
+
+#include "backends/recipedb.h"
+#include "datablocks/elementlist.h"
+#include "datablocks/ingredientpropertylist.h"
+#include "datablocks/recipe.h"
+
+bool autoConvert( RecipeDB* database, const Ingredient &from, const Ingredient &to, Ingredient &result )
+{
+ RecipeDB::ConversionStatus status = database->convertIngredientUnits( from, to.units, result );
+ bool converted = status == RecipeDB::Success || status == RecipeDB::MismatchedPrepMethodUsingApprox;
+
+ if ( converted ) // There is a ratio
+ {
+ double ratio = result.amount / from.amount;
+
+ if ( ratio > 1 ) // Convert to unit 1, since unit1 is bigger
+ {
+ result.units = from.units;
+ result.amount = from.amount + to.amount / ratio;
+ }
+ else { //Convert to unit2, since unit2 is bigger (just add, units are now correct)
+ result.amount += to.amount;
+ }
+ return true;
+ }
+ else
+ return false;
+}
+
+/*
+** Version with database I/O. DB must be provided
+*/
+
+void calculateProperties( Recipe& recipe, RecipeDB* database )
+{
+ recipe.properties.clear();
+ // Note that recipePropertyList is not attached to any ingredient. It's just the total of the recipe
+ IngredientPropertyList ingredientPropertyList; // property list for each ingredient
+
+ int ingredientNo = 1;
+
+ for ( IngredientList::const_iterator ing_it = recipe.ingList.begin(); ing_it != recipe.ingList.end(); ++ing_it ) {
+ database->loadProperties( &ingredientPropertyList, ( *ing_it ).ingredientID );
+ ingredientPropertyList.divide( recipe.yield.amount ); // calculates properties per yield unit
+ addPropertyToList( database, &recipe.properties, ingredientPropertyList, *ing_it, ingredientNo );
+ ingredientNo++;
+ }
+}
+
+
+void addPropertyToList( RecipeDB *database, IngredientPropertyList *recipePropertyList, IngredientPropertyList &ingPropertyList, const Ingredient &ing, int ingredientNo )
+{
+ TQMap<int,double> ratioCache; //unit->ratio
+
+ IngredientPropertyList::const_iterator prop_it;
+ for ( prop_it = ingPropertyList.begin(); prop_it != ingPropertyList.end(); ++prop_it ) {
+ // Find if property was listed before
+ int pos = recipePropertyList->findIndex( *prop_it );
+ if ( pos >= 0 ) //Exists. Add to it
+ {
+ IngredientPropertyList::iterator rec_property_it = recipePropertyList->at( pos );
+ Ingredient result;
+
+ bool converted;
+ TQMap<int,double>::const_iterator cache_it = ratioCache.find((*prop_it).perUnit.id);
+ if ( cache_it == ratioCache.end() ) {
+ RecipeDB::ConversionStatus status = database->convertIngredientUnits( ing, (*prop_it).perUnit, result );
+ converted = status == RecipeDB::Success || status == RecipeDB::MismatchedPrepMethodUsingApprox;
+
+ if ( converted )
+ ratioCache.insert((*prop_it).perUnit.id,result.amount / ing.amount);
+ else
+ ratioCache.insert((*prop_it).perUnit.id,-1);
+ }
+ else {
+ result.units = (*prop_it).perUnit;
+ result.amount = ing.amount * (*cache_it);
+ converted = result.amount > 0;
+ }
+
+ if ( converted ) // Could convert units to perUnit
+ (*rec_property_it).amount += ( (*prop_it).amount ) * result.amount;
+ }
+ else // Append new property
+ {
+ IngredientProperty property;
+ property.id = (*prop_it).id;
+ property.name = (*prop_it).name;
+ property.perUnit.id = -1; // It's not per unit, it's total sum of the recipe
+ property.perUnit.name = TQString::null; // "
+ property.units = (*prop_it).units;
+
+ Ingredient result;
+ bool converted;
+ TQMap<int,double>::const_iterator cache_it = ratioCache.find((*prop_it).perUnit.id);
+ if ( cache_it == ratioCache.end() ) {
+ RecipeDB::ConversionStatus status = database->convertIngredientUnits( ing, (*prop_it).perUnit, result );
+ converted = status == RecipeDB::Success || status == RecipeDB::MismatchedPrepMethodUsingApprox;
+ if ( converted )
+ ratioCache.insert((*prop_it).perUnit.id,result.amount / ing.amount);
+ else
+ ratioCache.insert((*prop_it).perUnit.id,-1);
+ }
+ else {
+ result.units = (*prop_it).perUnit;
+ result.amount = ing.amount * (*cache_it);
+ converted = result.amount > 0;
+ }
+
+ if ( converted ) // Could convert units to perUnit
+ {
+ property.amount = ( (*prop_it).amount ) * result.amount;
+ recipePropertyList->append( property );
+ }
+ }
+ }
+}
diff --git a/src/backends/propertycalculator.h b/src/backends/propertycalculator.h
new file mode 100644
index 0000000..3553d52
--- /dev/null
+++ b/src/backends/propertycalculator.h
@@ -0,0 +1,24 @@
+/***************************************************************************
+* Copyright (C) 2003 by Unai Garro *
+* ugarro@users.sourceforge.net *
+* *
+* 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 PROPERTYCALCULATOR_H
+#define PROPERTYCALCULATOR_H
+
+class RecipeDB;
+class Ingredient;
+class IngredientPropertyList;
+class Recipe;
+
+bool autoConvert( RecipeDB* database, const Ingredient &from, const Ingredient &to, Ingredient &result );
+void checkUndefined( IngredientPropertyList *recipePropertyList, IngredientPropertyList &addedPropertyList );
+void calculateProperties( Recipe& recipe, RecipeDB* database );
+void addPropertyToList( RecipeDB *database, IngredientPropertyList *recipePropertyList, IngredientPropertyList &ingPropertyList, const Ingredient &ing, int ingredientNo );
+
+#endif //PROPERTYCALCULATOR_H