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