libkcal

resourcecalendar.cpp

Go to the documentation of this file.
00001 /*
00002     This file is part of libkcal.
00003 
00004     Copyright (c) 1998 Preston Brown <pbrown@kde.org>
00005     Copyright (c) 2001-2004 Cornelius Schumacher <schumacher@kde.org>
00006     Copyright (c) 2002 Jan-Pascal van Best <janpascal@vanbest.org>
00007     Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
00008 
00009     This library is free software; you can redistribute it and/or
00010     modify it under the terms of the GNU Library General Public
00011     License as published by the Free Software Foundation; either
00012     version 2 of the License, or (at your option) any later version.
00013 
00014     This library is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017     Library General Public License for more details.
00018 
00019     You should have received a copy of the GNU Library General Public License
00020     along with this library; see the file COPYING.LIB.  If not, write to
00021     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00022     Boston, MA 02110-1301, USA.
00023 */
00024 
00025 #include <kconfig.h>
00026 #include <kdebug.h>
00027 #include <klocale.h>
00028 
00029 #include "calendar.h"
00030 
00031 #include "resourcecalendar.h"
00032 
00033 using namespace KCal;
00034 
00035 ResourceCalendar::ResourceCalendar( const KConfig *config )
00036     : KRES::Resource( config ),mResolveConflict( false )
00037 {
00038 }
00039 
00040 ResourceCalendar::~ResourceCalendar()
00041 {
00042 }
00043 
00044 void ResourceCalendar::setResolveConflict( bool b)
00045 {
00046  mResolveConflict = b;
00047 }
00048 QString ResourceCalendar::infoText() const
00049 {
00050   QString txt;
00051 
00052   txt += "<b>" + resourceName() + "</b>";
00053   txt += "<br>";
00054 
00055   KRES::Factory *factory = KRES::Factory::self( "calendar" );
00056   QString t = factory->typeName( type() );
00057   txt += i18n("Type: %1").arg( t );
00058 
00059   addInfoText( txt );
00060 
00061   return txt;
00062 }
00063 
00064 void ResourceCalendar::writeConfig( KConfig* config )
00065 {
00066 //  kdDebug(5800) << "ResourceCalendar::writeConfig()" << endl;
00067 
00068   KRES::Resource::writeConfig( config );
00069 }
00070 
00071 Incidence *ResourceCalendar::incidence( const QString &uid )
00072 {
00073   Incidence *i = event( uid );
00074   if ( i ) return i;
00075   i = todo( uid );
00076   if ( i ) return i;
00077   i = journal( uid );
00078   return i;
00079 }
00080 
00081 bool ResourceCalendar::addIncidence( Incidence *incidence )
00082 {
00083   Incidence::AddVisitor<ResourceCalendar> v( this );
00084   return incidence->accept( v );
00085 }
00086 
00087 bool ResourceCalendar::deleteIncidence( Incidence *incidence )
00088 {
00089   Incidence::DeleteVisitor<ResourceCalendar> v( this );
00090   return incidence->accept( v );
00091 }
00092 
00093 Incidence::List ResourceCalendar::rawIncidences()
00094 {
00095   return Calendar::mergeIncidenceList( rawEvents(), rawTodos(), rawJournals() );
00096 }
00097 
00098 void ResourceCalendar::setSubresourceActive( const QString &, bool )
00099 {
00100 }
00101 
00102 bool ResourceCalendar::addSubresource( const QString &, const QString & )
00103 {
00104   return true;
00105 }
00106 
00107 bool ResourceCalendar::removeSubresource( const QString & )
00108 {
00109   return true;
00110 }
00111 
00112 bool ResourceCalendar::load()
00113 {
00114   kdDebug(5800) << "Loading resource " + resourceName() << endl;
00115 
00116   mReceivedLoadError = false;
00117 
00118   bool success = true;
00119   if ( !isOpen() )
00120     success = open();
00121   if ( success )
00122     success = doLoad();
00123 
00124   if ( !success && !mReceivedLoadError )
00125     loadError();
00126 
00127   // If the resource is read-only, we need to set its incidences to read-only,
00128   // too. This can't be done at a lower-level, since the read-only setting
00129   // happens at this level
00130   if ( readOnly() ) {
00131     Incidence::List incidences( rawIncidences() );
00132     Incidence::List::Iterator it;
00133     for ( it = incidences.begin(); it != incidences.end(); ++it ) {
00134       (*it)->setReadOnly( true );
00135     }
00136   }
00137 
00138   kdDebug(5800) << "Done loading resource " + resourceName() << endl;
00139 
00140   return success;
00141 }
00142 
00143 void ResourceCalendar::loadError( const QString &err )
00144 {
00145   kdDebug(5800) << "Error loading resource: " << err << endl;
00146 
00147   mReceivedLoadError = true;
00148 
00149   QString msg = i18n("Error while loading %1.\n") .arg( resourceName() );
00150   if ( !err.isEmpty() ) {
00151     msg += err;
00152   }
00153   emit resourceLoadError( this, msg );
00154 }
00155 
00156 bool ResourceCalendar::save( Incidence *incidence )
00157 {
00158   if ( !readOnly() ) {
00159     kdDebug(5800) << "Save resource " + resourceName() << endl;
00160 
00161     mReceivedSaveError = false;
00162 
00163     if ( !isOpen() ) return true;
00164     bool success = incidence ? doSave(incidence) : doSave();
00165     if ( !success && !mReceivedSaveError ) saveError();
00166 
00167     return success;
00168   } else {
00169     // Read-only, just don't save...
00170     kdDebug(5800) << "Don't save read-only resource " + resourceName() << endl;
00171     return true;
00172   }
00173 }
00174 
00175 bool ResourceCalendar::doSave( Incidence * )
00176 {
00177   return doSave();
00178 }
00179 
00180 void ResourceCalendar::saveError( const QString &err )
00181 {
00182   kdDebug(5800) << "Error saving resource: " << err << endl;
00183 
00184   mReceivedSaveError = true;
00185 
00186   QString msg = i18n("Error while saving %1.\n") .arg( resourceName() );
00187   if ( !err.isEmpty() ) {
00188     msg += err;
00189   }
00190   emit resourceSaveError( this, msg );
00191 }
00192 
00193 bool ResourceCalendar::setValue( const QString &key, const QString &value )
00194 {
00195   return false;
00196 }
00197 
00198 QString ResourceCalendar::subresourceType( const QString &resource )
00199 {
00200   Q_UNUSED( resource );
00201   return QString();
00202 }
00203 
00204 
00205 #include "resourcecalendar.moc"