libkcal Library API Documentation

calendar.cpp

00001 /*
00002     This file is part of libkcal.
00003 
00004     Copyright (c) 1998 Preston Brown
00005     Copyright (c) 2000-2003 Cornelius Schumacher <schumacher@kde.org>
00006 
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Library General Public
00009     License as published by the Free Software Foundation; either
00010     version 2 of the License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Library General Public License for more details.
00016 
00017     You should have received a copy of the GNU Library General Public License
00018     along with this library; see the file COPYING.LIB.  If not, write to
00019     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00020     Boston, MA 02111-1307, USA.
00021 */
00022 
00023 #include <stdlib.h>
00024 
00025 #include <kdebug.h>
00026 #include <klocale.h>
00027 
00028 #include "exceptions.h"
00029 #include "calfilter.h"
00030 
00031 #include "calendar.h"
00032 
00033 using namespace KCal;
00034 
00035 Calendar::Calendar()
00036 {
00037   mTimeZoneId = QString::fromLatin1( "UTC" );
00038   mLocalTime = false;
00039 
00040   init();
00041 }
00042 
00043 Calendar::Calendar( const QString &timeZoneId )
00044 {
00045   mTimeZoneId = timeZoneId;
00046   mLocalTime = false;
00047 
00048   init();
00049 }
00050 
00051 void Calendar::init()
00052 {
00053   mObserver = 0;
00054   mNewObserver = false;
00055 
00056   mModified = false;
00057 
00058   // Setup default filter, which does nothing
00059   mDefaultFilter = new CalFilter;
00060   mFilter = mDefaultFilter;
00061   mFilter->setEnabled(false);
00062 
00063   // initialize random numbers.  This is a hack, and not
00064   // even that good of one at that.
00065 //  srandom(time(0));
00066 
00067   // user information...
00068   setOwner(i18n("Unknown Name"));
00069   setEmail(i18n("unknown@nowhere"));
00070 
00071 #if 0
00072   tmpStr = KOPrefs::instance()->mTimeZone;
00073 //  kdDebug(5800) << "Calendar::Calendar(): TimeZone: " << tmpStr << endl;
00074   int dstSetting = KOPrefs::instance()->mDaylightSavings;
00075   extern long int timezone;
00076   struct tm *now;
00077   time_t curtime;
00078   curtime = time(0);
00079   now = localtime(&curtime);
00080   int hourOff = - ((timezone / 60) / 60);
00081   if (now->tm_isdst)
00082     hourOff += 1;
00083   QString tzStr;
00084   tzStr.sprintf("%.2d%.2d",
00085         hourOff,
00086         abs((timezone / 60) % 60));
00087 
00088   // if no time zone was in the config file, write what we just discovered.
00089   if (tmpStr.isEmpty()) {
00090 //    KOPrefs::instance()->mTimeZone = tzStr;
00091   } else {
00092     tzStr = tmpStr;
00093   }
00094 
00095   // if daylight savings has changed since last load time, we need
00096   // to rewrite these settings to the config file.
00097   if ((now->tm_isdst && !dstSetting) ||
00098       (!now->tm_isdst && dstSetting)) {
00099     KOPrefs::instance()->mTimeZone = tzStr;
00100     KOPrefs::instance()->mDaylightSavings = now->tm_isdst;
00101   }
00102 
00103   setTimeZone(tzStr);
00104 #endif
00105 
00106 //  KOPrefs::instance()->writeConfig();
00107 }
00108 
00109 Calendar::~Calendar()
00110 {
00111   delete mDefaultFilter;
00112 }
00113 
00114 const QString &Calendar::getOwner() const
00115 {
00116   return mOwner;
00117 }
00118 
00119 void Calendar::setOwner(const QString &os)
00120 {
00121   int i;
00122   mOwner = os;
00123   i = mOwner.find(',');
00124   if (i != -1)
00125     mOwner = mOwner.left(i);
00126 
00127   setModified( true );
00128 }
00129 
00130 void Calendar::setTimeZoneId(const QString &id)
00131 {
00132   mTimeZoneId = id;
00133   mLocalTime = false;
00134 
00135   setModified( true );
00136   doSetTimeZoneId( id );
00137 }
00138 
00139 QString Calendar::timeZoneId() const
00140 {
00141   return mTimeZoneId;
00142 }
00143 
00144 void Calendar::setLocalTime()
00145 {
00146   mLocalTime = true;
00147   mTimeZone = 0;
00148   mTimeZoneId = "";
00149 
00150   setModified( true );
00151 }
00152 
00153 bool Calendar::isLocalTime() const
00154 {
00155   return mLocalTime;
00156 }
00157 
00158 const QString &Calendar::getEmail()
00159 {
00160   return mOwnerEmail;
00161 }
00162 
00163 void Calendar::setEmail(const QString &e)
00164 {
00165   mOwnerEmail = e;
00166 
00167   setModified( true );
00168 }
00169 
00170 void Calendar::setFilter(CalFilter *filter)
00171 {
00172   mFilter = filter;
00173 }
00174 
00175 CalFilter *Calendar::filter()
00176 {
00177   return mFilter;
00178 }
00179 
00180 Incidence::List Calendar::incidences()
00181 {
00182   return mergeIncidenceList( events(), todos(), journals() );
00183 }
00184 
00185 Incidence::List Calendar::rawIncidences()
00186 {
00187   return mergeIncidenceList( rawEvents(), rawTodos(), journals() );
00188 }
00189 
00190 Event::List Calendar::events( const QDate &date, bool sorted )
00191 {
00192   Event::List el = rawEventsForDate( date, sorted );
00193 
00194   mFilter->apply(&el);
00195 
00196   return el;
00197 }
00198 
00199 Event::List Calendar::events( const QDateTime &qdt )
00200 {
00201   Event::List el = rawEventsForDate(qdt);
00202   mFilter->apply(&el);
00203   return el;
00204 }
00205 
00206 Event::List Calendar::events( const QDate &start, const QDate &end,
00207                                   bool inclusive)
00208 {
00209   Event::List el = rawEvents(start,end,inclusive);
00210   mFilter->apply(&el);
00211   return el;
00212 }
00213 
00214 Event::List Calendar::events()
00215 {
00216   Event::List el = rawEvents();
00217   mFilter->apply(&el);
00218   return el;
00219 }
00220 
00221 
00222 bool Calendar::addIncidence(Incidence *i)
00223 {
00224   Incidence::AddVisitor<Calendar> v(this);
00225 
00226   return i->accept(v);
00227 }
00228 
00229 bool Calendar::deleteIncidence( Incidence *i )
00230 {
00231   Incidence::DeleteVisitor<Calendar> v( this );
00232   return i->accept( v );
00233 }
00234 
00235 Incidence *Calendar::incidence( const QString& uid )
00236 {
00237   Incidence *i = event( uid );
00238   if ( i ) return i;
00239   i = todo( uid );
00240   if ( i ) return i;
00241   i = journal( uid );
00242   return i;
00243 }
00244 
00245 Todo::List Calendar::todos()
00246 {
00247   Todo::List tl = rawTodos();
00248   mFilter->apply( &tl );
00249   return tl;
00250 }
00251 
00252 // When this is called, the todo have already been added to the calendar.
00253 // This method is only about linking related todos
00254 void Calendar::setupRelations( Incidence *incidence )
00255 {
00256   QString uid = incidence->uid();
00257 
00258   // First, go over the list of orphans and see if this is their parent
00259   while( Incidence* i = mOrphans[ uid ] ) {
00260     mOrphans.remove( uid );
00261     i->setRelatedTo( incidence );
00262     incidence->addRelation( i );
00263     mOrphanUids.remove( i->uid() );
00264   }
00265 
00266   // Now see about this incidences parent
00267   if( !incidence->relatedTo() && !incidence->relatedToUid().isEmpty() ) {
00268     // This incidence has a uid it is related to, but is not registered to it yet
00269     // Try to find it
00270     Incidence* parent = this->incidence( incidence->relatedToUid() );
00271     if( parent ) {
00272       // Found it
00273       incidence->setRelatedTo( parent );
00274       parent->addRelation( incidence );
00275     } else {
00276       // Not found, put this in the mOrphans list
00277       mOrphans.insert( incidence->relatedToUid(), incidence );
00278       mOrphanUids.insert( incidence->uid(), incidence );
00279     }
00280   }
00281 }
00282 
00283 // If a task with subtasks is deleted, move it's subtasks to the orphans list
00284 void Calendar::removeRelations( Incidence *incidence )
00285 {
00286   if( !incidence ) {
00287     kdDebug(5800) << "Warning: Calendar::removeRelations( 0 )!\n";
00288     return;
00289   }
00290 
00291   QString uid = incidence->uid();
00292 
00293   Incidence::List relations = incidence->relations();
00294   Incidence::List::ConstIterator it;
00295   for( it = relations.begin(); it != relations.end(); ++it ) {
00296     Incidence *i = *it;
00297     if( !mOrphanUids.find( i->uid() ) ) {
00298       mOrphans.insert( uid, i );
00299       mOrphanUids.insert( i->uid(), i );
00300       i->setRelatedTo( 0 );
00301       i->setRelatedToUid( uid );
00302     }
00303   }
00304 
00305   // If this incidence is related to something else, tell that about it
00306   if( incidence->relatedTo() )
00307     incidence->relatedTo()->removeRelation( incidence );
00308 
00309   // Remove this one from the orphans list
00310   if( mOrphanUids.remove( uid ) )
00311     // This incidence is located in the orphans list - it should be removed
00312     if( !( incidence->relatedTo() != 0 && mOrphans.remove( incidence->relatedTo()->uid() ) ) ) {
00313       // Removing wasn't that easy
00314       for( QDictIterator<Incidence> it( mOrphans ); it.current(); ++it ) {
00315     if( it.current()->uid() == uid ) {
00316       mOrphans.remove( it.currentKey() );
00317       break;
00318     }
00319       }
00320     }
00321 }
00322 
00323 void Calendar::registerObserver( Observer *observer )
00324 {
00325   mObserver = observer;
00326   mNewObserver = true;
00327 }
00328 
00329 void Calendar::setModified( bool modified )
00330 {
00331   if ( modified != mModified || mNewObserver ) {
00332     mNewObserver = false;
00333     if ( mObserver ) mObserver->calendarModified( modified, this );
00334     mModified = modified;
00335   }
00336 }
00337 
00338 void Calendar::setLoadedProductId( const QString &id )
00339 {
00340   mLoadedProductId = id;
00341 }
00342 
00343 QString Calendar::loadedProductId()
00344 {
00345   return mLoadedProductId;
00346 }
00347 
00348 Incidence::List Calendar::mergeIncidenceList( const Event::List &e,
00349                                               const Todo::List &t,
00350                                               const Journal::List &j )
00351 {
00352   Incidence::List incidences;
00353   
00354   Event::List::ConstIterator it1;
00355   for( it1 = e.begin(); it1 != e.end(); ++it1 ) incidences.append( *it1 );
00356 
00357   Todo::List::ConstIterator it2;
00358   for( it2 = t.begin(); it2 != t.end(); ++it2 ) incidences.append( *it2 );
00359 
00360   Journal::List::ConstIterator it3;
00361   for( it3 = j.begin(); it3 != j.end(); ++it3 ) incidences.append( *it3 );
00362 
00363   return incidences;
00364 }
00365 
00366 bool Calendar::beginChange( Incidence * )
00367 {
00368   return true;
00369 }
00370 
00371 bool Calendar::endChange( Incidence * )
00372 {
00373   return true;
00374 }
00375 
00376 #include "calendar.moc"
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:20 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003