<commit_before>/* contactsakonadidataproxy.h			KPilot
**
** Copyright (C) 2008 by <NAME> <<EMAIL>>
** Copyright (C) 2008 by Jason "vanRijn" Kasper <<EMAIL>>
*/

/*
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU Lesser General Public License as published by
** the Free Software Foundation; either version 2.1 of the License, or
** (at your option) any later version.
**
** This program 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 Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public License
** along with this program in a file called COPYING; if not, write to
** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
** MA 02110-1301, USA.
*/

/*
** Bug reports and questions can be sent to <EMAIL>
*/

#include "contactsakonadidataproxy.h"

#include <akonadi/control.h>
#include <akonadi/collection.h>
#include <akonadi/itemcreatejob.h>
#include <akonadi/itemmodifyjob.h>
#include <akonadi/itemdeletejob.h>
#include <akonadi/itemfetchjob.h>
#include <akonadi/itemfetchscope.h>
#include <kabc/addressee.h>

#include "akonadicontact.h"
#include "idmapping.h"
#include "options.h"

ContactsAkonadiDataProxy::ContactsAkonadiDataProxy( Entity::Id id, const IDMapping* mapping   )
	: fId( id ), fMapping( mapping )
{
	FUNCTIONSETUP;
	
	// Lets make sure that Akonadi is started.
	if ( !Control::start() )
	{
		DEBUGKPILOT << "Error: Could not start Akonadi.";
	}
}

ContactsAkonadiDataProxy::~ContactsAkonadiDataProxy()
{
	FUNCTIONSETUP;
}

void ContactsAkonadiDataProxy::addCategory( Record* rec, const QString& category )
{
	FUNCTIONSETUP;
	
	AkonadiContact* aRec = static_cast<AkonadiContact*>( rec );
	aRec->addCategory( category );
}

bool ContactsAkonadiDataProxy::createDataStore()
{
	FUNCTIONSETUP;
	// TODO: Implement
	
	return false;
}

bool ContactsAkonadiDataProxy::isOpen() const
{
	FUNCTIONSETUP;
	
	return Control::start();
}

void ContactsAkonadiDataProxy::loadAllRecords()
{
	FUNCTIONSETUP;
	
	// Fetch all items with full payload from the root collection
	ItemFetchJob* job = new ItemFetchJob( Collection( fId ) );
	job->fetchScope().fetchFullPayload();
	
	if ( job->exec() ) {
		Item::List items = job->items();
		foreach( const Item &item, items )
		{
			if( item.hasPayload<KABC::Addressee>() )
			{
				AkonadiContact* ac = new AkonadiContact( item, fMapping->lastSyncedDate() );
				fRecords.insert( ac->id(), ac );
			}
		}
		
		fCounter.setStartCount( fRecords.size() );
		
		// Now add dummy records for deleted records.
		foreach( const QString& mPcId, fMapping->pcRecordIds() )
		{
			if( !fRecords.contains( mPcId ) )
			{
				// Well the record with id mPcId doesn't seem to be in the akonadi
				// resource any more so it is deleted.
				AkonadiContact* ac = new DeletedAkonadiContact();
				ac->setId( mPcId );
				fRecords.insert( mPcId, ac );
			}
		}
		
		DEBUGKPILOT << "Loaded " << fRecords.size() << " records.";
	}
	else
	{
		DEBUGKPILOT << "Could not load records, is akonadi running?";
	}
}

void ContactsAkonadiDataProxy::setCategory( Record* rec, const QString& category )
{<commit_msg>Set the start count after reading the deleted records and not before.<commit_after>/* contactsakonadidataproxy.h			KPilot
**
** Copyright (C) 2008 by <NAME> <<EMAIL>>
** Copyright (C) 2008 by Jason "vanRijn" Kasper <<EMAIL>>
*/

/*
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU Lesser General Public License as published by
** the Free Software Foundation; either version 2.1 of the License, or
** (at your option) any later version.
**
** This program 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 Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public License
** along with this program in a file called COPYING; if not, write to
** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
** MA 02110-1301, USA.
*/

/*
** Bug reports and questions can be sent to <EMAIL>
*/

#include "contactsakonadidataproxy.h"

#include <akonadi/control.h>
#include <akonadi/collection.h>
#include <akonadi/itemcreatejob.h>
#include <akonadi/itemmodifyjob.h>
#include <akonadi/itemdeletejob.h>
#include <akonadi/itemfetchjob.h>
#include <akonadi/itemfetchscope.h>
#include <kabc/addressee.h>

#include "akonadicontact.h"
#include "idmapping.h"
#include "options.h"

ContactsAkonadiDataProxy::ContactsAkonadiDataProxy( Entity::Id id, const IDMapping* mapping   )
	: fId( id ), fMapping( mapping )
{
	FUNCTIONSETUP;
	
	// Lets make sure that Akonadi is started.
	if ( !Control::start() )
	{
		DEBUGKPILOT << "Error: Could not start Akonadi.";
	}
}

ContactsAkonadiDataProxy::~ContactsAkonadiDataProxy()
{
	FUNCTIONSETUP;
}

void ContactsAkonadiDataProxy::addCategory( Record* rec, const QString& category )
{
	FUNCTIONSETUP;
	
	AkonadiContact* aRec = static_cast<AkonadiContact*>( rec );
	aRec->addCategory( category );
}

bool ContactsAkonadiDataProxy::createDataStore()
{
	FUNCTIONSETUP;
	// TODO: Implement
	
	return false;
}

bool ContactsAkonadiDataProxy::isOpen() const
{
	FUNCTIONSETUP;
	
	return Control::start();
}

void ContactsAkonadiDataProxy::loadAllRecords()
{
	FUNCTIONSETUP;
	
	// Fetch all items with full payload from the root collection
	ItemFetchJob* job = new ItemFetchJob( Collection( fId ) );
	job->fetchScope().fetchFullPayload();
	
	if ( job->exec() ) {
		Item::List items = job->items();
		foreach( const Item &item, items )
		{
			if( item.hasPayload<KABC::Addressee>() )
			{
				AkonadiContact* ac = new AkonadiContact( item, fMapping->lastSyncedDate() );
				fRecords.insert( ac->id(), ac );
			}
		}
		
		// Now add dummy records for deleted records.
		foreach( const QString& mPcId, fMapping->pcRecordIds() )
		{
			if( !fRecords.contains( mPcId ) )
			{
				// Well the record with id mPcId doesn't seem to be in the akonadi
				// resource any more so it is deleted.
				AkonadiContact* ac = new DeletedAkonadiContact();
				ac->setId( mPcId );
				fRecords.insert( mPcId, ac );
			}
		}
		
		fCounter.setStartCount( fRecords.size() );
		
		DEBUGKPILOT << "Loaded " << fRecords.size() << " records.";
	}
	else
	{
		DEBUGKPILOT << "Could not load records, is akonadi running?";
	}
}

void ContactsAkonadiDataProxy::setCategory( Record* rec, const QString& category )
{
{'fraction_non_alphanumeric': 0.11853512705530643, 'fraction_numerical': 0.006278026905829596, 'mean_word_length': 4.314535345512311, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 51, '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': '4621980', 'n_tokens_mistral': 2346, 'n_tokens_neox': 2167, 'n_words': 677}