filters

wmlexport.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 2002 Ariya Hidayat <ariyahidayat@yahoo.de>
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 <config.h>
00021 
00022 #ifdef HAVE_UNISTD_H
00023 #include <unistd.h>
00024 #endif
00025 
00026 #include <qtextcodec.h>
00027 #include <qfile.h>
00028 #include <qfileinfo.h>
00029 #include <qtextstream.h>
00030 
00031 #include <kdebug.h>
00032 #include <KoFilterChain.h>
00033 #include <kgenericfactory.h>
00034 
00035 #include <KWEFStructures.h>
00036 #include <KWEFBaseWorker.h>
00037 #include <KWEFKWordLeader.h>
00038 #include <KWEFUtil.h>
00039 
00040 #include "wmlexport.h"
00041 
00042 typedef KGenericFactory<WMLExport, KoFilter> WMLExportFactory;
00043 K_EXPORT_COMPONENT_FACTORY( libwmlexport, WMLExportFactory( "kofficefilters" ) )
00044 
00045 class WMLWorker : public KWEFBaseWorker
00046 {
00047   public:
00048     WMLWorker(void)  { }
00049     virtual ~WMLWorker(void) { }
00050     virtual bool doOpenFile(const QString& filenameOut, const QString& to);
00051     virtual bool doCloseFile(void);
00052     virtual bool doOpenDocument(void);
00053     virtual bool doCloseDocument(void);
00054     virtual bool doFullParagraph(const QString& paraText, const LayoutData& layout,
00055         const ValueListFormatData& paraFormatDataList);
00056   private:
00057     QString filename;
00058     QString result;
00059     bool m_bold, m_italic, m_underline;
00060 };
00061 
00062 bool WMLWorker::doOpenFile(const QString& filenameOut, const QString& /*to*/)
00063 {
00064   filename = filenameOut;
00065   return TRUE;
00066 }
00067 
00068 bool WMLWorker::doCloseFile(void)
00069 {
00070   QFile out( filename );
00071   if( !out.open( IO_WriteOnly ) )
00072     return FALSE;
00073   QTextStream stream;
00074   stream.setDevice( &out );
00075   stream << result; 
00076   return TRUE;
00077 }
00078 
00079 bool WMLWorker::doOpenDocument(void)
00080 {
00081   result = "<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.1//EN\"\n";
00082   result += "      \"http://www.wapforum.org/DTD/wml_1.1.xml\" >\n";
00083   result += "<!-- Created using KWord, see www.koffice.org/kword -->\n";
00084   result += "<wml>\n";
00085   result += "<card>\n";
00086 
00087   m_bold = m_italic = m_underline = FALSE;
00088 
00089   return TRUE;
00090 }
00091 
00092 bool WMLWorker::doCloseDocument(void)
00093 {
00094   result += "</card>\n";
00095   result += "</wml>";
00096   return TRUE;
00097 }
00098 
00099 bool WMLWorker::doFullParagraph(const QString& paraText, 
00100   const LayoutData& layout, const ValueListFormatData& paraFormatDataList)
00101 {
00102   QString wmlText;
00103   QString text = paraText;
00104 
00105   ValueListFormatData::ConstIterator it;  
00106   for( it = paraFormatDataList.begin(); it!=paraFormatDataList.end(); ++it )
00107   {
00108     const FormatData& formatData = *it;
00109 
00110     // only if the format is for text (id==1)
00111     if( formatData.id == 1 )
00112     {
00113       QString partialText;
00114       partialText = text.mid( formatData.pos, formatData.len );
00115   
00116       // escape the text
00117       partialText = KWEFUtil::EscapeSgmlText( NULL, partialText, TRUE, TRUE );
00118 
00119       // apply formatting
00120       m_bold = formatData.text.weight >= 75;
00121       m_italic = formatData.text.italic;
00122       m_underline = formatData.text.underline;
00123 
00124       if( m_bold ) partialText = "<b>" + partialText + "</b>";
00125       if( m_italic ) partialText = "<i>" + partialText + "</i>";
00126       if( m_underline ) partialText = "<u>" + partialText + "</u>";
00127 
00128 
00129       wmlText += partialText; 
00130     }
00131   }
00132 
00133   // sentinel check
00134   QString align = layout.alignment.lower();
00135   if( ( align!="left" ) && ( align!="right" ) && ( align!="center" ) )
00136     align = "left"; 
00137 
00138   result += "<p align=\"" + align + "\">" + wmlText + "</p>\n";
00139 
00140   return TRUE;
00141 }
00142 
00143 WMLExport::WMLExport( KoFilter *, const char *, const QStringList& ):
00144                      KoFilter()
00145 {
00146 }
00147 
00148 KoFilter::ConversionStatus WMLExport::convert( const QCString& from, 
00149   const QCString& to )
00150 {
00151   // check for proper conversion
00152   if( to!= "text/vnd.wap.wml" || from != "application/x-kword" )
00153      return KoFilter::NotImplemented;
00154 
00155   WMLWorker* worker = new WMLWorker();
00156   KWEFKWordLeader* leader = new KWEFKWordLeader( worker );
00157 
00158   KoFilter::ConversionStatus result;
00159   result = leader->convert( m_chain, from, to );
00160 
00161   delete worker;
00162   delete leader;
00163 
00164   return result; 
00165 }
00166 
00167 #include "wmlexport.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys