24 #include "blobiohandler.h"
29 #include "SignOn/signonplugincommon.h"
31 #define SIGNON_IPC_BUFFER_PAGE_SIZE 16384
33 using namespace SignOn;
35 BlobIOHandler::BlobIOHandler(QIODevice *readChannel,
36 QIODevice *writeChannel,
39 m_readChannel(readChannel),
40 m_writeChannel(writeChannel),
46 void BlobIOHandler::setReadChannelSocketNotifier(QSocketNotifier *notifier)
51 m_readNotifier = notifier;
54 bool BlobIOHandler::sendData(
const QVariantMap &map)
56 if (m_writeChannel == 0) {
57 TRACE() <<
"NULL write channel.";
61 QDataStream stream(m_writeChannel);
62 QByteArray ba = variantMapToByteArray(map);
65 QVector<QByteArray> pages = pageByteArray(ba);
66 for (
int i = 0; i < pages.count(); ++i)
72 void BlobIOHandler::setReadNotificationEnabled(
bool enabled)
75 if (m_readNotifier != 0) {
76 m_readNotifier->setEnabled(
true);
77 connect(m_readNotifier, SIGNAL(activated(
int)),
78 this, SLOT(readBlob()));
80 connect(m_readChannel, SIGNAL(readyRead()),
81 this, SLOT(readBlob()));
84 if (m_readNotifier != 0) {
85 disconnect(m_readNotifier, SIGNAL(activated(
int)),
86 this, SLOT(readBlob()));
87 m_readNotifier->setEnabled(
false);
89 disconnect(m_readChannel, SIGNAL(readyRead()),
90 this, SLOT(readBlob()));
95 void BlobIOHandler::receiveData(
int expectedDataSize)
98 m_blobSize = expectedDataSize;
102 if (m_blobSize > SIGNON_IPC_BUFFER_PAGE_SIZE)
103 setReadNotificationEnabled(
true);
108 void BlobIOHandler::readBlob()
110 QDataStream in(m_readChannel);
112 QByteArray fractionBa;
114 m_blobBuffer.append(fractionBa);
117 if ((fractionBa.size() == 0) && (m_blobBuffer.size() < m_blobSize)) {
118 setReadNotificationEnabled(
false);
123 if (m_blobBuffer.size() == m_blobSize) {
124 QVariantMap sessionDataMap;
125 sessionDataMap = byteArrayToVariantMap(m_blobBuffer);
127 if (m_blobSize > SIGNON_IPC_BUFFER_PAGE_SIZE)
128 setReadNotificationEnabled(
false);
130 emit dataReceived(sessionDataMap);
134 static QVariantMap filterOutComplexTypes(
const QVariantMap &map)
136 QVariantMap filteredMap;
137 QVariantMap::const_iterator i;
138 for (i = map.constBegin(); i != map.constEnd(); i++) {
143 if (qstrcmp(i.value().typeName(),
"QDBusArgument") == 0) {
144 BLAME() <<
"Found QDBusArgument in map; skipping.";
147 filteredMap.insert(i.key(), i.value());
152 QByteArray BlobIOHandler::variantMapToByteArray(
const QVariantMap &map)
155 if (!buffer.open(QIODevice::WriteOnly))
156 BLAME() <<
"Buffer opening failed.";
158 QDataStream stream(&buffer);
159 stream << filterOutComplexTypes(map);
162 return buffer.data();
165 QVariantMap BlobIOHandler::byteArrayToVariantMap(
const QByteArray &array)
167 QByteArray nonConst = array;
168 QBuffer buffer(&nonConst);
169 if (!buffer.open(QIODevice::ReadOnly))
170 BLAME() <<
"Buffer opening failed.";
173 QDataStream stream(&buffer);
181 QVector<QByteArray> BlobIOHandler::pageByteArray(
const QByteArray &array)
183 QVector<QByteArray> dataPages;
184 QByteArray ba = array;
185 QBuffer pagingBuffer(&ba);
187 if (!pagingBuffer.open(QIODevice::ReadOnly))
188 BLAME() <<
"Error while paging BLOB. Buffer opening failed.";
190 while (!pagingBuffer.atEnd()) {
191 QByteArray page = pagingBuffer.read(SIGNON_IPC_BUFFER_PAGE_SIZE);
192 dataPages.append(page);
194 pagingBuffer.close();