summaryrefslogtreecommitdiffstats
path: root/kmail/bodyvisitor.cpp
blob: 99af6619618345c3331b1825da905d8f102be653 (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
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "bodyvisitor.h"
#include "kmmsgpart.h"
#include "attachmentstrategy.h"
#include <kdebug.h>

namespace KMail {

  BodyVisitor* BodyVisitorFactory::getVisitor( const AttachmentStrategy* strategy )
  {
    if (strategy == AttachmentStrategy::smart())
    {
      return new BodyVisitorSmart();
    } else if (strategy == AttachmentStrategy::iconic())
    {
      return new BodyVisitorHidden();
    } else if (strategy == AttachmentStrategy::inlined())
    {
      return new BodyVisitorInline();
    } else if (strategy == AttachmentStrategy::hidden())
    {
      return new BodyVisitorHidden();
    }
    // default
    return new BodyVisitorSmart();
  }

  //=============================================================================

  BodyVisitor::BodyVisitor()
  {
    // parts that are probably always ok to load
    mBasicList.clear();
    // body text
    mBasicList += "TEXT/PLAIN";
    mBasicList += "TEXT/HTML";
    mBasicList += "MESSAGE/DELIVERY-STATUS";
    // pgp stuff
    mBasicList += "APPLICATION/PGP-SIGNATURE";
    mBasicList += "APPLICATION/PGP";
    mBasicList += "APPLICATION/PGP-ENCRYPTED";
    mBasicList += "APPLICATION/PKCS7-SIGNATURE";
    // groupware
    mBasicList += "APPLICATION/MS-TNEF";
    mBasicList += "TEXT/CALENDAR";
    mBasicList += "TEXT/X-VCARD";
  }

  //-----------------------------------------------------------------------------
  BodyVisitor::~BodyVisitor()
  {
  }

  //-----------------------------------------------------------------------------
  void BodyVisitor::visit( KMMessagePart * part )
  {
    mParts.append( part );
  }

  //-----------------------------------------------------------------------------
  void BodyVisitor::visit( QPtrList<KMMessagePart> & list )
  {
    mParts = list;
  }

  //-----------------------------------------------------------------------------
  QPtrList<KMMessagePart> BodyVisitor::partsToLoad()
  {
    QPtrListIterator<KMMessagePart> it( mParts );
    QPtrList<KMMessagePart> selected;
    KMMessagePart *part = 0;
    bool headerCheck = false;
    while ( (part = it.current()) != 0 )
    {
      ++it;
      // skip this part if the parent part is already loading
      if ( part->parent() &&
           selected.contains( part->parent() ) &&
           part->loadPart() )
        continue;

      if ( part->originalContentTypeStr().contains("SIGNED") )
      {
        // signed messages have to be loaded completely
        // so construct a new dummy part that loads the body
        KMMessagePart *fake = new KMMessagePart();
        fake->setPartSpecifier( "TEXT" );
        fake->setOriginalContentTypeStr("");
        fake->setLoadPart( true );
        selected.append( fake );
        break;
      }

      if ( headerCheck && !part->partSpecifier().endsWith(".HEADER") )
      {
        // this is an embedded simple message (not multipart) so we get no header part
        // from imap. As we probably need to load the header (e.g. in smart or inline mode)
        // we add a fake part that is not included in the message itself
        KMMessagePart *fake = new KMMessagePart();
        QString partId = part->partSpecifier().section( '.', 0, -2 )+".HEADER";
        fake->setPartSpecifier( partId );
        fake->setOriginalContentTypeStr("");
        fake->setLoadPart( true );
        if ( addPartToList( fake ) )
          selected.append( fake );
      }

      if ( part->originalContentTypeStr() == "MESSAGE/RFC822" )
        headerCheck = true;
      else
        headerCheck = false;

      // check whether to load this part or not:
      // look at the basic list, ask the subclass and check the parent
      if ( mBasicList.contains( part->originalContentTypeStr() ) ||
           parentNeedsLoading( part ) ||
           addPartToList( part ) )
      {
        if ( part->typeStr() != "MULTIPART" ||
             part->partSpecifier().endsWith(".HEADER") )
        {
          // load the part itself
          part->setLoadPart( true );
        }
      }
      if ( !part->partSpecifier().endsWith(".HEADER") &&
           part->typeStr() != "MULTIPART" )
        part->setLoadHeaders( true ); // load MIME header

      if ( part->loadHeaders() || part->loadPart() )
        selected.append( part );
    }
    return selected;
  }

  //-----------------------------------------------------------------------------
  bool BodyVisitor::parentNeedsLoading( KMMessagePart *msgPart )
  {
    KMMessagePart *part = msgPart;
    while ( part )
    {
      if ( part->parent() &&
           ( part->parent()->originalContentTypeStr() == "MULTIPART/SIGNED" ||
           ( msgPart->originalContentTypeStr() == "APPLICATION/OCTET-STREAM" &&
             part->parent()->originalContentTypeStr() == "MULTIPART/ENCRYPTED" ) ) )
        return true;

      part = part->parent();
    }
    return false;
  }

  //=============================================================================

  BodyVisitorSmart::BodyVisitorSmart()
    : BodyVisitor()
  {
  }

  //-----------------------------------------------------------------------------
  bool BodyVisitorSmart::addPartToList( KMMessagePart * part )
  {
    // header of an encapsulated message
    if ( part->partSpecifier().endsWith(".HEADER") )
      return true;

    return false;
  }

  //=============================================================================

  BodyVisitorInline::BodyVisitorInline()
    : BodyVisitor()
  {
  }

  //-----------------------------------------------------------------------------
  bool BodyVisitorInline::addPartToList( KMMessagePart * part )
  {
    // header of an encapsulated message
    if ( part->partSpecifier().endsWith(".HEADER") )
      return true;
    else if ( part->typeStr() == "IMAGE" ) // images
      return true;
    else if ( part->typeStr() == "TEXT" ) // text, diff and stuff
      return true;

    return false;
  }

  //=============================================================================

  BodyVisitorHidden::BodyVisitorHidden()
    : BodyVisitor()
  {
  }

  //-----------------------------------------------------------------------------
  bool BodyVisitorHidden::addPartToList( KMMessagePart * part )
  {
    // header of an encapsulated message
    if ( part->partSpecifier().endsWith(".HEADER") )
      return true;

    return false;
  }

}