summaryrefslogtreecommitdiffstats
path: root/krecipes/src/dialogs/recipeviewdialog.cpp
blob: 8918ac0e4b9b886c491a7c3fce395ae050bb4fd6 (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
/***************************************************************************
*   Copyright (C) 2003 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.                                   *
***************************************************************************/

#include "recipeviewdialog.h"

#include <ntqlayout.h>
#include <ntqstyle.h>
#include <ntqfile.h>

#include <tdeapplication.h>
#include <kcursor.h>
#include <kdebug.h>
#include <tdehtmlview.h>
#include <tdehtml_part.h>
#include <tdelocale.h>
#include <tdemainwindow.h>
#include <kprogress.h>
#include <kstandarddirs.h>
#include <kstatusbar.h>
#include <tdeconfig.h>
#include <tdeglobal.h>

#include "datablocks/mixednumber.h"
#include "backends/recipedb.h"
#include "exporters/htmlexporter.h"
#include "recipeprintpreview.h"

RecipeViewDialog::RecipeViewDialog( TQWidget *parent, RecipeDB *db, int recipeID ) : TQVBox( parent ),
	database(db)
{
	// Initialize UI Elements
	recipeView = new TDEHTMLPart( this );

	connect( database, SIGNAL(recipeRemoved(int)), SLOT(recipeRemoved(int)) );

	tmp_filename = locateLocal( "tmp", "krecipes_recipe_view" );

	//----------Load  the recipe --------
	if ( recipeID != -1 )
		loadRecipe( recipeID );
}

RecipeViewDialog::~RecipeViewDialog()
{
	if ( recipe_loaded )
		removeOldFiles();
}

bool RecipeViewDialog::loadRecipe( int recipeID )
{
	TQValueList<int> ids;
	ids.append( recipeID );
	return loadRecipes( ids );
}

bool RecipeViewDialog::loadRecipes( const TQValueList<int> &ids, const TQString &layoutConfig )
{
	TDEApplication::setOverrideCursor( KCursor::waitCursor() );

	// Remove any files created by the last recipe loaded
	removeOldFiles();

	ids_loaded = ids; //need to save these ids in order to delete the html files later...make sure this comes after the call to removeOldFiles()
	recipe_loaded = ( ids.count() > 0 && ids[ 0 ] >= 0 );

	bool success = showRecipes( ids, layoutConfig );

	TDEApplication::restoreOverrideCursor();
	return success;
}

bool RecipeViewDialog::showRecipes( const TQValueList<int> &ids, const TQString &layoutConfig )
{
	KProgressDialog * progress_dialog = 0;

	if ( ids.count() > 1 )  //we don't want a progress bar coming up when there is only one recipe... it may show up during the splash screen
	{
		progress_dialog = new KProgressDialog( this, "open_progress_dialog", TQString::null, i18n( "Opening recipes, please wait..." ), true );
		progress_dialog->resize( 240, 80 );
	}

	HTMLExporter html_generator( tmp_filename + ".html", "html" );
	if ( layoutConfig != TQString::null ) {
		TDEConfig *config = TDEGlobal::config();
		config->setGroup( "Page Setup" );
		TQString styleFile = config->readEntry( layoutConfig+"Layout", locate( "appdata", "layouts/Default.klo" ) );
		if ( !styleFile.isEmpty() && TQFile::exists( styleFile ) )
			html_generator.setStyle( styleFile );

		TQString templateFile = config->readEntry( layoutConfig+"Template", locate( "appdata", "layouts/Default.template" ) );
		if ( !templateFile.isEmpty() && TQFile::exists( templateFile ) )
			html_generator.setTemplate( templateFile );
	}

	html_generator.exporter( ids, database, progress_dialog ); //writes the generated HTML to 'tmp_filename+".html"'
	if ( progress_dialog && progress_dialog->wasCancelled() ) {
		delete progress_dialog;
		return false;
	}

	delete recipeView;              // Temporary workaround
	recipeView = new TDEHTMLPart( this ); // to avoid the problem of caching images of TDEHTMLPart

	KURL url;
	url.setPath( tmp_filename + ".html" );
	recipeView->openURL( url );
	recipeView->show();
	kdDebug() << "Opening URL: " << url.htmlURL() << endl;

	delete progress_dialog;
	return true;
}

void RecipeViewDialog::print()
{
	if ( recipe_loaded ) {
		RecipePrintPreview preview( this, database, ids_loaded );
		preview.exec();
	}
}

void RecipeViewDialog::printNoPreview( void )
{
	if ( recipe_loaded ) {
		recipeView->view() ->print();
	}
}

void RecipeViewDialog::reload( const TQString &layoutConfig )
{
	loadRecipes( ids_loaded, layoutConfig );
}

void RecipeViewDialog::removeOldFiles()
{
	if ( ids_loaded.count() > 0 ) {
		RecipeList recipe_list;
		database->loadRecipes( &recipe_list, RecipeDB::Title, ids_loaded );

		TQValueList<int> recipe_ids;
		for ( RecipeList::const_iterator it = recipe_list.begin(); it != recipe_list.end(); ++it )
			recipe_ids << ( *it ).recipeID;

		HTMLExporter::removeHTMLFiles( tmp_filename, recipe_ids );
	}
}

void RecipeViewDialog::recipeRemoved( int id )
{
	//if the deleted recipe is loaded, clean the view up
	if ( ids_loaded.find(id) != ids_loaded.end() ) { 
		Recipe recipe; database->loadRecipe( &recipe, RecipeDB::Title, id );
		HTMLExporter::removeHTMLFiles( tmp_filename, recipe.recipeID );
		ids_loaded.remove(id);
	}
}

#include "recipeviewdialog.moc"