xref: /AOO41X/main/svx/source/dialog/sendreportunx.cxx (revision ca62e2c2083b5d0995f1245bad6c2edfb455fbec)
1f6e50924SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3f6e50924SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4f6e50924SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5f6e50924SAndrew Rist  * distributed with this work for additional information
6f6e50924SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7f6e50924SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8f6e50924SAndrew Rist  * "License"); you may not use this file except in compliance
9f6e50924SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11f6e50924SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13f6e50924SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14f6e50924SAndrew Rist  * software distributed under the License is distributed on an
15f6e50924SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16f6e50924SAndrew Rist  * KIND, either express or implied.  See the License for the
17f6e50924SAndrew Rist  * specific language governing permissions and limitations
18f6e50924SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20f6e50924SAndrew Rist  *************************************************************/
21f6e50924SAndrew Rist 
22f6e50924SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir 
25cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
26cdf0e10cSrcweir #include "precompiled_svx.hxx"
27cdf0e10cSrcweir #include "docrecovery.hxx"
28cdf0e10cSrcweir #include "osl/file.hxx"
29cdf0e10cSrcweir #include "rtl/bootstrap.hxx"
30cdf0e10cSrcweir #include "rtl/strbuf.hxx"
31cdf0e10cSrcweir #include "tools/appendunixshellword.hxx"
32cdf0e10cSrcweir #include <string>
33cdf0e10cSrcweir #include <stdio.h>
34cdf0e10cSrcweir #include <stdlib.h>
35cdf0e10cSrcweir #include <unistd.h>
36cdf0e10cSrcweir #include <pwd.h>
37cdf0e10cSrcweir 
38cdf0e10cSrcweir #define RCFILE ".crash_reportrc"
39cdf0e10cSrcweir 
40cdf0e10cSrcweir using namespace ::std;
41cdf0e10cSrcweir 
get_home_dir()42cdf0e10cSrcweir static const char *get_home_dir()
43cdf0e10cSrcweir {
44cdf0e10cSrcweir 	struct passwd *ppwd = getpwuid( getuid() );
45cdf0e10cSrcweir 
46cdf0e10cSrcweir 	return ppwd ? (ppwd->pw_dir ? ppwd->pw_dir : "/") : "/";
47cdf0e10cSrcweir }
48cdf0e10cSrcweir 
read_line(FILE * fp,string & rLine)49cdf0e10cSrcweir static bool read_line( FILE *fp, string& rLine )
50cdf0e10cSrcweir {
51cdf0e10cSrcweir 	char szBuffer[1024];
52cdf0e10cSrcweir 	bool bSuccess = false;
53cdf0e10cSrcweir 	bool bEOL = false;
54cdf0e10cSrcweir 	string	line;
55cdf0e10cSrcweir 
56cdf0e10cSrcweir 
57cdf0e10cSrcweir 	while ( !bEOL && fgets( szBuffer, sizeof(szBuffer), fp ) )
58cdf0e10cSrcweir 	{
59cdf0e10cSrcweir 		int	len = strlen(szBuffer);
60cdf0e10cSrcweir 
61cdf0e10cSrcweir 		bSuccess = true;
62cdf0e10cSrcweir 
63cdf0e10cSrcweir 		while ( len && szBuffer[len - 1] == '\n' )
64cdf0e10cSrcweir 		{
65cdf0e10cSrcweir 			szBuffer[--len] = 0;
66cdf0e10cSrcweir 			bEOL = true;
67cdf0e10cSrcweir 		}
68cdf0e10cSrcweir 
69cdf0e10cSrcweir 		line.append( szBuffer );
70cdf0e10cSrcweir 	}
71cdf0e10cSrcweir 
72cdf0e10cSrcweir 	rLine = line;
73cdf0e10cSrcweir 	return bSuccess;
74cdf0e10cSrcweir }
75cdf0e10cSrcweir 
trim_string(const string & rString)76cdf0e10cSrcweir static string trim_string( const string& rString )
77cdf0e10cSrcweir {
78cdf0e10cSrcweir 	string temp = rString;
79cdf0e10cSrcweir 
80cdf0e10cSrcweir 	while ( temp.length() && (temp[0] == ' ' || temp[0] == '\t') )
81cdf0e10cSrcweir 		temp.erase( 0, 1 );
82cdf0e10cSrcweir 
83cdf0e10cSrcweir 	string::size_type	len = temp.length();
84cdf0e10cSrcweir 
85cdf0e10cSrcweir 	while ( len && (temp[len-1] == ' ' || temp[len-1] == '\t') )
86cdf0e10cSrcweir 	{
87cdf0e10cSrcweir 		temp.erase( len - 1, 1 );
88cdf0e10cSrcweir 		len = temp.length();
89cdf0e10cSrcweir 	}
90cdf0e10cSrcweir 
91cdf0e10cSrcweir 	return temp;
92cdf0e10cSrcweir }
93cdf0e10cSrcweir 
get_profile_string(const char * pFileName,const char * pSectionName,const char * pKeyName,const char * pDefault=NULL)94cdf0e10cSrcweir static string get_profile_string( const char *pFileName, const char *pSectionName, const char *pKeyName, const char *pDefault = NULL )
95cdf0e10cSrcweir {
96cdf0e10cSrcweir 	FILE	*fp = fopen( pFileName, "r" );
97cdf0e10cSrcweir 	string	retValue = pDefault ? pDefault : "";
98cdf0e10cSrcweir 
99cdf0e10cSrcweir 	if ( fp )
100cdf0e10cSrcweir 	{
101cdf0e10cSrcweir 		string line;
102cdf0e10cSrcweir 		string section;
103cdf0e10cSrcweir 
104cdf0e10cSrcweir 		while ( read_line( fp, line ) )
105cdf0e10cSrcweir 		{
106cdf0e10cSrcweir 			line = trim_string( line );
107cdf0e10cSrcweir 
108cdf0e10cSrcweir 			if ( line.length() && line[0] == '[' )
109cdf0e10cSrcweir 			{
110cdf0e10cSrcweir 				line.erase( 0, 1 );
111cdf0e10cSrcweir 				string::size_type end = line.find( ']', 0 );
112cdf0e10cSrcweir 
113cdf0e10cSrcweir 				if ( string::npos != end )
114cdf0e10cSrcweir 					section = trim_string( line.substr( 0, end ) );
115cdf0e10cSrcweir 			}
116cdf0e10cSrcweir 			else
117cdf0e10cSrcweir 			{
118cdf0e10cSrcweir 
119cdf0e10cSrcweir 				string::size_type iEqualSign = line.find( '=', 0 );
120cdf0e10cSrcweir 
121cdf0e10cSrcweir 				if ( iEqualSign != string::npos )
122cdf0e10cSrcweir 				{
123cdf0e10cSrcweir 					string	keyname = line.substr( 0, iEqualSign );
124cdf0e10cSrcweir 					keyname = trim_string( keyname );
125cdf0e10cSrcweir 
126cdf0e10cSrcweir                     string  value = line.substr( iEqualSign + 1, string::npos );
127cdf0e10cSrcweir 					value = trim_string( value );
128cdf0e10cSrcweir 
129cdf0e10cSrcweir 					if (
130cdf0e10cSrcweir 						0 == strcasecmp( section.c_str(), pSectionName ) &&
131cdf0e10cSrcweir 						0 == strcasecmp( keyname.c_str(), pKeyName )
132cdf0e10cSrcweir 						 )
133cdf0e10cSrcweir 					{
134cdf0e10cSrcweir 						retValue = value;
135cdf0e10cSrcweir 						break;
136cdf0e10cSrcweir 					}
137cdf0e10cSrcweir 				}
138cdf0e10cSrcweir 			}
139cdf0e10cSrcweir 		}
140cdf0e10cSrcweir 
141cdf0e10cSrcweir 		fclose( fp );
142cdf0e10cSrcweir 	}
143cdf0e10cSrcweir 
144cdf0e10cSrcweir 	return retValue;
145cdf0e10cSrcweir }
146cdf0e10cSrcweir 
get_profile_bool(const char * pFileName,const char * pSectionName,const char * pKeyName)147cdf0e10cSrcweir static bool get_profile_bool( const char *pFileName, const char *pSectionName, const char *pKeyName )
148cdf0e10cSrcweir {
149cdf0e10cSrcweir 	string	str = get_profile_string( pFileName, pSectionName, pKeyName );
150cdf0e10cSrcweir 
151cdf0e10cSrcweir 	if ( !strcasecmp( str.c_str(), "true" ) )
152cdf0e10cSrcweir 		return true;
153cdf0e10cSrcweir 	return false;
154cdf0e10cSrcweir }
155cdf0e10cSrcweir 
get_profile_String(const char * pFileName,const char * pSectionName,const char * pKeyName,const char * =NULL)156cdf0e10cSrcweir static String get_profile_String( const char *pFileName, const char *pSectionName, const char *pKeyName, const char * = NULL )
157cdf0e10cSrcweir {
158cdf0e10cSrcweir 	string	str = get_profile_string( pFileName, pSectionName, pKeyName );
159cdf0e10cSrcweir 	String	result( str.c_str(), RTL_TEXTENCODING_UTF8 );
160cdf0e10cSrcweir 
161cdf0e10cSrcweir 	return result;
162cdf0e10cSrcweir }
163cdf0e10cSrcweir 
164cdf0e10cSrcweir namespace svx{
165cdf0e10cSrcweir     namespace DocRecovery{
166cdf0e10cSrcweir 
ReadParams()167cdf0e10cSrcweir 		bool ErrorRepSendDialog::ReadParams()
168cdf0e10cSrcweir 		{
169cdf0e10cSrcweir 			string	sRCFile = get_home_dir();
170cdf0e10cSrcweir 
171cdf0e10cSrcweir 			sRCFile += "/";
172cdf0e10cSrcweir 			sRCFile += string(RCFILE);
173cdf0e10cSrcweir 
174cdf0e10cSrcweir 			maEMailAddrED.SetText( get_profile_String( sRCFile.c_str(), "Options", "ReturnAddress" ) );
175cdf0e10cSrcweir 			maParams.maHTTPProxyServer = get_profile_String( sRCFile.c_str(), "Options", "ProxyServer" );
176cdf0e10cSrcweir 			maParams.maHTTPProxyPort = get_profile_String( sRCFile.c_str(), "Options", "ProxyPort" );
177cdf0e10cSrcweir 			maParams.miHTTPConnectionType = get_profile_bool( sRCFile.c_str(), "Options", "UseProxy" ) ? 2 : 1;
178cdf0e10cSrcweir 			maContactCB.Check( get_profile_bool( sRCFile.c_str(), "Options", "AllowContact" ) );
179cdf0e10cSrcweir 
180cdf0e10cSrcweir 			return true;
181cdf0e10cSrcweir 		}
182cdf0e10cSrcweir 
SaveParams()183cdf0e10cSrcweir 		bool ErrorRepSendDialog::SaveParams()
184cdf0e10cSrcweir 		{
185cdf0e10cSrcweir 			bool success = false;
186cdf0e10cSrcweir 			string	sRCFile = get_home_dir();
187cdf0e10cSrcweir 
188cdf0e10cSrcweir 			sRCFile += "/";
189cdf0e10cSrcweir 			sRCFile += string(RCFILE);
190cdf0e10cSrcweir 
191cdf0e10cSrcweir 			FILE *fp = fopen( sRCFile.c_str(), "w" );
192cdf0e10cSrcweir 
193cdf0e10cSrcweir 			if ( fp )
194cdf0e10cSrcweir 			{
195cdf0e10cSrcweir 				fprintf( fp, "[Options]\n" );
196cdf0e10cSrcweir 				fprintf( fp, "UseProxy=%s\n", 2 == maParams.miHTTPConnectionType ? "true" : "false" );
197cdf0e10cSrcweir 				fprintf( fp, "ProxyServer=%s\n", ByteString( maParams.maHTTPProxyServer, RTL_TEXTENCODING_UTF8 ).GetBuffer() );
198cdf0e10cSrcweir 				fprintf( fp, "ProxyPort=%s\n", ByteString( maParams.maHTTPProxyPort, RTL_TEXTENCODING_UTF8 ).GetBuffer() );
199cdf0e10cSrcweir 				fprintf( fp, "ReturnAddress=%s\n", ByteString( GetEMailAddress(), RTL_TEXTENCODING_UTF8 ).GetBuffer() );
200cdf0e10cSrcweir 				fprintf( fp, "AllowContact=%s\n", IsContactAllowed() ? "true" : "false" );
201cdf0e10cSrcweir 				fclose( fp );
202cdf0e10cSrcweir 			}
203cdf0e10cSrcweir 
204cdf0e10cSrcweir 			return success;
205cdf0e10cSrcweir 		}
206cdf0e10cSrcweir 
SendReport()207cdf0e10cSrcweir 		bool ErrorRepSendDialog::SendReport()
208cdf0e10cSrcweir 		{
209cdf0e10cSrcweir 			ByteString	strSubject( GetDocType(), RTL_TEXTENCODING_UTF8 );
210cdf0e10cSrcweir 
211cdf0e10cSrcweir #if defined( LINUX ) || defined (MACOSX )
212cdf0e10cSrcweir 			setenv( "ERRORREPORT_SUBJECT", strSubject.GetBuffer(), 1 );
213cdf0e10cSrcweir #else
214cdf0e10cSrcweir 			static ::rtl::OString	strEnvSubject = "ERRORREPORT_SUBJECT";
215cdf0e10cSrcweir 			strEnvSubject += "=";
216cdf0e10cSrcweir 			strEnvSubject += strSubject.GetBuffer();
217cdf0e10cSrcweir 			putenv( (char *)strEnvSubject.getStr() );
218cdf0e10cSrcweir #endif
219cdf0e10cSrcweir 
220cdf0e10cSrcweir 			char szBodyFile[L_tmpnam] = "";
221cdf0e10cSrcweir 			FILE *fp = fopen( tmpnam( szBodyFile ), "w" );
222cdf0e10cSrcweir 
223cdf0e10cSrcweir 			if ( fp )
224cdf0e10cSrcweir 			{
225cdf0e10cSrcweir 				ByteString	strUTF8( GetUsing(), RTL_TEXTENCODING_UTF8 );
226cdf0e10cSrcweir 
227cdf0e10cSrcweir 				fwrite( strUTF8.GetBuffer(), 1, strUTF8.Len(), fp );
228cdf0e10cSrcweir 				fclose( fp );
229cdf0e10cSrcweir #if defined( LINUX ) || defined (MACOSX)
230cdf0e10cSrcweir 				setenv( "ERRORREPORT_BODYFILE", szBodyFile, 1 );
231cdf0e10cSrcweir #else
232cdf0e10cSrcweir 			static ::rtl::OString	strEnvBodyFile = "ERRORREPORT_BODYFILE";
233cdf0e10cSrcweir 			strEnvBodyFile += "=";
234cdf0e10cSrcweir 			strEnvBodyFile += szBodyFile;
235cdf0e10cSrcweir 			putenv( (char *)strEnvBodyFile.getStr() );
236cdf0e10cSrcweir #endif
237cdf0e10cSrcweir 			}
238cdf0e10cSrcweir 
239cdf0e10cSrcweir             int ret = -1;
240cdf0e10cSrcweir             rtl::OUString path1(
241cdf0e10cSrcweir                 RTL_CONSTASCII_USTRINGPARAM(
242*910823aeSJürgen Schmidt                     "$OOO_BASE_DIR/program/crashrep"));
243cdf0e10cSrcweir             rtl::Bootstrap::expandMacros(path1);
244cdf0e10cSrcweir             rtl::OString path2;
245cdf0e10cSrcweir             if ((osl::FileBase::getSystemPathFromFileURL(path1, path1) ==
246cdf0e10cSrcweir                  osl::FileBase::E_None) &&
247cdf0e10cSrcweir                 path1.convertToString(
248cdf0e10cSrcweir                     &path2, osl_getThreadTextEncoding(),
249cdf0e10cSrcweir                     (RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR |
250cdf0e10cSrcweir                      RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR)))
251cdf0e10cSrcweir             {
252cdf0e10cSrcweir                 rtl::OStringBuffer cmd;
253cdf0e10cSrcweir                 tools::appendUnixShellWord(&cmd, path2);
254cdf0e10cSrcweir                 cmd.append(RTL_CONSTASCII_STRINGPARAM(" -debug -load -send -noui"));
255cdf0e10cSrcweir                 ret = system(cmd.getStr());
256cdf0e10cSrcweir             }
257cdf0e10cSrcweir 
258cdf0e10cSrcweir 			if ( szBodyFile[0] )
259cdf0e10cSrcweir 			{
260cdf0e10cSrcweir 				unlink( szBodyFile );
261cdf0e10cSrcweir 			}
262cdf0e10cSrcweir 
263cdf0e10cSrcweir 			return -1 != ret;
264cdf0e10cSrcweir 		}
265cdf0e10cSrcweir 
266cdf0e10cSrcweir 
267cdf0e10cSrcweir 	}	// namespace DocRecovery
268cdf0e10cSrcweir }	// namespace svx
269