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