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, 2005 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::mEventsHandled;
00042 QStringList               ADCalendarBase::mCalendarUrls;    // never delete or reorder anything in this list!
00043 
00044 ADCalendarBase::ADCalendarBase(const QString& url, const QCString& appname, Type type)
00045   : mUrlString(url),
00046     mAppName(appname),
00047     mActionType(type),
00048     mRcIndex(-1),
00049     mLoaded(false),
00050     mLoadedConnected(false),
00051     mUnregistered(false)
00052 {
00053   mUrlIndex = mCalendarUrls.findIndex(url);    // get unique index for this URL
00054   if (mUrlIndex < 0)
00055   {
00056     mUrlIndex = static_cast<int>(mCalendarUrls.count());
00057     mCalendarUrls.append(url);
00058   }
00059   if (mAppName == "korgac")
00060   {
00061     KConfig cfg( locate( "config", "korganizerrc" ) );
00062     cfg.setGroup( "Time & Date" );
00063     QString tz = cfg.readEntry( "TimeZoneId" );
00064     kdDebug(5900) << "ADCalendarBase(): tz: " << tz << endl;
00065     if( tz.isEmpty() ) {
00066       // Set a reasonable default timezone is none
00067       // was found in the config file
00068       // see koprefs.cpp in korganizer
00069       QString zone;
00070       char zonefilebuf[100];
00071       int len = readlink("/etc/localtime",zonefilebuf,100);
00072       if (len > 0 && len < 100) {
00073     zonefilebuf[len] = '\0';
00074     zone = zonefilebuf;
00075     zone = zone.mid(zone.find("zoneinfo/") + 9);
00076       } else {
00077     tzset();
00078     zone = tzname[0];
00079       }
00080       tz = zone;
00081     }
00082     setTimeZoneId( tz );
00083   }
00084 }
00085 
00086 /*
00087  * Load the calendar file.
00088  */
00089 bool ADCalendarBase::loadFile_()
00090 {
00091   if ( !mTempFileName.isNull() ) {
00092     // Don't try to load the file if already downloading it
00093     kdError(5900) << "ADCalendarBase::loadFile_(): already downloading another file\n";
00094     return false;
00095   }
00096   mLoaded = false;
00097   KURL url( mUrlString );
00098   if ( url.isLocalFile() ) {
00099     // It's a local file
00100     loadLocalFile( url.path() );
00101     emit loaded( this, mLoaded );
00102   } else {
00103     // It's a remote file. Download to a temporary file before loading it.
00104     KTempFile tempFile;
00105     mTempFileName = tempFile.name();
00106     KURL dest;
00107     dest.setPath( mTempFileName );
00108     KIO::FileCopyJob *job = KIO::file_copy( url, dest, -1, true );
00109     connect( job, SIGNAL( result( KIO::Job * ) ),
00110              SLOT( slotDownloadJobResult( KIO::Job * ) ) );
00111   }
00112   return true;
00113 }
00114 
00115 void ADCalendarBase::slotDownloadJobResult( KIO::Job *job )
00116 {
00117   if ( job->error() ) {
00118     KURL url( mUrlString );
00119     kdDebug(5900) << "Error downloading calendar from " << url.prettyURL() << endl;
00120     job->showErrorDialog( 0 );
00121   } else {
00122     kdDebug(5900) << "--- Downloaded to " << mTempFileName << endl;
00123     loadLocalFile( mTempFileName );
00124   }
00125   unlink( QFile::encodeName( mTempFileName ) );
00126   mTempFileName = QString::null;
00127   emit loaded( this, mLoaded );
00128 }
00129 
00130 void ADCalendarBase::loadLocalFile( const QString& filename )
00131 {
00132   mLoaded = load( filename );
00133   if (!mLoaded)
00134     kdDebug(5900) << "ADCalendarBase::loadLocalFile(): Error loading calendar file '" << filename << "'\n";
00135   else
00136   {
00137     // Remove all now non-existent events from the handled list
00138     for (EventsMap::Iterator it = mEventsHandled.begin();  it != mEventsHandled.end();  )
00139     {
00140       if (it.key().calendarIndex == mUrlIndex  &&  !event(it.key().eventID))
00141       {
00142         // Event belonged to this calendar, but can't find it any more
00143         EventsMap::Iterator i = it;
00144         ++it;                      // prevent iterator becoming invalid with remove()
00145         mEventsHandled.remove(i);
00146       }
00147       else
00148         ++it;
00149     }
00150   }
00151 }
00152 
00153 bool ADCalendarBase::setLoadedConnected()
00154 {
00155   if (mLoadedConnected)
00156     return true;
00157   mLoadedConnected = true;
00158   return false;
00159 }
00160 
00161 void ADCalendarBase::dump() const
00162 {
00163   kdDebug(5900) << "  <calendar>" << endl;
00164   kdDebug(5900) << "    <url>" << urlString() << "</url>" << endl;
00165   kdDebug(5900) << "    <appname>" << appName() << "</appname>" << endl;
00166   if ( loaded() ) kdDebug(5900) << "    <loaded/>" << endl;
00167   kdDebug(5900) << "    <actiontype>" << int(actionType()) << "</actiontype>" << endl;
00168   if (enabled() ) kdDebug(5900) << "    <enabled/>" << endl;
00169   else kdDebug(5900) << "    <disabled/>" << endl;
00170   if (available()) kdDebug(5900) << "    <available/>" << endl;
00171   kdDebug(5900) << "  </calendar>" << endl;
00172 }
KDE Logo
This file is part of the documentation for kalarmd Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Jan 31 15:53:42 2007 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003