xref: /trunk/main/chart2/source/model/main/ChartModel_Persistence.cxx (revision 46c69c84579d1f1df536015e7d54c9965db90b69)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_chartmodel.hxx"
26 
27 #include "ChartModel.hxx"
28 #include "MediaDescriptorHelper.hxx"
29 #include "ChartDebugTrace.hxx"
30 #include "macros.hxx"
31 #include "ChartViewHelper.hxx"
32 #include "ChartModelHelper.hxx"
33 #include "AxisHelper.hxx"
34 #include "ThreeDHelper.hxx"
35 
36 #include <com/sun/star/chart2/LegendPosition.hpp>
37 #include <com/sun/star/container/XNameAccess.hpp>
38 #include <com/sun/star/document/XExporter.hpp>
39 #include <com/sun/star/document/XImporter.hpp>
40 #include <com/sun/star/document/XFilter.hpp>
41 #include <com/sun/star/drawing/FillStyle.hpp>
42 #include <com/sun/star/drawing/LineStyle.hpp>
43 #include <com/sun/star/drawing/ProjectionMode.hpp>
44 #include <com/sun/star/embed/ElementModes.hpp>
45 #include <com/sun/star/embed/XStorage.hpp>
46 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
47 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
48 #include <com/sun/star/uno/XComponentContext.hpp>
49 #include <com/sun/star/io/XSeekable.hpp>
50 
51 #include <ucbhelper/content.hxx>
52 #ifndef _UNOTOOLS_UCBSTREAMHELPER_HXX
53 #include <unotools/ucbstreamhelper.hxx>
54 #endif
55 #include <vcl/cvtgrf.hxx>
56 #include <comphelper/storagehelper.hxx>
57 #include <vcl/svapp.hxx>
58 
59 #include <algorithm>
60 #include <functional>
61 
62 using namespace ::com::sun::star;
63 
64 using ::com::sun::star::uno::Reference;
65 using ::com::sun::star::uno::Sequence;
66 using ::rtl::OUString;
67 using ::osl::MutexGuard;
68 
69 namespace
70 {
71 struct lcl_PropNameEquals
72 {
73     typedef beans::PropertyValue argument_type;
74     typedef bool result_type;
lcl_PropNameEquals__anon18599e650111::lcl_PropNameEquals75     lcl_PropNameEquals( const OUString & rStrToCompareWith ) :
76             m_aStr( rStrToCompareWith )
77     {}
operator ()__anon18599e650111::lcl_PropNameEquals78     bool operator() ( const beans::PropertyValue & rProp )
79     {
80         return rProp.Name.equals( m_aStr );
81     }
82 private:
83     OUString m_aStr;
84 };
85 
86 template< typename T >
lcl_getProperty(const Sequence<beans::PropertyValue> & rMediaDescriptor,const OUString & rPropName)87 T lcl_getProperty(
88     const Sequence< beans::PropertyValue > & rMediaDescriptor,
89     const OUString & rPropName )
90 {
91     T aResult;
92     if( rMediaDescriptor.getLength())
93     {
94         OUString aPropName( rPropName );
95         const beans::PropertyValue * pIt = rMediaDescriptor.getConstArray();
96         const beans::PropertyValue * pEndIt = pIt +  + rMediaDescriptor.getLength();
97         pIt = ::std::find_if( pIt, pEndIt, lcl_PropNameEquals( aPropName ));
98         if( pIt != pEndIt )
99             (*pIt).Value >>= aResult;
100     }
101     return aResult;
102 }
103 
lcl_addStorageToMediaDescriptor(Sequence<beans::PropertyValue> & rOutMD,const Reference<embed::XStorage> & xStorage)104 void lcl_addStorageToMediaDescriptor(
105     Sequence< beans::PropertyValue > & rOutMD,
106     const Reference< embed::XStorage > & xStorage )
107 {
108     rOutMD.realloc( rOutMD.getLength() + 1 );
109     rOutMD[rOutMD.getLength() - 1] = beans::PropertyValue(
110         C2U("Storage"), -1, uno::makeAny( xStorage ), beans::PropertyState_DIRECT_VALUE );
111 }
112 
lcl_createStorage(const OUString & rURL,const Reference<uno::XComponentContext> & xContext,const Sequence<beans::PropertyValue> & rMediaDescriptor)113 Reference< embed::XStorage > lcl_createStorage(
114     const OUString & rURL,
115     const Reference< uno::XComponentContext > & xContext,
116     const Sequence< beans::PropertyValue > & rMediaDescriptor )
117 {
118     // create new storage
119     Reference< embed::XStorage > xStorage;
120     if( !xContext.is())
121         return xStorage;
122 
123     try
124     {
125         Reference< io::XStream > xStream(
126             ::ucbhelper::Content( rURL, Reference< ::com::sun::star::ucb::XCommandEnvironment >()).openStream(),
127             uno::UNO_QUERY );
128 
129         Reference< lang::XSingleServiceFactory > xStorageFact(
130             xContext->getServiceManager()->createInstanceWithContext(
131                 C2U("com.sun.star.embed.StorageFactory"),
132                 xContext ),
133             uno::UNO_QUERY_THROW );
134         Sequence< uno::Any > aStorageArgs( 3 );
135         aStorageArgs[0] <<= xStream;
136         aStorageArgs[1] <<= embed::ElementModes::READWRITE;
137         aStorageArgs[2] <<= rMediaDescriptor;
138         xStorage.set(
139             xStorageFact->createInstanceWithArguments( aStorageArgs ), uno::UNO_QUERY_THROW );
140         OSL_ENSURE( xStorage.is(), "No Storage" );
141     }
142     catch( ::com::sun::star::ucb::ContentCreationException & rEx )
143     {
144         ASSERT_EXCEPTION( rEx );
145     }
146 
147     return xStorage;
148 }
149 
150 } // anonymous namespace
151 
152 namespace chart
153 {
154 
impl_createFilter(const Sequence<beans::PropertyValue> & rMediaDescriptor)155 Reference< document::XFilter > ChartModel::impl_createFilter(
156     const Sequence< beans::PropertyValue > & rMediaDescriptor )
157 {
158     Reference< document::XFilter > xFilter;
159 
160     // find FilterName in MediaDescriptor
161     OUString aFilterName(
162         lcl_getProperty< OUString >( rMediaDescriptor, OUString::createFromAscii("FilterName")));
163 
164     // if FilterName was found, get Filter from factory
165     if( !aFilterName.isEmpty() )
166     {
167         try
168         {
169             Reference< container::XNameAccess > xFilterFact(
170                 m_xContext->getServiceManager()->createInstanceWithContext(
171                     C2U( "com.sun.star.document.FilterFactory" ), m_xContext ),
172                 uno::UNO_QUERY_THROW );
173             uno::Any aFilterProps( xFilterFact->getByName( aFilterName ));
174             Sequence< beans::PropertyValue > aProps;
175 
176             if( aFilterProps.hasValue() &&
177                 (aFilterProps >>= aProps))
178             {
179                 OUString aFilterServiceName(
180                     lcl_getProperty< OUString >( aProps, OUString::createFromAscii("FilterService")));
181 
182                 if( !aFilterServiceName.isEmpty() )
183                 {
184                     xFilter.set(
185                         m_xContext->getServiceManager()->createInstanceWithContext(
186                             aFilterServiceName, m_xContext ), uno::UNO_QUERY_THROW );
187                     OSL_TRACE( "Filter found for service %s", U2C( aFilterServiceName ));
188                 }
189             }
190         }
191         catch( uno::Exception & ex )
192         {
193             ASSERT_EXCEPTION( ex );
194         }
195         OSL_ENSURE( xFilter.is(), "Filter not found via factory" );
196     }
197 
198     // fall-back: create XML-Filter
199     if( ! xFilter.is())
200     {
201         OSL_TRACE( "No FilterName passed in MediaDescriptor" );
202         xFilter.set(
203             m_xContext->getServiceManager()->createInstanceWithContext(
204                 C2U("com.sun.star.comp.chart2.XMLFilter"), m_xContext ),
205             uno::UNO_QUERY_THROW );
206     }
207 
208     return xFilter;
209 }
210 
211 //-----------------------------------------------------------------
212 // frame::XStorable2
213 //-----------------------------------------------------------------
214 
storeSelf(const Sequence<beans::PropertyValue> & rMediaDescriptor)215 void SAL_CALL ChartModel::storeSelf( const Sequence< beans::PropertyValue >& rMediaDescriptor )
216 {
217     // only some parameters are allowed (see also SfxBaseModel)
218     // "VersionComment", "Author", "InteractionHandler", "StatusIndicator"
219     // However, they are ignored here.  They would become interesting when
220     // charts support a standalone format again.
221     impl_store( rMediaDescriptor, m_xStorage );
222 }
223 
224 //-----------------------------------------------------------------
225 // frame::XStorable (base of XStorable2)
226 //-----------------------------------------------------------------
hasLocation()227 sal_Bool SAL_CALL ChartModel::hasLocation()
228 {
229     //@todo guard
230     return !m_aResource.isEmpty();
231 }
232 
getLocation()233 ::rtl::OUString SAL_CALL ChartModel::getLocation()
234 {
235     return impl_g_getLocation();
236 }
237 
isReadonly()238 sal_Bool SAL_CALL ChartModel::isReadonly()
239 {
240     //@todo guard
241     return m_bReadOnly;
242 }
243 
store()244 void SAL_CALL ChartModel::store()
245 {
246     apphelper::LifeTimeGuard aGuard(m_aLifeTimeManager);
247     if(!aGuard.startApiCall(sal_True)) //start LongLastingCall
248         return; //behave passive if already disposed or closed or throw exception @todo?
249 
250     ::rtl::OUString aLocation = m_aResource;
251 
252     if( aLocation.isEmpty() )
253         throw io::IOException( C2U( "no location specified" ), static_cast< ::cppu::OWeakObject* >(this));
254     //@todo check whether aLocation is something like private:factory...
255     if( m_bReadOnly )
256         throw io::IOException( C2U( "document is read only" ), static_cast< ::cppu::OWeakObject* >(this));
257 
258     aGuard.clear();
259 
260     // store
261     impl_store( m_aMediaDescriptor, m_xStorage );
262 }
263 
storeAsURL(const::rtl::OUString & rURL,const uno::Sequence<beans::PropertyValue> & rMediaDescriptor)264 void SAL_CALL ChartModel::storeAsURL(
265     const ::rtl::OUString& rURL,
266     const uno::Sequence< beans::PropertyValue >& rMediaDescriptor )
267 {
268     apphelper::LifeTimeGuard aGuard(m_aLifeTimeManager);
269     if(!aGuard.startApiCall(sal_True)) //start LongLastingCall
270         return; //behave passive if already disposed or closed or throw exception @todo?
271 
272     apphelper::MediaDescriptorHelper aMediaDescriptorHelper(rMediaDescriptor);
273     uno::Sequence< beans::PropertyValue > aReducedMediaDescriptor(
274         aMediaDescriptorHelper.getReducedForModel() );
275 
276     m_bReadOnly = sal_False;
277     aGuard.clear();
278 
279     // create new storage
280     Reference< embed::XStorage > xStorage( lcl_createStorage( rURL, m_xContext, aReducedMediaDescriptor ));
281 
282     if( xStorage.is())
283     {
284         impl_store( aReducedMediaDescriptor, xStorage );
285         attachResource( rURL, aReducedMediaDescriptor );
286     }
287 }
288 
storeToURL(const::rtl::OUString & rURL,const uno::Sequence<beans::PropertyValue> & rMediaDescriptor)289 void SAL_CALL ChartModel::storeToURL(
290     const ::rtl::OUString& rURL,
291     const uno::Sequence< beans::PropertyValue >& rMediaDescriptor )
292 {
293     apphelper::LifeTimeGuard aGuard(m_aLifeTimeManager);
294     if(!aGuard.startApiCall(sal_True)) //start LongLastingCall
295         return; //behave passive if already disposed or closed or throw exception @todo?
296     //do not change the internal state of the document here
297     //...
298 
299     aGuard.clear();
300 
301     apphelper::MediaDescriptorHelper aMediaDescriptorHelper(rMediaDescriptor);
302     uno::Sequence< beans::PropertyValue > aReducedMediaDescriptor(
303         aMediaDescriptorHelper.getReducedForModel() );
304 
305     if( rURL.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("private:stream")))
306     {
307         try
308         {
309             if( m_xContext.is() && aMediaDescriptorHelper.ISSET_OutputStream )
310             {
311                 Reference< lang::XMultiServiceFactory > xFact( m_xContext->getServiceManager(), uno::UNO_QUERY_THROW );
312                 Reference< io::XStream > xStream(
313                     xFact->createInstance( OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.io.TempFile"))), uno::UNO_QUERY_THROW );
314                 Reference< io::XInputStream > xInputStream( xStream->getInputStream());
315 
316                 Reference< embed::XStorage > xStorage(
317                     ::comphelper::OStorageHelper::GetStorageFromStream( xStream, embed::ElementModes::READWRITE, xFact ));
318                 if( xStorage.is())
319                 {
320                     impl_store( aReducedMediaDescriptor, xStorage );
321 
322                     Reference< io::XSeekable > xSeekable( xStream, uno::UNO_QUERY_THROW );
323                     xSeekable->seek( 0 );
324                     ::comphelper::OStorageHelper::CopyInputToOutput( xInputStream, aMediaDescriptorHelper.OutputStream );
325                 }
326             }
327         }
328         catch( const uno::Exception & ex )
329         {
330             ASSERT_EXCEPTION( ex );
331         }
332     }
333     else
334     {
335         // create new storage
336         Reference< embed::XStorage > xStorage( lcl_createStorage( rURL, m_xContext, aReducedMediaDescriptor ));
337 
338         if( xStorage.is())
339             impl_store( aReducedMediaDescriptor, xStorage );
340     }
341 }
342 
impl_store(const Sequence<beans::PropertyValue> & rMediaDescriptor,const Reference<embed::XStorage> & xStorage)343 void ChartModel::impl_store(
344     const Sequence< beans::PropertyValue >& rMediaDescriptor,
345     const Reference< embed::XStorage > & xStorage )
346 {
347     Reference< document::XFilter > xFilter( impl_createFilter( rMediaDescriptor));
348     if( xFilter.is() && xStorage.is())
349     {
350         Sequence< beans::PropertyValue > aMD( rMediaDescriptor );
351         lcl_addStorageToMediaDescriptor( aMD, xStorage );
352         try
353         {
354             Reference< document::XExporter > xExporter( xFilter, uno::UNO_QUERY_THROW );
355             xExporter->setSourceDocument( Reference< lang::XComponent >( this ));
356             xFilter->filter( aMD );
357         }
358         catch( uno::Exception & ex )
359         {
360             ASSERT_EXCEPTION( ex );
361         }
362     }
363     else
364     {
365         OSL_ENSURE( false, "No filter" );
366     }
367 
368     setModified( sal_False );
369 
370     //#i66865#
371     //for data change notification during chart is not loaded:
372     //notify parent data provider after saving thus the parent document can store
373     //the ranges for which a load and update of the chart will be necessary
374     Reference< beans::XPropertySet > xPropSet( m_xParent, uno::UNO_QUERY );
375     if ( !hasInternalDataProvider() && xPropSet.is() )
376     {
377         apphelper::MediaDescriptorHelper aMDHelper(rMediaDescriptor);
378         try
379         {
380             xPropSet->setPropertyValue( OUString::createFromAscii("SavedObject"),
381                 uno::makeAny( aMDHelper.HierarchicalDocumentName ) );
382         }
383         catch ( uno::Exception& )
384         {
385         }
386     }
387 }
388 
389 //-----------------------------------------------------------------
390 // frame::XLoadable
391 //-----------------------------------------------------------------
initNew()392 void SAL_CALL ChartModel::initNew()
393 {
394     lockControllers();
395     createInternalDataProvider( sal_False );
396     try
397     {
398         // create default chart
399         Reference< chart2::XChartTypeTemplate > xTemplate( impl_createDefaultChartTypeTemplate() );
400         if( xTemplate.is())
401         {
402             try
403             {
404                 Reference< chart2::data::XDataSource > xDataSource( impl_createDefaultData() );
405                 Sequence< beans::PropertyValue > aParam;
406 
407                 bool bSupportsCategories = xTemplate->supportsCategories();
408                 if( bSupportsCategories )
409                 {
410                     aParam.realloc( 1 );
411                     aParam[0] = beans::PropertyValue( C2U("HasCategories"), -1, uno::makeAny( true ),
412                                                       beans::PropertyState_DIRECT_VALUE );
413                 }
414 
415                 Reference< chart2::XDiagram > xDiagram( xTemplate->createDiagramByDataSource( xDataSource, aParam ) );
416 
417                 setFirstDiagram( xDiagram );
418 
419                 bool bIsRTL = Application::GetSettings().GetLayoutRTL();
420                 //reverse x axis for rtl charts
421                 if( bIsRTL )
422                     AxisHelper::setRTLAxisLayout( AxisHelper::getCoordinateSystemByIndex( xDiagram, 0 ) );
423 
424                 // create and attach legend
425                 Reference< chart2::XLegend > xLegend(
426                     m_xContext->getServiceManager()->createInstanceWithContext(
427                         C2U( "com.sun.star.chart2.Legend" ), m_xContext ), uno::UNO_QUERY_THROW );
428                 Reference< beans::XPropertySet > xLegendProperties( xLegend, uno::UNO_QUERY );
429                 if( xLegendProperties.is() )
430                 {
431                     xLegendProperties->setPropertyValue( C2U( "FillStyle" ), uno::makeAny( drawing::FillStyle_NONE ));
432                     xLegendProperties->setPropertyValue( C2U( "LineStyle" ), uno::makeAny( drawing::LineStyle_NONE ));
433                     xLegendProperties->setPropertyValue( C2U( "LineColor" ), uno::makeAny( static_cast< sal_Int32 >( 0xb3b3b3 ) ));  // gray30
434                     xLegendProperties->setPropertyValue( C2U( "FillColor" ), uno::makeAny( static_cast< sal_Int32 >( 0xe6e6e6 ) ) ); // gray10
435 
436                     if( bIsRTL )
437                         xLegendProperties->setPropertyValue( C2U( "AnchorPosition" ), uno::makeAny( chart2::LegendPosition_LINE_START ));
438                 }
439                 if(xDiagram.is())
440                     xDiagram->setLegend( xLegend );
441 
442                 // set simple 3D look
443                 Reference< beans::XPropertySet > xDiagramProperties( xDiagram, uno::UNO_QUERY );
444                 if( xDiagramProperties.is() )
445                 {
446                     xDiagramProperties->setPropertyValue( C2U("RightAngledAxes"), uno::makeAny( sal_True ));
447                     xDiagramProperties->setPropertyValue( C2U("D3DScenePerspective"), uno::makeAny( drawing::ProjectionMode_PARALLEL ));
448                     ThreeDHelper::setScheme( xDiagram, ThreeDLookScheme_Realistic );
449                 }
450 
451                 //set some new 'defaults' for wall and floor
452                 if( xDiagram.is() )
453                 {
454                     Reference< beans::XPropertySet > xWall( xDiagram->getWall() );
455                     if( xWall.is() )
456                     {
457                         xWall->setPropertyValue( C2U( "LineStyle" ), uno::makeAny( drawing::LineStyle_SOLID ) );
458                         xWall->setPropertyValue( C2U( "FillStyle" ), uno::makeAny( drawing::FillStyle_NONE ) );
459                         xWall->setPropertyValue( C2U( "LineColor" ), uno::makeAny( static_cast< sal_Int32 >( 0xb3b3b3 ) ) ); // gray30
460                         xWall->setPropertyValue( C2U( "FillColor" ), uno::makeAny( static_cast< sal_Int32 >( 0xe6e6e6 ) ) ); // gray10
461                     }
462                     Reference< beans::XPropertySet > xFloor( xDiagram->getFloor() );
463                     if( xFloor.is() )
464                     {
465                         xFloor->setPropertyValue( C2U( "LineStyle" ), uno::makeAny( drawing::LineStyle_NONE ) );
466                         xFloor->setPropertyValue( C2U( "FillStyle" ), uno::makeAny( drawing::FillStyle_SOLID ) );
467                         xFloor->setPropertyValue( C2U( "LineColor" ), uno::makeAny( static_cast< sal_Int32 >( 0xb3b3b3 ) ) ); // gray30
468                         xFloor->setPropertyValue( C2U( "FillColor" ), uno::makeAny( static_cast< sal_Int32 >( 0xcccccc ) ) ); // gray20
469                     }
470 
471                 }
472             }
473             catch( uno::Exception & ex )
474             {
475                 ASSERT_EXCEPTION( ex );
476             }
477         }
478         ChartModelHelper::setIncludeHiddenCells( false, this );
479     }
480     catch( uno::Exception & ex )
481     {
482         ASSERT_EXCEPTION( ex );
483     }
484     setModified( sal_False );
485     unlockControllers();
486 
487 #if OSL_DEBUG_LEVEL >= CHART_TRACE_OSL_DEBUG_LEVEL
488     OSL_TRACE( "ChartModel::initNew: Showing ChartDocument structure" );
489     OSL_TRACE( "----------------------------------------------------" );
490     debug::ChartDebugTraceDocument( Reference< chart2::XChartDocument >( this ));
491 #endif
492 }
493 
load(const Sequence<beans::PropertyValue> & rMediaDescriptor)494 void SAL_CALL ChartModel::load(
495     const Sequence< beans::PropertyValue >& rMediaDescriptor )
496 {
497     Reference< embed::XStorage > xStorage;
498     OUString aURL;
499     try
500     {
501         apphelper::MediaDescriptorHelper aMDHelper( rMediaDescriptor );
502         if( aMDHelper.ISSET_Storage )
503         {
504             xStorage = aMDHelper.Storage;
505         }
506         else if( aMDHelper.ISSET_Stream ||
507                  aMDHelper.ISSET_InputStream )
508         {
509             if( aMDHelper.ISSET_FilterName &&
510                 (aMDHelper.FilterName.equals( C2U("StarChart 5.0")) ||
511                  aMDHelper.FilterName.equals( C2U("StarChart 4.0")) ||
512                  aMDHelper.FilterName.equals( C2U("StarChart 3.0")) ))
513             {
514                 attachResource( aMDHelper.URL, rMediaDescriptor );
515                 impl_load( rMediaDescriptor, 0 ); // cannot create a storage from binary streams, but I do not need the storage here anyhow
516                 m_bReadOnly = sal_True;
517                 return;
518             }
519 
520             Reference< lang::XSingleServiceFactory > xStorageFact(
521                 m_xContext->getServiceManager()->createInstanceWithContext(
522                     C2U("com.sun.star.embed.StorageFactory"),
523                     m_xContext ),
524                 uno::UNO_QUERY_THROW );
525 
526             if( aMDHelper.ISSET_Stream )
527             {
528                 // convert XStream to XStorage via the storage factory
529                 Sequence< uno::Any > aStorageArgs( 2 );
530                 aStorageArgs[0] <<= aMDHelper.Stream;
531                 // todo: check if stream is read-only
532                 aStorageArgs[1] <<= (embed::ElementModes::READ); //WRITE | embed::ElementModes::NOCREATE);
533 
534                 xStorage.set( xStorageFact->createInstanceWithArguments( aStorageArgs ),
535                     uno::UNO_QUERY_THROW );
536             }
537             else
538             {
539                 OSL_ASSERT( aMDHelper.ISSET_InputStream );
540                 // convert XInputStream to XStorage via the storage factory
541                 Sequence< uno::Any > aStorageArgs( 2 );
542                 aStorageArgs[0] <<= aMDHelper.InputStream;
543                 aStorageArgs[1] <<= (embed::ElementModes::READ);
544 
545                 xStorage.set( xStorageFact->createInstanceWithArguments( aStorageArgs ),
546                     uno::UNO_QUERY_THROW );
547             }
548         }
549 
550         if( aMDHelper.ISSET_URL )
551             aURL = aMDHelper.URL;
552     }
553     catch( uno::Exception & ex )
554     {
555         ASSERT_EXCEPTION( ex );
556     }
557 
558     if( xStorage.is())
559     {
560         attachResource( aURL, rMediaDescriptor );
561         impl_load( rMediaDescriptor, xStorage );
562     }
563 }
564 
impl_load(const Sequence<beans::PropertyValue> & rMediaDescriptor,const Reference<embed::XStorage> & xStorage)565 void ChartModel::impl_load(
566     const Sequence< beans::PropertyValue >& rMediaDescriptor,
567     const Reference< embed::XStorage >& xStorage )
568 {
569     {
570         MutexGuard aGuard( m_aModelMutex );
571         m_nInLoad++;
572     }
573 
574     Reference< document::XFilter > xFilter( impl_createFilter( rMediaDescriptor ));
575 
576     if( xFilter.is())
577     {
578         Reference< document::XImporter > xImporter( xFilter, uno::UNO_QUERY_THROW );
579         xImporter->setTargetDocument( this );
580         Sequence< beans::PropertyValue > aMD( rMediaDescriptor );
581         lcl_addStorageToMediaDescriptor( aMD, xStorage );
582 
583         xFilter->filter( aMD );
584         xFilter.clear();
585     }
586     else
587     {
588         OSL_ENSURE( false, "loadFromStorage cannot create filter" );
589     }
590 
591     if( xStorage.is() )
592         impl_loadGraphics( xStorage );
593 
594     setModified( sal_False );
595 
596     // switchToStorage without notifying listeners (which shouldn't exist at
597     // this time, anyway)
598     m_xStorage = xStorage;
599 
600     {
601         MutexGuard aGuard( m_aModelMutex );
602         m_nInLoad--;
603     }
604 }
605 
impl_loadGraphics(const Reference<embed::XStorage> & xStorage)606 void ChartModel::impl_loadGraphics(
607     const Reference< embed::XStorage >& xStorage )
608 {
609     try
610     {
611         const Reference< embed::XStorage >& xGraphicsStorage(
612             xStorage->openStorageElement( C2U( "Pictures" ),
613                                           embed::ElementModes::READ ) );
614 
615         if( xGraphicsStorage.is() )
616         {
617             const uno::Sequence< ::rtl::OUString > aElementNames(
618                 xGraphicsStorage->getElementNames() );
619 
620             for( int i = 0; i < aElementNames.getLength(); ++i )
621             {
622                 if( xGraphicsStorage->isStreamElement( aElementNames[ i ] ) )
623                 {
624                     uno::Reference< io::XStream > xElementStream(
625                         xGraphicsStorage->openStreamElement(
626                             aElementNames[ i ],
627                             embed::ElementModes::READ ) );
628 
629                     if( xElementStream.is() )
630                     {
631                         std::unique_ptr< SvStream > apIStm(
632                             ::utl::UcbStreamHelper::CreateStream(
633                                 xElementStream, true ) );
634 
635                         if( apIStm.get() )
636                         {
637                             Graphic aGraphic;
638 
639                             if( !GraphicConverter::Import(
640                                     *apIStm.get(),
641                                     aGraphic ) )
642                             {
643                                 m_aGraphicObjectVector.push_back( aGraphic );
644                             }
645                         }
646                     }
647                 }
648             }
649         }
650     }
651     catch ( uno::Exception& )
652     {
653     }
654 }
655 
656 //-----------------------------------------------------------------
657 // util::XModifiable
658 //-----------------------------------------------------------------
impl_notifyModifiedListeners()659 void SAL_CALL ChartModel::impl_notifyModifiedListeners()
660 {
661     {
662         MutexGuard aGuard( m_aModelMutex );
663         m_bUpdateNotificationsPending = false;
664     }
665 
666     //always notify the view first!
667     ChartViewHelper::setViewToDirtyState( this );
668 
669     ::cppu::OInterfaceContainerHelper* pIC = m_aLifeTimeManager.m_aListenerContainer
670         .getContainer( ::getCppuType((const uno::Reference< util::XModifyListener >*)0) );
671     if( pIC )
672     {
673         lang::EventObject aEvent( static_cast< lang::XComponent*>(this) );
674         ::cppu::OInterfaceIteratorHelper aIt( *pIC );
675         while( aIt.hasMoreElements() )
676         {
677             uno::Reference< util::XModifyListener > xListener( aIt.next(), uno::UNO_QUERY );
678             if( xListener.is() )
679                 xListener->modified( aEvent );
680         }
681     }
682 }
683 
isModified()684 sal_Bool SAL_CALL ChartModel::isModified()
685 {
686     //@todo guard
687     return m_bModified;
688 }
689 
setModified(sal_Bool bModified)690 void SAL_CALL ChartModel::setModified( sal_Bool bModified )
691 {
692     apphelper::LifeTimeGuard aGuard(m_aLifeTimeManager);
693     if(!aGuard.startApiCall())//@todo ? is this a long lasting call??
694         return; //behave passive if already disposed or closed or throw exception @todo?
695     m_bModified = bModified;
696 
697     if( m_nControllerLockCount > 0 )
698     {
699         m_bUpdateNotificationsPending = true;
700         return;//don't call listeners if controllers are locked
701     }
702     aGuard.clear();
703 
704     if(bModified)
705         impl_notifyModifiedListeners();
706 }
707 
708 //-----------------------------------------------------------------
709 // util::XModifyBroadcaster (base of XModifiable)
710 //-----------------------------------------------------------------
addModifyListener(const uno::Reference<util::XModifyListener> & xListener)711 void SAL_CALL ChartModel::addModifyListener(
712     const uno::Reference< util::XModifyListener >& xListener )
713 {
714     if( m_aLifeTimeManager.impl_isDisposedOrClosed() )
715         return; //behave passive if already disposed or closed
716 
717     m_aLifeTimeManager.m_aListenerContainer.addInterface(
718         ::getCppuType((const uno::Reference< util::XModifyListener >*)0), xListener );
719 }
720 
removeModifyListener(const uno::Reference<util::XModifyListener> & xListener)721 void SAL_CALL ChartModel::removeModifyListener(
722     const uno::Reference< util::XModifyListener >& xListener )
723 {
724     if( m_aLifeTimeManager.impl_isDisposedOrClosed(false) )
725         return; //behave passive if already disposed or closed
726 
727     m_aLifeTimeManager.m_aListenerContainer.removeInterface(
728         ::getCppuType((const uno::Reference< util::XModifyListener >*)0), xListener );
729 }
730 
731 //-----------------------------------------------------------------
732 // util::XModifyListener
733 //-----------------------------------------------------------------
modified(const lang::EventObject &)734 void SAL_CALL ChartModel::modified( const lang::EventObject& )
735 {
736     if( m_nInLoad == 0 )
737         setModified( sal_True );
738 }
739 
740 //-----------------------------------------------------------------
741 // lang::XEventListener (base of util::XModifyListener)
742 //-----------------------------------------------------------------
disposing(const lang::EventObject &)743 void SAL_CALL ChartModel::disposing( const lang::EventObject& )
744 {
745     // child was disposed -- should not happen from outside
746 }
747 
748 
749 //-----------------------------------------------------------------
750 // document::XStorageBasedDocument
751 //-----------------------------------------------------------------
loadFromStorage(const Reference<embed::XStorage> & xStorage,const Sequence<beans::PropertyValue> & rMediaDescriptor)752 void SAL_CALL ChartModel::loadFromStorage(
753     const Reference< embed::XStorage >& xStorage,
754     const Sequence< beans::PropertyValue >& rMediaDescriptor )
755 {
756     attachResource( OUString(), rMediaDescriptor );
757     impl_load( rMediaDescriptor, xStorage );
758 }
759 
storeToStorage(const Reference<embed::XStorage> & xStorage,const Sequence<beans::PropertyValue> & rMediaDescriptor)760 void SAL_CALL ChartModel::storeToStorage(
761     const Reference< embed::XStorage >& xStorage,
762     const Sequence< beans::PropertyValue >& rMediaDescriptor )
763 {
764     impl_store( rMediaDescriptor, xStorage );
765 }
766 
switchToStorage(const Reference<embed::XStorage> & xStorage)767 void SAL_CALL ChartModel::switchToStorage( const Reference< embed::XStorage >& xStorage )
768 {
769     m_xStorage = xStorage;
770     impl_notifyStorageChangeListeners();
771 }
772 
getDocumentStorage()773 Reference< embed::XStorage > SAL_CALL ChartModel::getDocumentStorage()
774 {
775     return m_xStorage;
776 }
777 
impl_notifyStorageChangeListeners()778 void SAL_CALL ChartModel::impl_notifyStorageChangeListeners()
779 {
780     ::cppu::OInterfaceContainerHelper* pIC = m_aLifeTimeManager.m_aListenerContainer
781           .getContainer( ::getCppuType((const uno::Reference< document::XStorageChangeListener >*)0) );
782     if( pIC )
783     {
784         ::cppu::OInterfaceIteratorHelper aIt( *pIC );
785         while( aIt.hasMoreElements() )
786         {
787             uno::Reference< document::XStorageChangeListener > xListener( aIt.next(), uno::UNO_QUERY );
788             if( xListener.is() )
789                 xListener->notifyStorageChange( static_cast< ::cppu::OWeakObject* >( this ), m_xStorage );
790         }
791     }
792 }
793 
addStorageChangeListener(const Reference<document::XStorageChangeListener> & xListener)794 void SAL_CALL ChartModel::addStorageChangeListener( const Reference< document::XStorageChangeListener >& xListener )
795 {
796     if( m_aLifeTimeManager.impl_isDisposedOrClosed() )
797         return; //behave passive if already disposed or closed
798 
799     m_aLifeTimeManager.m_aListenerContainer.addInterface(
800         ::getCppuType((const uno::Reference< document::XStorageChangeListener >*)0), xListener );
801 }
802 
removeStorageChangeListener(const Reference<document::XStorageChangeListener> & xListener)803 void SAL_CALL ChartModel::removeStorageChangeListener( const Reference< document::XStorageChangeListener >& xListener )
804 {
805     if( m_aLifeTimeManager.impl_isDisposedOrClosed(false) )
806         return; //behave passive if already disposed or closed
807 
808     m_aLifeTimeManager.m_aListenerContainer.removeInterface(
809         ::getCppuType((const uno::Reference< document::XStorageChangeListener >*)0), xListener );
810 }
811 
812 } //  namespace chart
813