LibreOffice Module cui (master) 1
CustomNotebookbarGenerator.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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
21#include <rtl/bootstrap.hxx>
22#include <config_folders.h>
24#include <osl/file.hxx>
25#include <osl/thread.h>
26#include <vcl/EnumContext.hxx>
27#include <vcl/settings.hxx>
28#include <sfx2/viewfrm.hxx>
29#include <com/sun/star/frame/ModuleManager.hpp>
31#include <libxml/parser.h>
32#include <o3tl/string_view.hxx>
33
34#define aUIPropertiesCount 3
35
36using namespace css;
37
39
41{
42 switch (eApp)
43 {
45 return "ActiveWriter";
47 return "ActiveCalc";
49 return "ActiveImpress";
51 return "ActiveDraw";
52 default:
53 return OUString();
54 }
55}
56
58{
59 switch (eApp)
60 {
62 return "Writer";
64 return "Calc";
66 return "Impress";
68 return "Draw";
69 default:
70 return OUString();
71 }
72}
73
74static OUString getAppNameRegistryPath()
75{
77
78 if (SfxViewFrame* pViewFrame = SfxViewFrame::Current())
79 {
80 const Reference<frame::XFrame>& xFrame = pViewFrame->GetFrame().GetFrameInterface();
81 const Reference<frame::XModuleManager> xModuleManager
82 = frame::ModuleManager::create(::comphelper::getProcessComponentContext());
83 eApp = vcl::EnumContext::GetApplicationEnum(xModuleManager->identify(xFrame));
84 }
85
86 OUString sAppName(lcl_getAppName(eApp));
87 return "org.openoffice.Office.UI.ToolbarMode/Applications/" + sAppName;
88}
89
90static OUString customizedUIPathBuffer()
91{
92 OUString sDirPath("${$BRAND_BASE_DIR/" LIBO_ETC_FOLDER "/" SAL_CONFIGFILE(
93 "bootstrap") ":UserInstallation}/user/config/soffice.cfg/");
94 rtl::Bootstrap::expandMacros(sDirPath);
95 return sDirPath;
96}
97
99{
100 OUString sAppName, sNotebookbarUIFileName;
101 CustomNotebookbarGenerator::getFileNameAndAppName(sAppName, sNotebookbarUIFileName);
102 return customizedUIPathBuffer() + "modules/s" + sAppName.toAsciiLowerCase() + "/ui/"
103 + sNotebookbarUIFileName;
104}
105
107{
108 OUString sAppName, sNotebookbarUIFileName;
109 CustomNotebookbarGenerator::getFileNameAndAppName(sAppName, sNotebookbarUIFileName);
110 return AllSettings::GetUIRootDir() + "modules/s" + sAppName.toAsciiLowerCase() + "/ui/"
111 + sNotebookbarUIFileName;
112}
113
114static OUString getUIDirPath()
115{
116 OUString sAppName, sNotebookbarUIFileName;
117 CustomNotebookbarGenerator::getFileNameAndAppName(sAppName, sNotebookbarUIFileName);
118 OUString sUIDirPath
119 = customizedUIPathBuffer() + "modules/s" + sAppName.toAsciiLowerCase() + "/ui/";
120 return sUIDirPath;
121}
122
123OString CustomNotebookbarGenerator::getSystemPath(OUString const& sURL)
124{
125 if (sURL.isEmpty())
126 return OString();
127 OUString sSystemPathSettings;
128 if (osl_getSystemPathFromFileURL(sURL.pData, &sSystemPathSettings.pData) != osl_File_E_None)
129 {
130 SAL_WARN("cui.customnotebookbar", "Cannot get system path for :" << sURL);
131 return OString();
132 }
133 OString osSystemPathSettings
134 = OUStringToOString(sSystemPathSettings, osl_getThreadTextEncoding());
135 return osSystemPathSettings;
136}
137
138static void changeNodeValue(xmlNode* pNodePtr, const char* pProperty, const char* pValue)
139{
140 pNodePtr = pNodePtr->xmlChildrenNode;
141 while (pNodePtr)
142 {
143 if (!(xmlStrcmp(pNodePtr->name, reinterpret_cast<const xmlChar*>("property"))))
144 {
145 xmlChar* UriValue = xmlGetProp(pNodePtr, reinterpret_cast<const xmlChar*>("name"));
146 if (!(xmlStrcmp(UriValue, reinterpret_cast<const xmlChar*>(pProperty))))
147 xmlNodeSetContent(pNodePtr, reinterpret_cast<const xmlChar*>(pValue));
148 xmlFree(UriValue);
149 break;
150 }
151 pNodePtr = pNodePtr->next;
152 }
153}
154
155static void searchNodeAndAttribute(xmlNode* pNodePtr, const char* pUIItemID, const char* pProperty,
156 const char* pValue)
157{
158 pNodePtr = pNodePtr->xmlChildrenNode;
159 while (pNodePtr)
160 {
161 if (pNodePtr->type == XML_ELEMENT_NODE)
162 {
163 if (!(xmlStrcmp(pNodePtr->name, reinterpret_cast<const xmlChar*>("object"))))
164 {
165 xmlChar* UriValue = xmlGetProp(pNodePtr, reinterpret_cast<const xmlChar*>("id"));
166 if (!(xmlStrcmp(UriValue, reinterpret_cast<const xmlChar*>(pUIItemID))))
167 changeNodeValue(pNodePtr, pProperty, pValue);
168 xmlFree(UriValue);
169 }
170 searchNodeAndAttribute(pNodePtr, pUIItemID, pProperty, pValue);
171 }
172 pNodePtr = pNodePtr->next;
173 }
174}
175
176static xmlDocPtr notebookbarXMLParser(const OString& rDocName, const OString& rUIItemID,
177 const OString& rProperty, const OString& rValue)
178{
179 xmlDocPtr pDocPtr = xmlParseFile(rDocName.getStr());
180 xmlNodePtr pNodePtr = xmlDocGetRootElement(pDocPtr);
181 searchNodeAndAttribute(pNodePtr, rUIItemID.getStr(), rProperty.getStr(), rValue.getStr());
182 return pDocPtr;
183}
184
185void CustomNotebookbarGenerator::modifyCustomizedUIFile(const Sequence<OUString>& sUIItemProperties)
186{
187 OString sCustomizedUIPath = getSystemPath(getCustomizedUIPath());
188 for (auto const& aValue : sUIItemProperties)
189 {
190 std::vector<OString> aProperties(aUIPropertiesCount);
191 for (sal_Int32 aIndex = 0; aIndex < aUIPropertiesCount; aIndex++)
192 {
193 sal_Int32 nPos = aIndex;
194 std::u16string_view sToken = o3tl::getToken(aValue, nPos, ',', nPos);
195 aProperties[aIndex] = OUStringToOString(sToken, RTL_TEXTENCODING_UTF8);
196 }
197 xmlDocPtr doc = notebookbarXMLParser(sCustomizedUIPath, aProperties[0], aProperties[1],
198 aProperties[2]);
199
200 if (doc != nullptr)
201 {
202 xmlSaveFormatFile(sCustomizedUIPath.getStr(), doc, 1);
203 xmlFreeDoc(doc);
204 }
205 }
206}
207
209 OUString& sNotebookbarUIFileName)
210{
212 if (!pFrame)
213 return;
214
215 const auto xContext = comphelper::getProcessComponentContext();
216 utl::OConfigurationTreeRoot aRoot(xContext, "org.openoffice.Office.UI.ToolbarMode/", false);
217 const Reference<frame::XFrame>& xFrame = pFrame->GetFrame().GetFrameInterface();
218 const Reference<frame::XModuleManager> xModuleManager = frame::ModuleManager::create(xContext);
219
221 = vcl::EnumContext::GetApplicationEnum(xModuleManager->identify(xFrame));
222 OUString sActiveAppName(lcl_activeAppName(eApp));
223 sAppName = lcl_getAppName(eApp);
224 const Any aValue = aRoot.getNodeValue(sActiveAppName);
225 aValue >>= sNotebookbarUIFileName;
226}
227
229{
230 OUString sUserUIDir = getUIDirPath();
231 OUString sOriginalUIPath = getOriginalUIPath();
232 OUString sCustomizedUIPath = getCustomizedUIPath();
233
234 sal_uInt32 nflag = osl_File_OpenFlag_Read | osl_File_OpenFlag_Write;
235 osl::Directory aDirectory(sUserUIDir);
236 if (aDirectory.open() != osl::FileBase::E_None)
237 osl::Directory::create(sUserUIDir, nflag);
238 else
239 SAL_WARN("cui.customnotebookbar",
240 "Cannot create the directory or directory was present :" << sUserUIDir);
241
242 osl::File aFile(sCustomizedUIPath);
243 if (aFile.open(nflag) != osl::FileBase::E_None)
244 osl::File::copy(sOriginalUIPath, sCustomizedUIPath);
245 else
246 SAL_WARN("cui.customnotebookbar",
247 "Cannot copy the file or file was present :" << sCustomizedUIPath);
248}
249
250Sequence<OUString> CustomNotebookbarGenerator::getCustomizedUIItem(OUString sNotebookbarConfigType)
251{
252 OUString aPath = getAppNameRegistryPath();
253 const utl::OConfigurationTreeRoot aAppNode(::comphelper::getProcessComponentContext(), aPath,
254 false);
255
256 const utl::OConfigurationNode aModesNode = aAppNode.openNode("Modes");
257 const utl::OConfigurationNode aModeNode(aModesNode.openNode(sNotebookbarConfigType));
258 const Any aValue = aModeNode.getNodeValue("UIItemProperties");
259 Sequence<OUString> aValues;
260 aValue >>= aValues;
261 return aValues;
262}
263
264void CustomNotebookbarGenerator::setCustomizedUIItem(Sequence<OUString> sUIItemProperties,
265 OUString sNotebookbarConfigType)
266{
267 OUString aPath = getAppNameRegistryPath();
268 const utl::OConfigurationTreeRoot aAppNode(::comphelper::getProcessComponentContext(), aPath,
269 true);
270 const utl::OConfigurationNode aModesNode = aAppNode.openNode("Modes");
271 const utl::OConfigurationNode aModeNode(aModesNode.openNode(sNotebookbarConfigType));
272
273 css::uno::Any aUIItemProperties(sUIItemProperties);
274 aModeNode.setNodeValue("UIItemProperties", aUIItemProperties);
275 aAppNode.commit();
276}
277
278/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static void searchNodeAndAttribute(xmlNode *pNodePtr, const char *pUIItemID, const char *pProperty, const char *pValue)
static void changeNodeValue(xmlNode *pNodePtr, const char *pProperty, const char *pValue)
static OUString getAppNameRegistryPath()
static xmlDocPtr notebookbarXMLParser(const OString &rDocName, const OString &rUIItemID, const OString &rProperty, const OString &rValue)
#define aUIPropertiesCount
static OUString getUIDirPath()
static OUString customizedUIPathBuffer()
static OUString lcl_activeAppName(vcl::EnumContext::Application eApp)
static OUString lcl_getAppName(vcl::EnumContext::Application eApp)
PropertiesInfo aProperties
static OUString GetUIRootDir()
static void setCustomizedUIItem(Sequence< OUString > sUIItemProperties, OUString sNotebookbarConfigType)
static void getFileNameAndAppName(OUString &sAppName, OUString &sNotebookbarUIFileName)
static OString getSystemPath(OUString const &sURL)
static void modifyCustomizedUIFile(const Sequence< OUString > &sUIItemProperties)
static Sequence< OUString > getCustomizedUIItem(OUString sNotebookbarConfigType)
const css::uno::Reference< css::frame::XFrame > & GetFrameInterface() const
static SAL_WARN_UNUSED_RESULT SfxViewFrame * Current()
SfxFrame & GetFrame() const
css::uno::Any getNodeValue(const OUString &_rPath) const noexcept
bool setNodeValue(const OUString &_rPath, const css::uno::Any &_rValue) const noexcept
OConfigurationNode openNode(const OUString &_rPath) const noexcept
bool commit() const noexcept
static Application GetApplicationEnum(const OUString &rsApplicationName)
#define SAL_CONFIGFILE(name)
std::deque< AttacherIndex_Impl > aIndex
sal_uInt16 nPos
#define SAL_WARN(area, stream)
Reference< XComponentContext > getProcessComponentContext()
std::basic_string_view< charT, traits > getToken(std::basic_string_view< charT, traits > sv, charT delimiter, std::size_t &position)
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
Reference< XFrame > xFrame