30
30
using namespace Akonadi;
32
bool ColCopy::copyCollection(const Collection & source, const Collection & target)
32
bool ColCopy::copyCollection( const Collection &source, const Collection &target )
34
34
if ( !CollectionQueryHelper::canBeMovedTo( source, target ) ) {
35
35
// We don't accept source==target, or source being an ancestor of target.
49
49
DataStore *db = connection()->storageBackend();
50
if ( !db->appendCollection( col ) )
50
if ( !db->appendCollection( col ) ) {
53
54
Q_FOREACH ( const MimeType &mt, source.mimeTypes() ) {
54
if ( !col.addMimeType( mt ) )
55
if ( !col.addMimeType( mt ) ) {
58
60
Q_FOREACH ( const CollectionAttribute &attr, source.attributes() ) {
59
61
CollectionAttribute newAttr = attr;
60
62
newAttr.setId( -1 );
61
63
newAttr.setCollectionId( col.id() );
62
if ( !newAttr.insert() )
64
if ( !newAttr.insert() ) {
66
69
// copy sub-collections
67
70
Q_FOREACH ( const Collection &child, source.children() ) {
68
if ( !copyCollection( child, col ) )
71
if ( !copyCollection( child, col ) ) {
73
77
Q_FOREACH ( const PimItem &item, source.items() ) {
74
if ( !copyItem( item, col ) )
78
if ( !copyItem( item, col ) ) {
81
86
bool ColCopy::parseStream()
83
QByteArray tmp = m_streamParser->readString();
88
QByteArray tmp = m_streamParser->readString();
84
89
const Collection source = HandlerHelper::collectionFromIdOrName( tmp );
85
if ( !source.isValid() )
90
if ( !source.isValid() ) {
86
91
return failureResponse( "No valid source specified" );
88
tmp = m_streamParser->readString();
94
tmp = m_streamParser->readString();
89
95
const Collection target = HandlerHelper::collectionFromIdOrName( tmp );
90
if ( !target.isValid() )
96
if ( !target.isValid() ) {
91
97
return failureResponse( "No valid target specified" );
93
100
// retrieve all not yet cached items of the source
94
101
ItemRetriever retriever( connection() );
95
102
retriever.setCollection( source, true );
96
103
retriever.setRetrieveFullPayload( true );
97
if (!retriever.exec()) {
104
if ( !retriever.exec() ) {
98
105
return failureResponse( retriever.lastError() );
101
108
DataStore *store = connection()->storageBackend();
102
109
Transaction transaction( store );
104
if ( !copyCollection( source, target ) )
111
if ( !copyCollection( source, target ) ) {
105
112
return failureResponse( "Failed to copy collection" );
107
if ( !transaction.commit() )
115
if ( !transaction.commit() ) {
108
116
return failureResponse( "Cannot commit transaction." );
110
119
return successResponse( "COLCOPY complete" );