From 1ce732668b2b3e4d735665bd60e1a18f139b1de2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Dan=20Vr=C3=A1til?= <dvratil@redhat.com>
Date: Fri, 5 Dec 2014 18:49:15 +0100
Subject: [PATCH 23/30] Always create a new PartType when it does not exist
Fixes a regression introduced in previous commit that caused Part operations
to fail when specified PartType did not exist in Akonadi storage yet.
---
server/src/handler/append.cpp | 4 ++--
server/src/storage/datastore.cpp | 2 +-
server/src/storage/parttypehelper.cpp | 16 +++++++++++++++-
server/src/storage/parttypehelper.h | 8 ++++++++
4 files changed, 26 insertions(+), 4 deletions(-)
diff --git a/server/src/handler/append.cpp b/server/src/handler/append.cpp
index b594e27..15fb9ea 100644
--- a/server/src/handler/append.cpp
+++ b/server/src/handler/append.cpp
@@ -134,7 +134,7 @@ bool Append::commit()
// wrap data into a part
Part part;
- part.setPartType( PartType::retrieveByFQName( QLatin1String("PLD"), QLatin1String("RFC822") ) );
+ part.setPartType( PartTypeHelper::fromFqName( QLatin1String("PLD"), QLatin1String("RFC822") ) );
part.setData( m_data );
part.setPimItemId( item.id() );
part.setDatasize( dataSize );
@@ -148,7 +148,7 @@ bool Append::commit()
//akDebug() << "Append handler: doPreprocessing is" << doPreprocessing;
if ( doPreprocessing ) {
Part hiddenAttribute;
- hiddenAttribute.setPartType( PartType::retrieveByFQName( QLatin1String("ATR"), QLatin1String("HIDDEN") ) );
+ hiddenAttribute.setPartType( PartTypeHelper::fromFqName( QLatin1String("ATR"), QLatin1String("HIDDEN") ) );
hiddenAttribute.setData( QByteArray() );
hiddenAttribute.setPimItemId( item.id() );
hiddenAttribute.setDatasize( 0 );
diff --git a/server/src/storage/datastore.cpp b/server/src/storage/datastore.cpp
index 0983d84..c9fa0c3 100644
--- a/server/src/storage/datastore.cpp
+++ b/server/src/storage/datastore.cpp
@@ -1027,7 +1027,7 @@ bool DataStore::unhideAllPimItems()
try {
return PartHelper::remove( Part::partTypeIdFullColumnName(),
- PartType::retrieveByFQName( QLatin1String("ATR"), QLatin1String("HIDDEN") ).id() );
+ PartTypeHelper::fromFqName( QLatin1String("ATR"), QLatin1String("HIDDEN") ).id() );
} catch ( ... ) {} // we can live with this failing
return false;
diff --git a/server/src/storage/parttypehelper.cpp b/server/src/storage/parttypehelper.cpp
index 7654108..bcff9c6 100644
--- a/server/src/storage/parttypehelper.cpp
+++ b/server/src/storage/parttypehelper.cpp
@@ -37,7 +37,8 @@ QPair< QString, QString > PartTypeHelper::parseFqName(const QString& fqName)
PartType PartTypeHelper::fromFqName(const QString& fqName)
{
const QPair<QString, QString> p = parseFqName( fqName );
- return PartType::retrieveByFQName(p.first, p.second);
+ return fromFqName(p.first, p.second);
+
}
PartType PartTypeHelper::fromFqName(const QByteArray& fqName)
@@ -45,6 +46,19 @@ PartType PartTypeHelper::fromFqName(const QByteArray& fqName)
return fromFqName( QLatin1String(fqName) );
}
+PartType PartTypeHelper::fromFqName(const QString& ns, const QString& name)
+{
+ PartType partType = PartType::retrieveByFQName(ns, name);
+ if (!partType.isValid()) {
+ PartType pt(name, ns);
+ if (!pt.insert()) {
+ throw PartTypeException( "Failed to append part type" );
+ }
+ partType = pt;
+ }
+ return partType;
+}
+
Query::Condition PartTypeHelper::conditionFromFqName(const QString& fqName)
{
const QPair<QString, QString> p = parseFqName( fqName );
diff --git a/server/src/storage/parttypehelper.h b/server/src/storage/parttypehelper.h
index 4c4f42f..6d3cf74 100644
--- a/server/src/storage/parttypehelper.h
+++ b/server/src/storage/parttypehelper.h
@@ -48,6 +48,14 @@ namespace PartTypeHelper
PartType fromFqName( const QByteArray &fqName );
/**
+ * Retrieve (or create) PartType for the given namespace and name
+ * @param ns Namespace
+ * @param name Name
+ * @throws PartTypeException
+ */
+ PartType fromFqName( const QString &ns, const QString &name );
+
+ /**
* Returns a query condition that matches the given part.
* @param fqName fully-qualified part type name
* @throws PartTypeException
--
2.1.0