/* This file is part of the KDE project Copyright (C) 2002 Ariya Hidayat This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #include #ifdef HAVE_UNISTD_H #include #endif #include #include #include #include #include #include #include #include #include typedef KGenericFactory WMLImportFactory; K_EXPORT_COMPONENT_FACTORY( libwmlimport, WMLImportFactory( "kofficefilters" ) ) WMLImport::WMLImport( KoFilter *, const char *, const TQStringList& ): KoFilter() { } // converts WML to KWord document class WMLConverter: public WMLParser { public: TQString root; TQString documentInfo; WMLConverter(); virtual void parse( const char* filename ); virtual bool doOpenCard( TQString, TQString ); virtual bool doCloseCard(); virtual bool doParagraph( TQString text, WMLFormatList formatList, WMLLayout layout ); private: TQString m_title; }; WMLConverter::WMLConverter() { root = ""; } static TQString WMLFormatAsXML( WMLFormat format ) { TQString result; if( format.href.isEmpty() ) { TQFont font = KoGlobal::defaultFont(); TQString fontFamily = font.family(); TQString fontSize = TQString::number( format.fontsize == WMLFormat::Big ? font.pointSizeFloat()+3 : format.fontsize == WMLFormat::Small ? font.pointSizeFloat()-3 : font.pointSizeFloat() ); TQString boldness = format.bold ? "75" : "50"; TQString italic = format.italic ? "1" : "0"; TQString underline = format.underline ? "1" : "0"; result = "\n"; result.append( " \n" ); result.append( " \n" ); result.append( " \n" ); result.append( " \n" ); result.append( " \n" ); result.append( "\n" ); } else { // hyperlink result.append( "\n"); result.append( "\n" ); result.append( " \n" ); result.append( " \n" ); result.append( "\n" ); result.append( "\n" ); } return result; } static TQString WMLLayoutAsXML( WMLLayout layout ) { TQString result; TQString align = "left"; if( layout.align == WMLLayout::Center ) align = "center"; if( layout.align == WMLLayout::Right ) align = "right"; TQFont font = KoGlobal::defaultFont(); TQString fontFamily = font.family(); TQString fontSize = TQString::number( font.pointSizeFloat() ); result.append( "\n" ); result.append( " \n" ); result.append( " \n" ); result.append( " \n" ); result.append( " \n" ); result.append( " \n" ); result.append( " \n" ); result.append( " \n" ); result.append( " \n" ); result.append( " \n" ); result.append( " \n" ); result.append( " \n" ); result.append( " \n" ); result.append( " \n" ); result.append( " \n" ); result.append( " \n" ); result.append( " \n" ); result.append( " \n" ); result.append( " \n" ); result.append( " \n" ); result.append( " \n" ); result.append( " \n" ); result.append( "\n" ); return result; } // use the first card title (or id) as document title bool WMLConverter::doOpenCard( TQString id, TQString title ) { if( m_title.isEmpty() ) m_title = ( !title.isEmpty() ) ? title : id; return TRUE; } // FIXME is this right ? bool WMLConverter::doCloseCard() { // add extra paragraph between cards return doParagraph( " ", WMLFormatList(), WMLLayout() ); } bool WMLConverter::doParagraph( TQString atext, WMLFormatList formatList, WMLLayout layout ) { TQString text, formats; // encode the text for XML-ness text = atext; text.replace( '&', "&" ); text.replace( '<', "<" ); text.replace( '>', ">" ); // formats, taken from formatList WMLFormatList::iterator it; for( it=formatList.begin(); it!=formatList.end(); ++it ) { WMLFormat& format = *it; formats.append( WMLFormatAsXML(format) ); } // assemble root.append( "\n" ); root.append( "" + text + "\n" ); root.append( "" + formats + "\n" ); root.append( WMLLayoutAsXML( layout) ); root.append( "\n" ); return TRUE; } void WMLConverter::parse( const char* filename ) { WMLParser::parse( filename ); TQString prolog; prolog += "\n"; prolog += "\n"; prolog += "\n"; prolog += "\n"; prolog += "\n"; prolog += "\n"; prolog += "\n"; prolog += "\n"; prolog += "\n"; TQString epilog; epilog = "\n"; epilog += "\n"; epilog += "\n"; root.prepend( prolog ); root.append( epilog ); // document information (only title though) documentInfo = "\n"; documentInfo += "\n"; documentInfo += "\n"; documentInfo += "\n"; documentInfo += "\n"; documentInfo += "\n"; documentInfo += "\n"; documentInfo += "\n"; documentInfo += "\n"; documentInfo += "\n"; documentInfo += "\n"; documentInfo += "\n"; documentInfo += "" + m_title + "\n"; documentInfo += "\n"; documentInfo += ""; } KoFilter::ConversionStatus WMLImport::convert( const TQCString& from, const TQCString& to ) { // check for proper conversion if( to!= "application/x-kword" || from != "text/vnd.wap.wml" ) return KoFilter::NotImplemented; // parse/convert input file WMLConverter filter; filter.parse( m_chain->inputFile().latin1() ); // check for error // FIXME better error handling/reporting if( filter.root.isEmpty() ) return KoFilter::StupidError; TQString root = filter.root; // prepare storage KoStoreDevice* out=m_chain->storageFile( "root", KoStore::Write ); // store output document if( out ) { TQCString cstring = root.utf8(); cstring.prepend( "\n" ); out->writeBlock( (const char*) cstring, cstring.length() ); } TQString documentInfo = filter.documentInfo; // store document info out = m_chain->storageFile( "documentinfo.xml", KoStore::Write ); if ( out ) { TQCString cstring = documentInfo.utf8(); cstring.prepend( "\n" ); out->writeBlock( (const char*) cstring, cstring.length() ); } return KoFilter::OK; } #include "wmlimport.moc"