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

KCalUtils Library

  • 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 <QDropEvent>
50 #include <QPixmap>
51 
52 using namespace KCalCore;
53 using namespace KCalUtils;
54 
59 //@cond PRIVATE
60 class KCalUtils::DndFactory::Private
61 {
62  public:
63  Private( const MemoryCalendar::Ptr &calendar )
64  : mCalendar ( calendar )
65  {}
66 
67  Incidence::Ptr pasteIncidence( const Incidence::Ptr &incidence,
68  KDateTime newDateTime,
69  const QFlags<PasteFlag> &pasteOptions )
70  {
71  Incidence::Ptr inc( incidence );
72 
73  if ( inc ) {
74  inc = Incidence::Ptr( inc->clone() );
75  inc->recreate();
76  }
77 
78  if ( inc && newDateTime.isValid() ) {
79  if ( inc->type() == Incidence::TypeEvent ) {
80  Event::Ptr event = inc.staticCast<Event>();
81  if ( pasteOptions & FlagPasteAtOriginalTime ) {
82  // Set date and preserve time and timezone stuff
83  const QDate date = newDateTime.date();
84  newDateTime = event->dtStart();
85  newDateTime.setDate( date );
86  }
87 
88  // in seconds
89  const int durationInSeconds = event->dtStart().secsTo( event->dtEnd() );
90  const int durationInDays = event->dtStart().daysTo( event->dtEnd() );
91 
92  event->setDtStart( newDateTime );
93 
94  if ( newDateTime.isDateOnly() ) {
95  event->setDtEnd( newDateTime.addDays( durationInDays ) );
96  } else {
97  event->setDtEnd( newDateTime.addSecs( durationInSeconds ) );
98  }
99 
100  } else if ( inc->type() == Incidence::TypeTodo ) {
101  Todo::Ptr aTodo = inc.staticCast<Todo>();
102  const bool pasteAtDtStart = ( pasteOptions & FlagTodosPasteAtDtStart );
103  if ( pasteOptions & FlagPasteAtOriginalTime ) {
104  // Set date and preserve time and timezone stuff
105  const QDate date = newDateTime.date();
106  newDateTime = pasteAtDtStart ? aTodo->dtStart() : aTodo->dtDue();
107  newDateTime.setDate( date );
108  }
109  if ( pasteAtDtStart ) {
110  aTodo->setDtStart( newDateTime );
111  } else {
112  aTodo->setDtDue( newDateTime );
113  }
114 
115  } else if ( inc->type() == Incidence::TypeJournal ) {
116  if ( pasteOptions & FlagPasteAtOriginalTime ) {
117  // Set date and preserve time and timezone stuff
118  const QDate date = newDateTime.date();
119  newDateTime = inc->dtStart();
120  newDateTime.setDate( date );
121  }
122  inc->setDtStart( newDateTime );
123  } else {
124  kDebug() << "Trying to paste unknown incidence of type" << int( inc->type() );
125  }
126  }
127 
128  return inc;
129  }
130 
131  MemoryCalendar::Ptr mCalendar;
132 };
133 //@endcond
134 
135 DndFactory::DndFactory( const MemoryCalendar::Ptr &calendar )
136  : d( new KCalUtils::DndFactory::Private ( calendar ) )
137 {
138 }
139 
140 DndFactory::~DndFactory()
141 {
142  delete d;
143 }
144 
145 QMimeData *DndFactory::createMimeData()
146 {
147  QMimeData *mimeData = new QMimeData;
148 
149  ICalDrag::populateMimeData( mimeData, d->mCalendar );
150  VCalDrag::populateMimeData( mimeData, d->mCalendar );
151 
152  return mimeData;
153 }
154 
155 QDrag *DndFactory::createDrag( QWidget *owner )
156 {
157  QDrag *drag = new QDrag( owner );
158  drag->setMimeData( createMimeData() );
159 
160  return drag;
161 }
162 
163 QMimeData *DndFactory::createMimeData( const Incidence::Ptr &incidence )
164 {
165  MemoryCalendar::Ptr cal( new MemoryCalendar( d->mCalendar->timeSpec() ) );
166  Incidence::Ptr i( incidence->clone() );
167  cal->addIncidence( i );
168 
169  QMimeData *mimeData = new QMimeData;
170 
171  ICalDrag::populateMimeData( mimeData, cal );
172  VCalDrag::populateMimeData( mimeData, cal );
173 
174  KUrl uri = i->uri();
175  if ( uri.isValid() ) {
176  QMap<QString, QString> metadata;
177  metadata["labels"] = KUrl::toPercentEncoding( i->summary() );
178  uri.populateMimeData( mimeData, metadata );
179  }
180 
181  return mimeData;
182 }
183 
184 QDrag *DndFactory::createDrag( const Incidence::Ptr &incidence, QWidget *owner )
185 {
186  QDrag *drag = new QDrag( owner );
187  drag->setMimeData( createMimeData( incidence ) );
188  drag->setPixmap( BarIcon( incidence->iconName() ) );
189 
190  return drag;
191 }
192 
193 MemoryCalendar::Ptr DndFactory::createDropCalendar( const QMimeData *mimeData )
194 {
195  return createDropCalendar( mimeData, d->mCalendar->timeSpec() );
196 }
197 
198 MemoryCalendar::Ptr DndFactory::createDropCalendar( const QMimeData *mimeData,
199  const KDateTime::Spec &timeSpec )
200 {
201  MemoryCalendar::Ptr calendar( new MemoryCalendar( timeSpec ) );
202 
203  if ( ICalDrag::fromMimeData( mimeData, calendar ) ||
204  VCalDrag::fromMimeData( mimeData, calendar ) ){
205  return calendar;
206  }
207 
208  return MemoryCalendar::Ptr();
209 }
210 
211 MemoryCalendar::Ptr DndFactory::createDropCalendar( QDropEvent *dropEvent )
212 {
213  MemoryCalendar::Ptr calendar( createDropCalendar( dropEvent->mimeData() ) );
214  if ( calendar ) {
215  dropEvent->accept();
216  return calendar;
217  }
218  return MemoryCalendar::Ptr();
219 }
220 
221 Event::Ptr DndFactory::createDropEvent( const QMimeData *mimeData )
222 {
223  kDebug();
224  Event::Ptr event;
225  MemoryCalendar::Ptr calendar( createDropCalendar( mimeData ) );
226 
227  if ( calendar ) {
228  Event::List events = calendar->events();
229  if ( !events.isEmpty() ) {
230  event = Event::Ptr( new Event( *events.first() ) );
231  }
232  }
233  return event;
234 }
235 
236 Event::Ptr DndFactory::createDropEvent( QDropEvent *dropEvent )
237 {
238  Event::Ptr event = createDropEvent( dropEvent->mimeData() );
239 
240  if ( event ) {
241  dropEvent->accept();
242  }
243 
244  return event;
245 }
246 
247 Todo::Ptr DndFactory::createDropTodo( const QMimeData *mimeData )
248 {
249  kDebug();
250  Todo::Ptr todo;
251  MemoryCalendar::Ptr calendar( createDropCalendar( mimeData ) );
252 
253  if ( calendar ) {
254  Todo::List todos = calendar->todos();
255  if ( !todos.isEmpty() ) {
256  todo = Todo::Ptr( new Todo( *todos.first() ) );
257  }
258  }
259 
260  return todo;
261 }
262 
263 Todo::Ptr DndFactory::createDropTodo( QDropEvent *dropEvent )
264 {
265  Todo::Ptr todo = createDropTodo( dropEvent->mimeData() );
266 
267  if ( todo ) {
268  dropEvent->accept();
269  }
270 
271  return todo;
272 }
273 
274 void DndFactory::cutIncidence( const Incidence::Ptr &selectedIncidence )
275 {
276  Incidence::List list;
277  list.append( selectedIncidence );
278  cutIncidences( list );
279 }
280 
281 bool DndFactory::cutIncidences( const Incidence::List &incidences )
282 {
283  if ( copyIncidences( incidences ) ) {
284  Incidence::List::ConstIterator it;
285  for ( it = incidences.constBegin(); it != incidences.constEnd(); ++it ) {
286  d->mCalendar->deleteIncidence( *it );
287  }
288  return true;
289  } else {
290  return false;
291  }
292 }
293 
294 bool DndFactory::copyIncidences( const Incidence::List &incidences )
295 {
296  QClipboard *clipboard = QApplication::clipboard();
297  Q_ASSERT( clipboard );
298  MemoryCalendar::Ptr calendar( new MemoryCalendar( d->mCalendar->timeSpec() ) );
299 
300  Incidence::List::ConstIterator it;
301  for ( it = incidences.constBegin(); it != incidences.constEnd(); ++it ) {
302  if ( *it ) {
303  calendar->addIncidence( Incidence::Ptr( ( *it )->clone() ) );
304  }
305  }
306 
307  QMimeData *mimeData = new QMimeData;
308 
309  ICalDrag::populateMimeData( mimeData, calendar );
310  VCalDrag::populateMimeData( mimeData, calendar );
311 
312  if ( calendar->incidences().isEmpty() ) {
313  return false;
314  } else {
315  clipboard->setMimeData( mimeData );
316  return true;
317  }
318 }
319 
320 bool DndFactory::copyIncidence( const Incidence::Ptr &selectedInc )
321 {
322  Incidence::List list;
323  list.append( selectedInc );
324  return copyIncidences( list );
325 }
326 
327 Incidence::List DndFactory::pasteIncidences( const KDateTime &newDateTime,
328  const QFlags<PasteFlag> &pasteOptions )
329 {
330  QClipboard *clipboard = QApplication::clipboard();
331  Q_ASSERT( clipboard );
332  MemoryCalendar::Ptr calendar( createDropCalendar( clipboard->mimeData() ) );
333  Incidence::List list;
334 
335  if ( !calendar ) {
336  kDebug() << "Can't parse clipboard";
337  return list;
338  }
339 
340  // All pasted incidences get new uids, must keep track of old uids,
341  // so we can update child's parents
342  QHash<QString, Incidence::Ptr> oldUidToNewInc;
343 
344  Incidence::List::ConstIterator it;
345  const Incidence::List incidences = calendar->incidences();
346  for ( it = incidences.constBegin();
347  it != incidences.constEnd(); ++it ) {
348  Incidence::Ptr incidence = d->pasteIncidence( *it, newDateTime, pasteOptions );
349  if ( incidence ) {
350  list.append( incidence );
351  oldUidToNewInc[(*it)->uid()] = *it;
352  }
353  }
354 
355  // update relations
356  for ( it = list.constBegin(); it != list.constEnd(); ++it ) {
357  Incidence::Ptr incidence = *it;
358  if ( oldUidToNewInc.contains( incidence->relatedTo() ) ) {
359  Incidence::Ptr parentInc = oldUidToNewInc[incidence->relatedTo()];
360  incidence->setRelatedTo( parentInc->uid() );
361  } else {
362  // not related to anything in the clipboard
363  incidence->setRelatedTo( QString() );
364  }
365  }
366 
367  return list;
368 }
369 
370 Incidence::Ptr DndFactory::pasteIncidence( const KDateTime &newDateTime,
371  const QFlags<PasteFlag> &pasteOptions )
372 {
373  QClipboard *clipboard = QApplication::clipboard();
374  MemoryCalendar::Ptr calendar( createDropCalendar( clipboard->mimeData() ) );
375 
376  if ( !calendar ) {
377  kDebug() << "Can't parse clipboard";
378  return Incidence::Ptr();
379  }
380 
381  Incidence::List incidenceList = calendar->incidences();
382  Incidence::Ptr incidence = incidenceList.isEmpty() ? Incidence::Ptr() : incidenceList.first();
383 
384  return d->pasteIncidence( incidence, newDateTime, pasteOptions );
385 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jul 13 2013 01:26:45 by doxygen 1.8.3.1 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-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