build fix: no comphelper/profilezone.hxx in this branch
[LibreOffice.git] / vcl / source / bitmap / CommandImageResolver.cxx
blobd34140d3b9d3ec2c27335836f90ae53754a250e9
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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 */
10 #include <vcl/CommandImageResolver.hxx>
11 #include <vcl/settings.hxx>
12 #include <vcl/svapp.hxx>
13 #include <rtl/ustrbuf.hxx>
14 #include <tools/urlobj.hxx>
16 using css::uno::Sequence;
18 namespace vcl
21 namespace
24 static const o3tl::enumarray<ImageType, const char*> ImageType_Prefixes =
26 "cmd/sc_",
27 "cmd/lc_",
28 "cmd/32/"
31 OUString lclConvertToCanonicalName(const OUString& rFileName)
33 bool bRemoveSlash(true);
34 sal_Int32 nLength = rFileName.getLength();
35 const sal_Unicode* pString = rFileName.getStr();
37 OUStringBuffer aBuffer(nLength);
39 for (sal_Int32 i = 0; i < nLength; i++)
41 const sal_Unicode cCurrentChar = pString[i];
42 switch (cCurrentChar)
44 // map forbidden characters to escape
45 case '/':
46 if (!bRemoveSlash)
47 aBuffer.append("%2f");
48 break;
49 case '\\': aBuffer.append("%5c"); bRemoveSlash = false; break;
50 case ':': aBuffer.append("%3a"); bRemoveSlash = false; break;
51 case '*': aBuffer.append("%2a"); bRemoveSlash = false; break;
52 case '?': aBuffer.append("%3f"); bRemoveSlash = false; break;
53 case '<': aBuffer.append("%3c"); bRemoveSlash = false; break;
54 case '>': aBuffer.append("%3e"); bRemoveSlash = false; break;
55 case '|': aBuffer.append("%7c"); bRemoveSlash = false; break;
56 default:
57 aBuffer.append(cCurrentChar); bRemoveSlash = false; break;
60 return aBuffer.makeStringAndClear();
63 } // end anonymouse namespace
65 CommandImageResolver::CommandImageResolver()
67 for (ImageList*& rp : m_pImageList)
68 rp = nullptr;
71 CommandImageResolver::~CommandImageResolver()
73 for (ImageList* p : m_pImageList)
74 delete p;
77 bool CommandImageResolver::registerCommands(Sequence<OUString>& aCommandSequence)
79 sal_Int32 nSequenceSize = aCommandSequence.getLength();
81 m_aImageCommandNameVector.resize(nSequenceSize);
82 m_aImageNameVector.resize(nSequenceSize);
84 for (sal_Int32 i = 0; i < nSequenceSize; ++i)
86 OUString aCommandName(aCommandSequence[i]);
87 OUString aImageName;
89 m_aImageCommandNameVector[i] = aCommandName;
91 if (aCommandName.indexOf(".uno:") != 0)
93 INetURLObject aUrlObject(aCommandName, INetURLObject::EncodeMechanism::All);
94 aImageName = aUrlObject.GetURLPath();
95 aImageName = lclConvertToCanonicalName(aImageName);
97 else
99 // just remove the schema
100 if (aCommandName.getLength() > 5)
101 aImageName = aCommandName.copy(5);
103 // Search for query part.
104 if (aImageName.indexOf('?') != -1)
105 aImageName = lclConvertToCanonicalName(aImageName);
108 // Image names are not case-dependent. Always use lower case characters to
109 // reflect this.
110 aImageName = aImageName.toAsciiLowerCase();
111 aImageName += ".png";
113 m_aImageNameVector[i] = aImageName;
114 m_aCommandToImageNameMap[aCommandName] = aImageName;
116 return true;
119 bool CommandImageResolver::hasImage(const OUString& rCommandURL)
121 CommandToImageNameMap::const_iterator pIterator = m_aCommandToImageNameMap.find(rCommandURL);
122 return pIterator != m_aCommandToImageNameMap.end();
125 ImageList* CommandImageResolver::getImageList(ImageType nImageType)
127 const OUString sIconTheme = Application::GetSettings().GetStyleSettings().DetermineIconTheme();
129 if (sIconTheme != m_sIconTheme)
131 m_sIconTheme = sIconTheme;
132 for (ImageList*& rp : m_pImageList)
134 delete rp;
135 rp = nullptr;
139 if (!m_pImageList[nImageType])
141 OUString sIconPath = OUString::createFromAscii(ImageType_Prefixes[nImageType]);
142 m_pImageList[nImageType] = new ImageList(m_aImageNameVector, sIconPath);
145 return m_pImageList[nImageType];
148 Image CommandImageResolver::getImageFromCommandURL(ImageType nImageType, const OUString& rCommandURL)
150 CommandToImageNameMap::const_iterator pIterator = m_aCommandToImageNameMap.find(rCommandURL);
151 if (pIterator != m_aCommandToImageNameMap.end())
153 ImageList* pImageList = getImageList(nImageType);
154 return pImageList->GetImage(pIterator->second);
156 return Image();
159 } // end namespace vcl
161 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */