1/*
2 Copyright (c) 2008 Kevin Krammer <kevin.krammer@gmx.at>
3
4 This library is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at your
7 option) any later version.
8
9 This library is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to the
16 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA.
18*/
19
20#include "akonadi_serializer_contactgroup.h"
21
22#include <akonadi/abstractdifferencesreporter.h>
23#include <akonadi/contact/contactgroupexpandjob.h>
24#include <akonadi/item.h>
25#include <akonadi/kabc/contactparts.h>
26
27#include <kabc/contactgroup.h>
28#include <kabc/contactgrouptool.h>
29#include <klocale.h>
30
31#include <QtCore/qplugin.h>
32
33using namespace Akonadi;
34
35//// ItemSerializerPlugin interface
36
37bool SerializerPluginContactGroup::deserialize( Item& item, const QByteArray& label, QIODevice& data, int version )
38{
39 Q_UNUSED( label );
40 Q_UNUSED( version );
41
42 KABC::ContactGroup contactGroup;
43
44 if ( !KABC::ContactGroupTool::convertFromXml( &data, contactGroup ) ) {
45 // TODO: error reporting
46 return false;
47 }
48
49 item.setPayload<KABC::ContactGroup>( contactGroup );
50
51 return true;
52}
53
54void SerializerPluginContactGroup::serialize( const Item& item, const QByteArray& label, QIODevice& data, int &version )
55{
56 Q_UNUSED( label );
57 Q_UNUSED( version );
58
59 if ( !item.hasPayload<KABC::ContactGroup>() )
60 return;
61
62 KABC::ContactGroupTool::convertToXml( item.payload<KABC::ContactGroup>(), &data );
63}
64
65//// DifferencesAlgorithmInterface interface
66
67static bool compareString( const QString &left, const QString &right )
68{
69 if ( left.isEmpty() && right.isEmpty() )
70 return true;
71 else
72 return left == right;
73}
74
75static QString toString( const KABC::Addressee &contact )
76{
77 return contact.fullEmail();
78}
79
80template <class T>
81static void compareList( AbstractDifferencesReporter *reporter, const QString &id, const QList<T> &left, const QList<T> &right )
82{
83 for ( int i = 0; i < left.count(); ++i ) {
84 if ( !right.contains( left[ i ] ) )
85 reporter->addProperty( AbstractDifferencesReporter::AdditionalLeftMode, id, toString( left[ i ] ), QString() );
86 }
87
88 for ( int i = 0; i < right.count(); ++i ) {
89 if ( !left.contains( right[ i ] ) )
90 reporter->addProperty( AbstractDifferencesReporter::AdditionalRightMode, id, QString(), toString( right[ i ] ) );
91 }
92}
93
94void SerializerPluginContactGroup::compare( Akonadi::AbstractDifferencesReporter *reporter,
95 const Akonadi::Item &leftItem,
96 const Akonadi::Item &rightItem )
97{
98 Q_ASSERT( reporter );
99 Q_ASSERT( leftItem.hasPayload<KABC::ContactGroup>() );
100 Q_ASSERT( rightItem.hasPayload<KABC::ContactGroup>() );
101
102 reporter->setLeftPropertyValueTitle( i18n( "Changed Contact Group" ) );
103 reporter->setRightPropertyValueTitle( i18n( "Conflicting Contact Group" ) );
104
105 const KABC::ContactGroup leftContactGroup = leftItem.payload<KABC::ContactGroup>();
106 const KABC::ContactGroup rightContactGroup = rightItem.payload<KABC::ContactGroup>();
107
108 if ( !compareString( leftContactGroup.name(), rightContactGroup.name() ) )
109 reporter->addProperty( AbstractDifferencesReporter::ConflictMode, i18n( "Name" ),
110 leftContactGroup.name(), rightContactGroup.name() );
111
112 // using job->exec() is ok here, not a hot path
113 Akonadi::ContactGroupExpandJob *leftJob = new Akonadi::ContactGroupExpandJob( leftContactGroup );
114 leftJob->exec();
115
116 Akonadi::ContactGroupExpandJob *rightJob = new Akonadi::ContactGroupExpandJob( rightContactGroup );
117 rightJob->exec();
118
119 compareList( reporter, i18n( "Member" ), leftJob->contacts(), rightJob->contacts() );
120}
121
122//// GidExtractorInterface
123
124QString SerializerPluginContactGroup::extractGid( const Item &item ) const
125{
126 if ( !item.hasPayload<KABC::ContactGroup>() ) {
127 return QString();
128 }
129 return item.payload<KABC::ContactGroup>().id();
130}
131
132Q_EXPORT_PLUGIN2( akonadi_serializer_contactgroup, Akonadi::SerializerPluginContactGroup )
133
134// kate: space-indent on; indent-width 2; replace-tabs on;
135