filters

palmdocimport.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 <qstringlist.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 <palmdb.h>
00036 #include <palmdoc.h>
00037 
00038 #include "palmdocimport.h"
00039 #include "palmdoc.h"
00040 
00041 typedef KGenericFactory<PalmDocImport, KoFilter> PalmDocImportFactory;
00042 K_EXPORT_COMPONENT_FACTORY( libpalmdocimport, PalmDocImportFactory( "kofficefilters" ) )
00043 
00044 PalmDocImport::PalmDocImport( KoFilter *, const char *, const QStringList& ):
00045                      KoFilter()
00046 {
00047 }
00048 
00049 KoFilter::ConversionStatus PalmDocImport::convert( const QCString& from, const QCString& to )
00050 {
00051   // check for proper conversion
00052   if( to!= "application/x-kword" || from != "application/vnd.palm" )
00053      return KoFilter::NotImplemented;
00054 
00055   PalmDoc doc;
00056   QString inputFile( m_chain->inputFile() );
00057   doc.load( inputFile.latin1() );
00058 
00059   if( doc.result() == PalmDoc::InvalidFormat )
00060     return KoFilter::NotImplemented;
00061   if( doc.result() == PalmDoc::ReadError )
00062     return KoFilter::FileNotFound;
00063 
00064   QString root = processPlainDocument( doc.text() );
00065 
00066   // prepare storage
00067   KoStoreDevice *out=m_chain->storageFile( "root", KoStore::Write );
00068 
00069   if( out )
00070     {
00071       QCString cstring = root.utf8();
00072       cstring.prepend( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );
00073       out->writeBlock( (const char*) cstring, cstring.length() );
00074     }
00075 
00076   QString docTitle = doc.name();
00077   if( docTitle.isEmpty() )
00078   {
00079     QFileInfo info( inputFile );
00080     docTitle = info.baseName();
00081   }
00082 
00083   QString documentInfo = processDocumentInfo( docTitle );
00084 
00085   // store document info
00086   out = m_chain->storageFile( "documentinfo.xml", KoStore::Write );
00087   if( out )
00088     {
00089        QCString cstring = documentInfo.utf8();
00090        cstring.prepend( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );
00091        out->writeBlock( (const char*) cstring, cstring.length() );
00092      }
00093 
00094   return KoFilter::OK;
00095 }
00096 
00097 QString PalmDocImport::processPlainParagraph( QString text )
00098 {
00099   QString formats, layout, result;
00100 
00101   // specify FORMAT (just empty element)
00102   formats.append ( "  <FORMAT id=\"1\" pos=\"0\" len=\"" +
00103     QString::number( text.length() )+ "\">\n" );
00104   formats.append ( "  </FORMAT>\n" );
00105 
00106   QFont font = KoGlobal::defaultFont();
00107   QString fontFamily = font.family();
00108   double fontSize = font.pointSizeFloat();
00109 
00110   // default LAYOUT
00111   layout.append( "<LAYOUT>\n" );
00112   layout.append( "  <NAME value=\"Standard\" />\n" );
00113   layout.append( "  <FLOW align=\"left\" />\n" );
00114   layout.append( "  <LINESPACING value=\"0\" />\n" );
00115   layout.append( "  <LEFTBORDER width=\"0\" style=\"0\" />\n" );
00116   layout.append( "  <RIGHTBORDER width=\"0\" style=\"0\" />\n" );
00117   layout.append( "  <TOPBORDER width=\"0\" style=\"0\" />\n" );
00118   layout.append( "  <BOTTOMBORDER width=\"0\" style=\"0\" />\n" );
00119   layout.append( "  <INDENTS />\n" );
00120   layout.append( "  <OFFSETS after=\"9\" />\n" );
00121   layout.append( "  <PAGEBREAKING />\n" );
00122   layout.append( "  <COUNTER />\n" );
00123   layout.append( "  <FORMAT id=\"1\">\n" );
00124   layout.append( "    <SIZE value=\"" + QString::number( fontSize ) + "\" />\n" );
00125   layout.append( "    <WEIGHT value=\"50\" />\n" );
00126   layout.append( "    <ITALIC value=\"0\" />\n" );
00127   layout.append( "    <UNDERLINE value=\"0\" />\n" );
00128   layout.append( "    <STRIKEOUT value=\"0\" />\n" );
00129   layout.append( "    <CHARSET value=\"0\" />\n" );
00130   layout.append( "    <VERTALIGN value=\"0\" />\n" );
00131   layout.append( "    <FONT name=\"" + fontFamily + "\" />\n" );
00132   layout.append( "  </FORMAT>\n" );
00133   layout.append( "</LAYOUT>\n" );
00134 
00135   // encode text for XML-ness
00136   text.replace( '&', "&amp;" );
00137   text.replace( '<', "&lt;" );
00138   text.replace( '>', "&gt;" );
00139   text.replace( '"', "&quot;" );
00140   text.replace( '\'', "&apos;" );
00141 
00142   // construct the <PARAGRAPH>
00143   result.append( "<PARAGRAPH>\n" );
00144   result.append( "<TEXT>" + text + "</TEXT>\n" );
00145   result.append( "<FORMATS>\n");
00146   result.append( formats );
00147   result.append( "</FORMATS>\n");
00148   result.append( layout );
00149   result.append( "</PARAGRAPH>\n" );
00150 
00151   return result;
00152 }
00153 
00154 QString PalmDocImport::processPlainDocument( QString plaindoc )
00155 {
00156   QString prolog, content, epilog;
00157   QStringList paragraphs;
00158 
00159   paragraphs = QStringList::split( "\n\n", plaindoc, TRUE );
00160   for( unsigned int i = 0; i < paragraphs.count(); i++ )
00161   {
00162       QString text = paragraphs[i];
00163       text.replace( '\n', ' ' );
00164       content.append( processPlainParagraph( text ) );
00165   }
00166 
00167   prolog = "<!DOCTYPE DOC>\n";
00168   prolog.append( "<DOC mime=\"application/x-kword\" syntaxVersion=\"2\" editor=\"KWord\">\n");
00169 
00170   prolog.append( "<PAPER width=\"595\" height=\"841\" format=\"1\" fType=\"0\" orientation=\"0\" hType=\"0\" columns=\"1\">\n" );
00171   prolog.append( " <PAPERBORDERS left=\"36\" right=\"36\" top=\"36\" bottom=\"36\" />\n" );
00172   prolog.append( "</PAPER>\n" );
00173 
00174   prolog.append( "<ATTRIBUTES standardpage=\"1\" hasFooter=\"0\" hasHeader=\"0\" processing=\"0\" />\n" );
00175 
00176   prolog.append( "<FRAMESETS>\n" );
00177   prolog.append( "<FRAMESET removable=\"0\" frameType=\"1\" frameInfo=\"0\" autoCreateNewFrame=\"1\">\n" );
00178   prolog.append( "<FRAME right=\"567\" left=\"28\" top=\"42\" bottom=\"799\" />\n" );
00179 
00180   epilog = "</FRAMESET>\n";
00181   epilog.append( "</FRAMESETS>\n" );
00182   epilog.append( "</DOC>\n" );
00183 
00184   return prolog + content + epilog;
00185 }
00186 
00187 QString PalmDocImport::processDocumentInfo( const QString &title )
00188 {
00189   QString documentInfo;
00190 
00191   documentInfo = "<!DOCTYPE document-info>\n";
00192 
00193   documentInfo += "<document-info>\n";
00194   documentInfo += "<log><text></text></log>\n";
00195 
00196   documentInfo += "<author>\n";
00197   documentInfo += "<full-name></full-name>\n";
00198   documentInfo += "<title></title>\n";
00199   documentInfo += "<company></company>\n";
00200   documentInfo += "<email></email>\n";
00201   documentInfo += "<telephone></telephone>\n";
00202   documentInfo += "</author>\n";
00203 
00204   documentInfo += "<about>\n";
00205   documentInfo += "<abstract></abstract>\n";
00206   documentInfo += "<title>" + title + "</title>\n";
00207   documentInfo += "</about>\n";
00208 
00209   documentInfo += "</document-info>";
00210 
00211   return documentInfo;
00212 }
00213 
00214 #include "palmdocimport.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys