2
Copyright (c) 2008 Bertjan Broeksema <b.broeksema@kdemail.net>
3
Copyright (c) 2008 Kevin Krammer <kevin.krammer@gmx.at>
5
This library is free software; you can redistribute it and/or modify it
6
under the terms of the GNU Library General Public License as published by
7
the Free Software Foundation; either version 2 of the License, or (at your
8
option) any later version.
10
This library is distributed in the hope that it will be useful, but WITHOUT
11
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13
License for more details.
15
You should have received a copy of the GNU Library General Public License
16
along with this library; see the file COPYING.LIB. If not, write to the
17
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21
#include "distlistresource.h"
22
#include "settingsadaptor.h"
23
#include "singlefileresourceconfigdialog.h"
25
#include <kabc/contactgrouptool.h>
27
#include <kfiledialog.h>
29
#include <kmimetype.h>
30
#include <KWindowSystem>
32
#include <QtDBus/QDBusConnection>
33
using namespace Akonadi;
35
DistListResource::DistListResource( const QString &id )
36
: SingleFileResource<Settings>( id ),
37
mLegacyKConfigFormat( false )
39
setSupportedMimetypes( QStringList() << KABC::ContactGroup::mimeType() );
41
new SettingsAdaptor( Settings::self() );
42
QDBusConnection::sessionBus().registerObject( QLatin1String( "/Settings" ),
43
Settings::self(), QDBusConnection::ExportAdaptors );
46
DistListResource::~DistListResource()
48
mContactGroups.clear();
51
void DistListResource::configure( WId windowId )
53
SingleFileResourceConfigDialog<Settings> dlg( windowId );
54
dlg.setFilter( "*.distlist|" + i18nc("Filedialog filter for *.distlist", "Distribution List File" ) );
55
dlg.setCaption( i18n("Select Distribution List File") );
56
if ( dlg.exec() == QDialog::Accepted ) {
61
void DistListResource::retrieveItems( const Akonadi::Collection & col )
63
// distributionlists does not support folders so we can safely ignore the collection
68
// FIXME: Check if the KIO::Job is done and was successful, if so send the
69
// items, otherwise set a bool and in the result slot of the job send the
70
// items if the bool is set.
72
foreach ( const KABC::ContactGroup &contactGroup, mContactGroups ) {
74
item.setRemoteId( contactGroup.id() );
75
item.setMimeType( KABC::ContactGroup::mimeType() );
76
item.setPayload( contactGroup );
80
itemsRetrieved( items );
83
bool DistListResource::retrieveItem( const Akonadi::Item &item, const QSet<QByteArray> &parts )
86
const QString rid = item.remoteId();
87
if ( !mContactGroups.contains( rid ) ) {
88
emit error( QString( "ContactGroup with uid '%1' not found!" ).arg( rid ) );
92
i.setPayload<KABC::ContactGroup>( mContactGroups.value( rid ) );
97
bool DistListResource::readFromFile( const QString &fileName )
99
mContactGroups.clear();
101
QFile file( KUrl( fileName ).path() );
102
if ( !file.open( QIODevice::ReadOnly ) ) {
103
emit status( Broken, i18n( "Unable to open distribution list file '%1'.", fileName ) );
107
const QByteArray peekData = file.peek( 100 );
108
KMimeType::Ptr contentType = KMimeType::findByContent( peekData );
109
if ( contentType->is( QLatin1String( "application/xml" ) ) ) {
110
kDebug() << "File format is XML based";
111
KABC::ContactGroup::List list;
112
// TODO check for error
113
KABC::ContactGroupTool::convertFromXml( &file, list );
117
foreach( const KABC::ContactGroup &group, list ) {
118
mContactGroups.insert( group.id(), group );
121
kDebug() << "Checking for legacy format";
124
KConfig cfg( fileName );
125
if ( cfg.hasGroup( "DistributionLists" ) ) {
126
mLegacyKConfigFormat = true;
128
KConfigGroup cg( &cfg, "DistributionLists" );
129
KConfigGroup cgId( &cfg, "DistributionLists-Identifiers" );
130
const QStringList entryList = cg.keyList();
132
foreach ( const QString &entry, entryList ) {
133
const QStringList value = cg.readEntry( entry, QStringList() );
136
if ( cgId.isValid() )
137
id = cgId.readEntry( entry, QString() );
139
KABC::ContactGroup contactGroup( entry );
141
contactGroup.setId( id );
143
QStringList::const_iterator entryIt = value.constBegin();
144
while ( entryIt != value.constEnd() ) {
145
const QString id = *entryIt;
148
const QString email = entryIt != value.constEnd() ? *entryIt : QString();
150
KABC::ContactGroup::ContactReference reference( id );
151
if ( !email.isEmpty() )
152
reference.setPreferredEmail( email );
154
contactGroup.append( reference );
156
if ( entryIt == value.constEnd() )
162
mContactGroups.insert( contactGroup.id(), contactGroup );
170
bool DistListResource::writeToFile( const QString &fileName )
172
QFile file( fileName );
173
if ( !file.open( QIODevice::WriteOnly ) ) {
174
emit status( Broken, i18n( "Unable to open distribution list file '%1'.", fileName ) );
178
if ( mLegacyKConfigFormat ) {
181
KConfig cfg( fileName );
182
KConfigGroup cg( &cfg, "DistributionLists" );
184
KConfigGroup cgId( &cfg, "DistributionLists-Identifiers" );
187
QMap<QString, KABC::ContactGroup>::const_iterator it = mContactGroups.constBegin();
188
QMap<QString, KABC::ContactGroup>::const_iterator endIt = mContactGroups.constEnd();
189
for ( ; it != endIt; ++it ) {
190
const KABC::ContactGroup contactGroup = it.value();
193
for ( unsigned int index = 0; index < contactGroup.contactReferenceCount(); ++index ) {
194
const KABC::ContactGroup::ContactReference reference = contactGroup.contactReference( index );
196
value.append( reference.uid() );
197
value.append( reference.preferredEmail() );
200
cg.writeEntry( contactGroup.name(), value );
201
cgId.writeEntry( contactGroup.name(), contactGroup.id() );
208
// TODO check for error
209
KABC::ContactGroupTool::convertToXml( mContactGroups.values(), &file );
216
void DistListResource::aboutToQuit()
219
Settings::self()->writeConfig();
222
void DistListResource::itemAdded( const Akonadi::Item &item, const Akonadi::Collection& )
224
kDebug() << "item id=" << item.id() << ", remoteId=" << item.remoteId()
225
<< "mimeType=" << item.mimeType();
226
KABC::ContactGroup contactGroup;
227
if ( item.hasPayload<KABC::ContactGroup>() )
228
contactGroup = item.payload<KABC::ContactGroup>();
230
if ( !contactGroup.id().isEmpty() ) {
231
if ( mLegacyKConfigFormat ) {
232
// legacy format only supports ContactGroup::Reference style data
233
if ( contactGroup.dataCount() > 0 ) {
234
error( i18nc( "@info:status",
235
"Current storage format cannot handle distribution list entries for which no address book entry exists." ) );
240
mContactGroups.insert( contactGroup.id(), contactGroup );
243
i.setRemoteId( contactGroup.id() );
244
changeCommitted( i );
252
void DistListResource::itemChanged( const Akonadi::Item &item, const QSet<QByteArray>& )
254
kDebug() << "item id=" << item.id() << ", remoteId=" << item.remoteId()
255
<< "mimeType=" << item.mimeType();
256
KABC::ContactGroup contactGroup;
257
if ( item.hasPayload<KABC::ContactGroup>() )
258
contactGroup = item.payload<KABC::ContactGroup>();
260
if ( !contactGroup.id().isEmpty() ) {
261
if ( mLegacyKConfigFormat ) {
262
// legacy format only supports ContactGroup::Reference style data
263
if ( contactGroup.dataCount() > 0 ) {
264
error( i18nc( "@info:status",
265
"Current storage format cannot handle distribution list entries for which no address book entry exists." ) );
270
mContactGroups.insert( contactGroup.id(), contactGroup );
273
i.setRemoteId( contactGroup.id() );
274
changeCommitted( i );
282
void DistListResource::itemRemoved(const Akonadi::Item & item)
284
kDebug() << "item id=" << item.id() << ", remoteId=" << item.remoteId()
285
<< "mimeType=" << item.mimeType();
286
if ( mContactGroups.contains( item.remoteId() ) )
287
mContactGroups.remove( item.remoteId() );
294
AKONADI_RESOURCE_MAIN( DistListResource )
296
#include "distlistresource.moc"
297
// kate: space-indent on; indent-width 2; replace-tabs on;