filters

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 <kstandarddirs.h>
32#include <tderecentdocument.h>
33#include <tdetempfile.h>
34#include <tdefiledialog.h>
35#include <kdebug.h>
36#include <KoFilterManager.h>
37#include <KoStoreDevice.h>
38
39/*#ifdef __FreeBSD__
40#include <unistd.h>
41#endif*/
42
43/*
44 * Constructs a XSLTExportDia which is a child of 'parent', with the
45 * name 'name' and widget flags set to 'f'.
46 *
47 * The dialog will by default be modeless, unless you set 'modal' to
48 * true to construct a modal dialog.
49 */
50XSLTExportDia::XSLTExportDia(KoStoreDevice* in, const TQCString &format, TQWidget* parent, const char* name_, bool modal, WFlags fl )
51 : XSLTDialog( parent, name_, modal, fl )
52{
53 int i = 0;
54 _in = in;
55 _format = format;
56 setCaption(i18n("Export XSLT Configuration"));
57
58 kapp->restoreOverrideCursor();
59
60 /* Recent files */
61 _config = new TDEConfig("xsltdialog");
62 _config->setGroup( "XSLT export filter" );
63 TQString value;
64 while(i < 10)
65 {
66 value = _config->readPathEntry( TQString("Recent%1").arg(i) );
67 kdDebug() << "recent : " << value << endl;
68 if(!value.isEmpty())
69 {
70 _recentList.append( value );
71 recentBox->insertItem(value);
72 }
73 else
74 i = 10;
75 i = i + 1;
76 }
77
78 /* Common xslt files box */
79 TQString appName = (const char*) TDEGlobal::instance()->instanceName();
80 kdDebug() << "app name = " << appName << endl;
81
82 TQString filenames = TQString("xsltfilter") + TQDir::separator() + TQString("export") +
83 TQDir::separator() + appName + TQDir::separator() + "*/*.xsl";
84 TQStringList commonFilesList = TDEGlobal::dirs()->findAllResources("data", filenames, true);
85 kdDebug() << "There are " << commonFilesList.size() << " entries like " << filenames << endl;
86
87 TQStringList tempList;
88 TQString name;
89 TQString file;
90
91 for(TQStringList::Iterator it = commonFilesList.begin(); it != commonFilesList.end(); ++it)
92 {
93 tempList = TQStringList::split("/", (*it));
94 file = tempList.last();
95 tempList.pop_back();
96 name = tempList.last();
97 tempList.pop_back();
98 kdDebug() << name << " " << file << endl;
99 if(!_namesList.contains(name) && file == "main.xsl")
100 {
101 _filesList.append(file);
102 _namesList.append(name);
103 _dirsList.append(tempList.join("/"));
104 kdDebug() << file << " get" << endl;
105 }
106 }
107
108 xsltList->insertStringList(_namesList);
109}
110
111/*
112 * Destroys the object and frees any allocated resources
113 */
114XSLTExportDia::~XSLTExportDia()
115{
116 delete _config;
117}
118
123void XSLTExportDia::cancelSlot()
124{
125 kdDebug() << "export cancelled" << endl;
126 reject();
127}
128
134void XSLTExportDia::chooseSlot()
135{
136
137 /* Use dir from currently selected file */
138 TQString dir = TQString();
139 if ( _currentFile.isLocalFile() && TQFile::exists( _currentFile.path() ) )
140 dir = TQFileInfo( _currentFile.path() ).absFilePath();
141
142 KFileDialog *dialog = new KFileDialog(dir, TQString(), 0L, "file dialog", true);
143 dialog->setCaption( i18n("Open Document") );
144 dialog->setMimeFilter( KoFilterManager::mimeFilter( _format, KoFilterManager::Export ) );
145 KURL u;
146
147 if(dialog->exec() == TQDialog::Accepted)
148 {
149 u = dialog->selectedURL();
150 TDERecentDocument::add(dialog->selectedURL().url(), !dialog->selectedURL().isLocalFile());
151 } else //revert state
152 {
153 //if (bEmpty) openEmpty();
154 //if (bTemplates) openTemplate();
155 }
156
157 delete dialog;
158
159 TQString filename = u.path();
160 TQString url = u.url();
161 bool local = u.isLocalFile();
162
163 bool ok = !url.isEmpty();
164 if (local) // additional checks for local files
165 ok = ok && (TQFileInfo( filename ).isFile() ||
166 (TQFileInfo( filename ).isSymLink() &&
167 !TQFileInfo( filename ).readLink().isEmpty() &&
168 TQFileInfo( TQFileInfo( filename ).readLink() ).isFile() ) );
169
170 if ( ok )
171 {
172 _currentFile = u;
173 okSlot();
174 }
175}
176
181void XSLTExportDia::chooseRecentSlot()
182{
183 kdDebug() << "recent slot : " << recentBox->currentText() << endl;
184 _currentFile = recentBox->currentText();
185}
186
191void XSLTExportDia::chooseCommonSlot()
192{
193 int num = xsltList->currentItem();
194 _currentFile = TQDir::separator() + _dirsList[num] + TQDir::separator() +
195 xsltList->currentText() + TQDir::separator() + _filesList[num];
196 kdDebug() << "common slot : " << _currentFile.url() << endl;
197}
198
203void XSLTExportDia::okSlot()
204{
205 hide();
206 if(_currentFile.url().isEmpty())
207 return;
208 kdDebug() << "XSLT FILTER --> BEGIN" << endl;
209 TQString stylesheet = _currentFile.directory() + TQDir::separator() + _currentFile.fileName();
210
211 /* Add the current file in the recent list if is not and save the list. */
212 if(_recentList.contains(stylesheet) == 0)
213 {
214 kdDebug() << "Style sheet add to recent list" << endl;
215 /* Remove the older stylesheet used */
216 if(_recentList.size() >= 10)
217 _recentList.pop_back();
218
219 /* Add the new */
220 _recentList.prepend(stylesheet);
221
222 /* Save the new list */
223 kdDebug() << "Recent list save " << _recentList.size() << " entrie(s)" << endl;
224 int i = 0;
225 while(_recentList.size() > 0)
226 {
227 kdDebug() << "save : " << _recentList.first() << endl;
228 _config->writePathEntry( TQString("Recent%1").arg(i), _recentList.first());
229 _recentList.pop_front();
230 i = i + 1;
231 }
232 /* Write config on disk */
233 _config->sync();
234 }
235
236 /* Temp file */
237 KTempFile temp("xsltexport-", "kwd");
238 temp.setAutoDelete(true);
239 TQFile* tempFile = temp.file();
240
241 const TQ_LONG buflen = 4096;
242 char buffer[ buflen ];
243 TQ_LONG readBytes = _in->readBlock( buffer, buflen );
244
245 while ( readBytes > 0 )
246 {
247 tempFile->writeBlock( buffer, readBytes );
248 readBytes = _in->readBlock( buffer, buflen );
249 }
250 temp.close();
251
252 kdDebug() << stylesheet << endl;
253 XSLTProc* xsltproc = new XSLTProc(temp.name(), _fileOut, stylesheet);
254 xsltproc->parse();
255
256 delete xsltproc;
257
258 kdDebug() << "XSLT FILTER --> END" << endl;
259 reject(); // ###### accept() ? (Werner)
260}
261
262#include <xsltexportdia.moc>