summaryrefslogtreecommitdiffstats
path: root/kbugbuster/backend/bugdetails.cpp
blob: d2c4f3bff1a0ba91d415739171d1d51653e46b4a (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

#include "bugdetails.h"

#include "bugdetailsimpl.h"
#include <tqstringlist.h>
#include <kdebug.h>
#include <kmdcodec.h>
#include <kmessagebox.h>
#include <tqregexp.h>

BugDetails::BugDetails()
{
}

BugDetails::BugDetails( BugDetailsImpl *impl ) :
    m_impl( impl )
{
}

BugDetails::BugDetails( const BugDetails &other )
{
    (*this) = other;
}

BugDetails &BugDetails::operator=( const BugDetails &rhs )
{
    m_impl = rhs.m_impl;
    return *this;
}

BugDetails::~BugDetails()
{
}

TQString BugDetails::version() const
{
    if ( !m_impl )
        return TQString();

    return m_impl->version;
}

TQString BugDetails::source() const
{
    if ( !m_impl )
        return TQString();

    return m_impl->source;
}

TQString BugDetails::compiler() const
{
    if ( !m_impl )
        return TQString();

    return m_impl->compiler;
}

TQString BugDetails::os() const
{
    if ( !m_impl )
        return TQString();

    return m_impl->os;
}

TQDateTime BugDetails::submissionDate() const
{
    if ( !m_impl ) return TQDateTime();

    if ( m_impl->parts.count() > 0 ) {
        return m_impl->parts.last().date;
    }
    return TQDateTime();
}

int BugDetails::age() const
{
   if ( !m_impl )
       return 0;

   return submissionDate().daysTo( TQDateTime::currentDateTime() );
}

BugDetailsPart::List BugDetails::parts() const
{
    if ( !m_impl )
        return BugDetailsPart::List();

    return m_impl->parts;
}

TQValueList<BugDetailsImpl::AttachmentDetails> BugDetails::attachmentDetails() const
{
    if ( m_impl )
        return m_impl->attachments;
    else
        return TQValueList<BugDetailsImpl::AttachmentDetails>();
}

void BugDetails::addAttachmentDetails( const TQValueList<BugDetailsImpl::AttachmentDetails>& attch )
{
    if ( m_impl )
        m_impl->attachments =  attch;
}

TQValueList<BugDetails::Attachment> BugDetails::extractAttachments() const
{
    TQValueList<BugDetails::Attachment> lst;
    if ( !m_impl )
        return lst;
    BugDetailsPart::List parts = m_impl->parts;
    for ( BugDetailsPart::List::ConstIterator it = parts.begin(); it != parts.end(); ++it ) {
        lst += extractAttachments( (*it).text );
    }
    return lst;
}

//#define DEBUG_EXTRACT

TQValueList<BugDetails::Attachment> BugDetails::extractAttachments( const TQString& text )
{
    TQValueList<BugDetails::Attachment> lst;
    TQStringList lines = TQStringList::split( '\n', text );
#ifdef DEBUG_EXTRACT
    kdDebug() << k_funcinfo << lines.count() << " lines." << endl;
#endif
    TQString boundary;
    for ( TQStringList::Iterator it = lines.begin() ; it != lines.end() ; ++it )
    {
#ifdef DEBUG_EXTRACT
        kdDebug() << "Line: " << *it << endl;
#endif
        if ( (*it).startsWith( " Content-Type" ) ) // ## leading space comes from khtml
        {
#ifdef DEBUG_EXTRACT
            //kdDebug() << "BugDetails::extractAttachments going back, looking for empty or boundary=" << boundary << endl;
#endif
            // Rewind until last empty line
            TQStringList::Iterator rit = it;
            for ( ; rit != lines.begin() ; --rit ) {
                TQString line = *rit;
                if ( line.endsWith( "<br />" ) )
                    line = line.left( line.length() - 6 );
                while ( !line.isEmpty() && line[0] == ' ' )
                    line.remove( 0, 1 );
#ifdef DEBUG_EXTRACT
                kdDebug() << "Back: '" << line << "'" << endl;
#endif
                if ( line.isEmpty() ) {
                    ++rit; // boundary is next line
                    boundary = *rit;
                    if ( boundary.endsWith( "<br />" ) )
                        boundary = boundary.left( boundary.length() - 6 );
                    if ( boundary[0] == ' ' )
                        boundary.remove( 0, 1 );
                    kdDebug() << "BugDetails::extractAttachments boundary=" << boundary << endl;
                    break;
                }
                if ( line == boundary )
                    break;
            }
            // Forward until next empty line (end of headers) - and parse filename
            TQString filename;
            TQString encoding;
            rit = it;
            for ( ; rit != lines.end() ; ++rit ) {
                TQString header = *rit;
                if ( header.endsWith( "<br />" ) )
                    header = header.left( header.length() - 6 );
                if ( header[0] == ' ' )
                    header.remove( 0, 1 );
#ifdef DEBUG_EXTRACT
                kdDebug() << "Header: '" << header << "'" << endl;
#endif
                if ( header.isEmpty() )
                    break;
                if ( header.startsWith( "Content-Disposition:" ) )
                {
#ifdef DEBUG_EXTRACT
                    kdDebug() << "BugDetails::extractAttachments found header " << *rit << endl;
#endif
                    // Taken from libkdenetwork/kmime_headers.cpp
                    int pos=header.find("filename=", 0, false);
                    TQString fn;
                    if(pos>-1) {
                        pos+=9;
                        fn=header.mid(pos, header.length()-pos);
                        if ( fn.startsWith( "\"" ) && fn.endsWith( "\"" ) )
                            fn = fn.mid( 1, fn.length() - 2 );
                        //filename=decodeRFC2047String(fn, &e_ncCS, defaultCS(), forceCS());
                        filename = fn; // hack
                        kdDebug() << "BugDetails::extractAttachments filename=" << filename << endl;
                    }

                }
                else if ( header.startsWith( "Content-Transfer-Encoding:" ) )
                {
                    encoding = header.mid( 26 ).lower();
                    while ( encoding[0] == ' ' )
                        encoding.remove( 0, 1 );
                    kdDebug() << "BugDetails::extractAttachments encoding=" << encoding << endl;
                }
            } //for
            if ( rit == lines.end() )
                break;

            it = rit;
            if ( rit != lines.end() && !filename.isEmpty() )
            {
                ++it;
                if ( it == lines.end() )
                    break;
                // Read encoded contents
                TQString contents;
                for ( ; it != lines.end() ; ++it )
                {
                    TQString line = *it;
                    if ( line.endsWith( "</tt>" ) )
                        line = line.left( line.length() - 5 );
                    if ( line.endsWith( "<br />" ) ) // necessary for the boundary check
                        line = line.left( line.length() - 6 );
                    while ( !line.isEmpty() && line[0] == ' ' )
                        line.remove( 0, 1 );
                    if ( line.isEmpty() || line == boundary ) // end of attachment
                        break;
                    if ( line == boundary+"--" ) // end of last attachment
                    {
                        boundary = TQString();
                        break;
                    }
                    contents += line; // no newline, because of linebreaking between <br and />
                }
                contents = contents.replace( TQRegExp("<br */>"), TQString() );
#ifdef DEBUG_EXTRACT
                kdDebug() << "BugDetails::extractAttachments contents=***\n" << contents << "\n***" << endl;
#endif
                kdDebug() << "BugDetails::extractAttachments creating attachment " << filename << endl;
                Attachment a;
                if ( encoding == "base64" )
                    KCodecs::base64Decode( contents.local8Bit(), a.contents /*out*/ );
                else
                    //KCodecs::uudecode( contents.local8Bit(), a.contents /*out*/ );
                    KMessageBox::information( 0, i18n("Attachment %1 could not be decoded.\nEncoding: %2").arg(filename).arg(encoding) );
#ifdef DEBUG_EXTRACT
                kdDebug() << "Result: ***\n" << TQCString( a.contents.data(), a.contents.size()+1 ) << "\n*+*" << endl;
#endif
                a.filename = filename;
                lst.append(a);
                if ( it == lines.end() )
                    break;
            }
        }
    }
#ifdef DEBUG_EXTRACT
    kdDebug() << "BugDetails::extractAttachments returning " << lst.count() << " attachments for this part." << endl;
#endif
    return lst;
}

bool BugDetails::operator==( const BugDetails &rhs )
{
    return m_impl == rhs.m_impl;
}

/**
 * vim:ts=4:sw=4:et
 */