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