xref: /trunk/main/sfx2/source/doc/sfxbasemodel.cxx (revision 3ce6c2dd3860afd353ceca3849779821e9b9f6f5)
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_sfx2.hxx"
26 
27 //________________________________________________________________________________________________________
28 //  my own includes
29 //________________________________________________________________________________________________________
30 
31 #include <sfx2/sfxbasemodel.hxx>
32 
33 //________________________________________________________________________________________________________
34 //  include of other projects
35 //________________________________________________________________________________________________________
36 
37 #include <com/sun/star/task/XInteractionHandler.hpp>
38 #include <com/sun/star/task/ErrorCodeRequest.hpp>
39 #include <com/sun/star/view/XSelectionSupplier.hpp>
40 #include <com/sun/star/view/XPrintJobListener.hpp>
41 #include <com/sun/star/lang/DisposedException.hpp>
42 #include <com/sun/star/lang/IllegalArgumentException.hpp>
43 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
44 #include <com/sun/star/frame/IllegalArgumentIOException.hpp>
45 #include <com/sun/star/frame/XUntitledNumbers.hpp>
46 #include <com/sun/star/frame/UntitledNumbersConst.hpp>
47 #include <com/sun/star/embed/XTransactionBroadcaster.hpp>
48 #include <com/sun/star/embed/XStorage.hpp>
49 #include <com/sun/star/embed/EmbedMapUnits.hpp>
50 #include <com/sun/star/document/XStorageChangeListener.hpp>
51 #include <com/sun/star/document/XActionLockable.hpp>
52 #include <com/sun/star/beans/XPropertySet.hpp>
53 #include <com/sun/star/container/XIndexContainer.hpp>
54 #include <com/sun/star/script/provider/XScriptProviderFactory.hpp>
55 #include <com/sun/star/script/provider/XScriptProvider.hpp>
56 #include <com/sun/star/ui/XUIConfigurationStorage.hpp>
57 #include <com/sun/star/ui/XUIConfigurationPersistence.hpp>
58 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
59 #include <com/sun/star/embed/ElementModes.hpp>
60 #include <com/sun/star/embed/Aspects.hpp>
61 #include <com/sun/star/document/XDocumentProperties.hpp>
62 #include <com/sun/star/frame/XTransientDocumentsDocumentContentFactory.hpp>
63 #include <comphelper/enumhelper.hxx>  // can be removed when this is a "real" service
64 
65 #include <cppuhelper/interfacecontainer.hxx>
66 #include <cppuhelper/exc_hlp.hxx>
67 #include <comphelper/processfactory.hxx>  // can be removed when this is a "real" service
68 #include <comphelper/componentcontext.hxx>
69 #include <comphelper/namedvaluecollection.hxx>
70 #include <svl/itemset.hxx>
71 #include <svl/stritem.hxx>
72 #include <svl/eitem.hxx>
73 #include <svl/intitem.hxx>
74 #include <basic/sbx.hxx>
75 #include <basic/sbuno.hxx>
76 #include <tools/urlobj.hxx>
77 #include <tools/diagnose_ex.h>
78 #include <unotools/tempfile.hxx>
79 #include <vos/mutex.hxx>
80 #include <vcl/salctype.hxx>
81 #include <sot/clsids.hxx>
82 #include <sot/storinfo.hxx>
83 #include <comphelper/storagehelper.hxx>
84 #include <toolkit/helper/vclunohelper.hxx>
85 #include <svtools/transfer.hxx>
86 #include <svtools/ehdl.hxx>
87 #include <svtools/sfxecode.hxx>
88 #include <rtl/logfile.hxx>
89 #include <framework/configimporter.hxx>
90 #include <framework/interaction.hxx>
91 #include <framework/titlehelper.hxx>
92 #include <comphelper/numberedcollection.hxx>
93 #include <unotools/ucbstreamhelper.hxx>
94 #include <unotools/ucbhelper.hxx>
95 
96 //________________________________________________________________________________________________________
97 //  includes of my own project
98 //________________________________________________________________________________________________________
99 
100 #include <sfx2/sfxbasecontroller.hxx>
101 #include "sfx2/viewfac.hxx"
102 #include "workwin.hxx"
103 #include <sfx2/signaturestate.hxx>
104 #include <sfx2/sfxuno.hxx>
105 #include <objshimp.hxx>
106 #include <sfx2/viewfrm.hxx>
107 #include <sfx2/viewsh.hxx>
108 #include <sfx2/docfile.hxx>
109 #include <sfx2/dispatch.hxx>
110 #include <sfx2/request.hxx>
111 #include <sfx2/objuno.hxx>
112 #include <sfx2/printer.hxx>
113 #include <sfx2/basmgr.hxx>
114 #include <sfx2/event.hxx>
115 #include <eventsupplier.hxx>
116 #include <sfx2/evntconf.hxx>
117 #include <sfx2/sfx.hrc>
118 #include <sfx2/app.hxx>
119 #include <sfx2/viewfrm.hxx>
120 #include "appdata.hxx"
121 #include <sfx2/docfac.hxx>
122 #include <sfx2/fcontnr.hxx>
123 #include "sfx2/docstoragemodifylistener.hxx"
124 #include "sfx2/brokenpackageint.hxx"
125 #include "graphhelp.hxx"
126 #include "docundomanager.hxx"
127 #include <sfx2/msgpool.hxx>
128 #include <sfx2/DocumentMetadataAccess.hxx>
129 
130 #include <sfx2/sfxresid.hxx>
131 #include <functional>
132 
133 //________________________________________________________________________________________________________
134 // const
135 static const ::rtl::OUString SERVICENAME_DESKTOP = ::rtl::OUString::createFromAscii ("com.sun.star.frame.Desktop");
136 
137 //________________________________________________________________________________________________________
138 //  namespaces
139 //________________________________________________________________________________________________________
140 
141 namespace css = ::com::sun::star;
142 using namespace ::com::sun::star;
143 using namespace ::com::sun::star::uno;
144 using ::com::sun::star::beans::PropertyValue;
145 using ::com::sun::star::frame::XFrame;
146 using ::com::sun::star::frame::XController;
147 using ::com::sun::star::frame::XController2;
148 using ::com::sun::star::lang::IllegalArgumentException;
149 using ::com::sun::star::io::IOException;
150 using ::com::sun::star::lang::WrappedTargetException;
151 using ::com::sun::star::uno::Type;
152 using ::com::sun::star::uno::Sequence;
153 using ::com::sun::star::document::XDocumentRecovery;
154 using ::com::sun::star::document::XUndoManager;
155 using ::com::sun::star::document::XUndoAction;
156 using ::com::sun::star::document::UndoFailedException;
157 using ::com::sun::star::frame::XModel;
158 
159 /** This Listener is used to get notified when the XDocumentProperties of the
160     XModel change.
161  */
162 class SfxDocInfoListener_Impl : public ::cppu::WeakImplHelper1<
163     ::com::sun::star::util::XModifyListener >
164 {
165 
166 public:
167     SfxObjectShell& m_rShell;
168 
SfxDocInfoListener_Impl(SfxObjectShell & i_rDoc)169     SfxDocInfoListener_Impl( SfxObjectShell& i_rDoc )
170         : m_rShell(i_rDoc)
171     { };
172 
173     ~SfxDocInfoListener_Impl();
174 
175     virtual void SAL_CALL disposing( const lang::EventObject& );
176     virtual void SAL_CALL modified( const lang::EventObject& );
177 };
~SfxDocInfoListener_Impl()178 SfxDocInfoListener_Impl::~SfxDocInfoListener_Impl()
179 {
180 }
modified(const lang::EventObject &)181 void SAL_CALL SfxDocInfoListener_Impl::modified( const lang::EventObject& )
182 {
183     ::vos::OGuard aSolarGuard( Application::GetSolarMutex() );
184 
185     // notify changes to the SfxObjectShell
186     m_rShell.FlushDocInfo();
187 }
188 
disposing(const lang::EventObject &)189 void SAL_CALL SfxDocInfoListener_Impl::disposing( const lang::EventObject& )
190 {
191 }
192 
193 //________________________________________________________________________________________________________
194 //  impl. declarations
195 //________________________________________________________________________________________________________
196 
197 
198 struct IMPL_SfxBaseModel_DataContainer : public ::sfx2::IModifiableDocument
199 {
200     // counter for SfxBaseModel instances created.
201     static sal_Int64                                        g_nInstanceCounter      ;
202     SfxObjectShellRef                                       m_pObjectShell          ;
203     ::rtl::OUString                                         m_sURL                  ;
204     ::rtl::OUString                                         m_sRuntimeUID           ;
205     ::rtl::OUString                                         m_aPreusedFilterName;
206     ::cppu::OMultiTypeInterfaceContainerHelper              m_aInterfaceContainer   ;
207     uno::Reference< uno::XInterface >                       m_xParent               ;
208     uno::Reference< frame::XController >                    m_xCurrent              ;
209     uno::Reference< document::XDocumentInfo >               m_xDocumentInfo         ;
210     uno::Reference< document::XDocumentProperties >         m_xDocumentProperties;
211     uno::Reference< script::XStarBasicAccess >              m_xStarBasicAccess      ;
212     uno::Reference< container::XNameReplace >               m_xEvents               ;
213     uno::Sequence< beans::PropertyValue>                    m_seqArguments          ;
214     uno::Sequence< uno::Reference< frame::XController > >   m_seqControllers        ;
215     uno::Reference< container::XIndexAccess >               m_contViewData          ;
216     sal_uInt16                                              m_nControllerLockCount  ;
217     sal_Bool                                                m_bClosed               ;
218     sal_Bool                                                m_bClosing              ;
219     sal_Bool                                                m_bSaving               ;
220     sal_Bool                                                m_bSuicide              ;
221     sal_Bool                                                m_bInitialized          ;
222     sal_Bool                                                m_bExternalTitle        ;
223     sal_Bool                                                m_bModifiedSinceLastSave;
224     uno::Reference< com::sun::star::view::XPrintable>       m_xPrintable            ;
225     uno::Reference< script::provider::XScriptProvider >     m_xScriptProvider;
226     uno::Reference< ui::XUIConfigurationManager >           m_xUIConfigurationManager;
227     ::rtl::Reference< ::sfx2::DocumentStorageModifyListener >   m_pStorageModifyListen;
228     ::rtl::OUString                                         m_sModuleIdentifier;
229     css::uno::Reference< css::frame::XTitle >               m_xTitleHelper;
230     css::uno::Reference< css::frame::XUntitledNumbers >     m_xNumberedControllers;
231     uno::Reference< rdf::XDocumentMetadataAccess>           m_xDocumentMetadata;
232     ::rtl::Reference< ::sfx2::DocumentUndoManager >         m_pDocumentUndoManager;
233 
234 
IMPL_SfxBaseModel_DataContainerIMPL_SfxBaseModel_DataContainer235     IMPL_SfxBaseModel_DataContainer( ::osl::Mutex& rMutex, SfxObjectShell* pObjectShell )
236             :   m_pObjectShell          ( pObjectShell  )
237             ,   m_aInterfaceContainer   ( rMutex        )
238             ,   m_nControllerLockCount  ( 0             )
239             ,   m_bClosed               ( sal_False     )
240             ,   m_bClosing              ( sal_False     )
241             ,   m_bSaving               ( sal_False     )
242             ,   m_bSuicide              ( sal_False     )
243             ,   m_bInitialized          ( sal_False     )
244             ,   m_bExternalTitle        ( sal_False     )
245             ,   m_bModifiedSinceLastSave( sal_False     )
246             ,   m_pStorageModifyListen  ( NULL          )
247             ,   m_xTitleHelper          ()
248             ,   m_xNumberedControllers  ()
249             ,   m_xDocumentMetadata     () // lazy
250             ,   m_pDocumentUndoManager  ()
251     {
252         // increase global instance counter.
253         ++g_nInstanceCounter;
254         // set own Runtime UID
255         m_sRuntimeUID = rtl::OUString::valueOf( g_nInstanceCounter );
256     }
257 
~IMPL_SfxBaseModel_DataContainerIMPL_SfxBaseModel_DataContainer258     virtual ~IMPL_SfxBaseModel_DataContainer()
259     {
260     }
261 
262     // ::sfx2::IModifiableDocument
storageIsModifiedIMPL_SfxBaseModel_DataContainer263     virtual void storageIsModified()
264     {
265         if ( m_pObjectShell.Is() && !m_pObjectShell->IsModified() )
266             m_pObjectShell->SetModified( sal_True );
267     }
268 
GetDMAIMPL_SfxBaseModel_DataContainer269     uno::Reference<rdf::XDocumentMetadataAccess> GetDMA()
270     {
271         if (!m_xDocumentMetadata.is())
272         {
273             OSL_ENSURE(m_pObjectShell, "GetDMA: no object shell?");
274             if (!m_pObjectShell)
275             {
276                 return 0;
277             }
278 
279             const uno::Reference<uno::XComponentContext> xContext(
280                 ::comphelper::getProcessComponentContext());
281             ::rtl::OUString uri;
282             const uno::Reference<frame::XModel> xModel(
283                 m_pObjectShell->GetModel());
284             const uno::Reference<lang::XMultiComponentFactory> xMsf(
285                 xContext->getServiceManager());
286             const uno::Reference<frame::
287                 XTransientDocumentsDocumentContentFactory> xTDDCF(
288                     xMsf->createInstanceWithContext(
289                         ::rtl::OUString::createFromAscii( "com.sun.star.frame."
290                             "TransientDocumentsDocumentContentFactory"),
291                     xContext),
292                 uno::UNO_QUERY_THROW);
293             const uno::Reference<ucb::XContent> xContent(
294                 xTDDCF->createDocumentContent(xModel) );
295             OSL_ENSURE(xContent.is(), "GetDMA: cannot create DocumentContent");
296             if (!xContent.is())
297             {
298                 return 0;
299             }
300             uri = xContent->getIdentifier()->getContentIdentifier();
301             OSL_ENSURE(uri.getLength(), "GetDMA: empty uri?");
302             if (uri.getLength() && !uri.endsWithAsciiL("/", 1))
303             {
304                 uri = uri + ::rtl::OUString::createFromAscii("/");
305             }
306 
307             m_xDocumentMetadata = new ::sfx2::DocumentMetadataAccess(
308                 xContext, *m_pObjectShell, uri);
309         }
310         return m_xDocumentMetadata;
311     }
312 
CreateDMAUninitializedIMPL_SfxBaseModel_DataContainer313     uno::Reference<rdf::XDocumentMetadataAccess> CreateDMAUninitialized()
314     {
315         return (m_pObjectShell)
316             ? new ::sfx2::DocumentMetadataAccess(
317                 ::comphelper::getProcessComponentContext(), *m_pObjectShell)
318             : 0;
319     }
320 };
321 
322 // static member initialization.
323 sal_Int64 IMPL_SfxBaseModel_DataContainer::g_nInstanceCounter = 0;
324 
325 // =======================================================================================================
326 
327 // Listener that forwards notifications from the PrintHelper to the "real" listeners
328 class SfxPrintHelperListener_Impl : public ::cppu::WeakImplHelper1< ::com::sun::star::view::XPrintJobListener >
329 {
330 public:
331     IMPL_SfxBaseModel_DataContainer* m_pData;
SfxPrintHelperListener_Impl(IMPL_SfxBaseModel_DataContainer * pData)332     SfxPrintHelperListener_Impl( IMPL_SfxBaseModel_DataContainer* pData )
333         : m_pData( pData )
334     {}
335 
336     virtual void SAL_CALL disposing( const lang::EventObject& aEvent ) ;
337     virtual void SAL_CALL printJobEvent( const view::PrintJobEvent& rEvent );
338 };
339 
disposing(const lang::EventObject &)340 void SAL_CALL SfxPrintHelperListener_Impl::disposing( const lang::EventObject& )
341 {
342     m_pData->m_xPrintable = 0;
343 }
344 
printJobEvent(const view::PrintJobEvent & rEvent)345 void SAL_CALL SfxPrintHelperListener_Impl::printJobEvent( const view::PrintJobEvent& rEvent )
346 {
347     ::cppu::OInterfaceContainerHelper* pContainer = m_pData->m_aInterfaceContainer.getContainer( ::getCppuType( ( const uno::Reference< view::XPrintJobListener >*) NULL ) );
348     if ( pContainer!=NULL )
349     {
350         ::cppu::OInterfaceIteratorHelper pIterator(*pContainer);
351         while (pIterator.hasMoreElements())
352             ((view::XPrintJobListener*)pIterator.next())->printJobEvent( rEvent );
353     }
354 }
355 
356 // SfxOwnFramesLocker ====================================================================================
357 // allows to lock all the frames related to the provided SfxObjectShell
358 class SfxOwnFramesLocker
359 {
360     uno::Sequence< uno::Reference< frame::XFrame > > m_aLockedFrames;
361 
362     Window* GetVCLWindow( const uno::Reference< frame::XFrame >& xFrame );
363 public:
364     SfxOwnFramesLocker( SfxObjectShell* ObjechShell );
365     ~SfxOwnFramesLocker();
366     void UnlockFrames();
367 };
368 
SfxOwnFramesLocker(SfxObjectShell * pObjectShell)369 SfxOwnFramesLocker::SfxOwnFramesLocker( SfxObjectShell* pObjectShell )
370 {
371     if ( !pObjectShell )
372         return;
373 
374     for (   SfxViewFrame *pFrame = SfxViewFrame::GetFirst( pObjectShell );
375             pFrame;
376             pFrame = SfxViewFrame::GetNext( *pFrame, pObjectShell )
377         )
378     {
379         SfxFrame& rSfxFrame = pFrame->GetFrame();
380         try
381         {
382             // get vcl window related to the frame and lock it if it is still not locked
383             uno::Reference< frame::XFrame > xFrame = rSfxFrame.GetFrameInterface();
384             Window* pWindow = GetVCLWindow( xFrame );
385             if ( !pWindow )
386                 throw uno::RuntimeException();
387 
388             if ( pWindow->IsEnabled() )
389             {
390                 pWindow->Disable();
391 
392                 try
393                 {
394                     sal_Int32 nLen = m_aLockedFrames.getLength();
395                     m_aLockedFrames.realloc( nLen + 1 );
396                     m_aLockedFrames[nLen] = xFrame;
397                 }
398                 catch( uno::Exception& )
399                 {
400                     pWindow->Enable();
401                     throw;
402                 }
403             }
404         }
405         catch( uno::Exception& )
406         {
407             OSL_ENSURE( sal_False, "Not possible to lock the frame window!\n" );
408         }
409     }
410 }
411 
~SfxOwnFramesLocker()412 SfxOwnFramesLocker::~SfxOwnFramesLocker()
413 {
414     UnlockFrames();
415 }
416 
GetVCLWindow(const uno::Reference<frame::XFrame> & xFrame)417 Window* SfxOwnFramesLocker::GetVCLWindow( const uno::Reference< frame::XFrame >& xFrame )
418 {
419     Window* pWindow = NULL;
420 
421     if ( xFrame.is() )
422     {
423         uno::Reference< awt::XWindow > xWindow = xFrame->getContainerWindow();
424         if ( xWindow.is() )
425             pWindow = VCLUnoHelper::GetWindow( xWindow );
426     }
427 
428     return pWindow;
429 }
430 
UnlockFrames()431 void SfxOwnFramesLocker::UnlockFrames()
432 {
433     for ( sal_Int32 nInd = 0; nInd < m_aLockedFrames.getLength(); nInd++ )
434     {
435         try
436         {
437             if ( m_aLockedFrames[nInd].is() )
438             {
439                 // get vcl window related to the frame and unlock it
440                 Window* pWindow = GetVCLWindow( m_aLockedFrames[nInd] );
441                 if ( !pWindow )
442                     throw uno::RuntimeException();
443 
444                 pWindow->Enable();
445 
446                 m_aLockedFrames[nInd] = uno::Reference< frame::XFrame >();
447             }
448         }
449         catch( uno::Exception& )
450         {
451             OSL_ENSURE( sal_False, "Can't unlock the frame window!\n" );
452         }
453     }
454 }
455 
456 // SfxSaveGuard ====================================================================================
457 class SfxSaveGuard
458 {
459     private:
460         uno::Reference< frame::XModel > m_xModel;
461         IMPL_SfxBaseModel_DataContainer* m_pData;
462         SfxOwnFramesLocker* m_pFramesLock;
463 
464     public:
465         SfxSaveGuard(const uno::Reference< frame::XModel >&             xModel                      ,
466                            IMPL_SfxBaseModel_DataContainer* pData                       ,
467                            sal_Bool                         bRejectConcurrentSaveRequest);
468         ~SfxSaveGuard();
469 };
470 
SfxSaveGuard(const uno::Reference<frame::XModel> & xModel,IMPL_SfxBaseModel_DataContainer * pData,sal_Bool bRejectConcurrentSaveRequest)471 SfxSaveGuard::SfxSaveGuard(const uno::Reference< frame::XModel >&             xModel                      ,
472                                  IMPL_SfxBaseModel_DataContainer* pData                       ,
473                                  sal_Bool                         bRejectConcurrentSaveRequest)
474     : m_xModel     (xModel)
475     , m_pData      (pData )
476     , m_pFramesLock(0     )
477 {
478     static ::rtl::OUString MSG_1 = ::rtl::OUString::createFromAscii("Object already disposed."                                       );
479     static ::rtl::OUString MSG_2 = ::rtl::OUString::createFromAscii("Concurrent save requests on the same document are not possible.");
480 
481     if ( m_pData->m_bClosed )
482         throw ::com::sun::star::lang::DisposedException(
483                 MSG_1,
484                 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >());
485 
486     if (
487         bRejectConcurrentSaveRequest &&
488         m_pData->m_bSaving
489        )
490         throw ::com::sun::star::io::IOException(
491                 MSG_2,
492                 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >());
493 
494     m_pData->m_bSaving = sal_True;
495     m_pFramesLock = new SfxOwnFramesLocker(m_pData->m_pObjectShell);
496 }
497 
~SfxSaveGuard()498 SfxSaveGuard::~SfxSaveGuard()
499 {
500     SfxOwnFramesLocker* pFramesLock = m_pFramesLock;
501     m_pFramesLock = 0;
502     delete pFramesLock;
503 
504     m_pData->m_bSaving = sal_False;
505 
506     // m_bSuicide was set e.g. in case somewhere tried to close a document, while it was used for
507     // storing at the same time. Further m_bSuicide was set to sal_True only if close(sal_True) was called.
508     // So the owner ship was delegated to the place where a veto exception was thrown.
509     // Now we have to call close() again and delegate the owner ship to the next one, which
510     // can't accept that. Close(sal_False) can't work in this case. Because then the document will may be never closed ...
511 
512     if ( m_pData->m_bSuicide )
513     {
514         // Reset this state. In case the new close() request is not accepted by somewhere else ...
515         // it's not a good idea to have two "owners" for close .-)
516         m_pData->m_bSuicide = sal_False;
517         try
518         {
519             uno::Reference< util::XCloseable > xClose(m_xModel, uno::UNO_QUERY);
520             if (xClose.is())
521                 xClose->close(sal_True);
522         }
523         catch(const util::CloseVetoException&)
524         {}
525     }
526 }
527 
528 // =======================================================================================================
529 
530 //________________________________________________________________________________________________________
531 //  constructor
532 //________________________________________________________________________________________________________
DBG_NAME(sfx2_SfxBaseModel)533 DBG_NAME(sfx2_SfxBaseModel)
534 SfxBaseModel::SfxBaseModel( SfxObjectShell *pObjectShell )
535 : BaseMutex()
536 , m_pData( new IMPL_SfxBaseModel_DataContainer( m_aMutex, pObjectShell ) )
537 , m_bSupportEmbeddedScripts( pObjectShell && pObjectShell->Get_Impl() ? !pObjectShell->Get_Impl()->m_bNoBasicCapabilities : false )
538 , m_bSupportDocRecovery( pObjectShell && pObjectShell->Get_Impl() ? pObjectShell->Get_Impl()->m_bDocRecoverySupport : false )
539 {
540     DBG_CTOR(sfx2_SfxBaseModel,NULL);
541     if ( pObjectShell != NULL )
542     {
543         StartListening( *pObjectShell ) ;
544     }
545 }
546 
547 //________________________________________________________________________________________________________
548 //  destructor
549 //________________________________________________________________________________________________________
550 
~SfxBaseModel()551 SfxBaseModel::~SfxBaseModel()
552 {
553     DBG_DTOR(sfx2_SfxBaseModel,NULL);
554 }
555 
556 //________________________________________________________________________________________________________
557 //  XInterface
558 //________________________________________________________________________________________________________
559 
queryInterface(const UNOTYPE & rType)560 uno::Any SAL_CALL SfxBaseModel::queryInterface( const UNOTYPE& rType )
561 {
562     if  (   ( !m_bSupportEmbeddedScripts && rType.equals( XEMBEDDEDSCRIPTS::static_type() ) )
563         ||  ( !m_bSupportDocRecovery && rType.equals( XDocumentRecovery::static_type() ) )
564         )
565         return Any();
566 
567     return SfxBaseModel_Base::queryInterface( rType );
568 }
569 
570 //________________________________________________________________________________________________________
571 //  XInterface
572 //________________________________________________________________________________________________________
573 
acquire()574 void SAL_CALL SfxBaseModel::acquire() throw( )
575 {
576     // Attention:
577     //  Don't use mutex or guard in this method!!! Is a method of XInterface.
578 
579     // Forward to baseclass
580     OWeakObject::acquire() ;
581 }
582 
583 //________________________________________________________________________________________________________
584 //  XInterface
585 //________________________________________________________________________________________________________
586 
release()587 void SAL_CALL SfxBaseModel::release() throw( )
588 {
589     // Attention:
590     //  Don't use mutex or guard in this method!!! Is a method of XInterface.
591 
592     // Forward to baseclass
593     OWeakObject::release() ;
594 }
595 
596 //________________________________________________________________________________________________________
597 //  XTypeProvider
598 //________________________________________________________________________________________________________
599 
600 namespace
601 {
lcl_stripType(Sequence<Type> & io_rTypes,const Type & i_rTypeToStrip)602     void lcl_stripType( Sequence< Type >& io_rTypes, const Type& i_rTypeToStrip )
603     {
604         Sequence< UNOTYPE > aStrippedTypes( io_rTypes.getLength() - 1 );
605         ::std::remove_copy_if(
606             io_rTypes.getConstArray(),
607             io_rTypes.getConstArray() + io_rTypes.getLength(),
608             aStrippedTypes.getArray(),
609             ::std::bind( ::std::equal_to< Type >(), ::std::placeholders::_1, i_rTypeToStrip )
610         );
611         io_rTypes = aStrippedTypes;
612     }
613 }
614 
getTypes()615 uno::Sequence< UNOTYPE > SAL_CALL SfxBaseModel::getTypes()
616 {
617     uno::Sequence< UNOTYPE > aTypes( SfxBaseModel_Base::getTypes() );
618 
619     if ( !m_bSupportEmbeddedScripts )
620         lcl_stripType( aTypes, XEMBEDDEDSCRIPTS::static_type() );
621 
622     if ( !m_bSupportDocRecovery )
623         lcl_stripType( aTypes, XDocumentRecovery::static_type() );
624 
625     return aTypes;
626 }
627 
628 //________________________________________________________________________________________________________
629 //  XTypeProvider
630 //________________________________________________________________________________________________________
631 
getImplementationId()632 uno::Sequence< sal_Int8 > SAL_CALL SfxBaseModel::getImplementationId()
633 {
634     // Create one Id for all instances of this class.
635     // Use ethernet address to do this! (sal_True)
636 
637     // Optimize this method
638     // We initialize a static variable only one time. And we don't must use a mutex at every call!
639     // For the first call; pID is NULL - for the second call pID is different from NULL!
640     static ::cppu::OImplementationId* pID = NULL ;
641 
642     if ( pID == NULL )
643     {
644         // Ready for multithreading; get global mutex for first call of this method only! see before
645         ::osl::MutexGuard aGuard( MUTEX::getGlobalMutex() ) ;
646 
647         // Control these pointer again ... it can be, that another instance will be faster then these!
648         if ( pID == NULL )
649         {
650             // Create a new static ID ...
651             static ::cppu::OImplementationId aID( sal_False ) ;
652             // ... and set his address to static pointer!
653             pID = &aID ;
654         }
655     }
656 
657     return pID->getImplementationId() ;
658 }
659 
660 //________________________________________________________________________________________________________
661 //  XStarBasicAccess
662 //________________________________________________________________________________________________________
663 
implGetStarBasicAccess(SfxObjectShell * pObjectShell)664 uno::Reference< script::XStarBasicAccess > implGetStarBasicAccess( SfxObjectShell* pObjectShell )
665 {
666     uno::Reference< script::XStarBasicAccess > xRet;
667     if( pObjectShell )
668     {
669         BasicManager* pMgr = pObjectShell->GetBasicManager();
670         xRet = getStarBasicAccess( pMgr );
671     }
672     return xRet;
673 }
674 
getLibraryContainer()675 uno::Reference< XNAMECONTAINER > SAL_CALL SfxBaseModel::getLibraryContainer()
676 {
677     SfxModelGuard aGuard( *this );
678 
679     uno::Reference< script::XStarBasicAccess >& rxAccess = m_pData->m_xStarBasicAccess;
680     if( !rxAccess.is() && m_pData->m_pObjectShell.Is() )
681         rxAccess = implGetStarBasicAccess( m_pData->m_pObjectShell );
682 
683     uno::Reference< XNAMECONTAINER > xRet;
684     if( rxAccess.is() )
685         xRet = rxAccess->getLibraryContainer();
686     return xRet;
687 }
688 
689 /**___________________________________________________________________________________________________
690     @seealso    XStarBasicAccess
691 */
createLibrary(const::rtl::OUString & LibName,const::rtl::OUString & Password,const::rtl::OUString & ExternalSourceURL,const::rtl::OUString & LinkTargetURL)692 void SAL_CALL SfxBaseModel::createLibrary( const ::rtl::OUString& LibName, const ::rtl::OUString& Password,
693     const ::rtl::OUString& ExternalSourceURL, const ::rtl::OUString& LinkTargetURL )
694 {
695     SfxModelGuard aGuard( *this );
696 
697     uno::Reference< script::XStarBasicAccess >& rxAccess = m_pData->m_xStarBasicAccess;
698     if( !rxAccess.is() && m_pData->m_pObjectShell.Is() )
699         rxAccess = implGetStarBasicAccess( m_pData->m_pObjectShell );
700 
701     if( rxAccess.is() )
702         rxAccess->createLibrary( LibName, Password, ExternalSourceURL, LinkTargetURL );
703 }
704 
705 /**___________________________________________________________________________________________________
706     @seealso    XStarBasicAccess
707 */
addModule(const::rtl::OUString & LibraryName,const::rtl::OUString & ModuleName,const::rtl::OUString & Language,const::rtl::OUString & Source)708 void SAL_CALL SfxBaseModel::addModule( const ::rtl::OUString& LibraryName, const ::rtl::OUString& ModuleName,
709     const ::rtl::OUString& Language, const ::rtl::OUString& Source )
710 {
711     SfxModelGuard aGuard( *this );
712 
713     uno::Reference< script::XStarBasicAccess >& rxAccess = m_pData->m_xStarBasicAccess;
714     if( !rxAccess.is() && m_pData->m_pObjectShell.Is() )
715         rxAccess = implGetStarBasicAccess( m_pData->m_pObjectShell );
716 
717     if( rxAccess.is() )
718         rxAccess->addModule( LibraryName, ModuleName, Language, Source );
719 }
720 
721 /**___________________________________________________________________________________________________
722     @seealso    XStarBasicAccess
723 */
addDialog(const::rtl::OUString & LibraryName,const::rtl::OUString & DialogName,const::com::sun::star::uno::Sequence<sal_Int8> & Data)724 void SAL_CALL SfxBaseModel::addDialog( const ::rtl::OUString& LibraryName, const ::rtl::OUString& DialogName,
725     const ::com::sun::star::uno::Sequence< sal_Int8 >& Data )
726 {
727     SfxModelGuard aGuard( *this );
728 
729     uno::Reference< script::XStarBasicAccess >& rxAccess = m_pData->m_xStarBasicAccess;
730     if( !rxAccess.is() && m_pData->m_pObjectShell.Is() )
731         rxAccess = implGetStarBasicAccess( m_pData->m_pObjectShell );
732 
733     if( rxAccess.is() )
734         rxAccess->addDialog( LibraryName, DialogName, Data );
735 }
736 
737 
738 //________________________________________________________________________________________________________
739 //  XChild
740 //________________________________________________________________________________________________________
741 
getParent()742 uno::Reference< uno::XInterface > SAL_CALL SfxBaseModel::getParent()
743 {
744     SfxModelGuard aGuard( *this );
745 
746     return m_pData->m_xParent;
747 }
748 
749 //________________________________________________________________________________________________________
750 //  XChild
751 //________________________________________________________________________________________________________
752 
setParent(const uno::Reference<uno::XInterface> & Parent)753 void SAL_CALL SfxBaseModel::setParent(const uno::Reference< uno::XInterface >& Parent)
754 {
755     SfxModelGuard aGuard( *this, SfxModelGuard::E_INITIALIZING );
756     m_pData->m_xParent = Parent;
757 }
758 
759 //________________________________________________________________________________________________________
760 //  XChild
761 //________________________________________________________________________________________________________
762 
dispose()763 void SAL_CALL SfxBaseModel::dispose()
764 {
765     SfxModelGuard aGuard( *this, SfxModelGuard::E_INITIALIZING );
766 
767     if  ( !m_pData->m_bClosed )
768     {
769         // gracefully accept wrong dispose calls instead of close call
770         // and try to make it work (may be really disposed later!)
771         try
772         {
773             close( sal_True );
774         }
775         catch ( com::sun::star::util::CloseVetoException& )
776         {
777         }
778 
779         return;
780     }
781 
782     if ( m_pData->m_pStorageModifyListen.is() )
783     {
784         m_pData->m_pStorageModifyListen->dispose();
785         m_pData->m_pStorageModifyListen = NULL;
786     }
787 
788     if ( m_pData->m_pDocumentUndoManager.is() )
789     {
790         m_pData->m_pDocumentUndoManager->disposing();
791         m_pData->m_pDocumentUndoManager = NULL;
792     }
793 
794     lang::EventObject aEvent( (frame::XModel *)this );
795     m_pData->m_aInterfaceContainer.disposeAndClear( aEvent );
796 
797     if ( m_pData->m_xDocumentInfo.is() )
798     {
799         // as long as an SfxObjectShell is assigned to an SfxBaseModel it is still existing here
800         // so we can't dispose the shared DocumentInfoObject here
801         // uno::Reference < lang::XComponent > xComp( m_pData->m_xDocumentInfo, uno::UNO_QUERY );
802         // xComp->dispose();
803         m_pData->m_xDocumentInfo = 0;
804     }
805 
806     m_pData->m_xDocumentProperties.clear();
807 
808     m_pData->m_xDocumentMetadata.clear();
809 
810     EndListening( *m_pData->m_pObjectShell );
811 
812     m_pData->m_xCurrent = uno::Reference< frame::XController > ();
813     m_pData->m_seqControllers = uno::Sequence< uno::Reference< frame::XController > > () ;
814 
815     // m_pData member must be set to zero before 0delete is called to
816     // force disposed exception whenever someone tries to access our
817     // instance while in the dtor.
818     IMPL_SfxBaseModel_DataContainer* pData = m_pData;
819     m_pData = 0;
820     delete pData;
821 }
822 
823 //________________________________________________________________________________________________________
824 //  XChild
825 //________________________________________________________________________________________________________
826 
addEventListener(const uno::Reference<XEVENTLISTENER> & aListener)827 void SAL_CALL SfxBaseModel::addEventListener( const uno::Reference< XEVENTLISTENER >& aListener )
828 {
829     SfxModelGuard aGuard( *this, SfxModelGuard::E_INITIALIZING );
830     m_pData->m_aInterfaceContainer.addInterface( ::getCppuType((const uno::Reference< XEVENTLISTENER >*)0), aListener );
831 }
832 
833 //________________________________________________________________________________________________________
834 //  XChild
835 //________________________________________________________________________________________________________
836 
removeEventListener(const uno::Reference<XEVENTLISTENER> & aListener)837 void SAL_CALL SfxBaseModel::removeEventListener( const uno::Reference< XEVENTLISTENER >& aListener )
838 {
839     SfxModelGuard aGuard( *this, SfxModelGuard::E_INITIALIZING );
840     m_pData->m_aInterfaceContainer.removeInterface( ::getCppuType((const uno::Reference< XEVENTLISTENER >*)0), aListener );
841 }
842 
843 //________________________________________________________________________________________________________
844 //  document::XDocumentInfoSupplier
845 //________________________________________________________________________________________________________
846 
getDocumentInfo()847 uno::Reference< document::XDocumentInfo > SAL_CALL SfxBaseModel::getDocumentInfo()
848 {
849     SfxModelGuard aGuard( *this, SfxModelGuard::E_INITIALIZING );
850     if ( !m_pData->m_xDocumentInfo.is() )
851     {
852         // WARNING: this will only work if (when loading a document) the
853         // document meta-data has already been read and completely written
854         // into the XDocumentProperties at this point
855         // ==> DO NOT call getDocumentInfo before document info has been read!
856         uno::Reference< document::XDocumentInfo > xDocInfo =
857             new SfxDocumentInfoObject;
858         uno::Reference< document::XDocumentProperties > xDocProps =
859             getDocumentProperties();
860         uno::Sequence< uno::Any > args(1);
861         args[0] <<= xDocProps;
862         uno::Reference< lang::XInitialization > xInit(
863             xDocInfo, uno::UNO_QUERY_THROW);
864         try {
865             xInit->initialize(args);
866             ((SfxBaseModel*)this)->m_pData->m_xDocumentInfo = xDocInfo;
867         } catch (uno::RuntimeException &) {
868             throw;
869         } catch (uno::Exception & e) {
870             throw lang::WrappedTargetRuntimeException(::rtl::OUString::createFromAscii(
871                 "SfxBaseModel::getDocumentInfo: cannot initialize"), *this,
872                 uno::makeAny(e));
873         }
874         try {
875             rtl::OUString aName = rtl::OUString::createFromAscii("MediaType");
876             uno::Reference < beans::XPropertySet > xSet(
877                 getDocumentStorage(), uno::UNO_QUERY );
878             uno::Any aMediaType = xSet->getPropertyValue( aName );
879             uno::Reference < beans::XPropertySet > xDocSet(
880                 m_pData->m_xDocumentInfo, uno::UNO_QUERY );
881             xDocSet->setPropertyValue( aName, aMediaType );
882         } catch (uno::Exception &) {
883             //ignore
884         }
885     }
886 
887     return m_pData->m_xDocumentInfo;
888 }
889 
890 // document::XDocumentPropertiesSupplier:
891 uno::Reference< document::XDocumentProperties > SAL_CALL
getDocumentProperties()892 SfxBaseModel::getDocumentProperties()
893 {
894     SfxModelGuard aGuard( *this, SfxModelGuard::E_INITIALIZING );
895     if ( !m_pData->m_xDocumentProperties.is() )
896     {
897         uno::Reference< lang::XInitialization > xDocProps(
898             ::comphelper::getProcessServiceFactory()->createInstance(
899                 DEFINE_CONST_UNICODE("com.sun.star.document.DocumentProperties") ),
900             uno::UNO_QUERY_THROW);
901 //        xDocProps->initialize(uno::Sequence<uno::Any>());
902         m_pData->m_xDocumentProperties.set(xDocProps, uno::UNO_QUERY_THROW);
903         uno::Reference<util::XModifyBroadcaster> xMB(m_pData->m_xDocumentProperties, uno::UNO_QUERY_THROW);
904         xMB->addModifyListener(new SfxDocInfoListener_Impl(*m_pData->m_pObjectShell));
905     }
906 
907     return m_pData->m_xDocumentProperties;
908 }
909 
910 
911 //________________________________________________________________________________________________________
912 //  XEVENTLISTENER
913 //________________________________________________________________________________________________________
914 
disposing(const lang::EventObject & aObject)915 void SAL_CALL SfxBaseModel::disposing( const lang::EventObject& aObject )
916 {
917     ::vos::OGuard aGuard( Application::GetSolarMutex() );
918     if ( impl_isDisposed() )
919         return;
920 
921     uno::Reference< XMODIFYLISTENER >  xMod( aObject.Source, uno::UNO_QUERY );
922     uno::Reference< XEVENTLISTENER >  xListener( aObject.Source, uno::UNO_QUERY );
923     uno::Reference< XDOCEVENTLISTENER >  xDocListener( aObject.Source, uno::UNO_QUERY );
924 
925     if ( xMod.is() )
926         m_pData->m_aInterfaceContainer.removeInterface( ::getCppuType((const uno::Reference< XMODIFYLISTENER >*)0), xMod );
927     else if ( xListener.is() )
928         m_pData->m_aInterfaceContainer.removeInterface( ::getCppuType((const uno::Reference< XEVENTLISTENER >*)0), xListener );
929     else if ( xDocListener.is() )
930         m_pData->m_aInterfaceContainer.removeInterface( ::getCppuType((const uno::Reference< XDOCEVENTLISTENER >*)0), xDocListener );
931 }
932 
933 //________________________________________________________________________________________________________
934 //  frame::XModel
935 //________________________________________________________________________________________________________
936 
attachResource(const::rtl::OUString & rURL,const uno::Sequence<beans::PropertyValue> & rArgs)937 sal_Bool SAL_CALL SfxBaseModel::attachResource( const   ::rtl::OUString&                   rURL    ,
938                                                 const   uno::Sequence< beans::PropertyValue >&  rArgs   )
939 {
940     SfxModelGuard aGuard( *this, SfxModelGuard::E_INITIALIZING );
941     if ( rURL.getLength() == 0 && rArgs.getLength() == 1 && rArgs[0].Name.equalsAscii( "SetEmbedded" ) )
942     {
943         // allows to set a windowless document to EMBEDDED state
944         // but _only_ before load() or initNew() methods
945         if ( m_pData->m_pObjectShell.Is() && !m_pData->m_pObjectShell->GetMedium() )
946         {
947             sal_Bool bEmb = sal_Bool();
948             if ( ( rArgs[0].Value >>= bEmb ) && bEmb )
949                 m_pData->m_pObjectShell->SetCreateMode_Impl( SFX_CREATE_MODE_EMBEDDED );
950         }
951 
952         return sal_True;
953     }
954 
955     if ( m_pData->m_pObjectShell.Is() )
956     {
957         m_pData->m_sURL = rURL;
958 
959         SfxObjectShell* pObjectShell = m_pData->m_pObjectShell;
960 
961         ::comphelper::NamedValueCollection aArgs( rArgs );
962 
963         Sequence< sal_Int32 > aWinExtent;
964         if ( ( aArgs.get( "WinExtent" ) >>= aWinExtent )&& ( aWinExtent.getLength() == 4 ) )
965         {
966             Rectangle aVisArea( aWinExtent[0], aWinExtent[1], aWinExtent[2], aWinExtent[3] );
967             aVisArea = OutputDevice::LogicToLogic( aVisArea, MAP_100TH_MM, pObjectShell->GetMapUnit() );
968             pObjectShell->SetVisArea( aVisArea );
969         }
970 
971         sal_Bool bBreakMacroSign = sal_False;
972         if ( aArgs.get( "BreakMacroSignature" ) >>= bBreakMacroSign )
973         {
974             pObjectShell->BreakMacroSign_Impl( bBreakMacroSign );
975         }
976 
977         aArgs.remove( "WinExtent" );
978         aArgs.remove( "BreakMacroSignature" );
979         aArgs.remove( "Stream" );
980         aArgs.remove( "InputStream" );
981         aArgs.remove( "URL" );
982         aArgs.remove( "Frame" );
983         aArgs.remove( "Password" );
984         aArgs.remove( "EncryptionData" );
985 
986         // TODO/LATER: all the parameters that are accepted by ItemSet of the DocShell must be removed here
987 
988         m_pData->m_seqArguments = aArgs.getPropertyValues();
989 
990         SfxMedium* pMedium = pObjectShell->GetMedium();
991         if ( pMedium )
992         {
993             SfxAllItemSet aSet( pObjectShell->GetPool() );
994             TransformParameters( SID_OPENDOC, rArgs, aSet );
995 
996             // the arguments are not allowed to reach the medium
997             aSet.ClearItem( SID_FILE_NAME );
998             aSet.ClearItem( SID_FILLFRAME );
999 
1000             pMedium->GetItemSet()->Put( aSet );
1001             SFX_ITEMSET_ARG( &aSet, pItem, SfxStringItem, SID_FILTER_NAME, sal_False );
1002             if ( pItem )
1003                 pMedium->SetFilter(
1004                     pObjectShell->GetFactory().GetFilterContainer()->GetFilter4FilterName( pItem->GetValue() ) );
1005 
1006             SFX_ITEMSET_ARG( &aSet, pTitleItem, SfxStringItem, SID_DOCINFO_TITLE, sal_False );
1007             if ( pTitleItem )
1008             {
1009                 SfxViewFrame* pFrame = SfxViewFrame::GetFirst( pObjectShell );
1010                 if ( pFrame )
1011                     pFrame->UpdateTitle();
1012             }
1013         }
1014     }
1015 
1016     return sal_True ;
1017 }
1018 
1019 //________________________________________________________________________________________________________
1020 //  frame::XModel
1021 //________________________________________________________________________________________________________
1022 
getURL()1023 ::rtl::OUString SAL_CALL SfxBaseModel::getURL()
1024 {
1025     SfxModelGuard aGuard( *this );
1026     return m_pData->m_sURL ;
1027 }
1028 
1029 //________________________________________________________________________________________________________
1030 //  frame::XModel
1031 //________________________________________________________________________________________________________
1032 
getArgs()1033 uno::Sequence< beans::PropertyValue > SAL_CALL SfxBaseModel::getArgs()
1034 {
1035     SfxModelGuard aGuard( *this );
1036     if ( m_pData->m_pObjectShell.Is() )
1037     {
1038         uno::Sequence< beans::PropertyValue > seqArgsNew;
1039         uno::Sequence< beans::PropertyValue > seqArgsOld;
1040         SfxAllItemSet aSet( m_pData->m_pObjectShell->GetPool() );
1041 
1042         // we need to know which properties are supported by the transformer
1043         // hopefully it is a temporary solution, I guess nonconvertable properties
1044         // should not be supported so then there will be only ItemSet from medium
1045 
1046         TransformItems( SID_OPENDOC, *(m_pData->m_pObjectShell->GetMedium()->GetItemSet()), seqArgsNew );
1047         TransformParameters( SID_OPENDOC, m_pData->m_seqArguments, aSet );
1048         TransformItems( SID_OPENDOC, aSet, seqArgsOld );
1049 
1050         sal_Int32 nOrgLength = m_pData->m_seqArguments.getLength();
1051         sal_Int32 nOldLength = seqArgsOld.getLength();
1052         sal_Int32 nNewLength = seqArgsNew.getLength();
1053 
1054         // "WinExtent" property should be updated always.
1055         // We can store it now to overwrite an old value
1056         // since it is not from ItemSet
1057         Rectangle aTmpRect = m_pData->m_pObjectShell->GetVisArea( ASPECT_CONTENT );
1058         aTmpRect = OutputDevice::LogicToLogic( aTmpRect, m_pData->m_pObjectShell->GetMapUnit(), MAP_100TH_MM );
1059 
1060         Sequence< sal_Int32 > aRectSeq(4);
1061         aRectSeq[0] = aTmpRect.Left();
1062         aRectSeq[1] = aTmpRect.Top();
1063         aRectSeq[2] = aTmpRect.Right();
1064         aRectSeq[3] = aTmpRect.Bottom();
1065 
1066         seqArgsNew.realloc( ++nNewLength );
1067         seqArgsNew[ nNewLength - 1 ].Name = ::rtl::OUString::createFromAscii( "WinExtent" );
1068         seqArgsNew[ nNewLength - 1 ].Value <<= aRectSeq;
1069 
1070         if ( m_pData->m_aPreusedFilterName.getLength() )
1071         {
1072             seqArgsNew.realloc( ++nNewLength );
1073             seqArgsNew[ nNewLength - 1 ].Name = ::rtl::OUString::createFromAscii( "PreusedFilterName" );
1074             seqArgsNew[ nNewLength - 1 ].Value <<= m_pData->m_aPreusedFilterName;
1075         }
1076 
1077         SfxViewFrame* pFrame = SfxViewFrame::GetFirst( m_pData->m_pObjectShell );
1078         if ( pFrame )
1079         {
1080             SvBorder aBorder = pFrame->GetBorderPixelImpl( pFrame->GetViewShell() );
1081 
1082             Sequence< sal_Int32 > aBorderSeq(4);
1083             aBorderSeq[0] = aBorder.Left();
1084             aBorderSeq[1] = aBorder.Top();
1085             aBorderSeq[2] = aBorder.Right();
1086             aBorderSeq[3] = aBorder.Bottom();
1087 
1088             seqArgsNew.realloc( ++nNewLength );
1089             seqArgsNew[ nNewLength - 1 ].Name = ::rtl::OUString::createFromAscii( "DocumentBorder" );
1090             seqArgsNew[ nNewLength - 1 ].Value <<= aBorderSeq;
1091         }
1092 
1093         // only the values that are not supported by the ItemSet must be cached here
1094         uno::Sequence< beans::PropertyValue > aFinalCache;
1095         sal_Int32 nFinalLength = 0;
1096 
1097         for ( sal_Int32 nOrg = 0; nOrg < nOrgLength; nOrg++ )
1098         {
1099             sal_Int32 nOldInd = 0;
1100             while ( nOldInd < nOldLength )
1101             {
1102                 if ( m_pData->m_seqArguments[nOrg].Name.equals( seqArgsOld[nOldInd].Name ) )
1103                     break;
1104                 nOldInd++;
1105             }
1106 
1107             if ( nOldInd == nOldLength )
1108             {
1109                 // the entity with this name should be new for seqArgsNew
1110                 // since it is not supported by transformer
1111 
1112                 seqArgsNew.realloc( ++nNewLength );
1113                 seqArgsNew[ nNewLength - 1 ] = m_pData->m_seqArguments[nOrg];
1114 
1115                 aFinalCache.realloc( ++nFinalLength );
1116                 aFinalCache[ nFinalLength - 1 ] = m_pData->m_seqArguments[nOrg];
1117             }
1118         }
1119 
1120         m_pData->m_seqArguments = aFinalCache;
1121 
1122         return seqArgsNew;
1123     }
1124 
1125     return m_pData->m_seqArguments;
1126 }
1127 
1128 //________________________________________________________________________________________________________
1129 //  frame::XModel
1130 //________________________________________________________________________________________________________
1131 
connectController(const uno::Reference<frame::XController> & xController)1132 void SAL_CALL SfxBaseModel::connectController( const uno::Reference< frame::XController >& xController )
1133 {
1134     SfxModelGuard aGuard( *this );
1135     OSL_PRECOND( xController.is(), "SfxBaseModel::connectController: invalid controller!" );
1136     if ( !xController.is() )
1137         return;
1138 
1139     sal_uInt32 nOldCount = m_pData->m_seqControllers.getLength();
1140     uno::Sequence< uno::Reference< frame::XController > > aNewSeq( nOldCount + 1 );
1141     for ( sal_uInt32 n = 0; n < nOldCount; n++ )
1142         aNewSeq.getArray()[n] = m_pData->m_seqControllers.getConstArray()[n];
1143     aNewSeq.getArray()[nOldCount] = xController;
1144     m_pData->m_seqControllers = aNewSeq;
1145 
1146     if ( m_pData->m_seqControllers.getLength() == 1 )
1147     {
1148         SfxViewFrame* pViewFrame = SfxViewFrame::Get( xController, GetObjectShell() );
1149         ENSURE_OR_THROW( pViewFrame, "SFX document without SFX view!?" );
1150         pViewFrame->UpdateDocument_Impl();
1151         const String sDocumentURL = GetObjectShell()->GetMedium()->GetName();
1152         if ( sDocumentURL.Len() )
1153             SFX_APP()->Broadcast( SfxStringHint( SID_OPENURL, sDocumentURL ) );
1154     }
1155 }
1156 
1157 //________________________________________________________________________________________________________
1158 //  frame::XModel
1159 //________________________________________________________________________________________________________
1160 
disconnectController(const uno::Reference<frame::XController> & xController)1161 void SAL_CALL SfxBaseModel::disconnectController( const uno::Reference< frame::XController >& xController )
1162 {
1163     SfxModelGuard aGuard( *this );
1164 
1165     sal_uInt32 nOldCount = m_pData->m_seqControllers.getLength();
1166     if ( !nOldCount )
1167         return;
1168 
1169     uno::Sequence< uno::Reference< frame::XController > > aNewSeq( nOldCount - 1 );
1170     for ( sal_uInt32 nOld = 0, nNew = 0; nOld < nOldCount; ++nOld )
1171     {
1172         if ( xController != m_pData->m_seqControllers.getConstArray()[nOld] )
1173         {
1174             aNewSeq.getArray()[nNew] = m_pData->m_seqControllers.getConstArray()[nOld];
1175             ++nNew;
1176         }
1177     }
1178 
1179     m_pData->m_seqControllers = aNewSeq;
1180 
1181     if ( xController == m_pData->m_xCurrent )
1182         m_pData->m_xCurrent = uno::Reference< frame::XController > ();
1183 }
1184 
1185 namespace
1186 {
1187     typedef ::cppu::WeakImplHelper1< XUndoAction > ControllerLockUndoAction_Base;
1188     class ControllerLockUndoAction : public ControllerLockUndoAction_Base
1189     {
1190     public:
ControllerLockUndoAction(const Reference<XModel> & i_model,const bool i_undoIsUnlock)1191         ControllerLockUndoAction( const Reference< XModel >& i_model, const bool i_undoIsUnlock )
1192             :m_xModel( i_model )
1193             ,m_bUndoIsUnlock( i_undoIsUnlock )
1194         {
1195         }
1196 
1197         // XUndoAction
1198         virtual ::rtl::OUString SAL_CALL getTitle();
1199         virtual void SAL_CALL undo(  );
1200         virtual void SAL_CALL redo(  );
1201 
1202     private:
1203         const Reference< XModel >   m_xModel;
1204         const bool                  m_bUndoIsUnlock;
1205     };
1206 
getTitle()1207     ::rtl::OUString SAL_CALL ControllerLockUndoAction::getTitle()
1208     {
1209         // this action is intended to be used within an UndoContext only, so nobody will ever see this title ...
1210         return ::rtl::OUString();
1211     }
1212 
undo()1213     void SAL_CALL ControllerLockUndoAction::undo(  )
1214     {
1215         if ( m_bUndoIsUnlock )
1216             m_xModel->unlockControllers();
1217         else
1218             m_xModel->lockControllers();
1219     }
1220 
redo()1221     void SAL_CALL ControllerLockUndoAction::redo(  )
1222     {
1223         if ( m_bUndoIsUnlock )
1224             m_xModel->lockControllers();
1225         else
1226             m_xModel->unlockControllers();
1227     }
1228 }
1229 
1230 //________________________________________________________________________________________________________
1231 //  frame::XModel
1232 //________________________________________________________________________________________________________
1233 
lockControllers()1234 void SAL_CALL SfxBaseModel::lockControllers()
1235 {
1236     SfxModelGuard aGuard( *this );
1237 
1238     ++m_pData->m_nControllerLockCount ;
1239 
1240     if  (   m_pData->m_pDocumentUndoManager.is()
1241         &&  m_pData->m_pDocumentUndoManager->isInContext()
1242         &&  !m_pData->m_pDocumentUndoManager->isLocked()
1243         )
1244     {
1245         m_pData->m_pDocumentUndoManager->addUndoAction( new ControllerLockUndoAction( this, true ) );
1246     }
1247 }
1248 
1249 //________________________________________________________________________________________________________
1250 //  frame::XModel
1251 //________________________________________________________________________________________________________
1252 
unlockControllers()1253 void SAL_CALL SfxBaseModel::unlockControllers()
1254 {
1255     SfxModelGuard aGuard( *this );
1256 
1257     --m_pData->m_nControllerLockCount ;
1258 
1259     if  (   m_pData->m_pDocumentUndoManager.is()
1260         &&  m_pData->m_pDocumentUndoManager->isInContext()
1261         &&  !m_pData->m_pDocumentUndoManager->isLocked()
1262         )
1263     {
1264         m_pData->m_pDocumentUndoManager->addUndoAction( new ControllerLockUndoAction( this, false ) );
1265     }
1266 }
1267 
1268 //________________________________________________________________________________________________________
1269 //  frame::XModel
1270 //________________________________________________________________________________________________________
1271 
hasControllersLocked()1272 sal_Bool SAL_CALL SfxBaseModel::hasControllersLocked()
1273 {
1274     SfxModelGuard aGuard( *this );
1275     return ( m_pData->m_nControllerLockCount != 0 ) ;
1276 }
1277 
1278 //________________________________________________________________________________________________________
1279 //  frame::XModel
1280 //________________________________________________________________________________________________________
1281 
getCurrentController()1282 uno::Reference< frame::XController > SAL_CALL SfxBaseModel::getCurrentController()
1283 {
1284     SfxModelGuard aGuard( *this );
1285 
1286     // get the last active controller of this model
1287     if ( m_pData->m_xCurrent.is() )
1288         return m_pData->m_xCurrent;
1289 
1290     // get the first controller of this model
1291     return m_pData->m_seqControllers.getLength() ? m_pData->m_seqControllers.getConstArray()[0] : m_pData->m_xCurrent;
1292 }
1293 
1294 //________________________________________________________________________________________________________
1295 //  frame::XModel
1296 //________________________________________________________________________________________________________
1297 
setCurrentController(const uno::Reference<frame::XController> & xCurrentController)1298 void SAL_CALL SfxBaseModel::setCurrentController( const uno::Reference< frame::XController >& xCurrentController )
1299 {
1300     SfxModelGuard aGuard( *this );
1301 
1302     m_pData->m_xCurrent = xCurrentController;
1303 }
1304 
1305 //________________________________________________________________________________________________________
1306 //  frame::XModel
1307 //________________________________________________________________________________________________________
1308 
getCurrentSelection()1309 uno::Reference< uno::XInterface > SAL_CALL SfxBaseModel::getCurrentSelection()
1310 {
1311     SfxModelGuard aGuard( *this );
1312 
1313     uno::Reference< uno::XInterface >     xReturn;
1314     uno::Reference< frame::XController >    xController =   getCurrentController()      ;
1315 
1316     if ( xController.is() )
1317     {
1318         uno::Reference< view::XSelectionSupplier >  xDocView( xController, uno::UNO_QUERY );
1319         if ( xDocView.is() )
1320         {
1321             uno::Any xSel = xDocView->getSelection();
1322             xSel >>= xReturn ;
1323         }
1324     }
1325 
1326     return xReturn ;
1327 }
1328 
1329 //________________________________________________________________________________________________________
1330 //  XModifiable2
1331 //________________________________________________________________________________________________________
1332 
disableSetModified()1333 sal_Bool SAL_CALL SfxBaseModel::disableSetModified()
1334 {
1335     SfxModelGuard aGuard( *this );
1336 
1337     if ( !m_pData->m_pObjectShell.Is() )
1338         throw uno::RuntimeException();
1339 
1340     sal_Bool bResult = m_pData->m_pObjectShell->IsEnableSetModified();
1341     m_pData->m_pObjectShell->EnableSetModified( sal_False );
1342 
1343     return bResult;
1344 }
1345 
enableSetModified()1346 sal_Bool SAL_CALL SfxBaseModel::enableSetModified()
1347 {
1348     SfxModelGuard aGuard( *this );
1349 
1350     if ( !m_pData->m_pObjectShell.Is() )
1351         throw uno::RuntimeException();
1352 
1353     sal_Bool bResult = m_pData->m_pObjectShell->IsEnableSetModified();
1354     m_pData->m_pObjectShell->EnableSetModified( sal_True );
1355 
1356     return bResult;
1357 }
1358 
isSetModifiedEnabled()1359 sal_Bool SAL_CALL SfxBaseModel::isSetModifiedEnabled()
1360 {
1361     SfxModelGuard aGuard( *this );
1362 
1363     if ( !m_pData->m_pObjectShell.Is() )
1364         throw uno::RuntimeException();
1365 
1366     return m_pData->m_pObjectShell->IsEnableSetModified();
1367 }
1368 
1369 //________________________________________________________________________________________________________
1370 //  XModifiable
1371 //________________________________________________________________________________________________________
1372 
isModified()1373 sal_Bool SAL_CALL SfxBaseModel::isModified()
1374 {
1375     SfxModelGuard aGuard( *this );
1376 
1377     return m_pData->m_pObjectShell.Is() ? m_pData->m_pObjectShell->IsModified() : sal_False;
1378 }
1379 
1380 //________________________________________________________________________________________________________
1381 //  XModifiable
1382 //________________________________________________________________________________________________________
1383 
setModified(sal_Bool bModified)1384 void SAL_CALL SfxBaseModel::setModified( sal_Bool bModified )
1385 {
1386     SfxModelGuard aGuard( *this );
1387 
1388     if ( m_pData->m_pObjectShell.Is() )
1389         m_pData->m_pObjectShell->SetModified(bModified);
1390 }
1391 
1392 //________________________________________________________________________________________________________
1393 //  XModifiable
1394 //________________________________________________________________________________________________________
1395 
addModifyListener(const uno::Reference<XMODIFYLISTENER> & xListener)1396 void SAL_CALL SfxBaseModel::addModifyListener(const uno::Reference< XMODIFYLISTENER >& xListener)
1397 {
1398     SfxModelGuard aGuard( *this, SfxModelGuard::E_INITIALIZING );
1399 
1400     m_pData->m_aInterfaceContainer.addInterface( ::getCppuType((const uno::Reference< XMODIFYLISTENER >*)0),xListener );
1401 }
1402 
1403 //________________________________________________________________________________________________________
1404 //  XModifiable
1405 //________________________________________________________________________________________________________
1406 
removeModifyListener(const uno::Reference<XMODIFYLISTENER> & xListener)1407 void SAL_CALL SfxBaseModel::removeModifyListener(const uno::Reference< XMODIFYLISTENER >& xListener)
1408 {
1409     SfxModelGuard aGuard( *this );
1410 
1411     m_pData->m_aInterfaceContainer.removeInterface( ::getCppuType((const uno::Reference< XMODIFYLISTENER >*)0), xListener );
1412 }
1413 
1414 //____________________________________________________________________________________________________
1415 //  XCloseable
1416 //____________________________________________________________________________________________________
1417 
close(sal_Bool bDeliverOwnership)1418 void SAL_CALL SfxBaseModel::close( sal_Bool bDeliverOwnership )
1419 {
1420     static ::rtl::OUString MSG_1 = ::rtl::OUString::createFromAscii("Can't close while saving.");
1421 
1422     ::vos::OGuard aGuard( Application::GetSolarMutex() );
1423     if ( impl_isDisposed() || m_pData->m_bClosed || m_pData->m_bClosing )
1424         return;
1425 
1426     uno::Reference< uno::XInterface > xSelfHold( static_cast< ::cppu::OWeakObject* >(this) );
1427     lang::EventObject             aSource    (static_cast< ::cppu::OWeakObject*>(this));
1428     ::cppu::OInterfaceContainerHelper* pContainer = m_pData->m_aInterfaceContainer.getContainer( ::getCppuType( ( const uno::Reference< util::XCloseListener >*) NULL ) );
1429     if (pContainer!=NULL)
1430     {
1431         ::cppu::OInterfaceIteratorHelper pIterator(*pContainer);
1432         while (pIterator.hasMoreElements())
1433         {
1434             try
1435             {
1436                 ((util::XCloseListener*)pIterator.next())->queryClosing( aSource, bDeliverOwnership );
1437             }
1438             catch( uno::RuntimeException& )
1439             {
1440                 pIterator.remove();
1441             }
1442         }
1443     }
1444 
1445     if ( m_pData->m_bSaving )
1446     {
1447         if (bDeliverOwnership)
1448             m_pData->m_bSuicide = sal_True;
1449         throw util::CloseVetoException(
1450                 MSG_1,
1451                 static_cast< ::com::sun::star::util::XCloseable* >(this));
1452     }
1453 
1454     // no own objections against closing!
1455     m_pData->m_bClosing = sal_True;
1456     pContainer = m_pData->m_aInterfaceContainer.getContainer( ::getCppuType( ( const uno::Reference< util::XCloseListener >*) NULL ) );
1457     if (pContainer!=NULL)
1458     {
1459         ::cppu::OInterfaceIteratorHelper pCloseIterator(*pContainer);
1460         while (pCloseIterator.hasMoreElements())
1461         {
1462             try
1463             {
1464                 ((util::XCloseListener*)pCloseIterator.next())->notifyClosing( aSource );
1465             }
1466             catch( uno::RuntimeException& )
1467             {
1468                 pCloseIterator.remove();
1469             }
1470         }
1471     }
1472 
1473     m_pData->m_bClosed = sal_True;
1474     m_pData->m_bClosing = sal_False;
1475 
1476     dispose();
1477 }
1478 
1479 //____________________________________________________________________________________________________
1480 //  XCloseBroadcaster
1481 //____________________________________________________________________________________________________
1482 
addCloseListener(const uno::Reference<XCLOSELISTENER> & xListener)1483 void SAL_CALL SfxBaseModel::addCloseListener( const uno::Reference< XCLOSELISTENER >& xListener )
1484 {
1485     SfxModelGuard aGuard( *this, SfxModelGuard::E_INITIALIZING );
1486 
1487     m_pData->m_aInterfaceContainer.addInterface( ::getCppuType((const uno::Reference< XCLOSELISTENER >*)0), xListener );
1488 }
1489 
1490 //____________________________________________________________________________________________________
1491 //  XCloseBroadcaster
1492 //____________________________________________________________________________________________________
1493 
removeCloseListener(const uno::Reference<XCLOSELISTENER> & xListener)1494 void SAL_CALL SfxBaseModel::removeCloseListener( const uno::Reference< XCLOSELISTENER >& xListener )
1495 {
1496     SfxModelGuard aGuard( *this );
1497 
1498     m_pData->m_aInterfaceContainer.removeInterface( ::getCppuType((const uno::Reference< XCLOSELISTENER >*)0), xListener );
1499 }
1500 
1501 //________________________________________________________________________________________________________
1502 //  XPrintable
1503 //________________________________________________________________________________________________________
1504 
getPrinter()1505 uno::Sequence< beans::PropertyValue > SAL_CALL SfxBaseModel::getPrinter()
1506 {
1507     SfxModelGuard aGuard( *this );
1508 
1509     if ( impl_getPrintHelper() )
1510         return m_pData->m_xPrintable->getPrinter();
1511     else
1512         return uno::Sequence< beans::PropertyValue >();
1513 }
1514 
setPrinter(const uno::Sequence<beans::PropertyValue> & rPrinter)1515 void SAL_CALL SfxBaseModel::setPrinter(const uno::Sequence< beans::PropertyValue >& rPrinter)
1516 {
1517     SfxModelGuard aGuard( *this );
1518 
1519     if ( impl_getPrintHelper() )
1520         m_pData->m_xPrintable->setPrinter( rPrinter );
1521 }
1522 
print(const uno::Sequence<beans::PropertyValue> & rOptions)1523 void SAL_CALL SfxBaseModel::print(const uno::Sequence< beans::PropertyValue >& rOptions)
1524 {
1525     SfxModelGuard aGuard( *this );
1526 
1527     if ( impl_getPrintHelper() )
1528         m_pData->m_xPrintable->print( rOptions );
1529 }
1530 
1531 //________________________________________________________________________________________________________
1532 //  XStorable
1533 //________________________________________________________________________________________________________
1534 
hasLocation()1535 sal_Bool SAL_CALL SfxBaseModel::hasLocation()
1536 {
1537     SfxModelGuard aGuard( *this );
1538 
1539     return m_pData->m_pObjectShell.Is() ? m_pData->m_pObjectShell->HasName() : sal_False;
1540 }
1541 
1542 //________________________________________________________________________________________________________
1543 //  XStorable
1544 //________________________________________________________________________________________________________
1545 
getLocation()1546 ::rtl::OUString SAL_CALL SfxBaseModel::getLocation()
1547 {
1548     SfxModelGuard aGuard( *this );
1549 
1550     if ( m_pData->m_pObjectShell.Is() )
1551     {
1552         // TODO/LATER: is it correct that the shared document returns shared file location?
1553         if ( m_pData->m_pObjectShell->IsDocShared() )
1554             return m_pData->m_pObjectShell->GetSharedFileURL();
1555         else
1556             return ::rtl::OUString(m_pData->m_pObjectShell->GetMedium()->GetName());
1557     }
1558 
1559     return m_pData->m_sURL;
1560 }
1561 
1562 //________________________________________________________________________________________________________
1563 //  XStorable
1564 //________________________________________________________________________________________________________
1565 
isReadonly()1566 sal_Bool SAL_CALL SfxBaseModel::isReadonly()
1567 {
1568     SfxModelGuard aGuard( *this );
1569 
1570     return m_pData->m_pObjectShell.Is() ? m_pData->m_pObjectShell->IsReadOnly() : sal_True;
1571 }
1572 
1573 //________________________________________________________________________________________________________
1574 //  XStorable2
1575 //________________________________________________________________________________________________________
1576 
storeSelf(const uno::Sequence<beans::PropertyValue> & aSeqArgs)1577 void SAL_CALL SfxBaseModel::storeSelf( const    uno::Sequence< beans::PropertyValue >&  aSeqArgs )
1578 {
1579     RTL_LOGFILE_PRODUCT_CONTEXT( aPerfLog, "PERFORMANCE - SfxBaseModel::storeSelf" );
1580 
1581     SfxModelGuard aGuard( *this );
1582 
1583     if ( m_pData->m_pObjectShell.Is() )
1584     {
1585         m_pData->m_pObjectShell->AddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "storeSelf" ) ) );
1586         SfxSaveGuard aSaveGuard(this, m_pData, sal_False);
1587 
1588         for ( sal_Int32 nInd = 0; nInd < aSeqArgs.getLength(); nInd++ )
1589         {
1590             // check that only acceptable parameters are provided here
1591             if ( !aSeqArgs[nInd].Name.equals( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "VersionComment" ) ) )
1592               && !aSeqArgs[nInd].Name.equals( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Author" ) ) )
1593               && !aSeqArgs[nInd].Name.equals( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "InteractionHandler" ) ) )
1594               && !aSeqArgs[nInd].Name.equals( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "StatusIndicator" ) ) )
1595               && !aSeqArgs[nInd].Name.equals( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "FailOnWarning" ) ) ) )
1596             {
1597                 m_pData->m_pObjectShell->AddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "unexpected parameter for storeSelf, might be no problem if SaveAs is executed." ) ) );
1598                 m_pData->m_pObjectShell->StoreLog();
1599 
1600                 ::rtl::OUString aMessage( RTL_CONSTASCII_USTRINGPARAM( "Unexpected MediaDescriptor parameter: " ) );
1601                 aMessage += aSeqArgs[nInd].Name;
1602                 throw lang::IllegalArgumentException( aMessage, uno::Reference< uno::XInterface >(), 1 );
1603             }
1604         }
1605 
1606         SfxAllItemSet *pParams = new SfxAllItemSet( SFX_APP()->GetPool() );
1607         TransformParameters( SID_SAVEDOC, aSeqArgs, *pParams );
1608 
1609         SFX_APP()->NotifyEvent( SfxEventHint( SFX_EVENT_SAVEDOC, GlobalEventConfig::GetEventName(STR_EVENT_SAVEDOC), m_pData->m_pObjectShell ) );
1610 
1611         sal_Bool bRet = sal_False;
1612 
1613         // TODO/LATER: let the embedded case of saving be handled more careful
1614         if ( m_pData->m_pObjectShell->GetCreateMode() == SFX_CREATE_MODE_EMBEDDED )
1615         {
1616             // If this is an embedded object that has no URL based location it should be stored to own storage.
1617             // An embedded object can have a location based on URL in case it is a link, then it should be
1618             // stored in normal way.
1619             if ( !hasLocation() || getLocation().compareToAscii( "private:", 8 ) == 0 )
1620             {
1621                 // actually in this very rare case only UI parameters have sense
1622                 // TODO/LATER: should be done later, after integration of sb19
1623                 bRet = m_pData->m_pObjectShell->DoSave()
1624                     && m_pData->m_pObjectShell->DoSaveCompleted();
1625             }
1626             else
1627             {
1628                 bRet = m_pData->m_pObjectShell->Save_Impl( pParams );
1629             }
1630         }
1631         else
1632             bRet = m_pData->m_pObjectShell->Save_Impl( pParams );
1633 
1634         DELETEZ( pParams );
1635 
1636         sal_uInt32 nErrCode = m_pData->m_pObjectShell->GetError() ? m_pData->m_pObjectShell->GetError()
1637                                                                     : ERRCODE_IO_CANTWRITE;
1638         m_pData->m_pObjectShell->ResetError();
1639 
1640         if ( bRet )
1641         {
1642             m_pData->m_pObjectShell->AddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "successful saving." ) ) );
1643             m_pData->m_aPreusedFilterName = GetMediumFilterName_Impl();
1644 
1645             SFX_APP()->NotifyEvent( SfxEventHint( SFX_EVENT_SAVEDOCDONE, GlobalEventConfig::GetEventName(STR_EVENT_SAVEDOCDONE), m_pData->m_pObjectShell ) );
1646         }
1647         else
1648         {
1649             m_pData->m_pObjectShell->AddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Storing failed!" ) ) );
1650             m_pData->m_pObjectShell->StoreLog();
1651 
1652             // write the contents of the logger to the file
1653             SFX_APP()->NotifyEvent( SfxEventHint( SFX_EVENT_SAVEDOCFAILED, GlobalEventConfig::GetEventName(STR_EVENT_SAVEDOCFAILED), m_pData->m_pObjectShell ) );
1654 
1655             throw task::ErrorCodeIOException( ::rtl::OUString(), uno::Reference< uno::XInterface >(), nErrCode );
1656         }
1657     }
1658 
1659 }
1660 
1661 //________________________________________________________________________________________________________
1662 //  XStorable
1663 //________________________________________________________________________________________________________
1664 
store()1665 void SAL_CALL SfxBaseModel::store()
1666 {
1667     storeSelf( uno::Sequence< beans::PropertyValue >() );
1668 }
1669 
1670 //________________________________________________________________________________________________________
1671 //  XStorable
1672 //________________________________________________________________________________________________________
1673 
storeAsURL(const::rtl::OUString & rURL,const uno::Sequence<beans::PropertyValue> & rArgs)1674 void SAL_CALL SfxBaseModel::storeAsURL( const   ::rtl::OUString&                   rURL    ,
1675                                         const   uno::Sequence< beans::PropertyValue >&  rArgs   )
1676 {
1677     RTL_LOGFILE_PRODUCT_CONTEXT( aPerfLog, "PERFORMANCE - SfxBaseModel::storeAsURL" );
1678 
1679     SfxModelGuard aGuard( *this );
1680 
1681     if ( m_pData->m_pObjectShell.Is() )
1682     {
1683         m_pData->m_pObjectShell->AddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "storeAsURL" ) ) );
1684         SfxSaveGuard aSaveGuard(this, m_pData, sal_False);
1685 
1686         impl_store( rURL, rArgs, sal_False );
1687 
1688         uno::Sequence< beans::PropertyValue > aSequence ;
1689         TransformItems( SID_OPENDOC, *m_pData->m_pObjectShell->GetMedium()->GetItemSet(), aSequence );
1690         attachResource( rURL, aSequence );
1691 
1692 #if OSL_DEBUG_LEVEL > 0
1693         SFX_ITEMSET_ARG( m_pData->m_pObjectShell->GetMedium()->GetItemSet(), pPasswdItem, SfxStringItem, SID_PASSWORD, sal_False);
1694         OSL_ENSURE( !pPasswdItem, "There should be no Password property in the document MediaDescriptor!" );
1695 #endif
1696     }
1697 }
1698 
1699 //________________________________________________________________________________________________________
1700 //  XUndoManagerSupplier
1701 //________________________________________________________________________________________________________
getUndoManager()1702 Reference< XUndoManager > SAL_CALL SfxBaseModel::getUndoManager(  )
1703 {
1704     SfxModelGuard aGuard( *this );
1705     if ( !m_pData->m_pDocumentUndoManager.is() )
1706         m_pData->m_pDocumentUndoManager.set( new ::sfx2::DocumentUndoManager( *this ) );
1707     return m_pData->m_pDocumentUndoManager.get();
1708 }
1709 
1710 //________________________________________________________________________________________________________
1711 //  XStorable
1712 //________________________________________________________________________________________________________
1713 
storeToURL(const::rtl::OUString & rURL,const uno::Sequence<beans::PropertyValue> & rArgs)1714 void SAL_CALL SfxBaseModel::storeToURL( const   ::rtl::OUString&                   rURL    ,
1715                                         const   uno::Sequence< beans::PropertyValue >&  rArgs   )
1716 {
1717     SfxModelGuard aGuard( *this );
1718 
1719     if ( m_pData->m_pObjectShell.Is() )
1720     {
1721         m_pData->m_pObjectShell->AddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "storeToURL" ) ) );
1722         SfxSaveGuard aSaveGuard(this, m_pData, sal_False);
1723         impl_store( rURL, rArgs, sal_True );
1724     }
1725 }
1726 
wasModifiedSinceLastSave()1727 ::sal_Bool SAL_CALL SfxBaseModel::wasModifiedSinceLastSave()
1728 {
1729     SfxModelGuard aGuard( *this );
1730     return m_pData->m_bModifiedSinceLastSave;
1731 }
1732 
storeToRecoveryFile(const::rtl::OUString & i_TargetLocation,const Sequence<PropertyValue> & i_MediaDescriptor)1733 void SAL_CALL SfxBaseModel::storeToRecoveryFile( const ::rtl::OUString& i_TargetLocation, const Sequence< PropertyValue >& i_MediaDescriptor )
1734 {
1735     SfxModelGuard aGuard( *this );
1736 
1737     // delegate
1738     SfxSaveGuard aSaveGuard( this, m_pData, sal_False );
1739     impl_store( i_TargetLocation, i_MediaDescriptor, sal_True );
1740 
1741     // no need for subsequent calls to storeToRecoveryFile, unless we're modified, again
1742     m_pData->m_bModifiedSinceLastSave = sal_False;
1743 }
1744 
recoverFromFile(const::rtl::OUString & i_SourceLocation,const::rtl::OUString & i_SalvagedFile,const Sequence<PropertyValue> & i_MediaDescriptor)1745 void SAL_CALL SfxBaseModel::recoverFromFile( const ::rtl::OUString& i_SourceLocation, const ::rtl::OUString& i_SalvagedFile, const Sequence< PropertyValue >& i_MediaDescriptor )
1746 {
1747     SfxModelGuard aGuard( *this, SfxModelGuard::E_INITIALIZING );
1748 
1749     // delegate to our "load" method
1750     ::comphelper::NamedValueCollection aMediaDescriptor( i_MediaDescriptor );
1751 
1752     // our load implementation expects the SalvagedFile to be in the media descriptor
1753     OSL_ENSURE( !aMediaDescriptor.has( "SalvagedFile" ) || ( aMediaDescriptor.getOrDefault( "SalvagedFile", ::rtl::OUString() ) == i_SalvagedFile ),
1754         "SfxBaseModel::recoverFromFile: inconsistent information!" );
1755     aMediaDescriptor.put( "SalvagedFile", i_SalvagedFile );
1756 
1757     // similar for the to-be-loaded file
1758     OSL_ENSURE( !aMediaDescriptor.has( "URL" ) || ( aMediaDescriptor.getOrDefault( "URL", ::rtl::OUString() ) == i_SourceLocation ),
1759         "SfxBaseModel::recoverFromFile: inconsistent information!" );
1760     aMediaDescriptor.put( "URL", i_SourceLocation );
1761 
1762     load( aMediaDescriptor.getPropertyValues() );
1763 
1764     // Note: The XDocumentRecovery interface specification requires us to do an attachResource after loading.
1765     // However, we will not do this here, as we know that our load implementation (respectively some method
1766     // called from there) already did so.
1767     // In particular, the load process might already have modified some elements of the media
1768     // descriptor, for instance the MacroExecMode (in case the user was involved to decide about it), and we do
1769     // not want to overwrite it with the "old" elements passed to this method here.
1770 }
1771 
1772 //________________________________________________________________________________________________________
1773 // XLoadable
1774 //________________________________________________________________________________________________________
1775 
initNew()1776 void SAL_CALL SfxBaseModel::initNew()
1777 {
1778     SfxModelGuard aGuard( *this, SfxModelGuard::E_INITIALIZING );
1779     if ( IsInitialized() )
1780         throw ::com::sun::star::frame::DoubleInitializationException( ::rtl::OUString(), *this );
1781 
1782     // the object shell should exist always
1783     DBG_ASSERT( m_pData->m_pObjectShell.Is(), "Model is useless without an ObjectShell" );
1784     if ( m_pData->m_pObjectShell.Is() )
1785     {
1786         if( m_pData->m_pObjectShell->GetMedium() )
1787             throw DOUBLEINITIALIZATIONEXCEPTION();
1788 
1789         sal_Bool bRes = m_pData->m_pObjectShell->DoInitNew( NULL );
1790         sal_uInt32 nErrCode = m_pData->m_pObjectShell->GetError() ?
1791                                     m_pData->m_pObjectShell->GetError() : ERRCODE_IO_CANTCREATE;
1792         m_pData->m_pObjectShell->ResetError();
1793 
1794         if ( !bRes )
1795             throw task::ErrorCodeIOException( ::rtl::OUString(), uno::Reference< uno::XInterface >(), nErrCode );
1796     }
1797 }
1798 
1799 //________________________________________________________________________________________________________
1800 // XLoadable
1801 //________________________________________________________________________________________________________
1802 
load(const uno::Sequence<beans::PropertyValue> & seqArguments)1803 void SAL_CALL SfxBaseModel::load(   const uno::Sequence< beans::PropertyValue >& seqArguments )
1804 {
1805     SfxModelGuard aGuard( *this, SfxModelGuard::E_INITIALIZING );
1806     if ( IsInitialized() )
1807         throw ::com::sun::star::frame::DoubleInitializationException( ::rtl::OUString(), *this );
1808 
1809     // the object shell should exist always
1810     DBG_ASSERT( m_pData->m_pObjectShell.Is(), "Model is useless without an ObjectShell" );
1811 
1812     if ( m_pData->m_pObjectShell.Is() )
1813     {
1814         if( m_pData->m_pObjectShell->GetMedium() )
1815             // if a Medium is present, the document is already initialized
1816             throw DOUBLEINITIALIZATIONEXCEPTION();
1817 
1818         SfxMedium* pMedium = new SfxMedium( seqArguments );
1819         String aFilterName;
1820         SFX_ITEMSET_ARG( pMedium->GetItemSet(), pFilterNameItem, SfxStringItem, SID_FILTER_NAME, sal_False );
1821         if( pFilterNameItem )
1822             aFilterName = pFilterNameItem->GetValue();
1823         if( !m_pData->m_pObjectShell->GetFactory().GetFilterContainer()->GetFilter4FilterName( aFilterName ) )
1824         {
1825             // filtername is not valid
1826             delete pMedium;
1827             throw frame::IllegalArgumentIOException();
1828         }
1829 
1830         // !TODO: currently not working
1831         //SFX_ITEMSET_ARG( pParams, pFrameItem, SfxFrameItem, SID_DOCFRAME, sal_False );
1832         //if( pFrameItem && pFrameItem->GetFrame() )
1833         //{
1834         //  SfxFrame* pFrame = pFrameItem->GetFrame();
1835         //  pMedium->SetLoadTargetFrame( pFrame );
1836         //}
1837 
1838         SFX_ITEMSET_ARG( pMedium->GetItemSet(), pSalvageItem, SfxStringItem, SID_DOC_SALVAGE, sal_False );
1839         sal_Bool bSalvage = pSalvageItem ? sal_True : sal_False;
1840 
1841         // SFX_ITEMSET_ARG( pMedium->GetItemSet(), pTemplateItem, SfxBoolItem, SID_TEMPLATE, sal_False);
1842         // sal_Bool bTemplate = pTemplateItem && pTemplateItem->GetValue();
1843         //
1844         // does already happen in DoLoad call
1845         //m_pData->m_pObjectShell->SetActivateEvent_Impl( bTemplate ? SFX_EVENT_CREATEDOC : SFX_EVENT_OPENDOC );
1846 
1847         // load document
1848         sal_uInt32 nError = ERRCODE_NONE;
1849         if ( !m_pData->m_pObjectShell->DoLoad(pMedium) )
1850             nError=ERRCODE_IO_GENERAL;
1851 
1852         // QUESTION: if the following happens outside of DoLoad, something important is missing there!
1853         ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionHandler > xHandler = pMedium->GetInteractionHandler();
1854         if( m_pData->m_pObjectShell->GetErrorCode() )
1855         {
1856             nError = m_pData->m_pObjectShell->GetErrorCode();
1857             if ( nError == ERRCODE_IO_BROKENPACKAGE && xHandler.is() )
1858             {
1859                 ::rtl::OUString aDocName = pMedium->GetURLObject().getName( INetURLObject::LAST_SEGMENT, true, INetURLObject::DECODE_WITH_CHARSET );
1860                 SFX_ITEMSET_ARG( pMedium->GetItemSet(), pRepairItem, SfxBoolItem, SID_REPAIRPACKAGE, sal_False );
1861                 if ( !pRepairItem || !pRepairItem->GetValue() )
1862                 {
1863                     RequestPackageReparation aRequest( aDocName );
1864                     xHandler->handle( aRequest.GetRequest() );
1865                     if( aRequest.isApproved() )
1866                     {
1867                         // broken package: try second loading and allow repair
1868                         pMedium->GetItemSet()->Put( SfxBoolItem( SID_REPAIRPACKAGE, sal_True ) );
1869                         pMedium->GetItemSet()->Put( SfxBoolItem( SID_TEMPLATE, sal_True ) );
1870                         pMedium->GetItemSet()->Put( SfxStringItem( SID_DOCINFO_TITLE, aDocName ) );
1871 
1872                         // the error must be reset and the storage must be reopened in new mode
1873                         pMedium->ResetError();
1874                         pMedium->CloseStorage();
1875                         m_pData->m_pObjectShell->PrepareSecondTryLoad_Impl();
1876                         if ( !m_pData->m_pObjectShell->DoLoad(pMedium) )
1877                             nError=ERRCODE_IO_GENERAL;
1878                         nError = m_pData->m_pObjectShell->GetErrorCode();
1879                     }
1880                 }
1881 
1882                 if ( nError == ERRCODE_IO_BROKENPACKAGE )
1883                 {
1884                     // repair either not allowed or not successful
1885                     NotifyBrokenPackage aRequest( aDocName );
1886                     xHandler->handle( aRequest.GetRequest() );
1887                 }
1888             }
1889         }
1890 
1891         if( m_pData->m_pObjectShell->IsAbortingImport() )
1892             nError = ERRCODE_ABORT;
1893 
1894         if( bSalvage )
1895         {
1896             // file recovery: restore original filter
1897             SFX_ITEMSET_ARG( pMedium->GetItemSet(), pFilterItem, SfxStringItem, SID_FILTER_NAME, sal_False );
1898             SfxFilterMatcher& rMatcher = SFX_APP()->GetFilterMatcher();
1899             const SfxFilter* pSetFilter = rMatcher.GetFilter4FilterName( pFilterItem->GetValue() );
1900             pMedium->SetFilter( pSetFilter );
1901             m_pData->m_pObjectShell->SetModified(sal_True);
1902         }
1903 
1904         // TODO/LATER: may be the mode should be retrieved from outside and the preused filter should not be set
1905         if ( m_pData->m_pObjectShell->GetCreateMode() == SFX_CREATE_MODE_EMBEDDED )
1906         {
1907             SFX_ITEMSET_ARG( pMedium->GetItemSet(), pFilterItem, SfxStringItem, SID_FILTER_NAME, sal_False );
1908             if ( pFilterItem )
1909                 m_pData->m_aPreusedFilterName = pFilterItem->GetValue();
1910         }
1911 
1912         if ( !nError )
1913             nError = pMedium->GetError();
1914 
1915         m_pData->m_pObjectShell->ResetError();
1916 
1917         if ( nError )
1918         {
1919             sal_Bool bSilent = sal_False;
1920             SFX_ITEMSET_ARG( pMedium->GetItemSet(), pSilentItem, SfxBoolItem, SID_SILENT, sal_False);
1921             if( pSilentItem )
1922                 bSilent = pSilentItem->GetValue();
1923 
1924             sal_Bool bWarning = ((nError & ERRCODE_WARNING_MASK) == ERRCODE_WARNING_MASK);
1925             if ( nError != ERRCODE_IO_BROKENPACKAGE && !bSilent )
1926             {
1927                 // broken package was handled already
1928                 if ( SfxObjectShell::UseInteractionToHandleError( xHandler, nError ) && !bWarning )
1929                 {
1930                     // abort loading (except for warnings)
1931                     nError = ERRCODE_IO_ABORT;
1932                 }
1933             }
1934 
1935             if ( m_pData->m_pObjectShell->GetMedium() != pMedium )
1936             {
1937                 // for whatever reason document now has another medium
1938                 DBG_ERROR("Document has rejected the medium?!");
1939                 delete pMedium;
1940             }
1941 
1942             if ( !bWarning )    // #i30711# don't abort loading if it's only a warning
1943             {
1944                 throw task::ErrorCodeIOException( ::rtl::OUString(),
1945                                                     uno::Reference< uno::XInterface >(),
1946                                                     nError ? nError : ERRCODE_IO_CANTREAD );
1947             }
1948         }
1949 
1950         sal_Bool bHidden = sal_False;
1951         SFX_ITEMSET_ARG( pMedium->GetItemSet(), pHidItem, SfxBoolItem, SID_HIDDEN, sal_False);
1952         if ( pHidItem )
1953             bHidden = pHidItem->GetValue();
1954 
1955 #if OSL_DEBUG_LEVEL > 0
1956         SFX_ITEMSET_ARG( pMedium->GetItemSet(), pPasswdItem, SfxStringItem, SID_PASSWORD, sal_False);
1957         OSL_ENSURE( !pPasswdItem, "There should be no Password property in the document MediaDescriptor!" );
1958 #endif
1959         // !TODO: will be done by Framework!
1960         pMedium->SetUpdatePickList( !bHidden );
1961     }
1962 }
1963 
1964 //________________________________________________________________________________________________________
1965 // XTransferable
1966 //________________________________________________________________________________________________________
1967 
getTransferData(const DATAFLAVOR & aFlavor)1968 uno::Any SAL_CALL SfxBaseModel::getTransferData( const DATAFLAVOR& aFlavor )
1969 {
1970     SfxModelGuard aGuard( *this );
1971 
1972     uno::Any aAny;
1973 
1974     if ( m_pData->m_pObjectShell.Is() )
1975     {
1976         if ( aFlavor.MimeType.equalsAscii( "application/x-openoffice-objectdescriptor-xml;windows_formatname=\"Star Object Descriptor (XML)\"" ) )
1977         {
1978             if ( aFlavor.DataType == getCppuType( (const Sequence< sal_Int8 >*) 0 ) )
1979             {
1980                 TransferableObjectDescriptor aDesc;
1981 
1982                 aDesc.maClassName = m_pData->m_pObjectShell->GetClassName();
1983                 aDesc.maTypeName = aFlavor.HumanPresentableName;
1984 
1985                 // TODO/LATER: ViewAspect needs to be sal_Int64
1986                 aDesc.mnViewAspect = sal::static_int_cast< sal_uInt16 >( embed::Aspects::MSOLE_CONTENT );
1987 
1988                 //TODO/LATER: status needs to become sal_Int64
1989                 aDesc.mnOle2Misc = m_pData->m_pObjectShell->GetMiscStatus();
1990                 Size aSize = m_pData->m_pObjectShell->GetVisArea().GetSize();
1991 
1992                 MapUnit aMapUnit = m_pData->m_pObjectShell->GetMapUnit();
1993                 aDesc.maSize = OutputDevice::LogicToLogic( aSize, aMapUnit, MAP_100TH_MM );
1994                 aDesc.maDragStartPos = Point();
1995                 aDesc.maDisplayName = String();
1996                 aDesc.mbCanLink = sal_False;
1997 
1998                 SvMemoryStream aMemStm( 1024, 1024 );
1999                 aMemStm << aDesc;
2000                 aAny <<= Sequence< sal_Int8 >( reinterpret_cast< const sal_Int8* >( aMemStm.GetData() ), aMemStm.Tell() );
2001             }
2002             else
2003                 throw datatransfer::UnsupportedFlavorException();
2004         }
2005         else if ( aFlavor.MimeType.equalsAscii( "application/x-openoffice-embed-source;windows_formatname=\"Star EMBS\"" ) )
2006         {
2007             if ( aFlavor.DataType == getCppuType( (const Sequence< sal_Int8 >*) 0 ) )
2008             {
2009                 try
2010                 {
2011                     utl::TempFile aTmp;
2012                     aTmp.EnableKillingFile( sal_True );
2013                     storeToURL( aTmp.GetURL(), uno::Sequence < beans::PropertyValue >() );
2014                     SvStream* pStream = aTmp.GetStream( STREAM_READ );
2015                     const sal_uInt32 nLen = pStream->Seek( STREAM_SEEK_TO_END );
2016                     ::com::sun::star::uno::Sequence< sal_Int8 > aSeq( nLen );
2017                     pStream->Seek( STREAM_SEEK_TO_BEGIN );
2018                     pStream->Read( aSeq.getArray(),  nLen );
2019                     delete pStream;
2020                     if( aSeq.getLength() )
2021                         aAny <<= aSeq;
2022                 }
2023                 catch ( uno::Exception& )
2024                 {
2025                 }
2026             }
2027             else
2028                 throw datatransfer::UnsupportedFlavorException();
2029         }
2030         else if ( aFlavor.MimeType.equalsAscii( "application/x-openoffice-gdimetafile;windows_formatname=\"GDIMetaFile\"" ) )
2031         {
2032             if ( aFlavor.DataType == getCppuType( (const Sequence< sal_Int8 >*) 0 ) )
2033             {
2034 
2035                 ::boost::shared_ptr<GDIMetaFile> pMetaFile =
2036                     m_pData->m_pObjectShell->GetPreviewMetaFile( sal_True );
2037 
2038                 if ( pMetaFile )
2039                 {
2040                     SvMemoryStream aMemStm( 65535, 65535 );
2041                     aMemStm.SetVersion( SOFFICE_FILEFORMAT_CURRENT );
2042 
2043                     pMetaFile->Write( aMemStm );
2044                     aAny <<= Sequence< sal_Int8 >( reinterpret_cast< const sal_Int8* >( aMemStm.GetData() ),
2045                                                     aMemStm.Seek( STREAM_SEEK_TO_END ) );
2046                 }
2047             }
2048             else
2049                 throw datatransfer::UnsupportedFlavorException();
2050         }
2051         else if ( aFlavor.MimeType.equalsAscii( "application/x-openoffice-highcontrast-gdimetafile;windows_formatname=\"GDIMetaFile\"" ) )
2052         {
2053             if ( aFlavor.DataType == getCppuType( (const Sequence< sal_Int8 >*) 0 ) )
2054             {
2055                 ::boost::shared_ptr<GDIMetaFile> pMetaFile =
2056                     m_pData->m_pObjectShell->CreatePreviewMetaFile_Impl( sal_True, sal_True );
2057 
2058                 if ( pMetaFile )
2059                 {
2060                     SvMemoryStream aMemStm( 65535, 65535 );
2061                     aMemStm.SetVersion( SOFFICE_FILEFORMAT_CURRENT );
2062 
2063                     pMetaFile->Write( aMemStm );
2064                     aAny <<= Sequence< sal_Int8 >( reinterpret_cast< const sal_Int8* >( aMemStm.GetData() ),
2065                                                     aMemStm.Seek( STREAM_SEEK_TO_END ) );
2066                 }
2067             }
2068             else
2069                 throw datatransfer::UnsupportedFlavorException();
2070         }
2071         else if ( aFlavor.MimeType.equalsAscii( "application/x-openoffice-emf;windows_formatname=\"Image EMF\"" ) )
2072         {
2073             if ( aFlavor.DataType == getCppuType( (const Sequence< sal_Int8 >*) 0 ) )
2074             {
2075                 ::boost::shared_ptr<GDIMetaFile> pMetaFile =
2076                     m_pData->m_pObjectShell->GetPreviewMetaFile( sal_True );
2077 
2078                 if ( pMetaFile )
2079                 {
2080                     ::boost::shared_ptr<SvMemoryStream> pStream(
2081                         GraphicHelper::getFormatStrFromGDI_Impl(
2082                             pMetaFile.get(), CVT_EMF ) );
2083                     if ( pStream )
2084                     {
2085                         pStream->SetVersion( SOFFICE_FILEFORMAT_CURRENT );
2086                         aAny <<= Sequence< sal_Int8 >( reinterpret_cast< const sal_Int8* >( pStream->GetData() ),
2087                                                         pStream->Seek( STREAM_SEEK_TO_END ) );
2088                     }
2089                 }
2090             }
2091             else if ( GraphicHelper::supportsMetaFileHandle_Impl()
2092               && aFlavor.DataType == getCppuType( (const sal_uInt64*) 0 ) )
2093             {
2094                 ::boost::shared_ptr<GDIMetaFile> pMetaFile =
2095                     m_pData->m_pObjectShell->GetPreviewMetaFile( sal_True );
2096 
2097                 if ( pMetaFile )
2098                 {
2099                     aAny <<= reinterpret_cast< const sal_uInt64 >(
2100                         GraphicHelper::getEnhMetaFileFromGDI_Impl( pMetaFile.get() ) );
2101                 }
2102             }
2103             else
2104                 throw datatransfer::UnsupportedFlavorException();
2105         }
2106         else if ( aFlavor.MimeType.equalsAscii( "application/x-openoffice-wmf;windows_formatname=\"Image WMF\"" ) )
2107         {
2108             if ( aFlavor.DataType == getCppuType( (const Sequence< sal_Int8 >*) 0 ) )
2109             {
2110                 ::boost::shared_ptr<GDIMetaFile> pMetaFile =
2111                     m_pData->m_pObjectShell->GetPreviewMetaFile( sal_True );
2112 
2113                 if ( pMetaFile )
2114                 {
2115                     ::boost::shared_ptr<SvMemoryStream> pStream(
2116                         GraphicHelper::getFormatStrFromGDI_Impl(
2117                             pMetaFile.get(), CVT_WMF ) );
2118 
2119                     if ( pStream )
2120                     {
2121                         pStream->SetVersion( SOFFICE_FILEFORMAT_CURRENT );
2122                         aAny <<= Sequence< sal_Int8 >( reinterpret_cast< const sal_Int8* >( pStream->GetData() ),
2123                                                         pStream->Seek( STREAM_SEEK_TO_END ) );
2124                     }
2125                 }
2126             }
2127             else if ( GraphicHelper::supportsMetaFileHandle_Impl()
2128               && aFlavor.DataType == getCppuType( (const sal_uInt64*) 0 ) )
2129             {
2130                 // means HGLOBAL handler to memory storage containing METAFILEPICT structure
2131 
2132                 ::boost::shared_ptr<GDIMetaFile> pMetaFile =
2133                     m_pData->m_pObjectShell->GetPreviewMetaFile( sal_True );
2134 
2135                 if ( pMetaFile )
2136                 {
2137                     Size aMetaSize = pMetaFile->GetPrefSize();
2138                     aAny <<= reinterpret_cast< const sal_uInt64 >(
2139                         GraphicHelper::getWinMetaFileFromGDI_Impl(
2140                             pMetaFile.get(), aMetaSize ) );
2141                 }
2142             }
2143             else
2144                 throw datatransfer::UnsupportedFlavorException();
2145         }
2146         else if ( aFlavor.MimeType.equalsAscii( "image/svg+xml" ) )
2147         {
2148             if ( aFlavor.DataType == getCppuType( (const Sequence< sal_Int8 >*) 0 ) )
2149             {
2150                 ::boost::shared_ptr<GDIMetaFile> pMetaFile =
2151                     m_pData->m_pObjectShell->GetPreviewMetaFile( sal_True );
2152 
2153                 if ( pMetaFile )
2154                 {
2155                     ::boost::shared_ptr<SvMemoryStream> pStream(
2156                         GraphicHelper::getFormatStrFromGDI_Impl(
2157                             pMetaFile.get(), CVT_SVG ) );
2158 
2159                     if ( pStream )
2160                     {
2161                         pStream->SetVersion( SOFFICE_FILEFORMAT_CURRENT );
2162                         aAny <<= Sequence< sal_Int8 >( reinterpret_cast< const sal_Int8* >( pStream->GetData() ),
2163                                                        pStream->Seek( STREAM_SEEK_TO_END ) );
2164                     }
2165                 }
2166             }
2167             else
2168                 throw datatransfer::UnsupportedFlavorException();
2169         }
2170         else if ( aFlavor.MimeType.equalsAscii( "application/x-openoffice-bitmap;windows_formatname=\"Bitmap\"" ) )
2171         {
2172             if ( aFlavor.DataType == getCppuType( (const Sequence< sal_Int8 >*) 0 ) )
2173             {
2174                 ::boost::shared_ptr<GDIMetaFile> pMetaFile =
2175                     m_pData->m_pObjectShell->GetPreviewMetaFile( sal_True );
2176 
2177                 if ( pMetaFile )
2178                 {
2179                     ::boost::shared_ptr<SvMemoryStream> pStream(
2180                         GraphicHelper::getFormatStrFromGDI_Impl(
2181                             pMetaFile.get(), CVT_BMP ) );
2182 
2183                     if ( pStream )
2184                     {
2185                         pStream->SetVersion( SOFFICE_FILEFORMAT_CURRENT );
2186                         aAny <<= Sequence< sal_Int8 >( reinterpret_cast< const sal_Int8* >( pStream->GetData() ),
2187                                                         pStream->Seek( STREAM_SEEK_TO_END ) );
2188                     }
2189                 }
2190             }
2191             else
2192                 throw datatransfer::UnsupportedFlavorException();
2193         }
2194         else if ( aFlavor.MimeType.equalsAscii( "image/png" ) )
2195         {
2196             if ( aFlavor.DataType == getCppuType( (const Sequence< sal_Int8 >*) 0 ) )
2197             {
2198                 ::boost::shared_ptr<GDIMetaFile> pMetaFile =
2199                     m_pData->m_pObjectShell->GetPreviewMetaFile( sal_True );
2200 
2201                 if ( pMetaFile )
2202                 {
2203                     ::boost::shared_ptr<SvMemoryStream> pStream(
2204                         GraphicHelper::getFormatStrFromGDI_Impl(
2205                             pMetaFile.get(), CVT_PNG ) );
2206 
2207                     if ( pStream )
2208                     {
2209                         pStream->SetVersion( SOFFICE_FILEFORMAT_CURRENT );
2210                         aAny <<= Sequence< sal_Int8 >( reinterpret_cast< const sal_Int8* >( pStream->GetData() ),
2211                                                         pStream->Seek( STREAM_SEEK_TO_END ) );
2212                     }
2213                 }
2214             }
2215             else
2216                 throw datatransfer::UnsupportedFlavorException();
2217         }
2218         else
2219             throw datatransfer::UnsupportedFlavorException();
2220     }
2221 
2222     return aAny;
2223 }
2224 
2225 //________________________________________________________________________________________________________
2226 // XTransferable
2227 //________________________________________________________________________________________________________
2228 
2229 
getTransferDataFlavors()2230 uno::Sequence< DATAFLAVOR > SAL_CALL SfxBaseModel::getTransferDataFlavors()
2231 {
2232     SfxModelGuard aGuard( *this );
2233 
2234     sal_Int32 nSuppFlavors = GraphicHelper::supportsMetaFileHandle_Impl() ? 11 : 9;
2235     uno::Sequence< DATAFLAVOR > aFlavorSeq( nSuppFlavors );
2236 
2237     aFlavorSeq[0].MimeType =
2238         ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "application/x-openoffice-gdimetafile;windows_formatname=\"GDIMetaFile\"" ) );
2239     aFlavorSeq[0].HumanPresentableName = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "GDIMetaFile" ) );
2240     aFlavorSeq[0].DataType = getCppuType( (const Sequence< sal_Int8 >*) 0 );
2241 
2242     aFlavorSeq[1].MimeType =
2243         ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "application/x-openoffice-highcontrast-gdimetafile;windows_formatname=\"GDIMetaFile\"" ) );
2244     aFlavorSeq[1].HumanPresentableName = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "GDIMetaFile" ) );
2245     aFlavorSeq[1].DataType = getCppuType( (const Sequence< sal_Int8 >*) 0 );
2246 
2247     aFlavorSeq[2].MimeType =
2248         ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "application/x-openoffice-emf;windows_formatname=\"Image EMF\"" ) );
2249     aFlavorSeq[2].HumanPresentableName = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Enhanced Windows MetaFile" ) );
2250     aFlavorSeq[2].DataType = getCppuType( (const Sequence< sal_Int8 >*) 0 );
2251 
2252     aFlavorSeq[3].MimeType =
2253         ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "application/x-openoffice-wmf;windows_formatname=\"Image WMF\"" ) );
2254     aFlavorSeq[3].HumanPresentableName = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Windows MetaFile" ) );
2255     aFlavorSeq[3].DataType = getCppuType( (const Sequence< sal_Int8 >*) 0 );
2256 
2257     aFlavorSeq[4].MimeType =
2258         ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "application/x-openoffice-objectdescriptor-xml;windows_formatname=\"Star Object Descriptor (XML)\"" ) );
2259     aFlavorSeq[4].HumanPresentableName = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Star Object Descriptor (XML)" ) );
2260     aFlavorSeq[4].DataType = getCppuType( (const Sequence< sal_Int8 >*) 0 );
2261 
2262     aFlavorSeq[5].MimeType =
2263         ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "application/x-openoffice-embed-source-xml;windows_formatname=\"Star Embed Source (XML)\"" ) );
2264     aFlavorSeq[5].HumanPresentableName = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Star Embed Source (XML)" ) );
2265     aFlavorSeq[5].DataType = getCppuType( (const Sequence< sal_Int8 >*) 0 );
2266 
2267     aFlavorSeq[6].MimeType =
2268         ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "application/x-openoffice-bitmap;windows_formatname=\"Bitmap\"" ) );
2269     aFlavorSeq[6].HumanPresentableName = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Bitmap" ) );
2270     aFlavorSeq[6].DataType = getCppuType( (const Sequence< sal_Int8 >*) 0 );
2271 
2272     aFlavorSeq[7].MimeType =
2273         ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "image/png" ) );
2274     aFlavorSeq[7].HumanPresentableName = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "PNG" ) );
2275     aFlavorSeq[7].DataType = getCppuType( (const Sequence< sal_Int8 >*) 0 );
2276 
2277     aFlavorSeq[8].MimeType =
2278         ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "image/svg+xml" ) );
2279     aFlavorSeq[8].HumanPresentableName = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "SVG" ) );
2280     aFlavorSeq[8].DataType = getCppuType( (const Sequence< sal_Int8 >*) 0 );
2281 
2282     if ( nSuppFlavors == 11 )
2283     {
2284         aFlavorSeq[9].MimeType =
2285             ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "application/x-openoffice-emf;windows_formatname=\"Image EMF\"" ) );
2286         aFlavorSeq[9].HumanPresentableName = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Enhanced Windows MetaFile" ) );
2287         aFlavorSeq[9].DataType = getCppuType( (const sal_uInt64*) 0 );
2288 
2289         aFlavorSeq[10].MimeType =
2290             ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "application/x-openoffice-wmf;windows_formatname=\"Image WMF\"" ) );
2291         aFlavorSeq[10].HumanPresentableName = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Windows MetaFile" ) );
2292         aFlavorSeq[10].DataType = getCppuType( (const sal_uInt64*) 0 );
2293     }
2294 
2295     return aFlavorSeq;
2296 }
2297 
2298 //________________________________________________________________________________________________________
2299 // XTransferable
2300 //________________________________________________________________________________________________________
2301 
2302 
isDataFlavorSupported(const DATAFLAVOR & aFlavor)2303 sal_Bool SAL_CALL SfxBaseModel::isDataFlavorSupported( const DATAFLAVOR& aFlavor )
2304 {
2305     SfxModelGuard aGuard( *this );
2306 
2307     if ( aFlavor.MimeType.equalsAscii( "application/x-openoffice-gdimetafile;windows_formatname=\"GDIMetaFile\"" ) )
2308     {
2309         if ( aFlavor.DataType == getCppuType( (const Sequence< sal_Int8 >*) 0 ) )
2310             return sal_True;
2311     }
2312     else if ( aFlavor.MimeType.equalsAscii( "application/x-openoffice-highcontrast-gdimetafile;windows_formatname=\"GDIMetaFile\"" ) )
2313     {
2314         if ( aFlavor.DataType == getCppuType( (const Sequence< sal_Int8 >*) 0 ) )
2315             return sal_True;
2316     }
2317     else if ( aFlavor.MimeType.equalsAscii( "application/x-openoffice-emf;windows_formatname=\"Image EMF\"" ) )
2318     {
2319         if ( aFlavor.DataType == getCppuType( (const Sequence< sal_Int8 >*) 0 ) )
2320             return sal_True;
2321         else if ( GraphicHelper::supportsMetaFileHandle_Impl()
2322           && aFlavor.DataType == getCppuType( (const sal_uInt64*) 0 ) )
2323             return sal_True;
2324     }
2325     else if ( aFlavor.MimeType.equalsAscii( "application/x-openoffice-wmf;windows_formatname=\"Image WMF\"" ) )
2326     {
2327         if ( aFlavor.DataType == getCppuType( (const Sequence< sal_Int8 >*) 0 ) )
2328             return sal_True;
2329         else if ( GraphicHelper::supportsMetaFileHandle_Impl()
2330           && aFlavor.DataType == getCppuType( (const sal_uInt64*) 0 ) )
2331             return sal_True;
2332     }
2333     else if ( aFlavor.MimeType.equalsAscii( "image/svg+xml" ) )
2334     {
2335         if ( aFlavor.DataType == getCppuType( (const Sequence< sal_Int8 >*) 0 ) )
2336             return sal_True;
2337     }
2338     else if ( aFlavor.MimeType.equalsAscii( "application/x-openoffice-objectdescriptor-xml;windows_formatname=\"Star Object Descriptor (XML)\"" ) )
2339     {
2340         if ( aFlavor.DataType == getCppuType( (const Sequence< sal_Int8 >*) 0 ) )
2341             return sal_True;
2342     }
2343     else if ( aFlavor.MimeType.equalsAscii( "application/x-openoffice-embed-source;windows_formatname=\"Star EMBS\"" ) )
2344     {
2345         if ( aFlavor.DataType == getCppuType( (const Sequence< sal_Int8 >*) 0 ) )
2346             return sal_True;
2347     }
2348     else if ( aFlavor.MimeType.equalsAscii( "application/x-openoffice-bitmap;windows_formatname=\"Bitmap\"" ) )
2349     {
2350         if ( aFlavor.DataType == getCppuType( (const Sequence< sal_Int8 >*) 0 ) )
2351             return sal_True;
2352     }
2353     else if ( aFlavor.MimeType.equalsAscii( "image/png" ) )
2354     {
2355         if ( aFlavor.DataType == getCppuType( (const Sequence< sal_Int8 >*) 0 ) )
2356             return sal_True;
2357     }
2358 
2359     return sal_False;
2360 }
2361 
2362 
2363 //--------------------------------------------------------------------------------------------------------
2364 //  XEventsSupplier
2365 //--------------------------------------------------------------------------------------------------------
2366 
getEvents()2367 uno::Reference< container::XNameReplace > SAL_CALL SfxBaseModel::getEvents()
2368 {
2369     SfxModelGuard aGuard( *this );
2370 
2371     if ( ! m_pData->m_xEvents.is() )
2372     {
2373         m_pData->m_xEvents = new SfxEvents_Impl( m_pData->m_pObjectShell, this );
2374     }
2375 
2376     return m_pData->m_xEvents;
2377 }
2378 
2379 //--------------------------------------------------------------------------------------------------------
2380 //  XEmbeddedScripts
2381 //--------------------------------------------------------------------------------------------------------
2382 
getBasicLibraries()2383 uno::Reference< script::XStorageBasedLibraryContainer > SAL_CALL SfxBaseModel::getBasicLibraries()
2384 {
2385     SfxModelGuard aGuard( *this );
2386 
2387     uno::Reference< script::XStorageBasedLibraryContainer > xBasicLibraries;
2388     if ( m_pData->m_pObjectShell )
2389         xBasicLibraries.set( m_pData->m_pObjectShell->GetBasicContainer(), UNO_QUERY_THROW );
2390     return xBasicLibraries;
2391 }
2392 
getDialogLibraries()2393 uno::Reference< script::XStorageBasedLibraryContainer > SAL_CALL SfxBaseModel::getDialogLibraries()
2394 {
2395     SfxModelGuard aGuard( *this );
2396 
2397     uno::Reference< script::XStorageBasedLibraryContainer > xDialogLibraries;
2398     if ( m_pData->m_pObjectShell )
2399         xDialogLibraries.set( m_pData->m_pObjectShell->GetDialogContainer(), UNO_QUERY_THROW );
2400     return xDialogLibraries;
2401 }
2402 
getAllowMacroExecution()2403 ::sal_Bool SAL_CALL SfxBaseModel::getAllowMacroExecution()
2404 {
2405     SfxModelGuard aGuard( *this );
2406 
2407     if ( m_pData->m_pObjectShell )
2408         return m_pData->m_pObjectShell->AdjustMacroMode( String(), false );
2409     return sal_False;
2410 }
2411 
2412 //--------------------------------------------------------------------------------------------------------
2413 //  XScriptInvocationContext
2414 //--------------------------------------------------------------------------------------------------------
2415 
getScriptContainer()2416 Reference< document::XEmbeddedScripts > SAL_CALL SfxBaseModel::getScriptContainer()
2417 {
2418     SfxModelGuard aGuard( *this );
2419 
2420     Reference< document::XEmbeddedScripts > xDocumentScripts;
2421 
2422     try
2423     {
2424         Reference< frame::XModel > xDocument( this );
2425         xDocumentScripts.set( xDocument, uno::UNO_QUERY );
2426         while ( !xDocumentScripts.is() && xDocument.is() )
2427         {
2428             Reference< container::XChild > xDocAsChild( xDocument, uno::UNO_QUERY );
2429             if ( !xDocAsChild.is() )
2430             {
2431                 xDocument = NULL;
2432                 break;
2433             }
2434 
2435             xDocument.set( xDocAsChild->getParent(), uno::UNO_QUERY );
2436             xDocumentScripts.set( xDocument, uno::UNO_QUERY );
2437         }
2438     }
2439     catch( const Exception& )
2440     {
2441         DBG_UNHANDLED_EXCEPTION();
2442         xDocumentScripts = NULL;
2443     }
2444 
2445     return xDocumentScripts;
2446 }
2447 
2448 //--------------------------------------------------------------------------------------------------------
2449 //  XEventBroadcaster
2450 //--------------------------------------------------------------------------------------------------------
2451 
addEventListener(const uno::Reference<XDOCEVENTLISTENER> & aListener)2452 void SAL_CALL SfxBaseModel::addEventListener( const uno::Reference< XDOCEVENTLISTENER >& aListener )
2453 {
2454     SfxModelGuard aGuard( *this, SfxModelGuard::E_INITIALIZING );
2455 
2456     m_pData->m_aInterfaceContainer.addInterface( ::getCppuType((const uno::Reference< XDOCEVENTLISTENER >*)0), aListener );
2457 }
2458 
2459 //--------------------------------------------------------------------------------------------------------
2460 //  XEventBroadcaster
2461 //--------------------------------------------------------------------------------------------------------
2462 
removeEventListener(const uno::Reference<XDOCEVENTLISTENER> & aListener)2463 void SAL_CALL SfxBaseModel::removeEventListener( const uno::Reference< XDOCEVENTLISTENER >& aListener )
2464 {
2465     SfxModelGuard aGuard( *this );
2466 
2467     m_pData->m_aInterfaceContainer.removeInterface( ::getCppuType((const uno::Reference< XDOCEVENTLISTENER >*)0), aListener );
2468 }
2469 
2470 //--------------------------------------------------------------------------------------------------------
2471 //  XDocumentEventBroadcaster
2472 //--------------------------------------------------------------------------------------------------------
2473 // ---------------------------------
addDocumentEventListener(const uno::Reference<document::XDocumentEventListener> & aListener)2474 void SAL_CALL SfxBaseModel::addDocumentEventListener( const uno::Reference< document::XDocumentEventListener >& aListener )
2475 {
2476     SfxModelGuard aGuard( *this, SfxModelGuard::E_INITIALIZING );
2477     m_pData->m_aInterfaceContainer.addInterface( ::getCppuType((const uno::Reference< document::XDocumentEventListener >*)0), aListener );
2478 }
2479 
2480 // ---------------------------------
removeDocumentEventListener(const uno::Reference<document::XDocumentEventListener> & aListener)2481 void SAL_CALL SfxBaseModel::removeDocumentEventListener( const uno::Reference< document::XDocumentEventListener >& aListener )
2482 {
2483     SfxModelGuard aGuard( *this );
2484     m_pData->m_aInterfaceContainer.removeInterface( ::getCppuType((const uno::Reference< document::XDocumentEventListener >*)0), aListener );
2485 }
2486 
2487 // ---------------------------------
notifyDocumentEvent(const::rtl::OUString &,const uno::Reference<frame::XController2> &,const uno::Any &)2488 void SAL_CALL SfxBaseModel::notifyDocumentEvent( const ::rtl::OUString&, const uno::Reference< frame::XController2 >&, const uno::Any& )
2489 {
2490     throw lang::NoSupportException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "SfxBaseModel controls all the sent notifications itself!" ) ), uno::Reference< uno::XInterface >() );
2491 }
2492 
2493 //________________________________________________________________________________________________________
2494 //  SfxListener
2495 //________________________________________________________________________________________________________
2496 
addTitle_Impl(Sequence<::com::sun::star::beans::PropertyValue> & rSeq,const::rtl::OUString & rTitle)2497 void addTitle_Impl( Sequence < ::com::sun::star::beans::PropertyValue >& rSeq, const ::rtl::OUString& rTitle )
2498 {
2499     sal_Int32 nCount = rSeq.getLength();
2500     sal_Int32 nArg;
2501 
2502     for ( nArg = 0; nArg < nCount; nArg++ )
2503     {
2504         ::com::sun::star::beans::PropertyValue& rProp = rSeq[nArg];
2505         if ( rProp.Name.equalsAscii("Title") )
2506         {
2507             rProp.Value <<= rTitle;
2508             break;
2509         }
2510     }
2511 
2512     if ( nArg == nCount )
2513     {
2514         rSeq.realloc( nCount+1 );
2515         rSeq[nCount].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Title") );
2516         rSeq[nCount].Value <<= rTitle;
2517     }
2518 }
2519 
NotifyStorageListeners_Impl()2520 void SfxBaseModel::NotifyStorageListeners_Impl()
2521 {
2522     uno::Reference< uno::XInterface > xSelfHold( static_cast< ::cppu::OWeakObject* >(this) );
2523 
2524     if ( m_pData->m_pObjectShell )
2525     {
2526         ::cppu::OInterfaceContainerHelper* pContainer =
2527             m_pData->m_aInterfaceContainer.getContainer(
2528                 ::getCppuType( ( const uno::Reference< document::XStorageChangeListener >*) NULL ) );
2529         if ( pContainer != NULL )
2530         {
2531             uno::Reference< embed::XStorage > xNewStorage = m_pData->m_pObjectShell->GetStorage();
2532             ::cppu::OInterfaceIteratorHelper pIterator(*pContainer);
2533             while ( pIterator.hasMoreElements() )
2534             {
2535                 try
2536                 {
2537                     ((document::XStorageChangeListener*)pIterator.next())->notifyStorageChange(
2538                                                                             xSelfHold,
2539                                                                             xNewStorage );
2540                 }
2541                 catch( uno::RuntimeException& )
2542                 {
2543                     pIterator.remove();
2544                 }
2545             }
2546         }
2547     }
2548 }
2549 
Notify(SfxBroadcaster & rBC,const SfxHint & rHint)2550 void SfxBaseModel::Notify(          SfxBroadcaster& rBC     ,
2551                             const   SfxHint&        rHint   )
2552 {
2553     if ( !m_pData )
2554         return;
2555 
2556     if ( &rBC == m_pData->m_pObjectShell )
2557     {
2558         SfxSimpleHint* pSimpleHint = PTR_CAST( SfxSimpleHint, &rHint );
2559         if ( pSimpleHint && pSimpleHint->GetId() == SFX_HINT_DOCCHANGED )
2560             changing();
2561 
2562         SfxEventHint* pNamedHint = PTR_CAST( SfxEventHint, &rHint );
2563         if ( pNamedHint )
2564         {
2565 
2566             switch ( pNamedHint->GetEventId() )
2567             {
2568             case SFX_EVENT_STORAGECHANGED:
2569             {
2570                 // for now this event is sent only on creation of a new storage for new document
2571                 // and in case of reload of medium without document reload
2572                 // other events are used to detect storage change
2573                 // NotifyStorageListeners_Impl();
2574 
2575                 if ( m_pData->m_xUIConfigurationManager.is()
2576                   && m_pData->m_pObjectShell->GetCreateMode() != SFX_CREATE_MODE_EMBEDDED )
2577                 {
2578                     uno::Reference< XSTORAGE > xConfigStorage;
2579                     rtl::OUString aUIConfigFolderName( RTL_CONSTASCII_USTRINGPARAM( "Configurations2" ));
2580 
2581                     xConfigStorage = getDocumentSubStorage( aUIConfigFolderName, com::sun::star::embed::ElementModes::READWRITE );
2582                     if ( !xConfigStorage.is() )
2583                         xConfigStorage = getDocumentSubStorage( aUIConfigFolderName, com::sun::star::embed::ElementModes::READ );
2584 
2585                     if ( xConfigStorage.is() || !m_pData->m_pObjectShell->GetStorage()->hasByName( aUIConfigFolderName ) )
2586                     {
2587                         // the storage is different, since otherwise it could not be opened, so it must be exchanged
2588                         Reference< ui::XUIConfigurationStorage > xUIConfigStorage( m_pData->m_xUIConfigurationManager, uno::UNO_QUERY );
2589                         xUIConfigStorage->setStorage( xConfigStorage );
2590                     }
2591                     else
2592                     {
2593                         OSL_ENSURE( sal_False, "Unexpected scenario!\n" );
2594                     }
2595                 }
2596 
2597                 ListenForStorage_Impl( m_pData->m_pObjectShell->GetStorage() );
2598             }
2599             break;
2600 
2601             case SFX_EVENT_LOADFINISHED:
2602             {
2603                 impl_getPrintHelper();
2604                 ListenForStorage_Impl( m_pData->m_pObjectShell->GetStorage() );
2605                 m_pData->m_bModifiedSinceLastSave = sal_False;
2606             }
2607             break;
2608 
2609             case SFX_EVENT_SAVEASDOCDONE:
2610             {
2611                 m_pData->m_sURL = m_pData->m_pObjectShell->GetMedium()->GetName();
2612 
2613                 SfxItemSet *pSet = m_pData->m_pObjectShell->GetMedium()->GetItemSet();
2614                 ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue > aArgs;
2615                 ::rtl::OUString aTitle = m_pData->m_pObjectShell->GetTitle();
2616                 TransformItems( SID_SAVEASDOC, *pSet, aArgs );
2617                 addTitle_Impl( aArgs, aTitle );
2618                 attachResource( m_pData->m_pObjectShell->GetMedium()->GetName(), aArgs );
2619             }
2620             break;
2621 
2622             case SFX_EVENT_DOCCREATED:
2623             {
2624                 impl_getPrintHelper();
2625                 m_pData->m_bModifiedSinceLastSave = sal_False;
2626             }
2627             break;
2628 
2629             case SFX_EVENT_MODIFYCHANGED:
2630             {
2631                 m_pData->m_bModifiedSinceLastSave = isModified();
2632             }
2633             break;
2634             }
2635 
2636 
2637             SfxViewEventHint* pViewHint = PTR_CAST( SfxViewEventHint, &rHint );
2638             postEvent_Impl( pNamedHint->GetEventName(), pViewHint ? pViewHint->GetController() : uno::Reference< frame::XController2 >() );
2639         }
2640 
2641         if ( pSimpleHint )
2642         {
2643             if ( pSimpleHint->GetId() == SFX_HINT_TITLECHANGED )
2644             {
2645                 ::rtl::OUString aTitle = m_pData->m_pObjectShell->GetTitle();
2646                 addTitle_Impl( m_pData->m_seqArguments, aTitle );
2647                 postEvent_Impl( GlobalEventConfig::GetEventName( STR_EVENT_TITLECHANGED ) );
2648             }
2649             if ( pSimpleHint->GetId() == SFX_HINT_MODECHANGED )
2650             {
2651                 postEvent_Impl( GlobalEventConfig::GetEventName( STR_EVENT_MODECHANGED ) );
2652             }
2653 /*
2654             else if ( pSimpleHint->GetId() == SFX_HINT_DYING
2655                 || pSimpleHint->GetId() == SFX_HINT_DEINITIALIZING )
2656             {
2657                 SfxObjectShellLock pShellLock = m_pData->m_pObjectShellLock;
2658                 m_pData->m_pObjectShellLock = SfxObjectShellLock();
2659             }
2660 */
2661         }
2662     }
2663 }
2664 
2665 //________________________________________________________________________________________________________
2666 //  public impl.
2667 //________________________________________________________________________________________________________
2668 
NotifyModifyListeners_Impl() const2669 void SfxBaseModel::NotifyModifyListeners_Impl() const
2670 {
2671     ::cppu::OInterfaceContainerHelper* pIC = m_pData->m_aInterfaceContainer.getContainer( ::getCppuType((const uno::Reference< XMODIFYLISTENER >*)0) );
2672     if ( pIC )
2673     {
2674         lang::EventObject aEvent( (frame::XModel *)this );
2675         pIC->notifyEach( &util::XModifyListener::modified, aEvent );
2676     }
2677 
2678     // this notification here is done too generously, we cannot simply assume that we're really modified
2679     // now, but we need to check it ...
2680     m_pData->m_bModifiedSinceLastSave = const_cast< SfxBaseModel* >( this )->isModified();
2681 }
2682 
changing()2683 void SfxBaseModel::changing()
2684 {
2685     SfxModelGuard aGuard( *this );
2686 
2687     // the notification should not be sent if the document can not be modified
2688     if ( !m_pData->m_pObjectShell.Is() || !m_pData->m_pObjectShell->IsEnableSetModified() )
2689         return;
2690 
2691     NotifyModifyListeners_Impl();
2692 }
2693 
impl_change()2694 void SfxBaseModel::impl_change()
2695 {
2696     // object already disposed?
2697     if ( impl_isDisposed() )
2698         return;
2699 
2700     NotifyModifyListeners_Impl();
2701 }
2702 
2703 //________________________________________________________________________________________________________
2704 //  public impl.
2705 //________________________________________________________________________________________________________
2706 
GetObjectShell() const2707 SfxObjectShell* SfxBaseModel::GetObjectShell() const
2708 {
2709     return m_pData ? (SfxObjectShell*) m_pData->m_pObjectShell : 0;
2710 }
2711 
impl_getObjectShell() const2712 SfxObjectShell* SfxBaseModel::impl_getObjectShell() const
2713 {
2714     return m_pData ? (SfxObjectShell*) m_pData->m_pObjectShell : 0;
2715 }
2716 
2717 //________________________________________________________________________________________________________
2718 //  public impl.
2719 //________________________________________________________________________________________________________
2720 
IsInitialized() const2721 sal_Bool SfxBaseModel::IsInitialized() const
2722 {
2723     if ( !m_pData || !m_pData->m_pObjectShell )
2724     {
2725         OSL_ENSURE( false, "SfxBaseModel::IsInitialized: this should have been caught earlier!" );
2726         return sal_False;
2727     }
2728 
2729     return m_pData->m_pObjectShell->GetMedium() != NULL;
2730 }
2731 
MethodEntryCheck(const bool i_mustBeInitialized) const2732 void SfxBaseModel::MethodEntryCheck( const bool i_mustBeInitialized ) const
2733 {
2734     if ( impl_isDisposed() )
2735         throw ::com::sun::star::lang::DisposedException( ::rtl::OUString(), *const_cast< SfxBaseModel* >( this ) );
2736     if ( i_mustBeInitialized && !IsInitialized() )
2737         throw ::com::sun::star::lang::NotInitializedException( ::rtl::OUString(), *const_cast< SfxBaseModel* >( this ) );
2738 }
2739 
impl_isDisposed() const2740 sal_Bool SfxBaseModel::impl_isDisposed() const
2741 {
2742     return ( m_pData == NULL ) ;
2743 }
2744 
2745 //________________________________________________________________________________________________________
2746 //  private impl.
2747 //________________________________________________________________________________________________________
2748 
GetMediumFilterName_Impl()2749 ::rtl::OUString SfxBaseModel::GetMediumFilterName_Impl()
2750 {
2751     const SfxFilter* pFilter = NULL;
2752     SfxMedium* pMedium = m_pData->m_pObjectShell->GetMedium();
2753     if ( pMedium )
2754         pFilter = pMedium->GetFilter();
2755 
2756     if ( pFilter )
2757         return pFilter->GetName();
2758 
2759     return ::rtl::OUString();
2760 }
2761 
impl_store(const::rtl::OUString & sURL,const uno::Sequence<beans::PropertyValue> & seqArguments,sal_Bool bSaveTo)2762 void SfxBaseModel::impl_store(  const   ::rtl::OUString&                   sURL            ,
2763                                 const   uno::Sequence< beans::PropertyValue >&  seqArguments    ,
2764                                         sal_Bool                    bSaveTo         )
2765 {
2766     if( !sURL.getLength() )
2767         throw frame::IllegalArgumentIOException();
2768 
2769     //sal_Bool aSaveAsTemplate = sal_False;
2770 
2771     sal_Bool bSaved = sal_False;
2772     if ( !bSaveTo && m_pData->m_pObjectShell && sURL.getLength()
2773       && sURL.compareToAscii( "private:stream", 14 ) != COMPARE_EQUAL
2774       && ::utl::UCBContentHelper::EqualURLs( getLocation(), sURL ) )
2775     {
2776         // this is the same file URL as the current document location, try to use storeOwn if possible
2777 
2778         ::comphelper::SequenceAsHashMap aArgHash( seqArguments );
2779         ::rtl::OUString aFilterString( RTL_CONSTASCII_USTRINGPARAM( "FilterName" ) );
2780         ::rtl::OUString aFilterName = aArgHash.getUnpackedValueOrDefault( aFilterString, ::rtl::OUString() );
2781         if ( aFilterName.getLength() )
2782         {
2783             SfxMedium* pMedium = m_pData->m_pObjectShell->GetMedium();
2784             if ( pMedium )
2785             {
2786                 const SfxFilter* pFilter = pMedium->GetFilter();
2787                 if ( pFilter && aFilterName.equals( pFilter->GetFilterName() ) )
2788                 {
2789                     // 119366 - If the former file saving with password, do not trying in StoreSelf anyway...
2790                     bool bFormerPassword = false;
2791                     {
2792                         uno::Sequence< beans::NamedValue > aOldEncryptionData;
2793                         if (GetEncryptionData_Impl( pMedium->GetItemSet(), aOldEncryptionData ))
2794                         {
2795                             bFormerPassword = true;
2796                         }
2797                     }
2798                     if ( !bFormerPassword )
2799                     {
2800                         aArgHash.erase( aFilterString );
2801                         aArgHash.erase( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "URL" ) ) );
2802 
2803                         try
2804                         {
2805                             storeSelf( aArgHash.getAsConstPropertyValueList() );
2806                             bSaved = sal_True;
2807                         }
2808                         catch( const lang::IllegalArgumentException& )
2809                         {
2810                             // some additional arguments do not allow to use saving, SaveAs should be done
2811                             // but only for normal documents, the shared documents would be overwritten in this case
2812                             // that would mean an information loss
2813                             // TODO/LATER: need a new interaction for this case
2814                             if ( m_pData->m_pObjectShell->IsDocShared() )
2815                             {
2816                                 m_pData->m_pObjectShell->AddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Can't store shared document!" ) ) );
2817                                 m_pData->m_pObjectShell->StoreLog();
2818 
2819                                 uno::Sequence< beans::NamedValue > aNewEncryptionData = aArgHash.getUnpackedValueOrDefault( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "EncryptionData" ) ), uno::Sequence< beans::NamedValue >() );
2820                                 if ( !aNewEncryptionData.getLength() )
2821                                 {
2822                                     ::rtl::OUString aNewPassword = aArgHash.getUnpackedValueOrDefault( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Password" ) ), ::rtl::OUString() );
2823                                     aNewEncryptionData = ::comphelper::OStorageHelper::CreatePackageEncryptionData( aNewPassword );
2824                                 }
2825 
2826                                 uno::Sequence< beans::NamedValue > aOldEncryptionData;
2827                                 GetEncryptionData_Impl( pMedium->GetItemSet(), aOldEncryptionData );
2828 
2829                                 if ( !aOldEncryptionData.getLength() && !aNewEncryptionData.getLength() )
2830                                     throw;
2831                                 else
2832                                 {
2833                                     // if the password is changed a special error should be used in case of shared document
2834                                     throw task::ErrorCodeIOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Can't change password for shared document." ) ), uno::Reference< uno::XInterface >(), ERRCODE_SFX_SHARED_NOPASSWORDCHANGE );
2835                                 }
2836                             }
2837                         }
2838                     }
2839                 }
2840             }
2841         }
2842     }
2843 
2844     if ( !bSaved && m_pData->m_pObjectShell )
2845     {
2846         SFX_APP()->NotifyEvent( SfxEventHint( bSaveTo ? SFX_EVENT_SAVETODOC : SFX_EVENT_SAVEASDOC, GlobalEventConfig::GetEventName( bSaveTo ? STR_EVENT_SAVETODOC : STR_EVENT_SAVEASDOC ),
2847                                                 m_pData->m_pObjectShell ) );
2848 
2849         SfxAllItemSet *aParams = new SfxAllItemSet( SFX_APP()->GetPool() );
2850         aParams->Put( SfxStringItem( SID_FILE_NAME, String(sURL) ) );
2851         if ( bSaveTo )
2852             aParams->Put( SfxBoolItem( SID_SAVETO, sal_True ) );
2853 
2854         TransformParameters( SID_SAVEASDOC, seqArguments, *aParams );
2855 
2856         SFX_ITEMSET_ARG( aParams, pCopyStreamItem, SfxBoolItem, SID_COPY_STREAM_IF_POSSIBLE, sal_False );
2857 
2858         if ( pCopyStreamItem && pCopyStreamItem->GetValue() && !bSaveTo )
2859         {
2860             m_pData->m_pObjectShell->AddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Misuse of CopyStreamIfPossible!" ) ) );
2861             m_pData->m_pObjectShell->StoreLog();
2862 
2863             throw frame::IllegalArgumentIOException(
2864                     ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("CopyStreamIfPossible parameter is not acceptable for storeAsURL() call!") ),
2865                     uno::Reference< uno::XInterface >() );
2866         }
2867 
2868         sal_uInt32 nModifyPasswordHash = 0;
2869         uno::Sequence< beans::PropertyValue > aModifyPasswordInfo;
2870         SFX_ITEMSET_ARG( aParams, pModifyPasswordInfoItem, SfxUnoAnyItem, SID_MODIFYPASSWORDINFO, sal_False );
2871         if ( pModifyPasswordInfoItem )
2872         {
2873             // it contains either a simple hash or a set of PropertyValues
2874             // TODO/LATER: the sequence of PropertyValue should replace the hash completely in future
2875             sal_Int32 nMPHTmp = 0;
2876             pModifyPasswordInfoItem->GetValue() >>= nMPHTmp;
2877             nModifyPasswordHash = (sal_uInt32)nMPHTmp;
2878             pModifyPasswordInfoItem->GetValue() >>= aModifyPasswordInfo;
2879         }
2880         aParams->ClearItem( SID_MODIFYPASSWORDINFO );
2881         sal_uInt32 nOldModifyPasswordHash = m_pData->m_pObjectShell->GetModifyPasswordHash();
2882         m_pData->m_pObjectShell->SetModifyPasswordHash( nModifyPasswordHash );
2883         uno::Sequence< beans::PropertyValue > aOldModifyPasswordInfo = m_pData->m_pObjectShell->GetModifyPasswordInfo();
2884         m_pData->m_pObjectShell->SetModifyPasswordInfo( aModifyPasswordInfo );
2885 
2886         // since saving a document modifies its DocumentInfo, the current
2887         // DocumentInfo must be saved on "SaveTo", so it can be restored
2888         // after saving
2889         sal_Bool bCopyTo =  bSaveTo ||
2890             m_pData->m_pObjectShell->GetCreateMode() == SFX_CREATE_MODE_EMBEDDED;
2891         uno::Reference<document::XDocumentProperties> xOldDocProps;
2892         uno::Reference<document::XDocumentInfo> xOldDocInfo;
2893         if ( bCopyTo )
2894         {
2895             xOldDocProps = getDocumentProperties();
2896             if (m_pData->m_xDocumentInfo.is())
2897             {
2898                 xOldDocInfo = getDocumentInfo();
2899                 const Reference<util::XCloneable> xCloneable(xOldDocInfo,
2900                     UNO_QUERY_THROW);
2901                 const Reference<document::XDocumentInfo> xNewDocInfo(
2902                     xCloneable->createClone(), UNO_QUERY_THROW);
2903                 const Reference<document::XDocumentPropertiesSupplier> xDPS(
2904                     xNewDocInfo, UNO_QUERY_THROW);
2905                 const Reference<document::XDocumentProperties> xNewDocProps(
2906                     xDPS->getDocumentProperties());
2907                 m_pData->m_xDocumentProperties = xNewDocProps;
2908                 m_pData->m_xDocumentInfo = xNewDocInfo;
2909             }
2910             else // try not to create DocumentInfo if it does not exist...
2911             {
2912                 const Reference<util::XCloneable> xCloneable(xOldDocProps,
2913                     UNO_QUERY_THROW);
2914                 const Reference<document::XDocumentProperties> xNewDocProps(
2915                     xCloneable->createClone(), UNO_QUERY_THROW);
2916                 m_pData->m_xDocumentProperties = xNewDocProps;
2917             }
2918         }
2919 
2920         sal_Bool bRet = m_pData->m_pObjectShell->APISaveAs_Impl( sURL, aParams );
2921 
2922         if ( bCopyTo )
2923         {
2924             // restore DocumentInfo if a copy was created
2925             m_pData->m_xDocumentProperties = xOldDocProps;
2926             m_pData->m_xDocumentInfo = xOldDocInfo;
2927         }
2928 
2929         uno::Reference < task::XInteractionHandler > xHandler;
2930         SFX_ITEMSET_ARG( aParams, pItem, SfxUnoAnyItem, SID_INTERACTIONHANDLER, sal_False);
2931         if ( pItem )
2932             pItem->GetValue() >>= xHandler;
2933 
2934         DELETEZ( aParams );
2935 
2936         sal_uInt32 nErrCode = m_pData->m_pObjectShell->GetErrorCode();
2937         if ( !bRet && !nErrCode )
2938         {
2939             m_pData->m_pObjectShell->AddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Storing has failed, no error is set!" ) ) );
2940             nErrCode = ERRCODE_IO_CANTWRITE;
2941         }
2942         m_pData->m_pObjectShell->ResetError();
2943 
2944         if ( bRet )
2945         {
2946             if ( nErrCode )
2947             {
2948                 // must be a warning - use Interactionhandler if possible or abandone
2949                 if ( xHandler.is() )
2950                 {
2951                     // TODO/LATER: a general way to set the error context should be available
2952                     SfxErrorContext aEc( ERRCTX_SFX_SAVEASDOC, m_pData->m_pObjectShell->GetTitle() );
2953 
2954                     ::com::sun::star::task::ErrorCodeRequest aErrorCode;
2955                     aErrorCode.ErrCode = nErrCode;
2956                     SfxMedium::CallApproveHandler( xHandler, uno::makeAny( aErrorCode ), sal_False );
2957                 }
2958             }
2959 
2960             m_pData->m_pObjectShell->AddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Storing succeeded!" ) ) );
2961             if ( !bSaveTo )
2962             {
2963                 m_pData->m_aPreusedFilterName = GetMediumFilterName_Impl();
2964                 m_pData->m_pObjectShell->SetModifyPasswordEntered();
2965 
2966                 SFX_APP()->NotifyEvent( SfxEventHint( SFX_EVENT_SAVEASDOCDONE, GlobalEventConfig::GetEventName(STR_EVENT_SAVEASDOCDONE), m_pData->m_pObjectShell ) );
2967             }
2968             else
2969             {
2970                 m_pData->m_pObjectShell->SetModifyPasswordHash( nOldModifyPasswordHash );
2971                 m_pData->m_pObjectShell->SetModifyPasswordInfo( aOldModifyPasswordInfo );
2972 
2973                 SFX_APP()->NotifyEvent( SfxEventHint( SFX_EVENT_SAVETODOCDONE, GlobalEventConfig::GetEventName(STR_EVENT_SAVETODOCDONE), m_pData->m_pObjectShell ) );
2974             }
2975         }
2976         else
2977         {
2978             // let the logring be stored to the related file
2979             m_pData->m_pObjectShell->AddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Storing failed!" ) ) );
2980             m_pData->m_pObjectShell->StoreLog();
2981 
2982             m_pData->m_pObjectShell->SetModifyPasswordHash( nOldModifyPasswordHash );
2983             m_pData->m_pObjectShell->SetModifyPasswordInfo( aOldModifyPasswordInfo );
2984 
2985 
2986             SFX_APP()->NotifyEvent( SfxEventHint( bSaveTo ? SFX_EVENT_SAVETODOCFAILED : SFX_EVENT_SAVEASDOCFAILED, GlobalEventConfig::GetEventName( bSaveTo ? STR_EVENT_SAVETODOCFAILED : STR_EVENT_SAVEASDOCFAILED),
2987                                                     m_pData->m_pObjectShell ) );
2988 
2989             throw task::ErrorCodeIOException( ::rtl::OUString(), uno::Reference< uno::XInterface >(), nErrCode );
2990         }
2991     }
2992 }
2993 
2994 //********************************************************************************************************
2995 namespace {
2996 template< typename ListenerT, typename EventT >
2997 class NotifySingleListenerIgnoreRE
2998 {
2999 private:
3000     typedef void ( SAL_CALL ListenerT::*NotificationMethod )( const EventT& );
3001     NotificationMethod  m_pMethod;
3002     const EventT&       m_rEvent;
3003 public:
NotifySingleListenerIgnoreRE(NotificationMethod method,const EventT & event)3004     NotifySingleListenerIgnoreRE( NotificationMethod method, const EventT& event ) : m_pMethod( method ), m_rEvent( event ) { }
3005 
operator ()(const uno::Reference<ListenerT> & listener) const3006     void operator()( const uno::Reference<ListenerT>& listener ) const
3007     {
3008         try
3009         {
3010             (listener.get()->*m_pMethod)( m_rEvent );
3011         }
3012         catch( uno::RuntimeException& )
3013         {
3014             // this exception is ignored to avoid problems with invalid listeners, the listener should be probably thrown away in future
3015         }
3016     }
3017 };
3018 } // anonymous namespace
3019 
postEvent_Impl(const::rtl::OUString & aName,const uno::Reference<frame::XController2> & xController)3020 void SfxBaseModel::postEvent_Impl( const ::rtl::OUString& aName, const uno::Reference< frame::XController2 >& xController )
3021 {
3022     // object already disposed?
3023     if ( impl_isDisposed() )
3024         return;
3025 
3026     DBG_ASSERT( aName.getLength(), "Empty event name!" );
3027     if (!aName.getLength())
3028         return;
3029 
3030     ::cppu::OInterfaceContainerHelper* pIC =
3031         m_pData->m_aInterfaceContainer.getContainer( ::getCppuType( (const uno::Reference< document::XDocumentEventListener >*)0 ) );
3032     if ( pIC )
3033     {
3034 #ifdef DBG_UTIL
3035         ByteString aTmp( "SfxDocumentEvent: " );
3036         aTmp += ByteString( String(aName), RTL_TEXTENCODING_UTF8 );
3037         DBG_TRACE( aTmp.GetBuffer() );
3038 #endif
3039 
3040         document::DocumentEvent aDocumentEvent( (frame::XModel*)this, aName, xController, uno::Any() );
3041 
3042         pIC->forEach< document::XDocumentEventListener, NotifySingleListenerIgnoreRE< document::XDocumentEventListener, document::DocumentEvent > >(
3043             NotifySingleListenerIgnoreRE< document::XDocumentEventListener, document::DocumentEvent >(
3044                 &document::XDocumentEventListener::documentEventOccured,
3045                 aDocumentEvent ) );
3046     }
3047 
3048     pIC = m_pData->m_aInterfaceContainer.getContainer( ::getCppuType( (const uno::Reference< document::XEventListener >*)0 ) );
3049     if ( pIC )
3050     {
3051 #ifdef DBG_UTIL
3052         ByteString aTmp( "SfxEvent: " );
3053         aTmp += ByteString( String(aName), RTL_TEXTENCODING_UTF8 );
3054         DBG_TRACE( aTmp.GetBuffer() );
3055 #endif
3056 
3057         document::EventObject aEvent( (frame::XModel*)this, aName );
3058 
3059         pIC->forEach< document::XEventListener, NotifySingleListenerIgnoreRE< document::XEventListener, document::EventObject > >(
3060             NotifySingleListenerIgnoreRE< document::XEventListener, document::EventObject >(
3061                 &document::XEventListener::notifyEvent,
3062                 aEvent ) );
3063     }
3064 
3065 }
3066 
getViewData()3067 uno::Reference < container::XIndexAccess > SAL_CALL SfxBaseModel::getViewData()
3068 {
3069     SfxModelGuard aGuard( *this );
3070 
3071     if ( m_pData->m_pObjectShell.Is() && !m_pData->m_contViewData.is() )
3072     {
3073         SfxViewFrame *pActFrame = SfxViewFrame::Current();
3074         if ( !pActFrame || pActFrame->GetObjectShell() != m_pData->m_pObjectShell )
3075             pActFrame = SfxViewFrame::GetFirst( m_pData->m_pObjectShell );
3076 
3077         if ( !pActFrame || !pActFrame->GetViewShell() )
3078             // currently no frame for this document at all or View is under construction
3079             return uno::Reference < container::XIndexAccess >();
3080 
3081         m_pData->m_contViewData = Reference < container::XIndexAccess >(
3082                 ::comphelper::getProcessServiceFactory()->createInstance(
3083                 DEFINE_CONST_UNICODE("com.sun.star.document.IndexedPropertyValues") ),
3084                 uno::UNO_QUERY );
3085 
3086         if ( !m_pData->m_contViewData.is() )
3087         {
3088             // error: no container class available!
3089             return uno::Reference < container::XIndexAccess >();
3090         }
3091 
3092         uno::Reference < container::XIndexContainer > xCont( m_pData->m_contViewData, uno::UNO_QUERY );
3093         sal_Int32 nCount = 0;
3094         uno::Sequence < beans::PropertyValue > aSeq;
3095         ::com::sun::star::uno::Any aAny;
3096         for ( SfxViewFrame *pFrame = SfxViewFrame::GetFirst( m_pData->m_pObjectShell ); pFrame;
3097                 pFrame = SfxViewFrame::GetNext( *pFrame, m_pData->m_pObjectShell ) )
3098         {
3099             sal_Bool bIsActive = ( pFrame == pActFrame );
3100             pFrame->GetViewShell()->WriteUserDataSequence( aSeq );
3101             aAny <<= aSeq;
3102             xCont->insertByIndex( bIsActive ? 0 : nCount, aAny );
3103             nCount++;
3104         }
3105     }
3106 
3107     return m_pData->m_contViewData;
3108 }
3109 
setViewData(const uno::Reference<container::XIndexAccess> & aData)3110 void SAL_CALL SfxBaseModel::setViewData( const uno::Reference < container::XIndexAccess >& aData )
3111 {
3112     SfxModelGuard aGuard( *this );
3113 
3114     m_pData->m_contViewData = aData;
3115 }
3116 
3117 /** calls all XEventListeners */
notifyEvent(const::com::sun::star::document::EventObject & aEvent) const3118 void SfxBaseModel::notifyEvent( const ::com::sun::star::document::EventObject& aEvent ) const
3119 {
3120     // object already disposed?
3121     if ( impl_isDisposed() )
3122         return;
3123 
3124     ::cppu::OInterfaceContainerHelper* pIC = m_pData->m_aInterfaceContainer.getContainer(
3125                                         ::getCppuType((const uno::Reference< XDOCEVENTLISTENER >*)0) );
3126     if( pIC )
3127 
3128     {
3129         ::cppu::OInterfaceIteratorHelper aIt( *pIC );
3130         while( aIt.hasMoreElements() )
3131         {
3132             try
3133             {
3134                 ((XDOCEVENTLISTENER *)aIt.next())->notifyEvent( aEvent );
3135             }
3136             catch( uno::RuntimeException& )
3137             {
3138                 aIt.remove();
3139             }
3140         }
3141     }
3142 }
3143 
3144 /** returns true if someone added a XEventListener to this XEventBroadcaster */
hasEventListeners() const3145 sal_Bool SfxBaseModel::hasEventListeners() const
3146 {
3147     return !impl_isDisposed() && (NULL != m_pData->m_aInterfaceContainer.getContainer( ::getCppuType((const uno::Reference< XDOCEVENTLISTENER >*)0) ) );
3148 }
3149 
addPrintJobListener(const uno::Reference<view::XPrintJobListener> & xListener)3150 void SAL_CALL SfxBaseModel::addPrintJobListener( const uno::Reference< view::XPrintJobListener >& xListener )
3151 {
3152     SfxModelGuard aGuard( *this, SfxModelGuard::E_INITIALIZING );
3153 
3154     if ( impl_getPrintHelper() )
3155     {
3156         uno::Reference < view::XPrintJobBroadcaster > xPJB( m_pData->m_xPrintable, uno::UNO_QUERY );
3157         if ( xPJB.is() )
3158             xPJB->addPrintJobListener( xListener );
3159     }
3160 //  else
3161 //      m_pData->m_aInterfaceContainer.addInterface( ::getCppuType((const uno::Reference< view::XPrintJobListener >*)0), xListener );
3162 }
3163 
removePrintJobListener(const uno::Reference<view::XPrintJobListener> & xListener)3164 void SAL_CALL SfxBaseModel::removePrintJobListener( const uno::Reference< view::XPrintJobListener >& xListener )
3165 {
3166     SfxModelGuard aGuard( *this );
3167 
3168     if ( impl_getPrintHelper() )
3169     {
3170         uno::Reference < view::XPrintJobBroadcaster > xPJB( m_pData->m_xPrintable, uno::UNO_QUERY );
3171         if ( xPJB.is() )
3172             xPJB->removePrintJobListener( xListener );
3173     }
3174 //  else
3175 //      m_pData->m_aInterfaceContainer.addInterface( ::getCppuType((const uno::Reference< view::XPrintJobListener >*)0), xListener );
3176 }
3177 
3178 // simple declaration of class SvObject is enough
3179 // the corresponding <so3/iface.hxx> cannon be included because it provides
3180 // declaration of class SvBorder that conflicts with ../../inc/viewfrm.hxx
3181 class SvObject;
getSomething(const::com::sun::star::uno::Sequence<sal_Int8> & aIdentifier)3182 sal_Int64 SAL_CALL SfxBaseModel::getSomething( const ::com::sun::star::uno::Sequence< sal_Int8 >& aIdentifier )
3183 {
3184     ::vos::OGuard aGuard( Application::GetSolarMutex() );
3185     if ( GetObjectShell() )
3186     {
3187         SvGlobalName aName( aIdentifier );
3188         if ( aName == SvGlobalName( SO3_GLOBAL_CLASSID ) )
3189             return (sal_Int64)(sal_IntPtr)(SvObject*)GetObjectShell();
3190         else if ( aName == SvGlobalName( SFX_GLOBAL_CLASSID ) )
3191             return (sal_Int64)(sal_IntPtr)(SfxObjectShell*)GetObjectShell();
3192     }
3193 
3194     return 0;
3195 }
3196 
3197 //____________________________________________________________________________________________________
3198 //  XDocumentSubStorageSupplier
3199 //____________________________________________________________________________________________________
3200 
ListenForStorage_Impl(const uno::Reference<embed::XStorage> & xStorage)3201 void SfxBaseModel::ListenForStorage_Impl( const uno::Reference< embed::XStorage >& xStorage )
3202 {
3203     uno::Reference< util::XModifiable > xModifiable( xStorage, uno::UNO_QUERY );
3204     if ( xModifiable.is() )
3205     {
3206         if ( !m_pData->m_pStorageModifyListen.is() )
3207         {
3208             m_pData->m_pStorageModifyListen = new ::sfx2::DocumentStorageModifyListener( *m_pData, Application::GetSolarMutex() );
3209         }
3210 
3211         // no need to deregister the listening for old storage since it should be disposed automatically
3212         xModifiable->addModifyListener( m_pData->m_pStorageModifyListen.get() );
3213     }
3214 }
3215 
getDocumentSubStorage(const::rtl::OUString & aStorageName,sal_Int32 nMode)3216 uno::Reference< XSTORAGE > SAL_CALL SfxBaseModel::getDocumentSubStorage( const ::rtl::OUString& aStorageName, sal_Int32 nMode )
3217 {
3218     SfxModelGuard aGuard( *this );
3219 
3220     uno::Reference< XSTORAGE > xResult;
3221     if ( m_pData->m_pObjectShell.Is() )
3222     {
3223         uno::Reference< embed::XStorage > xStorage = m_pData->m_pObjectShell->GetStorage();
3224         if ( xStorage.is() )
3225         {
3226             try
3227             {
3228                 xResult = xStorage->openStorageElement( aStorageName, nMode );
3229             }
3230             catch ( uno::Exception& )
3231             {
3232             }
3233         }
3234     }
3235 
3236     return xResult;
3237 }
3238 
getDocumentSubStoragesNames()3239 Sequence< ::rtl::OUString > SAL_CALL SfxBaseModel::getDocumentSubStoragesNames()
3240 {
3241     SfxModelGuard aGuard( *this );
3242 
3243     Sequence< ::rtl::OUString > aResult;
3244     sal_Int32 nResultSize = 0;
3245     sal_Bool bSuccess = sal_False;
3246     if ( m_pData->m_pObjectShell.Is() )
3247     {
3248         uno::Reference < embed::XStorage > xStorage = m_pData->m_pObjectShell->GetStorage();
3249         uno::Reference < container::XNameAccess > xAccess( xStorage, uno::UNO_QUERY );
3250         if ( xAccess.is() )
3251         {
3252             Sequence< ::rtl::OUString > aTemp = xAccess->getElementNames();
3253             for ( sal_Int32 n = 0; n < aTemp.getLength(); n++ )
3254             {
3255                 if ( xStorage->isStorageElement( aTemp[n] ) )
3256                 {
3257                     aResult.realloc( ++nResultSize );
3258                     aResult[ nResultSize - 1 ] = aTemp[n];
3259                 }
3260             }
3261 
3262             bSuccess = sal_True;
3263         }
3264     }
3265 
3266     if ( !bSuccess )
3267         throw io::IOException();
3268 
3269        return aResult;
3270 }
3271 
3272 //____________________________________________________________________________________________________
3273 //  XScriptProviderSupplier
3274 //____________________________________________________________________________________________________
3275 
3276 
getScriptProvider()3277 uno::Reference< script::provider::XScriptProvider > SAL_CALL SfxBaseModel::getScriptProvider()
3278 {
3279     SfxModelGuard aGuard( *this );
3280 
3281     uno::Reference< script::provider::XScriptProvider > xScriptProvider;
3282 
3283     ::comphelper::ComponentContext aContext( ::comphelper::getProcessServiceFactory() );
3284     Reference< script::provider::XScriptProviderFactory > xScriptProviderFactory(
3285         aContext.getSingleton( "com.sun.star.script.provider.theMasterScriptProviderFactory" ), uno::UNO_QUERY_THROW );
3286 
3287     try
3288     {
3289         Reference< XScriptInvocationContext > xScriptContext( this );
3290         xScriptProvider.set( xScriptProviderFactory->createScriptProvider( makeAny( xScriptContext ) ), uno::UNO_SET_THROW );
3291     }
3292     catch( const uno::RuntimeException& )
3293     {
3294         throw;
3295     }
3296     catch( const lang::IllegalArgumentException& )
3297     {
3298         throw lang::WrappedTargetRuntimeException(
3299             ::rtl::OUString(),
3300             *this,
3301             ::cppu::getCaughtException()
3302         );
3303     }
3304 
3305     return xScriptProvider;
3306 }
3307 
3308 //____________________________________________________________________________________________________
3309 //  XUIConfigurationManagerSupplier
3310 //____________________________________________________________________________________________________
3311 
getRuntimeUID() const3312 rtl::OUString SfxBaseModel::getRuntimeUID() const
3313 {
3314     OSL_ENSURE( m_pData->m_sRuntimeUID.getLength() > 0,
3315                 "SfxBaseModel::getRuntimeUID - ID is empty!" );
3316     return m_pData->m_sRuntimeUID;
3317 }
3318 
hasValidSignatures() const3319 sal_Bool SfxBaseModel::hasValidSignatures() const
3320 {
3321     ::vos::OGuard aGuard( Application::GetSolarMutex() );
3322     if ( m_pData->m_pObjectShell.Is() )
3323         return ( m_pData->m_pObjectShell->ImplGetSignatureState( sal_False ) == SIGNATURESTATE_SIGNATURES_OK );
3324     return sal_False;
3325 }
3326 
GetCommandFromSequence(rtl::OUString & rCommand,sal_Int32 & nIndex,const uno::Sequence<beans::PropertyValue> & rSeqPropValue)3327 static void GetCommandFromSequence( rtl::OUString& rCommand, sal_Int32& nIndex, const uno::Sequence< beans::PropertyValue >& rSeqPropValue )
3328 {
3329     rtl::OUString aCommand;
3330     nIndex = -1;
3331 
3332     for ( sal_Int32 i = 0; i < rSeqPropValue.getLength(); i++ )
3333     {
3334         if ( rSeqPropValue[i].Name.equalsAsciiL( "Command", 7 ))
3335         {
3336             rSeqPropValue[i].Value >>= rCommand;
3337             nIndex = i;
3338             return;
3339         }
3340     }
3341 }
3342 
ConvertSlotsToCommands(SfxObjectShell * pDoc,uno::Reference<container::XIndexContainer> & rToolbarDefinition)3343 static void ConvertSlotsToCommands( SfxObjectShell* pDoc, uno::Reference< container::XIndexContainer >& rToolbarDefinition )
3344 {
3345     if ( pDoc )
3346     {
3347         Any           aAny;
3348         SfxModule*    pModule( pDoc->GetFactory().GetModule() );
3349         rtl::OUString aSlotCmd( RTL_CONSTASCII_USTRINGPARAM( "slot:" ));
3350         rtl::OUString aUnoCmd( RTL_CONSTASCII_USTRINGPARAM( ".uno:" ));
3351         uno::Sequence< beans::PropertyValue > aSeqPropValue;
3352 
3353         for ( sal_Int32 i = 0; i < rToolbarDefinition->getCount(); i++ )
3354         {
3355             sal_Int32 nIndex( -1 );
3356             rtl::OUString aCommand;
3357 
3358             if ( rToolbarDefinition->getByIndex( i ) >>= aSeqPropValue )
3359             {
3360                 GetCommandFromSequence( aCommand, nIndex, aSeqPropValue );
3361                 if ( nIndex >= 0 && ( aCommand.indexOf( aSlotCmd ) == 0 ))
3362                 {
3363                     rtl::OUString aSlot( aCommand.copy( 5 ));
3364 
3365                     // We have to replace the old "slot-Command" with our new ".uno:-Command"
3366                     const SfxSlot* pSlot = pModule->GetSlotPool()->GetSlot( sal_uInt16( aSlot.toInt32() ));
3367                     if ( pSlot )
3368                     {
3369                         rtl::OUStringBuffer aStrBuf( aUnoCmd );
3370                         aStrBuf.appendAscii( pSlot->GetUnoName() );
3371 
3372                         aCommand = aStrBuf.makeStringAndClear();
3373                         aSeqPropValue[nIndex].Value <<= aCommand;
3374                         rToolbarDefinition->replaceByIndex( i, Any( aSeqPropValue ));
3375                     }
3376                 }
3377             }
3378         }
3379     }
3380 }
3381 
getUIConfigurationManager()3382 uno::Reference< ui::XUIConfigurationManager > SAL_CALL SfxBaseModel::getUIConfigurationManager()
3383 {
3384     SfxModelGuard aGuard( *this );
3385 
3386     if ( !m_pData->m_xUIConfigurationManager.is() )
3387     {
3388         uno::Reference< ui::XUIConfigurationManager > xNewUIConfMan(
3389             ::comphelper::getProcessServiceFactory()->createInstance(
3390                 ::rtl::OUString::createFromAscii( "com.sun.star.ui.UIConfigurationManager" )),
3391                 uno::UNO_QUERY );
3392 
3393         Reference< ui::XUIConfigurationStorage > xUIConfigStorage( xNewUIConfMan, uno::UNO_QUERY );
3394         if ( xUIConfigStorage.is() )
3395         {
3396             uno::Reference< XSTORAGE > xConfigStorage;
3397 
3398             rtl::OUString aUIConfigFolderName( RTL_CONSTASCII_USTRINGPARAM( "Configurations2" ));
3399             // First try to open with READWRITE and then READ
3400             xConfigStorage = getDocumentSubStorage( aUIConfigFolderName, embed::ElementModes::READWRITE );
3401             if ( xConfigStorage.is() )
3402             {
3403                 rtl::OUString aMediaTypeProp( RTL_CONSTASCII_USTRINGPARAM( "MediaType" ));
3404                 rtl::OUString aUIConfigMediaType(
3405                         RTL_CONSTASCII_USTRINGPARAM( "application/vnd.sun.xml.ui.configuration" ) );
3406                 rtl::OUString aMediaType;
3407                 uno::Reference< beans::XPropertySet > xPropSet( xConfigStorage, uno::UNO_QUERY );
3408                 Any a = xPropSet->getPropertyValue( aMediaTypeProp );
3409                 if ( !( a >>= aMediaType ) || ( aMediaType.getLength() == 0 ))
3410                 {
3411                     a <<= aUIConfigMediaType;
3412                     xPropSet->setPropertyValue( aMediaTypeProp, a );
3413                 }
3414             }
3415             else
3416                 xConfigStorage = getDocumentSubStorage( aUIConfigFolderName, embed::ElementModes::READ );
3417 
3418             // initialize ui configuration manager with document substorage
3419             xUIConfigStorage->setStorage( xConfigStorage );
3420 
3421             // embedded objects did not support local configuration data until OOo 3.0, so there's nothing to
3422             // migrate
3423             if ( m_pData->m_pObjectShell->GetCreateMode() != SFX_CREATE_MODE_EMBEDDED )
3424             {
3425                 // Import old UI configuration from OOo 1.x
3426                 uno::Reference< XSTORAGE > xOOo1ConfigStorage;
3427                 rtl::OUString         aOOo1UIConfigFolderName( RTL_CONSTASCII_USTRINGPARAM( "Configurations" ));
3428 
3429                 // Try to open with READ
3430                 xOOo1ConfigStorage = getDocumentSubStorage( aOOo1UIConfigFolderName, embed::ElementModes::READ );
3431                 if ( xOOo1ConfigStorage.is() )
3432                 {
3433                     uno::Reference< lang::XMultiServiceFactory > xServiceMgr( ::comphelper::getProcessServiceFactory() );
3434                     uno::Sequence< uno::Reference< container::XIndexContainer > > rToolbars;
3435 
3436                     sal_Bool bImported = framework::UIConfigurationImporterOOo1x::ImportCustomToolbars(
3437                                             xNewUIConfMan, rToolbars, xServiceMgr, xOOo1ConfigStorage );
3438                     if ( bImported )
3439                     {
3440                         SfxObjectShell* pObjShell = SfxBaseModel::GetObjectShell();
3441 
3442                         char aNum[]   = "private:resource/toolbar/custom_OOo1x_0";
3443                         char aTitle[] = "Toolbar 0";
3444                         sal_Int32 nNumIndex = strlen( aNum )-1;
3445                         sal_Int32 nTitleIndex = strlen( aTitle )-1;
3446                         for ( sal_Int32 i = 0; i < rToolbars.getLength(); i++ )
3447                         {
3448                             aNum[nNumIndex]++;
3449                             aTitle[nTitleIndex]++;
3450 
3451                             rtl::OUString aCustomTbxName( RTL_CONSTASCII_USTRINGPARAM( aNum ));
3452                             rtl::OUString aCustomTbxTitle( RTL_CONSTASCII_USTRINGPARAM( aTitle ));
3453 
3454                             uno::Reference< container::XIndexContainer > xToolbar = rToolbars[i];
3455                             ConvertSlotsToCommands( pObjShell, xToolbar );
3456                             if ( !xNewUIConfMan->hasSettings( aCustomTbxName ))
3457                             {
3458                                 // Set UIName for the toolbar with container property
3459                                 uno::Reference< beans::XPropertySet > xPropSet( xToolbar, UNO_QUERY );
3460                                 if ( xPropSet.is() )
3461                                 {
3462                                     try
3463                                     {
3464                                         rtl::OUString aPropName( RTL_CONSTASCII_USTRINGPARAM( "UIName" ));
3465                                         Any           aAny( aCustomTbxTitle );
3466                                         xPropSet->setPropertyValue( aPropName, aAny );
3467                                     }
3468                                     catch ( beans::UnknownPropertyException& )
3469                                     {
3470                                     }
3471                                 }
3472 
3473                                 uno::Reference< container::XIndexAccess > xToolbarData( xToolbar, uno::UNO_QUERY );
3474                                 xNewUIConfMan->insertSettings( aCustomTbxName, xToolbarData );
3475                                 uno::Reference< ui::XUIConfigurationPersistence > xPersist( xNewUIConfMan, uno::UNO_QUERY );
3476                                 xPersist->store();
3477                             }
3478                         }
3479                     }
3480                 }
3481             }
3482         }
3483 
3484         m_pData->m_xUIConfigurationManager = xNewUIConfMan;
3485     }
3486 
3487     return m_pData->m_xUIConfigurationManager;
3488 }
3489 
3490 //____________________________________________________________________________________________________
3491 //  XVisualObject
3492 //____________________________________________________________________________________________________
3493 
setVisualAreaSize(sal_Int64 nAspect,const awt::Size & aSize)3494 void SAL_CALL SfxBaseModel::setVisualAreaSize( sal_Int64 nAspect, const awt::Size& aSize )
3495 {
3496     SfxModelGuard aGuard( *this );
3497 
3498     if ( !m_pData->m_pObjectShell.Is() )
3499         throw uno::Exception(); // TODO: error handling
3500 
3501     SfxViewFrame* pViewFrm = SfxViewFrame::GetFirst( m_pData->m_pObjectShell, sal_False );
3502     if ( pViewFrm && m_pData->m_pObjectShell->GetCreateMode() == SFX_CREATE_MODE_EMBEDDED && !pViewFrm->GetFrame().IsInPlace() )
3503     {
3504         Window* pWindow = VCLUnoHelper::GetWindow( pViewFrm->GetFrame().GetFrameInterface()->getContainerWindow() );
3505         Size aWinSize = pWindow->GetSizePixel();
3506         awt::Size aCurrent = getVisualAreaSize( nAspect );
3507         Size aDiff( aSize.Width-aCurrent.Width, aSize.Height-aCurrent.Height );
3508         Size aWrongDiff = OutputDevice::LogicToLogic( aDiff , m_pData->m_pObjectShell->GetMapUnit(), pWindow->GetMapMode() );
3509         aDiff = pViewFrm->GetViewShell()->GetWindow()->LogicToPixel( aDiff );
3510         aWinSize.Width() += aDiff.Width();
3511         aWinSize.Height() += aDiff.Height();
3512         pWindow->SetSizePixel( aWinSize );
3513     }
3514     else
3515     {
3516         Rectangle aTmpRect = m_pData->m_pObjectShell->GetVisArea( ASPECT_CONTENT );
3517         aTmpRect.SetSize( Size( aSize.Width, aSize.Height ) );
3518         m_pData->m_pObjectShell->SetVisArea( aTmpRect );
3519     }
3520 }
3521 
getVisualAreaSize(sal_Int64)3522 awt::Size SAL_CALL SfxBaseModel::getVisualAreaSize( sal_Int64 /*nAspect*/ )
3523 {
3524     SfxModelGuard aGuard( *this );
3525 
3526     if ( !m_pData->m_pObjectShell.Is() )
3527         throw uno::Exception(); // TODO: error handling
3528 
3529     Rectangle aTmpRect = m_pData->m_pObjectShell->GetVisArea( ASPECT_CONTENT );
3530 
3531 #if 0
3532     Window* pWindow = NULL;
3533     SfxViewFrame* pViewFrm = m_pData->m_pObjectShell.Is() ?
3534                                 SfxViewFrame::GetFirst( m_pData->m_pObjectShell, 0, sal_False ) : 0;
3535 
3536     if ( pWindow )
3537     {
3538         MapMode aInternalMapMode( pViewFrm->GetWindow().GetMapMode() );
3539         MapMode aExternalMapMode( m_pData->m_pObjectShell->GetMapUnit() );
3540 
3541         aTmpRect = OutputDevice::LogicToLogic( aTmpRect, aInternalMapMode, aExternalMapMode );
3542     }
3543 #endif
3544 
3545     return awt::Size( aTmpRect.GetWidth(), aTmpRect.GetHeight() );
3546 }
3547 
3548 
getMapUnit(sal_Int64)3549 sal_Int32 SAL_CALL SfxBaseModel::getMapUnit( sal_Int64 /*nAspect*/ )
3550 {
3551     SfxModelGuard aGuard( *this );
3552 
3553     if ( !m_pData->m_pObjectShell.Is() )
3554         throw uno::Exception(); // TODO: error handling
3555 
3556     return VCLUnoHelper::VCL2UnoEmbedMapUnit( m_pData->m_pObjectShell->GetMapUnit() );
3557 }
3558 
getPreferredVisualRepresentation(::sal_Int64)3559 embed::VisualRepresentation SAL_CALL SfxBaseModel::getPreferredVisualRepresentation( ::sal_Int64 /*nAspect*/ )
3560 {
3561     SfxModelGuard aGuard( *this );
3562 
3563     datatransfer::DataFlavor aDataFlavor(
3564             ::rtl::OUString::createFromAscii( "application/x-openoffice-gdimetafile;windows_formatname=\"GDIMetaFile\"" ),
3565             ::rtl::OUString::createFromAscii( "GDIMetaFile" ),
3566             ::getCppuType( (const uno::Sequence< sal_Int8 >*) NULL ) );
3567 
3568     embed::VisualRepresentation aVisualRepresentation;
3569     aVisualRepresentation.Data = getTransferData( aDataFlavor );
3570     aVisualRepresentation.Flavor = aDataFlavor;
3571 
3572     return aVisualRepresentation;
3573 }
3574 
3575 //____________________________________________________________________________________________________
3576 //  XStorageBasedDocument
3577 //____________________________________________________________________________________________________
3578 
loadFromStorage(const uno::Reference<XSTORAGE> & xStorage,const uno::Sequence<beans::PropertyValue> & aMediaDescriptor)3579 void SAL_CALL SfxBaseModel::loadFromStorage( const uno::Reference< XSTORAGE >& xStorage,
3580                                              const uno::Sequence< beans::PropertyValue >& aMediaDescriptor )
3581 {
3582     SfxModelGuard aGuard( *this, SfxModelGuard::E_INITIALIZING );
3583     if ( IsInitialized() )
3584         throw ::com::sun::star::frame::DoubleInitializationException( ::rtl::OUString(), *this );
3585 
3586     // after i36090 is fixed the pool from object shell can be used
3587     // SfxAllItemSet aSet( m_pData->m_pObjectShell->GetPool() );
3588     SfxAllItemSet aSet( SFX_APP()->GetPool() );
3589 
3590     // the BaseURL is part of the ItemSet
3591     SfxMedium* pMedium = new SfxMedium( xStorage, String() );
3592     TransformParameters( SID_OPENDOC, aMediaDescriptor, aSet );
3593     pMedium->GetItemSet()->Put( aSet );
3594 
3595     // allow to use an interactionhandler (if there is one)
3596     pMedium->UseInteractionHandler( sal_True );
3597 
3598     SFX_ITEMSET_ARG( &aSet, pTemplateItem, SfxBoolItem, SID_TEMPLATE, sal_False);
3599     sal_Bool bTemplate = pTemplateItem && pTemplateItem->GetValue();
3600     m_pData->m_pObjectShell->SetActivateEvent_Impl( bTemplate ? SFX_EVENT_CREATEDOC : SFX_EVENT_OPENDOC );
3601     m_pData->m_pObjectShell->Get_Impl()->bOwnsStorage = sal_False;
3602 
3603     // load document
3604     if ( !m_pData->m_pObjectShell->DoLoad(pMedium) )
3605     {
3606         sal_uInt32 nError = m_pData->m_pObjectShell->GetErrorCode();
3607         throw task::ErrorCodeIOException( ::rtl::OUString(),
3608                                             uno::Reference< uno::XInterface >(),
3609                                             nError ? nError : ERRCODE_IO_CANTREAD );
3610     }
3611 }
3612 
storeToStorage(const uno::Reference<XSTORAGE> & xStorage,const uno::Sequence<beans::PropertyValue> & aMediaDescriptor)3613 void SAL_CALL SfxBaseModel::storeToStorage( const uno::Reference< XSTORAGE >& xStorage,
3614                                 const uno::Sequence< beans::PropertyValue >& aMediaDescriptor )
3615 {
3616     SfxModelGuard aGuard( *this );
3617 
3618     uno::Reference< XSTORAGE > xResult;
3619     if ( !m_pData->m_pObjectShell.Is() )
3620         throw IOEXCEPTION(); // TODO:
3621 
3622     SfxAllItemSet aSet( m_pData->m_pObjectShell->GetPool() );
3623     TransformParameters( SID_SAVEASDOC, aMediaDescriptor, aSet );
3624 
3625     // TODO/LATER: may be a special URL "private:storage" should be used
3626     SFX_ITEMSET_ARG( &aSet, pItem, SfxStringItem, SID_FILTER_NAME, sal_False );
3627     sal_Int32 nVersion = SOFFICE_FILEFORMAT_CURRENT;
3628     if( pItem )
3629     {
3630         String aFilterName = pItem->GetValue();
3631         const SfxFilter* pFilter = SFX_APP()->GetFilterMatcher().GetFilter4FilterName( aFilterName );
3632         if ( pFilter && pFilter->UsesStorage() )
3633             nVersion = pFilter->GetVersion();
3634     }
3635 
3636     sal_Bool bSuccess = sal_False;
3637     if ( xStorage == m_pData->m_pObjectShell->GetStorage() )
3638     {
3639         // storing to the own storage
3640         bSuccess = m_pData->m_pObjectShell->DoSave();
3641     }
3642     else
3643     {
3644         // TODO/LATER: if the provided storage has some data inside the storing might fail, probably the storage must be truncated
3645         // TODO/LATER: is it possible to have a template here?
3646         m_pData->m_pObjectShell->SetupStorage( xStorage, nVersion, sal_False );
3647 
3648         // BaseURL is part of the ItemSet
3649         SfxMedium aMedium( xStorage, String(), &aSet );
3650         aMedium.CanDisposeStorage_Impl( sal_False );
3651         if ( aMedium.GetFilter() )
3652         {
3653             // storing without a valid filter will often crash
3654             bSuccess = m_pData->m_pObjectShell->DoSaveObjectAs( aMedium, sal_True );
3655             m_pData->m_pObjectShell->DoSaveCompleted( NULL );
3656         }
3657     }
3658 
3659     sal_uInt32 nError = m_pData->m_pObjectShell->GetErrorCode();
3660     m_pData->m_pObjectShell->ResetError();
3661 
3662     // the warnings are currently not transported
3663     if ( !bSuccess )
3664     {
3665         throw task::ErrorCodeIOException( ::rtl::OUString(),
3666                                             uno::Reference< uno::XInterface >(),
3667                                             nError ? nError : ERRCODE_IO_GENERAL );
3668     }
3669 }
3670 
switchToStorage(const uno::Reference<XSTORAGE> & xStorage)3671 void SAL_CALL SfxBaseModel::switchToStorage( const uno::Reference< XSTORAGE >& xStorage )
3672 {
3673     SfxModelGuard aGuard( *this );
3674 
3675     uno::Reference< XSTORAGE > xResult;
3676     if ( !m_pData->m_pObjectShell.Is() )
3677         throw IOEXCEPTION(); // TODO:
3678 
3679     // the persistence should be switched only if the storage is different
3680     if ( xStorage != m_pData->m_pObjectShell->GetStorage()
3681       && !m_pData->m_pObjectShell->SwitchPersistance( xStorage ) )
3682     {
3683         sal_uInt32 nError = m_pData->m_pObjectShell->GetErrorCode();
3684         throw task::ErrorCodeIOException( ::rtl::OUString(),
3685                                             uno::Reference< uno::XInterface >(),
3686                                             nError ? nError : ERRCODE_IO_GENERAL );
3687     }
3688 
3689     m_pData->m_pObjectShell->Get_Impl()->bOwnsStorage = sal_False;
3690 }
3691 
getDocumentStorage()3692 uno::Reference< XSTORAGE > SAL_CALL SfxBaseModel::getDocumentStorage()
3693 {
3694     SfxModelGuard aGuard( *this );
3695 
3696     uno::Reference< XSTORAGE > xResult;
3697     if ( !m_pData->m_pObjectShell.Is() )
3698         throw IOEXCEPTION(); // TODO
3699 
3700     return m_pData->m_pObjectShell->GetStorage();
3701 }
3702 
addStorageChangeListener(const uno::Reference<document::XStorageChangeListener> & xListener)3703 void SAL_CALL SfxBaseModel::addStorageChangeListener(
3704             const uno::Reference< document::XStorageChangeListener >& xListener )
3705 {
3706     SfxModelGuard aGuard( *this, SfxModelGuard::E_INITIALIZING );
3707 
3708     m_pData->m_aInterfaceContainer.addInterface(
3709                                     ::getCppuType((const uno::Reference< document::XStorageChangeListener >*)0), xListener );
3710 }
3711 
removeStorageChangeListener(const uno::Reference<document::XStorageChangeListener> & xListener)3712 void SAL_CALL SfxBaseModel::removeStorageChangeListener(
3713             const uno::Reference< document::XStorageChangeListener >& xListener )
3714 {
3715     SfxModelGuard aGuard( *this );
3716 
3717     m_pData->m_aInterfaceContainer.removeInterface(
3718                                     ::getCppuType((const uno::Reference< document::XStorageChangeListener >*)0), xListener );
3719 }
3720 
3721 #include "printhelper.hxx"
impl_getPrintHelper()3722 bool SfxBaseModel::impl_getPrintHelper()
3723 {
3724     if ( m_pData->m_xPrintable.is() )
3725         return true;
3726     m_pData->m_xPrintable = new SfxPrintHelper();
3727     uno::Reference < lang::XInitialization > xInit( m_pData->m_xPrintable, uno::UNO_QUERY );
3728     uno::Sequence < uno::Any > aValues(1);
3729     aValues[0] <<= uno::Reference < frame::XModel > (static_cast< frame::XModel* >(this), uno::UNO_QUERY );
3730     xInit->initialize( aValues );
3731     uno::Reference < view::XPrintJobBroadcaster > xBrd( m_pData->m_xPrintable, uno::UNO_QUERY );
3732     xBrd->addPrintJobListener( new SfxPrintHelperListener_Impl( m_pData ) );
3733     return true;
3734 }
3735 
3736 //=============================================================================
3737 // css.frame.XModule
setIdentifier(const::rtl::OUString & Identifier)3738  void SAL_CALL SfxBaseModel::setIdentifier(const ::rtl::OUString& Identifier)
3739 {
3740     SfxModelGuard aGuard( *this );
3741     m_pData->m_sModuleIdentifier = Identifier;
3742 }
3743 
3744 //=============================================================================
3745 // css.frame.XModule
getIdentifier()3746  ::rtl::OUString SAL_CALL SfxBaseModel::getIdentifier()
3747 {
3748     SfxModelGuard aGuard( *this );
3749     if (m_pData->m_sModuleIdentifier.getLength() > 0)
3750         return m_pData->m_sModuleIdentifier;
3751     if (m_pData->m_pObjectShell)
3752         return m_pData->m_pObjectShell->GetFactory().GetDocumentServiceName();
3753     return ::rtl::OUString();
3754 }
3755 
3756 //=============================================================================
impl_getTitleHelper()3757 css::uno::Reference< css::frame::XTitle > SfxBaseModel::impl_getTitleHelper ()
3758 {
3759     SfxModelGuard aGuard( *this );
3760 
3761     if ( ! m_pData->m_xTitleHelper.is ())
3762     {
3763         css::uno::Reference< css::lang::XMultiServiceFactory > xSMGR   = ::comphelper::getProcessServiceFactory ();
3764         css::uno::Reference< css::frame::XUntitledNumbers >    xDesktop(xSMGR->createInstance(SERVICENAME_DESKTOP), css::uno::UNO_QUERY_THROW);
3765         css::uno::Reference< css::frame::XModel >              xThis   (static_cast< css::frame::XModel* >(this), css::uno::UNO_QUERY_THROW);
3766 
3767         ::framework::TitleHelper* pHelper = new ::framework::TitleHelper(xSMGR);
3768         m_pData->m_xTitleHelper = css::uno::Reference< css::frame::XTitle >(static_cast< ::cppu::OWeakObject* >(pHelper), css::uno::UNO_QUERY_THROW);
3769         pHelper->setOwner                   (xThis   );
3770         pHelper->connectWithUntitledNumbers (xDesktop);
3771     }
3772 
3773     return m_pData->m_xTitleHelper;
3774 }
3775 
3776 //=============================================================================
impl_getUntitledHelper()3777 css::uno::Reference< css::frame::XUntitledNumbers > SfxBaseModel::impl_getUntitledHelper ()
3778 {
3779     SfxModelGuard aGuard( *this );
3780 
3781     if ( ! m_pData->m_xNumberedControllers.is ())
3782     {
3783         css::uno::Reference< css::frame::XModel > xThis   (static_cast< css::frame::XModel* >(this), css::uno::UNO_QUERY_THROW);
3784         ::comphelper::NumberedCollection*         pHelper = new ::comphelper::NumberedCollection();
3785 
3786         m_pData->m_xNumberedControllers = css::uno::Reference< css::frame::XUntitledNumbers >(static_cast< ::cppu::OWeakObject* >(pHelper), css::uno::UNO_QUERY_THROW);
3787 
3788         pHelper->setOwner          (xThis);
3789         pHelper->setUntitledPrefix (::rtl::OUString::createFromAscii(" : "));
3790     }
3791 
3792     return m_pData->m_xNumberedControllers;
3793 }
3794 
3795 //=============================================================================
3796 // css.frame.XTitle
getTitle()3797 ::rtl::OUString SAL_CALL SfxBaseModel::getTitle()
3798 {
3799     // SYNCHRONIZED ->
3800     SfxModelGuard aGuard( *this );
3801 
3802     ::rtl::OUString aResult = impl_getTitleHelper()->getTitle ();
3803     if ( !m_pData->m_bExternalTitle && m_pData->m_pObjectShell )
3804     {
3805         SfxMedium* pMedium = m_pData->m_pObjectShell->GetMedium();
3806         if ( pMedium )
3807         {
3808             SFX_ITEMSET_ARG( pMedium->GetItemSet(), pRepairedDocItem, SfxBoolItem, SID_REPAIRPACKAGE, sal_False );
3809             if ( pRepairedDocItem && pRepairedDocItem->GetValue() )
3810                 aResult += String( SfxResId(STR_REPAIREDDOCUMENT) );
3811         }
3812 
3813         if ( m_pData->m_pObjectShell->IsReadOnlyUI() || (pMedium && pMedium->IsReadOnly()) )
3814             aResult += ::rtl::OUString( String( SfxResId(STR_READONLY) ) );
3815         else if ( m_pData->m_pObjectShell->IsDocShared() )
3816             aResult += ::rtl::OUString( String( SfxResId(STR_SHARED) ) );
3817 
3818         if ( m_pData->m_pObjectShell->GetDocumentSignatureState() == SIGNATURESTATE_SIGNATURES_OK )
3819             aResult += String( SfxResId( RID_XMLSEC_DOCUMENTSIGNED ) );
3820     }
3821 
3822     return aResult;
3823 }
3824 
3825 //=============================================================================
3826 // css.frame.XTitle
setTitle(const::rtl::OUString & sTitle)3827 void SAL_CALL SfxBaseModel::setTitle( const ::rtl::OUString& sTitle )
3828 {
3829     // SYNCHRONIZED ->
3830     SfxModelGuard aGuard( *this );
3831 
3832     impl_getTitleHelper()->setTitle (sTitle);
3833     m_pData->m_bExternalTitle = sal_True;
3834 }
3835 
3836 //=============================================================================
3837 // css.frame.XTitleChangeBroadcaster
addTitleChangeListener(const css::uno::Reference<css::frame::XTitleChangeListener> & xListener)3838 void SAL_CALL SfxBaseModel::addTitleChangeListener( const css::uno::Reference< css::frame::XTitleChangeListener >& xListener )
3839 {
3840     // SYNCHRONIZED ->
3841     SfxModelGuard aGuard( *this, SfxModelGuard::E_INITIALIZING );
3842 
3843     css::uno::Reference< css::frame::XTitleChangeBroadcaster > xBroadcaster(impl_getTitleHelper(), css::uno::UNO_QUERY);
3844     if (xBroadcaster.is ())
3845         xBroadcaster->addTitleChangeListener (xListener);
3846 }
3847 
3848 //=============================================================================
3849 // css.frame.XTitleChangeBroadcaster
removeTitleChangeListener(const css::uno::Reference<css::frame::XTitleChangeListener> & xListener)3850 void SAL_CALL SfxBaseModel::removeTitleChangeListener( const css::uno::Reference< css::frame::XTitleChangeListener >& xListener )
3851 {
3852     // SYNCHRONIZED ->
3853     SfxModelGuard aGuard( *this );
3854 
3855     css::uno::Reference< css::frame::XTitleChangeBroadcaster > xBroadcaster(impl_getTitleHelper(), css::uno::UNO_QUERY);
3856     if (xBroadcaster.is ())
3857         xBroadcaster->removeTitleChangeListener (xListener);
3858 }
3859 
3860 //=============================================================================
3861 // css.frame.XUntitledNumbers
leaseNumber(const css::uno::Reference<css::uno::XInterface> & xComponent)3862 ::sal_Int32 SAL_CALL SfxBaseModel::leaseNumber( const css::uno::Reference< css::uno::XInterface >& xComponent )
3863 {
3864     SfxModelGuard aGuard( *this );
3865 
3866     return impl_getUntitledHelper ()->leaseNumber (xComponent);
3867 }
3868 
3869 //=============================================================================
3870 // css.frame.XUntitledNumbers
releaseNumber(::sal_Int32 nNumber)3871 void SAL_CALL SfxBaseModel::releaseNumber( ::sal_Int32 nNumber )
3872 {
3873     SfxModelGuard aGuard( *this );
3874     impl_getUntitledHelper ()->releaseNumber (nNumber);
3875 }
3876 
3877 //=============================================================================
3878 // css.frame.XUntitledNumbers
releaseNumberForComponent(const css::uno::Reference<css::uno::XInterface> & xComponent)3879 void SAL_CALL SfxBaseModel::releaseNumberForComponent( const css::uno::Reference< css::uno::XInterface >& xComponent )
3880 {
3881     SfxModelGuard aGuard( *this );
3882     impl_getUntitledHelper ()->releaseNumberForComponent (xComponent);
3883 }
3884 
3885 //=============================================================================
3886 // css.frame.XUntitledNumbers
getUntitledPrefix()3887 ::rtl::OUString SAL_CALL SfxBaseModel::getUntitledPrefix()
3888 {
3889     SfxModelGuard aGuard( *this );
3890     return impl_getUntitledHelper ()->getUntitledPrefix ();
3891 }
3892 
3893 //=============================================================================
3894 // css::frame::XModel2
getControllers()3895 css::uno::Reference< css::container::XEnumeration > SAL_CALL SfxBaseModel::getControllers()
3896 {
3897     SfxModelGuard aGuard( *this );
3898 
3899     sal_Int32 c = m_pData->m_seqControllers.getLength();
3900     sal_Int32 i = 0;
3901     css::uno::Sequence< css::uno::Any > lEnum(c);
3902     for (i=0; i<c; ++i)
3903         lEnum[i] <<= m_pData->m_seqControllers[i];
3904 
3905     ::comphelper::OAnyEnumeration*                      pEnum = new ::comphelper::OAnyEnumeration(lEnum);
3906     css::uno::Reference< css::container::XEnumeration > xEnum(static_cast< css::container::XEnumeration* >(pEnum), css::uno::UNO_QUERY_THROW);
3907     return xEnum;
3908 }
3909 
3910 //=============================================================================
3911 // css::frame::XModel2
getAvailableViewControllerNames()3912 css::uno::Sequence< ::rtl::OUString > SAL_CALL SfxBaseModel::getAvailableViewControllerNames()
3913 {
3914     SfxModelGuard aGuard( *this );
3915 
3916     const SfxObjectFactory& rDocumentFactory = GetObjectShell()->GetFactory();
3917     const sal_Int32 nViewFactoryCount = rDocumentFactory.GetViewFactoryCount();
3918 
3919     Sequence< ::rtl::OUString > aViewNames( nViewFactoryCount );
3920     for ( sal_Int32 nViewNo = 0; nViewNo < nViewFactoryCount; ++nViewNo )
3921         aViewNames[nViewNo] = rDocumentFactory.GetViewFactory( nViewNo ).GetAPIViewName();
3922     return aViewNames;
3923 }
3924 
3925 //=============================================================================
3926 // css::frame::XModel2
createDefaultViewController(const css::uno::Reference<css::frame::XFrame> & i_rFrame)3927 css::uno::Reference< css::frame::XController2 > SAL_CALL SfxBaseModel::createDefaultViewController( const css::uno::Reference< css::frame::XFrame >& i_rFrame )
3928 {
3929     SfxModelGuard aGuard( *this );
3930 
3931     const SfxObjectFactory& rDocumentFactory = GetObjectShell()->GetFactory();
3932     const ::rtl::OUString sDefaultViewName = rDocumentFactory.GetViewFactory( 0 ).GetAPIViewName();
3933 
3934     aGuard.clear();
3935 
3936     return createViewController( sDefaultViewName, Sequence< PropertyValue >(), i_rFrame );
3937 }
3938 
3939 //=============================================================================
3940 namespace sfx { namespace intern {
3941 
3942     /** a class which, in its dtor, cleans up various objects (well, at the moment only the frame) collected during
3943         the creation of a document view, unless the creation was successful.
3944     */
3945     class SAL_DLLPRIVATE ViewCreationGuard
3946     {
3947     public:
ViewCreationGuard()3948         ViewCreationGuard()
3949             :m_bSuccess( false )
3950         {
3951         }
3952 
~ViewCreationGuard()3953         ~ViewCreationGuard()
3954         {
3955             if ( !m_bSuccess )
3956                 impl_closeAll();
3957         }
3958 
takeFrameOwnership(SfxFrame * i_pFrame)3959         void takeFrameOwnership( SfxFrame* i_pFrame )
3960         {
3961             OSL_PRECOND( !m_aWeakFrame, "ViewCreationGuard::takeFrameOwnership: already have a frame!" );
3962             OSL_PRECOND( i_pFrame != NULL, "ViewCreationGuard::takeFrameOwnership: invalid frame!" );
3963             m_aWeakFrame = i_pFrame;
3964         }
3965 
releaseAll()3966         void    releaseAll()
3967         {
3968             m_bSuccess = true;
3969         }
3970 
3971     private:
impl_closeAll()3972         void impl_closeAll()
3973         {
3974             if ( m_aWeakFrame && !m_aWeakFrame->GetCurrentDocument() )
3975             {
3976                 m_aWeakFrame->SetFrameInterface_Impl( NULL );
3977                 m_aWeakFrame->DoClose();
3978             }
3979         }
3980 
3981     private:
3982         bool            m_bSuccess;
3983         SfxFrameWeak    m_aWeakFrame;
3984     };
3985 } }
3986 
3987 //=============================================================================
FindOrCreateViewFrame_Impl(const Reference<XFrame> & i_rFrame,::sfx::intern::ViewCreationGuard & i_rGuard) const3988 SfxViewFrame* SfxBaseModel::FindOrCreateViewFrame_Impl( const Reference< XFrame >& i_rFrame, ::sfx::intern::ViewCreationGuard& i_rGuard ) const
3989 {
3990     SfxViewFrame* pViewFrame = NULL;
3991     for (   pViewFrame = SfxViewFrame::GetFirst( GetObjectShell(), sal_False );
3992             pViewFrame;
3993             pViewFrame= SfxViewFrame::GetNext( *pViewFrame, GetObjectShell(), sal_False )
3994         )
3995     {
3996         if ( pViewFrame->GetFrame().GetFrameInterface() == i_rFrame )
3997             break;
3998     }
3999     if ( !pViewFrame )
4000     {
4001     #if OSL_DEBUG_LEVEL > 0
4002         for (   SfxFrame* pCheckFrame = SfxFrame::GetFirst();
4003                 pCheckFrame;
4004                 pCheckFrame = SfxFrame::GetNext( *pCheckFrame )
4005              )
4006         {
4007             if ( pCheckFrame->GetFrameInterface() == i_rFrame )
4008             {
4009                 if  (   ( pCheckFrame->GetCurrentViewFrame() != NULL )
4010                     ||  ( pCheckFrame->GetCurrentDocument() != NULL )
4011                     )
4012                     // Note that it is perfectly letgitimate that during loading into an XFrame which already contains
4013                     // a document, there exist two SfxFrame instances bound to this XFrame - the old one, which will be
4014                     // destroyed later, and the new one, which we're going to create
4015                     continue;
4016 
4017                 OSL_ENSURE( false, "SfxBaseModel::FindOrCreateViewFrame_Impl: there already is an SfxFrame for the given XFrame, but no view in it!" );
4018                     // nowadays, we're the only instance allowed to create an SfxFrame for an XFrame, so this case here should not happen
4019                 break;
4020             }
4021         }
4022     #endif
4023 
4024         SfxFrame* pTargetFrame = SfxFrame::Create( i_rFrame );
4025         ENSURE_OR_THROW( pTargetFrame, "could not create an SfxFrame" );
4026         i_rGuard.takeFrameOwnership( pTargetFrame );
4027 
4028         // prepare it
4029         pTargetFrame->PrepareForDoc_Impl( *GetObjectShell() );
4030 
4031         // create view frame
4032         pViewFrame = new SfxViewFrame( *pTargetFrame, GetObjectShell() );
4033     }
4034     return pViewFrame;
4035 }
4036 
4037 //=============================================================================
4038 // css::frame::XModel2
createViewController(const::rtl::OUString & i_rViewName,const Sequence<PropertyValue> & i_rArguments,const Reference<XFrame> & i_rFrame)4039 css::uno::Reference< css::frame::XController2 > SAL_CALL SfxBaseModel::createViewController(
4040         const ::rtl::OUString& i_rViewName, const Sequence< PropertyValue >& i_rArguments, const Reference< XFrame >& i_rFrame )
4041 {
4042     SfxModelGuard aGuard( *this );
4043 
4044     if ( !i_rFrame.is() )
4045         throw css::lang::IllegalArgumentException( ::rtl::OUString(), *this, 3 );
4046 
4047     // find the proper SFX view factory
4048     SfxViewFactory* pViewFactory = GetObjectShell()->GetFactory().GetViewFactoryByViewName( i_rViewName );
4049     if ( !pViewFactory )
4050         throw IllegalArgumentException( ::rtl::OUString(), *this, 1 );
4051 
4052     // determine previous shell (used in some special cases)
4053     Reference< XController > xPreviousController( i_rFrame->getController() );
4054     const Reference< XModel > xMe( this );
4055     if  (   ( xPreviousController.is() )
4056         &&  ( xMe != xPreviousController->getModel() )
4057         )
4058     {
4059         xPreviousController.clear();
4060     }
4061     SfxViewShell* pOldViewShell = SfxViewShell::Get( xPreviousController );
4062     OSL_ENSURE( !xPreviousController.is() || ( pOldViewShell != NULL ),
4063         "SfxBaseModel::createViewController: invalid old controller!" );
4064 
4065     // a guard which will clean up in case of failure
4066     ::sfx::intern::ViewCreationGuard aViewCreationGuard;
4067 
4068     // determine the ViewFrame belonging to the given XFrame
4069     SfxViewFrame* pViewFrame = FindOrCreateViewFrame_Impl( i_rFrame, aViewCreationGuard );
4070     OSL_POSTCOND( pViewFrame, "SfxBaseModel::createViewController: no frame?" );
4071 
4072     // delegate to SFX' view factory
4073     pViewFrame->GetBindings().ENTERREGISTRATIONS();
4074     SfxViewShell* pViewShell = pViewFactory->CreateInstance( pViewFrame, pOldViewShell );
4075     pViewFrame->GetBindings().LEAVEREGISTRATIONS();
4076     ENSURE_OR_THROW( pViewShell, "invalid view shell provided by factory" );
4077 
4078     // by setting the ViewShell it is prevented that disposing the Controller will destroy this ViewFrame also
4079     pViewFrame->GetDispatcher()->SetDisableFlags( 0 );
4080     pViewFrame->SetViewShell_Impl( pViewShell );
4081 
4082     // remember ViewID
4083     pViewFrame->SetCurViewId_Impl( pViewFactory->GetOrdinal() );
4084 
4085     // ensure a default controller, if the view shell did not provide an own implementation
4086     if ( !pViewShell->GetController().is() )
4087         pViewShell->SetController( new SfxBaseController( pViewShell ) );
4088 
4089     // pass the creation arguments to the controller
4090     SfxBaseController* pBaseController = pViewShell->GetBaseController_Impl();
4091     ENSURE_OR_THROW( pBaseController, "invalid controller implementation!" );
4092     pBaseController->SetCreationArguments_Impl( i_rArguments );
4093 
4094     // some initial view settings, coming from our most recent attachResource call
4095     ::comphelper::NamedValueCollection aDocumentLoadArgs( getArgs() );
4096     if ( aDocumentLoadArgs.getOrDefault( "ViewOnly", false ) )
4097         pViewFrame->GetFrame().SetMenuBarOn_Impl( sal_False );
4098 
4099     const sal_Int16 nPluginMode = aDocumentLoadArgs.getOrDefault( "PluginMode", sal_Int16( 0 ) );
4100     if ( nPluginMode == 1 )
4101     {
4102         pViewFrame->ForceOuterResize_Impl( sal_False );
4103         pViewFrame->GetBindings().HidePopups( sal_True );
4104 
4105         SfxFrame& rFrame = pViewFrame->GetFrame();
4106         // MBA: layoutmanager of inplace frame starts locked and invisible
4107         rFrame.GetWorkWindow_Impl()->MakeVisible_Impl( sal_False );
4108         rFrame.GetWorkWindow_Impl()->Lock_Impl( sal_True );
4109 
4110         rFrame.GetWindow().SetBorderStyle( WINDOW_BORDER_NOBORDER );
4111         pViewFrame->GetWindow().SetBorderStyle( WINDOW_BORDER_NOBORDER );
4112     }
4113 
4114     // tell the guard we were successful
4115     aViewCreationGuard.releaseAll();
4116 
4117     // outta gere
4118     return pBaseController;
4119 }
4120 
4121 //=============================================================================
4122 // RDF DocumentMetadataAccess
4123 
4124 // ::com::sun::star::rdf::XRepositorySupplier:
4125 uno::Reference< rdf::XRepository > SAL_CALL
getRDFRepository()4126 SfxBaseModel::getRDFRepository()
4127 {
4128     SfxModelGuard aGuard( *this );
4129 
4130     const uno::Reference<rdf::XDocumentMetadataAccess> xDMA(m_pData->GetDMA());
4131     if (!xDMA.is()) {
4132         throw uno::RuntimeException( ::rtl::OUString::createFromAscii(
4133             "model has no document metadata"), *this );
4134     }
4135 
4136     return xDMA->getRDFRepository();
4137 }
4138 
4139 // ::com::sun::star::rdf::XNode:
4140 ::rtl::OUString SAL_CALL
getStringValue()4141 SfxBaseModel::getStringValue()
4142 {
4143     SfxModelGuard aGuard( *this );
4144 
4145     const uno::Reference<rdf::XDocumentMetadataAccess> xDMA(m_pData->GetDMA());
4146     if (!xDMA.is()) {
4147         throw uno::RuntimeException( ::rtl::OUString::createFromAscii(
4148             "model has no document metadata"), *this );
4149     }
4150 
4151     return xDMA->getStringValue();
4152 }
4153 
4154 // ::com::sun::star::rdf::XURI:
4155 ::rtl::OUString SAL_CALL
getNamespace()4156 SfxBaseModel::getNamespace()
4157 {
4158     SfxModelGuard aGuard( *this );
4159 
4160     const uno::Reference<rdf::XDocumentMetadataAccess> xDMA(m_pData->GetDMA());
4161     if (!xDMA.is()) {
4162         throw uno::RuntimeException( ::rtl::OUString::createFromAscii(
4163             "model has no document metadata"), *this );
4164     }
4165 
4166     return xDMA->getNamespace();
4167 }
4168 
4169 ::rtl::OUString SAL_CALL
getLocalName()4170 SfxBaseModel::getLocalName()
4171 {
4172     SfxModelGuard aGuard( *this );
4173 
4174     const uno::Reference<rdf::XDocumentMetadataAccess> xDMA(m_pData->GetDMA());
4175     if (!xDMA.is()) {
4176         throw uno::RuntimeException( ::rtl::OUString::createFromAscii(
4177             "model has no document metadata"), *this );
4178     }
4179 
4180     return xDMA->getLocalName();
4181 }
4182 
4183 // ::com::sun::star::rdf::XDocumentMetadataAccess:
4184 uno::Reference< rdf::XMetadatable > SAL_CALL
getElementByMetadataReference(const::com::sun::star::beans::StringPair & i_rReference)4185 SfxBaseModel::getElementByMetadataReference(
4186     const ::com::sun::star::beans::StringPair & i_rReference)
4187 {
4188     SfxModelGuard aGuard( *this );
4189 
4190     const uno::Reference<rdf::XDocumentMetadataAccess> xDMA(m_pData->GetDMA());
4191     if (!xDMA.is()) {
4192         throw uno::RuntimeException( ::rtl::OUString::createFromAscii(
4193             "model has no document metadata"), *this );
4194     }
4195 
4196     return xDMA->getElementByMetadataReference(i_rReference);
4197 }
4198 
4199 uno::Reference< rdf::XMetadatable > SAL_CALL
getElementByURI(const uno::Reference<rdf::XURI> & i_xURI)4200 SfxBaseModel::getElementByURI(const uno::Reference< rdf::XURI > & i_xURI)
4201 {
4202     SfxModelGuard aGuard( *this );
4203 
4204     const uno::Reference<rdf::XDocumentMetadataAccess> xDMA(m_pData->GetDMA());
4205     if (!xDMA.is()) {
4206         throw uno::RuntimeException( ::rtl::OUString::createFromAscii(
4207             "model has no document metadata"), *this );
4208     }
4209 
4210     return xDMA->getElementByURI(i_xURI);
4211 }
4212 
4213 uno::Sequence< uno::Reference< rdf::XURI > > SAL_CALL
getMetadataGraphsWithType(const uno::Reference<rdf::XURI> & i_xType)4214 SfxBaseModel::getMetadataGraphsWithType(
4215     const uno::Reference<rdf::XURI> & i_xType)
4216 {
4217     SfxModelGuard aGuard( *this );
4218 
4219     const uno::Reference<rdf::XDocumentMetadataAccess> xDMA(m_pData->GetDMA());
4220     if (!xDMA.is()) {
4221         throw uno::RuntimeException( ::rtl::OUString::createFromAscii(
4222             "model has no document metadata"), *this );
4223     }
4224 
4225     return xDMA->getMetadataGraphsWithType(i_xType);
4226 }
4227 
4228 uno::Reference<rdf::XURI> SAL_CALL
addMetadataFile(const::rtl::OUString & i_rFileName,const uno::Sequence<uno::Reference<rdf::XURI>> & i_rTypes)4229 SfxBaseModel::addMetadataFile(const ::rtl::OUString & i_rFileName,
4230     const uno::Sequence < uno::Reference< rdf::XURI > > & i_rTypes)
4231 {
4232     SfxModelGuard aGuard( *this );
4233 
4234     const uno::Reference<rdf::XDocumentMetadataAccess> xDMA(m_pData->GetDMA());
4235     if (!xDMA.is()) {
4236         throw uno::RuntimeException( ::rtl::OUString::createFromAscii(
4237             "model has no document metadata"), *this );
4238     }
4239 
4240     return xDMA->addMetadataFile(i_rFileName, i_rTypes);
4241 }
4242 
4243 uno::Reference<rdf::XURI> SAL_CALL
importMetadataFile(::sal_Int16 i_Format,const uno::Reference<io::XInputStream> & i_xInStream,const::rtl::OUString & i_rFileName,const uno::Reference<rdf::XURI> & i_xBaseURI,const uno::Sequence<uno::Reference<rdf::XURI>> & i_rTypes)4244 SfxBaseModel::importMetadataFile(::sal_Int16 i_Format,
4245     const uno::Reference< io::XInputStream > & i_xInStream,
4246     const ::rtl::OUString & i_rFileName,
4247     const uno::Reference< rdf::XURI > & i_xBaseURI,
4248     const uno::Sequence < uno::Reference< rdf::XURI > > & i_rTypes)
4249 {
4250     SfxModelGuard aGuard( *this );
4251 
4252     const uno::Reference<rdf::XDocumentMetadataAccess> xDMA(m_pData->GetDMA());
4253     if (!xDMA.is()) {
4254         throw uno::RuntimeException( ::rtl::OUString::createFromAscii(
4255             "model has no document metadata"), *this );
4256     }
4257 
4258     return xDMA->importMetadataFile(i_Format,
4259         i_xInStream, i_rFileName, i_xBaseURI, i_rTypes);
4260 }
4261 
4262 void SAL_CALL
removeMetadataFile(const uno::Reference<rdf::XURI> & i_xGraphName)4263 SfxBaseModel::removeMetadataFile(
4264     const uno::Reference< rdf::XURI > & i_xGraphName)
4265 {
4266     SfxModelGuard aGuard( *this );
4267 
4268     const uno::Reference<rdf::XDocumentMetadataAccess> xDMA(m_pData->GetDMA());
4269     if (!xDMA.is()) {
4270         throw uno::RuntimeException( ::rtl::OUString::createFromAscii(
4271             "model has no document metadata"), *this );
4272     }
4273 
4274     return xDMA->removeMetadataFile(i_xGraphName);
4275 }
4276 
4277 void SAL_CALL
addContentOrStylesFile(const::rtl::OUString & i_rFileName)4278 SfxBaseModel::addContentOrStylesFile(const ::rtl::OUString & i_rFileName)
4279 {
4280     SfxModelGuard aGuard( *this );
4281 
4282     const uno::Reference<rdf::XDocumentMetadataAccess> xDMA(m_pData->GetDMA());
4283     if (!xDMA.is()) {
4284         throw uno::RuntimeException( ::rtl::OUString::createFromAscii(
4285             "model has no document metadata"), *this );
4286     }
4287 
4288     return xDMA->addContentOrStylesFile(i_rFileName);
4289 }
4290 
4291 void SAL_CALL
removeContentOrStylesFile(const::rtl::OUString & i_rFileName)4292 SfxBaseModel::removeContentOrStylesFile(const ::rtl::OUString & i_rFileName)
4293 {
4294     SfxModelGuard aGuard( *this );
4295 
4296     const uno::Reference<rdf::XDocumentMetadataAccess> xDMA(m_pData->GetDMA());
4297     if (!xDMA.is()) {
4298         throw uno::RuntimeException( ::rtl::OUString::createFromAscii(
4299             "model has no document metadata"), *this );
4300     }
4301 
4302     return xDMA->removeContentOrStylesFile(i_rFileName);
4303 }
4304 
4305 void SAL_CALL
loadMetadataFromStorage(uno::Reference<embed::XStorage> const & i_xStorage,uno::Reference<rdf::XURI> const & i_xBaseURI,uno::Reference<task::XInteractionHandler> const & i_xHandler)4306 SfxBaseModel::loadMetadataFromStorage(
4307     uno::Reference< embed::XStorage > const & i_xStorage,
4308     uno::Reference<rdf::XURI> const & i_xBaseURI,
4309     uno::Reference<task::XInteractionHandler> const & i_xHandler)
4310 {
4311     SfxModelGuard aGuard( *this );
4312 
4313     const uno::Reference<rdf::XDocumentMetadataAccess> xDMA(
4314         m_pData->CreateDMAUninitialized());
4315     if (!xDMA.is()) {
4316         throw uno::RuntimeException( ::rtl::OUString::createFromAscii(
4317             "model has no document metadata"), *this );
4318     }
4319 
4320     try {
4321         xDMA->loadMetadataFromStorage(i_xStorage, i_xBaseURI, i_xHandler);
4322     } catch (lang::IllegalArgumentException &) {
4323         throw; // not initialized
4324     } catch (uno::Exception &) {
4325         // UGLY: if it's a RuntimeException, we can't be sure DMA is initialized
4326         m_pData->m_xDocumentMetadata = xDMA;
4327         throw;
4328     }
4329     m_pData->m_xDocumentMetadata = xDMA;
4330 
4331 }
4332 
4333 void SAL_CALL
storeMetadataToStorage(uno::Reference<embed::XStorage> const & i_xStorage)4334 SfxBaseModel::storeMetadataToStorage(
4335     uno::Reference< embed::XStorage > const & i_xStorage)
4336 {
4337     SfxModelGuard aGuard( *this );
4338 
4339     const uno::Reference<rdf::XDocumentMetadataAccess> xDMA(m_pData->GetDMA());
4340     if (!xDMA.is()) {
4341         throw uno::RuntimeException( ::rtl::OUString::createFromAscii(
4342             "model has no document metadata"), *this );
4343     }
4344 
4345     return xDMA->storeMetadataToStorage(i_xStorage);
4346 }
4347 
4348 void SAL_CALL
loadMetadataFromMedium(const uno::Sequence<beans::PropertyValue> & i_rMedium)4349 SfxBaseModel::loadMetadataFromMedium(
4350     const uno::Sequence< beans::PropertyValue > & i_rMedium)
4351 {
4352     SfxModelGuard aGuard( *this );
4353 
4354     const uno::Reference<rdf::XDocumentMetadataAccess> xDMA(
4355         m_pData->CreateDMAUninitialized());
4356     if (!xDMA.is()) {
4357         throw uno::RuntimeException( ::rtl::OUString::createFromAscii(
4358             "model has no document metadata"), *this );
4359     }
4360 
4361     try {
4362         xDMA->loadMetadataFromMedium(i_rMedium);
4363     } catch (lang::IllegalArgumentException &) {
4364         throw; // not initialized
4365     } catch (uno::Exception &) {
4366         // UGLY: if it's a RuntimeException, we can't be sure DMA is initialized
4367         m_pData->m_xDocumentMetadata = xDMA;
4368         throw;
4369     }
4370     m_pData->m_xDocumentMetadata = xDMA;
4371 }
4372 
4373 void SAL_CALL
storeMetadataToMedium(const uno::Sequence<beans::PropertyValue> & i_rMedium)4374 SfxBaseModel::storeMetadataToMedium(
4375     const uno::Sequence< beans::PropertyValue > & i_rMedium)
4376 {
4377     SfxModelGuard aGuard( *this );
4378 
4379     const uno::Reference<rdf::XDocumentMetadataAccess> xDMA(m_pData->GetDMA());
4380     if (!xDMA.is()) {
4381         throw uno::RuntimeException( ::rtl::OUString::createFromAscii(
4382             "model has no document metadata"), *this );
4383     }
4384 
4385     return xDMA->storeMetadataToMedium(i_rMedium);
4386 }
4387 
4388 // =====================================================================================================================
4389 // = SfxModelSubComponent
4390 // =====================================================================================================================
4391 
~SfxModelSubComponent()4392 SfxModelSubComponent::~SfxModelSubComponent()
4393 {
4394 }
4395 
disposing()4396 void SfxModelSubComponent::disposing()
4397 {
4398     // nothing to do here
4399 }
4400