LibreOffice Module svx (master)  1
unoctabl.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 <unotools/pathoptions.hxx>
21 #include <com/sun/star/lang/XServiceInfo.hpp>
22 #include <com/sun/star/container/XNameContainer.hpp>
23 #include <com/sun/star/uno/XComponentContext.hpp>
24 #include <cppuhelper/implbase.hxx>
26 #include <rtl/ref.hxx>
27 #include <svx/xtable.hxx>
28 
29 using namespace ::com::sun::star;
30 
31 namespace {
32 
33 class SvxUnoColorTable : public cppu::WeakImplHelper< container::XNameContainer, lang::XServiceInfo >
34 {
35 private:
36  XColorListRef pList;
37 
38 public:
39  SvxUnoColorTable();
40 
41  // XServiceInfo
42  virtual OUString SAL_CALL getImplementationName() override;
43  virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) override;
44  virtual uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
45 
46  // XNameContainer
47  virtual void SAL_CALL insertByName( const OUString& aName, const uno::Any& aElement ) override;
48  virtual void SAL_CALL removeByName( const OUString& Name ) override;
49 
50  // XNameReplace
51  virtual void SAL_CALL replaceByName( const OUString& aName, const uno::Any& aElement ) override;
52 
53  // XNameAccess
54  virtual uno::Any SAL_CALL getByName( const OUString& aName ) override;
55 
56  virtual uno::Sequence< OUString > SAL_CALL getElementNames() override;
57 
58  virtual sal_Bool SAL_CALL hasByName( const OUString& aName ) override;
59 
60  // XElementAccess
61  virtual uno::Type SAL_CALL getElementType() override;
62  virtual sal_Bool SAL_CALL hasElements() override;
63 };
64 
65 SvxUnoColorTable::SvxUnoColorTable()
66  : pList(XPropertyList::AsColorList(
67  XPropertyList::CreatePropertyList(
68  XPropertyListType::Color, SvtPathOptions().GetPalettePath(), "")))
69 {
70 }
71 
72 sal_Bool SAL_CALL SvxUnoColorTable::supportsService( const OUString& ServiceName )
73 {
74  return cppu::supportsService( this, ServiceName );
75 }
76 
77 OUString SAL_CALL SvxUnoColorTable::getImplementationName()
78 {
79  return "com.sun.star.drawing.SvxUnoColorTable";
80 }
81 
82 uno::Sequence< OUString > SAL_CALL SvxUnoColorTable::getSupportedServiceNames()
83 {
84  uno::Sequence<OUString> aSNS { "com.sun.star.drawing.ColorTable" };
85  return aSNS;
86 }
87 
88 // XNameContainer
89 void SAL_CALL SvxUnoColorTable::insertByName( const OUString& aName, const uno::Any& aElement )
90 {
91  if( hasByName( aName ) )
92  throw container::ElementExistException();
93 
94  Color aColor;
95  if( !(aElement >>= aColor) )
96  throw lang::IllegalArgumentException();
97 
98  if( pList.is() )
99  {
100  pList->Insert(std::make_unique<XColorEntry>(aColor, aName));
101  }
102 }
103 
104 void SAL_CALL SvxUnoColorTable::removeByName( const OUString& Name )
105 {
106  long nIndex = pList.is() ? pList->GetIndex( Name ) : -1;
107  if( nIndex == -1 )
108  throw container::NoSuchElementException();
109 
110  pList->Remove( nIndex );
111 }
112 
113 // XNameReplace
114 void SAL_CALL SvxUnoColorTable::replaceByName( const OUString& aName, const uno::Any& aElement )
115 {
116  Color nColor;
117  if( !(aElement >>= nColor) )
118  throw lang::IllegalArgumentException();
119 
120  long nIndex = pList.is() ? pList->GetIndex( aName ) : -1;
121  if( nIndex == -1 )
122  throw container::NoSuchElementException();
123 
124  pList->Replace(nIndex, std::make_unique<XColorEntry>(nColor, aName ));
125 }
126 
127 // XNameAccess
128 uno::Any SAL_CALL SvxUnoColorTable::getByName( const OUString& aName )
129 {
130  long nIndex = pList.is() ? pList->GetIndex( aName ) : -1;
131  if( nIndex == -1 )
132  throw container::NoSuchElementException();
133 
134  const XColorEntry* pEntry = pList->GetColor(nIndex);
135  return uno::Any( static_cast<sal_Int32>(pEntry->GetColor().GetRGBColor()) );
136 }
137 
138 uno::Sequence< OUString > SAL_CALL SvxUnoColorTable::getElementNames()
139 {
140  const long nCount = pList.is() ? pList->Count() : 0;
141 
142  uno::Sequence< OUString > aSeq( nCount );
143  OUString* pStrings = aSeq.getArray();
144 
145  for( long nIndex = 0; nIndex < nCount; nIndex++ )
146  {
147  const XColorEntry* pEntry = pList->GetColor(nIndex);
148  pStrings[nIndex] = pEntry->GetName();
149  }
150 
151  return aSeq;
152 }
153 
154 sal_Bool SAL_CALL SvxUnoColorTable::hasByName( const OUString& aName )
155 {
156  long nIndex = pList.is() ? pList->GetIndex( aName ) : -1;
157  return nIndex != -1;
158 }
159 
160 // XElementAccess
161 uno::Type SAL_CALL SvxUnoColorTable::getElementType()
162 {
163  return ::cppu::UnoType<sal_Int32>::get();
164 }
165 
166 sal_Bool SAL_CALL SvxUnoColorTable::hasElements()
167 {
168  return pList.is() && pList->Count() != 0;
169 }
170 
171 }
172 
173 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
175  css::uno::XComponentContext *,
176  css::uno::Sequence<css::uno::Any> const &)
177 {
178  return cppu::acquire(new SvxUnoColorTable);
179 }
180 
181 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Int32 nIndex
OUString getImplementationName()
Color GetRGBColor() const
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_drawing_SvxUnoColorTable_get_implementation(css::uno::XComponentContext *, css::uno::Sequence< css::uno::Any > const &)
Definition: unoctabl.cxx:174
int nCount
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
const OUString & GetName() const
XPropertyListType
Definition: xtable.hxx:131
css::uno::Sequence< OUString > getSupportedServiceNames()
unsigned char sal_Bool
Sequence< sal_Int8 > aSeq
const Color & GetColor() const
Definition: xtable.hxx:50