<commit_before>/*
    Copyright (c) 2008 <NAME> <<EMAIL>>

    This library is free software; you can redistribute it and/or modify it
    under the terms of the GNU Library General Public License as published by
    the Free Software Foundation; either version 2 of the License, or (at your
    option) any later version.

    This library is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
    License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to the
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
*/

#include "copy.h"

#include "akonadiconnection.h"
#include "handlerhelper.h"

#include "storage/datastore.h"
#include "storage/itemqueryhelper.h"
#include "storage/itemretriever.h"
#include "storage/selectquerybuilder.h"
#include "storage/transaction.h"
#include "storage/parthelper.h"

#include "libs/imapset_p.h"
#include "imapstreamparser.h"

using namespace Akonadi;

bool Copy::copyItem(const PimItem & item, const Collection & target)
{
  qDebug() << "Copy::copyItem";
  DataStore *store = connection()->storageBackend();
  PimItem newItem = item;
  newItem.setId( -1 );
  newItem.setRev( 0 );
  newItem.setDatetime( QDateTime::currentDateTime() );
  newItem.setAtime( QDateTime::currentDateTime() );
  newItem.setRemoteId( QString() );
  newItem.setRemoteRevision( QString() );
  newItem.setCollectionId( target.id() );
  Part::List parts;
  foreach ( const Part &part, item.parts() ) {
    Part newPart( part );
    newPart.setData(PartHelper::translateData(newPart.id(), newPart.data(), part.external()));
    newPart.setPimItemId( -1 );
    parts << newPart;
  }
  if ( !store->appendPimItem( parts, item.mimeType(), target, QDateTime::currentDateTime(), QString(), QString(), newItem ) )
    return false;
  foreach ( const Flag &flag, item.flags() ) {
    if ( !newItem.addFlag( flag ) )
      return false;
  }
  return true;
}

bool Copy::parseStream()
{
  ImapSet set = m_streamParser->readSequenceSet();
  if ( set.isEmpty() )
    return failureResponse( "No items specified" );

  ItemRetriever retriever( connection() );
  retriever.setItemSet( set );
  retriever.setRetrieveFullPayload( true );
  retriever.exec();

  const QByteArray tmp = m_streamParser->readString();
  const Collection col = HandlerHelper::collectionFromIdOrName( tmp );
  if ( !col.isValid() )
    return failureResponse( "No valid target specified" );

  SelectQueryBuilder<PimItem> qb;
  ItemQueryHelper::itemSetToQuery( set, qb );
  if ( !qb.exec() )
    return failureResponse( "Unable to retrieve items" );
  PimItem::List items = qb.result();

  DataStore *store = connection()->storageBackend();
  Transaction transaction( store );

  foreach ( const PimItem &item, items ) {
    if ( !copyItem( item, col ) )
      return failureResponse( "Unable to copy item" );
  }

  if ( !transaction.commit() )
    return failureResponse( "Cannot commit transaction." );

  return successResponse( "COPY complete" );
}

#include "copy.moc"<commit_msg>Rename col to target collection and move the DataStore variable to the place where it is actually used. Makes the code slightly more readable.<commit_after>/*
    Copyright (c) 2008 <NAME> <<EMAIL>>

    This library is free software; you can redistribute it and/or modify it
    under the terms of the GNU Library General Public License as published by
    the Free Software Foundation; either version 2 of the License, or (at your
    option) any later version.

    This library is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
    License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to the
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
*/

#include "copy.h"

#include "akonadiconnection.h"
#include "handlerhelper.h"

#include "storage/datastore.h"
#include "storage/itemqueryhelper.h"
#include "storage/itemretriever.h"
#include "storage/selectquerybuilder.h"
#include "storage/transaction.h"
#include "storage/parthelper.h"

#include "libs/imapset_p.h"
#include "imapstreamparser.h"

using namespace Akonadi;

bool Copy::copyItem(const PimItem & item, const Collection & target)
{
  qDebug() << "Copy::copyItem";

  PimItem newItem = item;
  newItem.setId( -1 );
  newItem.setRev( 0 );
  newItem.setDatetime( QDateTime::currentDateTime() );
  newItem.setAtime( QDateTime::currentDateTime() );
  newItem.setRemoteId( QString() );
  newItem.setRemoteRevision( QString() );
  newItem.setCollectionId( target.id() );
  Part::List parts;
  foreach ( const Part &part, item.parts() ) {
    Part newPart( part );
    newPart.setData(PartHelper::translateData(newPart.id(), newPart.data(), part.external()));
    newPart.setPimItemId( -1 );
    parts << newPart;
  }

  DataStore *store = connection()->storageBackend();
  if ( !store->appendPimItem( parts, item.mimeType(), target, QDateTime::currentDateTime(), QString(), QString(), newItem ) )
    return false;
  foreach ( const Flag &flag, item.flags() ) {
    if ( !newItem.addFlag( flag ) )
      return false;
  }
  return true;
}

bool Copy::parseStream()
{
  ImapSet set = m_streamParser->readSequenceSet();
  if ( set.isEmpty() )
    return failureResponse( "No items specified" );

  ItemRetriever retriever( connection() );
  retriever.setItemSet( set );
  retriever.setRetrieveFullPayload( true );
  retriever.exec();

  const QByteArray tmp = m_streamParser->readString();
  const Collection targetCollection = HandlerHelper::collectionFromIdOrName( tmp );
  if ( !targetCollection.isValid() )
    return failureResponse( "No valid target specified" );

  SelectQueryBuilder<PimItem> qb;
  ItemQueryHelper::itemSetToQuery( set, qb );
  if ( !qb.exec() )
    return failureResponse( "Unable to retrieve items" );
  PimItem::List items = qb.result();
  qb.query().finish();

  DataStore *store = connection()->storageBackend();
  Transaction transaction( store );

  foreach ( const PimItem &item, items ) {
    if ( !copyItem( item, targetCollection ) )
      return failureResponse( "Unable to copy item" );
  }

  if ( !transaction.commit() )
    return failureResponse( "Cannot commit transaction." );

  return successResponse( "COPY complete" );
}

#include "copy.moc"
{'fraction_non_alphanumeric': 0.11587982832618025, 'fraction_numerical': 0.005623797543288442, 'mean_word_length': 4.186492709132771, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7630592', 'n_tokens_mistral': 2051, 'n_tokens_neox': 1996, 'n_words': 639}