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(Event *selectedEv, QWidget *owner)
00047 {
00048   CalendarLocal cal;
00049   cal.setTimeZone( mCalendar->getTimeZoneStr() );
00050   Event *ev = new Event( *selectedEv );
00051   cal.addEvent(ev);
00052   ICalDrag *icd = new ICalDrag(&cal, owner);
00053   icd->setPixmap( BarIcon( "appointment" ) );
00054 
00055   return icd;
00056 }
00057 
00058 ICalDrag *DndFactory::createDragTodo(Todo *selectedEv, QWidget *owner)
00059 {
00060   CalendarLocal cal;
00061   cal.setTimeZone( mCalendar->getTimeZoneStr() );
00062   Todo *ev = new Todo( *selectedEv );
00063   cal.addTodo(ev);
00064   ICalDrag *icd = new ICalDrag(&cal, owner);
00065   icd->setPixmap( BarIcon( "todo" ) );
00066 
00067   return icd;
00068 }
00069 
00070 Event *DndFactory::createDrop(QDropEvent *de)
00071 {
00072   kdDebug() << "DndFactory::createDrop()" << endl;
00073 
00074   CalendarLocal cal;
00075   cal.setTimeZone( mCalendar->getTimeZoneStr() );
00076 
00077   if ( ICalDrag::decode( de, &cal ) || VCalDrag::decode( de, &cal ) ) {
00078     de->accept();
00079 
00080     QPtrList<Event> events = cal.events();
00081     if ( !events.isEmpty() ) {
00082       Event *event = new Event( *events.first() );
00083       return event;
00084     }
00085   }
00086 
00087   return 0;
00088 }
00089 
00090 Todo *DndFactory::createDropTodo(QDropEvent *de)
00091 {
00092   kdDebug(5800) << "VCalFormat::createDropTodo()" << endl;
00093 
00094   CalendarLocal cal;
00095   cal.setTimeZone( mCalendar->getTimeZoneStr() );
00096 
00097   if ( ICalDrag::decode( de, &cal ) || VCalDrag::decode( de, &cal ) ) {
00098     de->accept();
00099 
00100     QPtrList<Todo> todos = cal.todos();
00101     if ( !todos.isEmpty() ) {
00102       Todo *todo = new Todo( *todos.first() );
00103       return todo;
00104     }
00105   }
00106 
00107   return 0;
00108 }
00109 
00110 
00111 void DndFactory::cutEvent(Event *selectedEv)
00112 {
00113   if (copyEvent(selectedEv)) {
00114     mCalendar->deleteEvent(selectedEv);
00115   }
00116 }
00117 
00118 bool DndFactory::copyEvent( Event *selectedEv )
00119 {
00120   QClipboard *cb = QApplication::clipboard();
00121 
00122   CalendarLocal cal;
00123   cal.setTimeZone( mCalendar->getTimeZoneStr() );
00124   Event *ev = new Event( *selectedEv );
00125   cal.addEvent(ev);
00126   cb->setData( new ICalDrag( &cal ) );
00127 
00128   return true;
00129 }
00130 
00131 Event *DndFactory::pasteEvent(const QDate &newDate, const QTime *newTime)
00132 {
00133 //  kdDebug() << "DnDFactory::pasteEvent()" << endl;
00134 
00135   CalendarLocal cal;
00136 
00137   Event *anEvent = 0;
00138 
00139   QClipboard *cb = QApplication::clipboard();
00140 
00141   if ( !ICalDrag::decode( cb->data(), &cal ) &&
00142        !VCalDrag::decode( cb->data(), &cal ) ) {
00143     kdDebug(5800) << "Can't parse clipboard" << endl;
00144     return 0;
00145   }
00146 
00147   QPtrList<Event> evList = cal.events();
00148   Event *ev = evList.first();
00149   if ( ev ) {
00150     anEvent = new Event( *ev );
00151 
00152     anEvent->recreate();
00153 
00154     int daysOffset = anEvent->dtEnd().date().dayOfYear() -
00155       anEvent->dtStart().date().dayOfYear();
00156 
00157     if ( newTime ) {
00158       anEvent->setDtStart( QDateTime( newDate, *newTime ) );
00159     } else {
00160       anEvent->setDtStart( QDateTime( newDate, anEvent->dtStart().time() ) );
00161     }
00162 
00163     anEvent->setDtEnd( QDateTime( newDate.addDays( daysOffset ),
00164                                   anEvent->dtEnd().time() ) );
00165     mCalendar->addEvent( anEvent );
00166   } else {
00167     QPtrList<Todo> toList = cal.todos();
00168     Todo *to = toList.first();
00169     if (to) {
00170       //anTodo = new Todo(*to);
00171       kdDebug(5800) << "Trying to paste a Todo." << endl;
00172       // TODO: check, if todos can be pasted
00173       //    Todo *aTodo = VTodoToEvent(curVO);
00174       //    mCalendar->addTodo(aTodo);
00175     } else {
00176       kdDebug(5800) << "unknown event type in paste!!!" << endl;
00177     }
00178   }
00179 
00180   return anEvent;
00181 }
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sun Feb 15 11:40:27 2004 by doxygen 1.3.5 written by Dimitri van Heesch, © 1997-2001