• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.10.5 API Reference
  • KDE Home
  • Contact Us
 

akonadi

  • akonadi
  • calendar
invitationhandler.cpp
1 /*
2  Copyright (c) 2002-2004 Klarälvdalens Datakonsult AB
3  <info@klaralvdalens-datakonsult.se>
4 
5  Copyright (C) 2010 Bertjan Broeksema <broeksema@kde.org>
6  Copyright (C) 2010 Klaralvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>
7 
8  Copyright (C) 2012 SĂ©rgio Martins <iamsergio@gmail.com>
9 
10  This library is free software; you can redistribute it and/or modify it
11  under the terms of the GNU Library General Public License as published by
12  the Free Software Foundation; either version 2 of the License, or (at your
13  option) any later version.
14 
15  This library is distributed in the hope that it will be useful, but WITHOUT
16  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
18  License for more details.
19 
20  You should have received a copy of the GNU Library General Public License
21  along with this library; see the file COPYING.LIB. If not, write to the
22  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23  02110-1301, USA.
24 */
25 
26 #include "invitationhandler.h"
27 #include "invitationhandler_p.h"
28 #include "invitationhandlerhelper_p.h"
29 #include "calendarsettings.h"
30 #include "publishdialog.h"
31 #include "utils_p.h"
32 #include "mailclient_p.h"
33 
34 #include <kcalcore/icalformat.h>
35 #include <kcalcore/incidence.h>
36 #include <kcalcore/schedulemessage.h>
37 #include <kcalcore/attendee.h>
38 #include <kcalutils/stringify.h>
39 
40 
41 #include <kpimidentities/identitymanager.h>
42 #include <mailtransport/transportmanager.h>
43 
44 #include <KMessageBox>
45 #include <KLocale>
46 
47 using namespace Akonadi;
48 
49 GroupwareUiDelegate::~GroupwareUiDelegate()
50 {
51 }
52 
53 
54 InvitationHandler::InvitationHandler( QObject *parent )
55  : QObject( parent )
56  , d( new Private( this ) )
57 {
58 }
59 
60 InvitationHandler::~InvitationHandler()
61 {
62  delete d;
63 }
64 
65 void InvitationHandler::processiTIPMessage( const QString &receiver,
66  const QString &iCal,
67  const QString &action )
68 {
69  Q_ASSERT( d->m_currentOperation == OperationNone );
70  if ( d->m_currentOperation != OperationNone ) {
71  kError() << "There can't be an operation in progress!" << d->m_currentOperation;
72  return;
73  }
74 
75  d->m_currentOperation = OperationProcessiTIPMessage;
76 
77  if ( !d->m_calendar->isLoaded() ) {
78  d->m_queuedInvitation.receiver = receiver;
79  d->m_queuedInvitation.iCal = iCal;
80  d->m_queuedInvitation.action = action;
81  return;
82  }
83 
84  if ( d->m_calendarLoadError ) {
85  emit iTipMessageProcessed( ResultError, i18n( "Error loading calendar." ) );
86  return;
87  }
88 
89  KCalCore::ICalFormat format;
90  KCalCore::ScheduleMessage::Ptr message = format.parseScheduleMessage( d->m_calendar, iCal );
91 
92  if ( !message ) {
93  const QString errorMessage = format.exception() ? i18n( "Error message: %1", KCalUtils::Stringify::errorMessage( *format.exception() ) )
94  : i18n( "Unknown error while parsing iCal invitation" );
95 
96  kError() << "Error parsing" << errorMessage;
97  KMessageBox::detailedError( 0,// mParent, TODO
98  i18n( "Error while processing an invitation or update." ),
99  errorMessage );
100  emit iTipMessageProcessed( ResultError, errorMessage );
101  return;
102  }
103 
104  d->m_method = static_cast<KCalCore::iTIPMethod>( message->method() );
105  d->m_incidence.clear();
106 
107  KCalCore::ScheduleMessage::Status status = message->status();
108  KCalCore::Incidence::Ptr incidence = message->event().dynamicCast<KCalCore::Incidence>();
109  if ( !incidence ) {
110  emit iTipMessageProcessed( ResultError, QLatin1String( "Invalid incidence" ) );
111  return;
112  }
113 
114  if ( action.startsWith( QLatin1String( "accepted" ) ) ||
115  action.startsWith( QLatin1String( "tentative" ) ) ||
116  action.startsWith( QLatin1String( "delegated" ) ) ||
117  action.startsWith( QLatin1String( "counter" ) ) ) {
118  // Find myself and set my status. This can't be done in the scheduler,
119  // since this does not know the choice I made in the KMail bpf
120  const KCalCore::Attendee::List attendees = incidence->attendees();
121  foreach ( KCalCore::Attendee::Ptr attendee, attendees ) {
122  if ( attendee->email() == receiver ) {
123  if ( action.startsWith( QLatin1String( "accepted" ) ) ) {
124  attendee->setStatus( KCalCore::Attendee::Accepted );
125  } else if ( action.startsWith( QLatin1String( "tentative" ) ) ) {
126  attendee->setStatus( KCalCore::Attendee::Tentative );
127  } else if ( CalendarSettings::self()->outlookCompatCounterProposals() &&
128  action.startsWith( QLatin1String( "counter" ) ) ) {
129  attendee->setStatus( KCalCore::Attendee::Tentative );
130  } else if ( action.startsWith( QLatin1String( "delegated" ) ) ) {
131  attendee->setStatus( KCalCore::Attendee::Delegated );
132  }
133  break;
134  }
135  }
136  if ( CalendarSettings::self()->outlookCompatCounterProposals() ||
137  !action.startsWith( QLatin1String( "counter" ) ) ) {
138  d->m_scheduler->acceptTransaction( incidence, d->m_calendar, d->m_method, status, receiver );
139  return; // signal emitted in onSchedulerFinished().
140  }
141  //TODO: what happens here? we must emit a signal
142  } else if ( action.startsWith( QLatin1String( "cancel" ) ) ) {
143  // Delete the old incidence, if one is present
144  d->m_scheduler->acceptTransaction( incidence, d->m_calendar, KCalCore::iTIPCancel, status, receiver );
145  return; // signal emitted in onSchedulerFinished().
146  } else if ( action.startsWith( QLatin1String( "reply" ) ) ) {
147  if ( d->m_method != KCalCore::iTIPCounter ) {
148  d->m_scheduler->acceptTransaction( incidence, d->m_calendar, d->m_method, status, QString() );
149  } else {
150  d->m_incidence = incidence; // so we can access it in the slot
151  d->m_scheduler->acceptCounterProposal( incidence, d->m_calendar );
152  }
153  return; // signal emitted in onSchedulerFinished().
154  } else {
155  kError() << "Unknown incoming action" << action;
156  emit iTipMessageProcessed( ResultError, i18n( "Invalid action: %1", action ) );
157  }
158 
159  if ( d->m_uiDelegate && action.startsWith( QLatin1String( "counter" ) ) ) {
160  Akonadi::Item item;
161  item.setMimeType( incidence->mimeType() );
162  item.setPayload( KCalCore::Incidence::Ptr( incidence->clone() ) );
163  d->m_uiDelegate->requestIncidenceEditor( item );
164  }
165 }
166 
167 void InvitationHandler::sendiTIPMessage( KCalCore::iTIPMethod method,
168  const KCalCore::Incidence::Ptr &incidence,
169  QWidget *parentWidget )
170 {
171  if ( !incidence ) {
172  Q_ASSERT( false );
173  kError() << "Invalid incidence";
174  return;
175  }
176 
177  d->m_queuedInvitation.method = method;
178  d->m_queuedInvitation.incidence = incidence;
179  d->m_parentWidget = parentWidget;
180 
181  if ( !d->m_calendar->isLoaded() ) {
182  // This method will be called again once the calendar is loaded.
183  return;
184  }
185 
186  Q_ASSERT( d->m_currentOperation == OperationNone );
187  if ( d->m_currentOperation != OperationNone ) {
188  kError() << "There can't be an operation in progress!" << d->m_currentOperation;
189  return;
190  }
191 
192  d->m_currentOperation = OperationSendiTIPMessage;
193 
194  if ( incidence->attendeeCount() == 0 && method != KCalCore::iTIPPublish ) {
195  KMessageBox::information( parentWidget,
196  i18n( "The item '%1' has no attendees. "
197  "Therefore no groupware message will be sent.",
198  incidence->summary() ),
199  i18n( "Message Not Sent" ),
200  QLatin1String( "ScheduleNoAttendees" ) );
201  return;
202  }
203 
204  KCalCore::Incidence *incidenceCopy = incidence->clone();
205  incidenceCopy->registerObserver( 0 );
206  incidenceCopy->clearAttendees();
207 
208  d->m_scheduler->performTransaction( incidence, method );
209 }
210 
211 void InvitationHandler::publishInformation( const KCalCore::Incidence::Ptr &incidence,
212  QWidget *parentWidget )
213 {
214  Q_ASSERT( incidence );
215  if ( !incidence ) {
216  kError() << "Invalid incidence. Aborting.";
217  return;
218  }
219 
220  Q_ASSERT( d->m_currentOperation == OperationNone );
221  if ( d->m_currentOperation != OperationNone ) {
222  kError() << "There can't be an operation in progress!" << d->m_currentOperation;
223  return;
224  }
225 
226  d->m_queuedInvitation.incidence = incidence;
227  d->m_parentWidget = parentWidget;
228 
229  d->m_currentOperation = OperationPublishInformation;
230 
231  QPointer<Akonadi::PublishDialog> publishdlg = new Akonadi::PublishDialog();
232  if ( incidence->attendeeCount() > 0 ) {
233  KCalCore::Attendee::List attendees = incidence->attendees();
234  KCalCore::Attendee::List::ConstIterator it;
235  KCalCore::Attendee::List::ConstIterator end( attendees.constEnd() );
236  for ( it = attendees.constBegin(); it != end; ++it ) {
237  publishdlg->addAttendee( *it );
238  }
239  }
240  if ( publishdlg->exec() == QDialog::Accepted && publishdlg )
241  d->m_scheduler->publish( incidence, publishdlg->addresses() );
242  else
243  emit informationPublished( ResultSuccess, QString() ); // Canceled.
244  delete publishdlg;
245 }
246 
247 void InvitationHandler::sendAsICalendar( const KCalCore::Incidence::Ptr &incidence,
248  QWidget *parentWidget )
249 {
250  Q_ASSERT( incidence );
251  if ( !incidence ) {
252  kError() << "Invalid incidence";
253  return;
254  }
255 
256  KPIMIdentities::IdentityManager identityManager;
257 
258  QPointer<Akonadi::PublishDialog> publishdlg = new Akonadi::PublishDialog;
259  if ( publishdlg->exec() == QDialog::Accepted && publishdlg ) {
260  const QString recipients = publishdlg->addresses();
261  if ( incidence->organizer()->isEmpty() ) {
262  incidence->setOrganizer( KCalCore::Person::Ptr(
263  new KCalCore::Person( Akonadi::CalendarUtils::fullName(),
264  Akonadi::CalendarUtils::email() ) ) );
265  }
266 
267  KCalCore::ICalFormat format;
268  const QString from = Akonadi::CalendarUtils::email();
269  const bool bccMe = Akonadi::CalendarSettings::self()->bcc();
270  const QString messageText = format.createScheduleMessage( incidence, KCalCore::iTIPRequest );
271  MailClient *mailer = new MailClient();
272  d->m_queuedInvitation.incidence = incidence;
273  connect( mailer, SIGNAL(finished(Akonadi::MailClient::Result,QString)),
274  d, SLOT(finishSendAsICalendar(Akonadi::MailScheduler::Result,QString)) );
275 
276  mailer->mailTo( incidence, identityManager.identityForAddress( from ), from, bccMe,
277  recipients, messageText,
278  MailTransport::TransportManager::self()->defaultTransportName() );
279  }
280 }
281 
282 void InvitationHandler::setGroupwareUiDelegate( GroupwareUiDelegate *delegate )
283 {
284  d->m_uiDelegate = delegate;
285 }
286 
287 #include "invitationhandler.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jul 13 2013 01:27:37 by doxygen 1.8.3.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi

Skip menu "akonadi"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Modules
  • Related Pages

kdepimlibs-4.10.5 API Reference

Skip menu "kdepimlibs-4.10.5 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal