libkcal Library API Documentation

dndfactory.cpp

00001 /*
00002     This file is part of libkcal.
00003     Copyright (c) 1998 Preston Brwon
00004     Copyright (c) 2001,2002 Cornelius Schumacher <schumacher@kde.org>
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Library General Public
00008     License as published by the Free Software Foundation; either
00009     version 2 of the License, or (at your option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Library General Public License for more details.
00015 
00016     You should have received a copy of the GNU Library General Public License
00017     along with this library; see the file COPYING.LIB.  If not, write to
00018     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00019     Boston, MA 02111-1307, USA.
00020 */
00021 
00022 #include <qapplication.h>
00023 #include <qclipboard.h>
00024 
00025 #include <kiconloader.h>
00026 #include <kdebug.h>
00027 #include <kmessagebox.h>
00028 #include <klocale.h>
00029 
00030 #include "vcaldrag.h"
00031 #include "icaldrag.h"
00032 #include "calendar.h"
00033 #include "vcalformat.h"
00034 #include "icalformat.h"
00035 #include "calendarlocal.h"
00036 
00037 #include "dndfactory.h"
00038 
00039 using namespace KCal;
00040 
00041 DndFactory::DndFactory( Calendar *cal ) :
00042   mCalendar( cal )
00043 {
00044 }
00045 
00046 ICalDrag *DndFactory::createDrag( Incidence *incidence, QWidget *owner )
00047 {
00048   CalendarLocal cal( mCalendar->timeZoneId() );
00049   Incidence *i = incidence->clone();
00050   cal.addIncidence( i );
00051 
00052   ICalDrag *icd = new ICalDrag( &cal, owner );
00053   if ( i->type() == "Event" )
00054     icd->setPixmap( BarIcon( "appointment" ) );
00055   else if ( i->type() == "Todo" )
00056     icd->setPixmap( BarIcon( "todo" ) );
00057 
00058   return icd;
00059 }
00060 
00061 Event *DndFactory::createDrop(QDropEvent *de)
00062 {
00063   kdDebug() << "DndFactory::createDrop()" << endl;
00064 
00065   CalendarLocal cal( mCalendar->timeZoneId() );
00066 
00067   if ( ICalDrag::decode( de, &cal ) || VCalDrag::decode( de, &cal ) ) {
00068     de->accept();
00069 
00070     Event::List events = cal.events();
00071     if ( !events.isEmpty() ) {
00072       Event *event = new Event( *events.first() );
00073       return event;
00074     }
00075   }
00076 
00077   return 0;
00078 }
00079 
00080 Todo *DndFactory::createDropTodo(QDropEvent *de)
00081 {
00082   kdDebug(5800) << "VCalFormat::createDropTodo()" << endl;
00083 
00084   CalendarLocal cal( mCalendar->timeZoneId() );
00085 
00086   if ( ICalDrag::decode( de, &cal ) || VCalDrag::decode( de, &cal ) ) {
00087     de->accept();
00088 
00089     Todo::List todos = cal.todos();
00090     if ( !todos.isEmpty() ) {
00091       Todo *todo = new Todo( *todos.first() );
00092       return todo;
00093     }
00094   }
00095 
00096   return 0;
00097 }
00098 
00099 
00100 void DndFactory::cutEvent(Event *selectedEv)
00101 {
00102   if (copyEvent(selectedEv)) {
00103     mCalendar->deleteEvent(selectedEv);
00104   }
00105 }
00106 
00107 void DndFactory::cutTodo(Todo *selectedTodo)
00108 {
00109   if (copyTodo(selectedTodo)) {
00110     mCalendar->deleteTodo(selectedTodo);
00111   }
00112 }
00113 
00114 bool DndFactory::copyEvent( Event *selectedEv )
00115 {
00116   QClipboard *cb = QApplication::clipboard();
00117 
00118   CalendarLocal cal( mCalendar->timeZoneId() );
00119   Event *ev = new Event( *selectedEv );
00120   cal.addEvent(ev);
00121   cb->setData( new ICalDrag( &cal ) );
00122 
00123   return true;
00124 }
00125 
00126 bool DndFactory::copyTodo( Todo *selectedTodo )
00127 {
00128   QClipboard *cb = QApplication::clipboard();
00129 
00130   CalendarLocal cal( mCalendar->timeZoneId() );
00131   Todo *todo = new Todo( *selectedTodo );
00132   cal.addTodo(todo);
00133   cb->setData( new ICalDrag( &cal ) );
00134 
00135   return true;
00136 }
00137 
00138 Incidence *DndFactory::pasteIncidence(const QDate &newDate, const QTime *newTime)
00139 {
00140 //  kdDebug() << "DnDFactory::pasteEvent()" << endl;
00141 
00142   CalendarLocal cal( mCalendar->timeZoneId() );
00143 
00144   Event *anEvent = 0;
00145 
00146   QClipboard *cb = QApplication::clipboard();
00147 
00148   if ( !ICalDrag::decode( cb->data(), &cal ) &&
00149        !VCalDrag::decode( cb->data(), &cal ) ) {
00150     kdDebug(5800) << "Can't parse clipboard" << endl;
00151     return 0;
00152   }
00153 
00154   Event::List evList = cal.events();
00155   Event *ev = evList.first();
00156   if ( !evList.isEmpty() && ev ) {
00157     anEvent = new Event( *ev );
00158 
00159     anEvent->recreate();
00160 
00161     // Calculate length of event
00162     int daysOffset = anEvent->dtStart().date().daysTo(
00163       anEvent->dtEnd().date() );
00164     // new end date if event starts at the same time on the new day
00165     QDateTime endDate(newDate.addDays(daysOffset), anEvent->dtEnd().time() );
00166 
00167     if ( newTime ) {
00168       // additional offset for new time of day
00169       int addSecsOffset( anEvent->dtStart().time().secsTo( *newTime ));
00170       endDate=endDate.addSecs( addSecsOffset );
00171       anEvent->setDtStart( QDateTime( newDate, *newTime ) );
00172     } else {
00173       anEvent->setDtStart( QDateTime( newDate, anEvent->dtStart().time() ) );
00174     }
00175 
00176     anEvent->setDtEnd( endDate );
00177     if (mCalendar) mCalendar->addEvent( anEvent );
00178         return anEvent;
00179         
00180   } else {
00181     
00182     Todo::List toList = cal.todos();
00183     Todo *to = toList.first();
00184     if ( !toList.isEmpty() && to ) {
00185       Todo *anTodo = new Todo(*to);
00186             anTodo->recreate();
00187             
00188         if ( newTime ) {
00189         anTodo->setDtDue( QDateTime( newDate, *newTime ) );
00190         } else {
00191         anTodo->setDtDue( QDateTime( newDate, anTodo->dtDue().time() ) );
00192         }
00193       if (mCalendar) mCalendar->addTodo(anTodo);
00194             return anTodo;
00195     } else {
00196       kdDebug(5800) << "unknown event type in paste!!!" << endl;
00197     }
00198   }
00199 
00200   return 0;
00201 }
KDE Logo
This file is part of the documentation for libkcal Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat May 1 11:36:21 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003