korganizer

kogroupware.cpp

00001 /*
00002   This file is part of the Groupware/KOrganizer integration.
00003 
00004   Requires the Qt and KDE widget libraries, available at no cost at
00005   http://www.trolltech.com and http://www.kde.org respectively
00006 
00007   Copyright (c) 2002-2004 Klarälvdalens Datakonsult AB
00008         <info@klaralvdalens-datakonsult.se>
00009 
00010   This program is free software; you can redistribute it and/or modify
00011   it under the terms of the GNU General Public License as published by
00012   the Free Software Foundation; either version 2 of the License, or
00013   (at your option) any later version.
00014 
00015   This program is distributed in the hope that it will be useful,
00016   but WITHOUT ANY WARRANTY; without even the implied warranty of
00017   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00018   GNU General Public License for more details.
00019 
00020   You should have received a copy of the GNU General Public License
00021   along with this program; if not, write to the Free Software
00022   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00023   MA  02110-1301, USA.
00024 
00025   In addition, as a special exception, the copyright holders give
00026   permission to link the code of this program with any edition of
00027   the Qt library by Trolltech AS, Norway (or with modified versions
00028   of Qt that use the same license as Qt), and distribute linked
00029   combinations including the two.  You must obey the GNU General
00030   Public License in all respects for all of the code used other than
00031   Qt.  If you modify this file, you may extend this exception to
00032   your version of the file, but you are not obligated to do so.  If
00033   you do not wish to do so, delete this exception statement from
00034   your version.
00035 */
00036 
00037 #include "kogroupware.h"
00038 #include "freebusymanager.h"
00039 #include "calendarview.h"
00040 #include "mailscheduler.h"
00041 #include "koprefs.h"
00042 #include "koincidenceeditor.h"
00043 #include <libemailfunctions/email.h>
00044 #include <libkcal/attendee.h>
00045 #include <libkcal/journal.h>
00046 #include <libkcal/incidenceformatter.h>
00047 #include <kdebug.h>
00048 #include <kmessagebox.h>
00049 #include <kstandarddirs.h>
00050 #include <kdirwatch.h>
00051 #include <qfile.h>
00052 #include <qregexp.h>
00053 #include <qdir.h>
00054 #include <qtimer.h>
00055 
00056 FreeBusyManager *KOGroupware::mFreeBusyManager = 0;
00057 
00058 KOGroupware *KOGroupware::mInstance = 0;
00059 
00060 KOGroupware *KOGroupware::create( CalendarView *view,
00061                                   KCal::CalendarResources *calendar )
00062 {
00063   if( !mInstance )
00064     mInstance = new KOGroupware( view, calendar );
00065   return mInstance;
00066 }
00067 
00068 KOGroupware *KOGroupware::instance()
00069 {
00070   // Doesn't create, that is the task of create()
00071   Q_ASSERT( mInstance );
00072   return mInstance;
00073 }
00074 
00075 
00076 KOGroupware::KOGroupware( CalendarView* view, KCal::CalendarResources* cal )
00077    : QObject( 0, "kmgroupware_instance" ), mView( view ), mCalendar( cal ), mDoNotNotify( false )
00078 {
00079   // Set up the dir watch of the three incoming dirs
00080   KDirWatch* watcher = KDirWatch::self();
00081   watcher->addDir( locateLocal( "data", "korganizer/income.accepted/" ) );
00082   watcher->addDir( locateLocal( "data", "korganizer/income.tentative/" ) );
00083   watcher->addDir( locateLocal( "data", "korganizer/income.counter/" ) );
00084   watcher->addDir( locateLocal( "data", "korganizer/income.cancel/" ) );
00085   watcher->addDir( locateLocal( "data", "korganizer/income.reply/" ) );
00086   watcher->addDir( locateLocal( "data", "korganizer/income.delegated/" ) );
00087   connect( watcher, SIGNAL( dirty( const QString& ) ),
00088            this, SLOT( incomingDirChanged( const QString& ) ) );
00089   // Now set the ball rolling
00090   QTimer::singleShot( 0, this, SLOT(initialCheckForChanges()) );
00091 }
00092 
00093 void KOGroupware::initialCheckForChanges()
00094 {
00095   incomingDirChanged( locateLocal( "data", "korganizer/income.accepted/" ) );
00096   incomingDirChanged( locateLocal( "data", "korganizer/income.tentative/" ) );
00097   incomingDirChanged( locateLocal( "data", "korganizer/income.counter/" ) );
00098   incomingDirChanged( locateLocal( "data", "korganizer/income.cancel/" ) );
00099   incomingDirChanged( locateLocal( "data", "korganizer/income.reply/" ) );
00100   incomingDirChanged( locateLocal( "data", "korganizer/income.delegated/" ) );
00101 }
00102 
00103 void KOGroupware::slotViewNewIncidenceChanger( IncidenceChangerBase* changer )
00104 {
00105     // Call slot perhapsUploadFB if an incidence was added, changed or removed
00106     connect( changer, SIGNAL( incidenceAdded( Incidence* ) ),
00107              mFreeBusyManager, SLOT( slotPerhapsUploadFB() ) );
00108     connect( changer, SIGNAL( incidenceChanged( Incidence*, Incidence*, KOGlobals::WhatChanged ) ),
00109              mFreeBusyManager, SLOT( slotPerhapsUploadFB() ) );
00110     connect( changer, SIGNAL( incidenceDeleted( Incidence * ) ),
00111              mFreeBusyManager, SLOT( slotPerhapsUploadFB() ) );
00112 }
00113 
00114 FreeBusyManager *KOGroupware::freeBusyManager()
00115 {
00116   if ( !mFreeBusyManager ) {
00117     mFreeBusyManager = new FreeBusyManager( this, "freebusymanager" );
00118     mFreeBusyManager->setCalendar( mCalendar );
00119     connect( mCalendar, SIGNAL( calendarChanged() ),
00120              mFreeBusyManager, SLOT( slotPerhapsUploadFB() ) );
00121     connect( mView, SIGNAL( newIncidenceChanger( IncidenceChangerBase* ) ),
00122              this, SLOT( slotViewNewIncidenceChanger( IncidenceChangerBase* ) ) );
00123     slotViewNewIncidenceChanger( mView->incidenceChanger() );
00124   }
00125 
00126   return mFreeBusyManager;
00127 }
00128 
00129 void KOGroupware::incomingDirChanged( const QString& path )
00130 {
00131   const QString incomingDirName = locateLocal( "data","korganizer/" )
00132                                   + "income.";
00133   if ( !path.startsWith( incomingDirName ) ) {
00134     kdDebug(5850) << "incomingDirChanged: Wrong dir " << path << endl;
00135     return;
00136   }
00137   QString action = path.mid( incomingDirName.length() );
00138   while ( action.length() > 0 && action[ action.length()-1 ] == '/' )
00139     // Strip slashes at the end
00140     action.truncate( action.length()-1 );
00141 
00142   // Handle accepted invitations
00143   QDir dir( path );
00144   const QStringList files = dir.entryList( QDir::Files );
00145   if ( files.isEmpty() )
00146     // No more files here
00147     return;
00148 
00149   // Read the file and remove it
00150   QFile f( path + "/" + files[0] );
00151   if (!f.open(IO_ReadOnly)) {
00152     kdError(5850) << "Can't open file '" << files[0] << "'" << endl;
00153     return;
00154   }
00155   QTextStream t(&f);
00156   t.setEncoding( QTextStream::UnicodeUTF8 );
00157   QString receiver = KPIM::getFirstEmailAddress( t.readLine() );
00158   QString iCal = t.read();
00159 
00160   f.remove();
00161 
00162   ScheduleMessage *message = mFormat.parseScheduleMessage( mCalendar, iCal );
00163   if ( !message ) {
00164     QString errorMessage;
00165     if (mFormat.exception())
00166       errorMessage = i18n( "Error message: %1" ).arg( mFormat.exception()->message() );
00167     kdDebug(5850) << "MailScheduler::retrieveTransactions() Error parsing "
00168                   << errorMessage << endl;
00169     KMessageBox::detailedError( mView,
00170         i18n("Error while processing an invitation or update."),
00171         errorMessage );
00172     return;
00173   }
00174 
00175   KCal::Scheduler::Method method =
00176     static_cast<KCal::Scheduler::Method>( message->method() );
00177   KCal::ScheduleMessage::Status status = message->status();
00178   KCal::Incidence* incidence =
00179     dynamic_cast<KCal::Incidence*>( message->event() );
00180   if(!incidence) {
00181     delete message;
00182     return;
00183   }
00184   KCal::MailScheduler scheduler( mCalendar );
00185   if ( action.startsWith( "accepted" ) || action.startsWith( "tentative" )
00186        || action.startsWith( "delegated" ) || action.startsWith( "counter" ) ) {
00187     // Find myself and set my status. This can't be done in the scheduler,
00188     // since this does not know the choice I made in the KMail bpf
00189     KCal::Attendee::List attendees = incidence->attendees();
00190     KCal::Attendee::List::ConstIterator it;
00191     for ( it = attendees.begin(); it != attendees.end(); ++it ) {
00192       if( (*it)->email() == receiver ) {
00193         if ( action.startsWith( "accepted" ) )
00194           (*it)->setStatus( KCal::Attendee::Accepted );
00195         else if ( action.startsWith( "tentative" ) )
00196           (*it)->setStatus( KCal::Attendee::Tentative );
00197         else if ( KOPrefs::instance()->outlookCompatCounterProposals() && action.startsWith( "counter" ) )
00198           (*it)->setStatus( KCal::Attendee::Tentative );
00199         else if ( action.startsWith( "delegated" ) )
00200           (*it)->setStatus( KCal::Attendee::Delegated );
00201         break;
00202       }
00203     }
00204     if ( KOPrefs::instance()->outlookCompatCounterProposals() || !action.startsWith( "counter" ) )
00205       scheduler.acceptTransaction( incidence, method, status, receiver );
00206   } else if ( action.startsWith( "cancel" ) )
00207     // Delete the old incidence, if one is present
00208     scheduler.acceptTransaction( incidence, KCal::Scheduler::Cancel, status, receiver );
00209   else if ( action.startsWith( "reply" ) ) {
00210     if ( method != Scheduler::Counter ) {
00211       scheduler.acceptTransaction( incidence, method, status );
00212     } else {
00213       // accept counter proposal
00214       scheduler.acceptCounterProposal( incidence );
00215       // send update to all attendees
00216       sendICalMessage( mView, Scheduler::Request, incidence, KOGlobals::INCIDENCEEDITED, false );
00217     }
00218   } else
00219     kdError(5850) << "Unknown incoming action " << action << endl;
00220 
00221   if ( action.startsWith( "counter" ) ) {
00222     mView->editIncidence( incidence, true );
00223     KOIncidenceEditor *tmp = mView->editorDialog( incidence );
00224     tmp->selectInvitationCounterProposal( true );
00225   }
00226   mView->updateView();
00227 }
00228 
00229 class KOInvitationFormatterHelper : public InvitationFormatterHelper
00230 {
00231   public:
00232     virtual QString generateLinkURL( const QString &id ) { return "kmail:groupware_request_" + id; }
00233 };
00234 
00235 /* This function sends mails if necessary, and makes sure the user really
00236  * want to change his calendar.
00237  *
00238  * Return true means accept the changes
00239  * Return false means revert the changes
00240  */
00241 bool KOGroupware::sendICalMessage( QWidget* parent,
00242                                    KCal::Scheduler::Method method,
00243                                    Incidence* incidence,
00244                                    KOGlobals::HowChanged action,
00245                                    bool attendeeStatusChanged )
00246 {
00247   // If there are no attendees, don't bother
00248   if( incidence->attendees().isEmpty() )
00249     return true;
00250 
00251   bool isOrganizer = KOPrefs::instance()->thatIsMe( incidence->organizer().email() );
00252   int rc = 0;
00253   /*
00254    * There are two scenarios:
00255    * o "we" are the organizer, where "we" means any of the identities or mail
00256    *   addresses known to Kontact/PIM. If there are attendees, we need to mail
00257    *   them all, even if one or more of them are also "us". Otherwise there
00258    *   would be no way to invite a resource or our boss, other identities we
00259    *   also manage.
00260    * o "we: are not the organizer, which means we changed the completion status
00261    *   of a todo, or we changed our attendee status from, say, tentative to
00262    *   accepted. In both cases we only mail the organizer. All other changes
00263    *   bring us out of sync with the organizer, so we won't mail, if the user
00264    *   insists on applying them.
00265    */
00266 
00267   if ( isOrganizer ) {
00268     /* We are the organizer. If there is more than one attendee, or if there is
00269      * only one, and it's not the same as the organizer, ask the user to send
00270      * mail. */
00271     if ( incidence->attendees().count() > 1
00272         || incidence->attendees().first()->email() != incidence->organizer().email() ) {
00273 
00274       QString txt;
00275       switch( action ) {
00276       case KOGlobals::INCIDENCEEDITED:
00277         txt = i18n( "You changed the invitation \"%1\".\n"
00278                     "Do you want to email the attendees an update message?" ).
00279               arg( incidence->summary() );
00280         break;
00281       case KOGlobals::INCIDENCEDELETED:
00282         Q_ASSERT( incidence->type() == "Event" || incidence->type() == "Todo" );
00283         if ( incidence->type() == "Event" ) {
00284           txt = i18n( "You removed the invitation \"%1\".\n"
00285                       "Do you want to email the attendees that the event is canceled?" ).
00286                 arg( incidence->summary() );
00287         }
00288         else if ( incidence->type() == "Todo" ) {
00289           txt = i18n( "You removed the invitation \"%1\".\n"
00290                       "Do you want to email the attendees that the todo is canceled?" ).
00291                 arg( incidence->summary() );
00292         }
00293         break;
00294       case KOGlobals::INCIDENCEADDED:
00295         if ( incidence->type() == "Event" ) {
00296           txt = i18n( "The event \"%1\" includes other people.\n"
00297                       "Do you want to email the invitation to the attendees?" ).
00298                 arg( incidence->summary() );
00299         }
00300         else if ( incidence->type() == "Todo" ) {
00301           txt = i18n( "The todo \"%1\" includes other people.\n"
00302                       "Do you want to email the invitation to the attendees?" ).
00303                 arg( incidence->summary() );
00304         } else {
00305           QString type = incidence->type();
00306           if ( incidence->type() == "Event" )
00307             txt = i18n( "This event includes other people. "
00308                         "Should an email be sent to the attendees?" );
00309           else if ( incidence->type() == "Todo" )
00310             txt = i18n( "This todo includes other people. "
00311                         "Should an email be sent to the attendees?" );
00312           else
00313             txt = i18n( "This incidence includes other people. "
00314                         "Should an email be sent to the attendees?" );
00315         }
00316         break;
00317       default:
00318         kdError() << "Unsupported HowChanged action" << int( action ) << endl;
00319         break;
00320       }
00321 
00322       rc = KMessageBox::questionYesNoCancel(
00323              parent, txt, i18n( "Group Scheduling Email" ),
00324              KGuiItem( i18n( "Send Email" ) ), KGuiItem( i18n( "Do Not Send" ) ) );
00325     } else {
00326       return true;
00327     }
00328   } else if( incidence->type() == "Todo" ) {
00329     if( method == Scheduler::Request )
00330       // This is an update to be sent to the organizer
00331       method = Scheduler::Reply;
00332 
00333     // Ask if the user wants to tell the organizer about the current status
00334     QString txt = i18n( "Do you want to send a status update to the "
00335                         "organizer of this task?");
00336     rc = KMessageBox::questionYesNo( parent, txt, QString::null, i18n("Send Update"), i18n("Do Not Send") );
00337   } else if( incidence->type() == "Event" ) {
00338     QString txt;
00339     if ( attendeeStatusChanged && method == Scheduler::Request ) {
00340       txt = i18n( "Your status as an attendee of this event changed. "
00341                   "Do you want to send a status update to the event organizer?" );
00342       method = Scheduler::Reply;
00343       rc = KMessageBox::questionYesNo( parent, txt, QString::null, i18n("Send Update"), i18n("Do Not Send") );
00344     } else {
00345       if( action == KOGlobals::INCIDENCEDELETED ) {
00346         const QStringList myEmails = KOPrefs::instance()->allEmails();
00347         bool askConfirmation = false;
00348         for ( QStringList::ConstIterator it = myEmails.begin(); it != myEmails.end(); ++it ) {
00349           QString email = *it;
00350           Attendee *me = incidence->attendeeByMail(email);
00351           if (me && (me->status()==KCal::Attendee::Accepted || me->status()==KCal::Attendee::Delegated)) {
00352             askConfirmation = true;
00353             break;
00354           }
00355         }
00356 
00357         if ( !askConfirmation ) {
00358           return true;
00359         }
00360 
00361         txt = i18n( "You had previously accepted an invitation to this event. "
00362                     "Do you want to send an updated response to the organizer "
00363                     "declining the invitation?" );
00364         rc = KMessageBox::questionYesNo(
00365           parent, txt, i18n( "Group Scheduling Email" ),
00366           KGuiItem( i18n( "Send Update" ) ), KGuiItem( i18n( "Do Not Send" ) ) );
00367         setDoNotNotify( rc == KMessageBox::No );
00368       } else {
00369         txt = i18n( "You are not the organizer of this event. Editing it will "
00370                     "bring your calendar out of sync with the organizer's calendar. "
00371                     "Do you really want to edit it?" );
00372         rc = KMessageBox::warningYesNo( parent, txt );
00373         return ( rc == KMessageBox::Yes );
00374       }
00375     }
00376   } else {
00377     kdWarning(5850) << "Groupware messages for Journals are not implemented yet!" << endl;
00378     return true;
00379   }
00380 
00381   if ( rc == KMessageBox::Yes ) {
00382     // We will be sending out a message here. Now make sure there is
00383     // some summary
00384     if( incidence->summary().isEmpty() )
00385       incidence->setSummary( i18n("<No summary given>") );
00386 
00387     // Send the mail
00388     KCal::MailScheduler scheduler( mCalendar );
00389     scheduler.performTransaction( incidence, method );
00390 
00391     return true;
00392   } else if ( rc == KMessageBox::No ) {
00393     return true;
00394   } else {
00395     return false;
00396   }
00397 }
00398 
00399 void KOGroupware::sendCounterProposal(KCal::Calendar *calendar, KCal::Event * oldEvent, KCal::Event * newEvent) const
00400 {
00401   if ( !oldEvent || !newEvent || *oldEvent == *newEvent || !KOPrefs::instance()->mUseGroupwareCommunication )
00402     return;
00403   if ( KOPrefs::instance()->outlookCompatCounterProposals() ) {
00404     Incidence* tmp = oldEvent->clone();
00405     tmp->setSummary( i18n("Counter proposal: %1").arg( newEvent->summary() ) );
00406     tmp->setDescription( newEvent->description() );
00407     tmp->addComment( i18n("Proposed new meeting time: %1 - %2").arg( newEvent->dtStartStr(), newEvent->dtEndStr() ) );
00408     KCal::MailScheduler scheduler( calendar );
00409     scheduler.performTransaction( tmp, Scheduler::Reply );
00410     delete tmp;
00411   } else {
00412     KCal::MailScheduler scheduler( calendar );
00413     scheduler.performTransaction( newEvent, Scheduler::Counter );
00414   }
00415 }
00416 
00417 #include "kogroupware.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys