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

KCalUtils Library

  • sources
  • kde-4.12
  • kdepimlibs
  • kcalutils
dndfactory.cpp
Go to the documentation of this file.
1 /*
2  This file is part of the kcalutils library.
3 
4  Copyright (c) 1998 Preston Brown <pbrown@kde.org>
5  Copyright (c) 2001,2002 Cornelius Schumacher <schumacher@kde.org>
6  Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
7  Copyright (c) 2005 Rafal Rzepecki <divide@users.sourceforge.net>
8  Copyright (c) 2008 Thomas Thrainer <tom_t@gmx.at>
9 
10  This library is free software; you can redistribute it and/or
11  modify it under the terms of the GNU Library General Public
12  License as published by the Free Software Foundation; either
13  version 2 of the License, or (at your option) any later version.
14 
15  This library is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  Library General Public 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
22  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23  Boston, MA 02110-1301, USA.
24 */
37 #include "dndfactory.h"
38 #include "icaldrag.h"
39 #include "vcaldrag.h"
40 
41 #include <KDebug>
42 #include <KIconLoader> // for BarIcon
43 #include <KUrl>
44 
45 #include <QtCore/QMimeData>
46 #include <QApplication>
47 #include <QClipboard>
48 #include <QDrag>
49 #include <QDate>
50 #include <QWidget>
51 #include <QDropEvent>
52 #include <QPixmap>
53 
54 using namespace KCalCore;
55 using namespace KCalUtils;
56 
61 //@cond PRIVATE
62 class KCalUtils::DndFactory::Private
63 {
64 public:
65  Private(const MemoryCalendar::Ptr &calendar)
66  : mCalendar(calendar)
67  {}
68 
69  Incidence::Ptr pasteIncidence(const Incidence::Ptr &incidence,
70  KDateTime newDateTime,
71  const QFlags<PasteFlag> &pasteOptions)
72  {
73  Incidence::Ptr inc(incidence);
74 
75  if (inc) {
76  inc = Incidence::Ptr(inc->clone());
77  inc->recreate();
78  }
79 
80  if (inc && newDateTime.isValid()) {
81  if (inc->type() == Incidence::TypeEvent) {
82  Event::Ptr event = inc.staticCast<Event>();
83  if (pasteOptions & FlagPasteAtOriginalTime) {
84  // Set date and preserve time and timezone stuff
85  const QDate date = newDateTime.date();
86  newDateTime = event->dtStart();
87  newDateTime.setDate(date);
88  }
89 
90  // in seconds
91  const int durationInSeconds = event->dtStart().secsTo(event->dtEnd());
92  const int durationInDays = event->dtStart().daysTo(event->dtEnd());
93 
94  event->setDtStart(newDateTime);
95 
96  if (newDateTime.isDateOnly()) {
97  event->setDtEnd(newDateTime.addDays(durationInDays));
98  } else {
99  event->setDtEnd(newDateTime.addSecs(durationInSeconds));
100  }
101 
102  } else if (inc->type() == Incidence::TypeTodo) {
103  Todo::Ptr aTodo = inc.staticCast<Todo>();
104  const bool pasteAtDtStart = (pasteOptions & FlagTodosPasteAtDtStart);
105  if (pasteOptions & FlagPasteAtOriginalTime) {
106  // Set date and preserve time and timezone stuff
107  const QDate date = newDateTime.date();
108  newDateTime = pasteAtDtStart ? aTodo->dtStart() : aTodo->dtDue();
109  newDateTime.setDate(date);
110  }
111  if (pasteAtDtStart) {
112  aTodo->setDtStart(newDateTime);
113  } else {
114  aTodo->setDtDue(newDateTime);
115  }
116 
117  } else if (inc->type() == Incidence::TypeJournal) {
118  if (pasteOptions & FlagPasteAtOriginalTime) {
119  // Set date and preserve time and timezone stuff
120  const QDate date = newDateTime.date();
121  newDateTime = inc->dtStart();
122  newDateTime.setDate(date);
123  }
124  inc->setDtStart(newDateTime);
125  } else {
126  kDebug() << "Trying to paste unknown incidence of type" << int(inc->type());
127  }
128  }
129 
130  return inc;
131  }
132 
133  MemoryCalendar::Ptr mCalendar;
134 };
135 //@endcond
136 
137 DndFactory::DndFactory(const MemoryCalendar::Ptr &calendar)
138  : d(new KCalUtils::DndFactory::Private(calendar))
139 {
140 }
141 
142 DndFactory::~DndFactory()
143 {
144  delete d;
145 }
146 
147 QMimeData *DndFactory::createMimeData()
148 {
149  QMimeData *mimeData = new QMimeData;
150 
151  ICalDrag::populateMimeData(mimeData, d->mCalendar);
152  VCalDrag::populateMimeData(mimeData, d->mCalendar);
153 
154  return mimeData;
155 }
156 
157 QDrag *DndFactory::createDrag(QWidget *owner)
158 {
159  QDrag *drag = new QDrag(owner);
160  drag->setMimeData(createMimeData());
161 
162  return drag;
163 }
164 
165 QMimeData *DndFactory::createMimeData(const Incidence::Ptr &incidence)
166 {
167  MemoryCalendar::Ptr cal(new MemoryCalendar(d->mCalendar->timeSpec()));
168  Incidence::Ptr i(incidence->clone());
169  //strip recurrence id's, We don't want to drag the exception but the occurrence.
170  i->setRecurrenceId(KDateTime());
171  cal->addIncidence(i);
172 
173  QMimeData *mimeData = new QMimeData;
174 
175  ICalDrag::populateMimeData(mimeData, cal);
176  VCalDrag::populateMimeData(mimeData, cal);
177 
178  KUrl uri = i->uri();
179  if (uri.isValid()) {
180  QMap<QString, QString> metadata;
181  metadata[QLatin1String("labels")] = QLatin1String(KUrl::toPercentEncoding(i->summary()));
182  uri.populateMimeData(mimeData, metadata);
183  }
184 
185  return mimeData;
186 }
187 
188 QDrag *DndFactory::createDrag(const Incidence::Ptr &incidence, QWidget *owner)
189 {
190  QDrag *drag = new QDrag(owner);
191  drag->setMimeData(createMimeData(incidence));
192  drag->setPixmap(BarIcon(incidence->iconName()));
193 
194  return drag;
195 }
196 
197 MemoryCalendar::Ptr DndFactory::createDropCalendar(const QMimeData *mimeData)
198 {
199  return createDropCalendar(mimeData, d->mCalendar->timeSpec());
200 }
201 
202 MemoryCalendar::Ptr DndFactory::createDropCalendar(const QMimeData *mimeData,
203  const KDateTime::Spec &timeSpec)
204 {
205  MemoryCalendar::Ptr calendar(new MemoryCalendar(timeSpec));
206 
207  if (ICalDrag::fromMimeData(mimeData, calendar) ||
208  VCalDrag::fromMimeData(mimeData, calendar)) {
209  return calendar;
210  }
211 
212  return MemoryCalendar::Ptr();
213 }
214 
215 MemoryCalendar::Ptr DndFactory::createDropCalendar(QDropEvent *dropEvent)
216 {
217  MemoryCalendar::Ptr calendar(createDropCalendar(dropEvent->mimeData()));
218  if (calendar) {
219  dropEvent->accept();
220  return calendar;
221  }
222  return MemoryCalendar::Ptr();
223 }
224 
225 Event::Ptr DndFactory::createDropEvent(const QMimeData *mimeData)
226 {
227  //kDebug();
228  Event::Ptr event;
229  MemoryCalendar::Ptr calendar(createDropCalendar(mimeData));
230 
231  if (calendar) {
232  Event::List events = calendar->events();
233  if (!events.isEmpty()) {
234  event = Event::Ptr(new Event(*events.first()));
235  }
236  }
237  return event;
238 }
239 
240 Event::Ptr DndFactory::createDropEvent(QDropEvent *dropEvent)
241 {
242  Event::Ptr event = createDropEvent(dropEvent->mimeData());
243 
244  if (event) {
245  dropEvent->accept();
246  }
247 
248  return event;
249 }
250 
251 Todo::Ptr DndFactory::createDropTodo(const QMimeData *mimeData)
252 {
253  //kDebug();
254  Todo::Ptr todo;
255  MemoryCalendar::Ptr calendar(createDropCalendar(mimeData));
256 
257  if (calendar) {
258  Todo::List todos = calendar->todos();
259  if (!todos.isEmpty()) {
260  todo = Todo::Ptr(new Todo(*todos.first()));
261  }
262  }
263 
264  return todo;
265 }
266 
267 Todo::Ptr DndFactory::createDropTodo(QDropEvent *dropEvent)
268 {
269  Todo::Ptr todo = createDropTodo(dropEvent->mimeData());
270 
271  if (todo) {
272  dropEvent->accept();
273  }
274 
275  return todo;
276 }
277 
278 void DndFactory::cutIncidence(const Incidence::Ptr &selectedIncidence)
279 {
280  Incidence::List list;
281  list.append(selectedIncidence);
282  cutIncidences(list);
283 }
284 
285 bool DndFactory::cutIncidences(const Incidence::List &incidences)
286 {
287  if (copyIncidences(incidences)) {
288  Incidence::List::ConstIterator it;
289  for (it = incidences.constBegin(); it != incidences.constEnd(); ++it) {
290  d->mCalendar->deleteIncidence(*it);
291  }
292  return true;
293  } else {
294  return false;
295  }
296 }
297 
298 bool DndFactory::copyIncidences(const Incidence::List &incidences)
299 {
300  QClipboard *clipboard = QApplication::clipboard();
301  Q_ASSERT(clipboard);
302  MemoryCalendar::Ptr calendar(new MemoryCalendar(d->mCalendar->timeSpec()));
303 
304  Incidence::List::ConstIterator it;
305  for (it = incidences.constBegin(); it != incidences.constEnd(); ++it) {
306  if (*it) {
307  calendar->addIncidence(Incidence::Ptr((*it)->clone()));
308  }
309  }
310 
311  QMimeData *mimeData = new QMimeData;
312 
313  ICalDrag::populateMimeData(mimeData, calendar);
314  VCalDrag::populateMimeData(mimeData, calendar);
315 
316  if (calendar->incidences().isEmpty()) {
317  return false;
318  } else {
319  clipboard->setMimeData(mimeData);
320  return true;
321  }
322 }
323 
324 bool DndFactory::copyIncidence(const Incidence::Ptr &selectedInc)
325 {
326  Incidence::List list;
327  list.append(selectedInc);
328  return copyIncidences(list);
329 }
330 
331 Incidence::List DndFactory::pasteIncidences(const KDateTime &newDateTime,
332  const QFlags<PasteFlag> &pasteOptions)
333 {
334  QClipboard *clipboard = QApplication::clipboard();
335  Q_ASSERT(clipboard);
336  MemoryCalendar::Ptr calendar(createDropCalendar(clipboard->mimeData()));
337  Incidence::List list;
338 
339  if (!calendar) {
340  kDebug() << "Can't parse clipboard";
341  return list;
342  }
343 
344  // All pasted incidences get new uids, must keep track of old uids,
345  // so we can update child's parents
346  QHash<QString, Incidence::Ptr> oldUidToNewInc;
347 
348  Incidence::List::ConstIterator it;
349  const Incidence::List incidences = calendar->incidences();
350  for (it = incidences.constBegin();
351  it != incidences.constEnd(); ++it) {
352  Incidence::Ptr incidence = d->pasteIncidence(*it, newDateTime, pasteOptions);
353  if (incidence) {
354  list.append(incidence);
355  oldUidToNewInc[(*it)->uid()] = *it;
356  }
357  }
358 
359  // update relations
360  for (it = list.constBegin(); it != list.constEnd(); ++it) {
361  Incidence::Ptr incidence = *it;
362  if (oldUidToNewInc.contains(incidence->relatedTo())) {
363  Incidence::Ptr parentInc = oldUidToNewInc[incidence->relatedTo()];
364  incidence->setRelatedTo(parentInc->uid());
365  } else {
366  // not related to anything in the clipboard
367  incidence->setRelatedTo(QString());
368  }
369  }
370 
371  return list;
372 }
373 
374 Incidence::Ptr DndFactory::pasteIncidence(const KDateTime &newDateTime,
375  const QFlags<PasteFlag> &pasteOptions)
376 {
377  QClipboard *clipboard = QApplication::clipboard();
378  MemoryCalendar::Ptr calendar(createDropCalendar(clipboard->mimeData()));
379 
380  if (!calendar) {
381  kDebug() << "Can't parse clipboard";
382  return Incidence::Ptr();
383  }
384 
385  Incidence::List incidenceList = calendar->incidences();
386  Incidence::Ptr incidence = incidenceList.isEmpty() ? Incidence::Ptr() : incidenceList.first();
387 
388  return d->pasteIncidence(incidence, newDateTime, pasteOptions);
389 }
KCalUtils::VCalDrag::fromMimeData
KCALUTILS_EXPORT bool fromMimeData(const QMimeData *e, const KCalCore::MemoryCalendar::Ptr &cal)
Decode drag&drop object to vCalendar component vcal.
Definition: vcaldrag.cpp:54
KCalCore::Todo::dtStart
virtual KDateTime dtStart() const
KCalUtils::DndFactory::createDropTodo
KCalCore::Todo::Ptr createDropTodo(const QMimeData *md)
Create Todo object from mime data.
Definition: dndfactory.cpp:251
KCalUtils::DndFactory::cutIncidence
void cutIncidence(const KCalCore::Incidence::Ptr &)
Cut the incidence to the clipboard.
Definition: dndfactory.cpp:278
KCalCore::Event::Ptr
QSharedPointer< Event > Ptr
KCalUtils::ICalDrag::fromMimeData
KCALUTILS_EXPORT bool fromMimeData(const QMimeData *e, const KCalCore::MemoryCalendar::Ptr &cal)
Decode drag&drop object to iCalendar component cal.
Definition: icaldrag.cpp:54
KCalUtils::DndFactory::createDropCalendar
KCalCore::MemoryCalendar::Ptr createDropCalendar(QDropEvent *de)
Create the calendar that is contained in the drop event's data.
Definition: dndfactory.cpp:215
KCalUtils::DndFactory::createDropEvent
KCalCore::Event::Ptr createDropEvent(const QMimeData *md)
Create Event object from mime data.
Definition: dndfactory.cpp:225
KCalCore::Event::List
QVector< Ptr > List
KCalCore::MemoryCalendar
KCalCore::MemoryCalendar::Ptr
QSharedPointer< MemoryCalendar > Ptr
KCalUtils::ICalDrag::populateMimeData
KCALUTILS_EXPORT bool populateMimeData(QMimeData *e, const KCalCore::MemoryCalendar::Ptr &cal)
Sets the iCalendar representation as data of the drag object.
Definition: icaldrag.cpp:38
dndfactory.h
This file is part of the API for handling calendar data and defines the DndFactory class...
KCalCore::Todo::List
QVector< Ptr > List
KCalUtils::DndFactory::createDrag
QDrag * createDrag(QWidget *owner)
Create a drag object for the whole calendar.
Definition: dndfactory.cpp:157
KCalCore::Todo
KCalCore::Event
KCalUtils::DndFactory::copyIncidence
bool copyIncidence(const KCalCore::Incidence::Ptr &)
Copy the incidence to clipboard/.
Definition: dndfactory.cpp:324
KCalUtils::DndFactory::pasteIncidence
KCalCore::Incidence::Ptr pasteIncidence(const KDateTime &newDateTime=KDateTime(), const QFlags< PasteFlag > &pasteOptions=QFlags< PasteFlag >())
This function clones the incidence that's in the clipboard and sets the clone's date/time to the spec...
Definition: dndfactory.cpp:374
KCalUtils::DndFactory::createMimeData
QMimeData * createMimeData()
Create the mime data for the whole calendar.
Definition: dndfactory.cpp:147
KCalUtils::VCalDrag::populateMimeData
KCALUTILS_EXPORT bool populateMimeData(QMimeData *e, const KCalCore::MemoryCalendar::Ptr &cal)
Sets the vCalendar representation as data of the drag object.
Definition: vcaldrag.cpp:38
KCalUtils::DndFactory
vCalendar/iCalendar Drag-and-Drop object factory.
Definition: dndfactory.h:58
KCalUtils::DndFactory::cutIncidences
bool cutIncidences(const KCalCore::Incidence::List &incidences)
Cuts a list of incidences to the clipboard.
Definition: dndfactory.cpp:285
KCalUtils::DndFactory::copyIncidences
bool copyIncidences(const KCalCore::Incidence::List &incidences)
Copies a list of incidences to the clipboard.
Definition: dndfactory.cpp:298
KCalCore::Todo::Ptr
QSharedPointer< Todo > Ptr
KCalCore::Event::dtStart
virtual KDateTime dtStart() const
KCalUtils::DndFactory::pasteIncidences
KCalCore::Incidence::List pasteIncidences(const KDateTime &newDateTime=KDateTime(), const QFlags< PasteFlag > &pasteOptions=QFlags< PasteFlag >())
This function clones the incidences that are in the clipboard and sets the clone's date/time to the s...
Definition: dndfactory.cpp:331
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:00:18 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KCalUtils Library

Skip menu "KCalUtils Library"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Members
  • File List
  • Related Pages

kdepimlibs API Reference

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kldap
  • kmbox
  • kmime
  • kpimidentities
  • kpimtextedit
  • kresources
  • ktnef
  • kxmlrpcclient
  • microblog

Search



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