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 <rtl/ustrbuf.hxx>
23 #include <config_folders.h>
25 #include <osl/file.hxx>
26 #include <vcl/builder.hxx>
27 #include <vcl/EnumContext.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 aUIItemIDLength 255
34 #define aUIPropertiesCount 3
35 
36 using namespace css;
37 
39 
41 {
42  switch (eApp)
43  {
45  return "ActiveWriter";
46  break;
48  return "ActiveCalc";
49  break;
51  return "ActiveImpress";
52  break;
54  return "ActiveDraw";
55  break;
56  default:
57  return OUString();
58  break;
59  }
60 }
61 
63 {
64  switch (eApp)
65  {
67  return "Writer";
68  break;
70  return "Calc";
71  break;
73  return "Impress";
74  break;
76  return "Draw";
77  break;
78  default:
79  return OUString();
80  break;
81  }
82 }
83 
84 static OUString getAppNameRegistryPath()
85 {
87  const Reference<frame::XFrame>& xFrame
89  const Reference<frame::XModuleManager> xModuleManager
90  = frame::ModuleManager::create(::comphelper::getProcessComponentContext());
91  eApp = vcl::EnumContext::GetApplicationEnum(xModuleManager->identify(xFrame));
92 
93  OUString sAppName(lcl_getAppName(eApp));
94  return "org.openoffice.Office.UI.ToolbarMode/Applications/" + sAppName;
95 }
96 
97 static OUString customizedUIPathBuffer()
98 {
99  OUString sDirPath("${$BRAND_BASE_DIR/" LIBO_ETC_FOLDER "/" SAL_CONFIGFILE(
100  "bootstrap") ":UserInstallation}/user/config/soffice.cfg/");
101  rtl::Bootstrap::expandMacros(sDirPath);
102  return sDirPath;
103 }
104 
106 {
107  OUString sAppName, sNotebookbarUIFileName;
108  CustomNotebookbarGenerator::getFileNameAndAppName(sAppName, sNotebookbarUIFileName);
109  return customizedUIPathBuffer() + "modules/s" + sAppName.toAsciiLowerCase() + "/ui/"
110  + sNotebookbarUIFileName;
111 }
112 
114 {
115  OUString sAppName, sNotebookbarUIFileName;
116  CustomNotebookbarGenerator::getFileNameAndAppName(sAppName, sNotebookbarUIFileName);
117  return VclBuilderContainer::getUIRootDir() + "modules/s" + sAppName.toAsciiLowerCase() + "/ui/"
118  + sNotebookbarUIFileName;
119 }
120 
121 static OUString getUIDirPath()
122 {
123  OUString sAppName, sNotebookbarUIFileName;
124  CustomNotebookbarGenerator::getFileNameAndAppName(sAppName, sNotebookbarUIFileName);
125  OUString sUIDirPath
126  = customizedUIPathBuffer() + "modules/s" + sAppName.toAsciiLowerCase() + "/ui/";
127  return sUIDirPath;
128 }
129 
131 {
132  char* cString = new char[sString.getLength() + 1];
133  for (int nIdx = 0; nIdx < sString.getLength(); nIdx++)
134  *(cString + nIdx) = char(sString[nIdx]);
135  *(cString + sString.getLength()) = '\0';
136  return cString;
137 }
138 
139 static void changeNodeValue(xmlNode* pNodePtr, char* pProperty, char* pValue)
140 {
141  pNodePtr = pNodePtr->xmlChildrenNode;
142  while (pNodePtr)
143  {
144  if (!(xmlStrcmp(pNodePtr->name, reinterpret_cast<const xmlChar*>("property"))))
145  {
146  xmlChar* UriValue = xmlGetProp(pNodePtr, reinterpret_cast<const xmlChar*>("name"));
147  if (!(xmlStrcmp(UriValue, reinterpret_cast<const xmlChar*>(pProperty))))
148  xmlNodeSetContent(pNodePtr, reinterpret_cast<const xmlChar*>(pValue));
149  xmlFree(UriValue);
150  break;
151  }
152  pNodePtr = pNodePtr->next;
153  }
154 }
155 
156 static void searchNodeAndAttribute(xmlNode* pNodePtr, char* pUIItemID, char* pProperty,
157  char* pValue)
158 {
159  pNodePtr = pNodePtr->xmlChildrenNode;
160  while (pNodePtr)
161  {
162  if (pNodePtr->type == XML_ELEMENT_NODE)
163  {
164  if (!(xmlStrcmp(pNodePtr->name, reinterpret_cast<const xmlChar*>("object"))))
165  {
166  xmlChar* UriValue = xmlGetProp(pNodePtr, reinterpret_cast<const xmlChar*>("id"));
167  if (!(xmlStrcmp(UriValue, reinterpret_cast<const xmlChar*>(pUIItemID))))
168  changeNodeValue(pNodePtr, pProperty, pValue);
169  xmlFree(UriValue);
170  }
171  searchNodeAndAttribute(pNodePtr, pUIItemID, pProperty, pValue);
172  }
173  pNodePtr = pNodePtr->next;
174  }
175 }
176 
177 static xmlDocPtr notebookbarXMLParser(const char* pDocName, char* pUIItemID, char* pProperty,
178  char* pValue)
179 {
180  xmlDocPtr pDocPtr;
181  xmlNodePtr pNodePtr;
182 
183  pDocPtr = xmlParseFile(pDocName);
184  pNodePtr = xmlDocGetRootElement(pDocPtr);
185  searchNodeAndAttribute(pNodePtr, pUIItemID, pProperty, pValue);
186  return pDocPtr;
187 }
188 
190 {
191  OUString sCustomizedUIPath = getCustomizedUIPath();
192  char* cCustomizedUIPath = convertToCharPointer(sCustomizedUIPath);
193  for (auto const& aValue : sUIItemProperties)
194  {
195  char** pProperties = new char*[aUIPropertiesCount];
196  for (sal_Int32 aIndex = 0; aIndex < aUIPropertiesCount; aIndex++)
197  {
198  int nIdx = int(aIndex);
199  sal_Int32 rPos = aIndex;
200  pProperties[nIdx] = new char[aUIItemIDLength];
201  pProperties[nIdx] = convertToCharPointer(aValue.getToken(rPos, ',', rPos));
202  }
203  xmlDocPtr doc;
204  doc = notebookbarXMLParser(cCustomizedUIPath, pProperties[0], pProperties[1],
205  pProperties[2]);
206 
207  for (int nIdx = 0; nIdx < aUIPropertiesCount; nIdx++)
208  {
209  delete[] pProperties[nIdx];
210  }
211  delete[] pProperties;
212 
213  if (doc != nullptr)
214  {
215  xmlSaveFormatFile(cCustomizedUIPath, doc, 1);
216  xmlFreeDoc(doc);
217  }
218  }
219  delete[] cCustomizedUIPath;
220 }
221 
223  OUString& sNotebookbarUIFileName)
224 {
225  const auto xContext = comphelper::getProcessComponentContext();
226  utl::OConfigurationTreeRoot aRoot(xContext, "org.openoffice.Office.UI.ToolbarMode/", false);
227  const Reference<frame::XFrame>& xFrame
229  const Reference<frame::XModuleManager> xModuleManager = frame::ModuleManager::create(xContext);
230 
232  = vcl::EnumContext::GetApplicationEnum(xModuleManager->identify(xFrame));
233  OUString sActiveAppName(lcl_activeAppName(eApp));
234  sAppName = lcl_getAppName(eApp);
235  const Any aValue = aRoot.getNodeValue(sActiveAppName);
236  aValue >>= sNotebookbarUIFileName;
237 }
238 
240 {
241  OUString sUserUIDir = getUIDirPath();
242  OUString sOriginalUIPath = getOriginalUIPath();
243  OUString sCustomizedUIPath = getCustomizedUIPath();
244 
245  sal_uInt32 nflag = osl_File_OpenFlag_Read | osl_File_OpenFlag_Write;
246  osl::Directory aDirectory(sUserUIDir);
247  if (aDirectory.open() != osl::FileBase::E_None)
248  osl::Directory::create(sUserUIDir, nflag);
249  else
250  SAL_WARN("cui.customnotebookbar",
251  "Cannot create the directory or directory was present :" << sUserUIDir);
252 
253  osl::File aFile(sCustomizedUIPath);
254  if (aFile.open(nflag) != osl::FileBase::E_None)
255  osl::File::copy(sOriginalUIPath, sCustomizedUIPath);
256  else
257  SAL_WARN("cui.customnotebookbar",
258  "Cannot copy the file or file was present :" << sCustomizedUIPath);
259 }
260 
262 {
263  OUString aPath = getAppNameRegistryPath();
265  false);
266 
267  const utl::OConfigurationNode aModesNode = aAppNode.openNode("Modes");
268  const utl::OConfigurationNode aModeNode(aModesNode.openNode(sNotebookbarConfigType));
269  const Any aValue = aModeNode.getNodeValue("UIItemProperties");
270  Sequence<OUString> aValues;
271  aValue >>= aValues;
272  return aValues;
273 }
274 
276  OUString sNotebookbarConfigType)
277 {
278  OUString aPath = getAppNameRegistryPath();
280  true);
281  const utl::OConfigurationNode aModesNode = aAppNode.openNode("Modes");
282  const utl::OConfigurationNode aModeNode(aModesNode.openNode(sNotebookbarConfigType));
283 
284  css::uno::Any aUIItemProperties(makeAny(sUIItemProperties));
285  aModeNode.setNodeValue("UIItemProperties", aUIItemProperties);
286  aAppNode.commit();
287 }
288 
289 /* 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 changeNodeValue(xmlNode *pNodePtr, char *pProperty, char *pValue)
static void modifyCustomizedUIFile(const Sequence< OUString > &sUIItemProperties)
Reference< XFrame > xFrame
const wchar_t *typedef int(__stdcall *DllNativeUnregProc)(int
static void searchNodeAndAttribute(xmlNode *pNodePtr, char *pUIItemID, char *pProperty, char *pValue)
static OUString customizedUIPathBuffer()
SfxFrame & GetFrame() const
static OUString lcl_getAppName(vcl::EnumContext::Application eApp)
#define aUIItemIDLength
#define SAL_CONFIGFILE(name)
static OUString lcl_activeAppName(vcl::EnumContext::Application eApp)
static Application GetApplicationEnum(const OUString &rsApplicationName)
static OUString getUIRootDir()
const css::uno::Reference< css::frame::XFrame > & GetFrameInterface() const
static Sequence< OUString > getCustomizedUIItem(OUString sNotebookbarConfigType)
static OUString getAppNameRegistryPath()
static SfxViewFrame * Current()
static char * convertToCharPointer(const OUString &sString)
css::uno::Any getNodeValue(const OUString &_rPath) const
Reference< XComponentContext > getProcessComponentContext()
#define SAL_WARN(area, stream)
static void setCustomizedUIItem(Sequence< OUString > sUIItemProperties, OUString sNotebookbarConfigType)
static xmlDocPtr notebookbarXMLParser(const char *pDocName, char *pUIItemID, char *pProperty, char *pValue)
css::uno::Any SAL_CALL makeAny(const SharedUNOComponent< INTERFACE, COMPONENT > &value)