25#include <opencalcexport.h>
27#include <tqdatetime.h>
31#include <tqvaluelist.h>
34#include <tdemessagebox.h>
36#include <kgenericfactory.h>
39#include <KoDocumentInfo.h>
40#include <KoFilterChain.h>
43#include <kspread_aboutdata.h>
44#include <kspread_cell.h>
45#include <kspread_doc.h>
46#include <kspread_format.h>
47#include <kspread_map.h>
48#include <kspread_view.h>
49#include <kspread_canvas.h>
50#include <kspread_sheet.h>
51#include <kspread_sheetprint.h>
52#include <kspread_style.h>
53#include <kspread_style_manager.h>
54#include <kspread_util.h>
56using namespace KSpread;
58typedef TQValueList<Reference> AreaList;
60class OpenCalcExportFactory : KGenericFactory<OpenCalcExport, KoFilter>
63 OpenCalcExportFactory(
void) : KGenericFactory<OpenCalcExport, KoFilter> (
"kspreadopencalcexport")
66 virtual void setupTranslations(
void )
68 TDEGlobal::locale()->insertCatalogue(
"kofficefilters" );
72K_EXPORT_COMPONENT_FACTORY( libopencalcexport, OpenCalcExportFactory() )
81OpenCalcExport::OpenCalcExport( KoFilter *,
const char *,
const TQStringList & )
82 : KoFilter(), m_locale( 0 )
86KoFilter::ConversionStatus OpenCalcExport::convert(
const TQCString & from,
87 const TQCString & to )
102 KoDocument * document = m_chain->inputDocument();
105 return KoFilter::StupidError;
107 if ( !::tqt_cast<const KSpread::Doc *>( document ) )
109 kdWarning(30518) <<
"document isn't a KSpread::Doc but a "
110 << document->className() << endl;
111 return KoFilter::NotImplemented;
114 if ( ( to !=
"application/vnd.sun.xml.calc") || (from !=
"application/x-kspread" ) )
116 kdWarning(30518) <<
"Invalid mimetypes " << to <<
" " << from << endl;
117 return KoFilter::NotImplemented;
120 const Doc * ksdoc =
static_cast<const Doc *
>(document);
122 if ( ksdoc->mimeType() !=
"application/x-kspread" )
124 kdWarning(30518) <<
"Invalid document mimetype " << ksdoc->mimeType() << endl;
125 return KoFilter::NotImplemented;
128 m_locale =
static_cast<Doc*
>(document)->locale();
129 if ( !writeFile( ksdoc ) )
130 return KoFilter::CreationError;
132 emit sigProgress( 100 );
137bool OpenCalcExport::writeFile(
const Doc * ksdoc )
139 KoStore * store = KoStore::createStore( m_chain->outputFile(), KoStore::Write,
"", KoStore::Zip );
144 uint filesWritten = 0;
146 if ( !exportContent( store, ksdoc ) )
149 filesWritten |= contentXML;
152 if ( !exportDocInfo( store, ksdoc ) )
155 filesWritten |= metaXML;
157 if ( !exportStyles( store, ksdoc ) )
160 filesWritten |= stylesXML;
162 if ( !exportSettings( store, ksdoc ) )
165 filesWritten |= settingsXML;
167 if ( !writeMetaFile( store, filesWritten ) )
177bool OpenCalcExport::exportDocInfo( KoStore * store,
const Doc* ksdoc )
179 if ( !store->open(
"meta.xml" ) )
182 KoDocumentInfo * docInfo = ksdoc->documentInfo();
183 KoDocumentInfoAbout * aboutPage =
static_cast<KoDocumentInfoAbout *
>( docInfo->page(
"about" ) );
184 KoDocumentInfoAuthor * authorPage =
static_cast<KoDocumentInfoAuthor*
>( docInfo->page(
"author" ) );
187 meta.appendChild( meta.createProcessingInstruction(
"xml",
"version=\"1.0\" encoding=\"UTF-8\"" ) );
189 TQDomElement content = meta.createElement(
"office:document-meta" );
190 content.setAttribute(
"xmlns:office",
"http://openoffice.org/2000/office");
191 content.setAttribute(
"xmlns:xlink",
"http://www.w3.org/1999/xlink" );
192 content.setAttribute(
"xmlns:dc",
"http://purl.org/dc/elements/1.1/" );
193 content.setAttribute(
"xmlns:meta",
"http://openoffice.org/2000/meta" );
194 content.setAttribute(
"office:version",
"1.0" );
196 TQDomNode officeMeta = meta.createElement(
"office:meta" );
198 TQDomElement data = meta.createElement(
"meta:generator" );
199 TQString app(
"KSpread " );
200 app += KSpread::version;
201 data.appendChild( meta.createTextNode( app ) );
202 officeMeta.appendChild( data );
204 data = meta.createElement(
"meta:initial-creator" );
205 data.appendChild( meta.createTextNode( authorPage->fullName() ) );
206 officeMeta.appendChild( data );
208 data = meta.createElement(
"meta:creator" );
209 data.appendChild( meta.createTextNode( authorPage->fullName() ) );
210 officeMeta.appendChild( data );
212 data = meta.createElement(
"dc:description" );
213 data.appendChild( meta.createTextNode( aboutPage->abstract() ) );
214 officeMeta.appendChild( data );
216 data = meta.createElement(
"meta:keywords" );
217 TQDomElement dataItem = meta.createElement(
"meta:keyword" );
218 dataItem.appendChild( meta.createTextNode( aboutPage->keywords() ) );
219 data.appendChild( dataItem );
220 officeMeta.appendChild( data );
222 data = meta.createElement(
"dc:title" );
223 data.appendChild( meta.createTextNode( aboutPage->title() ) );
224 officeMeta.appendChild( data );
226 data = meta.createElement(
"dc:subject" );
227 data.appendChild( meta.createTextNode( aboutPage->subject() ) );
228 officeMeta.appendChild( data );
230 const TQDateTime dt ( TQDateTime::currentDateTime() );
233 data = meta.createElement(
"dc:date" );
234 data.appendChild( meta.createTextNode( dt.toString( TQt::ISODate ) ) );
235 officeMeta.appendChild( data );
247 data = meta.createElement(
"meta:document-statistic" );
248 data.setAttribute(
"meta:table-count", TQString::number( ksdoc->map()->count() ) );
250 officeMeta.appendChild( data );
252 content.appendChild( officeMeta );
253 meta.appendChild( content );
255 TQCString doc( meta.toCString() );
256 kdDebug(30518) <<
"Meta: " << doc << endl;
258 store->write( doc, doc.length() );
260 if ( !store->close() )
266bool OpenCalcExport::exportSettings( KoStore * store,
const Doc * ksdoc )
268 if ( !store->open(
"settings.xml" ) )
272 doc.appendChild( doc.createProcessingInstruction(
"xml",
"version=\"1.0\" encoding=\"UTF-8\"" ) );
274 TQDomElement settings = doc.createElement(
"office:document-settings" );
275 settings.setAttribute(
"xmlns:office",
"http://openoffice.org/2000/office");
276 settings.setAttribute(
"xmlns:xlink",
"http://www.w3.org/1999/xlink" );
277 settings.setAttribute(
"xmlns:config",
"http://openoffice.org/2001/config" );
278 settings.setAttribute(
"office:version",
"1.0" );
280 TQDomElement begin = doc.createElement(
"office:settings" );
282 TQDomElement configItem = doc.createElement(
"config:config-item-set" );
283 configItem.setAttribute(
"config:name",
"view-settings" );
285 TQDomElement mapIndexed = doc.createElement(
"config:config-item-map-indexed" );
286 mapIndexed.setAttribute(
"config:name",
"Views" );
287 configItem.appendChild( mapIndexed );
289 TQDomElement mapItem = doc.createElement(
"config:config-item-map-entry" );
291 TQDomElement attribute = doc.createElement(
"config:config-item" );
292 attribute.setAttribute(
"config:name",
"ActiveTable" );
293 attribute.setAttribute(
"config:type",
"string" );
295 View * view =
static_cast<View*
>( ksdoc->views().getFirst());
296 TQString activeTable;
299 Canvas * canvas = view->canvasWidget();
300 activeTable = canvas->activeSheet()->sheetName();
302 view->saveCurrentSheetSelection();
304 attribute.appendChild( doc.createTextNode( activeTable ) );
305 mapItem.appendChild( attribute );
307 TQDomElement configmaped = doc.createElement(
"config:config-item-map-named" );
308 configmaped.setAttribute(
"config:name",
"Tables" );
310 TQPtrListIterator<Sheet> it( ksdoc->map()->sheetList() );
311 for( ; it.current(); ++it )
316 marker = view->markerFromSheet( *it );
318 TQDomElement tmpItemMapNamed = doc.createElement(
"config:config-item-map-entry" );
319 tmpItemMapNamed.setAttribute(
"config:name", ( *it )->tableName() );
321 TQDomElement sheetAttribute = doc.createElement(
"config:config-item" );
322 sheetAttribute.setAttribute(
"config:name",
"CursorPositionX" );
323 sheetAttribute.setAttribute(
"config:type",
"int" );
324 sheetAttribute.appendChild( doc.createTextNode( TQString::number(marker.x() ) ) );
325 tmpItemMapNamed.appendChild( sheetAttribute );
327 sheetAttribute = doc.createElement(
"config:config-item" );
328 sheetAttribute.setAttribute(
"config:name",
"CursorPositionY" );
329 sheetAttribute.setAttribute(
"config:type",
"int" );
330 sheetAttribute.appendChild( doc.createTextNode( TQString::number(marker.y() ) ) );
331 tmpItemMapNamed.appendChild( sheetAttribute );
333 configmaped.appendChild( tmpItemMapNamed );
335 mapItem.appendChild( configmaped );
339 mapIndexed.appendChild( mapItem );
341 begin.appendChild( configItem );
343 settings.appendChild( begin );
345 doc.appendChild( settings );
347 TQCString f( doc.toCString() );
348 kdDebug(30518) <<
"Settings: " << (
char const * ) f << endl;
350 store->write( f, f.length() );
352 if ( !store->close() )
358bool OpenCalcExport::exportContent( KoStore * store,
const Doc * ksdoc )
360 if ( !store->open(
"content.xml" ) )
363 createDefaultStyles();
366 doc.appendChild( doc.createProcessingInstruction(
"xml",
"version=\"1.0\" encoding=\"UTF-8\"" ) );
368 TQDomElement content = doc.createElement(
"office:document-content" );
369 content.setAttribute(
"xmlns:office",
"http://openoffice.org/2000/office");
370 content.setAttribute(
"xmlns:style",
"http://openoffice.org/2000/style" );
371 content.setAttribute(
"xmlns:text",
"http://openoffice.org/2000/text" );
372 content.setAttribute(
"xmlns:table",
"http://openoffice.org/2000/table" );
373 content.setAttribute(
"xmlns:draw",
"http://openoffice.org/2000/drawing" );
374 content.setAttribute(
"xmlns:fo",
"http://www.w3.org/1999/XSL/Format" );
375 content.setAttribute(
"xmlns:xlink",
"http://www.w3.org/1999/xlink" );
376 content.setAttribute(
"xmlns:number",
"http://openoffice.org/2000/datastyle" );
377 content.setAttribute(
"xmlns:svg",
"http://www.w3.org/2000/svg" );
378 content.setAttribute(
"xmlns:chart",
"http://openoffice.org/2000/chart" );
379 content.setAttribute(
"xmlns:dr3d",
"http://openoffice.org/2000/dr3d" );
380 content.setAttribute(
"xmlns:math",
"http://www.w3.org/1998/Math/MathML" );
381 content.setAttribute(
"xmlns:form",
"http://openoffice.org/2000/form" );
382 content.setAttribute(
"xmlns:script",
"http://openoffice.org/2000/script" );
383 content.setAttribute(
"office:class",
"spreadsheet" );
384 content.setAttribute(
"office:version",
"1.0" );
386 TQDomElement data = doc.createElement(
"office:script" );
387 content.appendChild( data );
389 if ( !exportBody( doc, content, ksdoc ) )
392 doc.appendChild( content );
394 TQCString f( doc.toCString() );
395 kdDebug(30518) <<
"Content: " << (
char const * ) f << endl;
397 store->write( f, f.length() );
399 if ( !store->close() )
405void exportNamedExpr( TQDomDocument & doc, TQDomElement & parent,
406 AreaList
const & namedAreas )
408 AreaList::const_iterator it = namedAreas.begin();
409 AreaList::const_iterator end = namedAreas.end();
413 TQDomElement namedRange = doc.createElement(
"table:named-range" );
417 namedRange.setAttribute(
"table:name", ref.ref_name );
418 namedRange.setAttribute(
"table:base-cell-address", convertRefToBase( ref.sheet_name, ref.rect ) );
419 namedRange.setAttribute(
"table:cell-range-address", convertRefToRange( ref.sheet_name, ref.rect ) );
421 parent.appendChild( namedRange );
427bool OpenCalcExport::exportBody( TQDomDocument & doc, TQDomElement & content,
const Doc * ksdoc )
429 TQDomElement fontDecls = doc.createElement(
"office:font-decls" );
430 TQDomElement autoStyles = doc.createElement(
"office:automatic-styles" );
431 TQDomElement body = doc.createElement(
"office:body" );
433 if ( ksdoc->map()->isProtected() )
435 body.setAttribute(
"table:structure-protected",
"true" );
438 ksdoc->map()->password( passwd );
439 if ( passwd.length() > 0 )
441 TQCString str( KCodecs::base64Encode( passwd ) );
442 body.setAttribute(
"table:protection-key", TQString( str.data() ) );
448 TQPtrListIterator<Sheet> it( ksdoc->map()->sheetList() );
450 for( it.toFirst(); it.current(); ++it )
455 Sheet * sheet = it.current();
457 ts.visible = !sheet->isHidden();
459 TQDomElement tabElem = doc.createElement(
"table:table" );
460 tabElem.setAttribute(
"table:style-name", m_styles.sheetStyle( ts ) );
462 if ( sheet->isProtected() )
464 tabElem.setAttribute(
"table:protected",
"true" );
467 sheet->password( passwd );
468 if ( passwd.length() > 0 )
470 TQCString str( KCodecs::base64Encode( passwd ) );
471 tabElem.setAttribute(
"table:protection-key", TQString( str.data() ) );
475 TQString name( sheet->tableName() );
477 int n = name.find(
' ' );
480 kdDebug(30518) <<
"Sheet name converting: " << name << endl;
482 kdDebug(30518) <<
"Sheet name converted: " << name << endl;
484 name = name.replace(
' ',
"_" );
486 TQRect _printRange = sheet->print()->printRange();
487 if ( _printRange != ( TQRect( TQPoint( 1, 1 ), TQPoint( KS_colMax, KS_rowMax ) ) ) )
489 TQString range= convertRangeToRef( name, _printRange );
491 tabElem.setAttribute(
"table:print-ranges", range );
495 tabElem.setAttribute(
"table:name", name );
497 maxRowCols( sheet, maxCols, maxRows );
499 exportSheet( doc, tabElem, sheet, maxCols, maxRows );
501 body.appendChild( tabElem );
504 KoDocument * document = m_chain->inputDocument();
505 Doc * kspreadDoc =
static_cast<Doc *
>( document );
507 AreaList namedAreas = kspreadDoc->listArea();
508 if ( namedAreas.count() > 0 )
510 TQDomElement namedExpr = doc.createElement(
"table:named-expressions" );
511 exportNamedExpr( doc, namedExpr, namedAreas );
513 body.appendChild( namedExpr );
516 m_styles.writeStyles( doc, autoStyles );
517 m_styles.writeFontDecl( doc, fontDecls );
519 content.appendChild( fontDecls );
520 content.appendChild( autoStyles );
521 content.appendChild( body );
526void OpenCalcExport::exportSheet( TQDomDocument & doc, TQDomElement & tabElem,
527 const Sheet * sheet,
int maxCols,
int maxRows )
529 kdDebug(30518) <<
"exportSheet: " << sheet->tableName() << endl;
532 while ( i <= maxCols )
534 const ColumnFormat * column = sheet->columnFormat( i );
536 cs.breakB = ::Style::automatic;
537 cs.size = column->mmWidth() / 10;
538 bool hide = column->isHide();
542 while ( j <= maxCols )
544 const ColumnFormat *c = sheet->columnFormat( j );
546 cs1.breakB = ::Style::automatic;
547 cs1.size = c->mmWidth() / 10;
549 if ( ColumnStyle::isEqual( &cs, cs1 ) && ( hide == c->isHide() ) )
556 TQDomElement colElem = doc.createElement(
"table:table-column" );
557 colElem.setAttribute(
"table:style-name", m_styles.columnStyle( cs ) );
558 colElem.setAttribute(
"table:default-cell-style-name",
"Default" );
560 colElem.setAttribute(
"table:visibility",
"collapse" );
563 colElem.setAttribute(
"table:number-columns-repeated", TQString::number( repeated ) );
565 tabElem.appendChild( colElem );
569 for ( i = 1; i <= maxRows; ++i )
571 const RowFormat * row = sheet->rowFormat( i );
573 rs.breakB = ::Style::automatic;
574 rs.size = row->mmHeight() / 10;
576 TQDomElement rowElem = doc.createElement(
"table:table-row" );
577 rowElem.setAttribute(
"table:style-name", m_styles.rowStyle( rs ) );
579 rowElem.setAttribute(
"table:visibility",
"collapse" );
581 exportCells( doc, rowElem, sheet, i, maxCols );
583 tabElem.appendChild( rowElem );
587void OpenCalcExport::exportCells( TQDomDocument & doc, TQDomElement & rowElem,
588 const Sheet *sheet,
int row,
int maxCols )
591 while ( i <= maxCols )
594 bool hasComment =
false;
595 const Cell* cell = sheet->cellAt( i, row );
596 TQDomElement cellElem;
598 if ( !cell->isPartOfMerged() )
599 cellElem = doc.createElement(
"table:table-cell" );
601 cellElem = doc.createElement(
"table:covered-table-cell" );
604 Value
const value( cell->value() );
605 if ( !cell->isDefault() )
607 font = cell->format()->textFont( i, row );
608 m_styles.addFont( font );
610 if ( cell->format()->hasProperty( Format::PComment ) )
615 CellStyle::loadData( c, cell );
617 cellElem.setAttribute(
"table:style-name", m_styles.cellStyle( c ) );
620 if ( cell->isEmpty() && !hasComment && !cell->isPartOfMerged() && !cell->doesMergeCells() )
623 while ( j <= maxCols )
625 const Cell *cell1 = sheet->cellAt( j, row );
628 CellStyle::loadData( c1, cell1 );
630 if ( cell1->isEmpty() && !cell->format()->hasProperty( Format::PComment )
631 && CellStyle::isEqual( &c, c1 ) && !cell->isPartOfMerged() && !cell->doesMergeCells() )
638 cellElem.setAttribute(
"table:number-columns-repeated", TQString::number( repeated ) );
641 if ( value.isBoolean() )
643 kdDebug(30518) <<
"Type: Boolean" << endl;
644 cellElem.setAttribute(
"table:value-type",
"boolean" );
645 cellElem.setAttribute(
"table:boolean-value", ( value.asBoolean() ?
"true" :
"false" ) );
647 else if ( value.isNumber() )
649 kdDebug(30518) <<
"Type: Number" << endl;
650 FormatType type = cell->format()->getFormatType( i, row );
652 if ( type == Percentage_format )
653 cellElem.setAttribute(
"table:value-type",
"percentage" );
655 cellElem.setAttribute(
"table:value-type",
"float" );
657 cellElem.setAttribute(
"table:value", TQString::number( value.asFloat() ) );
661 kdDebug(30518) <<
"Type: " << value.type() << endl;
664 if ( cell->isFormula() )
666 kdDebug(30518) <<
"Formula found" << endl;
668 TQString formula( convertFormula( cell->text() ) );
669 cellElem.setAttribute(
"table:formula", formula );
671 else if ( !cell->link().isEmpty() )
673 TQDomElement link = doc.createElement(
"text:p" );
674 TQDomElement linkref = doc.createElement(
"text:a" );
676 TQString tmp = cell->link();
677 if ( localReferenceAnchor( tmp ) )
678 linkref.setAttribute(
"xlink:href", (
"#"+tmp ) );
680 linkref.setAttribute(
"xlink:href", tmp );
682 linkref.appendChild( doc.createTextNode( cell->text() ) );
684 link.appendChild( linkref );
685 cellElem.appendChild( link );
687 else if ( !cell->isEmpty() )
689 TQDomElement textElem = doc.createElement(
"text:p" );
690 textElem.appendChild( doc.createTextNode( cell->strOutText() ) );
692 cellElem.appendChild( textElem );
693 kdDebug(30518) <<
"Cell StrOut: " << cell->strOutText() << endl;
696 if ( cell->doesMergeCells() )
698 int colSpan = cell->mergedXCells() + 1;
699 int rowSpan = cell->mergedYCells() + 1;
702 cellElem.setAttribute(
"table:number-columns-spanned", TQString::number( colSpan ) );
705 cellElem.setAttribute(
"table:number-rows-spanned", TQString::number( rowSpan ) );
710 TQString comment( cell->format()->comment( i, row ) );
711 TQDomElement annotation = doc.createElement(
"office:annotation" );
712 TQDomElement text = doc.createElement(
"text:p" );
713 text.appendChild( doc.createTextNode( comment ) );
715 annotation.appendChild( text );
716 cellElem.appendChild( annotation );
719 rowElem.appendChild( cellElem );
725void OpenCalcExport::maxRowCols(
const Sheet *sheet,
726 int & maxCols,
int & maxRows )
728 Cell
const * cell = sheet->firstCell();
732 if ( cell->column() > maxCols )
733 maxCols = cell->column();
735 if ( cell->row() > maxRows )
736 maxRows = cell->row();
738 cell = cell->nextCell();
741 RowFormat
const * row = sheet->firstRow();
745 if ( row->row() > maxRows )
746 maxRows = row->row();
751 ColumnFormat
const * col = sheet->firstCol();
754 if ( col->column() > maxCols )
755 maxCols = col->column();
762bool OpenCalcExport::exportStyles( KoStore * store,
const Doc *ksdoc )
764 if ( !store->open(
"styles.xml" ) )
768 doc.appendChild( doc.createProcessingInstruction(
"xml",
"version=\"1.0\" encoding=\"UTF-8\"" ) );
770 TQDomElement content = doc.createElement(
"office:document-styles" );
771 content.setAttribute(
"xmlns:office",
"http://openoffice.org/2000/office" );
772 content.setAttribute(
"xmlns:style",
"http://openoffice.org/2000/style" );
773 content.setAttribute(
"xmlns:text",
"http://openoffice.org/2000/text" );
774 content.setAttribute(
"xmlns:table",
"http://openoffice.org/2000/table" );
775 content.setAttribute(
"xmlns:draw",
"http://openoffice.org/2000/drawing" );
776 content.setAttribute(
"xmlns:fo",
"http://www.w3.org/1999/XSL/Format" );
777 content.setAttribute(
"xmlns:xlink",
"http://www.w3.org/1999/xlink" );
778 content.setAttribute(
"xmlns:number",
"http://openoffice.org/2000/datastyle" );
779 content.setAttribute(
"xmlns:svg",
"http://www.w3.org/2000/svg" );
780 content.setAttribute(
"xmlns:chart",
"http://openoffice.org/2000/chart" );
781 content.setAttribute(
"xmlns:dr3d",
"http://openoffice.org/2000/dr3d" );
782 content.setAttribute(
"xmlns:math",
"http://www.w3.org/1998/Math/MathML" );
783 content.setAttribute(
"xmlns:form",
"http://openoffice.org/2000/form" );
784 content.setAttribute(
"xmlns:script",
"http://openoffice.org/2000/script" );
785 content.setAttribute(
"office:version",
"1.0" );
788 TQDomElement officeStyles = doc.createElement(
"office:styles" );
789 exportDefaultCellStyle( doc, officeStyles );
791 TQDomElement fontDecls = doc.createElement(
"office:font-decls" );
792 m_styles.writeFontDecl( doc, fontDecls );
797 TQDomElement defaultStyle = doc.createElement(
"style:style" );
798 defaultStyle.setAttribute(
"style:name",
"Default" );
799 defaultStyle.setAttribute(
"style:family",
"table-cell" );
800 officeStyles.appendChild( defaultStyle );
802 TQDomElement autoStyles = doc.createElement(
"office:automatic-styles" );
803 exportPageAutoStyles( doc, autoStyles, ksdoc );
805 TQDomElement masterStyles = doc.createElement(
"office:master-styles" );
806 exportMasterStyles( doc, masterStyles, ksdoc );
808 content.appendChild( fontDecls );
809 content.appendChild( officeStyles );
810 content.appendChild( autoStyles );
811 content.appendChild( masterStyles );
813 doc.appendChild( content );
815 TQCString f( doc.toCString() );
816 kdDebug(30518) <<
"Content: " << (
char const * ) f << endl;
818 store->write( f, f.length() );
820 if ( !store->close() )
826void OpenCalcExport::exportDefaultCellStyle( TQDomDocument & doc, TQDomElement & officeStyles )
828 TQDomElement defStyle = doc.createElement(
"style:default-style" );
829 defStyle.setAttribute(
"style:family",
"table-cell" );
831 KoDocument * document = m_chain->inputDocument();
832 Doc * ksdoc =
static_cast<Doc *
>(document);
834 Format * format =
new Format( 0, ksdoc->styleManager()->defaultStyle() );
835 const TDELocale *locale = ksdoc->locale();
840 TQString l( locale->language() );
841 TDELocale::splitLocale( l, language, country, charSet );
842 TQFont font( format->font() );
843 m_styles.addFont( font,
true );
845 TQDomElement style = doc.createElement(
"style:properties" );
846 style.setAttribute(
"style:font-name", font.family() );
847 style.setAttribute(
"fo:font-size", TQString(
"%1pt" ).arg( font.pointSize() ) );
848 style.setAttribute(
"style:decimal-places", TQString::number( locale->fracDigits() ) );
849 style.setAttribute(
"fo:language", language );
850 style.setAttribute(
"fo:country", country );
851 style.setAttribute(
"style:font-name-asian",
"HG Mincho Light J" );
852 style.setAttribute(
"style:language-asian",
"none" );
853 style.setAttribute(
"style:country-asian",
"none" );
854 style.setAttribute(
"style:font-name-complex",
"Arial Unicode MS" );
855 style.setAttribute(
"style:language-complex",
"none" );
856 style.setAttribute(
"style:country-complex",
"none" );
857 style.setAttribute(
"style:tab-stop-distance",
"1.25cm" );
859 defStyle.appendChild( style );
860 officeStyles.appendChild( defStyle );
864void OpenCalcExport::createDefaultStyles()
869void OpenCalcExport::exportPageAutoStyles( TQDomDocument & doc, TQDomElement & autoStyles,
872 TQPtrListIterator<Sheet> it( ksdoc->map()->sheetList() );
873 const Sheet * sheet = it.toFirst();
875 float width = 20.999;
876 float height = 29.699;
880 width = sheet->print()->paperWidth() / 10;
881 height = sheet->print()->paperHeight() / 10;
884 TQString sWidth = TQString(
"%1cm" ).arg( width );
885 TQString sHeight = TQString(
"%1cm" ).arg( height );
887 TQDomElement pageMaster = doc.createElement(
"style:page-master" );
888 pageMaster.setAttribute(
"style:name",
"pm1" );
890 TQDomElement properties = doc.createElement(
"style:properties" );
891 properties.setAttribute(
"fo:page-width", sWidth );
892 properties.setAttribute(
"fo:page-height", sHeight );
893 properties.setAttribute(
"fo:border",
"0.002cm solid #000000" );
894 properties.setAttribute(
"fo:padding",
"0cm" );
895 properties.setAttribute(
"fo:background-color",
"transparent" );
897 pageMaster.appendChild( properties );
899 TQDomElement header = doc.createElement(
"style:header-style" );
900 properties = doc.createElement(
"style:properties" );
901 properties.setAttribute(
"fo:min-height",
"0.75cm" );
902 properties.setAttribute(
"fo:margin-left",
"0cm" );
903 properties.setAttribute(
"fo:margin-right",
"0cm" );
904 properties.setAttribute(
"fo:margin-bottom",
"0.25cm" );
906 header.appendChild( properties );
908 TQDomElement footer = doc.createElement(
"style:header-style" );
909 properties = doc.createElement(
"style:properties" );
910 properties.setAttribute(
"fo:min-height",
"0.75cm" );
911 properties.setAttribute(
"fo:margin-left",
"0cm" );
912 properties.setAttribute(
"fo:margin-right",
"0cm" );
913 properties.setAttribute(
"fo:margin-bottom",
"0.25cm" );
915 footer.appendChild( properties );
917 pageMaster.appendChild( header );
918 pageMaster.appendChild( footer );
920 autoStyles.appendChild( pageMaster );
923void OpenCalcExport::exportMasterStyles( TQDomDocument & doc, TQDomElement & masterStyles,
926 TQDomElement masterPage = doc.createElement(
"style:master-page" );
927 masterPage.setAttribute(
"style:name",
"Default" );
928 masterPage.setAttribute(
"style:page-master-name",
"pm1" );
930 TQPtrListIterator<Sheet> it( ksdoc->map()->sheetList() );
931 const Sheet * sheet = it.toFirst();
934 TQString headerCenter;
935 TQString headerRight;
937 TQString footerCenter;
938 TQString footerRight;
942 headerLeft = sheet->print()->headLeft();
943 headerCenter = sheet->print()->headMid();
944 headerRight = sheet->print()->headRight();
945 footerLeft = sheet->print()->footLeft();
946 footerCenter = sheet->print()->footMid();
947 footerRight = sheet->print()->footRight();
950 if ( ( headerLeft.length() > 0 ) || ( headerCenter.length() > 0 )
951 || ( headerRight.length() > 0 ) )
953 TQDomElement header = doc.createElement(
"style:header" );
954 TQDomElement left = doc.createElement(
"style:region-left" );
955 TQDomElement text = doc.createElement(
"text:p" );
956 convertPart( headerLeft, doc, text, ksdoc );
957 left.appendChild( text );
959 TQDomElement center = doc.createElement(
"style:region-center" );
960 TQDomElement text1 = doc.createElement(
"text:p" );
961 convertPart( headerCenter, doc, text1, ksdoc );
962 center.appendChild( text1 );
964 TQDomElement right = doc.createElement(
"style:region-right" );
965 TQDomElement text2 = doc.createElement(
"text:p" );
966 convertPart( headerRight, doc, text2, ksdoc );
967 right.appendChild( text2 );
969 header.appendChild( left );
970 header.appendChild( center );
971 header.appendChild( right );
973 masterPage.appendChild( header );
977 TQDomElement header = doc.createElement(
"style:header" );
978 TQDomElement text = doc.createElement(
"text:p" );
979 TQDomElement name = doc.createElement(
"text:sheet-name" );
980 name.appendChild( doc.createTextNode(
"???" ) );
981 text.appendChild( name );
982 header.appendChild( text );
984 masterPage.appendChild( header );
987 if ( ( footerLeft.length() > 0 ) || ( footerCenter.length() > 0 )
988 || ( footerRight.length() > 0 ) )
990 TQDomElement footer = doc.createElement(
"style:footer" );
991 TQDomElement left = doc.createElement(
"style:region-left" );
992 TQDomElement text = doc.createElement(
"text:p" );
993 convertPart( footerLeft, doc, text, ksdoc );
994 left.appendChild( text );
996 TQDomElement center = doc.createElement(
"style:region-center" );
997 TQDomElement text1 = doc.createElement(
"text:p" );
998 convertPart( footerCenter, doc, text1, ksdoc );
999 center.appendChild( text1 );
1001 TQDomElement right = doc.createElement(
"style:region-right" );
1002 TQDomElement text2 = doc.createElement(
"text:p" );
1003 convertPart( footerRight, doc, text2, ksdoc );
1004 right.appendChild( text2 );
1006 footer.appendChild( left );
1007 footer.appendChild( center );
1008 footer.appendChild( right );
1010 masterPage.appendChild( footer );
1014 TQDomElement footer = doc.createElement(
"style:footer" );
1015 TQDomElement text = doc.createElement(
"text:p" );
1016 text.appendChild( doc.createTextNode( i18n(
"Page " ) ) );
1017 TQDomElement number = doc.createElement(
"text:page-number" );
1018 number.appendChild( doc.createTextNode(
"1" ) );
1019 text.appendChild( number );
1020 footer.appendChild( text );
1022 masterPage.appendChild( footer );
1025 masterStyles.appendChild( masterPage );
1028void OpenCalcExport::addText( TQString
const & text, TQDomDocument & doc,
1029 TQDomElement & parent )
1031 if (text.length() > 0 )
1032 parent.appendChild( doc.createTextNode( text ) );
1035void OpenCalcExport::convertPart( TQString
const & part, TQDomDocument & doc,
1036 TQDomElement & parent,
const Doc * ksdoc )
1043 uint l = part.length();
1046 if ( inVar || part[i] ==
'<' )
1050 if ( part[i] ==
'>' )
1053 if ( var ==
"<page>" )
1055 addText( text, doc, parent );
1057 TQDomElement page = doc.createElement(
"text:page-number" );
1058 page.appendChild( doc.createTextNode(
"1" ) );
1059 parent.appendChild( page );
1061 else if ( var ==
"<pages>" )
1063 addText( text, doc, parent );
1065 TQDomElement page = doc.createElement(
"text:page-count" );
1066 page.appendChild( doc.createTextNode(
"99" ) );
1067 parent.appendChild( page );
1069 else if ( var ==
"<date>" )
1071 addText( text, doc, parent );
1073 TQDomElement t = doc.createElement(
"text:date" );
1074 t.setAttribute(
"text:date-value",
"0-00-00" );
1076 t.appendChild( doc.createTextNode( TQDate::currentDate().toString() ) );
1077 parent.appendChild( t );
1079 else if ( var ==
"<time>" )
1081 addText( text, doc, parent );
1083 TQDomElement t = doc.createElement(
"text:time" );
1084 t.appendChild( doc.createTextNode( TQTime::currentTime().toString() ) );
1085 parent.appendChild( t );
1087 else if ( var ==
"<file>" )
1089 addText( text, doc, parent );
1091 TQDomElement t = doc.createElement(
"text:file-name" );
1092 t.setAttribute(
"text:display",
"full" );
1093 t.appendChild( doc.createTextNode(
"???" ) );
1094 parent.appendChild( t );
1096 else if ( var ==
"<name>" )
1098 addText( text, doc, parent );
1100 TQDomElement t = doc.createElement(
"text:title" );
1101 t.appendChild( doc.createTextNode(
"???" ) );
1102 parent.appendChild( t );
1104 else if ( var ==
"<author>" )
1106 KoDocumentInfo * docInfo = ksdoc->documentInfo();
1107 KoDocumentInfoAuthor * authorPage =
static_cast<KoDocumentInfoAuthor*
>( docInfo->page(
"author" ) );
1109 text += authorPage->fullName();
1111 addText( text, doc, parent );
1113 else if ( var ==
"<email>" )
1115 KoDocumentInfo * docInfo = ksdoc->documentInfo();
1116 KoDocumentInfoAuthor * authorPage =
static_cast<KoDocumentInfoAuthor*
>( docInfo->page(
"author" ) );
1118 text += authorPage->email();
1120 addText( text, doc, parent );
1122 else if ( var ==
"<org>" )
1124 KoDocumentInfo * docInfo = ksdoc->documentInfo();
1125 KoDocumentInfoAuthor * authorPage =
static_cast<KoDocumentInfoAuthor*
>( docInfo->page(
"author" ) );
1127 text += authorPage->company();
1129 addText( text, doc, parent );
1131 else if ( var ==
"<sheet>" )
1133 addText( text, doc, parent );
1135 TQDomElement s = doc.createElement(
"text:sheet-name" );
1136 s.appendChild( doc.createTextNode(
"???" ) );
1137 parent.appendChild( s );
1143 addText( text, doc, parent );
1156 if ( !text.isEmpty() || !var.isEmpty() )
1159 addText( text+var, doc, parent );
1163TQString OpenCalcExport::convertFormula( TQString
const & formula )
const
1165 TQChar decimalSymbol(
'.' );
1168 const TQString decimal ( m_locale->decimalSymbol() );
1169 if ( !decimal.isEmpty() )
1171 decimalSymbol = decimal.at( 0 );
1176 TQRegExp exp(
"(\\$?)([a-zA-Z]+)(\\$?)([0-9]+)");
1177 int n = exp.search( formula, 0 );
1178 kdDebug(30518) <<
"Exp: " << formula <<
", n: " << n <<
", Length: " << formula.length()
1179 <<
", Matched length: " << exp.matchedLength() << endl;
1181 bool inQuote1 =
false;
1182 bool inQuote2 =
false;
1184 int l = (int) formula.length();
1189 if ( ( n != -1 ) && ( n < i ) )
1191 n = exp.search( formula, i );
1192 kdDebug(30518) <<
"Exp: " << formula.right( l - i ) <<
", n: " << n << endl;
1194 if ( formula[i] ==
'"' )
1196 inQuote1 = !inQuote1;
1201 if ( formula[i] ==
'\'' )
1204 inQuote2 = !inQuote2;
1208 if ( inQuote1 || inQuote2 )
1214 if ( ( formula[i] ==
'=' ) && ( formula[i + 1] ==
'=' ) )
1220 if ( formula[i] ==
'!' )
1227 else if ( formula[i] == decimalSymbol )
1235 int ml = exp.matchedLength();
1236 if ( formula[ i + ml ] ==
'!' )
1238 kdDebug(30518) <<
"No cell ref but sheet name" << endl;
1243 if ( ( i > 0 ) && ( formula[i - 1] !=
'!' ) )
1245 for (
int j = 0; j < ml; ++j )
1261bool OpenCalcExport::writeMetaFile( KoStore * store, uint filesWritten )
1263 store->enterDirectory(
"META-INF" );
1264 if ( !store->open(
"manifest.xml" ) )
1267 TQDomImplementation impl;
1268 TQDomDocumentType type( impl.createDocumentType(
"manifest:manifest",
"-//OpenOffice.org//DTD Manifest 1.0//EN",
"Manifest.dtd" ) );
1270 TQDomDocument meta( type );
1271 meta.appendChild( meta.createProcessingInstruction(
"xml",
"version=\"1.0\" encoding=\"UTF-8\"" ) );
1273 TQDomElement content = meta.createElement(
"manifest:manifest" );
1274 content.setAttribute(
"xmlns:manifest",
"http://openoffice.org/2001/manifest" );
1276 TQDomElement entry = meta.createElement(
"manifest:file-entry" );
1277 entry.setAttribute(
"manifest:media-type",
"application/vnd.sun.xml.calc" );
1278 entry.setAttribute(
"manifest:full-path",
"/" );
1279 content.appendChild( entry );
1281 entry = meta.createElement(
"manifest:file-entry" );
1282 content.appendChild( entry );
1284 if ( filesWritten & contentXML )
1286 entry = meta.createElement(
"manifest:file-entry" );
1287 entry.setAttribute(
"manifest:media-type",
"text/xml" );
1288 entry.setAttribute(
"manifest:full-path",
"content.xml" );
1289 content.appendChild( entry );
1292 if ( filesWritten & stylesXML )
1294 entry = meta.createElement(
"manifest:file-entry" );
1295 entry.setAttribute(
"manifest:media-type",
"text/xml" );
1296 entry.setAttribute(
"manifest:full-path",
"styles.xml" );
1297 content.appendChild( entry );
1300 if ( filesWritten & metaXML )
1302 entry = meta.createElement(
"manifest:file-entry" );
1303 entry.setAttribute(
"manifest:media-type",
"text/xml" );
1304 entry.setAttribute(
"manifest:full-path",
"meta.xml" );
1305 content.appendChild( entry );
1308 if ( filesWritten & settingsXML )
1310 entry = meta.createElement(
"manifest:file-entry" );
1311 entry.setAttribute(
"manifest:media-type",
"text/xml" );
1312 entry.setAttribute(
"manifest:full-path",
"settings.xml" );
1313 content.appendChild( entry );
1316 meta.appendChild( content );
1318 TQCString doc( meta.toCString() );
1319 kdDebug(30518) <<
"Manifest: " << doc << endl;
1321 store->write( doc, doc.length() );
1323 if ( !store->close() )
1329#include <opencalcexport.moc>