libkcal

dndfactory.cpp

Go to the documentation of this file.
00001 /*
00002     This file is part of libkcal.
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 
00008     This library is free software; you can redistribute it and/or
00009     modify it under the terms of the GNU Library General Public
00010     License as published by the Free Software Foundation; either
00011     version 2 of the License, or (at your option) any later version.
00012 
00013     This library is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016     Library General Public License for more details.
00017 
00018     You should have received a copy of the GNU Library General Public License
00019     along with this library; see the file COPYING.LIB.  If not, write to
00020     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00021     Boston, MA 02110-1301, USA.
00022 */
00023 
00024 #include <qapplication.h>
00025 #include <qclipboard.h>
00026 
00027 #include <kiconloader.h>
00028 #include <kdebug.h>
00029 #include <kmessagebox.h>
00030 #include <klocale.h>
00031 
00032 #include "vcaldrag.h"
00033 #include "icaldrag.h"
00034 #include "calendar.h"
00035 #include "vcalformat.h"
00036 #include "icalformat.h"
00037 #include "calendarlocal.h"
00038 
00039 #include "dndfactory.h"
00040 
00041 using namespace KCal;
00042 
00043 DndFactory::DndFactory( Calendar *cal ) :
00044   mCalendar( cal )
00045 {
00046 }
00047 
00048 ICalDrag *DndFactory::createDrag( Incidence *incidence, QWidget *owner )
00049 {
00050   CalendarLocal cal( mCalendar->timeZoneId() );
00051   Incidence *i = incidence->clone();
00052   cal.addIncidence( i );
00053 
00054   ICalDrag *icd = new ICalDrag( &cal, owner );
00055   if ( i->type() == "Event" )
00056     icd->setPixmap( BarIcon( "appointment" ) );
00057   else if ( i->type() == "Todo" )
00058     icd->setPixmap( BarIcon( "todo" ) );
00059 
00060   return icd;
00061 }
00062 
00063 Event *DndFactory::createDrop(QDropEvent *de)
00064 {
00065   kdDebug(5800) << "DndFactory::createDrop()" << endl;
00066 
00067   CalendarLocal cal( mCalendar->timeZoneId() );
00068 
00069   if ( ICalDrag::decode( de, &cal ) || VCalDrag::decode( de, &cal ) ) {
00070     de->accept();
00071 
00072     Event::List events = cal.events();
00073     if ( !events.isEmpty() ) {
00074       Event *event = new Event( *events.first() );
00075       return event;
00076     }
00077   }
00078 
00079   return 0;
00080 }
00081 
00082 Todo *DndFactory::createDropTodo(QDropEvent *de)
00083 {
00084   kdDebug(5800) << "VCalFormat::createDropTodo()" << endl;
00085 
00086   CalendarLocal cal( mCalendar->timeZoneId() );
00087 
00088   if ( ICalDrag::decode( de, &cal ) || VCalDrag::decode( de, &cal ) ) {
00089     de->accept();
00090 
00091     Todo::List todos = cal.todos();
00092     if ( !todos.isEmpty() ) {
00093       Todo *todo = new Todo( *todos.first() );
00094       return todo;
00095     }
00096   }
00097 
00098   return 0;
00099 }
00100 
00101 
00102 void DndFactory::cutIncidence( Incidence *selectedInc )
00103 {
00104   if ( copyIncidence( selectedInc ) ) {
00105     mCalendar->deleteIncidence( selectedInc );
00106   }
00107 }
00108 
00109 bool DndFactory::copyIncidence( Incidence *selectedInc )
00110 {
00111   if ( !selectedInc )
00112     return false;
00113   QClipboard *cb = QApplication::clipboard();
00114 
00115   CalendarLocal cal( mCalendar->timeZoneId() );
00116   Incidence *inc = selectedInc->clone();
00117   cal.addIncidence( inc );
00118   cb->setData( new ICalDrag( &cal ) );
00119 
00120   return true;
00121 }
00122 
00123 Incidence *DndFactory::pasteIncidence(const QDate &newDate, const QTime *newTime)
00124 {
00125 //  kdDebug(5800) << "DnDFactory::pasteEvent()" << endl;
00126 
00127   CalendarLocal cal( mCalendar->timeZoneId() );
00128 
00129   QClipboard *cb = QApplication::clipboard();
00130 
00131   if ( !ICalDrag::decode( cb->data(), &cal ) &&
00132        !VCalDrag::decode( cb->data(), &cal ) ) {
00133     kdDebug(5800) << "Can't parse clipboard" << endl;
00134     return 0;
00135   }
00136 
00137   Incidence::List incList = cal.incidences();
00138   Incidence *inc = incList.first();
00139 
00140   if ( !incList.isEmpty() && inc ) {
00141     inc = inc->clone();
00142 
00143     inc->recreate();
00144 
00145     if ( inc->type() == "Event" ) {
00146 
00147       Event *anEvent = static_cast<Event*>( inc );
00148       // Calculate length of event
00149       int daysOffset = anEvent->dtStart().date().daysTo(
00150         anEvent->dtEnd().date() );
00151       // new end date if event starts at the same time on the new day
00152       QDateTime endDate( newDate.addDays(daysOffset), anEvent->dtEnd().time() );
00153 
00154       if ( newTime ) {
00155         // additional offset for new time of day
00156         int addSecsOffset( anEvent->dtStart().time().secsTo( *newTime ));
00157         endDate=endDate.addSecs( addSecsOffset );
00158         anEvent->setDtStart( QDateTime( newDate, *newTime ) );
00159       } else {
00160         anEvent->setDtStart( QDateTime( newDate, anEvent->dtStart().time() ) );
00161       }
00162       anEvent->setDtEnd( endDate );
00163 
00164     } else if ( inc->type() == "Todo" ) {
00165       Todo *anTodo = static_cast<Todo*>( inc );
00166       if ( newTime ) {
00167         anTodo->setDtDue( QDateTime( newDate, *newTime ) );
00168       } else {
00169         anTodo->setDtDue( QDateTime( newDate, anTodo->dtDue().time() ) );
00170       }
00171     } else if ( inc->type() == "Journal" ) {
00172       Journal *anJournal = static_cast<Journal*>( inc );
00173       if ( newTime ) {
00174         anJournal->setDtStart( QDateTime( newDate, *newTime ) );
00175       } else {
00176         anJournal->setDtStart( QDateTime( newDate ) );
00177       }
00178     } else {
00179       kdDebug(5850) << "Trying to paste unknown incidence of type " << inc->type() << endl;
00180     }
00181 
00182     return inc;
00183 
00184   }
00185 
00186   return 0;
00187 }