~ubuntu-branches/ubuntu/oneiric/kdepim/oneiric-updates

« back to all changes in this revision

Viewing changes to kalarm/akonadi/kdepim-runtime/ical/icalresourcebase.cpp

  • Committer: Package Import Robot
  • Author(s): Philip Muškovac
  • Date: 2011-06-28 19:33:24 UTC
  • mfrom: (0.2.13) (0.1.13 sid)
  • Revision ID: package-import@ubuntu.com-20110628193324-8yvjs8sdv9rdoo6c
Tags: 4:4.7.0-0ubuntu1
* New upstream release
  - update install files
  - add missing kdepim-doc package to control file
  - Fix Vcs lines
  - kontact breaks/replaces korganizer << 4:4.6.80
  - tighten the dependency of kdepim-dev on libkdepim4 to fix lintian error

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright (c) 2006 Till Adam <adam@kde.org>
 
3
    Copyright (c) 2009 David Jarvie <djarvie@kde.org>
 
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 "icalresourcebase.h"
 
22
#include "icalsettingsadaptor.h"
 
23
#include "singlefileresourceconfigdialog.h"
 
24
 
 
25
#include <akonadi/dbusconnectionpool.h>
 
26
 
 
27
#include <kcalcore/filestorage.h>
 
28
#include <kcalcore/memorycalendar.h>
 
29
#include <kcalcore/incidence.h>
 
30
#include <kcalcore/icalformat.h>
 
31
 
 
32
#include <kglobal.h>
 
33
#include <klocale.h>
 
34
#include <kdebug.h>
 
35
 
 
36
using namespace Akonadi;
 
37
using namespace KCalCore;
 
38
using namespace SETTINGS_NAMESPACE;
 
39
 
 
40
ICalResourceBase::ICalResourceBase( const QString &id )
 
41
  : SingleFileResource<Settings>( id )
 
42
{
 
43
  KGlobal::locale()->insertCatalog( "akonadi_ical_resource" );
 
44
}
 
45
 
 
46
void ICalResourceBase::initialise( const QStringList &mimeTypes, const QString &icon )
 
47
{
 
48
  setSupportedMimetypes( mimeTypes, icon );
 
49
  new ICalSettingsAdaptor( mSettings );
 
50
  DBusConnectionPool::threadConnection().registerObject( QLatin1String( "/Settings" ),
 
51
                                                         mSettings, QDBusConnection::ExportAdaptors );
 
52
}
 
53
 
 
54
ICalResourceBase::~ICalResourceBase()
 
55
{
 
56
}
 
57
 
 
58
bool ICalResourceBase::retrieveItem( const Akonadi::Item &item,
 
59
                                     const QSet<QByteArray> &parts )
 
60
{
 
61
  kDebug( 5251 ) << "Item:" << item.url();
 
62
 
 
63
  if ( !mCalendar ) {
 
64
    emit error( i18n("Calendar not loaded.") );
 
65
    return false;
 
66
  }
 
67
 
 
68
  return doRetrieveItem( item, parts );
 
69
}
 
70
 
 
71
void ICalResourceBase::aboutToQuit()
 
72
{
 
73
  if ( !mSettings->readOnly() ) {
 
74
    writeFile();
 
75
  }
 
76
  mSettings->writeConfig();
 
77
}
 
78
 
 
79
void ICalResourceBase::customizeConfigDialog( SingleFileResourceConfigDialog<Settings> *dlg )
 
80
{
 
81
#ifndef KDEPIM_MOBILE_UI
 
82
  dlg->setFilter( "text/calendar" );
 
83
#else
 
84
  dlg->setFilter( "*.ics *.vcs" );
 
85
#endif
 
86
  dlg->setCaption( i18n("Select Calendar") );
 
87
}
 
88
 
 
89
bool ICalResourceBase::readFromFile( const QString &fileName )
 
90
{
 
91
  mCalendar = KCalCore::MemoryCalendar::Ptr( new KCalCore::MemoryCalendar( QLatin1String( "UTC" ) ) );
 
92
  mFileStorage = KCalCore::FileStorage::Ptr( new KCalCore::FileStorage( mCalendar, fileName,
 
93
                                                                        new KCalCore::ICalFormat() ) );
 
94
  const bool result = mFileStorage->load();
 
95
  if ( !result ) {
 
96
    kError() << "Error loading file " << fileName;
 
97
  }
 
98
 
 
99
  return result;
 
100
}
 
101
 
 
102
void ICalResourceBase::itemRemoved( const Akonadi::Item &item )
 
103
{
 
104
  if ( !mCalendar ) {
 
105
    kError() << "mCalendar is 0!";
 
106
    cancelTask( i18n("Calendar not loaded.") );
 
107
    return;
 
108
  }
 
109
 
 
110
  Incidence::Ptr i = mCalendar->incidence( item.remoteId() );
 
111
  if ( i ) {
 
112
    if ( !mCalendar->deleteIncidence( i ) ) {
 
113
      kError() << "Can't delete incidence with uid " << item.remoteId()
 
114
               << "; item.id() = " << item.id();
 
115
      cancelTask();
 
116
      return;
 
117
    }
 
118
  } else {
 
119
    kError() << "Can't find incidence with uid " << item.remoteId()
 
120
             << "; item.id() = " << item.id();
 
121
  }
 
122
  scheduleWrite();
 
123
  changeProcessed();
 
124
}
 
125
 
 
126
void ICalResourceBase::retrieveItems( const Akonadi::Collection &col )
 
127
{
 
128
  reloadFile();
 
129
  if ( mCalendar ) {
 
130
    doRetrieveItems( col );
 
131
  } else {
 
132
    kError() << "mCalendar is 0!";
 
133
  }
 
134
}
 
135
 
 
136
bool ICalResourceBase::writeToFile( const QString &fileName )
 
137
{
 
138
  if ( !mCalendar ) {
 
139
    kError() << "mCalendar is 0!";
 
140
    return false;
 
141
  }
 
142
 
 
143
  KCalCore::FileStorage *fileStorage = mFileStorage.data();
 
144
  if ( fileName != mFileStorage->fileName() ) {
 
145
    fileStorage = new KCalCore::FileStorage( mCalendar,
 
146
                                             fileName,
 
147
                                             new KCalCore::ICalFormat() );
 
148
  }
 
149
 
 
150
  bool success = true;
 
151
  if ( !fileStorage->save() ) {
 
152
    kError() << "Failed to save calendar to file " + fileName;
 
153
    emit error( i18n("Failed to save calendar file to %1", fileName ) );
 
154
    success = false;
 
155
  }
 
156
 
 
157
  if ( fileStorage != mFileStorage.data() ) {
 
158
    delete fileStorage;
 
159
  }
 
160
 
 
161
  return success;
 
162
}
 
163
 
 
164
KCalCore::MemoryCalendar::Ptr ICalResourceBase::calendar() const
 
165
{
 
166
  return mCalendar;
 
167
}
 
168
 
 
169
KCalCore::FileStorage::Ptr ICalResourceBase::fileStorage() const
 
170
{
 
171
  return mFileStorage;
 
172
}
 
173
 
 
174
 
 
175
#include "icalresourcebase.moc"