132b1fd08SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
332b1fd08SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
432b1fd08SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
532b1fd08SAndrew Rist  * distributed with this work for additional information
632b1fd08SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
732b1fd08SAndrew Rist  * to you under the Apache License, Version 2.0 (the
832b1fd08SAndrew Rist  * "License"); you may not use this file except in compliance
932b1fd08SAndrew Rist  * with the License.  You may obtain a copy of the License at
1032b1fd08SAndrew Rist  *
1132b1fd08SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
1232b1fd08SAndrew Rist  *
1332b1fd08SAndrew Rist  * Unless required by applicable law or agreed to in writing,
1432b1fd08SAndrew Rist  * software distributed under the License is distributed on an
1532b1fd08SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1632b1fd08SAndrew Rist  * KIND, either express or implied.  See the License for the
1732b1fd08SAndrew Rist  * specific language governing permissions and limitations
1832b1fd08SAndrew Rist  * under the License.
1932b1fd08SAndrew Rist  *
2032b1fd08SAndrew Rist  *************************************************************/
2132b1fd08SAndrew Rist 
2232b1fd08SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifdef _MSC_VER
25cdf0e10cSrcweir #pragma warning(push, 1) /* disable warnings within system headers */
26cdf0e10cSrcweir #endif
27cdf0e10cSrcweir #define WIN32_LEAN_AND_MEAN
28cdf0e10cSrcweir #include <windows.h>
29cdf0e10cSrcweir #include <msiquery.h>
30cdf0e10cSrcweir #ifdef _MSC_VER
31cdf0e10cSrcweir #pragma warning(pop)
32cdf0e10cSrcweir #endif
33cdf0e10cSrcweir 
34cdf0e10cSrcweir #include <malloc.h>
35cdf0e10cSrcweir #include <tchar.h>
36cdf0e10cSrcweir #include <string>
37cdf0e10cSrcweir #include <stdexcept>
38cdf0e10cSrcweir #include <vector>
39cdf0e10cSrcweir 
40cdf0e10cSrcweir class RegistryKeyGuard
41cdf0e10cSrcweir {
42cdf0e10cSrcweir public:
RegistryKeyGuard(HKEY hkey=0)43cdf0e10cSrcweir     RegistryKeyGuard(HKEY hkey = 0) :
44cdf0e10cSrcweir         hkey_(hkey)
45cdf0e10cSrcweir     {
46cdf0e10cSrcweir     }
47cdf0e10cSrcweir 
~RegistryKeyGuard()48cdf0e10cSrcweir     ~RegistryKeyGuard()
49cdf0e10cSrcweir     {
50cdf0e10cSrcweir         if (hkey_)
51cdf0e10cSrcweir             RegCloseKey(hkey_);
52cdf0e10cSrcweir     }
53cdf0e10cSrcweir private:
54cdf0e10cSrcweir     HKEY hkey_;
55cdf0e10cSrcweir 
56cdf0e10cSrcweir private:
57cdf0e10cSrcweir     RegistryKeyGuard(const RegistryKeyGuard&);
58cdf0e10cSrcweir     RegistryKeyGuard& operator=(const RegistryKeyGuard&);
59cdf0e10cSrcweir };
60cdf0e10cSrcweir 
61cdf0e10cSrcweir typedef std::vector<TCHAR> CharacterBuffer_t;
62cdf0e10cSrcweir 
63cdf0e10cSrcweir /* throws std::runtime_error when the value "Path" could
64cdf0e10cSrcweir    not be found or contains an empty string or is not of
65cdf0e10cSrcweir    type REG_SZ. All such conditions are invalid for a
66cdf0e10cSrcweir    properly installed product. */
FindProductInstallationPath(HKEY hkey)67cdf0e10cSrcweir std::string FindProductInstallationPath(HKEY hkey)
68cdf0e10cSrcweir {
69cdf0e10cSrcweir     DWORD nSubKeys;
70cdf0e10cSrcweir     DWORD lLongestSubKey;
71cdf0e10cSrcweir 
72cdf0e10cSrcweir     if (RegQueryInfoKey(hkey, NULL, NULL, NULL, &nSubKeys, &lLongestSubKey, NULL, NULL, NULL, NULL, NULL, NULL) !=
73cdf0e10cSrcweir         ERROR_SUCCESS)
74cdf0e10cSrcweir         throw std::runtime_error("Cannot query info for registery key");
75cdf0e10cSrcweir 
76cdf0e10cSrcweir     CharacterBuffer_t buff(lLongestSubKey + 1);
77cdf0e10cSrcweir 
78cdf0e10cSrcweir     for (DWORD i = 0; i < nSubKeys; i++)
79cdf0e10cSrcweir     {
80cdf0e10cSrcweir         buff[0] = 0;
81cdf0e10cSrcweir         LONG ret = RegEnumKey(hkey, i, &buff[0], buff.size());
82cdf0e10cSrcweir 
83cdf0e10cSrcweir         if ((ret != ERROR_SUCCESS) && (ret != ERROR_MORE_DATA))
84cdf0e10cSrcweir             throw std::runtime_error("Error enumerating registry key");
85cdf0e10cSrcweir 
86cdf0e10cSrcweir         HKEY hSubKey;
87cdf0e10cSrcweir         if (RegOpenKey(hkey, &buff[0], &hSubKey) != ERROR_SUCCESS)
88cdf0e10cSrcweir             continue;
89cdf0e10cSrcweir 
90cdf0e10cSrcweir         RegistryKeyGuard guard(hSubKey);
91cdf0e10cSrcweir 
92cdf0e10cSrcweir         DWORD type;
93cdf0e10cSrcweir         TCHAR pbuff[MAX_PATH];
94cdf0e10cSrcweir         DWORD size = sizeof(pbuff);
95cdf0e10cSrcweir         if ((RegQueryValueEx(
96cdf0e10cSrcweir                 hSubKey, TEXT("Path"), NULL, &type, reinterpret_cast<LPBYTE>(pbuff), &size) != ERROR_SUCCESS) ||
97cdf0e10cSrcweir                 (type != REG_SZ))
98cdf0e10cSrcweir             continue;
99cdf0e10cSrcweir 
100cdf0e10cSrcweir         std::string path(pbuff);
101cdf0e10cSrcweir         std::string::size_type idx = path.rfind("program\\soffice.exe");
102cdf0e10cSrcweir         if (idx != std::string::npos)
103cdf0e10cSrcweir             return path.substr(0, idx);
104cdf0e10cSrcweir     } // for
105cdf0e10cSrcweir 
106cdf0e10cSrcweir     throw std::runtime_error("No valid product path found");
107cdf0e10cSrcweir }
108cdf0e10cSrcweir 
GetInstallProperty(MSIHANDLE handle,LPCTSTR name,CharacterBuffer_t * buffer)109cdf0e10cSrcweir UINT GetInstallProperty(MSIHANDLE handle, LPCTSTR name, CharacterBuffer_t* buffer)
110cdf0e10cSrcweir {
111cdf0e10cSrcweir     DWORD size = buffer->size();
112cdf0e10cSrcweir     UINT ret = MsiGetProperty(handle, name, &(*buffer)[0], &size);
113cdf0e10cSrcweir 
114cdf0e10cSrcweir     if (ret == ERROR_MORE_DATA)
115cdf0e10cSrcweir     {
116cdf0e10cSrcweir         buffer->resize(size + 1);
117cdf0e10cSrcweir         size = buffer->size();
118cdf0e10cSrcweir         ret = MsiGetProperty(handle, name, &(*buffer)[0], &size);
119cdf0e10cSrcweir     }
120cdf0e10cSrcweir     return ret;
121cdf0e10cSrcweir }
122cdf0e10cSrcweir 
123cdf0e10cSrcweir /*
124cdf0e10cSrcweir     Try to find the installation path to an already installed product.
125cdf0e10cSrcweir     The installation path will be written in the Windows registry
126cdf0e10cSrcweir     during the installation. There may exist different products in
127*ff3f4ebcSOliver-Rainer Wittmann     parallel e.g. StarOffice, StarSuite, OpenOffice.org, Apache OpenOffice.
128*ff3f4ebcSOliver-Rainer Wittmann     It will be     searched in this order for an installed product. If a product
129cdf0e10cSrcweir     will be found the path to the product will be set in the property
130cdf0e10cSrcweir     "INSTALLLOCATION" else nothing will be done.
131cdf0e10cSrcweir */
SetProductInstallationPath(MSIHANDLE handle)132cdf0e10cSrcweir extern "C" UINT __stdcall SetProductInstallationPath(MSIHANDLE handle)
133cdf0e10cSrcweir {
134cdf0e10cSrcweir     //MessageBox(NULL, TEXT("SetProductInstallationPath"), TEXT("Language Pack Installation Helper"), MB_OK | MB_ICONINFORMATION);
135cdf0e10cSrcweir 
136cdf0e10cSrcweir     try
137cdf0e10cSrcweir     {
138cdf0e10cSrcweir         CharacterBuffer_t regKeyProdPath(MAX_PATH);
139cdf0e10cSrcweir 
140cdf0e10cSrcweir         GetInstallProperty(handle, TEXT("REGKEYPRODPATH"), &regKeyProdPath);
141cdf0e10cSrcweir 
142cdf0e10cSrcweir         HKEY hKey;
143cdf0e10cSrcweir         if ((RegOpenKey(HKEY_CURRENT_USER, &regKeyProdPath[0], &hKey) == ERROR_SUCCESS) ||
144cdf0e10cSrcweir             (RegOpenKey(HKEY_LOCAL_MACHINE, &regKeyProdPath[0], &hKey) == ERROR_SUCCESS))
145cdf0e10cSrcweir         {
146cdf0e10cSrcweir             RegistryKeyGuard guard(hKey);
147cdf0e10cSrcweir             std::string path = FindProductInstallationPath(hKey);
148cdf0e10cSrcweir             MsiSetProperty(handle, TEXT("INSTALLLOCATION"), path.c_str());
149cdf0e10cSrcweir         }
150cdf0e10cSrcweir     }
151cdf0e10cSrcweir     catch(std::runtime_error& ex)
152cdf0e10cSrcweir     {
153cdf0e10cSrcweir         ex = ex; // no warnings
154cdf0e10cSrcweir     }
155cdf0e10cSrcweir     return ERROR_SUCCESS;
156cdf0e10cSrcweir }
157cdf0e10cSrcweir 
MakeCfgimportCommandLine(CharacterBuffer_t * productPath)158cdf0e10cSrcweir void MakeCfgimportCommandLine(CharacterBuffer_t* productPath)
159cdf0e10cSrcweir {
160cdf0e10cSrcweir     char* p = &(*productPath)[0] + lstrlen(&(*productPath)[0]) - 1;
161cdf0e10cSrcweir 
162cdf0e10cSrcweir     if (*p != '\\')
163cdf0e10cSrcweir         lstrcat(&(*productPath)[0], "\\program\\configimport.exe --spool");
164cdf0e10cSrcweir     else
165cdf0e10cSrcweir         lstrcat(&(*productPath)[0], "program\\configimport.exe --spool");
166cdf0e10cSrcweir }
167cdf0e10cSrcweir 
168cdf0e10cSrcweir /*
169cdf0e10cSrcweir     Calls configimport.exe --spool
170cdf0e10cSrcweir */
RegisterLanguagePack(MSIHANDLE handle)171cdf0e10cSrcweir extern "C" UINT __stdcall RegisterLanguagePack(MSIHANDLE handle)
172cdf0e10cSrcweir {
173cdf0e10cSrcweir     //MessageBox(NULL, TEXT("RegisterLanguagePack"), TEXT("Language Pack Installation Helper"), MB_OK | MB_ICONINFORMATION);
174cdf0e10cSrcweir 
175cdf0e10cSrcweir     CharacterBuffer_t productPath(MAX_PATH);
176cdf0e10cSrcweir     GetInstallProperty(handle, TEXT("INSTALLLOCATION"), &productPath);
177cdf0e10cSrcweir     MakeCfgimportCommandLine(&productPath);
178cdf0e10cSrcweir 
179cdf0e10cSrcweir     STARTUPINFO si;
180cdf0e10cSrcweir     ZeroMemory(&si, sizeof(si));
181cdf0e10cSrcweir     si.cb = sizeof(si);
182cdf0e10cSrcweir 
183cdf0e10cSrcweir     PROCESS_INFORMATION pi;
184cdf0e10cSrcweir     ZeroMemory(&pi, sizeof(pi));
185cdf0e10cSrcweir 
186cdf0e10cSrcweir     if (CreateProcess(
187cdf0e10cSrcweir             NULL, &productPath[0], NULL, NULL,
188cdf0e10cSrcweir             FALSE, CREATE_NO_WINDOW | NORMAL_PRIORITY_CLASS, NULL,
189cdf0e10cSrcweir             NULL, &si, &pi))
190cdf0e10cSrcweir     {
191cdf0e10cSrcweir         // Wait until child process exits.
192cdf0e10cSrcweir         WaitForSingleObject(pi.hProcess, INFINITE);
193cdf0e10cSrcweir 
194cdf0e10cSrcweir         // Close process and thread handles.
195cdf0e10cSrcweir         CloseHandle(pi.hProcess);
196cdf0e10cSrcweir         CloseHandle(pi.hThread);
197cdf0e10cSrcweir     }
198cdf0e10cSrcweir     return ERROR_SUCCESS;
199cdf0e10cSrcweir }
200cdf0e10cSrcweir 
201