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