Coverage Report

Created: 2026-10-14 09:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/sc/source/ui/unoobj/targuno.cxx
Line
Count
Source
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/*
3
 * This file is part of the LibreOffice project.
4
 *
5
 * This Source Code Form is subject to the terms of the Mozilla Public
6
 * License, v. 2.0. If a copy of the MPL was not distributed with this
7
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8
 *
9
 * This file incorporates work covered by the following license notice:
10
 *
11
 *   Licensed to the Apache Software Foundation (ASF) under one or more
12
 *   contributor license agreements. See the NOTICE file distributed
13
 *   with this work for additional information regarding copyright
14
 *   ownership. The ASF licenses this file to you under the Apache
15
 *   License, Version 2.0 (the "License"); you may not use this file
16
 *   except in compliance with the License. You may obtain a copy of
17
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18
 */
19
20
#include <sal/config.h>
21
22
#include <toolkit/helper/vclunohelper.hxx>
23
#include <svl/itemprop.hxx>
24
#include <svl/hint.hxx>
25
#include <utility>
26
#include <vcl/svapp.hxx>
27
#include <osl/diagnose.h>
28
#include <com/sun/star/awt/XBitmap.hpp>
29
#include <com/sun/star/beans/PropertyAttribute.hpp>
30
31
#include <targuno.hxx>
32
#include <miscuno.hxx>
33
#include <docuno.hxx>
34
#include <datauno.hxx>
35
#include <nameuno.hxx>
36
#include <docsh.hxx>
37
#include <content.hxx>
38
#include <scresid.hxx>
39
#include <strings.hrc>
40
#include <bitmaps.hlst>
41
#include <unonames.hxx>
42
43
#include <algorithm>
44
45
using  namespace ::com::sun::star;
46
47
const TranslateId aTypeResIds[SC_LINKTARGETTYPE_COUNT] =
48
{
49
    SCSTR_CONTENT_TABLE,        // SC_LINKTARGETTYPE_SHEET
50
    SCSTR_CONTENT_RANGENAME,    // SC_LINKTARGETTYPE_RANGENAME
51
    SCSTR_CONTENT_DBAREA        // SC_LINKTARGETTYPE_DBAREA
52
};
53
54
static std::span<const SfxItemPropertyMapEntry> lcl_GetLinkTargetMap()
55
0
{
56
0
    static const SfxItemPropertyMapEntry aLinkTargetMap_Impl[] =
57
0
    {
58
0
        { SC_UNO_LINKDISPBIT,  0,  cppu::UnoType<awt::XBitmap>::get(),   beans::PropertyAttribute::READONLY, 0 },
59
0
        { SC_UNO_LINKDISPNAME, 0,  cppu::UnoType<OUString>::get(),                beans::PropertyAttribute::READONLY, 0 },
60
0
    };
61
0
    return aLinkTargetMap_Impl;
62
0
}
63
64
// service for ScLinkTargetTypeObj is not defined
65
//  must not support document::LinkTarget because the target type cannot be used as a target
66
67
SC_SIMPLE_SERVICE_INFO( ScLinkTargetTypesObj, u"ScLinkTargetTypesObj"_ustr, u"com.sun.star.document.LinkTargets"_ustr )
68
SC_SIMPLE_SERVICE_INFO( ScLinkTargetTypeObj,  u"ScLinkTargetTypeObj"_ustr,  u"com.sun.star.document.LinkTargetSupplier"_ustr )
69
SC_SIMPLE_SERVICE_INFO( ScLinkTargetsObj,     u"ScLinkTargetsObj"_ustr,     u"com.sun.star.document.LinkTargets"_ustr )
70
71
ScLinkTargetTypesObj::ScLinkTargetTypesObj(ScDocShell* pDocSh) :
72
0
    pDocShell( pDocSh )
73
0
{
74
0
    pDocShell->GetDocument().AddUnoObject(*this);
75
76
0
    for (sal_uInt16 i=0; i<SC_LINKTARGETTYPE_COUNT; i++)
77
0
        aNames[i] = ScResId(aTypeResIds[i]);
78
0
}
79
80
ScLinkTargetTypesObj::~ScLinkTargetTypesObj()
81
0
{
82
0
    SolarMutexGuard g;
83
84
0
    if (pDocShell)
85
0
        pDocShell->GetDocument().RemoveUnoObject(*this);
86
0
}
87
88
void ScLinkTargetTypesObj::Notify( SfxBroadcaster&, const SfxHint& rHint )
89
0
{
90
0
    if ( rHint.GetId() == SfxHintId::Dying )
91
0
        pDocShell = nullptr;       // document gone
92
0
}
93
94
// container::XNameAccess
95
96
uno::Any SAL_CALL ScLinkTargetTypesObj::getByName(const OUString& aName)
97
0
{
98
0
    if (pDocShell)
99
0
    {
100
0
        for (sal_uInt16 i=0; i<SC_LINKTARGETTYPE_COUNT; i++)
101
0
            if ( aNames[i] == aName )
102
0
                return uno::Any(uno::Reference< beans::XPropertySet >(new ScLinkTargetTypeObj( pDocShell, i )));
103
0
    }
104
105
0
    throw container::NoSuchElementException();
106
0
}
107
108
uno::Sequence<OUString> SAL_CALL ScLinkTargetTypesObj::getElementNames()
109
0
{
110
0
    uno::Sequence<OUString> aRet(SC_LINKTARGETTYPE_COUNT);
111
0
    OUString* pArray = aRet.getArray();
112
0
    for (sal_uInt16 i=0; i<SC_LINKTARGETTYPE_COUNT; i++)
113
0
        pArray[i] = aNames[i];
114
0
    return aRet;
115
0
}
116
117
sal_Bool SAL_CALL ScLinkTargetTypesObj::hasByName(const OUString& aName)
118
0
{
119
0
    return std::ranges::find(aNames, aName) != std::end(aNames);
120
0
}
121
122
// container::XElementAccess
123
124
uno::Type SAL_CALL ScLinkTargetTypesObj::getElementType()
125
0
{
126
0
    return cppu::UnoType<beans::XPropertySet>::get();
127
0
}
128
129
sal_Bool SAL_CALL ScLinkTargetTypesObj::hasElements()
130
0
{
131
0
    return true;
132
0
}
133
134
ScLinkTargetTypeObj::ScLinkTargetTypeObj(ScDocShell* pDocSh, sal_uInt16 nT) :
135
0
    pDocShell( pDocSh ),
136
0
    nType( nT )
137
0
{
138
0
    pDocShell->GetDocument().AddUnoObject(*this);
139
0
    aName = ScResId(aTypeResIds[nType]);    //! on demand?
140
0
}
141
142
ScLinkTargetTypeObj::~ScLinkTargetTypeObj()
143
0
{
144
0
    SolarMutexGuard g;
145
146
0
    if (pDocShell)
147
0
        pDocShell->GetDocument().RemoveUnoObject(*this);
148
0
}
149
150
void ScLinkTargetTypeObj::Notify( SfxBroadcaster&, const SfxHint& rHint )
151
0
{
152
0
    if ( rHint.GetId() == SfxHintId::Dying )
153
0
        pDocShell = nullptr;       // document gone
154
0
}
155
156
// document::XLinkTargetSupplier
157
158
uno::Reference< container::XNameAccess > SAL_CALL  ScLinkTargetTypeObj::getLinks()
159
0
{
160
0
    uno::Reference< container::XNameAccess >  xCollection;
161
162
0
    if ( pDocShell )
163
0
    {
164
0
        switch ( nType )
165
0
        {
166
0
            case SC_LINKTARGETTYPE_SHEET:
167
0
                xCollection.set(new ScTableSheetsObj(pDocShell));
168
0
                break;
169
0
            case SC_LINKTARGETTYPE_RANGENAME:
170
0
                xCollection.set(new ScGlobalNamedRangesObj(pDocShell));
171
0
                break;
172
0
            case SC_LINKTARGETTYPE_DBAREA:
173
0
                xCollection.set(new ScDatabaseRangesObj(pDocShell));
174
0
                break;
175
0
            default:
176
0
                OSL_FAIL("invalid type");
177
0
        }
178
0
    }
179
180
    //  wrap collection in ScLinkTargetsObj because service document::LinkTargets requires
181
    //  beans::XPropertySet as ElementType in container::XNameAccess.
182
0
    if ( xCollection.is() )
183
0
        return new ScLinkTargetsObj( xCollection );
184
0
    return nullptr;
185
0
}
186
187
// beans::XPropertySet
188
189
uno::Reference< beans::XPropertySetInfo > SAL_CALL  ScLinkTargetTypeObj::getPropertySetInfo()
190
0
{
191
0
    static uno::Reference< beans::XPropertySetInfo >  aRef(new SfxItemPropertySetInfo( lcl_GetLinkTargetMap() ));
192
0
    return aRef;
193
0
}
194
195
void SAL_CALL ScLinkTargetTypeObj::setPropertyValue(const OUString& /* aPropertyName */,
196
            const uno::Any& /* aValue */)
197
0
{
198
    //  everything is read-only
199
    //! exception?
200
0
}
201
202
constexpr OUString aContentBmps[]=
203
{
204
    RID_BMP_CONTENT_TABLE,
205
    RID_BMP_CONTENT_RANGENAME,
206
    RID_BMP_CONTENT_DBAREA,
207
    RID_BMP_CONTENT_GRAPHIC,
208
    RID_BMP_CONTENT_OLEOBJECT,
209
    RID_BMP_CONTENT_NOTE,
210
    RID_BMP_CONTENT_AREALINK,
211
    RID_BMP_CONTENT_DRAWING
212
};
213
214
void ScLinkTargetTypeObj::SetLinkTargetBitmap( uno::Any& rRet, sal_uInt16 nType )
215
0
{
216
0
    ScContentId nImgId = ScContentId::ROOT;
217
0
    switch ( nType )
218
0
    {
219
0
        case SC_LINKTARGETTYPE_SHEET:
220
0
            nImgId = ScContentId::TABLE;
221
0
            break;
222
0
        case SC_LINKTARGETTYPE_RANGENAME:
223
0
            nImgId = ScContentId::RANGENAME;
224
0
            break;
225
0
        case SC_LINKTARGETTYPE_DBAREA:
226
0
            nImgId = ScContentId::DBAREA;
227
0
            break;
228
0
    }
229
0
    if (nImgId != ScContentId::ROOT)
230
0
    {
231
0
        Bitmap aBitmap { aContentBmps[static_cast<int>(nImgId) -1 ] };
232
0
        rRet <<= VCLUnoHelper::CreateBitmap(aBitmap);
233
0
    }
234
0
}
235
236
uno::Any SAL_CALL ScLinkTargetTypeObj::getPropertyValue(const OUString& PropertyName)
237
0
{
238
0
    uno::Any aRet;
239
0
    if ( PropertyName == SC_UNO_LINKDISPBIT )
240
0
        SetLinkTargetBitmap( aRet, nType );
241
0
    else if ( PropertyName == SC_UNO_LINKDISPNAME )
242
0
        aRet <<= aName;
243
244
0
    return aRet;
245
0
}
246
247
SC_IMPL_DUMMY_PROPERTY_LISTENER( ScLinkTargetTypeObj )
248
249
ScLinkTargetsObj::ScLinkTargetsObj( uno::Reference< container::XNameAccess > xColl ) :
250
0
    xCollection(std::move( xColl ))
251
0
{
252
0
    OSL_ENSURE( xCollection.is(), "ScLinkTargetsObj: NULL" );
253
0
}
254
255
ScLinkTargetsObj::~ScLinkTargetsObj()
256
0
{
257
0
}
258
259
// container::XNameAccess
260
261
uno::Any SAL_CALL ScLinkTargetsObj::getByName(const OUString& aName)
262
0
{
263
0
    uno::Reference<beans::XPropertySet> xProp(xCollection->getByName(aName), uno::UNO_QUERY);
264
0
    if (xProp.is())
265
0
        return uno::Any(xProp);
266
267
0
    throw container::NoSuchElementException();
268
0
}
269
270
uno::Sequence<OUString> SAL_CALL ScLinkTargetsObj::getElementNames()
271
0
{
272
0
    return xCollection->getElementNames();
273
0
}
274
275
sal_Bool SAL_CALL ScLinkTargetsObj::hasByName(const OUString& aName)
276
0
{
277
0
    return xCollection->hasByName(aName);
278
0
}
279
280
// container::XElementAccess
281
282
uno::Type SAL_CALL ScLinkTargetsObj::getElementType()
283
0
{
284
0
    return cppu::UnoType<beans::XPropertySet>::get();
285
0
}
286
287
sal_Bool SAL_CALL ScLinkTargetsObj::hasElements()
288
0
{
289
0
    return xCollection->hasElements();
290
0
}
291
292
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */