LibreOffice Module sc (master)  1
targuno.cxx
Go to the documentation of this file.
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 <string_view>
23 
25 #include <svl/itemprop.hxx>
26 #include <svl/hint.hxx>
27 #include <vcl/svapp.hxx>
28 #include <osl/diagnose.h>
29 #include <com/sun/star/awt/XBitmap.hpp>
30 #include <com/sun/star/beans/PropertyAttribute.hpp>
31 
32 #include <targuno.hxx>
33 #include <miscuno.hxx>
34 #include <docuno.hxx>
35 #include <datauno.hxx>
36 #include <nameuno.hxx>
37 #include <docsh.hxx>
38 #include <content.hxx>
39 #include <scresid.hxx>
40 #include <strings.hrc>
41 #include <bitmaps.hlst>
42 #include <unonames.hxx>
43 
44 using namespace ::com::sun::star;
45 
46 static const char* aTypeResIds[SC_LINKTARGETTYPE_COUNT] =
47 {
48  SCSTR_CONTENT_TABLE, // SC_LINKTARGETTYPE_SHEET
49  SCSTR_CONTENT_RANGENAME, // SC_LINKTARGETTYPE_RANGENAME
50  SCSTR_CONTENT_DBAREA // SC_LINKTARGETTYPE_DBAREA
51 };
52 
54 {
55  static const SfxItemPropertyMapEntry aLinkTargetMap_Impl[] =
56  {
57  {u"" SC_UNO_LINKDISPBIT, 0, cppu::UnoType<awt::XBitmap>::get(), beans::PropertyAttribute::READONLY, 0 },
58  {u"" SC_UNO_LINKDISPNAME, 0, cppu::UnoType<OUString>::get(), beans::PropertyAttribute::READONLY, 0 },
59  { u"", 0, css::uno::Type(), 0, 0 }
60  };
61  return aLinkTargetMap_Impl;
62 }
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, "ScLinkTargetTypesObj", "com.sun.star.document.LinkTargets" )
68 SC_SIMPLE_SERVICE_INFO( ScLinkTargetTypeObj, "ScLinkTargetTypeObj", "com.sun.star.document.LinkTargetSupplier" )
69 SC_SIMPLE_SERVICE_INFO( ScLinkTargetsObj, "ScLinkTargetsObj", "com.sun.star.document.LinkTargets" )
70 
72  pDocShell( pDocSh )
73 {
74  pDocShell->GetDocument().AddUnoObject(*this);
75 
76  for (sal_uInt16 i=0; i<SC_LINKTARGETTYPE_COUNT; i++)
77  aNames[i] = ScResId(aTypeResIds[i]);
78 }
79 
81 {
83 
84  if (pDocShell)
86 }
87 
89 {
90  if ( rHint.GetId() == SfxHintId::Dying )
91  pDocShell = nullptr; // document gone
92 }
93 
94 // container::XNameAccess
95 
96 uno::Any SAL_CALL ScLinkTargetTypesObj::getByName(const OUString& aName)
97 {
98  if (pDocShell)
99  {
100  for (sal_uInt16 i=0; i<SC_LINKTARGETTYPE_COUNT; i++)
101  if ( aNames[i] == aName )
102  return uno::makeAny(uno::Reference< beans::XPropertySet >(new ScLinkTargetTypeObj( pDocShell, i )));
103  }
104 
105  throw container::NoSuchElementException();
106 }
107 
108 uno::Sequence<OUString> SAL_CALL ScLinkTargetTypesObj::getElementNames()
109 {
110  uno::Sequence<OUString> aRet(SC_LINKTARGETTYPE_COUNT);
111  OUString* pArray = aRet.getArray();
112  for (sal_uInt16 i=0; i<SC_LINKTARGETTYPE_COUNT; i++)
113  pArray[i] = aNames[i];
114  return aRet;
115 }
116 
117 sal_Bool SAL_CALL ScLinkTargetTypesObj::hasByName(const OUString& aName)
118 {
119  for (const auto & i : aNames)
120  if ( i == aName )
121  return true;
122  return false;
123 }
124 
125 // container::XElementAccess
126 
128 {
130 }
131 
133 {
134  return true;
135 }
136 
138  pDocShell( pDocSh ),
139  nType( nT )
140 {
143 }
144 
146 {
147  SolarMutexGuard g;
148 
149  if (pDocShell)
151 }
152 
154 {
155  if ( rHint.GetId() == SfxHintId::Dying )
156  pDocShell = nullptr; // document gone
157 }
158 
159 // document::XLinkTargetSupplier
160 
161 uno::Reference< container::XNameAccess > SAL_CALL ScLinkTargetTypeObj::getLinks()
162 {
163  uno::Reference< container::XNameAccess > xCollection;
164 
165  if ( pDocShell )
166  {
167  switch ( nType )
168  {
170  xCollection.set(new ScTableSheetsObj(pDocShell));
171  break;
173  xCollection.set(new ScGlobalNamedRangesObj(pDocShell));
174  break;
176  xCollection.set(new ScDatabaseRangesObj(pDocShell));
177  break;
178  default:
179  OSL_FAIL("invalid type");
180  }
181  }
182 
183  // wrap collection in ScLinkTargetsObj because service document::LinkTargets requires
184  // beans::XPropertySet as ElementType in container::XNameAccess.
185  if ( xCollection.is() )
186  return new ScLinkTargetsObj( xCollection );
187  return nullptr;
188 }
189 
190 // beans::XPropertySet
191 
192 uno::Reference< beans::XPropertySetInfo > SAL_CALL ScLinkTargetTypeObj::getPropertySetInfo()
193 {
194  SolarMutexGuard aGuard;
195  static uno::Reference< beans::XPropertySetInfo > aRef(new SfxItemPropertySetInfo( lcl_GetLinkTargetMap() ));
196  return aRef;
197 }
198 
199 void SAL_CALL ScLinkTargetTypeObj::setPropertyValue(const OUString& /* aPropertyName */,
200  const uno::Any& /* aValue */)
201 {
202  // everything is read-only
204 }
205 
206 const std::u16string_view aContentBmps[]=
207 {
208  u"" RID_BMP_CONTENT_TABLE,
209  u"" RID_BMP_CONTENT_RANGENAME,
210  u"" RID_BMP_CONTENT_DBAREA,
211  u"" RID_BMP_CONTENT_GRAPHIC,
212  u"" RID_BMP_CONTENT_OLEOBJECT,
213  u"" RID_BMP_CONTENT_NOTE,
214  u"" RID_BMP_CONTENT_AREALINK,
215  u"" RID_BMP_CONTENT_DRAWING
216 };
217 
219 {
221  switch ( nType )
222  {
224  nImgId = ScContentId::TABLE;
225  break;
227  nImgId = ScContentId::RANGENAME;
228  break;
230  nImgId = ScContentId::DBAREA;
231  break;
232  }
233  if (nImgId != ScContentId::ROOT)
234  {
235  BitmapEx aBitmapEx { OUString(aContentBmps[static_cast<int>(nImgId) -1 ]) };
236  rRet <<= VCLUnoHelper::CreateBitmap(aBitmapEx);
237  }
238 }
239 
240 uno::Any SAL_CALL ScLinkTargetTypeObj::getPropertyValue(const OUString& PropertyName)
241 {
242  uno::Any aRet;
243  if ( PropertyName == SC_UNO_LINKDISPBIT )
244  SetLinkTargetBitmap( aRet, nType );
245  else if ( PropertyName == SC_UNO_LINKDISPNAME )
246  aRet <<= aName;
247 
248  return aRet;
249 }
250 
252 
253 ScLinkTargetsObj::ScLinkTargetsObj( const uno::Reference< container::XNameAccess > & rColl ) :
254  xCollection( rColl )
255 {
256  OSL_ENSURE( xCollection.is(), "ScLinkTargetsObj: NULL" );
257 }
258 
260 {
261 }
262 
263 // container::XNameAccess
264 
265 uno::Any SAL_CALL ScLinkTargetsObj::getByName(const OUString& aName)
266 {
267  uno::Reference<beans::XPropertySet> xProp(xCollection->getByName(aName), uno::UNO_QUERY);
268  if (xProp.is())
269  return uno::makeAny(xProp);
270 
271  throw container::NoSuchElementException();
272 }
273 
274 uno::Sequence<OUString> SAL_CALL ScLinkTargetsObj::getElementNames()
275 {
276  return xCollection->getElementNames();
277 }
278 
279 sal_Bool SAL_CALL ScLinkTargetsObj::hasByName(const OUString& aName)
280 {
281  return xCollection->hasByName(aName);
282 }
283 
284 // container::XElementAccess
285 
287 {
289 }
290 
292 {
293  return xCollection->hasElements();
294 }
295 
296 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual css::uno::Reference< css::beans::XPropertySetInfo > SAL_CALL getPropertySetInfo() override
Definition: targuno.cxx:192
#define SC_LINKTARGETTYPE_COUNT
Definition: targuno.hxx:40
static void SetLinkTargetBitmap(css::uno::Any &rRet, sal_uInt16 nType)
Definition: targuno.cxx:218
const std::u16string_view aContentBmps[]
Definition: targuno.cxx:206
SfxHintId GetId() const
virtual css::uno::Any SAL_CALL getByName(const OUString &aName) override
Definition: targuno.cxx:265
#define SC_LINKTARGETTYPE_RANGENAME
Definition: targuno.hxx:37
static const SfxItemPropertyMapEntry * lcl_GetLinkTargetMap()
Definition: targuno.cxx:53
virtual void Notify(SfxBroadcaster &rBC, const SfxHint &rHint) override
Definition: targuno.cxx:88
virtual css::uno::Sequence< OUString > SAL_CALL getElementNames() override
Definition: targuno.cxx:274
virtual ~ScLinkTargetTypesObj() override
Definition: targuno.cxx:80
virtual css::uno::Type SAL_CALL getElementType() override
Definition: targuno.cxx:127
static css::uno::Reference< css::awt::XBitmap > CreateBitmap(const BitmapEx &rBitmap)
OUString aNames[SC_LINKTARGETTYPE_COUNT]
Definition: targuno.hxx:53
int i
virtual css::uno::Type SAL_CALL getElementType() override
Definition: targuno.cxx:286
virtual css::uno::Sequence< OUString > SAL_CALL getElementNames() override
Definition: targuno.cxx:108
virtual sal_Bool SAL_CALL hasByName(const OUString &aName) override
Definition: targuno.cxx:279
css::uno::Reference< css::container::XNameAccess > xCollection
Definition: targuno.hxx:123
virtual css::uno::Any SAL_CALL getByName(const OUString &aName) override
Definition: targuno.cxx:96
float u
unsigned char sal_Bool
OUString ScResId(const char *pId)
Definition: scdll.cxx:89
ScDocShell * pDocShell
Definition: targuno.hxx:52
virtual void SAL_CALL setPropertyValue(const OUString &aPropertyName, const css::uno::Any &aValue) override
Definition: targuno.cxx:199
css::uno::Type const & get()
#define SC_SIMPLE_SERVICE_INFO(ClassName, ClassNameAscii, ServiceAscii)
Definition: miscuno.hxx:64
Graphic / OleObject (need separate collections!)
Definition: targuno.hxx:46
virtual ~ScLinkTargetTypeObj() override
Definition: targuno.cxx:145
ScContentId
Definition: content.hxx:35
#define SC_IMPL_DUMMY_PROPERTY_LISTENER(ClassName)
Definition: miscuno.hxx:73
void AddUnoObject(SfxListener &rObject)
Definition: documen3.cxx:892
virtual void Notify(SfxBroadcaster &rBC, const SfxHint &rHint) override
Definition: targuno.cxx:153
#define SC_LINKTARGETTYPE_SHEET
Definition: targuno.hxx:36
virtual sal_Bool SAL_CALL hasElements() override
Definition: targuno.cxx:291
virtual css::uno::Reference< css::container::XNameAccess > SAL_CALL getLinks() override
Definition: targuno.cxx:161
ScDocShell * pDocShell
Definition: targuno.hxx:83
#define SC_UNO_LINKDISPBIT
Definition: unonames.hxx:189
void RemoveUnoObject(SfxListener &rObject)
Definition: documen3.cxx:900
virtual sal_Bool SAL_CALL hasByName(const OUString &aName) override
Definition: targuno.cxx:117
QPRO_FUNC_TYPE nType
Definition: qproform.cxx:400
ScLinkTargetTypeObj(ScDocShell *pDocSh, sal_uInt16 nT)
Definition: targuno.cxx:137
const ScDocument & GetDocument() const
Definition: docsh.hxx:217
virtual sal_Bool SAL_CALL hasElements() override
Definition: targuno.cxx:132
sal_uInt16 nType
Definition: targuno.hxx:84
virtual css::uno::Any SAL_CALL getPropertyValue(const OUString &PropertyName) override
Definition: targuno.cxx:240
static const char * aTypeResIds[SC_LINKTARGETTYPE_COUNT]
Definition: targuno.cxx:46
#define SC_UNO_LINKDISPNAME
Definition: unonames.hxx:190
#define SC_LINKTARGETTYPE_DBAREA
Definition: targuno.hxx:38
virtual ~ScLinkTargetsObj() override
Definition: targuno.cxx:259