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