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

Skip menu "kdepimlibs-4.9.1 API Reference"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • 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