1*32b1fd08SAndrew Rist /**************************************************************
2*32b1fd08SAndrew Rist  *
3*32b1fd08SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*32b1fd08SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*32b1fd08SAndrew Rist  * distributed with this work for additional information
6*32b1fd08SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*32b1fd08SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*32b1fd08SAndrew Rist  * "License"); you may not use this file except in compliance
9*32b1fd08SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*32b1fd08SAndrew Rist  *
11*32b1fd08SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*32b1fd08SAndrew Rist  *
13*32b1fd08SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*32b1fd08SAndrew Rist  * software distributed under the License is distributed on an
15*32b1fd08SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*32b1fd08SAndrew Rist  * KIND, either express or implied.  See the License for the
17*32b1fd08SAndrew Rist  * specific language governing permissions and limitations
18*32b1fd08SAndrew Rist  * under the License.
19*32b1fd08SAndrew Rist  *
20*32b1fd08SAndrew Rist  *************************************************************/
21*32b1fd08SAndrew Rist 
22cdf0e10cSrcweir #include "msihelper.hxx"
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include <malloc.h>
25cdf0e10cSrcweir #include <assert.h>
26cdf0e10cSrcweir 
GetMsiProp(MSIHANDLE handle,LPCTSTR name,std::wstring & value)27cdf0e10cSrcweir bool GetMsiProp(MSIHANDLE handle, LPCTSTR name, /*out*/std::wstring& value)
28cdf0e10cSrcweir {
29cdf0e10cSrcweir     DWORD sz = 0;
30cdf0e10cSrcweir     LPTSTR dummy = TEXT("");
31cdf0e10cSrcweir     if (MsiGetProperty(handle, name, dummy, &sz) == ERROR_MORE_DATA)
32cdf0e10cSrcweir     {
33cdf0e10cSrcweir         sz++;
34cdf0e10cSrcweir         DWORD nbytes = sz * sizeof(TCHAR);
35cdf0e10cSrcweir         LPTSTR buff = reinterpret_cast<LPTSTR>(_alloca(nbytes));
36cdf0e10cSrcweir         ZeroMemory(buff, nbytes);
37cdf0e10cSrcweir         MsiGetProperty(handle, name, buff, &sz);
38cdf0e10cSrcweir         value = buff;
39cdf0e10cSrcweir         return true;
40cdf0e10cSrcweir     }
41cdf0e10cSrcweir     return false;
42cdf0e10cSrcweir }
43cdf0e10cSrcweir 
SetMsiProp(MSIHANDLE handle,LPCTSTR name)44cdf0e10cSrcweir void SetMsiProp(MSIHANDLE handle, LPCTSTR name)
45cdf0e10cSrcweir {
46cdf0e10cSrcweir     MsiSetProperty(handle, name, TEXT("1"));
47cdf0e10cSrcweir }
48cdf0e10cSrcweir 
UnsetMsiProp(MSIHANDLE handle,LPCTSTR name)49cdf0e10cSrcweir void UnsetMsiProp(MSIHANDLE handle, LPCTSTR name)
50cdf0e10cSrcweir {
51cdf0e10cSrcweir     MsiSetProperty(handle, name, TEXT(""));
52cdf0e10cSrcweir }
53cdf0e10cSrcweir 
IsSetMsiProp(MSIHANDLE handle,LPCTSTR name)54cdf0e10cSrcweir bool IsSetMsiProp(MSIHANDLE handle, LPCTSTR name)
55cdf0e10cSrcweir {
56cdf0e10cSrcweir     std::wstring val;
57cdf0e10cSrcweir     GetMsiProp(handle, name, val);
58cdf0e10cSrcweir     return (val == TEXT("1"));
59cdf0e10cSrcweir }
60cdf0e10cSrcweir 
IsMsiPropNotEmpty(MSIHANDLE handle,LPCTSTR name)61cdf0e10cSrcweir bool IsMsiPropNotEmpty(MSIHANDLE handle, LPCTSTR name)
62cdf0e10cSrcweir {
63cdf0e10cSrcweir     std::wstring val;
64cdf0e10cSrcweir     GetMsiProp(handle, name, val);
65cdf0e10cSrcweir     return (val != TEXT(""));
66cdf0e10cSrcweir }
67cdf0e10cSrcweir 
IsAllUserInstallation(MSIHANDLE handle)68cdf0e10cSrcweir bool IsAllUserInstallation(MSIHANDLE handle)
69cdf0e10cSrcweir {
70cdf0e10cSrcweir     return IsSetMsiProp(handle, TEXT("ALLUSERS"));
71cdf0e10cSrcweir }
72cdf0e10cSrcweir 
GetOfficeInstallationPath(MSIHANDLE handle)73cdf0e10cSrcweir std::wstring GetOfficeInstallationPath(MSIHANDLE handle)
74cdf0e10cSrcweir {
75cdf0e10cSrcweir     std::wstring progpath;
76cdf0e10cSrcweir     GetMsiProp(handle, TEXT("INSTALLLOCATION"), progpath);
77cdf0e10cSrcweir     return progpath;
78cdf0e10cSrcweir }
79cdf0e10cSrcweir 
GetOfficeExecutablePath(MSIHANDLE handle)80cdf0e10cSrcweir std::wstring GetOfficeExecutablePath(MSIHANDLE handle)
81cdf0e10cSrcweir {
82cdf0e10cSrcweir     std::wstring exepath = GetOfficeInstallationPath(handle);
83cdf0e10cSrcweir     exepath += TEXT("program\\soffice.exe");
84cdf0e10cSrcweir     return exepath;
85cdf0e10cSrcweir }
86cdf0e10cSrcweir 
GetProductName(MSIHANDLE handle)87cdf0e10cSrcweir std::wstring GetProductName(MSIHANDLE handle)
88cdf0e10cSrcweir {
89cdf0e10cSrcweir     std::wstring prodname;
90cdf0e10cSrcweir     GetMsiProp(handle, TEXT("ProductName"), prodname);
91cdf0e10cSrcweir     return prodname;
92cdf0e10cSrcweir }
93cdf0e10cSrcweir 
IsModuleInstalled(MSIHANDLE handle,LPCTSTR name)94cdf0e10cSrcweir bool IsModuleInstalled(MSIHANDLE handle, LPCTSTR name)
95cdf0e10cSrcweir {
96cdf0e10cSrcweir     INSTALLSTATE current_state;
97cdf0e10cSrcweir     INSTALLSTATE future_state;
98cdf0e10cSrcweir     MsiGetFeatureState(handle, name, &current_state, &future_state);
99cdf0e10cSrcweir     return (current_state == INSTALLSTATE_LOCAL);
100cdf0e10cSrcweir }
101cdf0e10cSrcweir 
IsModuleSelectedForInstallation(MSIHANDLE handle,LPCTSTR name)102cdf0e10cSrcweir bool IsModuleSelectedForInstallation(MSIHANDLE handle, LPCTSTR name)
103cdf0e10cSrcweir {
104cdf0e10cSrcweir     INSTALLSTATE current_state;
105cdf0e10cSrcweir     INSTALLSTATE future_state;
106cdf0e10cSrcweir     MsiGetFeatureState(handle, name, &current_state, &future_state);
107cdf0e10cSrcweir     return (future_state == INSTALLSTATE_LOCAL);
108cdf0e10cSrcweir }
109cdf0e10cSrcweir 
IsModuleSelectedForDeinstallation(MSIHANDLE handle,LPCTSTR name)110cdf0e10cSrcweir bool IsModuleSelectedForDeinstallation(MSIHANDLE handle, LPCTSTR name)
111cdf0e10cSrcweir {
112cdf0e10cSrcweir     INSTALLSTATE current_state;
113cdf0e10cSrcweir     INSTALLSTATE future_state;
114cdf0e10cSrcweir     MsiGetFeatureState(handle, name, &current_state, &future_state);
115cdf0e10cSrcweir     return ((current_state == INSTALLSTATE_LOCAL) && (future_state == INSTALLSTATE_ABSENT));
116cdf0e10cSrcweir }
117cdf0e10cSrcweir 
IsCompleteDeinstallation(MSIHANDLE handle)118cdf0e10cSrcweir bool IsCompleteDeinstallation(MSIHANDLE handle)
119cdf0e10cSrcweir {
120cdf0e10cSrcweir     std::wstring rm;
121cdf0e10cSrcweir     GetMsiProp(handle, TEXT("REMOVE"), rm);
122cdf0e10cSrcweir     return (rm == TEXT("ALL"));
123cdf0e10cSrcweir }
124