Commit cb86eff4 authored by Tobias Koenig's avatar Tobias Koenig
Browse files

Show error for name conflicts on move/copy

Show an error messagebox if a collection is moved or copied
to a collection that contains a child collection with the same
name already. This allows us to provide a better error message
than the default one from the failing CollectionMoveJob.

BUG: 244598
parent 8bfa7681
......@@ -143,10 +143,19 @@ void Collection::setParentRemoteId(const QString & remoteParent)
}
KUrl Collection::url() const
{
return url( UrlShort );
}
KUrl Collection::url( UrlType type ) const
{
KUrl url;
url.setProtocol( QString::fromLatin1( "akonadi" ) );
url.addQueryItem( QLatin1String( "collection" ), QString::number( id() ) );
if ( type == UrlWithName )
url.addQueryItem( QLatin1String( "name" ), name() );
return url;
}
......
......@@ -231,10 +231,30 @@ class AKONADI_EXPORT Collection : public Entity
void setStatistics( const CollectionStatistics &statistics );
/**
* Returns the collection url
* Returns the url of the collection.
*
* @todo KDE5 remove in favor of url( UrlType type = UrlShort ).
*/
KUrl url() const;
/**
* Describes the type of url which is returned in url().
*
* @since 4.7
*/
enum UrlType
{
UrlShort = 0, ///< A short url which contains the identifier only (equivalent to url())
UrlWithName = 1 ///< A url with identifier and name
};
/**
* Returns the url of the collection.
*
* @since 4.7
*/
KUrl url( UrlType type ) const;
/**
* Returns whether the collection is virtual, for example a search collection.
*
......
......@@ -31,6 +31,7 @@
#include <KDE/KIcon>
#include <KDE/KLocale>
#include <KDE/KMessageBox>
#include <KDE/KUrl>
#include <akonadi/attributefactory.h>
......@@ -494,6 +495,17 @@ bool EntityTreeModel::dropMimeData( const QMimeData * data, Qt::DropAction actio
kDebug() << "unwanted collection" << mimeChecker.wantedMimeTypes() << collection.contentMimeTypes();
return false;
}
if ( url.hasQueryItem( QLatin1String( "name" ) ) ) {
const QString collectionName = url.queryItemValue( QLatin1String( "name" ) );
const QStringList collectionNames = d->childCollectionNames( destCollection );
if ( collectionNames.contains( collectionName ) ) {
KMessageBox::error( 0, i18n( "The target collection '%1' contains already\na collection with name '%2'.",
destCollection.name(), collection.name() ) );
return false;
}
}
} else {
const Item item = d->m_items.value( Item::fromUrl( url ).id() );
if ( item.isValid() ) {
......@@ -684,7 +696,7 @@ QMimeData *EntityTreeModel::mimeData( const QModelIndexList &indexes ) const
const Node *node = reinterpret_cast<Node*>( index.internalPointer() );
if ( Node::Collection == node->type )
urls << d->m_collections.value( node->id ).url();
urls << d->m_collections.value( node->id ).url( Collection::UrlWithName );
else if ( Node::Item == node->type )
urls << d->m_items.value( node->id ).url( Item::UrlWithMimeType );
else // if that happens something went horrible wrong
......
......@@ -835,6 +835,20 @@ void EntityTreeModelPrivate::removeChildEntities( Collection::Id collectionId )
m_childEntities.remove( collectionId );
}
QStringList EntityTreeModelPrivate::childCollectionNames( const Collection &collection ) const
{
QStringList names;
QHashIterator<Collection::Id, Collection> it( m_collections );
while ( it.hasNext() ) {
it.next();
if ( it.value().parentCollection().id() == collection.id() )
names << it.value().name();
}
return names;
}
void EntityTreeModelPrivate::monitoredCollectionMoved( const Akonadi::Collection& collection,
const Akonadi::Collection& sourceCollection,
const Akonadi::Collection& destCollection )
......
......@@ -114,6 +114,12 @@ public:
Entity::Id childAt( Collection::Id, int position, bool *ok ) const;
Item getItem( Item::Id id ) const;
void removeChildEntities( Collection::Id collectionId );
/**
* Returns the list of names of the child collections of @p collection.
*/
QStringList childCollectionNames( const Collection &collection ) const;
/**
* Fetch parent collections and insert this @p collection and its parents into the node tree
*/
......
Supports Markdown
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