You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
369 lines
13 KiB
369 lines
13 KiB
15 years ago
|
/* This file is part of the KDE project
|
||
|
Copyright (C) 2002 Ariya Hidayat <ariyahidayat@yahoo.de>
|
||
|
|
||
|
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 <config.h>
|
||
|
|
||
|
#ifdef HAVE_UNISTD_H
|
||
|
#include <unistd.h>
|
||
|
#endif
|
||
|
|
||
13 years ago
|
#include <tqregexp.h>
|
||
|
#include <tqfileinfo.h>
|
||
|
#include <tqvaluelist.h>
|
||
|
#include <tqfont.h>
|
||
15 years ago
|
|
||
|
#include <kdebug.h>
|
||
|
#include <KoFilterChain.h>
|
||
|
#include <kgenericfactory.h>
|
||
|
#include <KoGlobal.h>
|
||
|
|
||
|
#include <amiproimport.h>
|
||
|
#include <amiproparser.h>
|
||
|
|
||
|
typedef KGenericFactory<AmiProImport, KoFilter> AmiProImportFactory;
|
||
|
K_EXPORT_COMPONENT_FACTORY( libamiproimport, AmiProImportFactory( "kofficefilters" ) )
|
||
|
|
||
13 years ago
|
AmiProImport::AmiProImport( KoFilter *, const char *, const TQStringList& ):
|
||
15 years ago
|
KoFilter()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
class AmiProConverter: public AmiProListener
|
||
|
{
|
||
|
public:
|
||
|
AmiProConverter();
|
||
13 years ago
|
TQString root, documentInfo;
|
||
15 years ago
|
virtual bool doOpenDocument();
|
||
|
virtual bool doCloseDocument();
|
||
|
virtual bool doDefineStyle( const AmiProStyle& style );
|
||
13 years ago
|
virtual bool doParagraph( const TQString& text, AmiProFormatList formatList,
|
||
15 years ago
|
AmiProLayout& l );
|
||
|
private:
|
||
|
AmiProStyleList styleList;
|
||
|
};
|
||
|
|
||
|
// helper function to escape string for XML-ness
|
||
13 years ago
|
static TQString XMLEscape( const TQString& str )
|
||
15 years ago
|
{
|
||
13 years ago
|
TQString result;
|
||
15 years ago
|
|
||
|
for( unsigned i=0; i<str.length(); i++ )
|
||
|
if( str[i] == '&' ) result += "&";
|
||
|
else if( str[i] == '<' ) result += "<";
|
||
|
else if( str[i] == '>' ) result += ">";
|
||
|
else if( str[i] == '"' ) result += """;
|
||
13 years ago
|
else if( str[i] == TQChar(39) ) result += "'";
|
||
15 years ago
|
else result += str[i];
|
||
|
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
// helper function to convert AmiPro format to KWord's FORMAT
|
||
13 years ago
|
static TQString AmiProFormatAsXML( AmiProFormat format )
|
||
15 years ago
|
{
|
||
13 years ago
|
TQString result;
|
||
15 years ago
|
|
||
13 years ago
|
TQString fontname = format.fontFamily;
|
||
15 years ago
|
if( fontname.isEmpty() ) fontname = KoGlobal::defaultFont().family();
|
||
13 years ago
|
TQString fontsize = TQString::number( format.fontSize );
|
||
|
TQString boldness = format.bold ? "75" : "50";
|
||
|
TQString italic = format.italic ? "1" : "0";
|
||
|
TQString strikeout = format.strikethrough ? "1" : "0";
|
||
|
TQString vertalign = format.superscript ? "2" : format.subscript ? "1" : "0";
|
||
|
TQString underline = format.double_underline ? "double" :
|
||
15 years ago
|
format.underline|format.word_underline ? "1" : "0";
|
||
|
|
||
13 years ago
|
result = "<FORMAT id=\"1\" pos=\"" + TQString::number(format.pos) +
|
||
|
"\" len=\"" + TQString::number(format.len) + "\">\n";
|
||
15 years ago
|
result.append( " <FONT name=\"" + fontname + "\" />\n" );
|
||
|
result.append( " <SIZE value=\"" + fontsize + "\" />\n" );
|
||
|
result.append( " <WEIGHT value=\"" + boldness + "\" />\n" );
|
||
|
result.append( " <ITALIC value=\"" + italic + "\" />\n" );
|
||
|
result.append( " <STRIKEOUT value=\"" + strikeout + "\" />\n" );
|
||
|
result.append( " <VERTALIGN value=\"" + vertalign + "\" />\n" );
|
||
|
result.append( " <UNDERLINE value=\"" + underline + "\" />\n" );
|
||
|
result.append( "</FORMAT>\n" );
|
||
|
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
// helper function to convert AmiPro list of formats to KWord FORMATS
|
||
13 years ago
|
static TQString AmiProFormatListAsXML( AmiProFormatList& formatList )
|
||
15 years ago
|
{
|
||
13 years ago
|
TQString result;
|
||
15 years ago
|
|
||
|
AmiProFormatList::iterator it;
|
||
|
for( it=formatList.begin(); it!=formatList.end(); ++it )
|
||
|
{
|
||
|
AmiProFormat& format = *it;
|
||
|
result.append( AmiProFormatAsXML(format) );
|
||
|
}
|
||
|
|
||
|
if( !result.isEmpty() )
|
||
|
{
|
||
|
result.prepend( "<FORMATS>\n" );
|
||
|
result.append( "</FORMATS>\n" );
|
||
|
}
|
||
|
|
||
|
return result;
|
||
|
}
|
||
|
|
||
13 years ago
|
// helper function to convert AmiPro paragraph layout to KWord's LAYOUT
|
||
|
static TQString AmiProLayoutAsXML( const AmiProLayout& layout )
|
||
15 years ago
|
{
|
||
13 years ago
|
TQString result;
|
||
15 years ago
|
|
||
13 years ago
|
TQString referredStyle = layout.name;
|
||
15 years ago
|
if( referredStyle.isEmpty() ) referredStyle = "Standard";
|
||
|
|
||
13 years ago
|
TQString fontname = layout.fontFamily;
|
||
15 years ago
|
if( fontname.isEmpty() ) fontname = KoGlobal::defaultFont().family();
|
||
13 years ago
|
TQString fontsize = TQString::number( layout.fontSize );
|
||
|
TQString fontcolor = "red=\"" + TQString::number( layout.fontColor.red() ) +
|
||
|
"\" green=\"" + TQString::number( layout.fontColor.green() ) +
|
||
|
"\" blue=\"" + TQString::number( layout.fontColor.blue() ) + "\"";
|
||
|
TQString boldness = layout.bold ? "75" : "50";
|
||
|
TQString italic = layout.italic ? "1" : "0";
|
||
|
TQString strikeout = layout.strikethrough ? "1" : "0";
|
||
|
TQString vertalign = layout.superscript ? "2" : layout.subscript ? "1" : "0";
|
||
|
TQString underline = layout.double_underline ? "double" :
|
||
|
layout.underline|layout.word_underline ? "1" : "0";
|
||
13 years ago
|
|
||
|
TQString align;
|
||
13 years ago
|
align = layout.align==TQt::AlignLeft ? "left" :
|
||
|
layout.align==TQt::AlignRight ? "right" :
|
||
|
layout.align==TQt::AlignCenter ? "center" :
|
||
|
layout.align==TQt::AlignJustify ? "justify" :
|
||
15 years ago
|
"left";
|
||
|
|
||
13 years ago
|
TQString offsets;
|
||
13 years ago
|
offsets = "before=\"" + TQString::number(layout.spaceBefore) +
|
||
|
"\" after=\"" + TQString::number(layout.spaceAfter) + "\"";
|
||
15 years ago
|
|
||
13 years ago
|
TQString linespacing;
|
||
13 years ago
|
linespacing = layout.linespace==AmiPro::LS_Single ? TQString::fromLatin1( "0" ) :
|
||
|
layout.linespace==AmiPro::LS_OneAndHalf ? TQString::fromLatin1( "oneandhalf" ) :
|
||
|
layout.linespace==AmiPro::LS_Double ? TQString::fromLatin1( "double" ) :
|
||
|
TQString::number( layout.linespace );
|
||
15 years ago
|
|
||
|
result.append( "<LAYOUT>\n" );
|
||
|
result.append( " <NAME value=\"" + XMLEscape( referredStyle ) + "\" />\n" );
|
||
|
result.append( " <FLOW align=\"" + align + "\" />\n" );
|
||
|
result.append( " <LINESPACING value=\"" + linespacing + "\" />\n" );
|
||
|
result.append( " <OFFSETS " + offsets + " />\n" );
|
||
|
result.append( " <LEFTBORDER width=\"0\" style=\"0\" />\n" );
|
||
|
result.append( " <RIGHTBORDER width=\"0\" style=\"0\" />\n" );
|
||
|
result.append( " <TOPBORDER width=\"0\" style=\"0\" />\n" );
|
||
|
result.append( " <BOTTOMBORDER width=\"0\" style=\"0\" />\n" );
|
||
|
result.append( " <INDENTS />\n" );
|
||
|
result.append( " <OFFSETS />\n" );
|
||
|
result.append( " <PAGEBREAKING />\n" );
|
||
|
result.append( " <COUNTER />\n" );
|
||
|
result.append( " <FORMAT id=\"1\">\n" );
|
||
|
result.append( " <FONT name=\"" + fontname + "\" />\n" );
|
||
|
result.append( " <SIZE value=\"" + fontsize + "\" />\n" );
|
||
|
result.append( " <COLOR " + fontcolor + " />\n" );
|
||
|
result.append( " <WEIGHT value=\"" + boldness + "\" />\n" );
|
||
|
result.append( " <ITALIC value=\"" + italic + "\" />\n" );
|
||
|
result.append( " <STRIKEOUT value=\"" + strikeout + "\" />\n" );
|
||
|
result.append( " <VERTALIGN value=\"" + vertalign + "\" />\n" );
|
||
|
result.append( " <UNDERLINE value=\"" + underline + "\" />\n" );
|
||
|
result.append( " </FORMAT>\n" );
|
||
|
result.append( "</LAYOUT>\n" );
|
||
|
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
// helper function to convert AmiPro style to KWord STYLE
|
||
13 years ago
|
static TQString AmiProStyleAsXML( const AmiProStyle& style )
|
||
15 years ago
|
{
|
||
13 years ago
|
TQString result;
|
||
15 years ago
|
|
||
13 years ago
|
TQString fontname = style.fontFamily;
|
||
15 years ago
|
if( fontname.isEmpty() ) fontname = KoGlobal::defaultFont().family();
|
||
13 years ago
|
TQString fontsize = TQString::number( style.fontSize );
|
||
|
TQString boldness = style.bold ? "75" : "50";
|
||
|
TQString italic = style.italic ? "1" : "0";
|
||
|
TQString strikeout = style.strikethrough ? "1" : "0";
|
||
|
TQString vertalign = style.superscript ? "2" : style.subscript ? "1" : "0";
|
||
|
TQString underline = style.double_underline ? "double" :
|
||
15 years ago
|
style.underline|style.word_underline ? "1" : "0";
|
||
|
|
||
13 years ago
|
TQString align;
|
||
|
align = style.align==TQt::AlignLeft ? "left" :
|
||
|
style.align==TQt::AlignRight ? "right" :
|
||
|
style.align==TQt::AlignCenter ? "center" :
|
||
|
style.align==TQt::AlignJustify ? "justify" :
|
||
15 years ago
|
"left";
|
||
|
|
||
13 years ago
|
TQString linespacing;
|
||
13 years ago
|
linespacing = style.linespace==AmiPro::LS_Single ? TQString::fromLatin1( "0" ) :
|
||
|
style.linespace==AmiPro::LS_OneAndHalf ? TQString::fromLatin1( "oneandhalf" ) :
|
||
|
style.linespace==AmiPro::LS_Double ? TQString::fromLatin1( "double" ) :
|
||
13 years ago
|
TQString::number( style.linespace );
|
||
15 years ago
|
|
||
13 years ago
|
TQString offsets;
|
||
|
offsets = "before=\"" + TQString::number(style.spaceBefore) +
|
||
|
"\" after=\"" + TQString::number(style.spaceAfter) + "\"";
|
||
15 years ago
|
|
||
|
result.append( "<STYLE>\n" );
|
||
|
result.append( " <NAME value=\"" + XMLEscape( style.name ) + "\" />\n" );
|
||
|
result.append( " <FOLLOWING name=\"Body Text\" />\n" );
|
||
|
result.append( " <FLOW align=\"" + align + "\" />\n" );
|
||
|
result.append( " <LINESPACING value=\"" + linespacing + "\" />\n" );
|
||
|
result.append( " <OFFSETS " + offsets + " />\n" );
|
||
|
result.append( " <LEFTBORDER width=\"0\" style=\"0\" />\n" );
|
||
|
result.append( " <RIGHTBORDER width=\"0\" style=\"0\" />\n" );
|
||
|
result.append( " <TOPBORDER width=\"0\" style=\"0\" />\n" );
|
||
|
result.append( " <BOTTOMBORDER width=\"0\" style=\"0\" />\n" );
|
||
|
result.append( " <FORMAT id=\"1\">\n" );
|
||
|
result.append( " <FONT name=\"" + fontname + "\" />\n" );
|
||
|
result.append( " <SIZE value=\"" + fontsize + "\" />\n" );
|
||
|
result.append( " <WEIGHT value=\"" + boldness + "\" />\n" );
|
||
|
result.append( " <ITALIC value=\"" + italic + "\" />\n" );
|
||
|
result.append( " <STRIKEOUT value=\"" + strikeout + "\" />\n" );
|
||
|
result.append( " <VERTALIGN value=\"" + vertalign + "\" />\n" );
|
||
|
result.append( " <UNDERLINE value=\"" + underline + "\" />\n" );
|
||
|
result.append( " </FORMAT>\n" );
|
||
|
result.append( "</STYLE>\n" );
|
||
|
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
// helper function to convert AmiPro styles to KWord STYLES
|
||
13 years ago
|
static TQString AmiProStyleListAsXML( AmiProStyleList& styleList )
|
||
15 years ago
|
{
|
||
13 years ago
|
TQString result;
|
||
15 years ago
|
|
||
|
AmiProStyleList::iterator it;
|
||
|
for( it=styleList.begin(); it!=styleList.end(); ++it )
|
||
|
{
|
||
|
AmiProStyle& style = *it;
|
||
|
result.append( AmiProStyleAsXML( style ) );
|
||
|
}
|
||
|
|
||
|
if( !result.isEmpty() )
|
||
|
{
|
||
|
result.prepend ( "<STYLES>\n" );
|
||
|
result.append( "</STYLES>\n" );
|
||
|
}
|
||
|
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
AmiProConverter::AmiProConverter()
|
||
|
{
|
||
|
root = "";
|
||
|
}
|
||
|
|
||
|
bool AmiProConverter::doOpenDocument()
|
||
|
{
|
||
13 years ago
|
TQString prolog = "<!DOCTYPE DOC>\n";
|
||
15 years ago
|
|
||
|
prolog.append( "<DOC mime=\"application/x-kword\" syntaxVersion=\"2\" editor=\"KWord\">\n");
|
||
|
prolog.append( "<PAPER width=\"595\" height=\"841\" format=\"1\" fType=\"0\" orientation=\"0\" hType=\"0\" columns=\"1\">\n" );
|
||
|
prolog.append( " <PAPERBORDERS left=\"36\" right=\"36\" top=\"36\" bottom=\"36\" />\n" );
|
||
|
prolog.append( "</PAPER>\n" );
|
||
|
prolog.append( "<ATTRIBUTES standardpage=\"1\" hasFooter=\"0\" hasHeader=\"0\" processing=\"0\" />\n" );
|
||
|
prolog.append( "<FRAMESETS>\n" );
|
||
|
prolog.append( "<FRAMESET removable=\"0\" frameType=\"1\" frameInfo=\"0\" autoCreateNewFrame=\"1\">\n" );
|
||
|
prolog.append( "<FRAME right=\"567\" left=\"28\" top=\"42\" bottom=\"799\" />\n" );
|
||
|
root = prolog;
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
bool AmiProConverter::doCloseDocument()
|
||
|
{
|
||
13 years ago
|
TQString epilog = "</FRAMESET>\n";
|
||
15 years ago
|
epilog.append( "</FRAMESETS>\n" );
|
||
|
epilog.append( AmiProStyleListAsXML( styleList ) );
|
||
|
epilog.append( "</DOC>\n" );
|
||
|
|
||
|
root.append( epilog );
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
bool AmiProConverter::doDefineStyle( const AmiProStyle& style )
|
||
|
{
|
||
|
styleList.append( style );
|
||
|
return true;
|
||
|
}
|
||
|
|
||
13 years ago
|
bool AmiProConverter::doParagraph( const TQString& text, AmiProFormatList formatList,
|
||
13 years ago
|
AmiProLayout& layout )
|
||
15 years ago
|
{
|
||
|
root.append( "<PARAGRAPH>\n" );
|
||
|
root.append( "<TEXT>" + XMLEscape( text ) + "</TEXT>\n" );
|
||
|
root.append( AmiProFormatListAsXML( formatList ) );
|
||
13 years ago
|
root.append( AmiProLayoutAsXML( layout ) );
|
||
15 years ago
|
root.append( "</PARAGRAPH>\n" );
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
13 years ago
|
KoFilter::ConversionStatus AmiProImport::convert( const TQCString& from, const TQCString& to )
|
||
15 years ago
|
{
|
||
|
// check for proper conversion
|
||
|
if( to!= "application/x-kword" || from != "application/x-amipro" )
|
||
|
return KoFilter::NotImplemented;
|
||
|
|
||
|
// parse/convert input file
|
||
|
AmiProParser *parser = new AmiProParser;
|
||
|
AmiProConverter *converter = new AmiProConverter;
|
||
|
parser->setListener( converter );
|
||
|
|
||
|
parser->process( m_chain->inputFile() );
|
||
|
|
||
|
if( converter->root.isEmpty() )
|
||
|
return KoFilter::StupidError;
|
||
|
|
||
13 years ago
|
TQString root = converter->root;
|
||
|
TQString documentInfo = converter->documentInfo;
|
||
15 years ago
|
|
||
|
delete converter;
|
||
|
delete parser;
|
||
|
|
||
|
// prepare storage
|
||
|
KoStoreDevice* out=m_chain->storageFile( "root", KoStore::Write );
|
||
|
|
||
|
// store output document
|
||
|
if( out )
|
||
|
{
|
||
13 years ago
|
TQCString cstring = root.utf8();
|
||
15 years ago
|
cstring.prepend( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );
|
||
|
out->writeBlock( (const char*) cstring, cstring.length() );
|
||
|
}
|
||
|
|
||
|
// store document info
|
||
|
out = m_chain->storageFile( "documentinfo.xml", KoStore::Write );
|
||
|
if ( out )
|
||
|
{
|
||
13 years ago
|
TQCString cstring = documentInfo.utf8();
|
||
15 years ago
|
cstring.prepend( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );
|
||
|
|
||
|
out->writeBlock( (const char*) cstring, cstring.length() );
|
||
|
}
|
||
|
|
||
|
return KoFilter::OK;
|
||
|
}
|
||
|
|
||
|
#include "amiproimport.moc"
|