summaryrefslogtreecommitdiffstats
path: root/kshowmail/showrecordelem.cpp
blob: 9edc83458f688a3631080193751a59247efbf0fc (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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
/***************************************************************************
                          showrecord.cpp  -  description
                             -------------------
    begin                : Thu Dec 28 2000
    copyright            : (C) 2000-2001 by Eggert Ehmke
    email                : eggert.ehmke@berlin.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/




#include "showrecordelem.h"

int const ShowRecordElem::continueShowHeaders( 0 );
int const ShowRecordElem::cancelShowHeaders( 1 );

ShowRecordElem::ShowRecordElem ()
{
  //set default values
  m_from = "???";
  m_subject = "???";
  m_size = 0;
  m_pItem = NULL;
  m_new = false;
  markAtViewRefresh = false;
}

ShowRecordElem::ShowRecordElem( int number, TQString& uid, bool isNew )
{
  //set default values
  m_from = "???";
  m_subject = "???";
  m_size = 0;
  m_pItem = NULL;
  markAtViewRefresh = false;

  //set given values
  m_nNumber = number;
  m_uid = uid;
  m_new = isNew;
}


TQCString ShowRecordElem::scanHeader( const TQString& item ) const
{
  TQCString headerline( "" );      //found header line

  //get e.g. the "From:" line, starting with cr,lf,"From:" and ending
  //with a carriage return

  //build the search string
  TQString searchstring( TQString( "\r\n%1:" ).arg( item ) );

  //searching...
  int pos1 = m_header.find( searchstring, 0, FALSE );
  int pos2 = m_header.find( '\r', pos1 + 2 );

  //cut out the interesting content, if we have found a matching line
  //if we have found nothing, the returned string will be ""
  if( pos1 >= 0 )
  {
    headerline = m_header.mid( pos1 + searchstring.length(), pos2 - pos1 - searchstring.length() );
  }

  return headerline;
}

void ShowRecordElem::setHeader( const TQString& header )
{
  //store given header
  m_header = header.ascii();

  //extract sender and store it
  TQCString from = scanHeader( "From" );
  from = from.simplifyWhiteSpace();
  setFrom( from );

  //extract addressee and store it
  TQCString to = scanHeader( "To" );
  to = to.simplifyWhiteSpace();
  setTo (to);

  //extract subject and store it
  TQCString subject = scanHeader( "Subject" );
  subject = subject.simplifyWhiteSpace();
  setSubject( subject );

  //extract date and store it
  TQCString date = scanHeader( "Date" );
  setDate( date );

  //extract content type
  TQCString content = scanHeader( "Content-Type" );
  content = content.simplifyWhiteSpace ();

  //remove the stuff after the content type; see RFC 2045
  int posSemicolon = content.find( ';' );
  if( posSemicolon != -1 )
  {
    content.remove( posSemicolon, content.length() - posSemicolon );
  }

  //store content type
  setContent (content);
}

void ShowRecordElem::setDate( const TQCString& date )
{
  DwDateTime dwDate;      //this class represents an RFC-822 date-time;
                          //see mimelib/datetime.h

  //convert and store the date-time
  dwDate.FromString( date );
  dwDate.Parse();
  m_unixDate.setTime_t( dwDate.AsUnixTime() );
}

TQString ShowRecordElem::from() const
{
  return Codecs::decodeRFC2047( m_from );
}

TQString ShowRecordElem::to() const
{
  return Codecs::decodeRFC2047( m_to );
}

TQString ShowRecordElem::subject() const
{
  return Codecs::decodeRFC2047( m_subject );
}

TQString ShowRecordElem::date() const
{
  return TDEGlobal::locale()->formatDateTime( m_unixDate, true, true );
}

TQString ShowRecordElem::strUnixTime() const
{
  return m_unixDate.toString( TQt::ISODate );
}

TQString ShowRecordElem::strSize() const
{
  return TQString( "%1" ).arg( m_size, 8 );
}

TQString ShowRecordElem::state() const
{
  if( m_new )
    return i18n( "new" );
  else
    return i18n( "old" );
}

void ShowRecordElem::saveOptions( TQDomDocument& doc, TQDomElement& parent )
{
  //build item tag of this mail( with mail number)
  TQString hdr = TQString( ITEM_MESSAGE );
  hdr.append( "%1" );
  hdr = hdr.arg( m_nNumber );

  //create a new element and store the mail meta data in it
  TQDomElement elem = doc.createElement( hdr );
  elem.setAttribute( ATTRIBUTE_MAIL_NUMBER, m_nNumber );
  elem.setAttribute( ATTRIBUTE_MAIL_SIZE, m_size );
  elem.setAttribute( ATTRIBUTE_MAIL_UID, m_uid );

  //create a sub element for the mail header in store the header in it
  TQDomElement subelem = doc.createElement( ITEM_MAIL_HEADER );
  subelem.appendChild( doc.createTextNode( m_header ) );

  //add header element to the mail element
  elem.appendChild( subelem );

  //add mail element to the account (parent) element
  parent.appendChild( elem );
}

void ShowRecordElem::readOptions( TQDomElement& elem )
{
  //get number, size and uid
  setNumber( elem.attribute( ATTRIBUTE_MAIL_NUMBER ).toInt() );
  setSize( elem.attribute( ATTRIBUTE_MAIL_SIZE ).toInt() );
  setUIDL( elem.attribute( ATTRIBUTE_MAIL_UID ) );

  //search for the header item and read it
  TQDomElement subelem = elem.namedItem( ITEM_MAIL_HEADER ).toElement();
  setHeader( subelem.text() );

  //the mail is not new
  setNew( false );
}


void ShowRecordElem::setFrom( const TQCString & from )
{
  m_from = from;
}

void ShowRecordElem::setTo( const TQCString & to )
{
  m_to = to;
}

void ShowRecordElem::setSubject( const TQCString & subject )
{
  m_subject = subject;
}

void ShowRecordElem::setContent( const TQCString& content )
{
  m_content = content;
}

TQString ShowRecordElem::header( ) const
{
  return TQString( m_header );
}

void ShowRecordElem::setUIDL( const TQString & uid )
{
  m_uid = uid;
}

TQString ShowRecordElem::uidl( ) const
{
  return m_uid;
}

void ShowRecordElem::setSize( int size )
{
  m_size = size;
}

int ShowRecordElem::size( ) const
{
  return m_size;
}

void ShowRecordElem::setNew( bool isnew )
{
  m_new = isnew;
}

bool ShowRecordElem::isNew( ) const
{
  return m_new;
}

void ShowRecordElem::setNumber( int n )
{
  m_nNumber = n;
}

int ShowRecordElem::number( ) const
{
  return m_nNumber;
}

TQString ShowRecordElem::content( ) const
{
  return m_content;
}

void ShowRecordElem::setViewItem( ShowListViewItem* item )
{
  m_pItem = item;

  //marks the new entry if recommend by the filter
  if( markAtViewRefresh )
  {
    //mark entry
    item->setSelected( true );

    //delete flag
    markAtViewRefresh = false;
  }
}

ShowListViewItem * ShowRecordElem::viewItem( ) const
{
  return m_pItem;
}

bool ShowRecordElem::isSelected( ) const
{
  if( m_pItem != NULL )
    return m_pItem->isSelected();
  else
    return false;
}

TQString ShowRecordElem::strSizePrefix( ) const
{
  TQString size;

  if( m_size >= 1024 * 1024 )
  {
    //prefix is mega
    size = TQString( "%L1M" ).arg( ( (double)m_size / ( 1024 * 1024 ) ), 0, 'f', 1 );
  }
  else if( m_size >= 1024 )
  {
    //prefix is kilo
    size = TQString( "%L1K" ).arg( ( (double)m_size / 1024 ), 0, 'f', 1 );
  }
  else
    //no prefix
    size = TQString( "%L1" ).arg( m_size );

  return size;
}

TQString ShowRecordElem::decodeMailBody( TQByteArray body, bool preferHTML ) const
{
  TQString charset;    //charset of the content
  TQString encoding;   //content transfer encoding

  //cast given body to a TQCString
  //class TQCString needs a null terminated char array to create
  //an object. Therefore we append an null byte to the given mail body
  body.resize( body.size() + 1 );
  body[ body.size() - 1 ] = '\0';
  TQCString strBody( (char *)body.data() );

  //normalize line ends; remove all \r characters
  for( uint i = 0; i < strBody.size(); i++ )
    if( strBody[ i ] == '\r' )
      strBody.remove( i, 1 );

  //get boundary that is separating the parts of a multipart message
  //if the header doesn't contain a boundary attribute, this messsage
  //has just one part
  TQString boundary = getBoundary();

  //process body subject to it is a multipart messsage or not
  if( boundary == "" )
  {
    //the message has just one body part

    //get the position of the first blank line
    int posBlankLine = strBody.find( "\n\n" );

    //truncate body; the found blank line is separating the
    //header from the message
    strBody = strBody.mid( posBlankLine + 2 );
    if( !strBody.isEmpty() )    //fixed bug 1773636
      while( strBody[ 0 ] == '\n')
        strBody.remove( 0, 1 );


    //get charset of the message; it is behind the
    //content type attribute in the header
    charset = getCharset();

    //get transfer encoding type from the header
    encoding = getTransferEncoding();
  }
  else
  {
    //the message has multiple parts

    //get positions of a plain text and html flag (value of the content type attribute)
    int posPlainFlag = strBody.find( "text/plain", 0, false );
    int posHTMLFlag = strBody.find( "text/html", 0, false );

    //just decode the body, if a plain text or a HTML part is available
    if( posPlainFlag != -1 || posHTMLFlag != -1 )
    {
      //do we want to take the HTML part?
      bool hasHTML = posHTMLFlag != -1;
      bool takeHTML = ( hasHTML && preferHTML ) || posPlainFlag == -1;

      //now we want to extract the designated part
      //While the (truncated) mail text (or the header at the first pass)
      //contains a boundary attribute we will extract the designated part
      //between the boundaries
      int posInside;    //a position inside the designated part
      while( boundary != "" )
      {
        //get a position inside the designated part
        if( takeHTML )
          posInside = strBody.find( "text/html", 0, false );
        else
          posInside = strBody.find( "text/plain", 0, false );

        //get length of the boundary
        int lengthBoundary = boundary.length();

        //calculate the begin and end of the part to extract
        int beginPart = strBody.findRev( boundary.ascii(), posInside ) + lengthBoundary + 1;
        int lengthPart = strBody.findRev( '\n', strBody.find( boundary.ascii(), posInside ) ) - beginPart;

        strBody = strBody.mid( beginPart, lengthPart );

        //looking for a further boundary attribute
        //get the position of the first occurance of "boundary="
        int posBoundary = strBody.find( "boundary=", 0, false );

        if( posBoundary >= 0 )
        {
          //calculate positon of the first quote
          int posFirstQuote = posBoundary + 9;

          //get the position of closing quote
          int posSecondQuote = strBody.find( '"', posFirstQuote + 1 );

          //get boundary string
          boundary.append( strBody.mid( posFirstQuote + 1, posSecondQuote - posFirstQuote - 1 ) );
        }
        else
          boundary = "";
      }

      //now we get charset and transfer encoding if available in the extracted
      //part

      //get the position of the first occurance of "charset="
      int posCharset = strBody.find( "charset=", 0, false );

      //continue, if a charset attribute was found
      if( posCharset >= 0 )
      {
        //calculate positon of the value
        int posBeginValue = posCharset + 8;

        //get end of the value
        int posEndValue = strBody.find( '\n', posBeginValue ) - 1;

        //get charset
        charset.append( strBody.mid( posBeginValue, posEndValue - posBeginValue + 1 ) );

        //remove quotes
        charset.remove( '"' );
        //remove all content after the first semicolon (inclusive)
        int posSemicolon = charset.find( ';' );
        charset = charset.left( posSemicolon );
      }

      //get the position of the first occurance of "charset="
      int posEncoding = strBody.find( "Content-Transfer-Encoding:", 0, false );

      //continue, if a charset attribute was found
      if( posEncoding >= 0 )
      {
        //calculate positon of the value
        int posBeginValue = posEncoding + 26;

        //get end of the value
        int posEndValue = strBody.find( '\n', posBeginValue ) - 1;

        //get charset
        encoding.append( strBody.mid( posBeginValue, posEndValue - posBeginValue + 1 ) );

        //remove quotes and spaces
        encoding = encoding.stripWhiteSpace();
        encoding.remove( '"' );
      }

      //cut off the part header; the found blank line is separating the
      //part header from the message
      if( posCharset != -1 || posEncoding != -1 )
      {
        int posBlankLine = strBody.find( "\n\n" );
        strBody = strBody.mid( posBlankLine + 2 );
        if( !strBody.isEmpty() )  //fixed bug 1773636
          while( strBody[ 0 ] == '\n')
            strBody.remove( 0, 1 );
      }
    }
  }

  //Good things come to those who wait. We have extract the message.
  //Now we have to decode the message, if it is encoded
  if( encoding == "quoted-printable" && !strBody.isEmpty() )  //fixed bug 1773636
  {
    strBody = KCodecs::quotedPrintableDecode( strBody );
  }

  return TQString( strBody );
}

TQString ShowRecordElem::getBoundary( ) const
{
  TQString boundary;

  //check, whether it is a multipart message
  if( m_content.contains( "multipart", false ) )
  {
    //it is a multipart message

    //get the position of the first occurance of "boundary="
    int posBoundary = m_header.find( "boundary=", 0, false );

    //continue, if a boundary attribute was found
    if( posBoundary >= 0 )
    {
      //calculate positon of the first quote
      int posFirstQuote = posBoundary + 9;

      //get the position of closing quote
      int posSecondQuote = m_header.find( '"', posFirstQuote + 1 );

      //get boundary string
      boundary.append( m_header.mid( posFirstQuote + 1, posSecondQuote - posFirstQuote - 1 ) );
    }
  }

  return boundary;
}

TQString ShowRecordElem::getCharset( ) const
{
  TQString charset;

  //get the position of the first occurance of "charset="
  int posCharset = m_header.find( "charset=", 0, false );

  //continue, if a charset attribute was found
  if( posCharset >= 0 )
  {
    //calculate positon of the value
    int posBeginValue = posCharset + 8;

    //get end of the value
    int posEndValue = m_header.find( '\r', posBeginValue ) - 1;

    //get charset
    charset.append( m_header.mid( posBeginValue, posEndValue - posBeginValue + 1 ) );

    //remove quotes
    charset.remove( '"' );
    //remove all content after the first semicolon (inclusive)
    int posSemicolon = charset.find( ';' );
    charset = charset.left( posSemicolon );
  }

  return TQString( charset );
}

TQString ShowRecordElem::getTransferEncoding( ) const
{
  TQString encoding;

  //get the position of the first occurance of "charset="
  int posEncoding = m_header.find( "Content-Transfer-Encoding:", 0, false );

  //continue, if a charset attribute was found
  if( posEncoding >= 0 )
  {
    //calculate positon of the value
    int posBeginValue = posEncoding + 26;

    //get end of the value
    int posEndValue = m_header.find( '\r', posBeginValue ) - 1;

    //get charset
    encoding.append( m_header.mid( posBeginValue, posEndValue - posBeginValue + 1 ) );

    //remove quotes and spaces
    encoding = encoding.stripWhiteSpace();
    encoding.remove( '"' );
  }

  return TQString( encoding );

}

int ShowRecordElem::showHeader( TQString& account )
{
  //show header
  TQString tsubject = subject();
  TQString tmailheader = header();

  //create and open the window
  ShowHeaderDialog dlg( kapp->mainWidget(), account, tsubject, tmailheader );
  int ret = dlg.exec();

  //returns the matching value
  return ret == TQDialog::Accepted ? ShowRecordElem::continueShowHeaders : ShowRecordElem::cancelShowHeaders;
}

FilterAction_Type ShowRecordElem::applyHeaderFilter( HeaderFilter* filter, TQString account, TQString& mailbox, FilterLog* log )
{
  FilterAction_Type action = filter->check( from(), to(), size(), subject(), header(), account, mailbox );

  //if the action is MARK, the related view entry shall be marked at the next view refresh
  if( action == FActMark ) markAtViewRefresh = true;

  //if the action is DELETE, we add a entry to the log
  if( log == NULL )
    kdError( "ShowRecordElem::applyHeaderFilter: Pointer to the filter log is NULL. Can't write to log." );
  if( action == FActDelete && log != NULL )
    log->addDeletedMail( sentDateTime(), from(), account, subject() );
  if( action == FActMove && log != NULL )
    log->addMovedMail( sentDateTime(), from(), account, subject(), mailbox );


  return action;
}

TQDateTime ShowRecordElem::sentDateTime() const
{
  return m_unixDate;
}

void ShowRecordElem::writeToMoveLog( FilterLog * log, TQString account, TQString mailbox )
{
  log->addMovedMail( sentDateTime(), from(), account, subject(), mailbox );
}

void ShowRecordElem::writeToDeleteLog( FilterLog * log, TQString account )
{
  log->addDeletedMail( sentDateTime(), from(), account, subject() );
}

void ShowRecordElem::setMarkAtNextViewRefresh( )
{
  markAtViewRefresh = true;
}