filters

wmlimport.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 <qfileinfo.h>
00027 #include <qvaluelist.h>
00028 #include <qfont.h>
00029 
00030 #include <kdebug.h>
00031 #include <KoFilterChain.h>
00032 #include <kgenericfactory.h>
00033 #include <KoGlobal.h>
00034 
00035 #include <wmlimport.h>
00036 #include <wmlparser.h>
00037 
00038 typedef KGenericFactory<WMLImport, KoFilter> WMLImportFactory;
00039 K_EXPORT_COMPONENT_FACTORY( libwmlimport, WMLImportFactory( "kofficefilters" ) )
00040 
00041 WMLImport::WMLImport( KoFilter *, const char *, const QStringList& ):
00042                      KoFilter()
00043 {
00044 }
00045 
00046 // converts WML to KWord document
00047 class WMLConverter: public WMLParser
00048 {
00049   public:
00050     QString root;
00051     QString documentInfo;
00052     WMLConverter();
00053     virtual void parse( const char* filename );
00054     virtual bool doOpenCard( QString, QString );
00055     virtual bool doCloseCard();
00056     virtual bool doParagraph( QString text, WMLFormatList formatList,
00057       WMLLayout layout );
00058   private:
00059     QString m_title;
00060 };
00061 
00062 WMLConverter::WMLConverter()
00063 {
00064   root = "";
00065 }
00066 
00067 static QString WMLFormatAsXML( WMLFormat format )
00068 {
00069   QString result;
00070 
00071   if( format.href.isEmpty() )
00072   {
00073     QFont font = KoGlobal::defaultFont();
00074     QString fontFamily = font.family();
00075     QString fontSize = QString::number( 
00076       format.fontsize == WMLFormat::Big ? font.pointSizeFloat()+3 :
00077       format.fontsize == WMLFormat::Small ? font.pointSizeFloat()-3 : font.pointSizeFloat() );
00078     QString boldness = format.bold ? "75" : "50";
00079     QString italic = format.italic ? "1" : "0";
00080     QString underline = format.underline ? "1" : "0";
00081 
00082     result = "<FORMAT id=\"1\" pos=\"" + QString::number(format.pos) +
00083        "\" len=\"" + QString::number(format.len) + "\">\n";
00084     result.append( "  <FONT name=\"" + fontFamily + "\" />\n" );
00085     result.append( "  <SIZE value=\"" + fontSize + "\" />\n" );
00086     result.append( "  <WEIGHT value=\"" + boldness + "\" />\n" );
00087     result.append( "  <ITALIC value=\"" + italic  + "\" />\n" );
00088     result.append( "  <UNDERLINE value=\"" + underline + "\" />\n" );
00089     result.append( "</FORMAT>\n" );
00090   }
00091   else
00092   {
00093     // hyperlink
00094     result.append( "<FORMAT id=\"4\" pos=\"" + QString::number(format.pos) +
00095      "\" len=\"" + QString::number(format.len) + "\">\n");
00096     result.append( "<VARIABLE>\n" );
00097     result.append( "  <TYPE key=\"STRING\" type=\"9\" text=\"" + format.link + "\" />\n" );
00098     result.append( "  <LINK linkName=\"" + format.link + "\" hrefName=\"" + 
00099       format.href + "\" />\n" );
00100     result.append( "</VARIABLE>\n" );
00101     result.append( "</FORMAT>\n" );
00102   }
00103 
00104 
00105   return result;
00106 }
00107 
00108 static QString WMLLayoutAsXML( WMLLayout layout )
00109 {
00110   QString result;
00111 
00112   QString align = "left";
00113   if( layout.align == WMLLayout::Center ) align = "center";
00114   if( layout.align == WMLLayout::Right ) align = "right";
00115 
00116   QFont font = KoGlobal::defaultFont();
00117   QString fontFamily = font.family();
00118   QString fontSize = QString::number( font.pointSizeFloat() );
00119 
00120   result.append( "<LAYOUT>\n" );
00121   result.append( "  <NAME value=\"Standard\" />\n" );
00122   result.append( "  <FLOW align=\"" + align + "\" />\n" );
00123   result.append( "  <LINESPACING value=\"0\" />\n" );
00124   result.append( "  <LEFTBORDER width=\"0\" style=\"0\" />\n" );
00125   result.append( "  <RIGHTBORDER width=\"0\" style=\"0\" />\n" );
00126   result.append( "  <TOPBORDER width=\"0\" style=\"0\" />\n" );
00127   result.append( "  <BOTTOMBORDER width=\"0\" style=\"0\" />\n" );
00128   result.append( "  <INDENTS />\n" );
00129   result.append( "  <OFFSETS />\n" );
00130   result.append( "  <PAGEBREAKING />\n" );
00131   result.append( "  <COUNTER />\n" );
00132   result.append( "  <FORMAT id=\"1\">\n" );
00133   result.append( "    <WEIGHT value=\"50\" />\n" );
00134   result.append( "    <ITALIC value=\"0\" />\n" );
00135   result.append( "    <UNDERLINE value=\"0\" />\n" );
00136   result.append( "    <STRIKEOUT value=\"0\" />\n" );
00137   result.append( "    <CHARSET value=\"0\" />\n" );
00138   result.append( "    <VERTALIGN value=\"0\" />\n" );
00139   result.append( "    <FONT name=\"" + fontFamily + "\" />\n" );
00140   result.append( "    <SIZE value=\"" + fontSize + "\" />\n" );
00141   result.append( "  </FORMAT>\n" );
00142   result.append( "</LAYOUT>\n" );
00143 
00144   return result;
00145 }
00146 
00147 // use the first card title (or id) as document title
00148 bool WMLConverter::doOpenCard( QString id, QString title )
00149 {
00150   if( m_title.isEmpty() )
00151     m_title = ( !title.isEmpty() ) ? title : id;
00152 
00153   return TRUE;
00154 }
00155 
00156 // FIXME is this right ?
00157 bool WMLConverter::doCloseCard()
00158 {
00159   // add extra paragraph between cards
00160   return doParagraph( " ", WMLFormatList(), WMLLayout() );
00161 }
00162 
00163 bool WMLConverter::doParagraph( QString atext, WMLFormatList formatList,
00164   WMLLayout layout  )
00165 {
00166   QString text, formats;
00167 
00168   // encode the text for XML-ness
00169   text = atext;
00170   text.replace( '&', "&amp;" );
00171   text.replace( '<', "&lt;" );
00172   text.replace( '>', "&gt;" );
00173 
00174   // formats, taken from formatList
00175   WMLFormatList::iterator it;
00176   for( it=formatList.begin(); it!=formatList.end(); ++it )
00177   {
00178     WMLFormat& format = *it;
00179     formats.append( WMLFormatAsXML(format) );
00180   }
00181 
00182   // assemble
00183   root.append( "<PARAGRAPH>\n" );
00184   root.append( "<TEXT>" + text + "</TEXT>\n" );
00185   root.append( "<FORMATS>" + formats + "</FORMATS>\n" );
00186   root.append( WMLLayoutAsXML( layout) );
00187   root.append( "</PARAGRAPH>\n" );
00188 
00189   return TRUE;
00190 }
00191 
00192 void WMLConverter::parse( const char* filename )
00193 {
00194   WMLParser::parse( filename );
00195 
00196   QString prolog;
00197   prolog += "<!DOCTYPE DOC>\n";
00198   prolog += "<DOC mime=\"application/x-kword\" syntaxVersion=\"2\" editor=\"KWord\" >\n";
00199   prolog += "<PAPER width=\"595\" spHeadBody=\"9\" format=\"1\" height=\"841\" fType=\"0\" orientation=\"0\" hType=\"0\" columnspacing=\"2\" spFootBody=\"9\" columns=\"1\" >\n";
00200   prolog += "<PAPERBORDERS right=\"28\" left=\"28\" bottom=\"42\" top=\"42\" />\n";
00201   prolog += "</PAPER>\n";
00202   prolog += "<ATTRIBUTES hasTOC=\"0\" standardpage=\"1\" hasFooter=\"0\" hasHeader=\"0\" processing=\"0\" />\n";
00203   prolog += "<FRAMESETS>\n";
00204   prolog += "<FRAMESET frameType=\"1\" frameInfo=\"0\" name=\"Text Frameset 1\" visible=\"1\" >\n";
00205    prolog += "<FRAME runaround=\"1\" copy=\"0\" right=\"567\" newFrameBehavior=\"0\" left=\"28\" bottom=\"799\" runaroundGap=\"2\" top=\"42\" />\n";
00206 
00207   QString epilog;
00208   epilog = "</FRAMESET>\n";
00209   epilog += "</FRAMESETS>\n";
00210   epilog += "</DOC>\n";
00211 
00212   root.prepend( prolog );
00213   root.append( epilog );
00214 
00215   // document information (only title though)
00216   documentInfo = "<!DOCTYPE document-info>\n";
00217   documentInfo += "<document-info>\n";
00218   documentInfo += "<log><text></text></log>\n";
00219   documentInfo += "<author>\n";
00220   documentInfo += "<full-name></full-name>\n";
00221   documentInfo += "<title></title>\n";
00222   documentInfo += "<company></company>\n";
00223   documentInfo += "<email></email>\n";
00224   documentInfo += "<telephone></telephone>\n";
00225   documentInfo += "</author>\n";
00226   documentInfo += "<about>\n";
00227   documentInfo += "<abstract></abstract>\n";
00228   documentInfo += "<title>" + m_title + "</title>\n";
00229   documentInfo += "</about>\n";
00230   documentInfo += "</document-info>";
00231 
00232 }
00233 
00234 KoFilter::ConversionStatus WMLImport::convert( const QCString& from, const QCString& to )
00235 {
00236   // check for proper conversion
00237   if( to!= "application/x-kword" || from != "text/vnd.wap.wml" )
00238      return KoFilter::NotImplemented;
00239 
00240   // parse/convert input file
00241   WMLConverter filter;
00242   filter.parse( m_chain->inputFile().latin1() );
00243 
00244   // check for error
00245   // FIXME better error handling/reporting
00246   if( filter.root.isEmpty() )
00247     return KoFilter::StupidError;
00248 
00249   QString root = filter.root;
00250 
00251   // prepare storage
00252   KoStoreDevice* out=m_chain->storageFile( "root", KoStore::Write );
00253 
00254   // store output document
00255   if( out )
00256     {
00257       QCString cstring = root.utf8();
00258       cstring.prepend( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );
00259       out->writeBlock( (const char*) cstring, cstring.length() );
00260     }
00261 
00262 
00263   QString documentInfo = filter.documentInfo;
00264 
00265   // store document info
00266   out = m_chain->storageFile( "documentinfo.xml", KoStore::Write );
00267   if ( out )
00268     {
00269        QCString cstring = documentInfo.utf8();
00270        cstring.prepend( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );
00271 
00272        out->writeBlock( (const char*) cstring, cstring.length() );
00273      }
00274 
00275   return KoFilter::OK;
00276 }
00277 
00278 #include "wmlimport.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys