xref: /trunk/main/svx/source/dialog/sendreportunx.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 
29 // MARKER(update_precomp.py): autogen include statement, do not remove
30 #include "precompiled_svx.hxx"
31 #include "docrecovery.hxx"
32 #include "osl/file.hxx"
33 #include "rtl/bootstrap.hxx"
34 #include "rtl/strbuf.hxx"
35 #include "tools/appendunixshellword.hxx"
36 #include <string>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <unistd.h>
40 #include <pwd.h>
41 
42 #define RCFILE ".crash_reportrc"
43 
44 using namespace ::std;
45 
46 static const char *get_home_dir()
47 {
48     struct passwd *ppwd = getpwuid( getuid() );
49 
50     return ppwd ? (ppwd->pw_dir ? ppwd->pw_dir : "/") : "/";
51 }
52 
53 static bool read_line( FILE *fp, string& rLine )
54 {
55     char szBuffer[1024];
56     bool bSuccess = false;
57     bool bEOL = false;
58     string  line;
59 
60 
61     while ( !bEOL && fgets( szBuffer, sizeof(szBuffer), fp ) )
62     {
63         int len = strlen(szBuffer);
64 
65         bSuccess = true;
66 
67         while ( len && szBuffer[len - 1] == '\n' )
68         {
69             szBuffer[--len] = 0;
70             bEOL = true;
71         }
72 
73         line.append( szBuffer );
74     }
75 
76     rLine = line;
77     return bSuccess;
78 }
79 
80 static string trim_string( const string& rString )
81 {
82     string temp = rString;
83 
84     while ( temp.length() && (temp[0] == ' ' || temp[0] == '\t') )
85         temp.erase( 0, 1 );
86 
87     string::size_type   len = temp.length();
88 
89     while ( len && (temp[len-1] == ' ' || temp[len-1] == '\t') )
90     {
91         temp.erase( len - 1, 1 );
92         len = temp.length();
93     }
94 
95     return temp;
96 }
97 
98 static string get_profile_string( const char *pFileName, const char *pSectionName, const char *pKeyName, const char *pDefault = NULL )
99 {
100     FILE    *fp = fopen( pFileName, "r" );
101     string  retValue = pDefault ? pDefault : "";
102 
103     if ( fp )
104     {
105         string line;
106         string section;
107 
108         while ( read_line( fp, line ) )
109         {
110             line = trim_string( line );
111 
112             if ( line.length() && line[0] == '[' )
113             {
114                 line.erase( 0, 1 );
115                 string::size_type end = line.find( ']', 0 );
116 
117                 if ( string::npos != end )
118                     section = trim_string( line.substr( 0, end ) );
119             }
120             else
121             {
122 
123                 string::size_type iEqualSign = line.find( '=', 0 );
124 
125                 if ( iEqualSign != string::npos )
126                 {
127                     string  keyname = line.substr( 0, iEqualSign );
128                     keyname = trim_string( keyname );
129 
130                     string  value = line.substr( iEqualSign + 1, string::npos );
131                     value = trim_string( value );
132 
133                     if (
134                         0 == strcasecmp( section.c_str(), pSectionName ) &&
135                         0 == strcasecmp( keyname.c_str(), pKeyName )
136                          )
137                     {
138                         retValue = value;
139                         break;
140                     }
141                 }
142             }
143         }
144 
145         fclose( fp );
146     }
147 
148     return retValue;
149 }
150 
151 static bool get_profile_bool( const char *pFileName, const char *pSectionName, const char *pKeyName )
152 {
153     string  str = get_profile_string( pFileName, pSectionName, pKeyName );
154 
155     if ( !strcasecmp( str.c_str(), "true" ) )
156         return true;
157     return false;
158 }
159 
160 static String get_profile_String( const char *pFileName, const char *pSectionName, const char *pKeyName, const char * = NULL )
161 {
162     string  str = get_profile_string( pFileName, pSectionName, pKeyName );
163     String  result( str.c_str(), RTL_TEXTENCODING_UTF8 );
164 
165     return result;
166 }
167 
168 namespace svx{
169     namespace DocRecovery{
170 
171         bool ErrorRepSendDialog::ReadParams()
172         {
173             string  sRCFile = get_home_dir();
174 
175             sRCFile += "/";
176             sRCFile += string(RCFILE);
177 
178             maEMailAddrED.SetText( get_profile_String( sRCFile.c_str(), "Options", "ReturnAddress" ) );
179             maParams.maHTTPProxyServer = get_profile_String( sRCFile.c_str(), "Options", "ProxyServer" );
180             maParams.maHTTPProxyPort = get_profile_String( sRCFile.c_str(), "Options", "ProxyPort" );
181             maParams.miHTTPConnectionType = get_profile_bool( sRCFile.c_str(), "Options", "UseProxy" ) ? 2 : 1;
182             maContactCB.Check( get_profile_bool( sRCFile.c_str(), "Options", "AllowContact" ) );
183 
184             return true;
185         }
186 
187         bool ErrorRepSendDialog::SaveParams()
188         {
189             bool success = false;
190             string  sRCFile = get_home_dir();
191 
192             sRCFile += "/";
193             sRCFile += string(RCFILE);
194 
195             FILE *fp = fopen( sRCFile.c_str(), "w" );
196 
197             if ( fp )
198             {
199                 fprintf( fp, "[Options]\n" );
200                 fprintf( fp, "UseProxy=%s\n", 2 == maParams.miHTTPConnectionType ? "true" : "false" );
201                 fprintf( fp, "ProxyServer=%s\n", ByteString( maParams.maHTTPProxyServer, RTL_TEXTENCODING_UTF8 ).GetBuffer() );
202                 fprintf( fp, "ProxyPort=%s\n", ByteString( maParams.maHTTPProxyPort, RTL_TEXTENCODING_UTF8 ).GetBuffer() );
203                 fprintf( fp, "ReturnAddress=%s\n", ByteString( GetEMailAddress(), RTL_TEXTENCODING_UTF8 ).GetBuffer() );
204                 fprintf( fp, "AllowContact=%s\n", IsContactAllowed() ? "true" : "false" );
205                 fclose( fp );
206             }
207 
208             return success;
209         }
210 
211         bool ErrorRepSendDialog::SendReport()
212         {
213             ByteString  strSubject( GetDocType(), RTL_TEXTENCODING_UTF8 );
214 
215 #if defined( LINUX ) || defined (MACOSX )
216             setenv( "ERRORREPORT_SUBJECT", strSubject.GetBuffer(), 1 );
217 #else
218             static ::rtl::OString   strEnvSubject = "ERRORREPORT_SUBJECT";
219             strEnvSubject += "=";
220             strEnvSubject += strSubject.GetBuffer();
221             putenv( (char *)strEnvSubject.getStr() );
222 #endif
223 
224             char szBodyFile[L_tmpnam] = "";
225             FILE *fp = fopen( tmpnam( szBodyFile ), "w" );
226 
227             if ( fp )
228             {
229                 ByteString  strUTF8( GetUsing(), RTL_TEXTENCODING_UTF8 );
230 
231                 fwrite( strUTF8.GetBuffer(), 1, strUTF8.Len(), fp );
232                 fclose( fp );
233 #if defined( LINUX ) || defined (MACOSX)
234                 setenv( "ERRORREPORT_BODYFILE", szBodyFile, 1 );
235 #else
236             static ::rtl::OString   strEnvBodyFile = "ERRORREPORT_BODYFILE";
237             strEnvBodyFile += "=";
238             strEnvBodyFile += szBodyFile;
239             putenv( (char *)strEnvBodyFile.getStr() );
240 #endif
241             }
242 
243             int ret = -1;
244             rtl::OUString path1(
245                 RTL_CONSTASCII_USTRINGPARAM(
246                     "$BRAND_BASE_DIR/program/crashrep"));
247             rtl::Bootstrap::expandMacros(path1);
248             rtl::OString path2;
249             if ((osl::FileBase::getSystemPathFromFileURL(path1, path1) ==
250                  osl::FileBase::E_None) &&
251                 path1.convertToString(
252                     &path2, osl_getThreadTextEncoding(),
253                     (RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR |
254                      RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR)))
255             {
256                 rtl::OStringBuffer cmd;
257                 tools::appendUnixShellWord(&cmd, path2);
258                 cmd.append(RTL_CONSTASCII_STRINGPARAM(" -debug -load -send -noui"));
259                 ret = system(cmd.getStr());
260             }
261 
262             if ( szBodyFile[0] )
263             {
264                 unlink( szBodyFile );
265             }
266 
267             return -1 != ret;
268         }
269 
270 
271     }   // namespace DocRecovery
272 }   // namespace svx
273