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 
29 // MARKER(update_precomp.py): autogen include statement, do not remove
30 #include "precompiled_svx.hxx"
31 
32 #define UNICODE
33 #define _UNICODE
34 
35 #include <tools/svwin.h>
36 
37 #define WIN32_LEAN_AND_MEAN
38 #include <tchar.h>
39 #include <stdio.h>
40 #include <systools/win32/uwinapi.h>
41 
42 // need to undef min and max macros from MS headers here to make
43 // the std::min and std::max from stl visible again
44 #ifdef min
45 #undef min
46 #endif
47 #ifdef max
48 #undef max
49 #endif
50 
51 #include "docrecovery.hxx"
52 
53 //***************************************************************************
54 
55 static LONG RegReadValue( HKEY hBaseKey, LPCTSTR lpSubKey, LPCTSTR lpValueName, LPVOID lpData, DWORD cbData )
56 {
57 	HKEY	hKey = NULL;
58 	LONG	lResult;
59 
60 	lResult = RegOpenKeyEx( hBaseKey, lpSubKey, 0, KEY_QUERY_VALUE, &hKey );
61 
62 	if ( ERROR_SUCCESS == lResult )
63 	{
64 		lResult = RegQueryValueEx( hKey, lpValueName, NULL, NULL, (LPBYTE)lpData, &cbData );
65 		RegCloseKey( hKey );
66 	}
67 
68 	return lResult;
69 }
70 
71 //***************************************************************************
72 
73 static LONG RegWriteValue( HKEY hBaseKey, LPCTSTR lpSubKey, LPCTSTR lpValueName, DWORD dwType, LPCVOID lpData, DWORD cbData )
74 {
75 	HKEY	hKey = NULL;
76 	LONG	lResult;
77 
78 	lResult = RegCreateKeyEx( hBaseKey, lpSubKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, NULL );
79 
80 	if ( ERROR_SUCCESS == lResult )
81 	{
82 		lResult = RegSetValueEx( hKey, lpValueName, NULL, dwType, (CONST sal_uInt8 *)lpData, cbData );
83 		RegCloseKey( hKey );
84 	}
85 
86 	return lResult;
87 }
88 
89 //***************************************************************************
90 
91 namespace svx{
92     namespace DocRecovery{
93 
94 		bool ErrorRepSendDialog::ReadParams()
95 		{
96 			_TCHAR	szBuffer[2048];
97 
98 			if ( ERROR_SUCCESS == RegReadValue(
99 				HKEY_CURRENT_USER,
100 				TEXT("SOFTWARE\\OpenOffice.org\\CrashReport"),
101 				TEXT("HTTPProxyServer"),
102 				szBuffer,
103 				sizeof(szBuffer) ) )
104 				maParams.maHTTPProxyServer = (sal_Unicode *)szBuffer;
105 
106 			DWORD	dwProxyPort;
107 			if ( ERROR_SUCCESS == RegReadValue(
108 				HKEY_CURRENT_USER,
109 				TEXT("SOFTWARE\\OpenOffice.org\\CrashReport"),
110 				TEXT("HTTPProxyPort"),
111 				&dwProxyPort,
112 				sizeof(dwProxyPort) ) )
113 			{
114 				_stprintf( szBuffer, _T("%d"), dwProxyPort );
115 				maParams.maHTTPProxyPort = (sal_Unicode *)szBuffer;
116 			}
117 
118 			if ( ERROR_SUCCESS == RegReadValue(
119 				HKEY_CURRENT_USER,
120 				TEXT("SOFTWARE\\OpenOffice.org\\CrashReport"),
121 				TEXT("ReturnAddress"),
122 				szBuffer,
123 				sizeof(szBuffer) ) )
124 				maEMailAddrED.SetText( (sal_Unicode *)szBuffer );
125 
126 			DWORD	fAllowContact = sal_False;
127 			RegReadValue(
128 				HKEY_CURRENT_USER,
129 				TEXT("SOFTWARE\\OpenOffice.org\\CrashReport"),
130 				TEXT("AllowContact"),
131 				&fAllowContact,
132 				sizeof(fAllowContact) );
133 			maContactCB.Check( (sal_Bool)fAllowContact );
134 
135 			DWORD	uInternetConnection = 0;
136 			RegReadValue(
137 				HKEY_CURRENT_USER,
138 				TEXT("SOFTWARE\\OpenOffice.org\\CrashReport"),
139 				TEXT("HTTPConnection"),
140 				&uInternetConnection,
141 				sizeof(uInternetConnection) );
142 			maParams.miHTTPConnectionType = uInternetConnection;
143 
144 			return true;
145 		}
146 
147 		bool ErrorRepSendDialog::SaveParams()
148 		{
149 			const _TCHAR	*lpHTTPProxyServer = reinterpret_cast<LPCTSTR>(maParams.maHTTPProxyServer.GetBuffer());
150 			RegWriteValue(
151 				HKEY_CURRENT_USER,
152 				TEXT("SOFTWARE\\OpenOffice.org\\CrashReport"),
153 				TEXT("HTTPProxyServer"), REG_SZ,
154 				lpHTTPProxyServer,
155 				sizeof(TCHAR) * (_tcslen(lpHTTPProxyServer) + 1) );
156 
157 			_TCHAR* endptr = NULL;
158 			DWORD dwProxyPort = _tcstoul( reinterpret_cast<LPCTSTR>(maParams.maHTTPProxyPort.GetBuffer()), &endptr, 10 );
159 
160 			RegWriteValue(
161 				HKEY_CURRENT_USER,
162 				TEXT("SOFTWARE\\OpenOffice.org\\CrashReport"),
163 				TEXT("HTTPProxyPort"), REG_DWORD,
164 				&dwProxyPort,
165 				sizeof(DWORD) );
166 
167 			DWORD	fAllowContact = IsContactAllowed();
168 			RegWriteValue(
169 				HKEY_CURRENT_USER,
170 				TEXT("SOFTWARE\\OpenOffice.org\\CrashReport"),
171 				TEXT("AllowContact"), REG_DWORD,
172 				&fAllowContact,
173 				sizeof(DWORD) );
174 
175 
176 			DWORD uInternetConnection = maParams.miHTTPConnectionType;
177 
178 			RegWriteValue(
179 				HKEY_CURRENT_USER,
180 				TEXT("SOFTWARE\\OpenOffice.org\\CrashReport"),
181 				TEXT("HTTPConnection"), REG_DWORD,
182 				&uInternetConnection,
183 				sizeof(DWORD) );
184 
185 			const _TCHAR	*lpEmail = reinterpret_cast<LPCTSTR>(GetEMailAddress().GetBuffer());
186 			RegWriteValue(
187 				HKEY_CURRENT_USER,
188 				TEXT("SOFTWARE\\OpenOffice.org\\CrashReport"),
189 				TEXT("ReturnAddress"), REG_SZ,
190 				lpEmail,
191 				sizeof(TCHAR) * (_tcslen(lpEmail) + 1) );
192 
193 			return true;
194 		}
195 
196 		bool ErrorRepSendDialog::SendReport()
197 		{
198 			TCHAR	szTempPath[MAX_PATH];
199 			TCHAR	szFileName[MAX_PATH];
200 
201 			GetTempPath( elementsof(szTempPath), szTempPath );
202 			GetTempFileName( szTempPath, TEXT("DSC"), 0, szFileName );
203 
204 			FILE *fp = _tfopen( szFileName, _T("wb") );
205 
206 			if ( fp )
207 			{
208 				ByteString	strUTF8( GetUsing(), RTL_TEXTENCODING_UTF8 );
209 
210 				fwrite( strUTF8.GetBuffer(), 1, strUTF8.Len(), fp );
211 				fclose( fp );
212 			}
213 
214 			SetEnvironmentVariable( TEXT("ERRORREPORT_SUBJECT"), reinterpret_cast<LPCTSTR>(GetDocType().GetBuffer()) );
215 			SetEnvironmentVariable( TEXT("ERRORREPORT_BODYFILE"), szFileName );
216 
217 			_TCHAR	szBuffer[1024];
218 			TCHAR	szPath[MAX_PATH];
219 			LPTSTR	lpFilePart;
220 			PROCESS_INFORMATION	ProcessInfo;
221 			STARTUPINFO	StartupInfo;
222 
223 			if ( SearchPath( NULL, TEXT("crashrep.exe"), NULL, MAX_PATH, szPath, &lpFilePart ) )
224 			{
225 				ZeroMemory( &StartupInfo, sizeof(StartupInfo) );
226 				StartupInfo.cb = sizeof(StartupInfo.cb);
227 
228 				sntprintf( szBuffer, elementsof(szBuffer),
229 					_T("%s -noui -load -send"),
230 					szPath );
231 
232 				if (
233 					CreateProcess(
234 						NULL,
235 						szBuffer,
236 						NULL,
237 						NULL,
238 						sal_False,
239 						0,
240 						NULL, NULL, &StartupInfo, &ProcessInfo )
241 					)
242 				{
243 					DWORD	dwExitCode;
244 
245 					WaitForSingleObject( ProcessInfo.hProcess, INFINITE );
246 					if ( GetExitCodeProcess( ProcessInfo.hProcess, &dwExitCode ) && 0 == dwExitCode )
247 						return true;
248 
249 				}
250 			}
251 
252 			DeleteFile( szFileName );
253 
254 
255 			return false;
256 		}
257 
258 
259 	}	// namespace DocRecovery
260 }	// namespace svx
261