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 const OUString sUIPath = getCustomizedUIPath();
188 if (osl::File(sUIPath).open(osl_File_OpenFlag_Read) != osl::FileBase::E_None)
190
191 const OString sCustomizedUIPath = getSystemPath(sUIPath);
192 for (auto const& aValue : sUIItemProperties)
193 {
194 std::vector<OString> aProperties(aUIPropertiesCount);
195 for (sal_Int32 aIndex = 0; aIndex < aUIPropertiesCount; aIndex++)
196 {
197 sal_Int32 nPos = aIndex;
198 std::u16string_view sToken = o3tl::getToken(aValue, nPos, ',', nPos);
199 aProperties[aIndex] = OUStringToOString(sToken, RTL_TEXTENCODING_UTF8);
200 }
201 xmlDocPtr doc = notebookbarXMLParser(sCustomizedUIPath, aProperties[0], aProperties[1],
202 aProperties[2]);
203
204 if (doc != nullptr)
205 {
206 xmlSaveFormatFile(sCustomizedUIPath.getStr(), doc, 1);
207 xmlFreeDoc(doc);
208 }
209 }
210}
211
213 OUString& sNotebookbarUIFileName)
214{
216 if (!pFrame)
217 return;
218
219 const auto xContext = comphelper::getProcessComponentContext();
220 utl::OConfigurationTreeRoot aRoot(xContext, "org.openoffice.Office.UI.ToolbarMode/", false);
221 const Reference<frame::XFrame>& xFrame = pFrame->GetFrame().GetFrameInterface();
222 const Reference<frame::XModuleManager> xModuleManager = frame::ModuleManager::create(xContext);
223
225 = vcl::EnumContext::GetApplicationEnum(xModuleManager->identify(xFrame));
226 OUString sActiveAppName(lcl_activeAppName(eApp));
227 sAppName = lcl_getAppName(eApp);
228 const Any aValue = aRoot.getNodeValue(sActiveAppName);
229 aValue >>= sNotebookbarUIFileName;
230}
231
233{
234 OUString sUserUIDir = getUIDirPath();
235 OUString sOriginalUIPath = getOriginalUIPath();
236 OUString sCustomizedUIPath = getCustomizedUIPath();
237
238 sal_uInt32 nflag = osl_File_OpenFlag_Read | osl_File_OpenFlag_Write;
239 osl::Directory aDirectory(sUserUIDir);
240 if (aDirectory.open() != osl::FileBase::E_None)
241 osl::Directory::create(sUserUIDir, nflag);
242 else
243 SAL_WARN("cui.customnotebookbar",
244 "Cannot create the directory or directory was present :" << sUserUIDir);
245
246 osl::File aFile(sCustomizedUIPath);
247 if (aFile.open(nflag) != osl::FileBase::E_None)
248 osl::File::copy(sOriginalUIPath, sCustomizedUIPath);
249 else
250 SAL_WARN("cui.customnotebookbar",
251 "Cannot copy the file or file was present :" << sCustomizedUIPath);
252}
253
254Sequence<OUString> CustomNotebookbarGenerator::getCustomizedUIItem(OUString sNotebookbarConfigType)
255{
256 OUString aPath = getAppNameRegistryPath();
257 const utl::OConfigurationTreeRoot aAppNode(::comphelper::getProcessComponentContext(), aPath,
258 false);
259
260 const utl::OConfigurationNode aModesNode = aAppNode.openNode("Modes");
261 const utl::OConfigurationNode aModeNode(aModesNode.openNode(sNotebookbarConfigType));
262 const Any aValue = aModeNode.getNodeValue("UIItemProperties");
263 Sequence<OUString> aValues;
264 aValue >>= aValues;
265 return aValues;
266}
267
268void CustomNotebookbarGenerator::setCustomizedUIItem(Sequence<OUString> sUIItemProperties,
269 OUString sNotebookbarConfigType)
270{
271 OUString aPath = getAppNameRegistryPath();
272 const utl::OConfigurationTreeRoot aAppNode(::comphelper::getProcessComponentContext(), aPath,
273 true);
274 const utl::OConfigurationNode aModesNode = aAppNode.openNode("Modes");
275 const utl::OConfigurationNode aModeNode(aModesNode.openNode(sNotebookbarConfigType));
276
277 css::uno::Any aUIItemProperties(sUIItemProperties);
278 aModeNode.setNodeValue("UIItemProperties", aUIItemProperties);
279 aAppNode.commit();
280}
281
282/* 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