filters
xsltimportdia.cc00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "xsltimportdia.h"
00021 #include "xsltimportdia.moc"
00022
00023
00024
00025 #include <qcombobox.h>
00026 #include <qcursor.h>
00027 #include <qstringlist.h>
00028 #include <qdir.h>
00029
00030 #include <kapplication.h>
00031 #include <klocale.h>
00032 #include <kstandarddirs.h>
00033 #include <kglobal.h>
00034 #include <krecentdocument.h>
00035 #include <KoFilterManager.h>
00036 #include <ktempfile.h>
00037 #include <kdebug.h>
00038
00039 #include <xsltproc.h>
00040
00041
00042
00043
00044
00045
00046
00047
00048 XSLTImportDia::XSLTImportDia(KoStore* out, const QCString &format, QWidget* parent, const char* name_, bool modal, WFlags fl )
00049 : XSLTDialog( parent, name_, modal, fl )
00050 {
00051 int i = 0;
00052 _out = out;
00053 _format = format;
00054 setCaption(i18n("Import XSLT Configuration"));
00055
00056
00057 _config = new KConfig("xsltdialog");
00058 _config->setGroup( "XSLT import filter" );
00059 QString value;
00060 while(i < 10)
00061 {
00062 value = _config->readPathEntry( QString("Recent%1").arg(i) );
00063 kdDebug() << "recent : " << value << endl;
00064 if(!value.isEmpty())
00065 {
00066 _recentList.append( value );
00067 recentBox->insertItem(value);
00068 }
00069 else
00070 i = 10;
00071 i = i + 1;
00072 }
00073
00074
00075 QString appName = (const char*) KGlobal::instance()->instanceName();
00076 kdDebug() << "app name = " << appName << endl;
00077
00078 QString filenames = QString("xsltfilter") + QDir::separator() + QString("import") +
00079 QDir::separator() + appName + QDir::separator() + "*/*.xsl";
00080 QStringList commonFilesList = KGlobal::dirs()->findAllResources("data", filenames, true);
00081 kdDebug() << "There are " << commonFilesList.size() << " entries like " << filenames << endl;
00082
00083 QStringList tempList;
00084 QString name;
00085 QString file;
00086
00087 for(QStringList::Iterator it = commonFilesList.begin(); it != commonFilesList.end(); ++it)
00088 {
00089 tempList = QStringList::split("/", (*it));
00090 file = tempList.last();
00091 tempList.pop_back();
00092 name = tempList.last();
00093 tempList.pop_back();
00094 kdDebug() << name << " " << file << endl;
00095 if(!_namesList.contains(name) && file == "main.xsl")
00096 {
00097 _filesList.append(file);
00098 _namesList.append(name);
00099 _dirsList.append(tempList.join("/"));
00100 kdDebug() << file << " get" << endl;
00101 }
00102 }
00103
00104 xsltList->insertStringList(_namesList);
00105 kapp->restoreOverrideCursor();
00106 }
00107
00108
00109
00110
00111 XSLTImportDia::~XSLTImportDia()
00112 {
00113
00114 delete _config;
00115 }
00116
00121 void XSLTImportDia::cancelSlot()
00122 {
00123 kdDebug() << "import cancelled" << endl;
00124 reject();
00125 }
00126
00132 void XSLTImportDia::chooseSlot()
00133 {
00134
00135
00136 QString dir = QString::null;
00137 if ( _currentFile.isLocalFile() && QFile::exists( _currentFile.path() ) )
00138 dir = QFileInfo( _currentFile.path() ).absFilePath();
00139
00140 KFileDialog *dialog = new KFileDialog(dir, QString::null, 0L, "file dialog", true);
00141 dialog->setCaption( i18n("Open Document") );
00142 dialog->setMimeFilter( KoFilterManager::mimeFilter( _format, KoFilterManager::Import ) );
00143 KURL u;
00144
00145 if(dialog->exec() == QDialog::Accepted)
00146 {
00147 u = dialog->selectedURL();
00148 KRecentDocument::add(dialog->selectedURL().url(), !dialog->selectedURL().isLocalFile());
00149 } else
00150 {
00151
00152
00153 }
00154
00155 delete dialog;
00156
00157 QString filename = u.path();
00158 QString url = u.url();
00159 bool local = u.isLocalFile();
00160
00161 bool ok = !url.isEmpty();
00162 if (local)
00163 ok = ok && (QFileInfo( filename ).isFile() ||
00164 (QFileInfo( filename ).isSymLink() &&
00165 !QFileInfo( filename ).readLink().isEmpty() &&
00166 QFileInfo( QFileInfo( filename ).readLink() ).isFile() ) );
00167
00168 if ( ok )
00169 {
00170 _currentFile = u;
00171 okSlot();
00172 }
00173 }
00174
00179 void XSLTImportDia::chooseRecentSlot()
00180 {
00181 kdDebug() << "recent slot : " << recentBox->currentText() << endl;
00182 _currentFile = recentBox->currentText();
00183 }
00184
00189 void XSLTImportDia::chooseCommonSlot()
00190 {
00191 int num = xsltList->currentItem();
00192 _currentFile = QDir::separator() + _dirsList[num] + QDir::separator() +
00193 xsltList->currentText() + QDir::separator() + _filesList[num];
00194 kdDebug() << "common slot : " << _currentFile.url() << endl;
00195 }
00196
00201 void XSLTImportDia::okSlot()
00202 {
00203 kapp->setOverrideCursor(QCursor(Qt::WaitCursor));
00204 hide();
00205 if(_currentFile.url().isEmpty())
00206 return;
00207 kdDebug() << "XSLT FILTER --> BEGIN" << endl;
00208 _out->open("root");
00209 QString stylesheet = _currentFile.directory() + "/" + _currentFile.fileName();
00210
00211
00212 if(_recentList.contains(stylesheet) == 0)
00213 {
00214 kdDebug() << "Style sheet add to recent list" << endl;
00215
00216 if(_recentList.size() >= 10)
00217 _recentList.pop_back();
00218
00219
00220 _recentList.prepend(stylesheet);
00221
00222
00223 kdDebug() << "Recent list save " << _recentList.size() << " entrie(s)" << endl;
00224 int i = 0;
00225 while(_recentList.size() > 0)
00226 {
00227 kdDebug() << "save : " << _recentList.first() << endl;
00228 #if KDE_IS_VERSION(3,1,3)
00229 _config->writePathEntry( QString("Recent%1").arg(i), _recentList.first());
00230 #else
00231 _config->writeEntry( QString("Recent%1").arg(i), _recentList.first());
00232 #endif
00233 _recentList.pop_front();
00234 i = i + 1;
00235 }
00236
00237 _config->sync();
00238 }
00239
00240
00241 KTempFile temp("xsltimport-", "kwd");
00242 temp.setAutoDelete(true);
00243
00244 QFile* tempFile = temp.file();
00245 tempFile->open(IO_WriteOnly);
00246
00247
00248 XSLTProc* xsltproc = new XSLTProc(_fileIn, temp.name(), stylesheet);
00249 xsltproc->parse();
00250
00251
00252 tempFile->open(IO_ReadOnly);
00253 _out->write(tempFile->readAll());
00254
00255 delete tempFile;
00256 delete xsltproc;
00257
00258 _out->close();
00259 temp.close();
00260
00261 kdDebug() << "XSLT FILTER --> END" << endl;
00262 reject();
00263 }
00264
|