• Skip to content
  • Skip to link menu
KDE 4.0 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • Sitemap
  • Contact Us
 

KCal Library

dndfactory.cpp

Go to the documentation of this file.
00001 /*
00002   This file is part of the kcal library.
00003 
00004   Copyright (c) 1998 Preston Brown <pbrown@kde.org>
00005   Copyright (c) 2001,2002 Cornelius Schumacher <schumacher@kde.org>
00006   Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
00007   Copyright (c) 2005 Rafal Rzepecki <divide@users.sourceforge.net>
00008 
00009   This library is free software; you can redistribute it and/or
00010   modify it under the terms of the GNU Library General Public
00011   License as published by the Free Software Foundation; either
00012   version 2 of the License, or (at your option) any later version.
00013 
00014   This library is distributed in the hope that it will be useful,
00015   but WITHOUT ANY WARRANTY; without even the implied warranty of
00016   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017   Library General Public License for more details.
00018 
00019   You should have received a copy of the GNU Library General Public License
00020   along with this library; see the file COPYING.LIB.  If not, write to
00021   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00022   Boston, MA 02110-1301, USA.
00023 */
00037 #include "dndfactory.h"
00038 #include "vcaldrag.h"
00039 #include "icaldrag.h"
00040 #include "calendar.h"
00041 #include "calendarlocal.h"
00042 
00043 #include <kiconloader.h>
00044 #include <kdebug.h>
00045 #include <klocale.h>
00046 #include <kurl.h>
00047 
00048 #include <QtGui/QApplication>
00049 #include <QtGui/QClipboard>
00050 #include <QtGui/QDropEvent>
00051 #include <QtGui/QPixmap>
00052 
00053 using namespace KCal;
00054 
00059 //@cond PRIVATE
00060 class KCal::DndFactory::Private
00061 {
00062   public:
00063     Private( Calendar *cal )
00064       : mCalendar ( cal )
00065     {}
00066     Calendar *mCalendar;
00067 };
00068 //@endcond
00069 
00070 DndFactory::DndFactory( Calendar *cal )
00071   : d( new KCal::DndFactory::Private ( cal ) )
00072 {
00073 }
00074 
00075 DndFactory::~DndFactory()
00076 {
00077   delete d;
00078 }
00079 
00080 QDrag *DndFactory::createDrag( QWidget *owner )
00081 {
00082   QDrag *drag = new QDrag( owner );
00083   QMimeData *mimeData = new QMimeData;
00084   drag->setMimeData( mimeData );
00085 
00086   ICalDrag::populateMimeData( mimeData, d->mCalendar );
00087   VCalDrag::populateMimeData( mimeData, d->mCalendar );
00088 
00089   return drag;
00090 }
00091 
00092 QDrag *DndFactory::createDrag( Incidence *incidence, QWidget *owner )
00093 {
00094   CalendarLocal cal( d->mCalendar->timeSpec() );
00095   Incidence *i = incidence->clone();
00096   cal.addIncidence( i );
00097 
00098   QDrag *drag = new QDrag( owner );
00099   QMimeData *mimeData = new QMimeData;
00100   drag->setMimeData( mimeData );
00101 
00102   ICalDrag::populateMimeData( mimeData, &cal );
00103   VCalDrag::populateMimeData( mimeData, &cal );
00104 
00105   KUrl uri = i->uri();
00106   if ( uri.isValid() ) {
00107     QMap<QString, QString> metadata;
00108     metadata["labels"] = KUrl::toPercentEncoding( i->summary() );
00109     uri.populateMimeData( mimeData, metadata );
00110   }
00111 
00112   if ( i->type() == "Event" ) {
00113     drag->setPixmap( BarIcon( "appointment" ) );
00114   } else if ( i->type() == "Todo" ) {
00115     drag->setPixmap( BarIcon( "todo" ) );
00116   }
00117 
00118   return drag;
00119 }
00120 
00121 Calendar *DndFactory::createDropCalendar( const QMimeData *md )
00122 {
00123   Calendar *cal = new CalendarLocal( d->mCalendar->timeSpec() );
00124 
00125   if ( ICalDrag::fromMimeData( md, cal ) ||
00126        VCalDrag::fromMimeData( md, cal ) ){
00127     return cal;
00128   }
00129   return 0;
00130 }
00131 
00132 Calendar *DndFactory::createDropCalendar( QDropEvent *de )
00133 {
00134   Calendar *cal = createDropCalendar( de->mimeData() );
00135   if ( cal ) {
00136     de->accept();
00137     return cal;
00138   }
00139   return 0;
00140 }
00141 
00142 Event *DndFactory::createDropEvent( QDropEvent *de )
00143 {
00144   kDebug(5800) << "DndFactory::createDrop()";
00145   Event *ev = 0;
00146   Calendar *cal = createDropCalendar( de );
00147 
00148   if ( cal ) {
00149     Event::List events = cal->events();
00150     if ( !events.isEmpty() ) {
00151       ev = new Event( *events.first() );
00152     }
00153     delete cal;
00154   }
00155   return ev;
00156 }
00157 
00158 Todo *DndFactory::createDropTodo( QDropEvent *de )
00159 {
00160   kDebug(5800) << "DndFactory::createDropTodo()";
00161   Todo *todo = 0;
00162   Calendar *cal = createDropCalendar( de );
00163 
00164   if ( cal ) {
00165     Todo::List todos = cal->todos();
00166     if ( !todos.isEmpty() ) {
00167       todo = new Todo( *todos.first() );
00168     }
00169     delete cal;
00170   }
00171 
00172   return todo;
00173 }
00174 
00175 void DndFactory::cutIncidence( Incidence *selectedInc )
00176 {
00177   if ( copyIncidence( selectedInc ) ) {
00178     d->mCalendar->deleteIncidence( selectedInc );
00179   }
00180 }
00181 
00182 bool DndFactory::copyIncidence( Incidence *selectedInc )
00183 {
00184   if ( !selectedInc ) {
00185     return false;
00186   }
00187 
00188   QClipboard *cb = QApplication::clipboard();
00189 
00190   CalendarLocal cal( d->mCalendar->timeSpec() );
00191   Incidence *inc = selectedInc->clone();
00192   cal.addIncidence( inc );
00193 
00194   QMimeData *mimeData = new QMimeData;
00195   cb->setMimeData( mimeData );
00196 
00197   ICalDrag::populateMimeData( mimeData, &cal );
00198   VCalDrag::populateMimeData( mimeData, &cal );
00199 
00200   return true;
00201 }
00202 
00203 Incidence *DndFactory::pasteIncidence( const QDate &newDate, const QTime *newTime )
00204 {
00205   QClipboard *cb = QApplication::clipboard();
00206   Calendar *cal = createDropCalendar( cb->mimeData() );
00207 
00208   if ( !cal ) {
00209     kDebug(5800) << "Can't parse clipboard";
00210     return 0;
00211   }
00212   Incidence *ret = 0;
00213 
00214   Incidence::List incList = cal->incidences();
00215   Incidence *inc = incList.first();
00216 
00217   if ( !incList.isEmpty() && inc ) {
00218     inc = inc->clone();
00219 
00220     inc->recreate();
00221 
00222     if ( inc->type() == "Event" ) {
00223 
00224       Event *anEvent = static_cast<Event*>( inc );
00225       // Calculate length of event
00226       int daysOffset = anEvent->dtStart().date().daysTo(
00227         anEvent->dtEnd().date() );
00228       // new end date if event starts at the same time on the new day
00229       KDateTime endDate( anEvent->dtEnd() );
00230       endDate.setDate( newDate.addDays(daysOffset) );
00231 
00232       KDateTime startDate( anEvent->dtStart() );
00233       startDate.setDate( newDate );
00234       if ( newTime ) {
00235         // additional offset for new time of day
00236         int addSecsOffset( anEvent->dtStart().time().secsTo( *newTime ) );
00237         endDate=endDate.addSecs( addSecsOffset );
00238         startDate.setTime( *newTime );
00239       }
00240       anEvent->setDtStart( startDate );
00241       anEvent->setDtEnd( endDate );
00242 
00243     } else if ( inc->type() == "Todo" ) {
00244       Todo *anTodo = static_cast<Todo*>( inc );
00245       KDateTime dueDate( anTodo->dtDue() );
00246       dueDate.setDate( newDate );
00247       if ( newTime ) {
00248         dueDate.setTime( *newTime );
00249       }
00250       anTodo->setDtDue( dueDate );
00251     } else if ( inc->type() == "Journal" ) {
00252       Journal *anJournal = static_cast<Journal*>( inc );
00253       KDateTime startDate( anJournal->dtStart() );
00254       startDate.setDate( newDate );
00255       if ( newTime ) {
00256         startDate.setTime( *newTime );
00257       } else {
00258         startDate.setTime( QTime( 0, 0, 0 ) );
00259       }
00260       anJournal->setDtStart( startDate );
00261     } else {
00262       kDebug(5800) << "Trying to paste unknown incidence of type" << inc->type();
00263     }
00264 
00265     ret = inc;
00266   }
00267   delete cal;
00268   return ret;
00269 }

KCal Library

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

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • kabc
  • kblog
  • kcal
  • kimap
  • kioslave
  •   imap4
  •   mbox
  • kldap
  • kmime
  • kpimidentities
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.5.5
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal