summaryrefslogtreecommitdiffstats
path: root/filters/xsltfilter/export/xsltexportdia.cc
blob: bf141f9036b39793c79da0e9788b05fe28720aaa (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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/* This file is part of the KDE project
   Copyright (C) 2002 Robert JACOLIN <rjacolin@ifrance.com>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
   This file use code from koTemplateOpenDia for the method chooseSlot.
*/

#include <xsltexportdia.h>
#include <xsltproc.h>

#include <tqdir.h>
#include <tqcombobox.h>

#include <tdeapplication.h>
#include <tdeglobal.h>
#include <tdelocale.h>
#include <tdeconfig.h>
#include <tdeversion.h>
#include <kstandarddirs.h>
#include <tderecentdocument.h>
#include <tdetempfile.h>
#include <tdefiledialog.h>
#include <kdebug.h>
#include <KoFilterManager.h>
#include <KoStoreDevice.h>

/*#ifdef __FreeBSD__
#include <unistd.h>
#endif*/

/*
 *  Constructs a XSLTExportDia which is a child of 'parent', with the
 *  name 'name' and widget flags set to 'f'.
 *
 *  The dialog will by default be modeless, unless you set 'modal' to
 *  TRUE to construct a modal dialog.
 */
XSLTExportDia::XSLTExportDia(KoStoreDevice* in, const TQCString &format, TQWidget* parent,  const char* name_, bool modal, WFlags fl )
    : XSLTDialog( parent, name_, modal, fl )
{
	int i = 0;
	_in = in;
	_format = format;
	setCaption(i18n("Export XSLT Configuration"));

	kapp->restoreOverrideCursor();

	/* Recent files */
	_config = new TDEConfig("xsltdialog");
	_config->setGroup( "XSLT export filter" );
	TQString value;
	while(i < 10)
	{
		value = _config->readPathEntry( TQString("Recent%1").arg(i) );
		kdDebug() << "recent : " << value << endl;
		if(!value.isEmpty())
		{
			_recentList.append( value );
			recentBox->insertItem(value);
		}
		else
			i = 10;
		i = i + 1;
	}

	/* Common xslt files box */
	TQString appName = (const char*) TDEGlobal::instance()->instanceName();
	kdDebug() << "app name = " << appName << endl;

	TQString filenames = TQString("xsltfilter") + TQDir::separator() + TQString("export") +
			TQDir::separator() + appName + TQDir::separator() + "*/*.xsl";
	TQStringList commonFilesList = TDEGlobal::dirs()->findAllResources("data", filenames, true);
	kdDebug() << "There are " << commonFilesList.size() << " entries like  " << filenames << endl;

	TQStringList tempList;
	TQString name;
	TQString file;

	for(TQStringList::Iterator it = commonFilesList.begin(); it != commonFilesList.end(); ++it)
	{
		tempList = TQStringList::split("/", (*it));
		file = tempList.last();
		tempList.pop_back();
		name = tempList.last();
		tempList.pop_back();
		kdDebug() << name << " " << file << endl;
		if(!_namesList.contains(name) && file == "main.xsl")
		{
			_filesList.append(file);
			_namesList.append(name);
			_dirsList.append(tempList.join("/"));
			kdDebug() << file << " get" << endl;
		}
	}

	xsltList->insertStringList(_namesList);
}

/*
 *  Destroys the object and frees any allocated resources
 */
XSLTExportDia::~XSLTExportDia()
{
    delete _config;
}

/**
 * Called when thecancel button is clicked.
 * Close the dialog box.
 */
void XSLTExportDia::cancelSlot()
{
	kdDebug() << "export cancelled" << endl;
	reject();
}

/**
 * Called when the choose button is clicked. A file dialog is open to allow to choose
 * the xslt to use.
 * Change the value of the current file.
 */
void XSLTExportDia::chooseSlot()
{

	/* Use dir from currently selected file */
    TQString dir = TQString();
    if ( _currentFile.isLocalFile() && TQFile::exists( _currentFile.path() ) )
        dir = TQFileInfo( _currentFile.path() ).absFilePath();

    KFileDialog *dialog = new KFileDialog(dir, TQString(), 0L, "file dialog", true);
    dialog->setCaption( i18n("Open Document") );
    dialog->setMimeFilter( KoFilterManager::mimeFilter( _format, KoFilterManager::Export ) );
    KURL u;

    if(dialog->exec() == TQDialog::Accepted)
    {
        u = dialog->selectedURL();
        TDERecentDocument::add(dialog->selectedURL().url(), !dialog->selectedURL().isLocalFile());
    } else //revert state
    {
        //if (bEmpty) openEmpty();
        //if (bTemplates) openTemplate();
    }

    delete dialog;

    TQString filename = u.path();
    TQString url = u.url();
    bool local = u.isLocalFile();

    bool ok = !url.isEmpty();
    if (local) // additional checks for local files
        ok = ok && (TQFileInfo( filename ).isFile() ||
                    (TQFileInfo( filename ).isSymLink() &&
                     !TQFileInfo( filename ).readLink().isEmpty() &&
                     TQFileInfo( TQFileInfo( filename ).readLink() ).isFile() ) );

    if ( ok )
    {
        _currentFile = u;
        okSlot();
    }
}

/**
 * Called when the user clic on an element in the recent list.
 * Change the value of the current file.
 */
void XSLTExportDia::chooseRecentSlot()
{
	kdDebug() << "recent slot : " << recentBox->currentText() << endl;
	_currentFile = recentBox->currentText();
}

/**
 * Called when teh user clic on an element in the common list of xslt sheet.
 * Change the value of the current file.
 */
void XSLTExportDia::chooseCommonSlot()
{
	int num = xsltList->currentItem();
	_currentFile = TQDir::separator() + _dirsList[num] + TQDir::separator() +
			xsltList->currentText() + TQDir::separator() + _filesList[num];
	kdDebug() << "common slot : " << _currentFile.url() << endl;
}

/**
 * Called when the user clic on the ok button. The xslt sheet is put on the recent list which is
 * saved, then the xslt processor is called to export the document.
 */
void XSLTExportDia::okSlot()
{
	hide();
	if(_currentFile.url().isEmpty())
		return;
	kdDebug() << "XSLT FILTER --> BEGIN" << endl;
	TQString stylesheet = _currentFile.directory() + TQDir::separator() + _currentFile.fileName();

	/* Add the current file in the recent list if is not and save the list. */
	if(_recentList.contains(stylesheet) == 0)
	{
		kdDebug() << "Style sheet add to recent list" << endl;
		/* Remove the older stylesheet used */
		if(_recentList.size() >= 10)
			_recentList.pop_back();

		/* Add the new */
		_recentList.prepend(stylesheet);

		/* Save the new list */
		kdDebug() << "Recent list save " << _recentList.size() << " entrie(s)" << endl;
		int i = 0;
		while(_recentList.size() > 0)
		{
			kdDebug() << "save : " << _recentList.first() << endl;
#if KDE_IS_VERSION(3,1,3)
			_config->writePathEntry( TQString("Recent%1").arg(i), _recentList.first());
#else
			_config->writeEntry( TQString("Recent%1").arg(i), _recentList.first());
#endif
			_recentList.pop_front();
			i = i + 1;
		}
		/* Write config on disk */
		_config->sync();
	}

	/* Temp file */
	KTempFile temp("xsltexport-", "kwd");
	temp.setAutoDelete(true);
	TQFile* tempFile = temp.file();

	const TQ_LONG buflen = 4096;
	char buffer[ buflen ];
	TQ_LONG readBytes = _in->readBlock( buffer, buflen );

	while ( readBytes > 0 )
	{
		tempFile->writeBlock( buffer, readBytes );
		readBytes = _in->readBlock( buffer, buflen );
	}
	temp.close();

	kdDebug() << stylesheet << endl;
	XSLTProc* xsltproc = new XSLTProc(temp.name(), _fileOut, stylesheet);
	xsltproc->parse();

	delete xsltproc;

	kdDebug() << "XSLT FILTER --> END" << endl;
	reject(); // ###### accept() ? (Werner)
}

#include <xsltexportdia.moc>