Commit cc3c59f7 authored by Damien Caliste's avatar Damien Caliste

[messagingframework] Introduce a new function to test content type content, fixes MER#1786

parent 8bb3bb88
......@@ -928,11 +928,7 @@ namespace findBody
bool inMultipartNone(const QMailMessagePartContainer &container, Context &ctx)
{
if (!ctx.contentType.isEmpty()
&& ctx.contentType != container.contentType().type().toLower())
return false;
if (!ctx.contentSubtype.isEmpty()
&& ctx.contentSubtype != container.contentType().subType().toLower())
if (!container.contentType().matches(ctx.contentType, ctx.contentSubtype))
return false;
ctx.found = const_cast<QMailMessagePartContainer*>(&container);
return true;
......@@ -943,12 +939,7 @@ namespace findBody
if (part.contentDisposition().type() == QMailMessageContentDisposition::Attachment)
return false;
if (!ctx.contentType.isEmpty()
&& ctx.contentType != part.contentType().type().toLower())
return false;
if (!ctx.contentSubtype.isEmpty()
&& ctx.contentSubtype != part.contentType().subType().toLower())
if (!part.contentType().matches(ctx.contentType, ctx.contentSubtype))
return false;
ctx.found = const_cast<QMailMessagePart*> (&part);
......@@ -1023,7 +1014,7 @@ namespace findBody
for (int i = (int)container.partCount() - 1; i >= 0; i--) {
if (i != bodyPart) {
const QMailMessagePart &part = container.partAt(i);
if (imageContentType == part.contentType().type().toLower()) {
if (part.contentType().matches(imageContentType)) {
ctx.htmlImageLoc << part.location();
ctx.htmlImageParts << &part;
} else if (!part.contentID().isEmpty()) {
......@@ -1175,8 +1166,8 @@ namespace findAttachments
{
QMailMessageContentType contentType = part.contentType();
bool isText = (contentType.type().toLower() == "text") &&
((contentType.subType().toLower() == "plain") || (contentType.subType().toLower() == "html"));
bool isText = (contentType.matches("text", "plain")
|| contentType.matches("text", "html"));
bool isInLine = (!part.contentDisposition().isNull()) &&
(part.contentDisposition().type() == QMailMessageContentDisposition::Inline);
......@@ -1184,8 +1175,7 @@ namespace findAttachments
bool isAttachment = (!part.contentDisposition().isNull()) &&
(part.contentDisposition().type() == QMailMessageContentDisposition::Attachment);
bool isRFC822 = (contentType.type().toLower() == "message") &&
(contentType.subType().toLower() == "rfc822");
bool isRFC822 = contentType.matches("message", "rfc822");
// Attached messages are considered as attachments even if content disposition
// is inline instead of attachment, but only if they aren't text/plain nor text/html
......@@ -1278,18 +1268,17 @@ namespace findAttachments
const QMailMessagePart &part = container.partAt(i);
// Skip parts of the message body
const QString contentType = QString(part.contentType().content()).toLower();
switch (i) {
case 0:
if (contentType == QString("text/plain")) {
if (part.contentType().matches("text", "plain")) {
firstPartIsTextPlain = true;
continue;
} else if (contentType == QString("text/html")) {
} else if (part.contentType().matches("text", "html")) {
continue;
}
break;
case 1:
if (firstPartIsTextPlain && contentType == QString("text/html")) {
if (firstPartIsTextPlain && part.contentType().matches("text", "html")) {
continue;
}
break;
......@@ -2694,6 +2683,18 @@ void QMailMessageContentType::setCharset(const QByteArray& charset)
setParameter("charset", charset);
}
/*!
Allow to test if the content type matches a specific "type / subtype" string.
*/
bool QMailMessageContentType::matches(const QByteArray& primary, const QByteArray& sub) const
{
// Content type is not encoded, we can use qstricmp directly.
if (!primary.isEmpty() && qstricmp(type(), primary) != 0)
return false;
return (sub.isEmpty() || qstricmp(subType(), sub) == 0);
}
/*!
\class QMailMessageContentDisposition
......@@ -3185,9 +3186,7 @@ QMailMessageBodyPrivate::QMailMessageBodyPrivate()
void QMailMessageBodyPrivate::ensureCharsetExist()
{
if (_type.type().toLower() != "text"
|| (_type.subType().toLower() != "plain"
&& _type.subType().toLower() != "html")) {
if (!_type.matches("text", "plain") && !_type.matches("text", "html")) {
QByteArray best(QMailCodec::bestCompatibleCharset(_type.charset(), true));
if (!best.isEmpty()) {
_type.setCharset(best);
......@@ -4043,7 +4042,7 @@ void QMailMessagePartContainerPrivate::defaultContentType(const QMailMessagePart
{
// Note that the default is 'message/rfc822' when the parent is 'multipart/digest'
QMailMessageContentType parentType = parent->contentType();
if (parentType.content().toLower() == "multipart/digest")
if (parentType.matches("multipart", "digest"))
{
type.setType("message");
type.setSubType("rfc822");
......@@ -4217,7 +4216,7 @@ void QMailMessagePartContainerPrivate::setBody(const QMailMessageBody& body, QMa
setBodyProperties(body.contentType(), body.transferEncoding());
// Multipart messages do not have their own bodies
if (body.contentType().type().toLower() != "multipart") {
if (!body.contentType().matches("multipart")) {
_body = body;
_hasBody = !_body.isEmpty();
}
......@@ -6110,14 +6109,10 @@ QString QMailMessagePart::displayName() const
if (id.isEmpty())
id = contentID();
if (id.isEmpty()) {
bool isRFC822 = (contentType().type().toLower() == "message") &&
(contentType().subType().toLower() == "rfc822");
if (isRFC822) {
// TODO don't load entire body into memory
QMailMessage msg = QMailMessage::fromRfc2822(body().data(QMailMessageBody::Decoded));
id = msg.subject();
}
if (id.isEmpty() && contentType().matches("message", "rfc822")) {
// TODO don't load entire body into memory
QMailMessage msg = QMailMessage::fromRfc2822(body().data(QMailMessageBody::Decoded));
id = msg.subject();
}
if (id.isEmpty()) {
......@@ -6249,7 +6244,7 @@ static QString partFileName(const QMailMessagePart &part)
// Application/octet-stream is a fallback for the case when mimetype
// is unknown by MUA, so it's treated exceptionally here, so the original
// attachment name is preserved
if (part.contentType().content()!=QString("application/octet-stream")) {
if (!part.contentType().matches("application", "octet-stream")) {
// If possible, create the file with a useful filename extension
QString existing;
int index = fileName.lastIndexOf('.');
......@@ -8478,8 +8473,7 @@ bool QMailMessage::hasCalendarInvitation() const
}
} else {
const QMailMessageContentType &ct(part->contentType());
if ((ct.type().toLower() == "text") &&
(ct.subType().toLower() == "calendar") &&
if (ct.matches("text", "calendar") &&
(ct.parameter("method").toLower() == "request")) {
return true;
}
......
......@@ -154,6 +154,8 @@ public:
QByteArray charset() const;
void setCharset(const QByteArray& charset);
bool matches(const QByteArray& primary, const QByteArray& sub = QByteArray()) const;
private:
// Don't allow the Id to be changed
void setId(const QByteArray& text);
......
......@@ -95,26 +95,21 @@ QMailMessageClassifier::~QMailMessageClassifier()
static QMailMessage::ContentType fromContentType(const QMailMessageContentType& contentType)
{
QString type(contentType.type().toLower());
QString subtype(contentType.subType().toLower());
QMailMessage::ContentType content = QMailMessage::UnknownContent;
if (type == "text") {
if (subtype == "html") {
content = QMailMessage::HtmlContent;
} else if (subtype == "plain") {
content = QMailMessage::PlainTextContent;
} else if (subtype == "x-vcard") {
content = QMailMessage::VCardContent;
} else if (subtype == "x-vcalendar") {
content = QMailMessage::VCalendarContent;
}
} else if (contentType.type().toLower() == "image") {
if (contentType.matches("text", "html")) {
content = QMailMessage::HtmlContent;
} else if (contentType.matches("text", "plain")) {
content = QMailMessage::PlainTextContent;
} else if (contentType.matches("text", "x-vcard")) {
content = QMailMessage::VCardContent;
} else if (contentType.matches("text", "x-vcalendar")) {
content = QMailMessage::VCalendarContent;
} else if (contentType.matches("image")) {
content = QMailMessage::ImageContent;
} else if (contentType.type().toLower() == "audio") {
} else if (contentType.matches("audio")) {
content = QMailMessage::AudioContent;
} else if (contentType.type().toLower() == "video") {
} else if (contentType.matches("video")) {
content = QMailMessage::VideoContent;
}
......@@ -191,7 +186,7 @@ bool QMailMessageClassifier::classifyMessage(QMailMessage& message)
if (multipartType == QMailMessagePartContainer::MultipartNone) {
content = fromContentType(contentType);
if (content == QMailMessage::UnknownContent) {
if (contentType.type().toLower() == "text") {
if (contentType.matches("text")) {
// Assume some type of richer-than-plain text
content = QMailMessage::RichTextContent;
}
......@@ -210,7 +205,7 @@ bool QMailMessageClassifier::classifyMessage(QMailMessage& message)
if (multipartType == QMailMessagePartContainer::MultipartNone) {
content = fromContentType(contentType);
if (content == QMailMessage::UnknownContent) {
if (contentType.type().toLower() == "text") {
if (contentType.matches("text")) {
// Assume some type of richer-than-plain text
content = QMailMessage::RichTextContent;
}
......
......@@ -1403,7 +1403,7 @@ void ImapFetchSelectedMessagesStrategy::metaDataAnalysis(ImapStrategyContextBase
}
ImapConfiguration imapCfg(context->config());
QString preferred(imapCfg.preferredTextSubtype().toLower());
QByteArray preferred(imapCfg.preferredTextSubtype().toLocal8Bit());
// Iterate over all parts, looking for the preferred body,
// download that first giving preference over all other parts
......@@ -1416,8 +1416,7 @@ void ImapFetchSelectedMessagesStrategy::metaDataAnalysis(ImapStrategyContextBase
if ((part.partCount() == 0)
&& (!part.partialContentAvailable())
&& (disposition.size() > 0)
&& (contentType.type().toLower() == "text")
&& (contentType.subType().toLower() == preferred)) {
&& (contentType.matches("text", preferred))) {
// There is a preferred text sub-part to retrieve.
// The preferred text part has priority over other parts so,
// we put it directly into the main completion list.
......@@ -1484,7 +1483,7 @@ void ImapFetchSelectedMessagesStrategy::prepareCompletionList(
completionList.append(message.id());
} else {
const QMailMessageContentType contentType(message.contentType());
if (contentType.type().toLower() == "text") {
if (contentType.matches("text")) {
// It is a text part. So, we can retrieve the first
// portion of it.
QMailMessagePart::Location location;
......@@ -1509,7 +1508,7 @@ void ImapFetchSelectedMessagesStrategy::prepareCompletionList(
completionSectionList.append(qMakePair(it->first, (uint)0));
bytesLeft -= it->second;
++partsToRetrieve;
} else if (part.contentType().type().toLower() == "text") {
} else if (part.contentType().matches("text")) {
// Text parts can be downloaded partially.
completionSectionList.append(qMakePair(it->first, (uint)bytesLeft));
bytesLeft = 0;
......
......@@ -372,10 +372,10 @@ void setPartFromDescription(const QStringList &details, QMailMessagePart *part)
int next = 7;
const QMailMessageContentType &type(part->contentType());
if (type.type().toUpper() == "TEXT") {
if (type.matches("text")) {
// The following field is the part's line count
++next;
} else if ((type.type().toUpper() == "MESSAGE") && (type.subType().toUpper() == "RFC822")) {
} else if (type.matches("message", "rfc822")) {
// For parts of type 'message/rfc822', there are three extra fields here...
next += 3;
}
......
......@@ -374,7 +374,7 @@ struct TextPartSearcher
bool operator()(const QMailMessagePart &part)
{
if (part.contentType().type().toLower() == "text") {
if (part.contentType().matches("text")) {
if (part.body().data().contains(text, Qt::CaseInsensitive)) {
return false;
}
......@@ -389,7 +389,7 @@ bool messageBodyContainsText(const QMailMessage &message, const QString& text)
{
// Search only messages or message parts that are of type 'text/*'
if (message.hasBody()) {
if (message.contentType().type().toLower() == "text") {
if (message.contentType().matches("text")) {
if (message.body().data().contains(text, Qt::CaseInsensitive))
return true;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment