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 
23 
24 #ifdef _MSC_VER
25 #pragma warning(push,1) // disable warnings within system headers
26 #pragma warning(disable: 4917)
27 #endif
28 #include <windows.h>
29 #include <msiquery.h>
30 #include <shlobj.h>
31 #ifdef _MSC_VER
32 #pragma warning(pop)
33 #endif
34 
35 #include <string.h>
36 #include <malloc.h>
37 #include <stdio.h>
38 #include <strsafe.h>
39 #include <string>
40 
41 //----------------------------------------------------------
42 #ifdef DEBUG
43 inline void OutputDebugStringFormat( LPCTSTR pFormat, ... )
44 {
45 	TCHAR    buffer[1024];
46 	va_list  args;
47 
48 	va_start( args, pFormat );
49 	StringCchVPrintf( buffer, sizeof(buffer), pFormat, args );
50 	OutputDebugString( buffer );
51 }
52 #else
53 static inline void OutputDebugStringFormat( LPCTSTR, ... )
54 {
55 }
56 #endif
57 
58 //----------------------------------------------------------
59 inline bool IsValidHandle( HANDLE handle )
60 {
61 	return (NULL != handle) && (INVALID_HANDLE_VALUE != handle);
62 }
63 
64 //----------------------------------------------------------
65 static bool GetMsiProp(MSIHANDLE handle, LPCTSTR name, /*out*/std::wstring& value)
66 {
67     DWORD sz = 0;
68     LPTSTR dummy = TEXT("");
69     if (MsiGetProperty(handle, name, dummy, &sz) == ERROR_MORE_DATA)
70     {
71         sz++;
72         DWORD nbytes = sz * sizeof(TCHAR);
73         LPTSTR buff = reinterpret_cast<LPTSTR>(_alloca(nbytes));
74         ZeroMemory(buff, nbytes);
75         MsiGetProperty(handle, name, buff, &sz);
76         value = buff;
77         return true;
78     }
79     return false;
80 }
81 
82 //----------------------------------------------------------
83 //----------------------------------------------------------
84 //----------------------------------------------------------
85 UINT ShowReleaseNotes( TCHAR* pFileName, TCHAR* pFilePath )
86 {
87     TCHAR sFullPath[ MAX_PATH ];
88 
89     if ( FAILED( StringCchCopy( sFullPath, MAX_PATH, pFilePath ) ) )
90     {
91         OutputDebugStringFormat( TEXT("DEBUG: ShowReleaseNotes: Could not copy path [%s]"), pFilePath );
92         return ERROR_SUCCESS;
93     }
94 
95     if ( FAILED( StringCchCat( sFullPath, MAX_PATH, pFileName ) ) )
96     {
97         OutputDebugStringFormat( TEXT("DEBUG: ShowReleaseNotes: Could not append filename [%s]"), pFileName );
98         return ERROR_SUCCESS;
99     }
100 
101     HANDLE hFile = CreateFile( sFullPath, 0, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
102 
103     if ( IsValidHandle(hFile) )
104     {
105         CloseHandle( hFile );
106         OutputDebugStringFormat( TEXT("DEBUG: ShowReleaseNotes: Found file [%s]"), sFullPath );
107 
108         SHELLEXECUTEINFOW aExecInf;
109         ZeroMemory( &aExecInf, sizeof( aExecInf ) );
110 
111         aExecInf.cbSize = sizeof( aExecInf );
112         aExecInf.fMask  = SEE_MASK_FLAG_DDEWAIT | SEE_MASK_FLAG_NO_UI;
113         aExecInf.lpVerb = TEXT("open");
114         aExecInf.lpFile = sFullPath;
115         aExecInf.lpDirectory = NULL;
116         aExecInf.nShow = SW_SHOWNORMAL;
117 
118         SetLastError( 0 );
119         ShellExecuteEx( &aExecInf );
120     }
121     else
122     {
123         OutputDebugStringFormat( TEXT("DEBUG: ShowReleaseNotes: File not found [%s]"), sFullPath );
124     }
125 
126 	return ERROR_SUCCESS;
127 }
128 
129 //----------------------------------------------------------
130 extern "C" UINT __stdcall ShowReleaseNotesBefore( MSIHANDLE )
131 {
132     TCHAR szPath[MAX_PATH];
133 
134     if( FAILED( SHGetSpecialFolderPath( NULL, szPath, CSIDL_COMMON_DOCUMENTS, true ) ) )
135         return ERROR_SUCCESS;
136 
137     OutputDebugString( TEXT("DEBUG: ShowReleaseNotesBefore called") );
138 
139     return ShowReleaseNotes( TEXT("\\sun\\releasenote1.url"), szPath );
140 }
141 
142 //----------------------------------------------------------
143 extern "C" UINT __stdcall ShowReleaseNotesAfter( MSIHANDLE )
144 {
145     TCHAR szPath[MAX_PATH];
146 
147     if( FAILED( SHGetSpecialFolderPath( NULL, szPath, CSIDL_COMMON_DOCUMENTS, true ) ) )
148         return ERROR_SUCCESS;
149 
150     OutputDebugString( TEXT("DEBUG: ShowReleaseNotesAfter called") );
151 
152     return ShowReleaseNotes( TEXT("\\sun\\releasenote2.url"), szPath );
153 }
154 
155 //----------------------------------------------------------
156 extern "C" UINT __stdcall ShowSurveyAfter( MSIHANDLE handle )
157 {
158     std::wstring prodname;
159 
160     GetMsiProp( handle, TEXT("ProductName"), prodname );
161 	std::wstring::size_type nIndex = prodname.find( TEXT( "Apache OpenOffice" ) );
162 	if( std::wstring::npos == nIndex )
163 	    return ERROR_SUCCESS;
164 
165     OutputDebugString( TEXT("DEBUG: ShowSurveyAfter called") );
166 
167     SHELLEXECUTEINFOW aExecInf;
168     ZeroMemory( &aExecInf, sizeof( aExecInf ) );
169 
170     aExecInf.cbSize = sizeof( aExecInf );
171     aExecInf.fMask  = SEE_MASK_FLAG_DDEWAIT | SEE_MASK_FLAG_NO_UI;
172     aExecInf.lpVerb = TEXT("open");
173     //aExecInf.lpFile = TEXT("http://surveys.services.openoffice.org/deinstall");
174     aExecInf.lpFile = TEXT("http://www.openoffice.org/support/");
175     aExecInf.lpDirectory = NULL;
176     aExecInf.nShow = SW_SHOWNORMAL;
177 
178     SetLastError( 0 );
179     ShellExecuteEx( &aExecInf );
180 
181     return ERROR_SUCCESS;
182 }
183 
184