filters

amiproexport.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 <amiproexport.h>
00041 
00042 typedef KGenericFactory<AmiProExport, KoFilter> AmiProExportFactory;
00043 K_EXPORT_COMPONENT_FACTORY( libamiproexport, AmiProExportFactory( "kofficefilters" ) )
00044 class AmiProWorker : public KWEFBaseWorker
00045 {
00046   public:
00047     AmiProWorker(void)  { }
00048     virtual ~AmiProWorker(void) { }
00049     virtual bool doOpenFile(const QString& filenameOut, const QString& to);
00050     virtual bool doCloseFile(void);
00051     virtual bool doOpenDocument(void);
00052     virtual bool doCloseDocument(void);
00053     virtual bool doFullParagraph(const QString& paraText, const LayoutData& layout,
00054         const ValueListFormatData& paraFormatDataList);
00055   private:
00056     QString filename;
00057     QString result;
00058     bool m_bold, m_italic, m_underline, m_underlineDouble;
00059     bool m_strike, m_subscript, m_superscript;
00060 };
00061 
00062 bool AmiProWorker::doOpenFile(const QString& filenameOut, const QString& /*to*/)
00063 {
00064   filename = filenameOut;
00065 
00066   return TRUE;
00067 }
00068 
00069 bool AmiProWorker::doCloseFile(void)
00070 {
00071   QFile out( filename );
00072   if( !out.open( IO_WriteOnly ) )
00073     return FALSE;
00074   QTextStream stream;
00075   stream.setDevice( &out );
00076   stream << result; 
00077   return TRUE;
00078 }
00079 
00080 bool AmiProWorker::doOpenDocument(void)
00081 {
00082   result = "[ver]\n\t4\n";
00083   result += "[sty]\n\t\n";
00084   result += "[lay]\n";
00085   result += "\tStandard\n";
00086   result += "\t516\n";
00087   result += "\t[rght]\n";
00088 
00089   // don't ask me, I have no idea what these numbers are
00090   int magic[] = { 16833, 11908, 1, 1440, 1440, 1, 1440, 1440,
00091    0, 1, 0, 1, 0, 2, 1, 1440, 10465, 12, 1, 720, 1, 1440,
00092    1, 2160, 1, 2880, 1, 3600, 1, 4320, 1, 5040, 1, 5760,
00093    1, 6480, 1, 7200, 1, 7920, 1, 8640 };
00094   for( uint i=0; i<sizeof(magic)/sizeof(magic[0]); i++ )
00095     result += "\t\t" + QString::number(magic[i]) + "\n";
00096 
00097   result += "[elay]\n";
00098   result += "[edoc]\n";
00099 
00100   m_bold = m_italic = m_underline = m_underlineDouble = 
00101   m_strike = m_subscript = m_superscript = FALSE;
00102 
00103   return TRUE;
00104 }
00105 
00106 bool AmiProWorker::doCloseDocument(void)
00107 {
00108   result += ">\n\n";
00109   return TRUE;
00110 }
00111 
00112 static QString AmiProEscape( const QString& text )
00113 {
00114   QString result;
00115 
00116   for( unsigned i=0; i<text.length(); i++ )
00117   {
00118     QChar ch = text[i];
00119     switch( ch.unicode() )
00120     {
00121       case '<': result += "<<"; break;
00122       case '>': result += "<;>"; break;
00123       case '[': result += "<[>"; break;
00124       case '@': result += "@@"; break;
00125       case '\'': result += "</R>"; break;
00126       default: result += ch; break;
00127     }
00128   }
00129 
00130   return result; 
00131 }
00132 
00133 bool AmiProWorker::doFullParagraph(const QString& paraText, 
00134   const LayoutData& /*layout*/, const ValueListFormatData& paraFormatDataList)
00135 {
00136   QString amiproText = "";
00137   QString text = paraText;
00138 
00139   ValueListFormatData::ConstIterator it;  
00140   for( it = paraFormatDataList.begin(); it!=paraFormatDataList.end(); ++it )
00141   {
00142     const FormatData& formatData = *it;
00143 
00144     // only if the format is for text (id==1)
00145     if( formatData.id == 1 )
00146     {
00147       QString partialText;
00148       partialText = text.mid( formatData.pos, formatData.len );
00149 
00150       partialText = AmiProEscape( partialText );
00151   
00152       // apply formatting
00153       m_bold = formatData.text.weight >= 75;
00154       m_italic = formatData.text.italic;
00155       m_underline = formatData.text.underline;
00156       m_underlineDouble = formatData.text.underlineValue == "double";
00157       m_subscript = formatData.text.verticalAlignment == 1;
00158       m_superscript = formatData.text.verticalAlignment == 2;
00159       m_strike = formatData.text.strikeout;
00160 
00161       if( m_bold ) partialText = "<+!>" + partialText + "<-!>";
00162       if( m_italic ) partialText = "<+\">" + partialText + "<-\">";
00163       if( m_underline && !m_underlineDouble ) partialText = "<+#>" + partialText + "<-#>";
00164       if( m_underlineDouble ) partialText = "<+)>" + partialText + "<-)>";
00165       if( m_subscript ) partialText = "<+'>" + partialText + "<-'>";
00166       if( m_superscript ) partialText = "<+&>" + partialText + "<-&>";
00167       if( m_strike) partialText = "<+%>" + partialText + "<-%>";
00168 
00169       amiproText += partialText; 
00170     }
00171   }
00172 
00173   result += amiproText + "\n\n";
00174 
00175   return TRUE;
00176 }
00177 
00178 AmiProExport::AmiProExport( KoFilter *, const char *, const QStringList& ):
00179                      KoFilter()
00180 {
00181 }
00182 
00183 KoFilter::ConversionStatus 
00184 AmiProExport::convert( const QCString& from, 
00185   const QCString& to )
00186 {
00187   // check for proper conversion
00188   if( to!= "application/x-amipro" || from != "application/x-kword" )
00189      return KoFilter::NotImplemented;
00190 
00191   AmiProWorker* worker = new AmiProWorker();
00192   KWEFKWordLeader* leader = new KWEFKWordLeader( worker );
00193 
00194   KoFilter::ConversionStatus result;
00195   result = leader->convert( m_chain, from, to );
00196 
00197   delete worker;
00198   delete leader;
00199 
00200   return result; 
00201 }
00202 
00203 #include "amiproexport.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys