summaryrefslogtreecommitdiffstats
path: root/krecipes/src/exporters/baseexporter.cpp
blob: 8dcd09ac95e888e916130bbd922674ab32685920 (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
/***************************************************************************
*   Copyright (C) 2003-2005 by                                            *
*   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 "baseexporter.h"

#include <ntqfile.h>
#include <ntqfileinfo.h>

#include <tdeaboutdata.h>
#include <kdebug.h>
#include <tdelocale.h>
#include <tdeglobal.h>
#include <tdemessagebox.h>
#include <ktar.h>
#include <kstandarddirs.h>

#include "backends/recipedb.h"

BaseExporter::BaseExporter( const TQString& _filename, const TQString &format ) :
		file( 0 ),
		tar_file( 0 ),
		filename( _filename ),
		m_progress_dlg( 0 ),
		compress(false)
{
	//automatically append extension
	TQString extension = format.right( format.length()-2 );
	if ( filename.right( extension.length() ) != extension )
		filename += "." + extension;
}

BaseExporter::~BaseExporter()
{
	delete file;
	delete tar_file;
}

int BaseExporter::headerFlags() const
{
	return RecipeDB::None;
}

void BaseExporter::setCompressed( bool b )
{
	compress = b;
}

void BaseExporter::exporter( const TQValueList<int> &ids, RecipeDB *database, KProgressDialog *progress_dlg )
{
	m_progress_dlg = progress_dlg;

	if ( createFile() )
		saveToFile( ids, database );
	else
		kdDebug() << "no output file has been selected for export." << endl;
}

void BaseExporter::exporter( int id, RecipeDB *database, KProgressDialog *progress_dlg )
{
	TQValueList<int> single_recipe_list;
	single_recipe_list << id ;
	exporter( single_recipe_list, database, progress_dlg );
}

void BaseExporter::writeStream( TQTextStream &stream, const RecipeList &recipe_list )
{
	stream << createHeader(recipe_list);
	stream << createContent(recipe_list);
	stream << createFooter();
}

bool BaseExporter::createFile()
{
	if ( file )
		return true;

	if ( !filename.isEmpty() ) {
		if ( compress ) {
			tar_file = new KTar( filename, "application/x-gzip" );
			TQFileInfo fi( filename );
			file = new TQFile( locateLocal( "tmp",fi.fileName()+"ml" ) );
		}
		else
			file = new TQFile(filename);

		return (file != 0);
	}
	else
		return false;
}

TQString BaseExporter::fileName() const
{
	return filename;
}

void BaseExporter::saveToFile( const TQValueList<int> &ids, RecipeDB *database )
{
	if ( file->open( IO_WriteOnly ) ) {
		if ( m_progress_dlg )
			m_progress_dlg->progressBar()->setTotalSteps( ids.count()/progressInterval() );

		TQValueList<int> ids_copy = ids;
		TQTextStream stream( file );
		stream.setEncoding( TQTextStream::UnicodeUTF8 );

		RecipeList recipe_list;
		if ( headerFlags() != RecipeDB::None ) {
			database->loadRecipes( &recipe_list, headerFlags(), ids );
		}
		stream << createHeader( recipe_list );

		recipe_list.clear();
		for ( uint i = 0; i < ids.count(); i += progressInterval() ) {
			TQValueList<int> sub_list;
			for ( int sub_i = 0; sub_i < progressInterval(); ++sub_i ) {
				if ( ids_copy.count() == 0 ) break;

				sub_list << *ids_copy.begin();
				ids_copy.remove( ids_copy.begin() );
			}

			RecipeList recipe_list;
			database->loadRecipes( &recipe_list, supportedItems(), sub_list );

			TQString content = createContent( recipe_list );
			if ( !content.isEmpty() )
				stream << content;

			if ( m_progress_dlg && m_progress_dlg->wasCancelled() )
				break;

			if ( m_progress_dlg ) {
				m_progress_dlg->progressBar()->advance( progressInterval() );
				kapp->processEvents();
			}
		}

		stream << createFooter();

		if ( tar_file && tar_file->open( IO_WriteOnly ) ) {
			//close, which flushes the buffer, and then open for reading
			file->close();
			file->open( IO_ReadOnly );

			TQFileInfo fi( file->name() );
			TQByteArray data = file->readAll();
			tar_file->writeFile( fi.fileName(), fi.owner(), fi.group(), data.size(), data );
			tar_file->close();
		}

		file->close();
	}
}

TQString BaseExporter::krecipes_version() const
{
	TDEInstance * this_instance = TDEGlobal::instance();
	if ( this_instance && this_instance->aboutData() )
		return this_instance->aboutData() ->version();

	return TQString::null; //Oh, well.  We couldn't get the version.
}