Пример #1
0
 /** @short Decode an encoded-word as per RFC2047 into a unicode string */
 static QString decodeWord(const QByteArray &fullWord, const QByteArray &charset, const QByteArray &encoding, const QByteArray &encoded) {
     if (encoding == "Q") {
         return decodeByteArray(_translateFromQuotedPrintable(encoded), charset);
     } else if (encoding == "B") {
         return decodeByteArray(QByteArray::fromBase64(encoded), charset);
     } else {
         return fullWord;
     }
 }
/** @short Data for the current message part are available now */
void MsgPartNetworkReply::slotMyDataChanged()
{
    if (part.data(Mailbox::RoleIsUnavailable).toBool()) {
        setError(TimeoutError, tr("Offline"));
        emit error(TimeoutError);
        emit finished();
        return;
    }

    if (!part.data(Mailbox::RoleIsFetched).toBool())
        return;

    MsgPartNetAccessManager *netAccess = qobject_cast<MsgPartNetAccessManager*>(manager());
    Q_ASSERT(netAccess);
    QString mimeType = netAccess->translateToSupportedMimeType(part.data(Mailbox::RolePartMimeType).toString());
    QString charset = part.data(Mailbox::RolePartCharset).toString();
    if (mimeType == "text/plain" && requireFormatting) {
        QString decodedString = decodeByteArray(buffer.data(), charset);
        decodedString = UiUtils::Formatting::markUpPlainTextInQML(decodedString);

        setHeader(QNetworkRequest::ContentTypeHeader, "text/html");
        formattedBufferContent = new QByteArray(decodedString.toUtf8().data());
        buffer.close();
        buffer.setBuffer(formattedBufferContent);
        buffer.open(QBuffer::ReadOnly);
    } else {
        setHeader(QNetworkRequest::ContentTypeHeader, mimeType);
    }
    emit readyRead();
    emit finished();
}
Пример #3
0
/** @short Decode RFC2231-style eextended parameter values into a real Unicode string */
QString extractRfc2231Param(const QMap<QByteArray, QByteArray> &parameters, const QByteArray &key)
{
    QMap<QByteArray, QByteArray>::const_iterator it = parameters.constFind(key);
    if (it != parameters.constEnd()) {
        // This parameter is not using the RFC 2231 syntax for extended parameters.
        // I have no idea whether this is correct, but I *guess* that trying to use RFC2047 is not going to hurt.
        return decodeRFC2047String(*it);
    }

    if (parameters.constFind(key + "*0") != parameters.constEnd()) {
        // There's a 2231-style continuation *without* the language/charset extension
        QByteArray raw;
        int num = 0;
        while ((it = parameters.constFind(key + '*' + QByteArray::number(num++))) != parameters.constEnd()) {
            raw += *it;
        }
        return decodeRFC2047String(raw);
    }

    QByteArray raw;
    if ((it = parameters.constFind(key + '*')) != parameters.constEnd()) {
        // No continuation, but language/charset is present
        raw = *it;
    } else if (parameters.constFind(key + "*0*") != parameters.constEnd()) {
        // Both continuation *and* the lang/charset extension are in there
        int num = 0;
        // The funny thing is that the other values might or might not end with the trailing star,
        // at least according to the example in the RFC
        do {
            if ((it = parameters.constFind(key + '*' + QByteArray::number(num))) != parameters.constEnd())
                raw += *it;
            else if ((it = parameters.constFind(key + '*' + QByteArray::number(num) + '*')) != parameters.constEnd())
                raw += *it;
            ++num;
        } while (it != parameters.constEnd());
    }

    // Process 2231-style language/charset continuation, if present
    int pos1 = raw.indexOf('\'', 0);
    int pos2 = raw.indexOf('\'', qMax(1, pos1 + 1));
    if (pos1 != -1 && pos2 != -1) {
        return decodeByteArray(translatePercentToBin(raw.mid(pos2 + 1)), raw.left(pos1));
    }

    // Fallback: it could be empty, or otherwise malformed. Just treat it as UTF-8 for compatibility
    return QString::fromUtf8(raw);
}