132b1fd08SAndrew Rist /**************************************************************
2*102561b7Smseidel  *
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
10*102561b7Smseidel  *
1132b1fd08SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*102561b7Smseidel  *
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.
19*102561b7Smseidel  *
2032b1fd08SAndrew Rist  *************************************************************/
2132b1fd08SAndrew Rist 
22*102561b7Smseidel 
23*102561b7Smseidel 
24cdf0e10cSrcweir #include "msihelper.hxx"
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include <malloc.h>
27cdf0e10cSrcweir #include <assert.h>
28cdf0e10cSrcweir 
GetMsiProp(MSIHANDLE handle,LPCTSTR name,std::wstring & value)29cdf0e10cSrcweir bool GetMsiProp(MSIHANDLE handle, LPCTSTR name, /*out*/std::wstring& value)
30cdf0e10cSrcweir {
31cdf0e10cSrcweir     DWORD sz = 0;
32cdf0e10cSrcweir     LPTSTR dummy = TEXT("");
33cdf0e10cSrcweir     if (MsiGetProperty(handle, name, dummy, &sz) == ERROR_MORE_DATA)
34cdf0e10cSrcweir     {
35cdf0e10cSrcweir         sz++;
36cdf0e10cSrcweir         DWORD nbytes = sz * sizeof(TCHAR);
37cdf0e10cSrcweir         LPTSTR buff = reinterpret_cast<LPTSTR>(_alloca(nbytes));
38cdf0e10cSrcweir         ZeroMemory(buff, nbytes);
39cdf0e10cSrcweir         MsiGetProperty(handle, name, buff, &sz);
40cdf0e10cSrcweir         value = buff;
41cdf0e10cSrcweir         return true;
42*102561b7Smseidel     }
43cdf0e10cSrcweir     return false;
44cdf0e10cSrcweir }
45cdf0e10cSrcweir 
SetMsiProp(MSIHANDLE handle,LPCTSTR name)46cdf0e10cSrcweir void SetMsiProp(MSIHANDLE handle, LPCTSTR name)
47cdf0e10cSrcweir {
48cdf0e10cSrcweir     MsiSetProperty(handle, name, TEXT("1"));
49cdf0e10cSrcweir }
50cdf0e10cSrcweir 
UnsetMsiProp(MSIHANDLE handle,LPCTSTR name)51cdf0e10cSrcweir void UnsetMsiProp(MSIHANDLE handle, LPCTSTR name)
52cdf0e10cSrcweir {
53cdf0e10cSrcweir     MsiSetProperty(handle, name, TEXT(""));
54cdf0e10cSrcweir }
55cdf0e10cSrcweir 
IsSetMsiProp(MSIHANDLE handle,LPCTSTR name)56cdf0e10cSrcweir bool IsSetMsiProp(MSIHANDLE handle, LPCTSTR name)
57cdf0e10cSrcweir {
58cdf0e10cSrcweir     std::wstring val;
59cdf0e10cSrcweir     GetMsiProp(handle, name, val);
60cdf0e10cSrcweir     return (val == TEXT("1"));
61cdf0e10cSrcweir }
62cdf0e10cSrcweir 
IsMsiPropNotEmpty(MSIHANDLE handle,LPCTSTR name)63cdf0e10cSrcweir bool IsMsiPropNotEmpty(MSIHANDLE handle, LPCTSTR name)
64cdf0e10cSrcweir {
65cdf0e10cSrcweir     std::wstring val;
66cdf0e10cSrcweir     GetMsiProp(handle, name, val);
67cdf0e10cSrcweir     return (val != TEXT(""));
68cdf0e10cSrcweir }
69cdf0e10cSrcweir 
IsAllUserInstallation(MSIHANDLE handle)70cdf0e10cSrcweir bool IsAllUserInstallation(MSIHANDLE handle)
71cdf0e10cSrcweir {
72*102561b7Smseidel     return IsSetMsiProp(handle, TEXT("ALLUSERS"));
73cdf0e10cSrcweir }
74cdf0e10cSrcweir 
GetOfficeInstallationPath(MSIHANDLE handle)75cdf0e10cSrcweir std::wstring GetOfficeInstallationPath(MSIHANDLE handle)
76cdf0e10cSrcweir {
77cdf0e10cSrcweir     std::wstring progpath;
78cdf0e10cSrcweir     GetMsiProp(handle, TEXT("INSTALLLOCATION"), progpath);
79cdf0e10cSrcweir     return progpath;
80cdf0e10cSrcweir }
81cdf0e10cSrcweir 
GetOfficeExecutablePath(MSIHANDLE handle)82cdf0e10cSrcweir std::wstring GetOfficeExecutablePath(MSIHANDLE handle)
83*102561b7Smseidel {
84cdf0e10cSrcweir     std::wstring exepath = GetOfficeInstallationPath(handle);
85cdf0e10cSrcweir     exepath += TEXT("program\\soffice.exe");
86cdf0e10cSrcweir     return exepath;
87cdf0e10cSrcweir }
88cdf0e10cSrcweir 
GetProductName(MSIHANDLE handle)89cdf0e10cSrcweir std::wstring GetProductName(MSIHANDLE handle)
90cdf0e10cSrcweir {
91cdf0e10cSrcweir     std::wstring prodname;
92cdf0e10cSrcweir     GetMsiProp(handle, TEXT("ProductName"), prodname);
93*102561b7Smseidel     return prodname;
94cdf0e10cSrcweir }
95cdf0e10cSrcweir 
IsModuleInstalled(MSIHANDLE handle,LPCTSTR name)96cdf0e10cSrcweir bool IsModuleInstalled(MSIHANDLE handle, LPCTSTR name)
97cdf0e10cSrcweir {
98cdf0e10cSrcweir     INSTALLSTATE current_state;
99cdf0e10cSrcweir     INSTALLSTATE future_state;
100cdf0e10cSrcweir     MsiGetFeatureState(handle, name, &current_state, &future_state);
101cdf0e10cSrcweir     return (current_state == INSTALLSTATE_LOCAL);
102cdf0e10cSrcweir }
103cdf0e10cSrcweir 
IsModuleSelectedForInstallation(MSIHANDLE handle,LPCTSTR name)104cdf0e10cSrcweir bool IsModuleSelectedForInstallation(MSIHANDLE handle, LPCTSTR name)
105cdf0e10cSrcweir {
106cdf0e10cSrcweir     INSTALLSTATE current_state;
107cdf0e10cSrcweir     INSTALLSTATE future_state;
108cdf0e10cSrcweir     MsiGetFeatureState(handle, name, &current_state, &future_state);
109cdf0e10cSrcweir     return (future_state == INSTALLSTATE_LOCAL);
110cdf0e10cSrcweir }
111cdf0e10cSrcweir 
IsModuleSelectedForDeinstallation(MSIHANDLE handle,LPCTSTR name)112cdf0e10cSrcweir bool IsModuleSelectedForDeinstallation(MSIHANDLE handle, LPCTSTR name)
113cdf0e10cSrcweir {
114cdf0e10cSrcweir     INSTALLSTATE current_state;
115cdf0e10cSrcweir     INSTALLSTATE future_state;
116cdf0e10cSrcweir     MsiGetFeatureState(handle, name, &current_state, &future_state);
117*102561b7Smseidel     return ((current_state == INSTALLSTATE_LOCAL) && (future_state == INSTALLSTATE_ABSENT));
118cdf0e10cSrcweir }
119cdf0e10cSrcweir 
IsCompleteDeinstallation(MSIHANDLE handle)120cdf0e10cSrcweir bool IsCompleteDeinstallation(MSIHANDLE handle)
121cdf0e10cSrcweir {
122cdf0e10cSrcweir     std::wstring rm;
123cdf0e10cSrcweir     GetMsiProp(handle, TEXT("REMOVE"), rm);
124cdf0e10cSrcweir     return (rm == TEXT("ALL"));
125cdf0e10cSrcweir }
126