# Cppcheck report - LibreOffice: sfx2/source/view/frmload.cxx

      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493
    494
    495
    496
    497
    498
    499
    500
    501
    502
    503
    504
    505
    506
    507
    508
    509
    510
    511
    512
    513
    514
    515
    516
    517
    518
    519
    520
    521
    522
    523
    524
    525
    526
    527
    528
    529
    530
    531
    532
    533
    534
    535
    536
    537
    538
    539
    540
    541
    542
    543
    544
    545
    546
    547
    548
    549
    550
    551
    552
    553
    554
    555
    556
    557
    558
    559
    560
    561
    562
    563
    564
    565
    566
    567
    568
    569
    570
    571
    572
    573
    574
    575
    576
    577
    578
    579
    580
    581
    582
    583
    584
    585
    586
    587
    588
    589
    590
    591
    592
    593
    594
    595
    596
    597
    598
    599
    600
    601
    602
    603
    604
    605
    606
    607
    608
    609
    610
    611
    612
    613
    614
    615
    616
    617
    618
    619
    620
    621
    622
    623
    624
    625
    626
    627
    628
    629
    630
    631
    632
    633
    634
    635
    636
    637
    638
    639
    640
    641
    642
    643
    644
    645
    646
    647
    648
    649
    650
    651
    652
    653
    654
    655
    656
    657
    658
    659
    660
    661
    662
    663
    664
    665
    666
    667
    668
    669
    670
    671
    672
    673
    674
    675
    676
    677
    678
    679
    680
    681
    682
    683
    684
    685
    686
    687
    688
    689
    690
    691
    692
    693
    694
    695
    696
    697
    698
    699
    700
    701
    702
    703
    704
    705
    706
    707
    708
    709
    710
    711
    712
    713
    714
    715
    716
    717
    718
    719
    720
    721
    722
    723
    724
    725
    726
    727
    728
    729
    730
    731
    732
    733
    734
    735
    736
    737
    738
    739
    740
    741
    742
    743
    744
    745
    746
    747
    748
    749
    750
    751
    752
    753
    754
    755
    756
    757
    758
    759
    760
    761
    762
    763
    764
    765
    766
    767
    768
    769
    770
    771
    772
    773
    774
    775
    776
    777
    778
    779
    780
    781
    782
    783
    784
    785
    786
    787
    788
    789
    790
    791
    792
    793
    794
    795
    796

    /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
    /*
     * This file is part of the LibreOffice project.
     *
     * This Source Code Form is subject to the terms of the Mozilla Public
     * License, v. 2.0. If a copy of the MPL was not distributed with this
     * file, You can obtain one at http://mozilla.org/MPL/2.0/.
     *
     * This file incorporates work covered by the following license notice:
     *
     *   Licensed to the Apache Software Foundation (ASF) under one or more
     *   contributor license agreements. See the NOTICE file distributed
     *   with this work for additional information regarding copyright
     *   ownership. The ASF licenses this file to you under the Apache
     *   License, Version 2.0 (the "License"); you may not use this file
     *   except in compliance with the License. You may obtain a copy of
     *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
     */

    #include <sal/macros.h>
    #include "objshimp.hxx"
    #include <sfx2/app.hxx>
    #include <sfx2/dispatch.hxx>
    #include <sfx2/docfac.hxx>
    #include <sfx2/docfile.hxx>
    #include <sfx2/docfilt.hxx>
    #include <sfx2/doctempl.hxx>
    #include <sfx2/fcontnr.hxx>
    #include <sfx2/frame.hxx>
    #include <sfx2/objsh.hxx>
    #include <sfx2/request.hxx>
    #include <sfx2/sfx.hrc>
    #include <sfx2/sfxsids.hrc>
    #include <sfx2/sfxuno.hxx>
    #include <sfx2/viewfrm.hxx>
    #include <sfx2/viewsh.hxx>
    #include <sfx2/viewfac.hxx>

    #include <com/sun/star/container/XContainerQuery.hpp>
    #include <com/sun/star/document/XTypeDetection.hpp>
    #include <com/sun/star/frame/XFrame.hpp>
    #include <com/sun/star/frame/XLoadable.hpp>
    #include <com/sun/star/frame/XModel.hpp>
    #include <com/sun/star/task/XInteractionHandler.hpp>
    #include <com/sun/star/task/XInteractionHandler2.hpp>
    #include <com/sun/star/document/XViewDataSupplier.hpp>
    #include <com/sun/star/container/XIndexAccess.hpp>
    #include <com/sun/star/frame/XSynchronousFrameLoader.hpp>
    #include <com/sun/star/frame/XController2.hpp>
    #include <com/sun/star/frame/XModel2.hpp>
    #include <com/sun/star/lang/XServiceInfo.hpp>
    #include <com/sun/star/lang/XInitialization.hpp>

    #include <comphelper/interaction.hxx>
    #include <comphelper/namedvaluecollection.hxx>
    #include <cppuhelper/exc_hlp.hxx>
    #include <cppuhelper/implbase2.hxx>
    #include <cppuhelper/supportsservice.hxx>
    #include <framework/interaction.hxx>
    #include <rtl/ref.hxx>
    #include <rtl/ustring.h>
    #include <sot/storinfo.hxx>
    #include <svtools/ehdl.hxx>
    #include <svl/eitem.hxx>
    #include <svl/itemset.hxx>
    #include <unotools/moduleoptions.hxx>
    #include <svtools/sfxecode.hxx>
    #include <svl/stritem.hxx>
    #include <toolkit/helper/vclunohelper.hxx>
    #include <tools/diagnose_ex.h>
    #include <ucbhelper/simpleinteractionrequest.hxx>
    #include <osl/mutex.hxx>

    using namespace com::sun::star;
    using ::com::sun::star::beans::PropertyValue;
    using ::com::sun::star::container::XContainerQuery;
    using ::com::sun::star::container::XEnumeration;
    using ::com::sun::star::document::XTypeDetection;
    using ::com::sun::star::frame::XFrame;
    using ::com::sun::star::frame::XLoadable;
    using ::com::sun::star::frame::XModel;
    using ::com::sun::star::lang::XMultiServiceFactory;
    using ::com::sun::star::task::XInteractionHandler;
    using ::com::sun::star::task::XInteractionHandler2;
    using ::com::sun::star::task::XInteractionRequest;
    using ::com::sun::star::task::XStatusIndicator;
    using ::com::sun::star::uno::Any;
    using ::com::sun::star::uno::Exception;
    using ::com::sun::star::uno::Reference;
    using ::com::sun::star::uno::RuntimeException;
    using ::com::sun::star::uno::Sequence;
    using ::com::sun::star::uno::UNO_QUERY;
    using ::com::sun::star::uno::UNO_QUERY_THROW;
    using ::com::sun::star::uno::UNO_SET_THROW;
    using ::com::sun::star::uno::makeAny;
    using ::com::sun::star::util::XCloseable;
    using ::com::sun::star::document::XViewDataSupplier;
    using ::com::sun::star::container::XIndexAccess;
    using ::com::sun::star::frame::XController2;
    using ::com::sun::star::frame::XController;
    using ::com::sun::star::frame::XModel2;

    namespace {

    class SfxFrameLoader_Impl : public ::cppu::WeakImplHelper2< css::frame::XSynchronousFrameLoader, css::lang::XServiceInfo >
    {
        css::uno::Reference < css::uno::XComponentContext >  m_aContext;

    public:
        SfxFrameLoader_Impl( const css::uno::Reference < css::uno::XComponentContext >& _rxContext );

        virtual OUString SAL_CALL getImplementationName()
            throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;

        virtual sal_Bool SAL_CALL supportsService(OUString const & ServiceName)
            throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;

        virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames()
            throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;

        // XSynchronousFrameLoader

        virtual sal_Bool SAL_CALL load( const css::uno::Sequence< css::beans::PropertyValue >& _rArgs, const css::uno::Reference< css::frame::XFrame >& _rxFrame ) throw( css::uno::RuntimeException, std::exception ) SAL_OVERRIDE;
        virtual void SAL_CALL cancel() throw( css::uno::RuntimeException, std::exception ) SAL_OVERRIDE;

    protected:
        virtual                 ~SfxFrameLoader_Impl();

    private:
        const SfxFilter*    impl_getFilterFromServiceName_nothrow(
                                const OUString& i_rServiceName
                            ) const;

        OUString     impl_askForFilter_nothrow(
                                const css::uno::Reference< css::task::XInteractionHandler >& i_rxHandler,
                                const OUString& i_rDocumentURL
                            ) const;

        const SfxFilter*    impl_detectFilterForURL(
                                const OUString& _rURL,
                                const ::comphelper::NamedValueCollection& i_rDescriptor,
                                const SfxFilterMatcher& rMatcher
                            ) const;

        bool            impl_createNewDocWithSlotParam(
                                const sal_uInt16 _nSlotID,
                                const css::uno::Reference< css::frame::XFrame >& i_rxFrame,
                                const bool i_bHidden
                            );

        void                impl_determineFilter(
                                      ::comphelper::NamedValueCollection& io_rDescriptor
                            ) const;

        bool                impl_determineTemplateDocument(
                                ::comphelper::NamedValueCollection& io_rDescriptor
                            ) const;

        sal_uInt16              impl_findSlotParam(
                                const OUString& i_rFactoryURL
                            ) const;

        SfxObjectShellRef   impl_findObjectShell(
                                const css::uno::Reference< css::frame::XModel2 >& i_rxDocument
                            ) const;

        void                impl_handleCaughtError_nothrow(
                                const css::uno::Any& i_rCaughtError,
                                const ::comphelper::NamedValueCollection& i_rDescriptor
                            ) const;

        void                impl_removeLoaderArguments(
                                ::comphelper::NamedValueCollection& io_rDescriptor
                            );

        sal_Int16           impl_determineEffectiveViewId_nothrow(
                                const SfxObjectShell& i_rDocument,
                                const ::comphelper::NamedValueCollection& i_rDescriptor
                            );

        ::comphelper::NamedValueCollection
                            impl_extractViewCreationArgs(
                                      ::comphelper::NamedValueCollection& io_rDescriptor
                            );

        css::uno::Reference< css::frame::XController2 >
                            impl_createDocumentView(
                                const css::uno::Reference< css::frame::XModel2 >& i_rModel,
                                const css::uno::Reference< css::frame::XFrame >& i_rFrame,
                                const ::comphelper::NamedValueCollection& i_rViewFactoryArgs,
                                const OUString& i_rViewName
                            );
    };

    SfxFrameLoader_Impl::SfxFrameLoader_Impl( const Reference< css::uno::XComponentContext >& _rxContext )
        :m_aContext( _rxContext )
    {
    }

    SfxFrameLoader_Impl::~SfxFrameLoader_Impl()
    {
    }

    const SfxFilter* SfxFrameLoader_Impl::impl_detectFilterForURL( const OUString& sURL,
            const ::comphelper::NamedValueCollection& i_rDescriptor, const SfxFilterMatcher& rMatcher ) const
    {
        OUString sFilter;
        try
        {
            if ( sURL.isEmpty() )
                return 0;

            Reference< XTypeDetection > xDetect(
                m_aContext->getServiceManager()->createInstanceWithContext("com.sun.star.document.TypeDetection", m_aContext),
                UNO_QUERY_THROW);

            ::comphelper::NamedValueCollection aNewArgs;
            aNewArgs.put( "URL", sURL );

            if ( i_rDescriptor.has( "InteractionHandler" ) )
                aNewArgs.put( "InteractionHandler", i_rDescriptor.get( "InteractionHandler" ) );
            if ( i_rDescriptor.has( "StatusIndicator" ) )
                aNewArgs.put( "StatusIndicator", i_rDescriptor.get( "StatusIndicator" ) );

            Sequence< PropertyValue > aQueryArgs( aNewArgs.getPropertyValues() );
            OUString sType = xDetect->queryTypeByDescriptor( aQueryArgs, sal_True );
            if ( !sType.isEmpty() )
            {
                const SfxFilter* pFilter = rMatcher.GetFilter4EA( sType );
                if ( pFilter )
                    sFilter = pFilter->GetName();
            }
        }
        catch ( const RuntimeException& )
        {
            throw;
        }
        catch( const Exception& )
        {
            DBG_UNHANDLED_EXCEPTION();
            sFilter.clear();
        }

        const SfxFilter* pFilter = 0;
        if (!sFilter.isEmpty())
            pFilter = rMatcher.GetFilter4FilterName(sFilter);
        return pFilter;
    }

    const SfxFilter* SfxFrameLoader_Impl::impl_getFilterFromServiceName_nothrow( const OUString& i_rServiceName ) const
    {
        try
        {
            ::comphelper::NamedValueCollection aQuery;
            aQuery.put( "DocumentService", i_rServiceName );

            const Reference< XContainerQuery > xQuery(
                m_aContext->getServiceManager()->createInstanceWithContext("com.sun.star.document.FilterFactory", m_aContext),
                UNO_QUERY_THROW );

            const SfxFilterMatcher& rMatcher = SfxGetpApp()->GetFilterMatcher();
            const SfxFilterFlags nMust = SFX_FILTER_IMPORT;
            const SfxFilterFlags nDont = SFX_FILTER_NOTINSTALLED;

            Reference < XEnumeration > xEnum( xQuery->createSubSetEnumerationByProperties(
                aQuery.getNamedValues() ), UNO_SET_THROW );
            while ( xEnum->hasMoreElements() )
            {
                ::comphelper::NamedValueCollection aType( xEnum->nextElement() );
                OUString sFilterName = aType.getOrDefault( "Name", OUString() );
                if ( sFilterName.isEmpty() )
                    continue;

                const SfxFilter* pFilter = rMatcher.GetFilter4FilterName( sFilterName );
                if ( !pFilter )
                    continue;

                SfxFilterFlags nFlags = pFilter->GetFilterFlags();
                if  (   ( ( nFlags & nMust ) == nMust )
                    &&  ( ( nFlags & nDont ) == 0 )
                    )
                {
                    return pFilter;
                }
            }
        }
        catch( const Exception& )
        {
            DBG_UNHANDLED_EXCEPTION();
        }
        return NULL;
    }

    OUString SfxFrameLoader_Impl::impl_askForFilter_nothrow( const Reference< XInteractionHandler >& i_rxHandler,
                                                                     const OUString& i_rDocumentURL ) const
    {
        ENSURE_OR_THROW( i_rxHandler.is(), "invalid interaction handler" );

        OUString sFilterName;
        try
        {
            ::framework::RequestFilterSelect aRequest( i_rDocumentURL );
            i_rxHandler->handle( aRequest.GetRequest() );
            if( !aRequest.isAbort() )
                sFilterName = aRequest.getFilter();
        }
        catch( const Exception& )
        {
            DBG_UNHANDLED_EXCEPTION();
        }

        return sFilterName;
    }

    namespace
    {
        bool lcl_getDispatchResult( const SfxPoolItem* _pResult )
        {
            if ( !_pResult )
                return false;

            // default must be set to true, because some return values
            // can't be checked, but nonetheless indicate "success"!
            bool bSuccess = true;

            // On the other side some special slots return a boolean state,
            // which can be set to FALSE.
            const SfxBoolItem *pItem = PTR_CAST( SfxBoolItem, _pResult );
            if ( pItem )
                bSuccess = pItem->GetValue();

            return bSuccess;
        }
    }

    bool SfxFrameLoader_Impl::impl_createNewDocWithSlotParam( const sal_uInt16 _nSlotID, const Reference< XFrame >& i_rxFrame,
                                                                  const bool i_bHidden )
    {
        SfxRequest aRequest( _nSlotID, SfxCallMode::SYNCHRON, SfxGetpApp()->GetPool() );
        aRequest.AppendItem( SfxUnoFrameItem( SID_FILLFRAME, i_rxFrame ) );
        if ( i_bHidden )
            aRequest.AppendItem( SfxBoolItem( SID_HIDDEN, true ) );
        return lcl_getDispatchResult( SfxGetpApp()->ExecuteSlot( aRequest ) );
    }

    void SfxFrameLoader_Impl::impl_determineFilter( ::comphelper::NamedValueCollection& io_rDescriptor ) const
    {
        const OUString     sURL         = io_rDescriptor.getOrDefault( "URL",                OUString() );
        const OUString     sTypeName    = io_rDescriptor.getOrDefault( "TypeName",           OUString() );
        const OUString     sFilterName  = io_rDescriptor.getOrDefault( "FilterName",         OUString() );
        const OUString     sServiceName = io_rDescriptor.getOrDefault( "DocumentService",    OUString() );
        const Reference< XInteractionHandler >
                                  xInteraction = io_rDescriptor.getOrDefault( "InteractionHandler", Reference< XInteractionHandler >() );

        const SfxFilterMatcher& rMatcher = SfxGetpApp()->GetFilterMatcher();
        const SfxFilter* pFilter = NULL;

        // get filter by its name directly ...
        if ( !sFilterName.isEmpty() )
            pFilter = rMatcher.GetFilter4FilterName( sFilterName );

        // or search the preferred filter for the detected type ...
        if ( !pFilter && !sTypeName.isEmpty() )
            pFilter = rMatcher.GetFilter4EA( sTypeName );

        // or use given document service for detection, too
        if ( !pFilter && !sServiceName.isEmpty() )
            pFilter = impl_getFilterFromServiceName_nothrow( sServiceName );

        // or use interaction to ask user for right filter.
        if ( !pFilter && xInteraction.is() && !sURL.isEmpty() )
        {
            OUString sSelectedFilter = impl_askForFilter_nothrow( xInteraction, sURL );
            if ( !sSelectedFilter.isEmpty() )
                pFilter = rMatcher.GetFilter4FilterName( sSelectedFilter );
        }

        if ( pFilter )
        {
            io_rDescriptor.put( "FilterName", OUString( pFilter->GetFilterName() ) );

            // If detected filter indicates using of an own template format
            // add property "AsTemplate" to descriptor. But suppress this step
            // if such property already exists.
            if ( pFilter->IsOwnTemplateFormat() && !io_rDescriptor.has( "AsTemplate" ) )
                io_rDescriptor.put( "AsTemplate", true );

            // The DocumentService property will finally be used to determine the document type to create, so
            // override it with the service name as indicated by the found filter.
            io_rDescriptor.put( "DocumentService", OUString( pFilter->GetServiceName() ) );
        }
    }

    SfxObjectShellRef SfxFrameLoader_Impl::impl_findObjectShell( const Reference< XModel2 >& i_rxDocument ) const
    {
        for ( SfxObjectShell* pDoc = SfxObjectShell::GetFirst( NULL, false ); pDoc; pDoc = SfxObjectShell::GetNext( *pDoc, NULL, false ) )
        {
            if ( i_rxDocument == pDoc->GetModel() )
            {
                return pDoc;
            }
        }

        SAL_WARN( "sfx.view", "SfxFrameLoader_Impl::impl_findObjectShell: model is not based on SfxObjectShell - wrong frame loader usage!" );
        return NULL;
    }

    bool SfxFrameLoader_Impl::impl_determineTemplateDocument( ::comphelper::NamedValueCollection& io_rDescriptor ) const
    {
        try
        {
            const OUString sTemplateRegioName = io_rDescriptor.getOrDefault( "TemplateRegionName", OUString() );
            const OUString sTemplateName      = io_rDescriptor.getOrDefault( "TemplateName",       OUString() );
            const OUString sServiceName       = io_rDescriptor.getOrDefault( "DocumentService",    OUString() );
            const OUString sURL               = io_rDescriptor.getOrDefault( "URL",                OUString() );

            // determine the full URL of the template to use, if any
            OUString sTemplateURL;
            if ( !sTemplateRegioName.isEmpty() && !sTemplateName.isEmpty() )
            {
                SfxDocumentTemplates aTmpFac;
                aTmpFac.GetFull( sTemplateRegioName, sTemplateName, sTemplateURL );
            }
            else
            {
                if ( !sServiceName.isEmpty() )
                    sTemplateURL = SfxObjectFactory::GetStandardTemplate( sServiceName );
                else
                    sTemplateURL = SfxObjectFactory::GetStandardTemplate( SfxObjectShell::GetServiceNameFromFactory( sURL ) );
            }

            if ( !sTemplateURL.isEmpty() )
            {
                // detect the filter for the template. Might still be NULL (if the template is broken, or does not
                // exist, or some such), but this is handled by our caller the same way as if no template/URL was present.
                const SfxFilter* pTemplateFilter = impl_detectFilterForURL( sTemplateURL, io_rDescriptor, SfxGetpApp()->GetFilterMatcher() );
                if ( pTemplateFilter )
                {
                    // load the template document, but, well, "as template"
                    io_rDescriptor.put( "FilterName", OUString( pTemplateFilter->GetName() ) );
                    io_rDescriptor.put( "FileName", OUString( sTemplateURL ) );
                    io_rDescriptor.put( "AsTemplate", sal_True );

                    // #i21583#
                    // the DocumentService property will finally be used to create the document. Thus, override any possibly
                    // present value with the document service of the template.
                    io_rDescriptor.put( "DocumentService", OUString( pTemplateFilter->GetServiceName() ) );
                    return true;
                }
            }
        }
        catch (...)
        {
        }
        return false;
    }

    sal_uInt16 SfxFrameLoader_Impl::impl_findSlotParam( const OUString& i_rFactoryURL ) const
    {
        OUString sSlotParam;
        const sal_Int32 nParamPos = i_rFactoryURL.indexOf( '?' );
        if ( nParamPos >= 0 )
        {
            // currently only the "slot" parameter is supported
            const sal_Int32 nSlotPos = i_rFactoryURL.indexOf( "slot=", nParamPos );
            if ( nSlotPos > 0 )
                sSlotParam = i_rFactoryURL.copy( nSlotPos + 5 );
        }

        if ( !sSlotParam.isEmpty() )
            return sal_uInt16( sSlotParam.toInt32() );

        return 0;
    }

    void SfxFrameLoader_Impl::impl_handleCaughtError_nothrow( const Any& i_rCaughtError, const ::comphelper::NamedValueCollection& i_rDescriptor ) const
    {
        try
        {
            const Reference< XInteractionHandler > xInteraction =
                i_rDescriptor.getOrDefault( "InteractionHandler", Reference< XInteractionHandler >() );
            if ( !xInteraction.is() )
                return;
            ::rtl::Reference< ::comphelper::OInteractionRequest > pRequest( new ::comphelper::OInteractionRequest( i_rCaughtError ) );
            ::rtl::Reference< ::comphelper::OInteractionApprove > pApprove( new ::comphelper::OInteractionApprove );
            pRequest->addContinuation( pApprove.get() );

            const Reference< XInteractionHandler2 > xHandler( xInteraction, UNO_QUERY );
        #if OSL_DEBUG_LEVEL > 0
            const bool bHandled =
        #endif
            xHandler.is() && xHandler->handleInteractionRequest( pRequest.get() );

        #if OSL_DEBUG_LEVEL > 0
            if ( !bHandled )
                // the interaction handler couldn't deal with this error
                // => report it as assertion, at least (done in the DBG_UNHANDLED_EXCEPTION below)
                ::cppu::throwException( i_rCaughtError );
        #endif
        }
        catch( const Exception& )
        {
            DBG_UNHANDLED_EXCEPTION();
        }
    }

    void SfxFrameLoader_Impl::impl_removeLoaderArguments( ::comphelper::NamedValueCollection& io_rDescriptor )
    {
        // remove the arguments which are for the loader only, and not for a call to attachResource
        io_rDescriptor.remove( "StatusIndicator" );
        io_rDescriptor.remove( "Model" );
    }

    ::comphelper::NamedValueCollection SfxFrameLoader_Impl::impl_extractViewCreationArgs( ::comphelper::NamedValueCollection& io_rDescriptor )
    {
        const sal_Char* pKnownViewArgs[] = {
            "JumpMark"
        };

        ::comphelper::NamedValueCollection aViewArgs;
        for ( size_t i=0; i < sizeof( pKnownViewArgs ) / sizeof( pKnownViewArgs[0] ); ++i )
        {
            if ( io_rDescriptor.has( pKnownViewArgs[i] ) )
            {
                aViewArgs.put( pKnownViewArgs[i], io_rDescriptor.get( pKnownViewArgs[i] ) );
                io_rDescriptor.remove( pKnownViewArgs[i] );
            }
        }
        return aViewArgs;
    }

    sal_Int16 SfxFrameLoader_Impl::impl_determineEffectiveViewId_nothrow( const SfxObjectShell& i_rDocument, const ::comphelper::NamedValueCollection& i_rDescriptor )
    {
        sal_Int16 nViewId = i_rDescriptor.getOrDefault( "ViewId", sal_Int16( 0 ) );
        try
        {
            if ( nViewId == 0 ) do
            {
                Reference< XViewDataSupplier > xViewDataSupplier( i_rDocument.GetModel(), UNO_QUERY );
                Reference< XIndexAccess > xViewData;
                if ( xViewDataSupplier.is() )
                    xViewData.set( xViewDataSupplier->getViewData() );

                if ( !xViewData.is() || ( xViewData->getCount() == 0 ) )
                    // no view data stored together with the model
                    break;

                // obtain the ViewID from the view data
                Sequence< PropertyValue > aViewData;
                if ( !( xViewData->getByIndex( 0 ) >>= aViewData ) )
                    break;

                ::comphelper::NamedValueCollection aNamedViewData( aViewData );
                OUString sViewId = aNamedViewData.getOrDefault( "ViewId", OUString() );
                if ( sViewId.isEmpty() )
                    break;

                // somewhat weird convention here ... in the view data, the ViewId is a string, effectively describing
                // a view name. In the document load descriptor, the ViewId is in fact the numeric ID.

                SfxViewFactory* pViewFactory = i_rDocument.GetFactory().GetViewFactoryByViewName( sViewId );
                if ( pViewFactory )
                    nViewId = sal_Int16( pViewFactory->GetOrdinal() );
            }
            while ( false );
        }
        catch( const Exception& )
        {
            DBG_UNHANDLED_EXCEPTION();
        }

        if ( nViewId == 0 )
            nViewId = i_rDocument.GetFactory().GetViewFactory( 0 ).GetOrdinal();
        return nViewId;
    }

    Reference< XController2 > SfxFrameLoader_Impl::impl_createDocumentView( const Reference< XModel2 >& i_rModel,
            const Reference< XFrame >& i_rFrame, const ::comphelper::NamedValueCollection& i_rViewFactoryArgs,
            const OUString& i_rViewName )
    {
        // let the model create a new controller
        const Reference< XController2 > xController( i_rModel->createViewController(
            i_rViewName,
            i_rViewFactoryArgs.getPropertyValues(),
            i_rFrame
        ), UNO_SET_THROW );

        // introduce model/view/controller to each other
        xController->attachModel( i_rModel.get() );
        i_rModel->connectController( xController.get() );
        i_rFrame->setComponent( xController->getComponentWindow(), xController.get() );
        xController->attachFrame( i_rFrame );
        i_rModel->setCurrentController( xController.get() );

        return xController;
    }

    sal_Bool SAL_CALL SfxFrameLoader_Impl::load( const Sequence< PropertyValue >& rArgs,
                                                 const Reference< XFrame >& _rTargetFrame )
        throw( RuntimeException, std::exception )
    {
        ENSURE_OR_THROW( _rTargetFrame.is(), "illegal NULL frame" );

        SolarMutexGuard aGuard;

        SAL_INFO( "sfx.view", "SfxFrameLoader::load" );

        ::comphelper::NamedValueCollection aDescriptor( rArgs );

        // ensure the descriptor contains a referrer
        if ( !aDescriptor.has( "Referer" ) )
            aDescriptor.put( "Referer", OUString() );

        // did the caller already pass a model?
        Reference< XModel2 > xModel = aDescriptor.getOrDefault( "Model", Reference< XModel2 >() );
        const bool bExternalModel = xModel.is();

        // check for factory URLs to create a new doc, instead of loading one
        const OUString sURL = aDescriptor.getOrDefault( "URL", OUString() );
        const bool bIsFactoryURL = sURL.startsWith( "private:factory/" );
        bool bInitNewModel = bIsFactoryURL;
        if ( bIsFactoryURL && !bExternalModel )
        {
            const OUString sFactory = sURL.copy( sizeof( "private:factory/" ) -1 );
            // special handling for some weird factory URLs a la private:factory/swriter?slot=21053
            const sal_uInt16 nSlotParam = impl_findSlotParam( sFactory );
            if ( nSlotParam != 0 )
            {
                return impl_createNewDocWithSlotParam( nSlotParam, _rTargetFrame, aDescriptor.getOrDefault( "Hidden", false ) );
            }

            const bool bDescribesValidTemplate = impl_determineTemplateDocument( aDescriptor );
            if ( bDescribesValidTemplate )
            {
                // if the media descriptor allowed us to determine a template document to create the new document
                // from, then do not init a new document model from scratch (below), but instead load the
                // template document
                bInitNewModel = false;
            }
            else
            {
                const OUString sServiceName = SfxObjectShell::GetServiceNameFromFactory( sFactory );
                aDescriptor.put( "DocumentService", sServiceName );
            }
        }
        else
        {
            // compatibility
            aDescriptor.put( "FileName", aDescriptor.get( "URL" ) );
        }

        bool bLoadSuccess = false;
        try
        {
            // extract view releant arguments from the loader args
            ::comphelper::NamedValueCollection aViewCreationArgs( impl_extractViewCreationArgs( aDescriptor ) );

            // no model passed from outside? => create one from scratch
            if ( !xModel.is() )
            {
                bool bInternalFilter = aDescriptor.getOrDefault<OUString>("FilterProvider", OUString()).isEmpty();

                if (bInternalFilter && !bInitNewModel)
                {
                    // Ensure that the current SfxFilter instance is loaded before
                    // going further.  We don't need to do this for external
                    // filter providers.
                    impl_determineFilter(aDescriptor);
                }

                // create the new doc
                const OUString sServiceName = aDescriptor.getOrDefault( "DocumentService", OUString() );
                xModel.set( m_aContext->getServiceManager()->createInstanceWithContext(sServiceName, m_aContext), UNO_QUERY_THROW );

                // load resp. init it
                const Reference< XLoadable > xLoadable( xModel, UNO_QUERY_THROW );
                if ( bInitNewModel )
                {
                    xLoadable->initNew();

                    impl_removeLoaderArguments( aDescriptor );
                    xModel->attachResource( OUString(), aDescriptor.getPropertyValues() );
                }
                else
                {
                    xLoadable->load( aDescriptor.getPropertyValues() );
                }
            }
            else
            {
                // tell the doc its (current) load args.
                impl_removeLoaderArguments( aDescriptor );
                xModel->attachResource( xModel->getURL(), aDescriptor.getPropertyValues() );
            }

            // get the SfxObjectShell (still needed at the moment)
            // SfxObjectShellRef is used here ( instead of ...Lock ) since the model is closed below if necessary
            // SfxObjectShellLock would be even dangerous here, since the lifetime control should be done outside in case of success
            const SfxObjectShellRef xDoc = impl_findObjectShell( xModel );
            ENSURE_OR_THROW( xDoc.Is(), "no SfxObjectShell for the given model" );

            // ensure the ID of the to-be-created view is in the descriptor, if possible
            const sal_Int16 nViewId = impl_determineEffectiveViewId_nothrow( *xDoc, aDescriptor );
            const sal_Int16 nViewNo = xDoc->GetFactory().GetViewNo_Impl( nViewId, 0 );
            const OUString sViewName( xDoc->GetFactory().GetViewFactory( nViewNo ).GetAPIViewName() );

            // plug the document into the frame
            Reference<XController2> xController =
                impl_createDocumentView( xModel, _rTargetFrame, aViewCreationArgs, sViewName );

            Reference<lang::XInitialization> xInit(xController, UNO_QUERY);
            if (xInit.is())
            {
                uno::Sequence<uno::Any> aArgs; // empty for now.
                xInit->initialize(aArgs);
            }

            bLoadSuccess = true;
        }
        catch ( Exception& )
        {
            const Any aError( ::cppu::getCaughtException() );
            if ( !aDescriptor.getOrDefault( "Silent", sal_False ) )
                impl_handleCaughtError_nothrow( aError, aDescriptor );
        }

        // if loading was not successful, close the document
        if ( !bLoadSuccess && !bExternalModel )
        {
            try
            {
                const Reference< XCloseable > xCloseable( xModel, UNO_QUERY_THROW );
                xCloseable->close( sal_True );
            }
            catch ( Exception& )
            {
                DBG_UNHANDLED_EXCEPTION();
            }
        }

        return bLoadSuccess;
    }

    void SfxFrameLoader_Impl::cancel() throw( RuntimeException, std::exception )
    {
    }

    /* XServiceInfo */
    OUString SAL_CALL SfxFrameLoader_Impl::getImplementationName() throw( RuntimeException, std::exception )
    {
        return OUString("com.sun.star.comp.office.FrameLoader");
    }
                                                                                                                                    \
    /* XServiceInfo */
    sal_Bool SAL_CALL SfxFrameLoader_Impl::supportsService( const OUString& sServiceName ) throw( RuntimeException, std::exception )
    {
        return cppu::supportsService(this, sServiceName);
    }

    /* XServiceInfo */
    Sequence< OUString > SAL_CALL SfxFrameLoader_Impl::getSupportedServiceNames() throw( RuntimeException, std::exception )
    {
        Sequence< OUString > seqServiceNames( 2 );
        seqServiceNames.getArray() [0] = "com.sun.star.frame.SynchronousFrameLoader";
        seqServiceNames.getArray() [1] = "com.sun.star.frame.OfficeFrameLoader";
        return seqServiceNames ;
    }

    }

    extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
    com_sun_star_comp_office_FrameLoader_get_implementation(<--- The function 'com_sun_star_comp_office_FrameLoader_get_implementation' is never used.
        css::uno::XComponentContext *context,
        css::uno::Sequence<css::uno::Any> const &)
    {
        return cppu::acquire(new SfxFrameLoader_Impl(context));
    }

    /* vim:set shiftwidth=4 softtabstop=4 expandtab: */