2
Copyright (c) 2006 Till Adam <adam@kde.org>
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.
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.
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
20
#include "icalresource.h"
21
#include "settingsadaptor.h"
22
#include "singlefileresourceconfigdialog.h"
23
#include "kcal/kcalmimetypevisitor.h" // the kcal at the akonadi top-level
25
#include <kcal/assignmentvisitor.h>
26
#include <kcal/calendarlocal.h>
27
#include <kcal/incidence.h>
30
#include <kfiledialog.h>
34
#include <boost/shared_ptr.hpp>
36
using namespace Akonadi;
39
typedef boost::shared_ptr<KCal::Incidence> IncidencePtr;
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" ) )
47
QStringList mimeTypes;
48
if ( isNotesResource() ) {
49
mimeTypes << mNotesMimeType;
50
setSupportedMimetypes( mimeTypes, "knotes" );
53
mimeTypes << QLatin1String( "text/calendar" );
54
mimeTypes += mMimeVisitor->allMimeTypes();
55
setSupportedMimetypes( mimeTypes, "office-calendar" );
59
new SettingsAdaptor( Settings::self() );
60
QDBusConnection::sessionBus().registerObject( QLatin1String( "/Settings" ),
61
Settings::self(), QDBusConnection::ExportAdaptors );
64
ICalResource::~ICalResource()
68
delete mIncidenceAssigner;
71
bool ICalResource::retrieveItem( const Akonadi::Item &item, const QSet<QByteArray> &parts )
74
kDebug( 5251 ) << "Item:" << item.url();
77
emit error( i18n("Calendar not loaded.") );
81
const QString rid = item.remoteId();
82
Incidence* incidence = mCalendar->incidence( rid );
84
emit error( i18n("Incidence with uid '%1' not found.", rid ) );
88
IncidencePtr incidencePtr( incidence->clone() );
92
if ( isNotesResource() )
93
mimeType = mNotesMimeType;
95
mimeType = mMimeVisitor->mimeType( incidencePtr.get() );
97
i.setMimeType( mimeType );
98
i.setPayload<IncidencePtr>( incidencePtr );
103
void ICalResource::aboutToQuit()
106
Settings::self()->writeConfig();
109
void ICalResource::configure( WId windowId )
111
if ( isNotesResource() )
112
Settings::self()->setPath( KGlobal::dirs()->saveLocation( "data", "knotes/" ) + "notes.ics" );
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 ) {
122
bool ICalResource::readFromFile( const QString &fileName )
127
mCalendar = new KCal::CalendarLocal( QLatin1String( "UTC" ) );
128
mCalendar->load( fileName );
132
void ICalResource::itemAdded( const Akonadi::Item & item, const Akonadi::Collection& )
135
cancelTask( i18n("Calendar not loaded.") );
139
if ( !item.hasPayload<IncidencePtr>() ) {
140
cancelTask( i18n("Unable to retrieve added item %1.").arg( item.id() ) );
144
IncidencePtr i = item.payload<IncidencePtr>();
145
mCalendar->addIncidence( i.get()->clone() );
147
it.setRemoteId( i->uid() );
149
changeCommitted( it );
152
void ICalResource::itemChanged( const Akonadi::Item &item, const QSet<QByteArray> &parts )
157
cancelTask( i18n("Calendar not loaded.") );
161
if ( !item.hasPayload<IncidencePtr>() ) {
162
cancelTask( i18n("Unable to retrieve modified item %1.").arg( item.id() ) );
166
IncidencePtr payload = item.payload<IncidencePtr>();
167
Incidence *incidence = mCalendar->incidence( item.remoteId() );
169
// not in the calendar yet, should not happen -> add it
170
mCalendar->addIncidence( payload.get()->clone() );
172
// make sure any observer the resource might have installed gets properly notified
173
incidence->startUpdates();
174
bool assignResult = mIncidenceAssigner->assign( incidence, payload.get() );
176
incidence->updated();
177
incidence->endUpdates();
179
if ( !assignResult ) {
180
kWarning() << "Item changed incidence type. Replacing it.";
182
mCalendar->deleteIncidence( incidence );
184
mCalendar->addIncidence( payload.get()->clone() );
188
changeCommitted( item );
191
void ICalResource::itemRemoved(const Akonadi::Item & item)
194
cancelTask( i18n("Calendar not loaded.") );
198
Incidence *i = mCalendar->incidence( item.remoteId() );
200
mCalendar->deleteIncidence( i );
205
void ICalResource::retrieveItems( const Akonadi::Collection & col )
211
Incidence::List incidences = mCalendar->incidences();
213
foreach ( Incidence *incidence, incidences ) {
215
if ( isNotesResource() )
216
mimeType = mNotesMimeType;
218
mimeType = mMimeVisitor->mimeType( incidence );
219
Item item ( mimeType );
220
item.setRemoteId( incidence->uid() );
221
item.setPayload( IncidencePtr( incidence->clone() ) );
224
itemsRetrieved( items );
227
bool ICalResource::writeToFile( const QString &fileName )
229
if ( !mCalendar->save( fileName ) ) {
230
emit error( i18n("Failed to save calendar file to %1", fileName ) );
236
bool ICalResource::isNotesResource() const
238
return identifier().startsWith("akonadi_notes");
241
AKONADI_RESOURCE_MAIN( ICalResource )
244
#include "icalresource.moc"