# Cppcheck report - LibreOffice: framework/source/services/modulemanager.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

    /* -*- 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/config.h>

    #include <com/sun/star/frame/XFrame.hpp>
    #include <com/sun/star/frame/XController.hpp>
    #include <com/sun/star/frame/XModel.hpp>
    #include <com/sun/star/frame/XModule.hpp>
    #include <com/sun/star/lang/XServiceInfo.hpp>
    #include <com/sun/star/frame/XModuleManager2.hpp>
    #include <com/sun/star/container/XNameReplace.hpp>
    #include <com/sun/star/container/XContainerQuery.hpp>
    #include <com/sun/star/uno/XComponentContext.hpp>

    #include <cppuhelper/implbase3.hxx>
    #include <cppuhelper/supportsservice.hxx>
    #include <comphelper/configurationhelper.hxx>
    #include <comphelper/sequenceashashmap.hxx>
    #include <comphelper/sequenceasvector.hxx>
    #include <comphelper/enumhelper.hxx>

    #include <boost/noncopyable.hpp>

    namespace {

    class ModuleManager:
        public cppu::WeakImplHelper3<
            css::lang::XServiceInfo,
            css::frame::XModuleManager2,
            css::container::XContainerQuery >,
        private boost::noncopyable
    {
    private:

        /** the global uno service manager.
            Must be used to create own needed services.
         */
        css::uno::Reference< css::uno::XComponentContext > m_xContext;

        /** points to the underlying configuration.
            This ModuleManager does not cache - it calls directly the
            configuration API!
          */
        css::uno::Reference< css::container::XNameAccess > m_xCFG;

    public:

        ModuleManager(const css::uno::Reference< css::uno::XComponentContext >& xContext);

        virtual ~ModuleManager();

        // XServiceInfo
        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;

        // XModuleManager
        virtual OUString SAL_CALL identify(const css::uno::Reference< css::uno::XInterface >& xModule)
            throw(css::lang::IllegalArgumentException,
                  css::frame::UnknownModuleException,
                  css::uno::RuntimeException, std::exception         ) SAL_OVERRIDE;

        // XNameReplace
        virtual void SAL_CALL replaceByName(const OUString& sName ,
                                            const css::uno::Any&   aValue)
            throw (css::lang::IllegalArgumentException   ,
                   css::container::NoSuchElementException,
                   css::lang::WrappedTargetException     ,
                   css::uno::RuntimeException, std::exception            ) SAL_OVERRIDE;

        // XNameAccess
        virtual css::uno::Any SAL_CALL getByName(const OUString& sName)
            throw(css::container::NoSuchElementException,
                  css::lang::WrappedTargetException     ,
                  css::uno::RuntimeException, std::exception            ) SAL_OVERRIDE;

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

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

        // XElementAccess
        virtual css::uno::Type SAL_CALL getElementType()
            throw(css::uno::RuntimeException, std::exception) SAL_OVERRIDE;

        virtual sal_Bool SAL_CALL hasElements()
            throw(css::uno::RuntimeException, std::exception) SAL_OVERRIDE;

        // XContainerQuery
        virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL createSubSetEnumerationByQuery(const OUString& sQuery)
            throw(css::uno::RuntimeException, std::exception) SAL_OVERRIDE;

        virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL createSubSetEnumerationByProperties(const css::uno::Sequence< css::beans::NamedValue >& lProperties)
            throw(css::uno::RuntimeException, std::exception) SAL_OVERRIDE;

    private:

        /** @short  makes the real identification of the module.

            @descr  It checks for the optional but preferred interface
                    XModule first. If this module does not exists at the
                    given component it tries to use XServiceInfo instead.

                    Note: This method try to locate a suitable module name.
                    Nothing else. Selecting the right component and throwing suitable
                    exceptions must be done outside.

            @see    identify()

            @param  xComponent
                    the module for identification.

            @return The identifier of the given module.
                    Can be empty if given component is not a real module !

            @threadsafe
         */
        OUString implts_identify(const css::uno::Reference< css::uno::XInterface >& xComponent);
    };

    ModuleManager::ModuleManager(const css::uno::Reference< css::uno::XComponentContext >& xContext)
        : m_xContext(xContext)
    {
        m_xCFG.set( comphelper::ConfigurationHelper::openConfig(
                    m_xContext, "/org.openoffice.Setup/Office/Factories",
                    comphelper::ConfigurationHelper::E_READONLY),
                css::uno::UNO_QUERY_THROW );
    }

    ModuleManager::~ModuleManager()
    {
    }

    OUString ModuleManager::getImplementationName()
        throw (css::uno::RuntimeException, std::exception)
    {
        return OUString("com.sun.star.comp.framework.ModuleManager");
    }

    sal_Bool ModuleManager::supportsService(OUString const & ServiceName)
        throw (css::uno::RuntimeException, std::exception)
    {
        return cppu::supportsService(this, ServiceName);
    }

    css::uno::Sequence< OUString > ModuleManager::getSupportedServiceNames()
        throw (css::uno::RuntimeException, std::exception)
    {
        css::uno::Sequence< OUString > s(1);
        s[0] = "com.sun.star.frame.ModuleManager";
        return s;
    }

    OUString SAL_CALL ModuleManager::identify(const css::uno::Reference< css::uno::XInterface >& xModule)
        throw(css::lang::IllegalArgumentException,
              css::frame::UnknownModuleException,
              css::uno::RuntimeException, std::exception         )
    {
        // valid parameter?
        css::uno::Reference< css::frame::XFrame >      xFrame     (xModule, css::uno::UNO_QUERY);
        css::uno::Reference< css::awt::XWindow >       xWindow    (xModule, css::uno::UNO_QUERY);
        css::uno::Reference< css::frame::XController > xController(xModule, css::uno::UNO_QUERY);
        css::uno::Reference< css::frame::XModel >      xModel     (xModule, css::uno::UNO_QUERY);

        if (
            (!xFrame.is()     ) &&
            (!xWindow.is()    ) &&
            (!xController.is()) &&
            (!xModel.is()     )
           )
        {
            throw css::lang::IllegalArgumentException(
                    OUString("Given module is not a frame nor a window, controller or model."),
                    static_cast< ::cppu::OWeakObject* >(this),
                    1);
        }

        if (xFrame.is())
        {
            xController = xFrame->getController();
            xWindow     = xFrame->getComponentWindow();
        }
        if (xController.is())
            xModel = xController->getModel();

        // modules are implemented by the deepest component in hierarchy ...
        // Means: model -> controller -> window
        // No fallbacks to higher components are allowed !
        // Note : A frame provides access to module components only ... but it's not a module by himself.

        OUString sModule;
        if (xModel.is())
            sModule = implts_identify(xModel);
        else if (xController.is())
            sModule = implts_identify(xController);
        else if (xWindow.is())
            sModule = implts_identify(xWindow);

        if (sModule.isEmpty())
            throw css::frame::UnknownModuleException(
                    OUString("Can not find suitable module for the given component."),
                    static_cast< ::cppu::OWeakObject* >(this));

        return sModule;
    }

    void SAL_CALL ModuleManager::replaceByName(const OUString& sName ,
                                               const css::uno::Any&   aValue)
        throw (css::lang::IllegalArgumentException   ,
               css::container::NoSuchElementException,
               css::lang::WrappedTargetException     ,
               css::uno::RuntimeException, std::exception            )
    {
        ::comphelper::SequenceAsHashMap lProps(aValue);
        if (lProps.empty() )
        {
            throw css::lang::IllegalArgumentException(
                    OUString("No properties given to replace part of module."),
                    static_cast< cppu::OWeakObject * >(this),
                    2);
        }

        // get access to the element
        // Note: Dont use impl_getConfig() method here. Because it creates a readonly access only, further
        // it cache it as a member of this module manager instance. If we change some props there ... but dont
        // flush changes (because an error occurred) we will read them later. If we use a different config access
        // we can close it without a flush ... and our read data wont be affected .-)
        css::uno::Reference< css::uno::XInterface >         xCfg      = ::comphelper::ConfigurationHelper::openConfig(
                                                                            m_xContext,
                                                                            "/org.openoffice.Setup/Office/Factories",
                                                                            ::comphelper::ConfigurationHelper::E_STANDARD);
        css::uno::Reference< css::container::XNameAccess >  xModules (xCfg, css::uno::UNO_QUERY_THROW);
        css::uno::Reference< css::container::XNameReplace > xModule  ;

        xModules->getByName(sName) >>= xModule;
        if (!xModule.is())
        {
            throw css::uno::RuntimeException(
                    OUString("Was not able to get write access to the requested module entry inside configuration."),
                    static_cast< cppu::OWeakObject * >(this));
        }

        ::comphelper::SequenceAsHashMap::const_iterator pProp;
        for (  pProp  = lProps.begin();
               pProp != lProps.end()  ;
             ++pProp                  )
        {
            const OUString& sPropName  = pProp->first;
            const css::uno::Any&   aPropValue = pProp->second;

            // let "NoSuchElementException" out ! We support the same API ...
            // and without a flush() at the end all changed data before will be ignored !
            xModule->replaceByName(sPropName, aPropValue);
        }

        ::comphelper::ConfigurationHelper::flush(xCfg);
    }

    css::uno::Any SAL_CALL ModuleManager::getByName(const OUString& sName)
        throw(css::container::NoSuchElementException,
              css::lang::WrappedTargetException     ,
              css::uno::RuntimeException, std::exception            )
    {
        // get access to the element
        css::uno::Reference< css::container::XNameAccess > xModule;
        m_xCFG->getByName(sName) >>= xModule;
        if (!xModule.is())
        {
            throw css::uno::RuntimeException(
                    OUString("Was not able to get write access to the requested module entry inside configuration."),
                    static_cast< cppu::OWeakObject * >(this));
        }

        // convert it to seq< PropertyValue >
        const css::uno::Sequence< OUString > lPropNames = xModule->getElementNames();
        comphelper::SequenceAsHashMap lProps;

        lProps[OUString("ooSetupFactoryModuleIdentifier")] <<= sName;
        for (sal_Int32 i = 0; i < lPropNames.getLength(); ++i)
        {
            const OUString& sPropName = lPropNames[i];
            lProps[sPropName] = xModule->getByName(sPropName);
        }

        return css::uno::makeAny(lProps.getAsConstPropertyValueList());
    }

    css::uno::Sequence< OUString > SAL_CALL ModuleManager::getElementNames()
        throw(css::uno::RuntimeException, std::exception)
    {
        return m_xCFG->getElementNames();
    }

    sal_Bool SAL_CALL ModuleManager::hasByName(const OUString& sName)
        throw(css::uno::RuntimeException, std::exception)
    {
        return m_xCFG->hasByName(sName);
    }

    css::uno::Type SAL_CALL ModuleManager::getElementType()
        throw(css::uno::RuntimeException, std::exception)
    {
        return ::getCppuType((const css::uno::Sequence< css::beans::PropertyValue >*)0);
    }

    sal_Bool SAL_CALL ModuleManager::hasElements()
        throw(css::uno::RuntimeException, std::exception)
    {
        return m_xCFG->hasElements();
    }

    css::uno::Reference< css::container::XEnumeration > SAL_CALL ModuleManager::createSubSetEnumerationByQuery(const OUString&)
        throw(css::uno::RuntimeException, std::exception)
    {
        return css::uno::Reference< css::container::XEnumeration >();
    }

    css::uno::Reference< css::container::XEnumeration > SAL_CALL ModuleManager::createSubSetEnumerationByProperties(const css::uno::Sequence< css::beans::NamedValue >& lProperties)
        throw(css::uno::RuntimeException, std::exception)
    {
        ::comphelper::SequenceAsHashMap lSearchProps(lProperties);
        const css::uno::Sequence< OUString > lModules = getElementNames();
        ::comphelper::SequenceAsVector< css::uno::Any > lResult;

        for (sal_Int32 i = 0; i < lModules.getLength(); ++i)
        {
            try
            {
                ::comphelper::SequenceAsHashMap lModuleProps = getByName(lModules[i]);
                if (lModuleProps.match(lSearchProps))
                    lResult.push_back(css::uno::makeAny(lModuleProps.getAsConstPropertyValueList()));
            }
            catch(const css::uno::Exception&)
            {
            }
        }

        ::comphelper::OAnyEnumeration*                      pEnum = new ::comphelper::OAnyEnumeration(lResult.getAsConstList());
        css::uno::Reference< css::container::XEnumeration > xEnum(static_cast< css::container::XEnumeration* >(pEnum), css::uno::UNO_QUERY_THROW);
        return xEnum;
    }

    OUString ModuleManager::implts_identify(const css::uno::Reference< css::uno::XInterface >& xComponent)
    {
        // Search for an optional (!) interface XModule first.
        // Its used to overrule an existing service name. Used e.g. by our database form designer
        // which uses a writer module internally.
        css::uno::Reference< css::frame::XModule > xModule(xComponent, css::uno::UNO_QUERY);
        if (xModule.is())
            return xModule->getIdentifier();

        // detect modules in a generic way ...
        // comparing service names with configured entries ...
        css::uno::Reference< css::lang::XServiceInfo > xInfo(xComponent, css::uno::UNO_QUERY);
        if (!xInfo.is())
            return OUString();

        const css::uno::Sequence< OUString > lKnownModules = getElementNames();
        for (sal_Int32 i = 0; i < lKnownModules.getLength(); ++i)
        {
            if (xInfo->supportsService(lKnownModules[i]))
                return lKnownModules[i];
        }

        return OUString();
    }

    struct Instance {
        explicit Instance(
            css::uno::Reference<css::uno::XComponentContext> const & context):
            instance(static_cast<cppu::OWeakObject *>(new ModuleManager(context)))
        {
        }

        css::uno::Reference<css::uno::XInterface> instance;
    };

    struct Singleton:
        public rtl::StaticWithArg<
            Instance, css::uno::Reference<css::uno::XComponentContext>, Singleton>
    {};

    }

    extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
    com_sun_star_comp_framework_ModuleManager_get_implementation(<--- The function 'com_sun_star_comp_framework_ModuleManager_get_implementation' is never used.
        css::uno::XComponentContext *context,
        css::uno::Sequence<css::uno::Any> const &)
    {
        return cppu::acquire(static_cast<cppu::OWeakObject *>(
                    Singleton::get(context).instance.get()));
    }

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