1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include "docrecovery.hxx"
22 #include "osl/file.hxx"
23 #include "osl/process.h"
24 #include "rtl/bootstrap.hxx"
25 #include "rtl/strbuf.hxx"
26 #include "tools/appendunixshellword.hxx"
33 #define RCFILE ".crash_reportrc"
35 using namespace ::std
;
37 static const char *get_home_dir()
39 struct passwd
*ppwd
= getpwuid( getuid() );
41 return ppwd
? (ppwd
->pw_dir
? ppwd
->pw_dir
: "/") : "/";
44 static bool read_line( FILE *fp
, string
& rLine
)
47 bool bSuccess
= false;
52 while ( !bEOL
&& fgets( szBuffer
, sizeof(szBuffer
), fp
) )
54 int len
= strlen(szBuffer
);
58 while ( len
&& szBuffer
[len
- 1] == '\n' )
64 line
.append( szBuffer
);
71 static string
trim_string( const string
& rString
)
73 string temp
= rString
;
75 while ( temp
.length() && (temp
[0] == ' ' || temp
[0] == '\t') )
78 string::size_type len
= temp
.length();
80 while ( len
&& (temp
[len
-1] == ' ' || temp
[len
-1] == '\t') )
82 temp
.erase( len
- 1, 1 );
89 static string
get_profile_string( const char *pFileName
, const char *pSectionName
, const char *pKeyName
, const char *pDefault
= NULL
)
91 FILE *fp
= fopen( pFileName
, "r" );
92 string retValue
= pDefault
? pDefault
: "";
99 while ( read_line( fp
, line
) )
101 line
= trim_string( line
);
103 if ( line
.length() && line
[0] == '[' )
106 string::size_type end
= line
.find( ']', 0 );
108 if ( string::npos
!= end
)
109 section
= trim_string( line
.substr( 0, end
) );
114 string::size_type iEqualSign
= line
.find( '=', 0 );
116 if ( iEqualSign
!= string::npos
)
118 string keyname
= line
.substr( 0, iEqualSign
);
119 keyname
= trim_string( keyname
);
121 string value
= line
.substr( iEqualSign
+ 1, string::npos
);
122 value
= trim_string( value
);
125 0 == strcasecmp( section
.c_str(), pSectionName
) &&
126 0 == strcasecmp( keyname
.c_str(), pKeyName
)
142 static bool get_profile_bool( const char *pFileName
, const char *pSectionName
, const char *pKeyName
)
144 string str
= get_profile_string( pFileName
, pSectionName
, pKeyName
);
146 if ( !strcasecmp( str
.c_str(), "true" ) )
151 static String
get_profile_String( const char *pFileName
, const char *pSectionName
, const char *pKeyName
, const char * = NULL
)
153 string str
= get_profile_string( pFileName
, pSectionName
, pKeyName
);
154 String
result( str
.c_str(), RTL_TEXTENCODING_UTF8
);
160 namespace DocRecovery
{
162 bool ErrorRepSendDialog::ReadParams()
164 string sRCFile
= get_home_dir();
167 sRCFile
+= string(RCFILE
);
169 maEMailAddrED
.SetText( get_profile_String( sRCFile
.c_str(), "Options", "ReturnAddress" ) );
170 maParams
.maHTTPProxyServer
= get_profile_String( sRCFile
.c_str(), "Options", "ProxyServer" );
171 maParams
.maHTTPProxyPort
= get_profile_String( sRCFile
.c_str(), "Options", "ProxyPort" );
172 maParams
.miHTTPConnectionType
= get_profile_bool( sRCFile
.c_str(), "Options", "UseProxy" ) ? 2 : 1;
173 maContactCB
.Check( get_profile_bool( sRCFile
.c_str(), "Options", "AllowContact" ) );
178 bool ErrorRepSendDialog::SaveParams()
180 bool success
= false;
181 string sRCFile
= get_home_dir();
184 sRCFile
+= string(RCFILE
);
186 FILE *fp
= fopen( sRCFile
.c_str(), "w" );
190 fprintf( fp
, "[Options]\n" );
191 fprintf( fp
, "UseProxy=%s\n", 2 == maParams
.miHTTPConnectionType
? "true" : "false" );
192 fprintf( fp
, "ProxyServer=%s\n", OUStringToOString( maParams
.maHTTPProxyServer
, RTL_TEXTENCODING_UTF8
).getStr() );
193 fprintf( fp
, "ProxyPort=%s\n", OUStringToOString( maParams
.maHTTPProxyPort
, RTL_TEXTENCODING_UTF8
).getStr() );
194 fprintf( fp
, "ReturnAddress=%s\n", OUStringToOString( GetEMailAddress(), RTL_TEXTENCODING_UTF8
).getStr() );
195 fprintf( fp
, "AllowContact=%s\n", IsContactAllowed() ? "true" : "false" );
202 bool ErrorRepSendDialog::SendReport()
204 OUString
sSubEnvVar("ERRORREPORT_SUBJECT");
205 OUString
strSubject(GetDocType());
206 osl_setEnvironment(sSubEnvVar
.pData
, strSubject
.pData
);
208 char szBodyFile
[L_tmpnam
] = "";
209 FILE *fp
= fopen( tmpnam( szBodyFile
), "w" );
213 OString
strUTF8(OUStringToOString(GetUsing(), RTL_TEXTENCODING_UTF8
));
215 size_t nWritten
= fwrite(strUTF8
.getStr(), 1, strUTF8
.getLength(), fp
);
216 OSL_VERIFY(nWritten
== static_cast<size_t>(strUTF8
.getLength()));
219 OUString
sBodyEnvVar("ERRORREPORT_BODYFILE");
220 OUString
strBodyFile(OStringToOUString(OString(szBodyFile
),
221 osl_getThreadTextEncoding()));
222 osl_setEnvironment(sBodyEnvVar
.pData
, strBodyFile
.pData
);
226 OUString
path1("$BRAND_BASE_DIR/program/crashrep");
227 rtl::Bootstrap::expandMacros(path1
);
229 if ((osl::FileBase::getSystemPathFromFileURL(path1
, path1
) ==
230 osl::FileBase::E_None
) &&
231 path1
.convertToString(
232 &path2
, osl_getThreadTextEncoding(),
233 (RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR
|
234 RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR
)))
237 tools::appendUnixShellWord(&cmd
, path2
);
238 cmd
.append(" -debug -load -send -noui");
239 ret
= system(cmd
.getStr());
244 unlink( szBodyFile
);
251 } // namespace DocRecovery
254 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */