xsltexportdia.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2002 Robert JACOLIN <rjacolin@ifrance.com>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 This file use code from koTemplateOpenDia for the method chooseSlot.
19*/
20
21#include <xsltexportdia.h>
22#include <xsltproc.h>
23
24#include <tqdir.h>
25#include <tqcombobox.h>
26
27#include <tdeapplication.h>
28#include <tdeglobal.h>
29#include <tdelocale.h>
30#include <tdeconfig.h>
31#include <tdeversion.h>
32#include <kstandarddirs.h>
33#include <tderecentdocument.h>
34#include <tdetempfile.h>
35#include <tdefiledialog.h>
36#include <kdebug.h>
37#include <KoFilterManager.h>
38#include <KoStoreDevice.h>
39
40/*#ifdef __FreeBSD__
41#include <unistd.h>
42#endif*/
43
44/*
45 * Constructs a XSLTExportDia which is a child of 'parent', with the
46 * name 'name' and widget flags set to 'f'.
47 *
48 * The dialog will by default be modeless, unless you set 'modal' to
49 * true to construct a modal dialog.
50 */
51XSLTExportDia::XSLTExportDia(KoStoreDevice* in, const TQCString &format, TQWidget* parent, const char* name_, bool modal, WFlags fl )
52 : XSLTDialog( parent, name_, modal, fl )
53{
54 int i = 0;
55 _in = in;
56 _format = format;
57 setCaption(i18n("Export XSLT Configuration"));
58
59 kapp->restoreOverrideCursor();
60
61 /* Recent files */
62 _config = new TDEConfig("xsltdialog");
63 _config->setGroup( "XSLT export filter" );
64 TQString value;
65 while(i < 10)
66 {
67 value = _config->readPathEntry( TQString("Recent%1").arg(i) );
68 kdDebug() << "recent : " << value << endl;
69 if(!value.isEmpty())
70 {
71 _recentList.append( value );
72 recentBox->insertItem(value);
73 }
74 else
75 i = 10;
76 i = i + 1;
77 }
78
79 /* Common xslt files box */
80 TQString appName = (const char*) TDEGlobal::instance()->instanceName();
81 kdDebug() << "app name = " << appName << endl;
82
83 TQString filenames = TQString("xsltfilter") + TQDir::separator() + TQString("export") +
84 TQDir::separator() + appName + TQDir::separator() + "*/*.xsl";
85 TQStringList commonFilesList = TDEGlobal::dirs()->findAllResources("data", filenames, true);
86 kdDebug() << "There are " << commonFilesList.size() << " entries like " << filenames << endl;
87
88 TQStringList tempList;
89 TQString name;
90 TQString file;
91
92 for(TQStringList::Iterator it = commonFilesList.begin(); it != commonFilesList.end(); ++it)
93 {
94 tempList = TQStringList::split("/", (*it));
95 file = tempList.last();
96 tempList.pop_back();
97 name = tempList.last();
98 tempList.pop_back();
99 kdDebug() << name << " " << file << endl;
100 if(!_namesList.contains(name) && file == "main.xsl")
101 {
102 _filesList.append(file);
103 _namesList.append(name);
104 _dirsList.append(tempList.join("/"));
105 kdDebug() << file << " get" << endl;
106 }
107 }
108
109 xsltList->insertStringList(_namesList);
110}
111
112/*
113 * Destroys the object and frees any allocated resources
114 */
115XSLTExportDia::~XSLTExportDia()
116{
117 delete _config;
118}
119
124void XSLTExportDia::cancelSlot()
125{
126 kdDebug() << "export cancelled" << endl;
127 reject();
128}
129
135void XSLTExportDia::chooseSlot()
136{
137
138 /* Use dir from currently selected file */
139 TQString dir = TQString();
140 if ( _currentFile.isLocalFile() && TQFile::exists( _currentFile.path() ) )
141 dir = TQFileInfo( _currentFile.path() ).absFilePath();
142
143 KFileDialog *dialog = new KFileDialog(dir, TQString(), 0L, "file dialog", true);
144 dialog->setCaption( i18n("Open Document") );
145 dialog->setMimeFilter( KoFilterManager::mimeFilter( _format, KoFilterManager::Export ) );
146 KURL u;
147
148 if(dialog->exec() == TQDialog::Accepted)
149 {
150 u = dialog->selectedURL();
151 TDERecentDocument::add(dialog->selectedURL().url(), !dialog->selectedURL().isLocalFile());
152 } else //revert state
153 {
154 //if (bEmpty) openEmpty();
155 //if (bTemplates) openTemplate();
156 }
157
158 delete dialog;
159
160 TQString filename = u.path();
161 TQString url = u.url();
162 bool local = u.isLocalFile();
163
164 bool ok = !url.isEmpty();
165 if (local) // additional checks for local files
166 ok = ok && (TQFileInfo( filename ).isFile() ||
167 (TQFileInfo( filename ).isSymLink() &&
168 !TQFileInfo( filename ).readLink().isEmpty() &&
169 TQFileInfo( TQFileInfo( filename ).readLink() ).isFile() ) );
170
171 if ( ok )
172 {
173 _currentFile = u;
174 okSlot();
175 }
176}
177
182void XSLTExportDia::chooseRecentSlot()
183{
184 kdDebug() << "recent slot : " << recentBox->currentText() << endl;
185 _currentFile = recentBox->currentText();
186}
187
192void XSLTExportDia::chooseCommonSlot()
193{
194 int num = xsltList->currentItem();
195 _currentFile = TQDir::separator() + _dirsList[num] + TQDir::separator() +
196 xsltList->currentText() + TQDir::separator() + _filesList[num];
197 kdDebug() << "common slot : " << _currentFile.url() << endl;
198}
199
204void XSLTExportDia::okSlot()
205{
206 hide();
207 if(_currentFile.url().isEmpty())
208 return;
209 kdDebug() << "XSLT FILTER --> BEGIN" << endl;
210 TQString stylesheet = _currentFile.directory() + TQDir::separator() + _currentFile.fileName();
211
212 /* Add the current file in the recent list if is not and save the list. */
213 if(_recentList.contains(stylesheet) == 0)
214 {
215 kdDebug() << "Style sheet add to recent list" << endl;
216 /* Remove the older stylesheet used */
217 if(_recentList.size() >= 10)
218 _recentList.pop_back();
219
220 /* Add the new */
221 _recentList.prepend(stylesheet);
222
223 /* Save the new list */
224 kdDebug() << "Recent list save " << _recentList.size() << " entrie(s)" << endl;
225 int i = 0;
226 while(_recentList.size() > 0)
227 {
228 kdDebug() << "save : " << _recentList.first() << endl;
229#if KDE_IS_VERSION(3,1,3)
230 _config->writePathEntry( TQString("Recent%1").arg(i), _recentList.first());
231#else
232 _config->writeEntry( TQString("Recent%1").arg(i), _recentList.first());
233#endif
234 _recentList.pop_front();
235 i = i + 1;
236 }
237 /* Write config on disk */
238 _config->sync();
239 }
240
241 /* Temp file */
242 KTempFile temp("xsltexport-", "kwd");
243 temp.setAutoDelete(true);
244 TQFile* tempFile = temp.file();
245
246 const TQ_LONG buflen = 4096;
247 char buffer[ buflen ];
248 TQ_LONG readBytes = _in->readBlock( buffer, buflen );
249
250 while ( readBytes > 0 )
251 {
252 tempFile->writeBlock( buffer, readBytes );
253 readBytes = _in->readBlock( buffer, buflen );
254 }
255 temp.close();
256
257 kdDebug() << stylesheet << endl;
258 XSLTProc* xsltproc = new XSLTProc(temp.name(), _fileOut, stylesheet);
259 xsltproc->parse();
260
261 delete xsltproc;
262
263 kdDebug() << "XSLT FILTER --> END" << endl;
264 reject(); // ###### accept() ? (Werner)
265}
266
267#include <xsltexportdia.moc>