Verified Commit d6cea44a authored by Daniel Vrátil's avatar Daniel Vrátil 🤖
Browse files

Remove legacy payload handling code from Item

parent 8b0a0404
......@@ -44,102 +44,6 @@ uint Akonadi::qHash(const Akonadi::Item &item)
return ::qHash(item.id());
}
namespace
{
struct ByTypeId {
typedef bool result_type;
bool operator()(const std::shared_ptr<Internal::PayloadBase> &lhs,
const std::shared_ptr<Internal::PayloadBase> &rhs) const
{
return strcmp(lhs->typeName(), rhs->typeName()) < 0;
}
};
} // anon namespace
typedef QHash<QString, std::map<std::shared_ptr<Internal::PayloadBase>, std::pair<int, int>, ByTypeId>> LegacyMap;
Q_GLOBAL_STATIC(LegacyMap, typeInfoToMetaTypeIdMap)
Q_GLOBAL_STATIC_WITH_ARGS(QReadWriteLock, legacyMapLock, (QReadWriteLock::Recursive))
void Item::addToLegacyMappingImpl(const QString &mimeType, int spid, int mtid,
std::unique_ptr<Internal::PayloadBase> &p)
{
if (!p.get()) {
return;
}
const std::shared_ptr<Internal::PayloadBase> sp(p.release());
const QWriteLocker locker(legacyMapLock());
std::pair<int, int> &item = (*typeInfoToMetaTypeIdMap())[mimeType][sp];
item.first = spid;
item.second = mtid;
}
namespace
{
class MyReadLocker
{
public:
explicit MyReadLocker(QReadWriteLock *rwl)
: rwl(rwl)
, locked(false)
{
if (rwl) {
rwl->lockForRead();
}
locked = true;
}
~MyReadLocker()
{
if (rwl && locked) {
rwl->unlock();
}
}
template <typename T>
std::shared_ptr<T> makeUnlockingPointer(T *t)
{
if (t) {
// the bind() doesn't throw, so if shared_ptr
// construction line below, or anything else after it,
// throws, we're unlocked. Mark us as such:
locked = false;
const std::shared_ptr<T> result(t, [&](const void *) {
rwl->unlock();
});
// from now on, the shared_ptr is responsible for unlocking
return result;
} else {
return std::shared_ptr<T>();
}
}
private:
Q_DISABLE_COPY(MyReadLocker)
QReadWriteLock *const rwl;
bool locked;
};
}
static std::shared_ptr<const std::pair<int, int>> lookupLegacyMapping(const QString &mimeType,
Internal::PayloadBase *p)
{
MyReadLocker locker(legacyMapLock());
const LegacyMap::const_iterator hit = typeInfoToMetaTypeIdMap()->constFind(mimeType);
if (hit == typeInfoToMetaTypeIdMap()->constEnd()) {
return std::shared_ptr<const std::pair<int, int>>();
}
const std::shared_ptr<Internal::PayloadBase> sp(p, [ = ](Internal::PayloadBase *) {
/*noop*/
});
const LegacyMap::mapped_type::const_iterator it = hit->find(sp);
if (it == hit->end()) {
return std::shared_ptr<const std::pair<int, int>>();
}
return locker.makeUnlockingPointer(&it->second);
}
// Change to something != RFC822 as soon as the server supports it
const char Item::FullPayload[] = "RFC822";
......@@ -514,36 +418,6 @@ Item Item::fromUrl(const QUrl &url)
return Item(itemId);
}
namespace
{
class Dummy
{
};
}
Q_GLOBAL_STATIC(Internal::Payload<Dummy>, dummyPayload)
Internal::PayloadBase *Item::payloadBase() const
{
d_ptr->tryEnsureLegacyPayload();
if (d_ptr->mLegacyPayload) {
return d_ptr->mLegacyPayload.get();
} else {
return dummyPayload();
}
}
void ItemPrivate::tryEnsureLegacyPayload() const
{
if (!mLegacyPayload) {
for (PayloadContainer::const_iterator it = mPayloads.begin(), end = mPayloads.end(); it != end; ++it) {
if (lookupLegacyMapping(mMimeType, it->payload.get())) {
mLegacyPayload = it->payload; // clones
}
}
}
}
Internal::PayloadBase *Item::payloadBaseV2(int spid, int mtid) const
{
return d_ptr->payloadBaseImpl(spid, mtid);
......@@ -573,10 +447,10 @@ bool Item::ensureMetaTypeId(int mtid) const
Item converted = ItemSerializer::convert(*this, mtid);
return d_ptr->movePayloadFrom(converted.d_ptr, mtid);
} catch (const std::exception &e) {
qCDebug(AKONADICORE_LOG) << "conversion threw:" << e.what();
qCWarning(AKONADICORE_LOG) << "Item payload conversion threw:" << e.what();
return false;
} catch (...) {
qCDebug(AKONADICORE_LOG) << "conversion threw something not derived from std::exception: fix the program!";
qCCritical(AKONADICORE_LOG, "conversion threw something not derived from std::exception: fix the program!");
return false;
}
}
......@@ -597,17 +471,6 @@ static QString format_types(const PayloadContainer &c)
return result.join(QLatin1String(", "));
}
#if 0
QString Item::payloadExceptionText(int spid, int mtid) const
{
if (d_ptr->mPayloads.empty()) {
return QStringLiteral("No payload set");
} else {
return QStringLiteral("Wrong payload type (requested: %1; present: %2")
.arg(format_type(spid, mtid), format_types(d_ptr->mPayloads));
}
}
#else
void Item::throwPayloadException(int spid, int mtid) const
{
if (d_ptr->mPayloads.empty()) {
......@@ -620,27 +483,6 @@ void Item::throwPayloadException(int spid, int mtid) const
.arg(format_type(spid, mtid), format_types(d_ptr->mPayloads)));
}
}
#endif
void Item::setPayloadBase(Internal::PayloadBase *p)
{
d_ptr->setLegacyPayloadBaseImpl(std::unique_ptr<Internal::PayloadBase>(p));
}
void ItemPrivate::setLegacyPayloadBaseImpl(std::unique_ptr<Internal::PayloadBase> p)
{
if (const std::shared_ptr<const std::pair<int, int>> pair = lookupLegacyMapping(mMimeType, p.get())) {
std::unique_ptr<Internal::PayloadBase> clone;
if (p.get()) {
clone.reset(p->clone());
}
setPayloadBaseImpl(pair->first, pair->second, p, false);
mLegacyPayload.reset(clone.release());
} else {
mPayloads.clear();
mLegacyPayload.reset(p.release());
}
}
void Item::setPayloadBaseV2(int spid, int mtid, std::unique_ptr<Internal::PayloadBase> &p)
{
......@@ -672,7 +514,7 @@ QVector<int> Item::availablePayloadMetaTypeIds() const
QVector<int> result;
result.reserve(d_ptr->mPayloads.size());
// Stable Insertion Sort - N is typically _very_ low (1 or 2).
for (PayloadContainer::const_iterator it = d_ptr->mPayloads.begin(), end = d_ptr->mPayloads.end(); it != end; ++it) {
for (auto it = d_ptr->mPayloads.begin(), end = d_ptr->mPayloads.end(); it != end; ++it) {
result.insert(std::upper_bound(result.begin(), result.end(), it->metaTypeId), it->metaTypeId);
}
return result;
......
......@@ -690,16 +690,6 @@ public:
*/
void apply(const Item &other);
/**
* Registers \a T as a legacy type for mime type \a mimeType.
*
* This is required information for Item to return the correct
* type from payload() when clients have not been recompiled to
* use the new code.
* @param mimeType the mimeType to register
* @since 4.6
*/
template <typename T> static void addToLegacyMapping(const QString &mimeType);
void setCachedPayloadParts(const QSet<QByteArray> &cachedParts);
private:
......@@ -710,16 +700,11 @@ private:
friend class ItemModifyJobPrivate;
friend class ItemSync;
friend class ProtocolHelper;
Internal::PayloadBase *payloadBase() const;
void setPayloadBase(Internal::PayloadBase *p);
Internal::PayloadBase *payloadBaseV2(int sharedPointerId, int metaTypeId) const;
//std::auto_ptr<PayloadBase> takePayloadBase( int sharedPointerId, int metaTypeId );
void setPayloadBaseV2(int sharedPointerId, int metaTypeId,
std::unique_ptr<Internal::PayloadBase> &p);
void addPayloadBaseVariant(int sharedPointerId, int metaTypeId,
std::unique_ptr<Internal::PayloadBase> &p) const;
static void addToLegacyMappingImpl(const QString &mimeType, int sharedPointerId, int metaTypeId,
std::unique_ptr<Internal::PayloadBase> &p);
/**
* Try to ensure that we have a variant of the payload for metatype id @a mtid.
......@@ -1077,15 +1062,6 @@ void Item::setPayload(std::unique_ptr<T> p)
p.Nope_even_std_unique_ptr_is_not_allowed;
}
template <typename T>
void Item::addToLegacyMapping(const QString &mimeType)
{
typedef Internal::PayloadTrait<T> PayloadType;
static_assert(!PayloadType::isPolymorphic, "Payload type must not be polymorphic");
std::unique_ptr<Internal::PayloadBase> p(new Internal::Payload<T>);
addToLegacyMappingImpl(mimeType, PayloadType::sharedPointerId, PayloadType::elementMetaTypeId(), p);
}
} // namespace Akonadi
Q_DECLARE_METATYPE(Akonadi::Item)
......
......@@ -174,7 +174,6 @@ public:
: QSharedData()
, mRevision(-1)
, mId(id)
, mLegacyPayload()
, mPayloads()
, mCollectionId(-1)
, mSize(0)
......@@ -204,7 +203,6 @@ public:
mSize = other.mSize;
mModificationTime = other.mModificationTime;
mMimeType = other.mMimeType;
mLegacyPayload = other.mLegacyPayload;
mPayloads = other.mPayloads;
mFlagsOverwritten = other.mFlagsOverwritten;
mSizeChanged = other.mSizeChanged;
......@@ -239,9 +237,7 @@ public:
bool hasMetaTypeId(int mtid) const
{
return std::find_if(mPayloads.cbegin(), mPayloads.cend(),
_detail::BySharedPointerAndMetaTypeID(-1, mtid))
!= mPayloads.cend();
return std::any_of(mPayloads.cbegin(), mPayloads.cend(), _detail::BySharedPointerAndMetaTypeID(-1, mtid));
}
Internal::PayloadBase *payloadBaseImpl(int spid, int mtid) const
......@@ -260,9 +256,7 @@ public:
const size_t numMatching = std::count_if(oPayloads.begin(), oPayloads.end(), matcher);
mPayloads.resize(oldSize + numMatching);
using namespace std; // for swap()
for (PayloadContainer::iterator
dst = mPayloads.begin() + oldSize,
src = oPayloads.begin(), end = oPayloads.end(); src != end; ++src) {
for (auto dst = mPayloads.begin() + oldSize, src = oPayloads.begin(), end = oPayloads.end(); src != end; ++src) {
if (matcher(*src)) {
swap(*dst, *src);
++dst;
......@@ -271,29 +265,8 @@ public:
return numMatching > 0;
}
#if 0
std::auto_ptr<PayloadBase> takePayloadBaseImpl(int spid, int mtid)
{
PayloadContainer::iterator it
= std::find_if(mPayloads.begin(), mPayloads.end(),
_detail::BySharedPointerAndMetaTypeID(spid, mtid));
if (it == mPayloads.end()) {
return std::auto_ptr<PayloadBase>();
}
std::rotate(it, it + 1, mPayloads.end());
std::auto_ptr<PayloadBase> result(it->payload.release());
mPayloads.pop_back();
return result;
}
#endif
void setPayloadBaseImpl(int spid, int mtid, std::unique_ptr<Internal::PayloadBase> &p, bool add) const /*sic!*/
{
if (!add) {
mLegacyPayload.reset();
}
if (!p.get()) {
if (!add) {
mPayloads.clear();
......@@ -311,8 +284,6 @@ public:
tp.metaTypeId = mtid;
}
void setLegacyPayloadBaseImpl(std::unique_ptr<Internal::PayloadBase> p);
void tryEnsureLegacyPayload() const;
// Utilise the 4-bytes padding from QSharedData
int mRevision;
......@@ -321,7 +292,6 @@ public:
QString mRemoteRevision;
mutable QString mPayloadPath;
mutable QScopedPointer<Collection> mParent;
mutable _detail::clone_ptr<Internal::PayloadBase> mLegacyPayload;
mutable PayloadContainer mPayloads;
Item::Flags mFlags;
Tag::List mTags;
......
......@@ -41,10 +41,7 @@ Q_DECLARE_METATYPE(std::string)
namespace Akonadi
{
DefaultItemSerializerPlugin::DefaultItemSerializerPlugin()
{
Item::addToLegacyMapping<QByteArray>(QStringLiteral("application/octet-stream"));
}
DefaultItemSerializerPlugin::DefaultItemSerializerPlugin() = default;
bool DefaultItemSerializerPlugin::deserialize(Item &item, const QByteArray &label, QIODevice &data, int)
{
......@@ -187,7 +184,7 @@ QSet<QByteArray> ItemSerializer::allowedForeignParts(const Item &item)
Item ItemSerializer::convert(const Item &item, int mtid)
{
// qCDebug(AKONADICORE_LOG) << "asked to convert a" << item.mimeType() << "item to format" << ( mtid ? QMetaType::typeName( mtid ) : "<legacy>" );
qCDebug(AKONADICORE_LOG) << "asked to convert a" << item.mimeType() << "item to format" << ( mtid ? QMetaType::typeName( mtid ) : "<legacy>" );
if (!item.hasPayload()) {
qCDebug(AKONADICORE_LOG) << " -> but item has no payload!";
return Item();
......
......@@ -26,9 +26,7 @@
using namespace Akonadi;
ItemSerializerPlugin::~ItemSerializerPlugin()
{
}
ItemSerializerPlugin::~ItemSerializerPlugin() = default;
QSet<QByteArray> ItemSerializerPlugin::parts(const Item &item) const
{
......
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