summaryrefslogtreecommitdiff
path: root/svx/source/dialog/sendreportunx.cxx
blob: d59bc9bb1cbad8c5bdba90d0695048054393c8dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2000, 2010 Oracle and/or its affiliates.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/


// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_svx.hxx"
#include "docrecovery.hxx"
#include "osl/file.hxx"
#include "rtl/bootstrap.hxx"
#include "rtl/strbuf.hxx"
#include "tools/appendunixshellword.hxx"
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pwd.h>

#define RCFILE ".crash_reportrc"

using namespace ::std;

static const char *get_home_dir()
{
    struct passwd *ppwd = getpwuid( getuid() );

    return ppwd ? (ppwd->pw_dir ? ppwd->pw_dir : "/") : "/";
}

static bool read_line( FILE *fp, string& rLine )
{
    char szBuffer[1024];
    bool bSuccess = false;
    bool bEOL = false;
    string  line;


    while ( !bEOL && fgets( szBuffer, sizeof(szBuffer), fp ) )
    {
        int len = strlen(szBuffer);

        bSuccess = true;

        while ( len && szBuffer[len - 1] == '\n' )
        {
            szBuffer[--len] = 0;
            bEOL = true;
        }

        line.append( szBuffer );
    }

    rLine = line;
    return bSuccess;
}

static string trim_string( const string& rString )
{
    string temp = rString;

    while ( temp.length() && (temp[0] == ' ' || temp[0] == '\t') )
        temp.erase( 0, 1 );

    string::size_type   len = temp.length();

    while ( len && (temp[len-1] == ' ' || temp[len-1] == '\t') )
    {
        temp.erase( len - 1, 1 );
        len = temp.length();
    }

    return temp;
}

static string get_profile_string( const char *pFileName, const char *pSectionName, const char *pKeyName, const char *pDefault = NULL )
{
    FILE    *fp = fopen( pFileName, "r" );
    string  retValue = pDefault ? pDefault : "";

    if ( fp )
    {
        string line;
        string section;

        while ( read_line( fp, line ) )
        {
            line = trim_string( line );

            if ( line.length() && line[0] == '[' )
            {
                line.erase( 0, 1 );
                string::size_type end = line.find( ']', 0 );

                if ( string::npos != end )
                    section = trim_string( line.substr( 0, end ) );
            }
            else
            {

                string::size_type iEqualSign = line.find( '=', 0 );

                if ( iEqualSign != string::npos )
                {
                    string  keyname = line.substr( 0, iEqualSign );
                    keyname = trim_string( keyname );

                    string  value = line.substr( iEqualSign + 1, string::npos );
                    value = trim_string( value );

                    if (
                        0 == strcasecmp( section.c_str(), pSectionName ) &&
                        0 == strcasecmp( keyname.c_str(), pKeyName )
                         )
                    {
                        retValue = value;
                        break;
                    }
                }
            }
        }

        fclose( fp );
    }

    return retValue;
}

static bool get_profile_bool( const char *pFileName, const char *pSectionName, const char *pKeyName )
{
    string  str = get_profile_string( pFileName, pSectionName, pKeyName );

    if ( !strcasecmp( str.c_str(), "true" ) )
        return true;
    return false;
}

static String get_profile_String( const char *pFileName, const char *pSectionName, const char *pKeyName, const char * = NULL )
{
    string  str = get_profile_string( pFileName, pSectionName, pKeyName );
    String  result( str.c_str(), RTL_TEXTENCODING_UTF8 );

    return result;
}

namespace svx{
    namespace DocRecovery{

        bool ErrorRepSendDialog::ReadParams()
        {
            string  sRCFile = get_home_dir();

            sRCFile += "/";
            sRCFile += string(RCFILE);

            maEMailAddrED.SetText( get_profile_String( sRCFile.c_str(), "Options", "ReturnAddress" ) );
            maParams.maHTTPProxyServer = get_profile_String( sRCFile.c_str(), "Options", "ProxyServer" );
            maParams.maHTTPProxyPort = get_profile_String( sRCFile.c_str(), "Options", "ProxyPort" );
            maParams.miHTTPConnectionType = get_profile_bool( sRCFile.c_str(), "Options", "UseProxy" ) ? 2 : 1;
            maContactCB.Check( get_profile_bool( sRCFile.c_str(), "Options", "AllowContact" ) );

            return true;
        }

        bool ErrorRepSendDialog::SaveParams()
        {
            bool success = false;
            string  sRCFile = get_home_dir();

            sRCFile += "/";
            sRCFile += string(RCFILE);

            FILE *fp = fopen( sRCFile.c_str(), "w" );

            if ( fp )
            {
                fprintf( fp, "[Options]\n" );
                fprintf( fp, "UseProxy=%s\n", 2 == maParams.miHTTPConnectionType ? "true" : "false" );
                fprintf( fp, "ProxyServer=%s\n", ByteString( maParams.maHTTPProxyServer, RTL_TEXTENCODING_UTF8 ).GetBuffer() );
                fprintf( fp, "ProxyPort=%s\n", ByteString( maParams.maHTTPProxyPort, RTL_TEXTENCODING_UTF8 ).GetBuffer() );
                fprintf( fp, "ReturnAddress=%s\n", ByteString( GetEMailAddress(), RTL_TEXTENCODING_UTF8 ).GetBuffer() );
                fprintf( fp, "AllowContact=%s\n", IsContactAllowed() ? "true" : "false" );
                fclose( fp );
            }

            return success;
        }

        bool ErrorRepSendDialog::SendReport()
        {
            ByteString  strSubject( GetDocType(), RTL_TEXTENCODING_UTF8 );

#if defined( LINUX ) || defined (MACOSX )
            setenv( "ERRORREPORT_SUBJECT", strSubject.GetBuffer(), 1 );
#else
            static ::rtl::OString   strEnvSubject = "ERRORREPORT_SUBJECT";
            strEnvSubject += "=";
            strEnvSubject += strSubject.GetBuffer();
            putenv( (char *)strEnvSubject.getStr() );
#endif

            char szBodyFile[L_tmpnam] = "";
            FILE *fp = fopen( tmpnam( szBodyFile ), "w" );

            if ( fp )
            {
                ByteString  strUTF8( GetUsing(), RTL_TEXTENCODING_UTF8 );

                fwrite( strUTF8.GetBuffer(), 1, strUTF8.Len(), fp );
                fclose( fp );
#if defined( LINUX ) || defined (MACOSX)
                setenv( "ERRORREPORT_BODYFILE", szBodyFile, 1 );
#else
            static ::rtl::OString   strEnvBodyFile = "ERRORREPORT_BODYFILE";
            strEnvBodyFile += "=";
            strEnvBodyFile += szBodyFile;
            putenv( (char *)strEnvBodyFile.getStr() );
#endif
            }

            int ret = -1;
            rtl::OUString path1(
                RTL_CONSTASCII_USTRINGPARAM(
                    "$BRAND_BASE_DIR/program/crashrep"));
            rtl::Bootstrap::expandMacros(path1);
            rtl::OString path2;
            if ((osl::FileBase::getSystemPathFromFileURL(path1, path1) ==
                 osl::FileBase::E_None) &&
                path1.convertToString(
                    &path2, osl_getThreadTextEncoding(),
                    (RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR |
                     RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR)))
            {
                rtl::OStringBuffer cmd;
                tools::appendUnixShellWord(&cmd, path2);
                cmd.append(RTL_CONSTASCII_STRINGPARAM(" -debug -load -send -noui"));
                ret = system(cmd.getStr());
            }

            if ( szBodyFile[0] )
            {
                unlink( szBodyFile );
            }

            return -1 != ret;
        }


    }   // namespace DocRecovery
}   // namespace svx