Update ooo320-m1
[ooovba.git] / svx / source / dialog / sendreportunx.cxx
blob2a1c0b860a87c7cb910c92b75e9c0210cd7614d3
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: sendreportunx.cxx,v $
10 * $Revision: 1.9 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
32 // MARKER(update_precomp.py): autogen include statement, do not remove
33 #include "precompiled_svx.hxx"
34 #include "docrecovery.hxx"
35 #include "osl/file.hxx"
36 #include "rtl/bootstrap.hxx"
37 #include "rtl/strbuf.hxx"
38 #include "tools/appendunixshellword.hxx"
39 #include <string>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <unistd.h>
43 #include <pwd.h>
45 #define RCFILE ".crash_reportrc"
47 using namespace ::std;
49 static const char *get_home_dir()
51 struct passwd *ppwd = getpwuid( getuid() );
53 return ppwd ? (ppwd->pw_dir ? ppwd->pw_dir : "/") : "/";
56 static bool read_line( FILE *fp, string& rLine )
58 char szBuffer[1024];
59 bool bSuccess = false;
60 bool bEOL = false;
61 string line;
64 while ( !bEOL && fgets( szBuffer, sizeof(szBuffer), fp ) )
66 int len = strlen(szBuffer);
68 bSuccess = true;
70 while ( len && szBuffer[len - 1] == '\n' )
72 szBuffer[--len] = 0;
73 bEOL = true;
76 line.append( szBuffer );
79 rLine = line;
80 return bSuccess;
83 static string trim_string( const string& rString )
85 string temp = rString;
87 while ( temp.length() && (temp[0] == ' ' || temp[0] == '\t') )
88 temp.erase( 0, 1 );
90 string::size_type len = temp.length();
92 while ( len && (temp[len-1] == ' ' || temp[len-1] == '\t') )
94 temp.erase( len - 1, 1 );
95 len = temp.length();
98 return temp;
101 static string get_profile_string( const char *pFileName, const char *pSectionName, const char *pKeyName, const char *pDefault = NULL )
103 FILE *fp = fopen( pFileName, "r" );
104 string retValue = pDefault ? pDefault : "";
106 if ( fp )
108 string line;
109 string section;
111 while ( read_line( fp, line ) )
113 line = trim_string( line );
115 if ( line.length() && line[0] == '[' )
117 line.erase( 0, 1 );
118 string::size_type end = line.find( ']', 0 );
120 if ( string::npos != end )
121 section = trim_string( line.substr( 0, end ) );
123 else
126 string::size_type iEqualSign = line.find( '=', 0 );
128 if ( iEqualSign != string::npos )
130 string keyname = line.substr( 0, iEqualSign );
131 keyname = trim_string( keyname );
133 string value = line.substr( iEqualSign + 1, string::npos );
134 value = trim_string( value );
136 if (
137 0 == strcasecmp( section.c_str(), pSectionName ) &&
138 0 == strcasecmp( keyname.c_str(), pKeyName )
141 retValue = value;
142 break;
148 fclose( fp );
151 return retValue;
154 static bool get_profile_bool( const char *pFileName, const char *pSectionName, const char *pKeyName )
156 string str = get_profile_string( pFileName, pSectionName, pKeyName );
158 if ( !strcasecmp( str.c_str(), "true" ) )
159 return true;
160 return false;
163 static String get_profile_String( const char *pFileName, const char *pSectionName, const char *pKeyName, const char * = NULL )
165 string str = get_profile_string( pFileName, pSectionName, pKeyName );
166 String result( str.c_str(), RTL_TEXTENCODING_UTF8 );
168 return result;
171 namespace svx{
172 namespace DocRecovery{
174 bool ErrorRepSendDialog::ReadParams()
176 string sRCFile = get_home_dir();
178 sRCFile += "/";
179 sRCFile += string(RCFILE);
181 maEMailAddrED.SetText( get_profile_String( sRCFile.c_str(), "Options", "ReturnAddress" ) );
182 maParams.maHTTPProxyServer = get_profile_String( sRCFile.c_str(), "Options", "ProxyServer" );
183 maParams.maHTTPProxyPort = get_profile_String( sRCFile.c_str(), "Options", "ProxyPort" );
184 maParams.miHTTPConnectionType = get_profile_bool( sRCFile.c_str(), "Options", "UseProxy" ) ? 2 : 1;
185 maContactCB.Check( get_profile_bool( sRCFile.c_str(), "Options", "AllowContact" ) );
187 return true;
190 bool ErrorRepSendDialog::SaveParams()
192 bool success = false;
193 string sRCFile = get_home_dir();
195 sRCFile += "/";
196 sRCFile += string(RCFILE);
198 FILE *fp = fopen( sRCFile.c_str(), "w" );
200 if ( fp )
202 fprintf( fp, "[Options]\n" );
203 fprintf( fp, "UseProxy=%s\n", 2 == maParams.miHTTPConnectionType ? "true" : "false" );
204 fprintf( fp, "ProxyServer=%s\n", ByteString( maParams.maHTTPProxyServer, RTL_TEXTENCODING_UTF8 ).GetBuffer() );
205 fprintf( fp, "ProxyPort=%s\n", ByteString( maParams.maHTTPProxyPort, RTL_TEXTENCODING_UTF8 ).GetBuffer() );
206 fprintf( fp, "ReturnAddress=%s\n", ByteString( GetEMailAddress(), RTL_TEXTENCODING_UTF8 ).GetBuffer() );
207 fprintf( fp, "AllowContact=%s\n", IsContactAllowed() ? "true" : "false" );
208 fclose( fp );
211 return success;
214 bool ErrorRepSendDialog::SendReport()
216 ByteString strSubject( GetDocType(), RTL_TEXTENCODING_UTF8 );
218 #if defined( LINUX ) || defined (MACOSX )
219 setenv( "ERRORREPORT_SUBJECT", strSubject.GetBuffer(), 1 );
220 #else
221 static ::rtl::OString strEnvSubject = "ERRORREPORT_SUBJECT";
222 strEnvSubject += "=";
223 strEnvSubject += strSubject.GetBuffer();
224 putenv( (char *)strEnvSubject.getStr() );
225 #endif
227 char szBodyFile[L_tmpnam] = "";
228 FILE *fp = fopen( tmpnam( szBodyFile ), "w" );
230 if ( fp )
232 ByteString strUTF8( GetUsing(), RTL_TEXTENCODING_UTF8 );
234 fwrite( strUTF8.GetBuffer(), 1, strUTF8.Len(), fp );
235 fclose( fp );
236 #if defined( LINUX ) || defined (MACOSX)
237 setenv( "ERRORREPORT_BODYFILE", szBodyFile, 1 );
238 #else
239 static ::rtl::OString strEnvBodyFile = "ERRORREPORT_BODYFILE";
240 strEnvBodyFile += "=";
241 strEnvBodyFile += szBodyFile;
242 putenv( (char *)strEnvBodyFile.getStr() );
243 #endif
246 int ret = -1;
247 rtl::OUString path1(
248 RTL_CONSTASCII_USTRINGPARAM(
249 "$BRAND_BASE_DIR/program/crash_report.bin"));
250 rtl::Bootstrap::expandMacros(path1);
251 rtl::OString path2;
252 if ((osl::FileBase::getSystemPathFromFileURL(path1, path1) ==
253 osl::FileBase::E_None) &&
254 path1.convertToString(
255 &path2, osl_getThreadTextEncoding(),
256 (RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR |
257 RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR)))
259 rtl::OStringBuffer cmd;
260 tools::appendUnixShellWord(&cmd, path2);
261 cmd.append(RTL_CONSTASCII_STRINGPARAM(" -debug -load -send -noui"));
262 ret = system(cmd.getStr());
265 if ( szBodyFile[0] )
267 unlink( szBodyFile );
270 return -1 != ret;
274 } // namespace DocRecovery
275 } // namespace svx