kalarmd Library API Documentation

adcalendarbase.cpp

00001 /*
00002     Calendar access for KDE Alarm Daemon.
00003 
00004     This file is part of the KDE alarm daemon.
00005     Copyright (c) 2001 David Jarvie <software@astrojar.org.uk>
00006 
00007     This program is free software; you can redistribute it and/or modify
00008     it under the terms of the GNU General Public License as published by
00009     the Free Software Foundation; either version 2 of the License, or
00010     (at your option) any later version.
00011 
00012     This program 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
00015     GNU General Public License for more details.
00016 
00017     You should have received a copy of the GNU General Public License
00018     along with this program; if not, write to the Free Software
00019     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00020 
00021     As a special exception, permission is given to link this program
00022     with any edition of Qt, and distribute the resulting executable,
00023     without including the source code for Qt in the source distribution.
00024 */
00025 
00026 #include <unistd.h>
00027 #include <time.h>
00028 
00029 #include <qfile.h>
00030 
00031 #include <kstandarddirs.h>
00032 #include <ksimpleconfig.h>
00033 #include <ktempfile.h>
00034 #include <kio/job.h>
00035 #include <kio/jobclasses.h>
00036 #include <kdebug.h>
00037 
00038 #include "adcalendarbase.h"
00039 #include "adcalendarbase.moc"
00040 
00041 ADCalendarBase::EventsMap ADCalendarBase::eventsHandled_;
00042 
00043 ADCalendarBase::ADCalendarBase(const QString& url, const QCString& appname, Type type)
00044   : mUrlString(url),
00045     mAppName(appname),
00046     mActionType(type),
00047     mRcIndex(-1),
00048     mLoaded(false),
00049     mLoadedConnected(false),
00050     mUnregistered(false)
00051 {
00052   if (mAppName == "korgac")
00053   {
00054     KConfig cfg( locate( "config", "korganizerrc" ) );
00055     cfg.setGroup( "Time & Date" );
00056     QString tz = cfg.readEntry( "TimeZoneId" );
00057     kdDebug(5900) << "ADCalendarBase(): tz: " << tz << endl;
00058     if( tz.isEmpty() ) {
00059       // Set a reasonable default timezone is none
00060       // was found in the config file
00061       // see koprefs.cpp in korganizer
00062       QString zone;
00063       char zonefilebuf[100];
00064       int len = readlink("/etc/localtime",zonefilebuf,100);
00065       if (len > 0 && len < 100) {
00066     zonefilebuf[len] = '\0';
00067     zone = zonefilebuf;
00068     zone = zone.mid(zone.find("zoneinfo/") + 9);
00069       } else {
00070     tzset();
00071     zone = tzname[0];
00072       }
00073       tz = zone;
00074     }
00075     setTimeZoneId( tz );
00076   }
00077 }
00078 
00079 /*
00080  * Load the calendar file.
00081  */
00082 bool ADCalendarBase::loadFile_()
00083 {
00084   if ( !mTempFileName.isNull() ) {
00085     // Don't try to load the file if already downloading it
00086     kdError(5900) << "ADCalendarBase::loadFile_(): already downloading another file\n";
00087     return false;
00088   }
00089   mLoaded = false;
00090   KURL url( mUrlString );
00091   if ( url.isLocalFile() ) {
00092     // It's a local file
00093     loadLocalFile( url.path() );
00094     emit loaded( this, mLoaded );
00095   } else {
00096     // It's a remote file. Download to a temporary file before loading it.
00097     KTempFile tempFile;
00098     mTempFileName = tempFile.name();
00099     KURL dest;
00100     dest.setPath( mTempFileName );
00101     KIO::FileCopyJob *job = KIO::file_copy( url, dest, -1, true );
00102     connect( job, SIGNAL( result( KIO::Job * ) ),
00103              SLOT( slotDownloadJobResult( KIO::Job * ) ) );
00104   }
00105   return true;
00106 }
00107 
00108 void ADCalendarBase::slotDownloadJobResult( KIO::Job *job )
00109 {
00110   if ( job->error() ) {
00111     KURL url( mUrlString );
00112     kdDebug(5900) << "Error downloading calendar from " << url.prettyURL() << endl;
00113     job->showErrorDialog( 0 );
00114   } else {
00115     kdDebug(5900) << "--- Downloaded to " << mTempFileName << endl;
00116     loadLocalFile( mTempFileName );
00117   }
00118   unlink( QFile::encodeName( mTempFileName ) );
00119   mTempFileName = QString::null;
00120   emit loaded( this, mLoaded );
00121 }
00122 
00123 void ADCalendarBase::loadLocalFile( const QString& filename )
00124 {
00125   mLoaded = load( filename );
00126   if (!mLoaded)
00127     kdDebug(5900) << "ADCalendarBase::loadLocalFile(): Error loading calendar file '" << filename << "'\n";
00128   else
00129   {
00130     // Remove all now non-existent events from the handled list
00131     for (EventsMap::Iterator it = eventsHandled_.begin();  it != eventsHandled_.end();  )
00132     {
00133       if (it.data().calendarURL == mUrlString  &&  !event(it.key()))
00134       {
00135         // Event belonged to this calendar, but can't find it any more
00136         EventsMap::Iterator i = it;
00137         ++it;                      // prevent iterator becoming invalid with remove()
00138         eventsHandled_.remove(i);
00139       }
00140       else
00141         ++it;
00142     }
00143   }
00144 }
00145 
00146 bool ADCalendarBase::setLoadedConnected()
00147 {
00148   if (mLoadedConnected)
00149     return true;
00150   mLoadedConnected = true;
00151   return false;
00152 }
00153 
00154 void ADCalendarBase::dump() const
00155 {
00156   kdDebug(5900) << "  <calendar>" << endl;
00157   kdDebug(5900) << "    <url>" << urlString() << "</url>" << endl;
00158   kdDebug(5900) << "    <appname>" << appName() << "</appname>" << endl;
00159   if ( loaded() ) kdDebug(5900) << "    <loaded/>" << endl;
00160   kdDebug(5900) << "    <actiontype>" << int(actionType()) << "</actiontype>" << endl;
00161   if (enabled() ) kdDebug(5900) << "    <enabled/>" << endl;
00162   else kdDebug(5900) << "    <disabled/>" << endl;
00163   if (available()) kdDebug(5900) << "    <available/>" << endl;
00164   kdDebug(5900) << "  </calendar>" << endl;
00165 }
KDE Logo
This file is part of the documentation for kalarmd Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat May 1 11:38:04 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003