bump product version to 4.1.6.2
[LibreOffice.git] / svx / source / dialog / sendreportunx.cxx
blob3271f6803f9c76b6e81c4c02e4eefdf2a542ae08
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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"
27 #include <string>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <pwd.h>
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 )
46 char szBuffer[1024];
47 bool bSuccess = false;
48 bool bEOL = false;
49 string line;
52 while ( !bEOL && fgets( szBuffer, sizeof(szBuffer), fp ) )
54 int len = strlen(szBuffer);
56 bSuccess = true;
58 while ( len && szBuffer[len - 1] == '\n' )
60 szBuffer[--len] = 0;
61 bEOL = true;
64 line.append( szBuffer );
67 rLine = line;
68 return bSuccess;
71 static string trim_string( const string& rString )
73 string temp = rString;
75 while ( temp.length() && (temp[0] == ' ' || temp[0] == '\t') )
76 temp.erase( 0, 1 );
78 string::size_type len = temp.length();
80 while ( len && (temp[len-1] == ' ' || temp[len-1] == '\t') )
82 temp.erase( len - 1, 1 );
83 len = temp.length();
86 return temp;
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 : "";
94 if ( fp )
96 string line;
97 string section;
99 while ( read_line( fp, line ) )
101 line = trim_string( line );
103 if ( line.length() && line[0] == '[' )
105 line.erase( 0, 1 );
106 string::size_type end = line.find( ']', 0 );
108 if ( string::npos != end )
109 section = trim_string( line.substr( 0, end ) );
111 else
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 );
124 if (
125 0 == strcasecmp( section.c_str(), pSectionName ) &&
126 0 == strcasecmp( keyname.c_str(), pKeyName )
129 retValue = value;
130 break;
136 fclose( fp );
139 return retValue;
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" ) )
147 return true;
148 return false;
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 );
156 return result;
159 namespace svx{
160 namespace DocRecovery{
162 bool ErrorRepSendDialog::ReadParams()
164 string sRCFile = get_home_dir();
166 sRCFile += "/";
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" ) );
175 return true;
178 bool ErrorRepSendDialog::SaveParams()
180 bool success = false;
181 string sRCFile = get_home_dir();
183 sRCFile += "/";
184 sRCFile += string(RCFILE);
186 FILE *fp = fopen( sRCFile.c_str(), "w" );
188 if ( fp )
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" );
196 fclose( fp );
199 return success;
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" );
211 if ( fp )
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()));
217 fclose( fp );
219 OUString sBodyEnvVar("ERRORREPORT_BODYFILE");
220 OUString strBodyFile(OStringToOUString(OString(szBodyFile),
221 osl_getThreadTextEncoding()));
222 osl_setEnvironment(sBodyEnvVar.pData, strBodyFile.pData);
225 int ret = -1;
226 OUString path1("$BRAND_BASE_DIR/program/crashrep");
227 rtl::Bootstrap::expandMacros(path1);
228 OString path2;
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)))
236 OStringBuffer cmd;
237 tools::appendUnixShellWord(&cmd, path2);
238 cmd.append(" -debug -load -send -noui");
239 ret = system(cmd.getStr());
242 if ( szBodyFile[0] )
244 unlink( szBodyFile );
247 return -1 != ret;
251 } // namespace DocRecovery
252 } // namespace svx
254 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */