1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #undef UNICODE
29 #undef _UNICODE
30 
31 #define _WIN32_WINDOWS 0x0410
32 
33 #ifdef _MSC_VER
34 #pragma warning(push, 1) /* disable warnings within system headers */
35 #endif
36 #define WIN32_LEAN_AND_MEAN
37 #include <windows.h>
38 #include <msiquery.h>
39 #ifdef _MSC_VER
40 #pragma warning(pop)
41 #endif
42 
43 #include <malloc.h>
44 #include <assert.h>
45 
46 #include <tchar.h>
47 #include <string>
48 #include <systools/win32/uwinapi.h>
49 
50 #include <../tools/seterror.hxx>
51 
52 using namespace std;
53 
54 namespace
55 {
56     string GetMsiProperty(MSIHANDLE handle, const string& sProperty)
57     {
58         string  result;
59         TCHAR   szDummy[1] = TEXT("");
60         DWORD   nChars = 0;
61 
62         if (MsiGetProperty(handle, sProperty.c_str(), szDummy, &nChars) == ERROR_MORE_DATA)
63         {
64             DWORD nBytes = ++nChars * sizeof(TCHAR);
65             LPTSTR buffer = reinterpret_cast<LPTSTR>(_alloca(nBytes));
66             ZeroMemory( buffer, nBytes );
67             MsiGetProperty(handle, sProperty.c_str(), buffer, &nChars);
68             result = buffer;
69         }
70         return result;
71     }
72 
73     inline void SetMsiProperty(MSIHANDLE handle, const string& sProperty, const string& sValue)
74     {
75         MsiSetProperty(handle, sProperty.c_str(), sValue.c_str());
76     }
77 
78     void stripFinalBackslash(std::string * path) {
79         std::string::size_type i = path->size();
80         if (i > 1) {
81             --i;
82             if ((*path)[i] == '\\') {
83                 path->erase(i);
84             }
85         }
86     }
87 
88 // Copied more or less verbatim from
89 // desktop/source/deployment/inc/dp_version.hxx:1.5 and
90 // desktop/source/deployment/misc/dp_version.cxx:1.5:
91 
92 enum Order { ORDER_LESS, ORDER_EQUAL, ORDER_GREATER };
93 
94 string getElement(string const & version, string::size_type * index) {
95     while (*index < version.size() && version[*index] == '0') {
96         ++*index;
97     }
98     string::size_type i = *index;
99     *index = version.find('.', i);
100     if (*index == string::npos) {
101         *index = version.size();
102         return string(version, i);
103     } else {
104         ++*index;
105         return string(version, i, *index - 1 - i);
106     }
107 }
108 
109 Order compareVersions(string const & version1, string const & version2) {
110     for (string::size_type i1 = 0, i2 = 0;
111          i1 < version1.size() || i2 < version2.size();)
112     {
113         string e1(getElement(version1, &i1));
114         string e2(getElement(version2, &i2));
115 
116 	    // string myText1 = TEXT("e1: ") + e1;
117 	    // string myText2 = TEXT("e2: ") + e2;
118 	    // MessageBox(NULL, myText1.c_str(), "DEBUG", MB_OK);
119 	    // MessageBox(NULL, myText2.c_str(), "DEBUG", MB_OK);
120 
121         if (e1.size() < e2.size()) {
122             return ORDER_LESS;
123         } else if (e1.size() > e2.size()) {
124             return ORDER_GREATER;
125         } else if (e1 < e2) {
126             return ORDER_LESS;
127         } else if (e1 > e2) {
128             return ORDER_GREATER;
129         }
130     }
131     return ORDER_EQUAL;
132 }
133 
134 } // namespace
135 
136 extern "C" UINT __stdcall DotNetCheck(MSIHANDLE handle) {
137     string present(GetMsiProperty(handle, TEXT("MsiNetAssemblySupport")));
138     string required(GetMsiProperty(handle, TEXT("REQUIRED_DOTNET_VERSION")));
139 
140     // string myText1 = TEXT("MsiNetAssemblySupport: ") + present;
141     // string myText2 = TEXT("REQUIRED_DOTNET_VERSION: ") + required;
142     // MessageBox(NULL, myText1.c_str(), "DEBUG", MB_OK);
143     // MessageBox(NULL, myText2.c_str(), "DEBUG", MB_OK);
144 
145     SetMsiProperty(
146         handle, TEXT("DOTNET_SUFFICIENT"),
147         (present.empty() || compareVersions(present, required) == ORDER_LESS ?
148          TEXT("0") : TEXT("1")));
149 
150     // string result(GetMsiProperty(handle, TEXT("DOTNET_SUFFICIENT")));
151     // string myText3 = TEXT("DOTNET_SUFFICIENT: ") + result;
152     // MessageBox(NULL, myText3.c_str(), "DEBUG", MB_OK);
153 
154 
155     return ERROR_SUCCESS;
156 }
157 
158 extern "C" UINT __stdcall ShowProperties(MSIHANDLE handle)
159 {
160     string property = GetMsiProperty(handle, TEXT("INSTALLLOCATION"));
161     string myText = TEXT("INSTALLLOCATION: ") + property;
162 	MessageBox(NULL, myText.c_str(), "INSTALLLOCATION", MB_OK);
163 
164     property = GetMsiProperty(handle, TEXT("Installed"));
165     myText = TEXT("Installed: ") + property;
166 	MessageBox(NULL, myText.c_str(), "Installed", MB_OK);
167 
168     property = GetMsiProperty(handle, TEXT("PATCH"));
169     myText = TEXT("PATCH: ") + property;
170 	MessageBox(NULL, myText.c_str(), "PATCH", MB_OK);
171 
172     property = GetMsiProperty(handle, TEXT("REMOVE"));
173     myText = TEXT("REMOVE: ") + property;
174 	MessageBox(NULL, myText.c_str(), "REMOVE", MB_OK);
175 
176     property = GetMsiProperty(handle, TEXT("ALLUSERS"));
177     myText = TEXT("ALLUSERS: ") + property;
178 	MessageBox(NULL, myText.c_str(), "ALLUSERS", MB_OK);
179 
180     return ERROR_SUCCESS;
181 }
182