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

« back to all changes in this revision

Viewing changes to akonadi/resources/ical/icalresource.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Christian Mangold
  • Date: 2009-07-10 06:34:50 UTC
  • mfrom: (1.1.40 upstream)
  • Revision ID: james.westby@ubuntu.com-20090710063450-neojgew2fh0n3y0u
Tags: 4:4.2.96-0ubuntu1
* New upstream release
* Bump kde build-deps to 4.2.96

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
    Copyright (c) 2006 Till Adam <adam@kde.org>
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 "icalresource.h"
21
 
#include "settingsadaptor.h"
22
 
#include "singlefileresourceconfigdialog.h"
23
 
#include "kcal/kcalmimetypevisitor.h"  // the kcal at the akonadi top-level
24
 
 
25
 
#include <kcal/assignmentvisitor.h>
26
 
#include <kcal/calendarlocal.h>
27
 
#include <kcal/incidence.h>
28
 
 
29
 
#include <kdebug.h>
30
 
#include <kfiledialog.h>
31
 
#include <klocale.h>
32
 
#include <kurl.h>
33
 
 
34
 
#include <boost/shared_ptr.hpp>
35
 
 
36
 
using namespace Akonadi;
37
 
using namespace KCal;
38
 
 
39
 
typedef boost::shared_ptr<KCal::Incidence> IncidencePtr;
40
 
 
41
 
ICalResource::ICalResource( const QString &id )
42
 
    : SingleFileResource<Settings>( id ), mCalendar( 0 ),
43
 
      mMimeVisitor( new KCalMimeTypeVisitor() ),
44
 
      mIncidenceAssigner( new AssignmentVisitor() ),
45
 
      mNotesMimeType( QLatin1String( "application/x-vnd.kde.notes" ) )
46
 
{
47
 
  QStringList mimeTypes;
48
 
  if ( isNotesResource() ) {
49
 
    mimeTypes << mNotesMimeType;
50
 
    setSupportedMimetypes( mimeTypes, "knotes" );
51
 
  }
52
 
  else {
53
 
    mimeTypes << QLatin1String( "text/calendar" );
54
 
    mimeTypes += mMimeVisitor->allMimeTypes();
55
 
    setSupportedMimetypes( mimeTypes, "office-calendar" );
56
 
  }
57
 
 
58
 
 
59
 
  new SettingsAdaptor( Settings::self() );
60
 
  QDBusConnection::sessionBus().registerObject( QLatin1String( "/Settings" ),
61
 
                            Settings::self(), QDBusConnection::ExportAdaptors );
62
 
}
63
 
 
64
 
ICalResource::~ICalResource()
65
 
{
66
 
  delete mCalendar;
67
 
  delete mMimeVisitor;
68
 
  delete mIncidenceAssigner;
69
 
}
70
 
 
71
 
bool ICalResource::retrieveItem( const Akonadi::Item &item, const QSet<QByteArray> &parts )
72
 
{
73
 
  Q_UNUSED( parts );
74
 
  kDebug( 5251 ) << "Item:" << item.url();
75
 
 
76
 
  if ( !mCalendar ) {
77
 
    emit error( i18n("Calendar not loaded.") );
78
 
    return false;
79
 
  }
80
 
 
81
 
  const QString rid = item.remoteId();
82
 
  Incidence* incidence = mCalendar->incidence( rid );
83
 
  if ( !incidence ) {
84
 
    emit error( i18n("Incidence with uid '%1' not found.", rid ) );
85
 
    return false;
86
 
  }
87
 
 
88
 
  IncidencePtr incidencePtr( incidence->clone() );
89
 
 
90
 
  Item i = item;
91
 
  QString mimeType;
92
 
  if ( isNotesResource() )
93
 
    mimeType = mNotesMimeType;
94
 
  else
95
 
    mimeType = mMimeVisitor->mimeType( incidencePtr.get() );
96
 
 
97
 
  i.setMimeType( mimeType );
98
 
  i.setPayload<IncidencePtr>( incidencePtr );
99
 
  itemRetrieved( i );
100
 
  return true;
101
 
}
102
 
 
103
 
void ICalResource::aboutToQuit()
104
 
{
105
 
  writeFile();
106
 
  Settings::self()->writeConfig();
107
 
}
108
 
 
109
 
void ICalResource::configure( WId windowId )
110
 
{
111
 
  if ( isNotesResource() )
112
 
    Settings::self()->setPath( KGlobal::dirs()->saveLocation( "data", "knotes/" ) + "notes.ics" );
113
 
 
114
 
  SingleFileResourceConfigDialog<Settings> dlg( windowId );
115
 
  dlg.setFilter( "*.ics *.ical|" + i18nc("Filedialog filter for *.ics *.ical", "iCal Calendar File" ) );
116
 
  dlg.setCaption( i18n("Select Calendar") );
117
 
  if ( dlg.exec() == QDialog::Accepted ) {
118
 
    reloadFile();
119
 
  }
120
 
}
121
 
 
122
 
bool ICalResource::readFromFile( const QString &fileName )
123
 
{
124
 
  delete mCalendar;
125
 
  mCalendar = 0;
126
 
 
127
 
  mCalendar = new KCal::CalendarLocal( QLatin1String( "UTC" ) );
128
 
  mCalendar->load( fileName );
129
 
  return true;
130
 
}
131
 
 
132
 
void ICalResource::itemAdded( const Akonadi::Item & item, const Akonadi::Collection& )
133
 
{
134
 
  if ( !mCalendar ) {
135
 
    cancelTask( i18n("Calendar not loaded.") );
136
 
    return;
137
 
  }
138
 
 
139
 
  if ( !item.hasPayload<IncidencePtr>() ) {
140
 
    cancelTask( i18n("Unable to retrieve added item %1.").arg( item.id() ) );
141
 
    return;
142
 
  }
143
 
 
144
 
  IncidencePtr i = item.payload<IncidencePtr>();
145
 
  mCalendar->addIncidence( i.get()->clone() );
146
 
  Item it( item );
147
 
  it.setRemoteId( i->uid() );
148
 
  fileDirty();
149
 
  changeCommitted( it );
150
 
}
151
 
 
152
 
void ICalResource::itemChanged( const Akonadi::Item &item, const QSet<QByteArray> &parts )
153
 
{
154
 
  Q_UNUSED( parts )
155
 
 
156
 
  if ( !mCalendar ) {
157
 
    cancelTask( i18n("Calendar not loaded.") );
158
 
    return;
159
 
  }
160
 
 
161
 
  if ( !item.hasPayload<IncidencePtr>() ) {
162
 
    cancelTask( i18n("Unable to retrieve modified item %1.").arg( item.id() ) );
163
 
    return;
164
 
  }
165
 
 
166
 
  IncidencePtr payload = item.payload<IncidencePtr>();
167
 
  Incidence *incidence = mCalendar->incidence( item.remoteId() );
168
 
  if ( !incidence ) {
169
 
    // not in the calendar yet, should not happen -> add it
170
 
    mCalendar->addIncidence( payload.get()->clone() );
171
 
  } else {
172
 
    // make sure any observer the resource might have installed gets properly notified
173
 
    incidence->startUpdates();
174
 
    bool assignResult = mIncidenceAssigner->assign( incidence, payload.get() );
175
 
    if ( assignResult )
176
 
      incidence->updated();
177
 
    incidence->endUpdates();
178
 
 
179
 
    if ( !assignResult ) {
180
 
      kWarning() << "Item changed incidence type. Replacing it.";
181
 
 
182
 
      mCalendar->deleteIncidence( incidence );
183
 
      delete incidence;
184
 
      mCalendar->addIncidence( payload.get()->clone() );
185
 
    }
186
 
  }
187
 
  fileDirty();
188
 
  changeCommitted( item );
189
 
}
190
 
 
191
 
void ICalResource::itemRemoved(const Akonadi::Item & item)
192
 
{
193
 
  if ( !mCalendar ) {
194
 
    cancelTask( i18n("Calendar not loaded.") );
195
 
    return;
196
 
  }
197
 
 
198
 
  Incidence *i = mCalendar->incidence( item.remoteId() );
199
 
  if ( i )
200
 
    mCalendar->deleteIncidence( i );
201
 
  fileDirty();
202
 
  changeProcessed();
203
 
}
204
 
 
205
 
void ICalResource::retrieveItems( const Akonadi::Collection & col )
206
 
{
207
 
  Q_UNUSED( col );
208
 
  if ( !mCalendar )
209
 
    return;
210
 
 
211
 
  Incidence::List incidences = mCalendar->incidences();
212
 
  Item::List items;
213
 
  foreach ( Incidence *incidence, incidences ) {
214
 
    QString mimeType;
215
 
    if ( isNotesResource() )
216
 
      mimeType = mNotesMimeType;
217
 
    else
218
 
      mimeType = mMimeVisitor->mimeType( incidence );
219
 
    Item item ( mimeType );
220
 
    item.setRemoteId( incidence->uid() );
221
 
    item.setPayload( IncidencePtr( incidence->clone() ) );
222
 
    items << item;
223
 
  }
224
 
  itemsRetrieved( items );
225
 
}
226
 
 
227
 
bool ICalResource::writeToFile( const QString &fileName )
228
 
{
229
 
  if ( !mCalendar->save( fileName ) ) {
230
 
    emit error( i18n("Failed to save calendar file to %1", fileName ) );
231
 
    return false;
232
 
  }
233
 
  return true;
234
 
}
235
 
 
236
 
bool ICalResource::isNotesResource() const
237
 
{
238
 
  return identifier().startsWith("akonadi_notes");
239
 
}
240
 
 
241
 
AKONADI_RESOURCE_MAIN( ICalResource )
242
 
 
243
 
 
244
 
#include "icalresource.moc"