bump product version to 5.0.4.1
[LibreOffice.git] / configmgr / source / winreg.cxx
blob455c7119f329934fcb9f1678af8e70f038775935
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 */
11 #include <cwchar>
12 #ifdef _MSC_VER
13 #pragma warning(push, 1) /* disable warnings within system headers */
14 #endif
15 #define WIN32_LEAN_AND_MEAN
16 #include <windows.h>
17 #include <msiquery.h>
18 #ifdef _MSC_VER
19 #pragma warning(pop)
20 #endif
22 #include <com/sun/star/uno/Any.hxx>
23 #include <com/sun/star/uno/Reference.hxx>
24 #include <com/sun/star/uno/RuntimeException.hpp>
25 #include <com/sun/star/uno/Sequence.hxx>
26 #include <com/sun/star/uno/XInterface.hpp>
27 #include <rtl/ustring.hxx>
28 #include <sal/log.hxx>
29 #include <osl/file.h>
30 #include <osl/file.hxx>
31 #include "winreg.hxx"
32 #include "writemodfile.hxx"
34 #define MAX_KEY_LENGTH 255
36 namespace configmgr {
38 namespace {
39 // This is not a generic registry reader. We assume the following structure:
40 // Last element of Key becomes prop, first part is the path and optionally nodes,
41 // when the node has oor:op attribute.
42 // Values can be the following: Value (string) and Final (dword, optional)
44 // For example the following registry setting:
45 // [HKEY_LOCAL_MACHINE\SOFTWARE\Policies\LibreOffice\org.openoffice.UserProfile\Data\o]
46 // "Value"="Example Corp."
47 // "Final"=dword:00000001
48 // becomes the following in configuration:
49 // <!-- set the Company name -->
50 // <item oor:path="/org.openoffice.UserProfile/Data">
51 // <prop oor:name="o" oor:finalized="true">
52 // <value>Example Corp.</value>
53 // </prop>
54 // </item>
56 // Another example:
57 // [HKEY_LOCAL_MACHINE\SOFTWARE\Policies\LibreOffice\org.openoffice.Office.OptionsDialog\OptionsDialogGroups\ProductName/#fuse\Pages\Java/#fuse\Hide]
58 // "Value"="true"
59 // becomes the following in configuration:
60 // <!-- Hide Tools - Options - LibreOffice - Advanced panel -->
61 // <item oor:path="/org.openoffice.Office.OptionsDialog/OptionsDialogGroups">
62 // <node oor:name="ProductName" oor:op="fuse">
63 // <node oor:name="Pages">
64 // <node oor:name="Java" oor:op="fuse">
65 // <prop oor:name="Hide">
66 // <value>true</value>
67 // </prop>
68 // </node>
69 // </node>
70 // </node>
71 // </item>
73 void dumpWindowsRegistryKey(HKEY hKey, OUString aKeyName, oslFileHandle aFileHandle)
75 HKEY hCurKey;
77 if(RegOpenKeyExW(hKey, aKeyName.getStr(), 0, KEY_READ, &hCurKey) == ERROR_SUCCESS)
79 DWORD nSubKeys = 0;
80 DWORD nValues = 0;
81 DWORD nLongestValueNameLen, nLongestValueLen;
82 // Query the number of subkeys
83 RegQueryInfoKeyW(hCurKey, NULL, NULL, NULL, &nSubKeys, NULL, NULL, &nValues, &nLongestValueNameLen, &nLongestValueLen, NULL, NULL);
84 if(nSubKeys)
86 //Look for subkeys in this key
87 for(DWORD i = 0; i < nSubKeys; i++)
89 wchar_t buffKeyName[MAX_KEY_LENGTH];
90 buffKeyName[0] = '\0';
91 DWORD buffSize=MAX_KEY_LENGTH;
92 OUString aSubkeyName;
93 //Get subkey name
94 RegEnumKeyExW(hCurKey, i, buffKeyName, &buffSize, NULL, NULL, NULL, NULL);
96 //Make up full key name
97 if(aKeyName.isEmpty())
98 aSubkeyName = aKeyName + OUString(buffKeyName);
99 else
100 aSubkeyName = aKeyName + "\\" + OUString(buffKeyName);
102 //Recursion, until no more subkeys are found
103 dumpWindowsRegistryKey(hKey, aSubkeyName, aFileHandle);
106 else if(nValues)
108 // No more subkeys, we are at a leaf
109 wchar_t* pValueName = new wchar_t[nLongestValueNameLen + 1];
110 wchar_t* pValue = new wchar_t[nLongestValueLen + 1];
112 bool bFinal = false;
113 OUString aValue;
115 for(DWORD i = 0; i < nValues; ++i)
117 DWORD nValueNameLen = nLongestValueNameLen + 1;
118 DWORD nValueLen = nLongestValueLen + 1;
120 RegEnumValueW(hCurKey, i, pValueName, &nValueNameLen, NULL, NULL, (LPBYTE)pValue, &nValueLen);
121 const wchar_t wsValue[] = L"Value";
122 const wchar_t wsFinal[] = L"Final";
124 if(!wcscmp(pValueName, wsValue))
125 aValue = OUString(pValue);
126 if(!wcscmp(pValueName, wsFinal) && *(DWORD*)pValue == 1)
127 bFinal = true;
129 sal_Int32 aLastSeparator = aKeyName.lastIndexOf('\\');
130 OUString aPathAndNodes = aKeyName.copy(0, aLastSeparator);
131 OUString aProp = aKeyName.copy(aLastSeparator + 1);
132 bool bHasNode = false;
133 sal_Int32 nCloseNode = 0;
135 writeData(aFileHandle, "<item oor:path=\"");
136 for(sal_Int32 nIndex = 0;; ++nIndex)
138 OUString aNextPathPart = aPathAndNodes.getToken(nIndex, '\\');
140 if(!aNextPathPart.isEmpty())
142 if((aNextPathPart.lastIndexOf("/#") != -1) || bHasNode)
144 bHasNode = true;
145 nCloseNode++;
146 writeData(aFileHandle, "\"><node oor:name=\"");
147 sal_Int32 nCommandSeparator = aNextPathPart.lastIndexOf('#');
148 if(nCommandSeparator != -1)
150 OUString aNodeOp = aNextPathPart.copy(nCommandSeparator + 1);
151 writeAttributeValue(aFileHandle, aNextPathPart.copy(0, nCommandSeparator - 1));
152 writeData(aFileHandle, "\" oor:op=\"");
153 writeAttributeValue(aFileHandle, aNodeOp);
155 else
157 writeAttributeValue(aFileHandle, aNextPathPart);
160 else
162 writeAttributeValue(aFileHandle, "/" + aNextPathPart);
165 else
167 writeData(aFileHandle, "\">");
168 break;
172 writeData(aFileHandle, "<prop oor:name=\"");
173 writeAttributeValue(aFileHandle, aProp);
174 writeData(aFileHandle, "\"");
175 if(bFinal)
176 writeData(aFileHandle, " oor:finalized=\"true\"");
177 writeData(aFileHandle, "><value>");
178 writeValueContent(aFileHandle, aValue);
179 writeData(aFileHandle, "</value></prop>");
180 for(; nCloseNode > 0; nCloseNode--)
181 writeData(aFileHandle, "</node>");
182 writeData(aFileHandle, "</item>\n");
183 delete[] pValueName;
184 delete[] pValue;
186 RegCloseKey(hCurKey);
191 bool dumpWindowsRegistry(OUString* pFileURL, WinRegType eType)
193 HKEY hKey;
194 HKEY hDomain = eType == LOCAL_MACHINE ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
195 if(RegOpenKeyExW(hDomain, L"SOFTWARE\\Policies\\LibreOffice", 0, KEY_READ, &hKey) != ERROR_SUCCESS)
197 SAL_INFO(
198 "configmgr",
199 ("Windows registry settings do not exist in HKLM\\SOFTWARE\\Policies\\LibreOffice"));
200 return false;
203 oslFileHandle aFileHandle;
204 switch (osl::FileBase::createTempFile(0, &aFileHandle, pFileURL)) {
205 case osl::FileBase::E_None:
206 break;
207 case osl::FileBase::E_ACCES:
208 SAL_INFO(
209 "configmgr",
210 ("cannot create temp Windows registry dump (E_ACCES)"));
211 return false;
212 default:
213 throw css::uno::RuntimeException(
214 "cannot create temporary file");
216 writeData(
217 aFileHandle,
218 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<oor:items"
219 " xmlns:oor=\"http://openoffice.org/2001/registry\""
220 " xmlns:xs=\"http://www.w3.org/2001/XMLSchema\""
221 " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n");
222 dumpWindowsRegistryKey(hKey, "", aFileHandle);
223 writeData(aFileHandle, "</oor:items>");
224 oslFileError e = osl_closeFile(aFileHandle);
225 if (e != osl_File_E_None)
226 SAL_WARN("configmgr", "osl_closeFile failed with " << +e);
227 RegCloseKey(hKey);
228 return true;
233 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */