QString EmailMessageListModel::bodyPlainText(const QMailMessage &mailMsg) const
{
    QMailMessagePartContainer *container = (QMailMessagePartContainer *)&mailMsg;
    QMailMessageContentType contentType = container->contentType();
    if (container->hasBody() && contentType.type().toLower() == "text" &&
            contentType.subType().toLower() == "plain")
    {
        return container->body().data();

    }

    QString text("");
    for ( uint i = 0; i < container->partCount(); i++ )
    {
        QMailMessagePart messagePart = container->partAt(i);

        contentType = messagePart.contentType();
        if (messagePart.hasBody() && contentType.type().toLower() == "text" &&
                contentType.subType().toLower() == "plain")
        {
            text += messagePart.body().data() + "\n";
        }
        QMailMessagePart subPart;
        for (uint j = 0; j < messagePart.partCount(); j++)
        {
            subPart = messagePart.partAt(j);
            contentType = subPart.contentType();
            if (subPart.hasBody() && contentType.type().toLower() == "text" &&
                    contentType.subType().toLower() == "plain")
                text += subPart.body().data() + "\n";
        }
    }
    return text;
}
Пример #2
0
bool QMailCryptoSMIME::partHasSignature(const QMailMessagePartContainer &part) const
{
    if (part.multipartType() != QMailMessagePartContainerFwd::MultipartSigned ||
        part.partCount() != 2)
        return false;

    const QMailMessagePart signature = part.partAt(1);

    if (!signature.contentType().matches("application", "pkcs7-signature") &&
        !signature.contentType().matches("application", "x-pkcs7-signature"))
        return false;

    return true;
}
Пример #3
0
void EmailPropertySetter::setAttachment(const QString& s)
{
    QFileInfo fi( s );

    QMailMessageContentDisposition disposition(QMailMessageContentDisposition::Attachment);
    disposition.setFilename( fi.absoluteFilePath().toLatin1() );

    QMailMessageContentType type( QMimeType( fi.absoluteFilePath() ).id().toLatin1() );
    type.setName(fi.baseName().toLatin1());

    QMailMessagePart attachmentPart;
    attachmentPart.setBody( QMailMessageBody::fromFile(fi.absoluteFilePath(), type, QMailMessageBody::Base64, QMailMessageBody::RequiresEncoding) );
    attachmentPart.setContentDisposition(disposition);

    target.appendPart( attachmentPart );
}
void ConversationPage::processAttachments(const QMailMessage &message)
{
    if (!message.status() & QMailMessageMetaData::HasAttachments)
        return;

    connect(this, SIGNAL(downloadCompleted()), this, SLOT(saveAttachment()));

    bool oneTimeFlag = true;
    for (uint i = 1; i < message.partCount(); ++i)
    {
        QMailMessagePart sourcePart = message.partAt(i);
        if (!(sourcePart.multipartType() == QMailMessagePartContainer::MultipartNone))
            continue;

        if (oneTimeFlag)
        {
            MSeparator *separator = new MSeparator();
            separator->setObjectName("Separator");
            m_policy->addItem(separator);
            oneTimeFlag = false;
        }

        MStylableWidget *w = new MStylableWidget(this);
        QGraphicsLinearLayout *layout = new QGraphicsLinearLayout(Qt::Horizontal);
        w->setLayout(layout);
        //% "Attached: "
        MLabel *label = new MLabel(qtTrId("xx_attached"));
        label->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
        label->setObjectName ("AttachmentText");
        layout->addItem(label);
        MButton *button = new MButton(sourcePart.displayName());
        button->setObjectName ("AttachmentButton");
        button->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
        layout->addItem(button);

        //% "Download..."
        AttachmentAction *action = new AttachmentAction(qtTrId("xx_download_context_menu"), button, sourcePart);
        connect(action, SIGNAL(triggered()), this, SLOT(openAttachmentDownloadDialog()));

        //% "Open..."
        action = new AttachmentAction(qtTrId("xx_open_context_menu"), button, sourcePart);
        connect(action, SIGNAL(triggered()), this, SLOT(openAttachmentOpenDialog()));
        m_policy->addItem (w);
    }
}
QString AttachmentListModel::attachmentUrl(const QMailMessage message, const QString &attachmentLocation)
{
    QString attachmentDownloadFolder = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation) + "/mail_attachments/" + attachmentLocation;
    for (uint i = 0; i < message.partCount(); i++) {
        QMailMessagePart sourcePart = message.partAt(i);
        if (attachmentLocation == sourcePart.location().toString(true)) {
            QString attachmentPath = attachmentDownloadFolder + "/" + sourcePart.displayName();
            QFile f(attachmentPath);
            if (f.exists()) {
                return attachmentPath;
            } else {
                // we have the part downloaded locally but not in a file type yet
                if (sourcePart.hasBody()) {
                    QString path = sourcePart.writeBodyTo(attachmentDownloadFolder);
                    return path;
                }
                return QString();
            }
        }
    }
    return QString();
}
void tst_QMailMessagePart::testToRfc2822()
{
    QMailMessagePart body = QMailMessagePart::fromData
        (QStringLiteral("Some body text"),
         QMailMessageContentDisposition(),
         QMailMessageContentType("text/plain"),
         QMailMessageBody::QuotedPrintable);
    QMailMessagePart disposition = QMailMessagePart::fromData
        (QByteArray(),
         QMailMessageContentDisposition(),
         QMailMessageContentType("message/disposition-notification"),
         QMailMessageBody::NoEncoding);
    disposition.setHeaderField("Original-Recipient", "*****@*****.**");
    disposition.setHeaderField("Original-Message-ID", "123456789");

    QMailMessagePart alt = QMailMessagePart::fromData
        (QByteArray(),
         QMailMessageContentDisposition(),
         QMailMessageContentType(),
         QMailMessageBodyFwd::NoEncoding);
    alt.setMultipartType(QMailMessagePartContainer::MultipartAlternative);
    alt.appendPart(body);
    alt.appendPart(disposition);

    const QByteArray expected(
"Content-Type: multipart/alternative; boundary=\"[}<}]\"" CRLF
"Content-Disposition:" CRLF
CRLF
CRLF
"--[}<}]" CRLF
"Content-Type: text/plain" CRLF
"Content-Transfer-Encoding: quoted-printable" CRLF
"Content-Disposition:" CRLF
CRLF
"Some body text" CRLF
"--[}<}]" CRLF
"Content-Type: message/disposition-notification" CRLF
"Content-Disposition:" CRLF
"Original-Recipient: [email protected]" CRLF
"Original-Message-ID: 123456789" CRLF
CRLF
CRLF
"--[}<}]--" CRLF
);

    const QByteArray serialized = alt.toRfc2822();
    QCOMPARE( serialized, expected );
}
void tst_QMailMessagePart::setContentLanguage()
{
    QMailMessagePart part;

    QString language1("en");
    QString language2("de");

    QCOMPARE( part.contentLanguage(), QString() );

    part.setContentLanguage(language1);
    QCOMPARE( part.contentLanguage(), language1 );

    part.setContentLanguage(language2);
    QCOMPARE( part.contentLanguage(), language2 );
}
void tst_QMailMessagePart::setContentDisposition()
{
    QMailMessagePart part;

    QByteArray disposition1("Content-Disposition: inline");
    QByteArray disposition2("Content-Disposition: attachment; filename=sample.txt");

    QCOMPARE( part.contentDisposition().toString(), QMailMessageContentDisposition().toString() );

    part.setContentDisposition(QMailMessageContentDisposition(disposition1));
    QCOMPARE( part.contentDisposition().toString(), QMailMessageContentDisposition(disposition1).toString() );

    part.setContentDisposition(QMailMessageContentDisposition(disposition2));
    QCOMPARE( part.contentDisposition().toString(), QMailMessageContentDisposition(disposition2).toString() );
}
void tst_QMailMessagePart::setContentDescription()
{
    QMailMessagePart part;

    QString description1("Some content-description");
    QString description2("Some other content-description");

    QCOMPARE( part.contentDescription(), QString() );

    part.setContentDescription(description1);
    QCOMPARE( part.contentDescription(), description1 );

    part.setContentDescription(description2);
    QCOMPARE( part.contentDescription(), description2 );
}
void tst_QMailMessagePart::setContentLocation()
{
    QMailMessagePart part;

    QString location1("Some content-location");
    QString location2("Some other content-location");

    QCOMPARE( part.contentLocation(), QString() );

    part.setContentLocation(location1);
    QCOMPARE( part.contentLocation(), location1 );

    part.setContentLocation(location2);
    QCOMPARE( part.contentLocation(), location2 );
}
void tst_QMailMessagePart::setContentID()
{
    QMailMessagePart part;

    QString id1("Some content-ID x@yyyy");
    QString id2("<Some other content-ID y@yyyy>");

    QCOMPARE( part.contentID(), QString() );

    part.setContentID(id1);
    QCOMPARE( part.contentID(), id1 );

    part.setContentID(id2);
    QCOMPARE( part.contentID(), id2.mid(1, id2.length() - 2) );
}
Пример #12
0
QMailCryptoFwd::VerificationResult QMailCryptoSMIME::verifySignature(const QMailMessagePartContainer &part) const
{
    if (!partHasSignature(part))
        return QMailCryptoFwd::VerificationResult(QMailCryptoFwd::MissingSignature);

    QMailMessagePart body = part.partAt(0);
    QMailMessagePart signature = part.partAt(1);

    if (!body.contentAvailable() ||
        !signature.contentAvailable())
        return QMailCryptoFwd::VerificationResult();

    QMailCryptoFwd::VerificationResult result;
    result.engine = QStringLiteral("libsmime.so");
    result.summary = verify(signature.body().data(QMailMessageBodyFwd::Decoded),
                            body.undecodedData(), result.keyResults);
    return result;
}
QString EmailMessageListModel::bodyHtmlText(QMailMessagePartContainer *container) const
{
    QMailMessageContentType contentType = container->contentType();

    if (container->multipartType() == QMailMessagePartContainerFwd::MultipartNone)
    {
        if (contentType.subType().toLower() == "html")
        {
            if (container->hasBody() && container->body().data().size() > 1)
                return container->body().data();
            else
            {
                connect (m_retrievalAction, SIGNAL(activityChanged(QMailServiceAction::Activity)),
                         this, SLOT(downloadActivityChanged(QMailServiceAction::Activity)));
                QMailMessage *msg = (QMailMessage *)container;
                QMailMessageIdList ids;
                ids << msg->id();
                m_retrievalAction->retrieveMessages(ids, QMailRetrievalAction::Content);
                return " ";  // Put a space here as a place holder to notify UI that we do have html body.
                // Should find a better way.
            }
        }
        return "";
    }

    if (!container->contentAvailable())
    {
        // if content is not available, attempts to downlaod from the server.
        connect (m_retrievalAction, SIGNAL(activityChanged(QMailServiceAction::Activity)),
                 this, SLOT(downloadActivityChanged(QMailServiceAction::Activity)));
        QMailMessage *msg = (QMailMessage *)container;
        QMailMessageIdList ids;
        ids << msg->id();
        m_retrievalAction->retrieveMessages(ids, QMailRetrievalAction::Content);
        return " ";  // Put a space here as a place holder to notify UI that we do have html body.
    }

    QString text("");
    for ( uint i = 0; i < container->partCount(); i++ )
    {
        QMailMessagePart messagePart = container->partAt(i);
        contentType = messagePart.contentType();
        if (contentType.type().toLower() == "text" && contentType.subType().toLower() == "html")
        {
            if (messagePart.hasBody())
            {
                text += messagePart.body().data();
            }
            else
            {
                connect (m_retrievalAction, SIGNAL(activityChanged(QMailServiceAction::Activity)),
                         this, SLOT(downloadActivityChanged(QMailServiceAction::Activity)));

                QMailMessagePart::Location location = messagePart.location();
                m_retrievalAction->retrieveMessagePart(location);
                text = " ";
                break;
            }
        }
        QMailMessagePart subPart;
        for (uint j = 0; j < messagePart.partCount(); j++)
        {
            subPart = messagePart.partAt(j);
            contentType = subPart.contentType();
            if (contentType.type().toLower() == "text" && contentType.subType().toLower() == "html")
            {
                if (subPart.hasBody())
                {
                    text += subPart.body().data();
                }
                else
                {
                    connect (m_retrievalAction, SIGNAL(activityChanged(QMailServiceAction::Activity)),
                             this, SLOT(downloadActivityChanged(QMailServiceAction::Activity)));
                    QMailMessagePart::Location location = subPart.location();
                    m_retrievalAction->retrieveMessagePart(location);
                    text = " ";
                    break;
                }
            }
        }
    }
    return text;
}
QVariant EmailMessageListModel::data(const QModelIndex & index, int role) const {
    if (!index.isValid() || index.row() > rowCount(parent(index)))
        return QVariant();

    if (role == QMailMessageModelBase::MessageTimeStampTextRole)
    {
        QMailMessageId msgId = idFromIndex(index);
        QMailMessageMetaData message(msgId);
        QDateTime timeStamp = message.date().toLocalTime();
        return (timeStamp.toString("hh:mm MM/dd/yyyy"));
    }
    else if (role == MessageAttachmentCountRole)
    {
        // return number of attachments
        QMailMessage messageMetaData(idFromIndex(index));
        if (!messageMetaData.status() & QMailMessageMetaData::HasAttachments)
            return 0;

        // TODO: can we satisfy this from metadata too?
        QMailMessage message(idFromIndex(index));
        int numberOfAttachments = 0;
        for (uint i = 1; i < message.partCount(); i++)
        {
            QMailMessagePart sourcePart = message.partAt(i);
            if (!(sourcePart.multipartType() == QMailMessagePartContainer::MultipartNone))
                continue;

            QMailMessageContentType contentType = sourcePart.contentType();
            if (sourcePart.hasBody() && contentType.type().toLower() == "text" &&
                    contentType.subType().toLower() == "plain")
                continue;

            if (i == 1 && contentType.type().toLower() == "text" && contentType.subType().toLower() == "html")
                continue;

            numberOfAttachments += 1;
        }
        return numberOfAttachments;
    }
    else if (role == MessageAttachmentsRole)
    {
        // return a stringlist of attachments
        QMailMessage messageMetaData(idFromIndex(index));
        if (!messageMetaData.status() & QMailMessageMetaData::HasAttachments)
            return QStringList();

        QMailMessage message(idFromIndex(index));
        QStringList attachments;
        for (uint i = 1; i < message.partCount(); i++)
        {
            QMailMessagePart sourcePart = message.partAt(i);
            if (!(sourcePart.multipartType() == QMailMessagePartContainer::MultipartNone))
                continue;

            QMailMessageContentType contentType = sourcePart.contentType();
            if (sourcePart.hasBody() && contentType.type().toLower() == "text" &&
                    contentType.subType().toLower() == "plain")
                continue;

            if (i == 1 && contentType.type().toLower() == "text" && contentType.subType().toLower() == "html")
                continue;

            attachments << sourcePart.displayName();
        }

        return attachments;
    }
    else if (role == MessageRecipientsRole)
    {
        // TODO: metadata?
        QMailMessage message (idFromIndex(index));
        QStringList recipients;
        QList<QMailAddress> addresses = message.to();
        foreach (const QMailAddress &address, addresses) {
            recipients << address.address();
        }
        return recipients;
    }
Пример #15
0
QMailMessage EmailComposerInterface::message() const
{
    QMailMessage mail;

    if( isEmpty() )
        return mail;

    QList<AttachmentItem*> attachments = m_composer->addAttDialog()->attachedFiles();

    QString messageText( m_composer->toPlainText() );

    QMailMessageContentType type("text/plain; charset=UTF-8");
    if(attachments.isEmpty()) {
        mail.setBody( QMailMessageBody::fromData( messageText, type, QMailMessageBody::Base64 ) );
    } else {
        QMailMessagePart textPart;
        textPart.setBody(QMailMessageBody::fromData(messageText.toUtf8(), type, QMailMessageBody::Base64));
        mail.setMultipartType(QMailMessagePartContainer::MultipartMixed);
        mail.appendPart(textPart);

        foreach (AttachmentItem* current, attachments) {
            const QContent& doc( current->document() );
            QString fileName( doc.fileName() );

            QFileInfo fi( fileName );
            QString partName( fi.fileName() );

            fileName = fi.absoluteFilePath();

            QString content( doc.type() );
            if (content.isEmpty())
                content = QMimeType( fileName ).id();

            QMailMessageContentType type( content.toLatin1() );
            type.setName( partName.toLatin1() );

            QMailMessageContentDisposition disposition( QMailMessageContentDisposition::Attachment );
            disposition.setFilename( partName.toLatin1() );

            QMailMessagePart part;

            if ((current->action() != QMailMessage::LinkToAttachments) ||
                (fileName.startsWith(Qtopia::tempDir()))) {
                // This file is temporary - extract the data and create a part from that
                QFile dataFile(fileName);
                if (dataFile.open(QIODevice::ReadOnly)) {
                    QDataStream in(&dataFile);

                    part = QMailMessagePart::fromStream(in, disposition, type, QMailMessageBody::Base64, QMailMessageBody::RequiresEncoding);
                } else {
                    qWarning() << "Unable to open temporary file:" << fileName;
                }
            } else {
                part = QMailMessagePart::fromFile(fileName, disposition, type, QMailMessageBody::Base64, QMailMessageBody::RequiresEncoding);
            }

            mail.appendPart(part);
        }
    }

    mail.setMessageType( QMailMessage::Email );

    return mail;
}