korganizer

mailscheduler.cpp

00001 /*
00002     This file is part of KOrganizer.
00003 
00004     Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program 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
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019 
00020     As a special exception, permission is given to link this program
00021     with any edition of Qt, and distribute the resulting executable,
00022     without including the source code for Qt in the source distribution.
00023 */
00024 
00025 #include <tqdir.h>
00026 #include <tqfile.h>
00027 #include <tqregexp.h>
00028 
00029 #include <klocale.h>
00030 #include <kstandarddirs.h>
00031 #include <kdebug.h>
00032 
00033 #include <libkcal/calendar.h>
00034 #include <libkcal/event.h>
00035 #include <libkcal/icalformat.h>
00036 
00037 #include "komailclient.h"
00038 #include "incidencechanger.h"
00039 
00040 #include "mailscheduler.h"
00041 
00042 
00043 using namespace KCal;
00044 
00045 MailScheduler::MailScheduler( Calendar *calendar )
00046   : IMIPScheduler( calendar )
00047 {
00048 }
00049 
00050 MailScheduler::~MailScheduler()
00051 {
00052 }
00053 
00054 bool MailScheduler::publish( IncidenceBase *incidence,
00055                              const TQString &recipients )
00056 {
00057   TQString messageText = mFormat->createScheduleMessage( incidence,
00058                                                         Scheduler::Publish );
00059   KOMailClient mailer;
00060   return mailer.mailTo( incidence, recipients, messageText );
00061 }
00062 
00063 bool MailScheduler::performTransaction( IncidenceBase *incidence,
00064                                         Method method,
00065                                         const TQString &recipients )
00066 {
00067   TQString messageText = mFormat->createScheduleMessage( incidence, method );
00068 
00069   KOMailClient mailer;
00070   return mailer.mailTo( incidence, recipients, messageText );
00071 }
00072 
00073 bool MailScheduler::performTransaction( IncidenceBase *incidence,
00074                                         Method method )
00075 {
00076   TQString messageText = mFormat->createScheduleMessage( incidence, method );
00077 
00078   KOMailClient mailer;
00079   bool status;
00080   if ( method == Request ||
00081        method == Cancel ||
00082        method == Add ||
00083        method == Declinecounter ) {
00084     status = mailer.mailAttendees( incidence, messageText );
00085   } else {
00086     TQString subject;
00087     Incidence *inc = dynamic_cast<Incidence*>( incidence );
00088     if ( inc && method == Counter )
00089       subject = i18n( "Counter proposal: %1" ).arg( inc->summary() );
00090     status = mailer.mailOrganizer( incidence, messageText, subject );
00091   }
00092   return status;
00093 }
00094 
00095 TQPtrList<ScheduleMessage> MailScheduler::retrieveTransactions()
00096 {
00097   TQString incomingDirName = locateLocal( "data", "korganizer/income" );
00098   kdDebug(5850) << "MailScheduler::retrieveTransactions: dir: "
00099                 << incomingDirName << endl;
00100 
00101   TQPtrList<ScheduleMessage> messageList;
00102 
00103   TQDir incomingDir( incomingDirName );
00104   TQStringList incoming = incomingDir.entryList( TQDir::Files );
00105   TQStringList::ConstIterator it;
00106   for( it = incoming.begin(); it != incoming.end(); ++it ) {
00107     kdDebug(5850) << "-- File: " << (*it) << endl;
00108 
00109     TQFile f( incomingDirName + "/" + (*it) );
00110     bool inserted = false;
00111     TQMap<IncidenceBase*, TQString>::Iterator iter;
00112     for ( iter = mEventMap.begin(); iter != mEventMap.end(); ++iter ) {
00113       if ( iter.data() == incomingDirName + "/" + (*it) )
00114         inserted = true;
00115     }
00116     if ( !inserted ) {
00117       if ( !f.open( IO_ReadOnly ) ) {
00118         kdDebug(5850)
00119           << "MailScheduler::retrieveTransactions(): Can't open file'"
00120           << (*it) << "'" << endl;
00121       } else {
00122         TQTextStream t( &f );
00123         t.setEncoding( TQTextStream::Latin1 );
00124         TQString messageString = t.read();
00125         messageString.replace( TQRegExp( "\n[ \t]"), "" );
00126         messageString = TQString::fromUtf8( messageString.latin1() );
00127         ScheduleMessage *mess = mFormat->parseScheduleMessage( mCalendar,
00128                                                                messageString );
00129         if ( mess) {
00130           kdDebug(5850)
00131             << "MailScheduler::retrieveTransactions: got message '"
00132             << (*it) << "'" << endl;
00133           messageList.append( mess );
00134           mEventMap[ mess->event() ] = incomingDirName + "/" + (*it);
00135         } else {
00136           TQString errorMessage;
00137           if ( mFormat->exception() ) {
00138             errorMessage = mFormat->exception()->message();
00139           }
00140           kdDebug(5850)
00141             << "MailScheduler::retrieveTransactions() Error parsing message: "
00142             << errorMessage << endl;
00143         }
00144         f.close();
00145       }
00146     }
00147   }
00148   return messageList;
00149 }
00150 
00151 bool MailScheduler::deleteTransaction( IncidenceBase *incidence )
00152 {
00153   bool status;
00154   TQFile f( mEventMap[incidence] );
00155   mEventMap.remove( incidence );
00156   if ( !f.exists() ) {
00157     status = false;
00158   } else {
00159     status = f.remove();
00160   }
00161   return status;
00162 }
00163 
00164 TQString MailScheduler::freeBusyDir()
00165 {
00166   return locateLocal( "data", "korganizer/freebusy" );
00167 }
00168 
00169 bool MailScheduler::acceptCounterProposal( Incidence *incidence )
00170 {
00171   if ( !incidence )
00172     return false;
00173 
00174   Incidence *exInc = mCalendar->incidence( incidence->uid() );
00175   if ( !exInc )
00176     exInc = mCalendar->incidenceFromSchedulingID( incidence->uid() );
00177   incidence->setRevision( incidence->revision() + 1 );
00178   if ( exInc ) {
00179     incidence->setRevision( QMAX( incidence->revision(), exInc->revision() + 1 ) );
00180     // some stuff we don't want to change, just to be safe
00181     incidence->setSchedulingID( exInc->schedulingID() );
00182     incidence->setUid( exInc->uid() );
00183 
00184     mCalendar->beginChange( exInc );
00185     IncidenceChanger::assignIncidence( exInc, incidence );
00186     exInc->updated();
00187     mCalendar->endChange( exInc );
00188   } else {
00189     mCalendar->addIncidence( incidence );
00190   }
00191   return true;
00192 }