1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: sendreportunx.cxx,v $
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"
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
)
59 bool bSuccess
= false;
64 while ( !bEOL
&& fgets( szBuffer
, sizeof(szBuffer
), fp
) )
66 int len
= strlen(szBuffer
);
70 while ( len
&& szBuffer
[len
- 1] == '\n' )
76 line
.append( szBuffer
);
83 static string
trim_string( const string
& rString
)
85 string temp
= rString
;
87 while ( temp
.length() && (temp
[0] == ' ' || temp
[0] == '\t') )
90 string::size_type len
= temp
.length();
92 while ( len
&& (temp
[len
-1] == ' ' || temp
[len
-1] == '\t') )
94 temp
.erase( len
- 1, 1 );
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
: "";
111 while ( read_line( fp
, line
) )
113 line
= trim_string( line
);
115 if ( line
.length() && line
[0] == '[' )
118 string::size_type end
= line
.find( ']', 0 );
120 if ( string::npos
!= end
)
121 section
= trim_string( line
.substr( 0, end
) );
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
);
137 0 == strcasecmp( section
.c_str(), pSectionName
) &&
138 0 == strcasecmp( keyname
.c_str(), pKeyName
)
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" ) )
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
);
172 namespace DocRecovery
{
174 bool ErrorRepSendDialog::ReadParams()
176 string sRCFile
= get_home_dir();
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" ) );
190 bool ErrorRepSendDialog::SaveParams()
192 bool success
= false;
193 string sRCFile
= get_home_dir();
196 sRCFile
+= string(RCFILE
);
198 FILE *fp
= fopen( sRCFile
.c_str(), "w" );
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" );
214 bool ErrorRepSendDialog::SendReport()
216 ByteString
strSubject( GetDocType(), RTL_TEXTENCODING_UTF8
);
218 #if defined( LINUX ) || defined (MACOSX )
219 setenv( "ERRORREPORT_SUBJECT", strSubject
.GetBuffer(), 1 );
221 static ::rtl::OString strEnvSubject
= "ERRORREPORT_SUBJECT";
222 strEnvSubject
+= "=";
223 strEnvSubject
+= strSubject
.GetBuffer();
224 putenv( (char *)strEnvSubject
.getStr() );
227 char szBodyFile
[L_tmpnam
] = "";
228 FILE *fp
= fopen( tmpnam( szBodyFile
), "w" );
232 ByteString
strUTF8( GetUsing(), RTL_TEXTENCODING_UTF8
);
234 fwrite( strUTF8
.GetBuffer(), 1, strUTF8
.Len(), fp
);
236 #if defined( LINUX ) || defined (MACOSX)
237 setenv( "ERRORREPORT_BODYFILE", szBodyFile
, 1 );
239 static ::rtl::OString strEnvBodyFile
= "ERRORREPORT_BODYFILE";
240 strEnvBodyFile
+= "=";
241 strEnvBodyFile
+= szBodyFile
;
242 putenv( (char *)strEnvBodyFile
.getStr() );
248 RTL_CONSTASCII_USTRINGPARAM(
249 "$BRAND_BASE_DIR/program/crash_report.bin"));
250 rtl::Bootstrap::expandMacros(path1
);
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());
267 unlink( szBodyFile
);
274 } // namespace DocRecovery