Frage

I have an email which contains perfectly formatted html with the single exception that images are linked differently: <img width=456 height=384 id="_x0000_i1026" src="cid:X.MA2.1374935634@aol.com" alt="cid:X.MA4.1372453963@aol.com"> the email has other parts including the image with this content id. The problem is that I dont know how to point the QWebview to the data (which I have). Is there a way to add the image to its cache?

War es hilfreich?

Lösung

It's possible but not easy. Basically you need to:

1- provide your own QNetworkAccessManager-inherited class, overriding createRequest() to catch these links refering to "cid":

QNetworkReply*
MyManager::createRequest (Operation op,
                 const QNetworkRequest & req,
                 QIODevice * outgoingData = 0)
{
  if (op==GetOperation && req.url().scheme() == "cid")
    return MyNetworkReply(req.url().path());
  else
    return QNetworkAccessManager::createRequest(op, req, outgoingData);
}

2- Connect it to the webview with:

MyManager* manager = new MyManager;
view->page()->setNetworkAccessManager(manager);

3- Provide an implementation of MyNetworkReply which inherits from QNetworkReply, a QIODevice-class. And this is the complicated part. You need to provide at least readData(), bytesAvailable(), a constructor that sets up the reply in terms of HTTP headers, and launches the actual asynchronous read with QTimer::singleShot()

4- Decode the attachment (probably from base64 if it's a picture) into a QByteArray for your MyNetworkReply::readData() to read from that.

There's a complete example on qt.gitorious.org written by Qt Labs developers in the Qt 4.6 days. They display an internally generated PNG, not an external mail attachment, but the general steps are as described above. See:

http://qt.gitorious.org/qt-labs/graphics-dojo/blobs/master/url-rendering/main.cpp

However this code has a flaw with Qt-4.8. in the constructor for RendererReply, when it does:

open(ReadOnly|Unbuffered);

this should be:

open(ReadOnly);

otherwise webkit never reads the entire data and displays the broken picture icon.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top