1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_svx.hxx"
26 #include "DGColorNameLookUp.hxx"
27 #include <com/sun/star/container/XNameContainer.hpp>
28 #include <com/sun/star/container/XNameAccess.hpp>
29 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
30 #include <comphelper/processfactory.hxx>
31 #include <vos/mutex.hxx>
32 #include <vcl/svapp.hxx>
33 
34 
35 using ::rtl::OUString;
36 using namespace ::com::sun::star;
37 
38 namespace accessibility {
39 
40 // Initialize the class instance with NULL.  A true instance is created only
41 // when the static <member>Instance</member> is called for the first time.
42 DGColorNameLookUp* DGColorNameLookUp::mpInstance = NULL;
43 
Instance(void)44 DGColorNameLookUp& DGColorNameLookUp::Instance (void)
45 {
46     // Using double check pattern to make sure that exactly one instance of
47     // the shape type handler is instantiated.
48     if (mpInstance == NULL)
49     {
50         ::vos::OGuard aGuard (::Application::GetSolarMutex());
51         if (mpInstance == NULL)
52         {
53             // Create the single instance of the color name look up.
54             mpInstance = new DGColorNameLookUp();
55         }
56     }
57 
58     return *mpInstance;
59 }
60 
61 
62 
63 
LookUpColor(long int nColor) const64 OUString DGColorNameLookUp::LookUpColor (long int nColor) const
65 {
66     OUString sColorName;
67     tColorValueToNameMap::const_iterator I;
68     I = maColorValueToNameMap.find (nColor);
69     if (I != maColorValueToNameMap.end())
70         // Found the color value.  Return the associated name.
71         sColorName = I->second;
72     else
73     {
74         // Did not find the given color.  Append its rgb tuple to the
75         // description.
76         ::rtl::OUStringBuffer sNameBuffer;
77         sNameBuffer.append (sal_Unicode('#'));
78         sNameBuffer.append (nColor, 16);
79         sColorName = sNameBuffer.makeStringAndClear();
80     }
81     return sColorName;
82 }
83 
84 
85 
86 
DGColorNameLookUp(void)87 DGColorNameLookUp::DGColorNameLookUp (void)
88 {
89     uno::Sequence<OUString> aNames;
90     uno::Reference<container::XNameAccess> xNA;
91 
92     try
93     {
94         // Create color table in which to look up the given color.
95         uno::Reference<container::XNameContainer> xColorTable (
96             ::comphelper::getProcessServiceFactory()->createInstance(
97                 OUString::createFromAscii("com.sun.star.drawing.ColorTable")),
98             uno::UNO_QUERY);
99 
100         // Get list of color names in order to iterate over the color table.
101         xNA = uno::Reference<container::XNameAccess>(xColorTable, uno::UNO_QUERY);
102         if (xNA.is())
103         {
104             // Look the solar mutex here as workarround for missing lock in
105             // called function.
106             ::vos::OGuard aGuard (::Application::GetSolarMutex());
107             aNames = xNA->getElementNames();
108         }
109     }
110     catch (uno::RuntimeException e)
111     {
112         // When an excpetion occurred then whe have an empty name sequence
113         // and the loop below is not entered.
114     }
115 
116     // Fill the map to convert from numerical color values to names.
117     if (xNA.is())
118         for (long int i=0; i<aNames.getLength(); i++)
119         {
120             // Get the numerical value for the i-th color name.
121             try
122             {
123                 uno::Any aColor (xNA->getByName (aNames[i]));
124                 long nColor = 0;
125                 aColor >>= nColor;
126                 maColorValueToNameMap[nColor] = aNames[i];
127             }
128             catch (uno::RuntimeException e)
129             {
130                 // Ignore the exception: the color who lead to the excpetion
131                 // is not included into the map.
132             }
133         }
134 }
135 
136 
137 
138 
~DGColorNameLookUp(void)139 DGColorNameLookUp::~DGColorNameLookUp (void)
140 {
141     maColorValueToNameMap.clear();
142 }
143 
144 } // end of namespace accessibility
145