filters

xsltimportdia.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 2000 Robert JACOLIN <rjacolin@ifrance.com>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "xsltimportdia.h"
00021 #include "xsltimportdia.moc"
00022 
00023 //#include <stdio.h>
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  *  Constructs a XSLTImportDia which is a child of 'parent', with the
00043  *  name 'name' and widget flags set to 'f'.
00044  *
00045  *  The dialog will by default be modeless, unless you set 'modal' to
00046  *  TRUE to construct a modal dialog.
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     /* Recent files */
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     /* Common xslt files box */
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  *  Destroys the object and frees any allocated resources
00110  */
00111 XSLTImportDia::~XSLTImportDia()
00112 {
00113     // no need to delete child widgets, Qt does it all for us
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     /* Use dir from currently selected file */
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 //revert state
00150     {
00151         //if (bEmpty) openEmpty();
00152         //if (bTemplates) openTemplate();
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) // additional checks for local files
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     /* Add the current file in the recent list if is not and save the list. */
00212     if(_recentList.contains(stylesheet) == 0)
00213     {
00214         kdDebug() << "Style sheet add to recent list" << endl;
00215         /* Remove the older stylesheet used */
00216         if(_recentList.size() >= 10)
00217             _recentList.pop_back();
00218 
00219         /* Add the new */
00220         _recentList.prepend(stylesheet);
00221 
00222         /* Save the new list */
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         /* Write config on disk */
00237         _config->sync();
00238     }
00239     
00240     /* Create a temp file */
00241     KTempFile temp("xsltimport-", "kwd");
00242     temp.setAutoDelete(true);
00243 
00244     QFile* tempFile = temp.file();
00245     tempFile->open(IO_WriteOnly);
00246 
00247     /* Generate the data in the temp file */
00248     XSLTProc* xsltproc = new XSLTProc(_fileIn, temp.name(), stylesheet);
00249     xsltproc->parse();
00250 
00251     /* Save the temp file in the store */
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 
KDE Home | KDE Accessibility Home | Description of Access Keys