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

akonadi

  • akonadi
  • calendar
calendarclipboard.cpp
1 /*
2  Copyright (C) 2012 Sérgio Martins <iamsergio@gmail.com>
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 "calendarclipboard_p.h"
21 #include <kcalutils/dndfactory.h>
22 #include <kcalutils/icaldrag.h>
23 
24 #include <KLocalizedString>
25 #include <KMessageBox>
26 
27 #include <QApplication>
28 #include <QClipboard>
29 
30 using namespace Akonadi;
31 
32 CalendarClipboard::Private::Private(const Akonadi::CalendarBase::Ptr &calendar,
33  Akonadi::IncidenceChanger *changer,
34  CalendarClipboard *qq) : QObject(qq)
35  , m_calendar(calendar)
36  , m_changer(changer)
37  , m_abortCurrentOperation(false)
38  , q(qq)
39 {
40  Q_ASSERT(m_calendar);
41  if (!m_changer) {
42  m_changer = new Akonadi::IncidenceChanger(this);
43  m_changer->setHistoryEnabled(false);
44  m_changer->setGroupwareCommunication(false);
45  }
46 
47  m_dndfactory = new KCalUtils::DndFactory(m_calendar);
48 
49  connect(m_changer,
50  SIGNAL(modifyFinished(int,Akonadi::Item,Akonadi::IncidenceChanger::ResultCode,QString)),
51  SLOT(slotModifyFinished(int,Akonadi::Item,Akonadi::IncidenceChanger::ResultCode,QString)));
52 
53  connect(m_changer,
54  SIGNAL(deleteFinished(int,QVector<Akonadi::Item::Id>,Akonadi::IncidenceChanger::ResultCode,QString)),
55  SLOT(slotDeleteFinished(int,QVector<Akonadi::Item::Id>,Akonadi::IncidenceChanger::ResultCode,QString)));
56 }
57 
58 CalendarClipboard::Private::~Private()
59 {
60  delete m_dndfactory;
61 }
62 
63 void CalendarClipboard::Private::getIncidenceHierarchy(const KCalCore::Incidence::Ptr &incidence,
64  QStringList &uids)
65 {
66  // protecion against looping hierarchies
67  if (incidence && !uids.contains(incidence->uid())) {
68  KCalCore::Incidence::List immediateChildren = m_calendar->childIncidences(incidence->uid());
69 
70  foreach(const KCalCore::Incidence::Ptr &child, immediateChildren) {
71  getIncidenceHierarchy(child, uids);
72  }
73  uids.append(incidence->uid());
74  }
75 }
76 
77 void CalendarClipboard::Private::cut(const KCalCore::Incidence::List &incidences)
78 {
79  const bool result = m_dndfactory->copyIncidences(incidences);
80  m_pendingChangeIds.clear();
81  // Note: Don't use DndFactory::cutIncidences(), it doesn't use IncidenceChanger for deletion
82  // we would loose async error handling and redo/undo features
83  if (result) {
84  Akonadi::Item::List items = m_calendar->itemList(incidences);
85  const int result = m_changer->deleteIncidences(items);
86  if (result == -1) {
87  emit q->cutFinished(false, i18n("Error performing deletion."));
88  } else {
89  m_pendingChangeIds << result;
90  }
91  } else {
92  emit q->cutFinished(false, i18n("Error performing copy."));
93  }
94 }
95 
96 void CalendarClipboard::Private::cut(const KCalCore::Incidence::Ptr &incidence)
97 {
98  KCalCore::Incidence::List incidences;
99  incidences << incidence;
100  cut(incidences);
101 }
102 
103 void CalendarClipboard::Private::makeChildsIndependent(const KCalCore::Incidence::Ptr &incidence)
104 {
105  Q_ASSERT(incidence);
106  const KCalCore::Incidence::List childs = m_calendar->childIncidences(incidence->uid());
107 
108  if (childs.isEmpty()) {
109  cut(incidence);
110  } else {
111  m_pendingChangeIds.clear();
112  m_abortCurrentOperation = false;
113  foreach(const KCalCore::Incidence::Ptr &child, childs) {
114  Akonadi::Item childItem = m_calendar->item(incidence);
115  if (!childItem.isValid()) {
116  emit q->cutFinished( false, i18n("Can't find item: %1", childItem.id()));
117  return;
118  }
119 
120  KCalCore::Incidence::Ptr newIncidence(child->clone());
121  newIncidence->setRelatedTo(QString());
122  childItem.setPayload<KCalCore::Incidence::Ptr>(newIncidence);
123  const int changeId = m_changer->modifyIncidence(childItem, /*originalPayload*/child);
124  if (changeId == -1) {
125  m_abortCurrentOperation = true;
126  break;
127  } else {
128  m_pendingChangeIds << changeId;
129  }
130  }
131  if (m_pendingChangeIds.isEmpty() && m_abortCurrentOperation) {
132  emit q->cutFinished(false, i18n("Error while removing relations."));
133  } // if m_pendingChangeIds isn't empty, we wait for all jobs to finish first.
134  }
135 }
136 
137 void CalendarClipboard::Private::slotModifyFinished(int changeId, const Akonadi::Item &item,
138  IncidenceChanger::ResultCode resultCode,
139  const QString &errorMessage)
140 {
141  if (!m_pendingChangeIds.contains(changeId))
142  return; // Not ours, someone else deleted something, not our business.
143 
144  m_pendingChangeIds.remove(changeId);
145  const bool isLastChange = m_pendingChangeIds.isEmpty();
146 
147  Q_UNUSED(item);
148  Q_UNUSED(errorMessage);
149  if (m_abortCurrentOperation && isLastChange) {
150  emit q->cutFinished(false, i18n("Error while removing relations."));
151  } else if (!m_abortCurrentOperation) {
152  if (resultCode == IncidenceChanger::ResultCodeSuccess) {
153  if (isLastChange) {
154  // All children are unparented, lets cut.
155  Q_ASSERT(item.isValid() && item.hasPayload());
156  cut(item.payload<KCalCore::Incidence::Ptr>());
157  }
158  } else {
159  m_abortCurrentOperation = true;
160  }
161  }
162 }
163 
164 void CalendarClipboard::Private::slotDeleteFinished(int changeId, const QVector<Akonadi::Item::Id> &ids,
165  Akonadi::IncidenceChanger::ResultCode result,
166  const QString &errorMessage)
167 {
168  if (!m_pendingChangeIds.contains(changeId))
169  return; // Not ours, someone else deleted something, not our business.
170 
171  m_pendingChangeIds.remove(changeId);
172 
173  Q_UNUSED(ids);
174  if (result == IncidenceChanger::ResultCodeSuccess) {
175  emit q->cutFinished(true, QString());
176  } else {
177  emit q->cutFinished(false, i18n("Error while deleting incidences: %1",
178  errorMessage));
179  }
180 }
181 
182 CalendarClipboard::CalendarClipboard(const Akonadi::CalendarBase::Ptr &calendar,
183  Akonadi::IncidenceChanger *changer,
184  QObject *parent) : QObject(parent)
185  , d(new Private(calendar, changer, this))
186 {
187 
188 
189 }
190 
191 CalendarClipboard::~CalendarClipboard()
192 {
193 }
194 
195 void CalendarClipboard::cutIncidence(const KCalCore::Incidence::Ptr &incidence,
196  CalendarClipboard::Mode mode)
197 {
198  const bool hasChildren = !d->m_calendar->childIncidences(incidence->uid()).isEmpty();
199  if (mode == AskMode && hasChildren) {
200  const int km = KMessageBox::questionYesNoCancel(0,
201  i18n("The item \"%1\" has sub-to-dos. "
202  "Do you want to cut just this item and "
203  "make all its sub-to-dos independent, or "
204  "cut the to-do with all its sub-to-dos?",
205  incidence->summary()),
206  i18n("KOrganizer Confirmation"),
207  KGuiItem(i18n("Cut Only This")),
208  KGuiItem(i18n("Cut All")));
209 
210  if (km == KMessageBox::Cancel) {
211  emit cutFinished(/*success=*/true, QString());
212  return;
213  }
214  mode = km == KMessageBox::Yes ? SingleMode : RecursiveMode;
215  } else if (mode == AskMode) {
216  mode = SingleMode; // Doesn't have children, don't ask
217  }
218 
219  if (mode == SingleMode) {
220  d->makeChildsIndependent(incidence); // Will call d->cut(incidence) when it finishes.
221  } else {
222  QStringList uids;
223  d->getIncidenceHierarchy(incidence, uids);
224  Q_ASSERT(!uids.isEmpty());
225  KCalCore::Incidence::List incidencesToCut;
226  foreach(const QString &uid, uids) {
227  KCalCore::Incidence::Ptr child = d->m_calendar->incidence(uid);
228  if (child)
229  incidencesToCut << child;
230  }
231  d->cut(incidencesToCut);
232  }
233 }
234 
235 bool CalendarClipboard::copyIncidence(const KCalCore::Incidence::Ptr &incidence,
236  CalendarClipboard::Mode mode)
237 {
238  const bool hasChildren = !d->m_calendar->childIncidences(incidence->uid()).isEmpty();
239  if (mode == AskMode && hasChildren) {
240  const int km = KMessageBox::questionYesNoCancel(0,
241  i18n("The item \"%1\" has sub-to-dos. "
242  "Do you want to copy just this item or "
243  "copy the to-do with all its sub-to-dos?",
244  incidence->summary()),
245  i18n("KOrganizer Confirmation"),
246  KGuiItem(i18n("Copy Only This")),
247  KGuiItem(i18n("Copy All")));
248  if (km == KMessageBox::Cancel) {
249  return true;
250  }
251  mode = km == KMessageBox::Yes ? SingleMode : RecursiveMode;
252  } else if (mode == AskMode) {
253  mode = SingleMode; // Doesn't have children, don't ask
254  }
255 
256  KCalCore::Incidence::List incidencesToCopy;
257  if (mode == SingleMode) {
258  incidencesToCopy << incidence;
259  } else {
260  QStringList uids;
261  d->getIncidenceHierarchy(incidence, uids);
262  Q_ASSERT(!uids.isEmpty());
263  foreach(const QString &uid, uids) {
264  KCalCore::Incidence::Ptr child = d->m_calendar->incidence(uid);
265  if (child)
266  incidencesToCopy << child;
267  }
268  }
269 
270  return d->m_dndfactory->copyIncidences(incidencesToCopy);
271 }
272 
273 bool CalendarClipboard::pasteAvailable() const
274 {
275  return KCalUtils::ICalDrag::canDecode(QApplication::clipboard()->mimeData());
276 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Fri Jan 17 2014 22:12:29 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.11.5 API Reference

Skip menu "kdepimlibs-4.11.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