~ubuntu-branches/ubuntu/karmic/kdepim/karmic-backports

« back to all changes in this revision

Viewing changes to akonadi/resources/distlist/distlistresource.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi, Alessandro Ghersi, Harald Sitter
  • Date: 2009-06-27 04:40:05 UTC
  • mfrom: (1.1.39 upstream)
  • Revision ID: james.westby@ubuntu.com-20090627044005-4y2vm9xz7rvmzi4p
Tags: 4:4.2.95svn20090701-0ubuntu1
[ Alessandro Ghersi ]
* New upstream release
  - Bump build-deps
* Remove akonadi-kde and libmaildir4 packages
  - remove akonadi-kde.install and libmaildir4.install
  - remove libmaildir4 from debian/rules
  - remove akonadi-kde and libmaildir4 from depends
  - remove akonadi-kde and libmaildir4 from installgen
* Update kdepim-dev.install
* Update kpilot.install
* Add akonadi-kde and libmaildir4 transitional packages

[ Harald Sitter ]
* KAddressbook replaces Kontact << 4.2.85 (LP: #378373)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright (c) 2008 Bertjan Broeksema <b.broeksema@kdemail.net>
 
3
    Copyright (c) 2008 Kevin Krammer <kevin.krammer@gmx.at>
 
4
 
 
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.
 
9
 
 
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.
 
14
 
 
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
 
18
    02110-1301, USA.
 
19
*/
 
20
 
 
21
#include "distlistresource.h"
 
22
#include "settingsadaptor.h"
 
23
#include "singlefileresourceconfigdialog.h"
 
24
 
 
25
#include <kabc/contactgrouptool.h>
 
26
 
 
27
#include <kfiledialog.h>
 
28
#include <klocale.h>
 
29
#include <kmimetype.h>
 
30
#include <KWindowSystem>
 
31
 
 
32
#include <QtDBus/QDBusConnection>
 
33
using namespace Akonadi;
 
34
 
 
35
DistListResource::DistListResource( const QString &id )
 
36
  : SingleFileResource<Settings>( id ),
 
37
    mLegacyKConfigFormat( false )
 
38
{
 
39
  setSupportedMimetypes( QStringList() << KABC::ContactGroup::mimeType() );
 
40
 
 
41
  new SettingsAdaptor( Settings::self() );
 
42
  QDBusConnection::sessionBus().registerObject( QLatin1String( "/Settings" ),
 
43
                            Settings::self(), QDBusConnection::ExportAdaptors );
 
44
}
 
45
 
 
46
DistListResource::~DistListResource()
 
47
{
 
48
  mContactGroups.clear();
 
49
}
 
50
 
 
51
void DistListResource::configure( WId windowId )
 
52
{
 
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 ) {
 
57
    reloadFile();
 
58
  }
 
59
}
 
60
 
 
61
void DistListResource::retrieveItems( const Akonadi::Collection & col )
 
62
{
 
63
  // distributionlists does not support folders so we can safely ignore the collection
 
64
  Q_UNUSED( col );
 
65
 
 
66
  Item::List items;
 
67
 
 
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.
 
71
 
 
72
  foreach ( const KABC::ContactGroup &contactGroup, mContactGroups ) {
 
73
    Item item;
 
74
    item.setRemoteId( contactGroup.id() );
 
75
    item.setMimeType( KABC::ContactGroup::mimeType() );
 
76
    item.setPayload( contactGroup );
 
77
    items.append( item );
 
78
  }
 
79
 
 
80
  itemsRetrieved( items );
 
81
}
 
82
 
 
83
bool DistListResource::retrieveItem( const Akonadi::Item &item, const QSet<QByteArray> &parts )
 
84
{
 
85
  Q_UNUSED( parts );
 
86
  const QString rid = item.remoteId();
 
87
  if ( !mContactGroups.contains( rid ) ) {
 
88
    emit error( QString( "ContactGroup with uid '%1' not found!" ).arg( rid ) );
 
89
    return false;
 
90
  }
 
91
  Item i( item );
 
92
  i.setPayload<KABC::ContactGroup>( mContactGroups.value( rid ) );
 
93
  itemRetrieved( i );
 
94
  return true;
 
95
}
 
96
 
 
97
bool DistListResource::readFromFile( const QString &fileName )
 
98
{
 
99
  mContactGroups.clear();
 
100
 
 
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 ) );
 
104
    return false;
 
105
  }
 
106
 
 
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 );
 
114
 
 
115
    file.close();
 
116
 
 
117
    foreach( const KABC::ContactGroup &group, list ) {
 
118
      mContactGroups.insert( group.id(), group );
 
119
    }
 
120
  } else {
 
121
    kDebug() << "Checking for legacy format";
 
122
    file.close();
 
123
 
 
124
    KConfig cfg( fileName );
 
125
    if ( cfg.hasGroup( "DistributionLists" ) ) {
 
126
      mLegacyKConfigFormat = true;
 
127
 
 
128
      KConfigGroup cg( &cfg, "DistributionLists" );
 
129
      KConfigGroup cgId( &cfg, "DistributionLists-Identifiers" );
 
130
      const QStringList entryList = cg.keyList();
 
131
 
 
132
      foreach ( const QString &entry, entryList ) {
 
133
        const QStringList value = cg.readEntry( entry, QStringList() );
 
134
 
 
135
        QString id;
 
136
        if ( cgId.isValid() )
 
137
          id = cgId.readEntry( entry, QString() );
 
138
 
 
139
        KABC::ContactGroup contactGroup( entry );
 
140
        if ( !id.isEmpty() )
 
141
          contactGroup.setId( id );
 
142
 
 
143
        QStringList::const_iterator entryIt = value.constBegin();
 
144
        while ( entryIt != value.constEnd() ) {
 
145
          const QString id = *entryIt;
 
146
          ++entryIt;
 
147
 
 
148
          const QString email = entryIt != value.constEnd() ? *entryIt : QString();
 
149
 
 
150
          KABC::ContactGroup::ContactReference reference( id );
 
151
          if ( !email.isEmpty() )
 
152
            reference.setPreferredEmail( email );
 
153
 
 
154
          contactGroup.append( reference );
 
155
 
 
156
          if ( entryIt == value.constEnd() )
 
157
            break;
 
158
 
 
159
          ++entryIt;
 
160
        }
 
161
 
 
162
        mContactGroups.insert( contactGroup.id(), contactGroup );
 
163
      }
 
164
    }
 
165
  }
 
166
 
 
167
  return true;
 
168
}
 
169
 
 
170
bool DistListResource::writeToFile( const QString &fileName )
 
171
{
 
172
  QFile file( fileName );
 
173
  if ( !file.open( QIODevice::WriteOnly ) ) {
 
174
    emit status( Broken, i18n( "Unable to open distribution list file '%1'.", fileName ) );
 
175
    return false;
 
176
  }
 
177
 
 
178
  if ( mLegacyKConfigFormat ) {
 
179
    file.close();
 
180
 
 
181
    KConfig cfg( fileName );
 
182
    KConfigGroup cg( &cfg, "DistributionLists" );
 
183
    cg.deleteGroup();
 
184
    KConfigGroup cgId( &cfg, "DistributionLists-Identifiers" );
 
185
    cgId.deleteGroup();
 
186
 
 
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();
 
191
 
 
192
      QStringList value;
 
193
      for ( unsigned int index = 0; index < contactGroup.contactReferenceCount(); ++index ) {
 
194
        const KABC::ContactGroup::ContactReference reference = contactGroup.contactReference( index );
 
195
 
 
196
        value.append( reference.uid() );
 
197
        value.append( reference.preferredEmail() );
 
198
      }
 
199
 
 
200
      cg.writeEntry( contactGroup.name(), value );
 
201
      cgId.writeEntry( contactGroup.name(), contactGroup.id() );
 
202
    }
 
203
 
 
204
    cfg.sync();
 
205
    return true;
 
206
  }
 
207
 
 
208
  // TODO check for error
 
209
  KABC::ContactGroupTool::convertToXml( mContactGroups.values(), &file );
 
210
 
 
211
  file.close();
 
212
 
 
213
  return true;
 
214
}
 
215
 
 
216
void DistListResource::aboutToQuit()
 
217
{
 
218
  writeFile();
 
219
  Settings::self()->writeConfig();
 
220
}
 
221
 
 
222
void DistListResource::itemAdded( const Akonadi::Item &item, const Akonadi::Collection& )
 
223
{
 
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>();
 
229
 
 
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." ) );
 
236
        changeProcessed();
 
237
        return;
 
238
      }
 
239
    }
 
240
    mContactGroups.insert( contactGroup.id(), contactGroup );
 
241
 
 
242
    Item i( item );
 
243
    i.setRemoteId( contactGroup.id() );
 
244
    changeCommitted( i );
 
245
 
 
246
    fileDirty();
 
247
  } else {
 
248
    changeProcessed();
 
249
  }
 
250
}
 
251
 
 
252
void DistListResource::itemChanged( const Akonadi::Item &item, const QSet<QByteArray>& )
 
253
{
 
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>();
 
259
 
 
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." ) );
 
266
        changeProcessed();
 
267
        return;
 
268
      }
 
269
    }
 
270
    mContactGroups.insert( contactGroup.id(), contactGroup );
 
271
 
 
272
    Item i( item );
 
273
    i.setRemoteId( contactGroup.id() );
 
274
    changeCommitted( i );
 
275
 
 
276
    fileDirty();
 
277
  } else {
 
278
    changeProcessed();
 
279
  }
 
280
}
 
281
 
 
282
void DistListResource::itemRemoved(const Akonadi::Item & item)
 
283
{
 
284
  kDebug() << "item id="  << item.id() << ", remoteId=" << item.remoteId()
 
285
           << "mimeType=" << item.mimeType();
 
286
  if ( mContactGroups.contains( item.remoteId() ) )
 
287
    mContactGroups.remove( item.remoteId() );
 
288
 
 
289
  fileDirty();
 
290
 
 
291
  changeProcessed();
 
292
}
 
293
 
 
294
AKONADI_RESOURCE_MAIN( DistListResource )
 
295
 
 
296
#include "distlistresource.moc"
 
297
// kate: space-indent on; indent-width 2; replace-tabs on;