summaryrefslogtreecommitdiffstats
path: root/framework/source/uiconfiguration/CommandImageResolver.cxx
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-27 16:51:28 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-27 16:51:28 +0000
commit940b4d1848e8c70ab7642901a68594e8016caffc (patch)
treeeb72f344ee6c3d9b80a7ecc079ea79e9fba8676d /framework/source/uiconfiguration/CommandImageResolver.cxx
parentInitial commit. (diff)
downloadlibreoffice-upstream/1%7.0.4.tar.xz
libreoffice-upstream/1%7.0.4.zip
Adding upstream version 1:7.0.4.upstream/1%7.0.4upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r--framework/source/uiconfiguration/CommandImageResolver.cxx152
1 files changed, 152 insertions, 0 deletions
diff --git a/framework/source/uiconfiguration/CommandImageResolver.cxx b/framework/source/uiconfiguration/CommandImageResolver.cxx
new file mode 100644
index 000000000..b44393668
--- /dev/null
+++ b/framework/source/uiconfiguration/CommandImageResolver.cxx
@@ -0,0 +1,152 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+#include "CommandImageResolver.hxx"
+#include <vcl/settings.hxx>
+#include <vcl/svapp.hxx>
+#include <rtl/ustrbuf.hxx>
+#include <tools/urlobj.hxx>
+
+using css::uno::Sequence;
+
+namespace vcl
+{
+
+namespace
+{
+
+static const o3tl::enumarray<ImageType, const char*> ImageType_Prefixes =
+{
+ "cmd/sc_",
+ "cmd/lc_",
+ "cmd/32/"
+};
+
+OUString lclConvertToCanonicalName(const OUString& rFileName)
+{
+ bool bRemoveSlash(true);
+ sal_Int32 nLength = rFileName.getLength();
+ const sal_Unicode* pString = rFileName.getStr();
+
+ OUStringBuffer aBuffer(nLength*2);
+
+ for (sal_Int32 i = 0; i < nLength; i++)
+ {
+ const sal_Unicode cCurrentChar = pString[i];
+ switch (cCurrentChar)
+ {
+ // map forbidden characters to escape
+ case '/':
+ if (!bRemoveSlash)
+ aBuffer.append("%2f");
+ break;
+ case '\\': aBuffer.append("%5c"); bRemoveSlash = false; break;
+ case ':': aBuffer.append("%3a"); bRemoveSlash = false; break;
+ case '*': aBuffer.append("%2a"); bRemoveSlash = false; break;
+ case '?': aBuffer.append("%3f"); bRemoveSlash = false; break;
+ case '<': aBuffer.append("%3c"); bRemoveSlash = false; break;
+ case '>': aBuffer.append("%3e"); bRemoveSlash = false; break;
+ case '|': aBuffer.append("%7c"); bRemoveSlash = false; break;
+ default:
+ aBuffer.append(cCurrentChar); bRemoveSlash = false; break;
+ }
+ }
+ return aBuffer.makeStringAndClear();
+}
+
+} // end anonymous namespace
+
+CommandImageResolver::CommandImageResolver()
+{
+}
+
+CommandImageResolver::~CommandImageResolver()
+{
+}
+
+void CommandImageResolver::registerCommands(Sequence<OUString>& aCommandSequence)
+{
+ sal_Int32 nSequenceSize = aCommandSequence.getLength();
+
+ m_aImageCommandNameVector.resize(nSequenceSize);
+ m_aImageNameVector.resize(nSequenceSize);
+
+ for (sal_Int32 i = 0; i < nSequenceSize; ++i)
+ {
+ OUString aCommandName(aCommandSequence[i]);
+ OUString aImageName;
+
+ m_aImageCommandNameVector[i] = aCommandName;
+
+ if (aCommandName.indexOf(".uno:") != 0)
+ {
+ INetURLObject aUrlObject(aCommandName, INetURLObject::EncodeMechanism::All);
+ aImageName = aUrlObject.GetURLPath();
+ aImageName = lclConvertToCanonicalName(aImageName);
+ }
+ else
+ {
+ // just remove the schema
+ if (aCommandName.getLength() > 5)
+ aImageName = aCommandName.copy(5);
+
+ // Search for query part.
+ if (aImageName.indexOf('?') != -1)
+ aImageName = lclConvertToCanonicalName(aImageName);
+ }
+
+ // Image names are not case-dependent. Always use lower case characters to
+ // reflect this.
+ aImageName = aImageName.toAsciiLowerCase() + ".png";
+
+ m_aImageNameVector[i] = aImageName;
+ m_aCommandToImageNameMap[aCommandName] = aImageName;
+ }
+}
+
+bool CommandImageResolver::hasImage(const OUString& rCommandURL)
+{
+ CommandToImageNameMap::const_iterator pIterator = m_aCommandToImageNameMap.find(rCommandURL);
+ return pIterator != m_aCommandToImageNameMap.end();
+}
+
+ImageList* CommandImageResolver::getImageList(ImageType nImageType)
+{
+ const OUString sIconTheme = Application::GetSettings().GetStyleSettings().DetermineIconTheme();
+
+ if (sIconTheme != m_sIconTheme)
+ {
+ m_sIconTheme = sIconTheme;
+ for (auto& rp : m_pImageList)
+ rp.reset();
+ }
+
+ if (!m_pImageList[nImageType])
+ {
+ OUString sIconPath = OUString::createFromAscii(ImageType_Prefixes[nImageType]);
+ m_pImageList[nImageType].reset( new ImageList(m_aImageNameVector, sIconPath) );
+ }
+
+ return m_pImageList[nImageType].get();
+}
+
+Image CommandImageResolver::getImageFromCommandURL(ImageType nImageType, const OUString& rCommandURL)
+{
+ CommandToImageNameMap::const_iterator pIterator = m_aCommandToImageNameMap.find(rCommandURL);
+ if (pIterator != m_aCommandToImageNameMap.end())
+ {
+ ImageList* pImageList = getImageList(nImageType);
+ return pImageList->GetImage(pIterator->second);
+ }
+ return Image();
+}
+
+} // end namespace vcl
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */