libkcal Library API Documentation

calendar.cpp

00001 /*
00002     This file is part of libkcal.
00003     Copyright (c) 1998 Preston Brown
00004     Copyright (c) 2000,2001 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 <stdlib.h>
00023 
00024 #include <kdebug.h>
00025 #include <klocale.h>
00026 
00027 #include "exceptions.h"
00028 #include "calfilter.h"
00029 
00030 #include "calendar.h"
00031 
00032 using namespace KCal;
00033 
00034 class AddIncidenceVisitor : public Incidence::Visitor {
00035   public:
00037     AddIncidenceVisitor( Calendar *calendar ) : mCalendar( calendar ) {}
00038     
00039     bool visit( Event *e ) { mCalendar->addEvent( e ); return true; }
00040     bool visit( Todo *t ) { mCalendar->addTodo( t ); return true; }
00041     bool visit( Journal *j ) { mCalendar->addJournal( j ); return true; }
00042 
00043   private:
00044     Calendar *mCalendar;
00045 };
00046 
00047 Calendar::Calendar()
00048 {
00049   mTimeZoneId = QString::fromLatin1( "UTC" );
00050 
00051   init();
00052 }
00053 
00054 Calendar::Calendar( const QString &timeZoneId )
00055 {
00056   mTimeZoneId = timeZoneId;
00057   
00058   init();
00059 }
00060 
00061 void Calendar::init()
00062 {
00063   mObserver = 0;
00064   mNewObserver = false;
00065 
00066   mModified = false;
00067 
00068   // Setup default filter, which does nothing
00069   mDefaultFilter = new CalFilter;
00070   mFilter = mDefaultFilter;
00071   mFilter->setEnabled(false);
00072 
00073   // initialize random numbers.  This is a hack, and not
00074   // even that good of one at that.
00075 //  srandom(time(0L));
00076 
00077   // user information...
00078   setOwner(i18n("Unknown Name"));
00079   setEmail(i18n("unknown@nowhere"));
00080 
00081 #if 0
00082   tmpStr = KOPrefs::instance()->mTimeZone;
00083 //  kdDebug(5800) << "Calendar::Calendar(): TimeZone: " << tmpStr << endl;
00084   int dstSetting = KOPrefs::instance()->mDaylightSavings;
00085   extern long int timezone;
00086   struct tm *now;
00087   time_t curtime;
00088   curtime = time(0);
00089   now = localtime(&curtime);
00090   int hourOff = - ((timezone / 60) / 60);
00091   if (now->tm_isdst)
00092     hourOff += 1;
00093   QString tzStr;
00094   tzStr.sprintf("%.2d%.2d",
00095                 hourOff, 
00096                 abs((timezone / 60) % 60));
00097 
00098   // if no time zone was in the config file, write what we just discovered.
00099   if (tmpStr.isEmpty()) {
00100 //    KOPrefs::instance()->mTimeZone = tzStr;
00101   } else {
00102     tzStr = tmpStr;
00103   }
00104   
00105   // if daylight savings has changed since last load time, we need
00106   // to rewrite these settings to the config file.
00107   if ((now->tm_isdst && !dstSetting) ||
00108       (!now->tm_isdst && dstSetting)) {
00109     KOPrefs::instance()->mTimeZone = tzStr;
00110     KOPrefs::instance()->mDaylightSavings = now->tm_isdst;
00111   }
00112   
00113   setTimeZone(tzStr);
00114 #endif
00115 
00116 //  KOPrefs::instance()->writeConfig();
00117 }
00118 
00119 Calendar::~Calendar() 
00120 {
00121   delete mDefaultFilter;
00122 }
00123 
00124 const QString &Calendar::getOwner() const
00125 {
00126   return mOwner;
00127 }
00128 
00129 void Calendar::setOwner(const QString &os)
00130 {
00131   int i;
00132   mOwner = os;
00133   i = mOwner.find(',');
00134   if (i != -1)
00135     mOwner = mOwner.left(i);
00136 
00137   setModified( true );
00138 }
00139 
00140 void Calendar::setTimeZone(const QString & tz)
00141 {
00142   bool neg = FALSE;
00143   int hours, minutes;
00144   QString tmpStr(tz);
00145 
00146   if (tmpStr.left(1) == "-")
00147     neg = TRUE;
00148   if (tmpStr.left(1) == "-" || tmpStr.left(1) == "+")
00149     tmpStr.remove(0, 1);
00150   hours = tmpStr.left(2).toInt();
00151   if (tmpStr.length() > 2) 
00152     minutes = tmpStr.right(2).toInt();
00153   else
00154     minutes = 0;
00155   mTimeZone = (60*hours+minutes);
00156   if (neg)
00157     mTimeZone = -mTimeZone;
00158   mLocalTime = false;
00159 
00160   setModified( true );
00161 }
00162 
00163 QString Calendar::getTimeZoneStr() const 
00164 {
00165   if (mLocalTime)
00166     return QString();
00167   QString tmpStr;
00168   int hours = abs(mTimeZone / 60);
00169   int minutes = abs(mTimeZone % 60);
00170   bool neg = mTimeZone < 0;
00171 
00172   tmpStr.sprintf("%c%.2d%.2d",
00173                  (neg ? '-' : '+'),
00174                  hours, minutes);
00175   return tmpStr;
00176 }
00177 
00178 void Calendar::setTimeZone(int tz)
00179 {
00180   mTimeZone = tz;
00181   mLocalTime = false;
00182 
00183   setModified( true );
00184 }
00185 
00186 int Calendar::getTimeZone() const
00187 {
00188   return mTimeZone;
00189 }
00190 
00191 void Calendar::setTimeZoneId(const QString &id)
00192 {
00193   mTimeZoneId = id;
00194   mLocalTime = false;
00195 
00196   setModified( true );
00197 }
00198 
00199 QString Calendar::timeZoneId() const
00200 {
00201   return mTimeZoneId;
00202 }
00203 
00204 void Calendar::setLocalTime()
00205 {
00206   mLocalTime = true;
00207   mTimeZone = 0;
00208   mTimeZoneId = "";
00209 
00210   setModified( true );
00211 }
00212 
00213 bool Calendar::isLocalTime() const
00214 {
00215   return mLocalTime;
00216 }
00217 
00218 const QString &Calendar::getEmail()
00219 {
00220   return mOwnerEmail;
00221 }
00222 
00223 void Calendar::setEmail(const QString &e)
00224 {
00225   mOwnerEmail = e;
00226 
00227   setModified( true );
00228 }
00229 
00230 void Calendar::setFilter(CalFilter *filter)
00231 {
00232   mFilter = filter;
00233 }
00234 
00235 CalFilter *Calendar::filter()
00236 {
00237   return mFilter;
00238 }
00239 
00240 QPtrList<Event> Calendar::events( const QDate &date, bool sorted )
00241 {
00242   QPtrList<Event> el = rawEventsForDate(date,sorted);
00243   mFilter->apply(&el);
00244   return el;
00245 }
00246 
00247 QPtrList<Event> Calendar::events( const QDateTime &qdt )
00248 {
00249   QPtrList<Event> el = rawEventsForDate(qdt);
00250   mFilter->apply(&el);
00251   return el;
00252 }
00253 
00254 QPtrList<Event> Calendar::events( const QDate &start, const QDate &end,
00255                                   bool inclusive)
00256 {
00257   QPtrList<Event> el = rawEvents(start,end,inclusive);
00258   mFilter->apply(&el);
00259   return el;
00260 }
00261 
00262 QPtrList<Event> Calendar::events()
00263 {
00264   QPtrList<Event> el = rawEvents();
00265   mFilter->apply(&el);
00266   return el;
00267 }
00268 
00269 
00270 void Calendar::addIncidence(Incidence *i)
00271 {
00272   AddIncidenceVisitor v(this);
00273 
00274   i->accept(v);
00275 }
00276 
00277 QPtrList<Todo> Calendar::todos()
00278 {
00279   QPtrList<Todo> tl = rawTodos();
00280   mFilter->apply( &tl );
00281   return tl;
00282 }
00283 
00284 void Calendar::registerObserver( Observer *observer )
00285 {
00286   mObserver = observer;
00287   mNewObserver = true;
00288 }
00289 
00290 void Calendar::setModified( bool modified )
00291 {
00292   if ( modified != mModified || mNewObserver ) {
00293     mNewObserver = false;
00294     if ( mObserver ) mObserver->calendarModified( modified, this );
00295     mModified = modified;
00296   }
00297 }
00298 
00299 void Calendar::setLoadedProductId( const QString &id )
00300 {
00301   mLoadedProductId = id;
00302 }
00303 
00304 QString Calendar::loadedProductId()
00305 {
00306   return mLoadedProductId;
00307 }
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:26 2004 by doxygen 1.3.5 written by Dimitri van Heesch, © 1997-2001