filters

xsltimportdia.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2000 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*/
19
20#include "xsltimportdia.h"
21#include "xsltimportdia.moc"
22
23//#include <stdio.h>
24
25#include <tqcombobox.h>
26#include <tqcursor.h>
27#include <tqstringlist.h>
28#include <tqdir.h>
29
30#include <tdeapplication.h>
31#include <tdelocale.h>
32#include <kstandarddirs.h>
33#include <tdeglobal.h>
34#include <tderecentdocument.h>
35#include <KoFilterManager.h>
36#include <tdetempfile.h>
37#include <kdebug.h>
38
39#include <xsltproc.h>
40
41/*
42 * Constructs a XSLTImportDia which is a child of 'parent', with the
43 * name 'name' and widget flags set to 'f'.
44 *
45 * The dialog will by default be modeless, unless you set 'modal' to
46 * true to construct a modal dialog.
47 */
48XSLTImportDia::XSLTImportDia(KoStore* out, const TQCString &format, TQWidget* parent, const char* name_, bool modal, WFlags fl )
49 : XSLTDialog( parent, name_, modal, fl )
50{
51 int i = 0;
52 _out = out;
53 _format = format;
54 setCaption(i18n("Import XSLT Configuration"));
55
56 /* Recent files */
57 _config = new TDEConfig("xsltdialog");
58 _config->setGroup( "XSLT import filter" );
59 TQString value;
60 while(i < 10)
61 {
62 value = _config->readPathEntry( TQString("Recent%1").arg(i) );
63 kdDebug() << "recent : " << value << endl;
64 if(!value.isEmpty())
65 {
66 _recentList.append( value );
67 recentBox->insertItem(value);
68 }
69 else
70 i = 10;
71 i = i + 1;
72 }
73
74 /* Common xslt files box */
75 TQString appName = (const char*) TDEGlobal::instance()->instanceName();
76 kdDebug() << "app name = " << appName << endl;
77
78 TQString filenames = TQString("xsltfilter") + TQDir::separator() + TQString("import") +
79 TQDir::separator() + appName + TQDir::separator() + "*/*.xsl";
80 TQStringList commonFilesList = TDEGlobal::dirs()->findAllResources("data", filenames, true);
81 kdDebug() << "There are " << commonFilesList.size() << " entries like " << filenames << endl;
82
83 TQStringList tempList;
84 TQString name;
85 TQString file;
86
87 for(TQStringList::Iterator it = commonFilesList.begin(); it != commonFilesList.end(); ++it)
88 {
89 tempList = TQStringList::split("/", (*it));
90 file = tempList.last();
91 tempList.pop_back();
92 name = tempList.last();
93 tempList.pop_back();
94 kdDebug() << name << " " << file << endl;
95 if(!_namesList.contains(name) && file == "main.xsl")
96 {
97 _filesList.append(file);
98 _namesList.append(name);
99 _dirsList.append(tempList.join("/"));
100 kdDebug() << file << " get" << endl;
101 }
102 }
103
104 xsltList->insertStringList(_namesList);
105 kapp->restoreOverrideCursor();
106}
107
108/*
109 * Destroys the object and frees any allocated resources
110 */
111XSLTImportDia::~XSLTImportDia()
112{
113 // no need to delete child widgets, TQt does it all for us
114 delete _config;
115}
116
121void XSLTImportDia::cancelSlot()
122{
123 kdDebug() << "import cancelled" << endl;
124 reject();
125}
126
132void XSLTImportDia::chooseSlot()
133{
134
135 /* Use dir from currently selected file */
136 TQString dir = TQString();
137 if ( _currentFile.isLocalFile() && TQFile::exists( _currentFile.path() ) )
138 dir = TQFileInfo( _currentFile.path() ).absFilePath();
139
140 KFileDialog *dialog = new KFileDialog(dir, TQString(), 0L, "file dialog", true);
141 dialog->setCaption( i18n("Open Document") );
142 dialog->setMimeFilter( KoFilterManager::mimeFilter( _format, KoFilterManager::Import ) );
143 KURL u;
144
145 if(dialog->exec() == TQDialog::Accepted)
146 {
147 u = dialog->selectedURL();
148 TDERecentDocument::add(dialog->selectedURL().url(), !dialog->selectedURL().isLocalFile());
149 } else //revert state
150 {
151 //if (bEmpty) openEmpty();
152 //if (bTemplates) openTemplate();
153 }
154
155 delete dialog;
156
157 TQString filename = u.path();
158 TQString url = u.url();
159 bool local = u.isLocalFile();
160
161 bool ok = !url.isEmpty();
162 if (local) // additional checks for local files
163 ok = ok && (TQFileInfo( filename ).isFile() ||
164 (TQFileInfo( filename ).isSymLink() &&
165 !TQFileInfo( filename ).readLink().isEmpty() &&
166 TQFileInfo( TQFileInfo( filename ).readLink() ).isFile() ) );
167
168 if ( ok )
169 {
170 _currentFile = u;
171 okSlot();
172 }
173}
174
179void XSLTImportDia::chooseRecentSlot()
180{
181 kdDebug() << "recent slot : " << recentBox->currentText() << endl;
182 _currentFile = recentBox->currentText();
183}
184
189void XSLTImportDia::chooseCommonSlot()
190{
191 int num = xsltList->currentItem();
192 _currentFile = TQDir::separator() + _dirsList[num] + TQDir::separator() +
193 xsltList->currentText() + TQDir::separator() + _filesList[num];
194 kdDebug() << "common slot : " << _currentFile.url() << endl;
195}
196
201void XSLTImportDia::okSlot()
202{
203 kapp->setOverrideCursor(TQCursor(TQt::WaitCursor));
204 hide();
205 if(_currentFile.url().isEmpty())
206 return;
207 kdDebug() << "XSLT FILTER --> BEGIN" << endl;
208 _out->open("root");
209 TQString stylesheet = _currentFile.directory() + "/" + _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 /* Create a temp file */
237 KTempFile temp("xsltimport-", "kwd");
238 temp.setAutoDelete(true);
239
240 TQFile* tempFile = temp.file();
241 tempFile->open(IO_WriteOnly);
242
243 /* Generate the data in the temp file */
244 XSLTProc* xsltproc = new XSLTProc(_fileIn, temp.name(), stylesheet);
245 xsltproc->parse();
246
247 /* Save the temp file in the store */
248 tempFile->open(IO_ReadOnly);
249 _out->write(tempFile->readAll());
250
251 delete tempFile;
252 delete xsltproc;
253
254 _out->close();
255 temp.close();
256
257 kdDebug() << "XSLT FILTER --> END" << endl;
258 reject();
259}
260