update dev300-m58
[ooovba.git] / sw / source / filter / xml / xmltexti.cxx
blob160aa2286aa67345263f5adb5ce0a1ef0c89f032
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: xmltexti.cxx,v $
10 * $Revision: 1.54 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sw.hxx"
35 #include <comphelper/storagehelper.hxx>
36 #include <comphelper/processfactory.hxx>
37 #include <com/sun/star/embed/EmbedStates.hpp>
38 #include <com/sun/star/embed/XEmbedObjectCreator.hpp>
39 #include <com/sun/star/embed/XLinkCreator.hpp>
40 #include <com/sun/star/embed/XEmbeddedObject.hpp>
41 #include <com/sun/star/embed/XVisualObject.hpp>
42 #include <com/sun/star/embed/Aspects.hpp>
43 #include <com/sun/star/task/XInteractionHandler.hpp>
44 #include <rtl/ustrbuf.hxx>
45 #include <sot/clsids.hxx>
46 #include <com/sun/star/lang/XUnoTunnel.hpp>
47 #include <xmloff/prstylei.hxx>
48 #include <xmloff/maptype.hxx>
49 #include <xmloff/xmlprmap.hxx>
50 #ifndef _XMLOFF_TXTPRMAP_HXX
51 #include <xmloff/txtprmap.hxx>
52 #endif
53 #include <xmloff/i18nmap.hxx>
54 #include "unocrsr.hxx"
55 #include "unoobj.hxx"
56 #include "unoframe.hxx"
57 #include "doc.hxx"
58 #include "unocoll.hxx"
59 #include <fmtfsize.hxx>
60 #include <fmtanchr.hxx>
61 #include "xmlimp.hxx"
62 #include "xmltbli.hxx"
63 #include "xmltexti.hxx"
64 #include "XMLRedlineImportHelper.hxx"
65 #include <xmloff/XMLFilterServiceNames.h>
66 #include <SwAppletImpl.hxx>
67 #include <ndole.hxx>
68 #include <docsh.hxx>
69 #include <sfx2/docfile.hxx>
71 // for locking SolarMutex: svapp + mutex
72 #include <vcl/svapp.hxx>
73 #include <vos/mutex.hxx>
75 #include <toolkit/helper/vclunohelper.hxx>
76 #include <svtools/embedhlp.hxx>
77 #include <svtools/urihelper.hxx>
79 using ::rtl::OUString;
80 using ::rtl::OUStringBuffer;
81 using namespace ::com::sun::star;
82 using namespace ::com::sun::star::uno;
83 using namespace ::com::sun::star::lang;
84 using namespace ::com::sun::star::text;
85 using namespace ::com::sun::star::frame;
86 using namespace ::com::sun::star::beans;
87 using namespace xml::sax;
90 struct XMLServiceMapEntry_Impl
92 const sal_Char *sFilterService;
93 sal_Int32 nFilterServiceLen;
95 sal_uInt32 n1;
96 sal_uInt16 n2, n3;
97 sal_uInt8 n4, n5, n6, n7, n8, n9, n10, n11;
100 #define SERVICE_MAP_ENTRY( app, s ) \
101 { XML_IMPORT_FILTER_##app, sizeof(XML_IMPORT_FILTER_##app)-1, \
102 SO3_##s##_CLASSID }
104 const XMLServiceMapEntry_Impl aServiceMap[] =
106 SERVICE_MAP_ENTRY( WRITER, SW ),
107 SERVICE_MAP_ENTRY( CALC, SC ),
108 SERVICE_MAP_ENTRY( DRAW, SDRAW ),
109 SERVICE_MAP_ENTRY( IMPRESS, SIMPRESS ),
110 SERVICE_MAP_ENTRY( CHART, SCH ),
111 SERVICE_MAP_ENTRY( MATH, SM ),
112 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
114 static void lcl_putHeightAndWidth ( SfxItemSet &rItemSet,
115 sal_Int32 nHeight, sal_Int32 nWidth,
116 long *pTwipHeight=0, long *pTwipWidth=0 )
118 if( nWidth > 0 && nHeight > 0 )
120 nWidth = MM100_TO_TWIP( nWidth );
121 if( nWidth < MINFLY )
122 nWidth = MINFLY;
123 nHeight = MM100_TO_TWIP( nHeight );
124 if( nHeight < MINFLY )
125 nHeight = MINFLY;
126 rItemSet.Put( SwFmtFrmSize( ATT_FIX_SIZE, nWidth, nHeight ) );
129 SwFmtAnchor aAnchor( FLY_AUTO_CNTNT );
130 rItemSet.Put( aAnchor );
132 if( pTwipWidth )
133 *pTwipWidth = nWidth;
134 if( pTwipHeight )
135 *pTwipHeight = nHeight;
138 static void lcl_setObjectVisualArea( const uno::Reference< embed::XEmbeddedObject >& xObj,
139 sal_Int64 nAspect,
140 const Size& aVisSize,
141 const MapUnit& aUnit )
143 if( xObj.is() && nAspect != embed::Aspects::MSOLE_ICON )
145 // convert the visual area to the objects units
146 MapUnit aObjUnit = VCLUnoHelper::UnoEmbed2VCLMapUnit( xObj->getMapUnit( nAspect ) );
147 Size aObjVisSize = OutputDevice::LogicToLogic( aVisSize, aUnit, aObjUnit );
148 awt::Size aSz;
149 aSz.Width = aObjVisSize.Width();
150 aSz.Height = aObjVisSize.Height();
154 xObj->setVisualAreaSize( nAspect, aSz );
156 catch( uno::Exception& )
158 OSL_ASSERT( "Couldn't set visual area of the object!\n" );
163 SwXMLTextImportHelper::SwXMLTextImportHelper(
164 const uno::Reference < XModel>& rModel,
165 SvXMLImport& rImport,
166 const uno::Reference<XPropertySet> & rInfoSet,
167 sal_Bool bInsertM, sal_Bool bStylesOnlyM, sal_Bool _bProgress,
168 sal_Bool bBlockM, sal_Bool bOrganizerM,
169 sal_Bool /*bPreserveRedlineMode*/ ) :
170 XMLTextImportHelper( rModel, rImport, bInsertM, bStylesOnlyM, _bProgress,
171 bBlockM, bOrganizerM ),
172 pRedlineHelper( NULL )
174 uno::Reference<XPropertySet> xDocPropSet( rModel, UNO_QUERY );
175 pRedlineHelper = new XMLRedlineImportHelper(
176 bInsertM || bBlockM, xDocPropSet, rInfoSet );
179 SwXMLTextImportHelper::~SwXMLTextImportHelper()
181 // #90463# the redline helper destructor sets properties on the document
182 // and may through an exception while doing so... catch this
185 delete pRedlineHelper;
187 catch ( const RuntimeException& )
189 // ignore
193 SvXMLImportContext *SwXMLTextImportHelper::CreateTableChildContext(
194 SvXMLImport& rImport,
195 sal_uInt16 nPrefix, const OUString& rLocalName,
196 const uno::Reference< XAttributeList > & xAttrList )
198 return new SwXMLTableContext(
199 (SwXMLImport&)rImport, nPrefix, rLocalName, xAttrList );
202 sal_Bool SwXMLTextImportHelper::IsInHeaderFooter() const
204 uno::Reference<XUnoTunnel> xCrsrTunnel(
205 ((SwXMLTextImportHelper *)this)->GetCursor(), UNO_QUERY );
206 ASSERT( xCrsrTunnel.is(), "missing XUnoTunnel for Cursor" );
207 OTextCursorHelper *pTxtCrsr = reinterpret_cast< OTextCursorHelper * >(
208 sal::static_int_cast< sal_IntPtr >( xCrsrTunnel->getSomething( OTextCursorHelper::getUnoTunnelId() )));
209 ASSERT( pTxtCrsr, "SwXTextCursor missing" );
210 SwDoc *pDoc = pTxtCrsr->GetDoc();
212 return pDoc && pDoc->IsInHeaderFooter( pTxtCrsr->GetPaM()->GetPoint()->nNode );
215 SwOLENode *lcl_GetOLENode( const SwFrmFmt *pFrmFmt )
217 SwOLENode *pOLENd = 0;
218 if( pFrmFmt )
220 const SwFmtCntnt& rCntnt = pFrmFmt->GetCntnt();
221 const SwNodeIndex *pNdIdx = rCntnt.GetCntntIdx();
222 pOLENd = pNdIdx->GetNodes()[pNdIdx->GetIndex() + 1]->GetOLENode();
224 ASSERT( pOLENd, "Where is the OLE node" );
225 return pOLENd;
228 uno::Reference< XPropertySet > SwXMLTextImportHelper::createAndInsertOLEObject(
229 SvXMLImport& rImport,
230 const OUString& rHRef,
231 const OUString& rStyleName,
232 const OUString& rTblName,
233 sal_Int32 nWidth, sal_Int32 nHeight )
235 // this method will modify the document directly -> lock SolarMutex
236 vos::OGuard aGuard(Application::GetSolarMutex());
238 uno::Reference < XPropertySet > xPropSet;
240 sal_Int32 nPos = rHRef.indexOf( ':' );
241 if( -1 == nPos )
242 return xPropSet;
244 OUString aObjName( rHRef.copy( nPos+1) );
246 if( !aObjName.getLength() )
247 return xPropSet;
249 uno::Reference<XUnoTunnel> xCrsrTunnel( GetCursor(), UNO_QUERY );
250 ASSERT( xCrsrTunnel.is(), "missing XUnoTunnel for Cursor" );
251 OTextCursorHelper *pTxtCrsr = reinterpret_cast< OTextCursorHelper * >(
252 sal::static_int_cast< sal_IntPtr >( xCrsrTunnel->getSomething( OTextCursorHelper::getUnoTunnelId() )));
253 ASSERT( pTxtCrsr, "SwXTextCursor missing" );
254 SwDoc *pDoc = SwImport::GetDocFromXMLImport( rImport );
256 SfxItemSet aItemSet( pDoc->GetAttrPool(), RES_FRMATR_BEGIN,
257 RES_FRMATR_END );
258 Size aTwipSize( 0, 0 );
259 Rectangle aVisArea( 0, 0, nWidth, nHeight );
260 lcl_putHeightAndWidth( aItemSet, nHeight, nWidth,
261 &aTwipSize.Height(), &aTwipSize.Width() );
263 SwFrmFmt *pFrmFmt = 0;
264 SwOLENode *pOLENd = 0;
265 if( rHRef.copy( 0, nPos ).equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("vnd.sun.star.ServiceName") ) )
267 sal_Bool bInsert = sal_False;
268 SvGlobalName aClassName;
269 const XMLServiceMapEntry_Impl *pEntry = aServiceMap;
270 while( pEntry->sFilterService )
272 if( aObjName.equalsAsciiL( pEntry->sFilterService,
273 pEntry->nFilterServiceLen ) )
275 aClassName = SvGlobalName( pEntry->n1, pEntry->n2,
276 pEntry->n3, pEntry->n4,
277 pEntry->n5, pEntry->n6,
278 pEntry->n7, pEntry->n8,
279 pEntry->n9, pEntry->n10,
280 pEntry->n11 );
281 bInsert = sal_True;
282 break;
284 pEntry++;
287 if( bInsert )
289 uno::Reference < embed::XStorage > xStorage = comphelper::OStorageHelper::GetTemporaryStorage();
292 // create object with desired ClassId
293 sal_Int64 nAspect = embed::Aspects::MSOLE_CONTENT;
294 ::rtl::OUString aName = ::rtl::OUString::createFromAscii( "DummyName" );
295 uno::Sequence < sal_Int8 > aClass( aClassName.GetByteSequence() );
296 uno::Reference < embed::XEmbedObjectCreator > xFactory( ::comphelper::getProcessServiceFactory()->createInstance(
297 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.embed.EmbeddedObjectCreator")) ), uno::UNO_QUERY );
298 uno::Reference < embed::XEmbeddedObject > xObj =
299 uno::Reference < embed::XEmbeddedObject >( xFactory->createInstanceInitNew(
300 aClass, ::rtl::OUString(), xStorage, aName,
301 uno::Sequence < beans::PropertyValue >() ), uno::UNO_QUERY );
302 if ( xObj.is() )
304 //TODO/LATER: is it enough to only set the VisAreaSize?
305 lcl_setObjectVisualArea( xObj, nAspect, aTwipSize, MAP_TWIP );
308 pFrmFmt = pDoc->Insert( *pTxtCrsr->GetPaM(),
309 ::svt::EmbeddedObjectRef( xObj, embed::Aspects::MSOLE_CONTENT ),
310 &aItemSet,
311 NULL,
312 NULL );
313 pOLENd = lcl_GetOLENode( pFrmFmt );
314 if( pOLENd )
315 aObjName = pOLENd->GetOLEObj().GetCurrentPersistName();
317 catch ( uno::Exception& )
322 else
324 // check whether an object with this name already exists in the document
325 String aName;
326 SwClientIter aIter( *(SwModify*)pDoc->GetDfltGrfFmtColl() );
327 for( SwCntntNode* pNd = (SwCntntNode*)aIter.First( TYPE( SwCntntNode ) );
328 pNd; pNd = (SwCntntNode*)aIter.Next() )
330 SwOLENode* pExistingOLENd = pNd->GetOLENode();
331 if( pExistingOLENd )
333 ::rtl::OUString aExistingName = pExistingOLENd->GetOLEObj().GetCurrentPersistName();
334 if ( aExistingName.equals( aObjName ) )
336 OSL_ENSURE( sal_False, "The document contains duplicate object references, means it is partially broken, please let developers know how this document was generated!\n" );
338 ::rtl::OUString aTmpName = pDoc->GetPersist()->GetEmbeddedObjectContainer().CreateUniqueObjectName();
341 pDoc->GetPersist()->GetStorage()->copyElementTo( aObjName,
342 pDoc->GetPersist()->GetStorage(),
343 aTmpName );
344 aName = aTmpName;
346 catch ( uno::Exception& )
348 OSL_ENSURE( sal_False, "Couldn't create a copy of the object!\n" );
351 break;
356 if ( !aName.Len() )
357 aName = aObjName;
359 // the correct aspect will be set later
360 // TODO/LATER: Actually it should be set here
361 if( pTxtCrsr )
363 pFrmFmt = pDoc->InsertOLE( *pTxtCrsr->GetPaM(), aName, embed::Aspects::MSOLE_CONTENT, &aItemSet, NULL, NULL );
364 pOLENd = lcl_GetOLENode( pFrmFmt );
366 aObjName = aName;
369 if( !pFrmFmt )
370 return xPropSet;
372 if( IsInsertMode() )
374 if( !pOLENd )
375 pOLENd = lcl_GetOLENode( pFrmFmt );
376 if( pOLENd )
377 pOLENd->SetOLESizeInvalid( sal_True );
380 SwXFrame *pXFrame = SwXFrames::GetObject( *pFrmFmt, FLYCNTTYPE_OLE );
381 xPropSet = pXFrame;
382 if( pDoc->GetDrawModel() )
383 SwXFrame::GetOrCreateSdrObject(
384 static_cast<SwFlyFrmFmt*>( pXFrame->GetFrmFmt() ) ); // req for z-order
385 if( rTblName.getLength() )
387 const SwFmtCntnt& rCntnt = pFrmFmt->GetCntnt();
388 const SwNodeIndex *pNdIdx = rCntnt.GetCntntIdx();
389 SwOLENode *pOLENode = pNdIdx->GetNodes()[pNdIdx->GetIndex() + 1]->GetOLENode();
390 ASSERT( pOLENode, "Where is the OLE node" );
392 OUStringBuffer aBuffer( rTblName.getLength() );
393 sal_Bool bQuoted = sal_False;
394 sal_Bool bEscape = sal_False;
395 sal_Bool bError = sal_False;
396 for( sal_Int32 i=0; i < rTblName.getLength(); i++ )
398 sal_Bool bEndOfNameFound = sal_False;
399 sal_Unicode c = rTblName[i];
400 switch( c )
402 case '\'':
403 if( bEscape )
405 aBuffer.append( c );
406 bEscape = sal_False;
408 else if( bQuoted )
410 bEndOfNameFound = sal_True;
412 else if( 0 == i )
414 bQuoted = sal_True;
416 else
418 bError = sal_True;
420 break;
421 case '\\':
422 if( bEscape )
424 aBuffer.append( c );
425 bEscape = sal_False;
427 else
429 bEscape = sal_True;
431 break;
432 case ' ':
433 case '.':
434 if( !bQuoted )
436 bEndOfNameFound = sal_True;
438 else
440 aBuffer.append( c );
441 bEscape = sal_False;
443 break;
444 default:
446 aBuffer.append( c );
447 bEscape = sal_False;
449 break;
451 if( bError || bEndOfNameFound )
452 break;
454 if( !bError )
456 OUString sTblName( aBuffer.makeStringAndClear() );
457 pOLENode->SetChartTblName( GetRenameMap().Get( XML_TEXT_RENAME_TYPE_TABLE, sTblName ) );
461 sal_Int64 nDrawAspect = 0;
462 const XMLPropStyleContext *pStyle = 0;
463 sal_Bool bHasSizeProps = sal_False;
464 if( rStyleName.getLength() )
466 pStyle = FindAutoFrameStyle( rStyleName );
467 if( pStyle )
469 UniReference < SvXMLImportPropertyMapper > xImpPrMap =
470 pStyle->GetStyles()
471 ->GetImportPropertyMapper(pStyle->GetFamily());
472 ASSERT( xImpPrMap.is(), "Where is the import prop mapper?" );
473 if( xImpPrMap.is() )
475 UniReference<XMLPropertySetMapper> rPropMapper =
476 xImpPrMap->getPropertySetMapper();
478 sal_Int32 nCount = pStyle->GetProperties().size();
479 for( sal_Int32 i=0; i < nCount; i++ )
481 const XMLPropertyState& rProp = pStyle->GetProperties()[i];
482 sal_Int32 nIdx = rProp.mnIndex;
483 if( -1 == nIdx )
484 continue;
486 switch( rPropMapper->GetEntryContextId(nIdx) )
488 case CTF_OLE_VIS_AREA_LEFT:
490 sal_Int32 nVal = 0;
491 rProp.maValue >>= nVal;
492 aVisArea.setX( nVal );
494 break;
495 case CTF_OLE_VIS_AREA_TOP:
497 sal_Int32 nVal = 0;
498 rProp.maValue >>= nVal;
499 aVisArea.setY( nVal );
501 break;
502 case CTF_OLE_VIS_AREA_WIDTH:
504 sal_Int32 nVal = 0;
505 rProp.maValue >>= nVal;
506 aVisArea.setWidth( nVal );
507 bHasSizeProps = sal_True;
509 break;
510 case CTF_OLE_VIS_AREA_HEIGHT:
512 sal_Int32 nVal = 0;
513 rProp.maValue >>= nVal;
514 aVisArea.setHeight( nVal );
515 bHasSizeProps = sal_True;
517 break;
518 case CTF_OLE_DRAW_ASPECT:
520 rProp.maValue >>= nDrawAspect;
522 if ( !nDrawAspect )
523 nDrawAspect = embed::Aspects::MSOLE_CONTENT;
525 if ( pOLENd )
526 pOLENd->GetOLEObj().GetObject().SetViewAspect( nDrawAspect );
528 break;
535 if ( bHasSizeProps )
537 uno::Reference < embed::XEmbeddedObject > xObj =
538 pDoc->GetPersist()->GetEmbeddedObjectContainer().GetEmbeddedObject( aObjName );
539 if( xObj.is() )
540 lcl_setObjectVisualArea( xObj, ( nDrawAspect ? nDrawAspect : embed::Aspects::MSOLE_CONTENT ),
541 aVisArea.GetSize(), MAP_100TH_MM );
544 return xPropSet;
547 uno::Reference< XPropertySet > SwXMLTextImportHelper::createAndInsertOOoLink(
548 SvXMLImport& rImport,
549 const OUString& rHRef,
550 const OUString& /*rStyleName*/,
551 const OUString& /*rTblName*/,
552 sal_Int32 nWidth, sal_Int32 nHeight )
554 // this method will modify the document directly -> lock SolarMutex
555 vos::OGuard aGuard(Application::GetSolarMutex());
557 uno::Reference < XPropertySet > xPropSet;
559 uno::Reference<XUnoTunnel> xCrsrTunnel( GetCursor(), UNO_QUERY );
560 ASSERT( xCrsrTunnel.is(), "missing XUnoTunnel for Cursor" );
561 OTextCursorHelper *pTxtCrsr = reinterpret_cast< OTextCursorHelper * >(
562 sal::static_int_cast< sal_IntPtr >( xCrsrTunnel->getSomething( OTextCursorHelper::getUnoTunnelId() )));
563 ASSERT( pTxtCrsr, "SwXTextCursor missing" );
564 SwDoc *pDoc = SwImport::GetDocFromXMLImport( rImport );
566 SfxItemSet aItemSet( pDoc->GetAttrPool(), RES_FRMATR_BEGIN,
567 RES_FRMATR_END );
568 Size aTwipSize( 0, 0 );
569 Rectangle aVisArea( 0, 0, nWidth, nHeight );
570 lcl_putHeightAndWidth( aItemSet, nHeight, nWidth,
571 &aTwipSize.Height(), &aTwipSize.Width() );
573 // We'll need a (valid) URL. If we don't have do not insert the link and return early.
574 // Copy URL into URL oject on the way.
575 INetURLObject aURLObj;
576 bool bValidURL = rHRef.getLength() != 0 &&
577 aURLObj.SetURL( URIHelper::SmartRel2Abs(
578 INetURLObject( GetXMLImport().GetBaseURL() ), rHRef ) );
579 if( !bValidURL )
580 return xPropSet;
582 uno::Reference < embed::XStorage > xStorage = comphelper::OStorageHelper::GetTemporaryStorage();
585 // create object with desired ClassId
586 ::rtl::OUString aName = ::rtl::OUString::createFromAscii( "DummyName" );
587 uno::Reference < embed::XLinkCreator > xFactory( ::comphelper::getProcessServiceFactory()->createInstance(
588 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.embed.OOoEmbeddedObjectFactory")) ),
589 uno::UNO_QUERY_THROW );
591 uno::Sequence< beans::PropertyValue > aMediaDescriptor( 1 );
592 aMediaDescriptor[0].Name = ::rtl::OUString::createFromAscii( "URL" );
593 aMediaDescriptor[0].Value <<= ::rtl::OUString( aURLObj.GetMainURL( INetURLObject::NO_DECODE ) );
594 if ( pDoc && pDoc->GetDocShell() && pDoc->GetDocShell()->GetMedium() )
596 uno::Reference< task::XInteractionHandler > xInteraction =
597 pDoc->GetDocShell()->GetMedium()->GetInteractionHandler();
598 if ( xInteraction.is() )
600 aMediaDescriptor.realloc( 2 );
601 aMediaDescriptor[1].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "InteractionHandler" ) );
602 aMediaDescriptor[1].Value <<= xInteraction;
606 uno::Reference < embed::XEmbeddedObject > xObj(
607 xFactory->createInstanceLink(
608 xStorage, aName, aMediaDescriptor, uno::Sequence< beans::PropertyValue >() ),
609 uno::UNO_QUERY_THROW );
612 SwFrmFmt *pFrmFmt = pDoc->Insert( *pTxtCrsr->GetPaM(),
613 ::svt::EmbeddedObjectRef( xObj, embed::Aspects::MSOLE_CONTENT ),
614 &aItemSet,
615 NULL,
616 NULL );
618 // TODO/LATER: in future may need a way to set replacement image url to the link ( may be even to the object ), needs oasis cws???
620 SwXFrame *pXFrame = SwXFrames::GetObject( *pFrmFmt, FLYCNTTYPE_OLE );
621 xPropSet = pXFrame;
622 if( pDoc->GetDrawModel() )
623 SwXFrame::GetOrCreateSdrObject(
624 static_cast<SwFlyFrmFmt*>( pXFrame->GetFrmFmt() ) ); // req for z-order
627 catch ( uno::Exception& )
631 // TODO/LATER: should the rStyleName and rTblName be handled as for usual embedded object?
633 return xPropSet;
636 uno::Reference< XPropertySet > SwXMLTextImportHelper::createAndInsertApplet(
637 const OUString &rName,
638 const OUString &rCode,
639 sal_Bool bMayScript,
640 const OUString& rHRef,
641 sal_Int32 nWidth, sal_Int32 nHeight )
643 // this method will modify the document directly -> lock SolarMutex
644 vos::OGuard aGuard(Application::GetSolarMutex());
646 uno::Reference < XPropertySet > xPropSet;
647 uno::Reference<XUnoTunnel> xCrsrTunnel( GetCursor(), UNO_QUERY );
648 ASSERT( xCrsrTunnel.is(), "missing XUnoTunnel for Cursor" );
649 OTextCursorHelper *pTxtCrsr = reinterpret_cast< OTextCursorHelper * >(
650 sal::static_int_cast< sal_IntPtr >( xCrsrTunnel->getSomething( OTextCursorHelper::getUnoTunnelId() )));
651 ASSERT( pTxtCrsr, "SwXTextCursor missing" );
652 SwDoc *pDoc = pTxtCrsr->GetDoc();
654 SfxItemSet aItemSet( pDoc->GetAttrPool(), RES_FRMATR_BEGIN,
655 RES_FRMATR_END );
656 lcl_putHeightAndWidth( aItemSet, nHeight, nWidth);
658 SwApplet_Impl aAppletImpl ( aItemSet );
660 String sCodeBase;
661 if( rHRef.getLength() )
662 sCodeBase = GetXMLImport().GetAbsoluteReference( rHRef );
664 aAppletImpl.CreateApplet ( rCode, rName, bMayScript, sCodeBase, GetXMLImport().GetDocumentBase() );
666 // set the size of the applet
667 lcl_setObjectVisualArea( aAppletImpl.GetApplet(),
668 embed::Aspects::MSOLE_CONTENT,
669 Size( nWidth, nHeight ),
670 MAP_100TH_MM );
672 SwFrmFmt *pFrmFmt = pDoc->Insert( *pTxtCrsr->GetPaM(),
673 ::svt::EmbeddedObjectRef( aAppletImpl.GetApplet(), embed::Aspects::MSOLE_CONTENT ),
674 &aAppletImpl.GetItemSet(),
675 NULL,
676 NULL);
677 SwXFrame *pXFrame = SwXFrames::GetObject( *pFrmFmt, FLYCNTTYPE_OLE );
678 xPropSet = pXFrame;
679 if( pDoc->GetDrawModel() )
680 SwXFrame::GetOrCreateSdrObject(
681 static_cast<SwFlyFrmFmt*>( pXFrame->GetFrmFmt() ) ); // req for z-order
683 return xPropSet;
686 uno::Reference< XPropertySet > SwXMLTextImportHelper::createAndInsertPlugin(
687 const OUString &rMimeType,
688 const OUString& rHRef,
689 sal_Int32 nWidth, sal_Int32 nHeight )
691 uno::Reference < XPropertySet > xPropSet;
692 uno::Reference<XUnoTunnel> xCrsrTunnel( GetCursor(), UNO_QUERY );
693 ASSERT( xCrsrTunnel.is(), "missing XUnoTunnel for Cursor" );
694 OTextCursorHelper *pTxtCrsr = reinterpret_cast< OTextCursorHelper * >(
695 sal::static_int_cast< sal_IntPtr >( xCrsrTunnel->getSomething( OTextCursorHelper::getUnoTunnelId() )));
696 ASSERT( pTxtCrsr, "SwXTextCursor missing" );
697 SwDoc *pDoc = pTxtCrsr->GetDoc();
699 SfxItemSet aItemSet( pDoc->GetAttrPool(), RES_FRMATR_BEGIN,
700 RES_FRMATR_END );
701 lcl_putHeightAndWidth( aItemSet, nHeight, nWidth);
703 // We'll need a (valid) URL, or we need a MIME type. If we don't have
704 // either, do not insert plugin and return early. Copy URL into URL oject
705 // on the way.
706 INetURLObject aURLObj;
708 bool bValidURL = rHRef.getLength() != 0 &&
709 aURLObj.SetURL( URIHelper::SmartRel2Abs( INetURLObject( GetXMLImport().GetBaseURL() ), rHRef ) );
710 bool bValidMimeType = rMimeType.getLength() != 0;
711 if( !bValidURL && !bValidMimeType )
712 return xPropSet;
714 uno::Reference < embed::XStorage > xStorage = comphelper::OStorageHelper::GetTemporaryStorage();
717 // create object with desired ClassId
718 ::rtl::OUString aName = ::rtl::OUString::createFromAscii( "DummyName" );
719 uno::Sequence < sal_Int8 > aClass( SvGlobalName( SO3_PLUGIN_CLASSID ).GetByteSequence() );
720 uno::Reference < embed::XEmbedObjectCreator > xFactory( ::comphelper::getProcessServiceFactory()->createInstance(
721 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.embed.EmbeddedObjectCreator")) ), uno::UNO_QUERY );
722 uno::Reference < embed::XEmbeddedObject > xObj =
723 uno::Reference < embed::XEmbeddedObject >( xFactory->createInstanceInitNew(
724 aClass, ::rtl::OUString(), xStorage, aName,
725 uno::Sequence < beans::PropertyValue >() ), uno::UNO_QUERY );
727 // set size to the object
728 lcl_setObjectVisualArea( xObj,
729 embed::Aspects::MSOLE_CONTENT,
730 Size( nWidth, nHeight ),
731 MAP_100TH_MM );
733 if ( svt::EmbeddedObjectRef::TryRunningState( xObj ) )
735 uno::Reference < beans::XPropertySet > xSet( xObj->getComponent(), uno::UNO_QUERY );
736 if ( xSet.is() )
738 if( bValidURL )
739 xSet->setPropertyValue( ::rtl::OUString::createFromAscii("PluginURL"),
740 makeAny( ::rtl::OUString( aURLObj.GetMainURL( INetURLObject::NO_DECODE ) ) ) );
741 if( bValidMimeType )
742 xSet->setPropertyValue( ::rtl::OUString::createFromAscii("PluginMimeType"),
743 makeAny( ::rtl::OUString( rMimeType ) ) );
746 SwFrmFmt *pFrmFmt = pDoc->Insert( *pTxtCrsr->GetPaM(),
747 ::svt::EmbeddedObjectRef( xObj, embed::Aspects::MSOLE_CONTENT ),
748 &aItemSet,
749 NULL,
750 NULL);
751 SwXFrame *pXFrame = SwXFrames::GetObject( *pFrmFmt, FLYCNTTYPE_OLE );
752 xPropSet = pXFrame;
753 if( pDoc->GetDrawModel() )
754 SwXFrame::GetOrCreateSdrObject(
755 static_cast<SwFlyFrmFmt*>( pXFrame->GetFrmFmt() ) ); // req for z-order
758 catch ( uno::Exception& )
762 return xPropSet;
764 uno::Reference< XPropertySet > SwXMLTextImportHelper::createAndInsertFloatingFrame(
765 const OUString& rName,
766 const OUString& rHRef,
767 const OUString& rStyleName,
768 sal_Int32 nWidth, sal_Int32 nHeight )
770 // this method will modify the document directly -> lock SolarMutex
771 vos::OGuard aGuard(Application::GetSolarMutex());
773 uno::Reference < XPropertySet > xPropSet;
774 uno::Reference<XUnoTunnel> xCrsrTunnel( GetCursor(), UNO_QUERY );
775 ASSERT( xCrsrTunnel.is(), "missing XUnoTunnel for Cursor" );
776 OTextCursorHelper *pTxtCrsr = reinterpret_cast< OTextCursorHelper * >(
777 sal::static_int_cast< sal_IntPtr >( xCrsrTunnel->getSomething( OTextCursorHelper::getUnoTunnelId() )));
778 ASSERT( pTxtCrsr, "SwXTextCursor missing" );
779 SwDoc *pDoc = pTxtCrsr->GetDoc();
781 SfxItemSet aItemSet( pDoc->GetAttrPool(), RES_FRMATR_BEGIN,
782 RES_FRMATR_END );
783 lcl_putHeightAndWidth( aItemSet, nHeight, nWidth);
785 ScrollingMode eScrollMode = ScrollingAuto;
786 sal_Bool bHasBorder = sal_False;
787 sal_Bool bIsBorderSet = sal_False;
788 Size aMargin( SIZE_NOT_SET, SIZE_NOT_SET );
789 const XMLPropStyleContext *pStyle = 0;
790 if( rStyleName.getLength() )
792 pStyle = FindAutoFrameStyle( rStyleName );
793 if( pStyle )
795 UniReference < SvXMLImportPropertyMapper > xImpPrMap =
796 pStyle->GetStyles()
797 ->GetImportPropertyMapper(pStyle->GetFamily());
798 ASSERT( xImpPrMap.is(), "Where is the import prop mapper?" );
799 if( xImpPrMap.is() )
801 UniReference<XMLPropertySetMapper> rPropMapper =
802 xImpPrMap->getPropertySetMapper();
804 sal_Int32 nCount = pStyle->GetProperties().size();
805 for( sal_Int32 i=0; i < nCount; i++ )
807 const XMLPropertyState& rProp = pStyle->GetProperties()[i];
808 sal_Int32 nIdx = rProp.mnIndex;
809 if( -1 == nIdx )
810 continue;
812 switch( rPropMapper->GetEntryContextId(nIdx) )
814 case CTF_FRAME_DISPLAY_SCROLLBAR:
816 sal_Bool bYes = *(sal_Bool *)rProp.maValue.getValue();
817 eScrollMode = bYes ? ScrollingYes : ScrollingNo;
819 break;
820 case CTF_FRAME_DISPLAY_BORDER:
822 bHasBorder = *(sal_Bool *)rProp.maValue.getValue();
823 bIsBorderSet = sal_True;
825 break;
826 case CTF_FRAME_MARGIN_HORI:
828 sal_Int32 nVal = SIZE_NOT_SET;
829 rProp.maValue >>= nVal;
830 aMargin.Width() = nVal;
832 break;
833 case CTF_FRAME_MARGIN_VERT:
835 sal_Int32 nVal = SIZE_NOT_SET;
836 rProp.maValue >>= nVal;
837 aMargin.Height() = nVal;
839 break;
846 uno::Reference < embed::XStorage > xStorage = comphelper::OStorageHelper::GetTemporaryStorage();
849 // create object with desired ClassId
850 ::rtl::OUString aName = ::rtl::OUString::createFromAscii( "DummyName" );
851 uno::Sequence < sal_Int8 > aClass( SvGlobalName( SO3_IFRAME_CLASSID ).GetByteSequence() );
852 uno::Reference < embed::XEmbedObjectCreator > xFactory( ::comphelper::getProcessServiceFactory()->createInstance(
853 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.embed.EmbeddedObjectCreator")) ), uno::UNO_QUERY );
854 uno::Reference < embed::XEmbeddedObject > xObj =
855 uno::Reference < embed::XEmbeddedObject >( xFactory->createInstanceInitNew(
856 aClass, ::rtl::OUString(), xStorage, aName,
857 uno::Sequence < beans::PropertyValue >() ), uno::UNO_QUERY );
859 // set size to the object
860 lcl_setObjectVisualArea( xObj,
861 embed::Aspects::MSOLE_CONTENT,
862 Size( nWidth, nHeight ),
863 MAP_100TH_MM );
865 if ( svt::EmbeddedObjectRef::TryRunningState( xObj ) )
867 uno::Reference < beans::XPropertySet > xSet( xObj->getComponent(), uno::UNO_QUERY );
868 if ( xSet.is() )
870 xSet->setPropertyValue( ::rtl::OUString::createFromAscii("FrameURL"),
871 makeAny( ::rtl::OUString( URIHelper::SmartRel2Abs(
872 INetURLObject( GetXMLImport().GetBaseURL() ), rHRef ) ) ) );
874 xSet->setPropertyValue( ::rtl::OUString::createFromAscii("FrameName"),
875 makeAny( ::rtl::OUString( rName ) ) );
877 if ( eScrollMode == ScrollingAuto )
878 xSet->setPropertyValue( ::rtl::OUString::createFromAscii("FrameIsAutoScroll"),
879 makeAny( sal_True ) );
880 else
881 xSet->setPropertyValue( ::rtl::OUString::createFromAscii("FrameIsScrollingMode"),
882 makeAny( (sal_Bool) (eScrollMode == ScrollingYes) ) );
884 if ( bIsBorderSet )
885 xSet->setPropertyValue( ::rtl::OUString::createFromAscii("FrameIsBorder"),
886 makeAny( bHasBorder ) );
887 else
888 xSet->setPropertyValue( ::rtl::OUString::createFromAscii("FrameIsAutoBorder"),
889 makeAny( sal_True ) );
891 xSet->setPropertyValue( ::rtl::OUString::createFromAscii("FrameMarginWidth"),
892 makeAny( sal_Int32( aMargin.Width() ) ) );
894 xSet->setPropertyValue( ::rtl::OUString::createFromAscii("FrameMarginHeight"),
895 makeAny( sal_Int32( aMargin.Height() ) ) );
898 SwFrmFmt *pFrmFmt = pDoc->Insert( *pTxtCrsr->GetPaM(),
899 ::svt::EmbeddedObjectRef( xObj, embed::Aspects::MSOLE_CONTENT ),
900 &aItemSet,
901 NULL,
902 NULL);
903 SwXFrame *pXFrame = SwXFrames::GetObject( *pFrmFmt, FLYCNTTYPE_OLE );
904 xPropSet = pXFrame;
905 if( pDoc->GetDrawModel() )
906 SwXFrame::GetOrCreateSdrObject(
907 static_cast<SwFlyFrmFmt*>( pXFrame->GetFrmFmt() ) ); // req for z-order
910 catch ( uno::Exception& )
914 return xPropSet;
917 void SwXMLTextImportHelper::endAppletOrPlugin(
918 const uno::Reference < XPropertySet > &rPropSet,
919 ::std::map < const ::rtl::OUString, ::rtl::OUString, ::comphelper::UStringLess > &rParamMap)
921 // this method will modify the document directly -> lock SolarMutex
922 vos::OGuard aGuard(Application::GetSolarMutex());
924 uno::Reference<XUnoTunnel> xCrsrTunnel( rPropSet, UNO_QUERY );
925 ASSERT( xCrsrTunnel.is(), "missing XUnoTunnel for embedded" );
926 SwXFrame *pFrame = reinterpret_cast< SwXFrame * >(
927 sal::static_int_cast< sal_IntPtr >( xCrsrTunnel->getSomething( SwXFrame::getUnoTunnelId() )));
928 ASSERT( pFrame, "SwXFrame missing" );
929 SwFrmFmt *pFrmFmt = pFrame->GetFrmFmt();
930 const SwFmtCntnt& rCntnt = pFrmFmt->GetCntnt();
931 const SwNodeIndex *pNdIdx = rCntnt.GetCntntIdx();
932 SwOLENode *pOLENd = pNdIdx->GetNodes()[pNdIdx->GetIndex() + 1]->GetNoTxtNode()->GetOLENode();
933 SwOLEObj& rOLEObj = pOLENd->GetOLEObj();
935 uno::Reference < embed::XEmbeddedObject > xEmbObj( rOLEObj.GetOleRef() );
936 if ( svt::EmbeddedObjectRef::TryRunningState( xEmbObj ) )
938 uno::Reference < beans::XPropertySet > xSet( xEmbObj->getComponent(), uno::UNO_QUERY );
939 if ( xSet.is() )
941 const sal_Int32 nCount = rParamMap.size();
942 uno::Sequence< beans::PropertyValue > aCommandSequence( nCount );
944 ::std::map < const ::rtl::OUString, ::rtl::OUString, ::comphelper::UStringLess > ::iterator aIter = rParamMap.begin();
945 ::std::map < const ::rtl::OUString, ::rtl::OUString, ::comphelper::UStringLess > ::iterator aEnd = rParamMap.end();
946 sal_Int32 nIndex=0;
947 while (aIter != aEnd )
949 aCommandSequence[nIndex].Name = (*aIter).first;
950 aCommandSequence[nIndex].Handle = -1;
951 aCommandSequence[nIndex].Value = makeAny( OUString((*aIter).second) );
952 aCommandSequence[nIndex].State = beans::PropertyState_DIRECT_VALUE;
953 aIter++, nIndex++;
956 // unfortunately the names of the properties are depending on the object
957 ::rtl::OUString aParaName = ::rtl::OUString::createFromAscii("AppletCommands");
960 xSet->setPropertyValue( aParaName, makeAny( aCommandSequence ) );
962 catch ( uno::Exception& )
964 aParaName = ::rtl::OUString::createFromAscii("PluginCommands");
967 xSet->setPropertyValue( aParaName, makeAny( aCommandSequence ) );
969 catch ( uno::Exception& )
977 XMLTextImportHelper* SwXMLImport::CreateTextImport()
979 return new SwXMLTextImportHelper( GetModel(), *this, getImportInfo(),
980 IsInsertMode(),
981 IsStylesOnlyMode(), bShowProgress,
982 IsBlockMode(), IsOrganizerMode(),
983 bPreserveRedlineMode );
987 // redlining helper methods
988 // (override to provide the real implementation)
990 void SwXMLTextImportHelper::RedlineAdd(
991 const OUString& rType,
992 const OUString& rId,
993 const OUString& rAuthor,
994 const OUString& rComment,
995 const util::DateTime& rDateTime,
996 sal_Bool bMergeLastPara)
998 // create redline helper on demand
999 DBG_ASSERT(NULL != pRedlineHelper, "helper should have been created in constructor");
1000 if (NULL != pRedlineHelper)
1001 pRedlineHelper->Add(rType, rId, rAuthor, rComment, rDateTime,
1002 bMergeLastPara);
1005 uno::Reference<XTextCursor> SwXMLTextImportHelper::RedlineCreateText(
1006 uno::Reference<XTextCursor> & rOldCursor,
1007 const OUString& rId)
1009 uno::Reference<XTextCursor> xRet;
1011 if (NULL != pRedlineHelper)
1013 xRet = pRedlineHelper->CreateRedlineTextSection(rOldCursor, rId);
1016 return xRet;
1019 void SwXMLTextImportHelper::RedlineSetCursor(
1020 const OUString& rId,
1021 sal_Bool bStart,
1022 sal_Bool bIsOutsideOfParagraph)
1024 if (NULL != pRedlineHelper) {
1025 uno::Reference<XTextRange> xTextRange( GetCursor()->getStart() );
1026 pRedlineHelper->SetCursor(rId, bStart, xTextRange,
1027 bIsOutsideOfParagraph);
1029 // else: ignore redline (wasn't added before, else we'd have a helper)
1032 void SwXMLTextImportHelper::RedlineAdjustStartNodeCursor(
1033 sal_Bool bStart)
1035 OUString rId = GetOpenRedlineId();
1036 if ((NULL != pRedlineHelper) && (rId.getLength() > 0))
1038 uno::Reference<XTextRange> xTextRange( GetCursor()->getStart() );
1039 pRedlineHelper->AdjustStartNodeCursor(rId, bStart, xTextRange );
1040 ResetOpenRedlineId();
1042 // else: ignore redline (wasn't added before, or no open redline ID
1045 void SwXMLTextImportHelper::SetShowChanges( sal_Bool bShowChanges )
1047 if ( NULL != pRedlineHelper )
1048 pRedlineHelper->SetShowChanges( bShowChanges );
1051 void SwXMLTextImportHelper::SetRecordChanges( sal_Bool bRecordChanges )
1053 if ( NULL != pRedlineHelper )
1054 pRedlineHelper->SetRecordChanges( bRecordChanges );
1057 void SwXMLTextImportHelper::SetChangesProtectionKey(
1058 const Sequence<sal_Int8> & rKey )
1060 if ( NULL != pRedlineHelper )
1061 pRedlineHelper->SetProtectionKey( rKey );