aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/imap/MCIMAPMessage.cc
blob: 6f8a0dbbc5e8c188bb11ee0b42117fb8ec595b66 (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
#include "MCIMAPMessage.h"

#include "MCMessageHeader.h"
#include "MCIMAPPart.h"
#include "MCIMAPMessagePart.h"
#include "MCIMAPMultipart.h"
#include "MCHTMLRenderer.h"

using namespace mailcore;

static AbstractPart * partForPartIDInPart(AbstractPart * part, String * partID);
static AbstractPart * partForPartIDInMultipart(AbstractMultipart * part, String * partID);
static AbstractPart * partForPartIDInMessagePart(AbstractMessagePart * part, String * partID);

void IMAPMessage::init()
{
    mUid = 0;
    mFlags = MessageFlagNone;
    mOriginalFlags = MessageFlagNone;
	mMainPart = NULL;
	mLabels = NULL;
    mModSeqValue = 0;
}

IMAPMessage::IMAPMessage()
{
    init();
}

IMAPMessage::IMAPMessage(IMAPMessage * other)
{
    init();
    setUid(other->uid());
    setFlags(other->flags());
    setOriginalFlags(other->originalFlags());
    setMainPart((AbstractPart *) other->mainPart()->copy()->autorelease());
    setGmailLabels(other->gmailLabels());
}

IMAPMessage::~IMAPMessage()
{
    MC_SAFE_RELEASE(mMainPart);
    MC_SAFE_RELEASE(mLabels);
}

Object * IMAPMessage::copy()
{
    return new IMAPMessage(this);
}

String * IMAPMessage::description()
{
    String * result = String::string();
    result->appendUTF8Format("<%s:%p %u\n", className()->UTF8Characters(), this, (unsigned int) uid());
    result->appendString(header()->description());
    if (mainPart() != NULL) {
        result->appendString(mainPart()->description());
        result->appendUTF8Characters("\n");
    }
    result->appendUTF8Characters(">");
    return result;
}

uint32_t IMAPMessage::uid()
{
    return mUid;
}

void IMAPMessage::setUid(uint32_t uid)
{
    mUid = uid;
}

void IMAPMessage::setFlags(MessageFlag flags)
{
    mFlags = flags;
}

MessageFlag IMAPMessage::flags()
{
    return mFlags;
}

void IMAPMessage::setOriginalFlags(MessageFlag flags)
{
    mOriginalFlags = flags;
}

MessageFlag IMAPMessage::originalFlags()
{
    return mOriginalFlags;
}

void IMAPMessage::setModSeqValue(uint64_t uid)
{
    mModSeqValue = uid;
}

uint64_t IMAPMessage::modSeqValue()
{
    return mModSeqValue;
}

void IMAPMessage::setMainPart(AbstractPart * mainPart)
{
    MC_SAFE_REPLACE_RETAIN(AbstractPart, mMainPart, mainPart);
}

AbstractPart * IMAPMessage::mainPart()
{
    return mMainPart;
}

void IMAPMessage::setGmailLabels(Array * labels)
{
    MC_SAFE_REPLACE_COPY(Array, mLabels, labels);
}

Array * IMAPMessage::gmailLabels()
{
    return mLabels;
}

AbstractPart * IMAPMessage::partForPartID(String * partID)
{
    return partForPartIDInPart(mainPart(), partID);
}

static AbstractPart * partForPartIDInPart(AbstractPart * part, String * partID)
{
    switch (part->partType()) {
        case PartTypeSingle:
            if (partID->isEqual(((IMAPPart *) part)->partID())) {
                return part;
            }
            return NULL;
        case mailcore::PartTypeMultipartMixed:
        case mailcore::PartTypeMultipartRelated:
        case mailcore::PartTypeMultipartAlternative:
            if (partID->isEqual(((IMAPMultipart *) part)->partID())) {
                return part;
            }
            return partForPartIDInMultipart((AbstractMultipart *) part, partID);
        case mailcore::PartTypeMessage:
            if (partID->isEqual(((IMAPMessagePart *) part)->partID())) {
                return part;
            }
            return partForPartIDInMessagePart((AbstractMessagePart *) part, partID);
        default:
            return NULL;
    }
}

static AbstractPart * partForPartIDInMessagePart(AbstractMessagePart * part, String * partID)
{
    return partForPartIDInPart(part->mainPart(), partID);
}

static AbstractPart * partForPartIDInMultipart(AbstractMultipart * part, String * partID)
{
    for(unsigned int i = 0 ; i < part->parts()->count() ; i ++) {
        mailcore::AbstractPart * subpart = (mailcore::AbstractPart *) part->parts()->objectAtIndex(i);
        mailcore::AbstractPart * result = partForPartIDInPart(subpart, partID);
        if (result != NULL)
            return result;
    }
    return NULL;
}

AbstractPart * IMAPMessage::partForContentID(String * contentID)
{
    return mainPart()->partForContentID(contentID);
}

AbstractPart * IMAPMessage::partForUniqueID(String * uniqueID)
{
    return mainPart()->partForUniqueID(uniqueID);
}

String * IMAPMessage::htmlRendering(String * folder,
                                    HTMLRendererIMAPCallback * dataCallback,
                                    HTMLRendererTemplateCallback * htmlCallback)
{
    return HTMLRenderer::htmlForIMAPMessage(folder, this, dataCallback, htmlCallback);
}