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
/* -*- 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 <svtools/insdlg.hxx>
#include <svtools/strings.hrc>
#include <svtools/svtresid.hxx>

#include <unotools/configmgr.hxx>
#include <comphelper/classids.hxx>
#include <sot/stg.hxx>
#include <sal/macros.h>

#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/beans/PropertyValue.hpp>
#include <com/sun/star/configuration/theDefaultProvider.hpp>
#include <comphelper/processfactory.hxx>
#include <comphelper/propertysequence.hxx>
#include <com/sun/star/container/XNameAccess.hpp>

using namespace ::com::sun::star;


// this struct conforms to the Microsoft
// OBJECTDESCRIPTOR -> see oleidl.h
// (MS platform sdk)


struct OleObjectDescriptor
{
    sal_uInt32 const  cbSize;
    ClsId const       clsid;
    sal_uInt32 const  dwDrawAspect;
    Size const        sizel;
    Point const       pointl;
    sal_uInt32 const  dwStatus;
    sal_uInt32  dwFullUserTypeName;
    sal_uInt32  dwSrcOfCopy;
};

/********************** SvObjectServerList ********************************
**************************************************************************/

const SvObjectServer * SvObjectServerList::Get( const OUString & rHumanName ) const
{
    for(const auto & i : aObjectServerList)
    {
        if( rHumanName == i.GetHumanName() )<--- Consider using std::find_if algorithm instead of a raw loop.
            return &i;
    }
    return nullptr;
}

const SvObjectServer * SvObjectServerList::Get( const SvGlobalName & rName ) const
{
    for(const auto & i : aObjectServerList)
    {
        if( rName == i.GetClassName() )<--- Consider using std::find_if algorithm instead of a raw loop.
            return &i;
    }
    return nullptr;
}

void SvObjectServerList::Remove( const SvGlobalName & rName )
{
    aObjectServerList.erase(std::remove_if(aObjectServerList.begin(), aObjectServerList.end(),
        [rName](const SvObjectServer& rServer) { return rServer.GetClassName() == rName; }),
        aObjectServerList.end());
}


void SvObjectServerList::FillInsertObjects()
/* [Description]

    The list is filled with all types which can be selected in the insert-dialog.
*/
{
    try{
        uno::Reference< uno::XComponentContext > xContext = comphelper::getProcessComponentContext();

        uno::Reference< lang::XMultiServiceFactory > sProviderMSFactory =
            configuration::theDefaultProvider::get(xContext);

        uno::Sequence<uno::Any> aArguments(comphelper::InitAnyPropertySequence(
        {
            {"nodepath", uno::Any(OUString( "/org.openoffice.Office.Embedding/ObjectNames" ))}
        }));
        uno::Reference< container::XNameAccess > xNameAccess(
            sProviderMSFactory->createInstanceWithArguments( "com.sun.star.configuration.ConfigurationAccess", aArguments ),
            uno::UNO_QUERY );

        if( xNameAccess.is())
        {
            uno::Sequence< OUString > seqNames= xNameAccess->getElementNames();
            sal_Int32 nInd;

            OUString aStringProductName( "%PRODUCTNAME" );
            sal_Int32 nStringProductNameLength = aStringProductName.getLength();

            OUString aStringProductVersion( "%PRODUCTVERSION" );
            sal_Int32 nStringProductVersionLength = aStringProductVersion.getLength();

            for( nInd = 0; nInd < seqNames.getLength(); nInd++ )
            {
                uno::Reference< container::XNameAccess > xEntry ;
                xNameAccess->getByName( seqNames[nInd] ) >>= xEntry;
                if ( xEntry.is() )
                {
                    OUString aUIName;
                    OUString aClassID;
                    xEntry->getByName("ObjectUIName") >>= aUIName;
                    xEntry->getByName("ClassID") >>= aClassID;

                    if ( !aUIName.isEmpty() )
                    {
                        // replace %PRODUCTNAME
                        sal_Int32 nIndex = aUIName.indexOf( aStringProductName );
                        while( nIndex != -1 )
                        {
                            aUIName = aUIName.replaceAt(
                                nIndex, nStringProductNameLength,
                                utl::ConfigManager::getProductName() );
                            nIndex = aUIName.indexOf( aStringProductName );
                        }

                        // replace %PRODUCTVERSION
                        nIndex = aUIName.indexOf( aStringProductVersion );
                        while( nIndex != -1 )
                        {
                            aUIName = aUIName.replaceAt(
                                nIndex, nStringProductVersionLength,
                                utl::ConfigManager::getProductVersion() );
                            nIndex = aUIName.indexOf( aStringProductVersion );
                        }
                    }

                    SvGlobalName aClassName;
                    if( aClassName.MakeId( aClassID) )
                    {
                        if( !Get( aClassName ) )
                            // not entered yet
                            aObjectServerList.emplace_back( aClassName, aUIName );
                    }
                }
            }
        }


#ifdef _WIN32
        SvGlobalName aOleFact( SO3_OUT_CLASSID );
        OUString aOleObj( SvtResId( STR_FURTHER_OBJECT ) );
        aObjectServerList.push_back( SvObjectServer( aOleFact, aOleObj ) );
#endif

    }catch(const container::NoSuchElementException&)
    {
    }catch(const uno::Exception&)
    {
    }
    catch(...)
    {
    }
}

OUString SvPasteObjectHelper::GetSotFormatUIName( SotClipboardFormatId nId )
{
    struct SotResourcePair
    {
        SotClipboardFormatId const   mnSotId;
        const char* mpResId;
    };

    static const SotResourcePair aSotResourcePairs[] =
    {
        { SotClipboardFormatId::STRING,              STR_FORMAT_STRING },
        { SotClipboardFormatId::BITMAP,              STR_FORMAT_BITMAP },
        { SotClipboardFormatId::GDIMETAFILE,         STR_FORMAT_GDIMETAFILE },
        { SotClipboardFormatId::RTF,                 STR_FORMAT_RTF },
        { SotClipboardFormatId::DRAWING,             STR_FORMAT_ID_DRAWING },
        { SotClipboardFormatId::SVXB,                STR_FORMAT_ID_SVXB },
        { SotClipboardFormatId::INTERNALLINK_STATE,  STR_FORMAT_ID_INTERNALLINK_STATE },
        { SotClipboardFormatId::SOLK,                STR_FORMAT_ID_SOLK },
        { SotClipboardFormatId::NETSCAPE_BOOKMARK,   STR_FORMAT_ID_NETSCAPE_BOOKMARK },
        { SotClipboardFormatId::STARSERVER,          STR_FORMAT_ID_STARSERVER },
        { SotClipboardFormatId::STAROBJECT,          STR_FORMAT_ID_STAROBJECT },
        { SotClipboardFormatId::APPLETOBJECT,        STR_FORMAT_ID_APPLETOBJECT },
        { SotClipboardFormatId::PLUGIN_OBJECT,       STR_FORMAT_ID_PLUGIN_OBJECT },
        { SotClipboardFormatId::STARWRITER_30,       STR_FORMAT_ID_STARWRITER_30 },
        { SotClipboardFormatId::STARWRITER_40,       STR_FORMAT_ID_STARWRITER_40 },
        { SotClipboardFormatId::STARWRITER_50,       STR_FORMAT_ID_STARWRITER_50 },
        { SotClipboardFormatId::STARWRITERWEB_40,    STR_FORMAT_ID_STARWRITERWEB_40 },
        { SotClipboardFormatId::STARWRITERWEB_50,    STR_FORMAT_ID_STARWRITERWEB_50 },
        { SotClipboardFormatId::STARWRITERGLOB_40,   STR_FORMAT_ID_STARWRITERGLOB_40 },
        { SotClipboardFormatId::STARWRITERGLOB_50,   STR_FORMAT_ID_STARWRITERGLOB_50 },
        { SotClipboardFormatId::STARDRAW,            STR_FORMAT_ID_STARDRAW },
        { SotClipboardFormatId::STARDRAW_40,         STR_FORMAT_ID_STARDRAW_40 },
        { SotClipboardFormatId::STARIMPRESS_50,      STR_FORMAT_ID_STARIMPRESS_50 },
        { SotClipboardFormatId::STARDRAW_50,         STR_FORMAT_ID_STARDRAW_50 },
        { SotClipboardFormatId::STARCALC,            STR_FORMAT_ID_STARCALC },
        { SotClipboardFormatId::STARCALC_40,         STR_FORMAT_ID_STARCALC_40 },
        { SotClipboardFormatId::STARCALC_50,         STR_FORMAT_ID_STARCALC_50 },
        { SotClipboardFormatId::STARCHART,           STR_FORMAT_ID_STARCHART },
        { SotClipboardFormatId::STARCHART_40,        STR_FORMAT_ID_STARCHART_40 },
        { SotClipboardFormatId::STARCHART_50,        STR_FORMAT_ID_STARCHART_50 },
        { SotClipboardFormatId::STARIMAGE,           STR_FORMAT_ID_STARIMAGE },
        { SotClipboardFormatId::STARIMAGE_40,        STR_FORMAT_ID_STARIMAGE_40 },
        { SotClipboardFormatId::STARIMAGE_50,        STR_FORMAT_ID_STARIMAGE_50 },
        { SotClipboardFormatId::STARMATH,            STR_FORMAT_ID_STARMATH },
        { SotClipboardFormatId::STARMATH_40,         STR_FORMAT_ID_STARMATH_40 },
        { SotClipboardFormatId::STARMATH_50,         STR_FORMAT_ID_STARMATH_50 },
        { SotClipboardFormatId::STAROBJECT_PAINTDOC, STR_FORMAT_ID_STAROBJECT_PAINTDOC },
        { SotClipboardFormatId::HTML,                STR_FORMAT_ID_HTML },
        { SotClipboardFormatId::HTML_SIMPLE,         STR_FORMAT_ID_HTML_SIMPLE },
        { SotClipboardFormatId::BIFF_5,              STR_FORMAT_ID_BIFF_5 },
        { SotClipboardFormatId::BIFF_8,              STR_FORMAT_ID_BIFF_8 },
        { SotClipboardFormatId::SYLK,                STR_FORMAT_ID_SYLK },
        { SotClipboardFormatId::LINK,                STR_FORMAT_ID_LINK },
        { SotClipboardFormatId::DIF,                 STR_FORMAT_ID_DIF },
        { SotClipboardFormatId::MSWORD_DOC,          STR_FORMAT_ID_MSWORD_DOC },
        { SotClipboardFormatId::STAR_FRAMESET_DOC,   STR_FORMAT_ID_STAR_FRAMESET_DOC },
        { SotClipboardFormatId::OFFICE_DOC,          STR_FORMAT_ID_OFFICE_DOC },
        { SotClipboardFormatId::NOTES_DOCINFO,       STR_FORMAT_ID_NOTES_DOCINFO },
        { SotClipboardFormatId::SFX_DOC,             STR_FORMAT_ID_SFX_DOC },
        { SotClipboardFormatId::STARCHARTDOCUMENT_50,STR_FORMAT_ID_STARCHARTDOCUMENT_50 },
        { SotClipboardFormatId::GRAPHOBJ,            STR_FORMAT_ID_GRAPHOBJ },
        { SotClipboardFormatId::STARWRITER_60,       STR_FORMAT_ID_STARWRITER_60 },
        { SotClipboardFormatId::STARWRITERWEB_60,    STR_FORMAT_ID_STARWRITERWEB_60 },
        { SotClipboardFormatId::STARWRITERGLOB_60,   STR_FORMAT_ID_STARWRITERGLOB_60 },
        { SotClipboardFormatId::STARDRAW_60,         STR_FORMAT_ID_STARDRAW_60 },
        { SotClipboardFormatId::STARIMPRESS_60,      STR_FORMAT_ID_STARIMPRESS_60 },
        { SotClipboardFormatId::STARCALC_60,         STR_FORMAT_ID_STARCALC_60 },
        { SotClipboardFormatId::STARCHART_60,        STR_FORMAT_ID_STARCHART_60 },
        { SotClipboardFormatId::STARMATH_60,         STR_FORMAT_ID_STARMATH_60 },
        { SotClipboardFormatId::WMF,                 STR_FORMAT_ID_WMF },
        { SotClipboardFormatId::DBACCESS_QUERY,      STR_FORMAT_ID_DBACCESS_QUERY },
        { SotClipboardFormatId::DBACCESS_TABLE,      STR_FORMAT_ID_DBACCESS_TABLE },
        { SotClipboardFormatId::DBACCESS_COMMAND,    STR_FORMAT_ID_DBACCESS_COMMAND },
        { SotClipboardFormatId::DIALOG_60,           STR_FORMAT_ID_DIALOG_60 },
        { SotClipboardFormatId::FILEGRPDESCRIPTOR,   STR_FORMAT_ID_FILEGRPDESCRIPTOR },
        { SotClipboardFormatId::HTML_NO_COMMENT,     STR_FORMAT_ID_HTML_NO_COMMENT },
        { SotClipboardFormatId::RICHTEXT,            STR_FORMAT_ID_RICHTEXT },
        { SotClipboardFormatId::STRING_TSVC,         STR_FORMAT_ID_STRING_TSVC },
        { SotClipboardFormatId::PNG,                 STR_FORMAT_ID_PNG_BITMAP },
    };

    const char* pResId = nullptr;

    sal_uInt32 const nCount = SAL_N_ELEMENTS( aSotResourcePairs );
    for (sal_uInt32 i = 0; ( i < nCount ) && !pResId; ++i)
    {
        if( aSotResourcePairs[ i ].mnSotId == nId )
            pResId = aSotResourcePairs[ i ].mpResId;
    }

    OUString aUIName;
    if (pResId)
        aUIName = SvtResId(pResId);
    else
        aUIName = SotExchange::GetFormatName( nId );

    return aUIName;
}

bool SvPasteObjectHelper::GetEmbeddedName(const TransferableDataHelper& rData, OUString& _rName, OUString& _rSource, SotClipboardFormatId const & _nFormat)
{
    bool bRet = false;
    if( _nFormat == SotClipboardFormatId::EMBED_SOURCE_OLE || _nFormat == SotClipboardFormatId::EMBEDDED_OBJ_OLE )
    {
        datatransfer::DataFlavor aFlavor;
        SotExchange::GetFormatDataFlavor( SotClipboardFormatId::OBJECTDESCRIPTOR_OLE, aFlavor );

        uno::Any aAny;
        if( rData.HasFormat( aFlavor ) &&
            ( aAny = rData.GetAny(aFlavor, OUString()) ).hasValue() )
        {
            uno::Sequence< sal_Int8 > anySequence;
            aAny >>= anySequence;<--- Variable 'aAny' is assigned a value that is never used.

            OleObjectDescriptor* pOleObjDescr =
                reinterpret_cast< OleObjectDescriptor* >( anySequence.getArray( ) );

            // determine the user friendly description of the embedded object
            if ( pOleObjDescr->dwFullUserTypeName )
            {
                // we set the pointer to the start of user friendly description
                // string. it starts  at &OleObjectDescriptor + dwFullUserTypeName.
                // dwFullUserTypeName is the offset in bytes.
                // the user friendly description string is '\0' terminated.
                const sal_Unicode* pUserTypeName =
                    reinterpret_cast< sal_Unicode* >(
                        reinterpret_cast< sal_Char* >( pOleObjDescr ) +
                            pOleObjDescr->dwFullUserTypeName );

                _rName += pUserTypeName;
                // the following statement was here for historical reasons, it is commented out since it causes bug i49460
                // _nFormat = SotClipboardFormatId::EMBED_SOURCE_OLE;
            }

            // determine the source of the embedded object
            if ( pOleObjDescr->dwSrcOfCopy )
            {
                // we set the pointer to the start of source string
                // it starts  at &OleObjectDescriptor + dwSrcOfCopy.
                // dwSrcOfCopy is the offset in bytes.
                // the source string is '\0' terminated.
                const sal_Unicode* pSrcOfCopy =
                    reinterpret_cast< sal_Unicode* >(
                        reinterpret_cast< sal_Char* >( pOleObjDescr ) +
                            pOleObjDescr->dwSrcOfCopy );

                _rSource += pSrcOfCopy;
            }
            else
                _rSource = SvtResId(STR_UNKNOWN_SOURCE);
        }
        bRet = true;
    }
    return bRet;
}


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