kmail

callback.cpp

Go to the documentation of this file.
00001 /*  -*- c++ -*-
00002     callback.cpp
00003 
00004     This file is used by KMail's plugin interface.
00005     Copyright (c) 2004 Bo Thorsen <bo@sonofthor.dk>
00006 
00007     KMail is free software; you can redistribute it and/or modify it
00008     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     KMail is distributed in the hope that it will be useful, but
00013     WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00020 
00021     In addition, as a special exception, the copyright holders give
00022     permission to link the code of this program with any edition of
00023     the Qt library by Trolltech AS, Norway (or with modified versions
00024     of Qt that use the same license as Qt), and distribute linked
00025     combinations including the two.  You must obey the GNU General
00026     Public License in all respects for all of the code used other than
00027     Qt.  If you modify this file, you may extend this exception to
00028     your version of the file, but you are not obligated to do so.  If
00029     you do not wish to do so, delete this exception statement from
00030     your version.
00031 */
00032 
00033 #include "callback.h"
00034 #include "kmkernel.h"
00035 #include "kmmessage.h"
00036 #include "kmmsgpart.h"
00037 #include <libemailfunctions/email.h>
00038 #include <libkpimidentities/identity.h>
00039 #include <libkpimidentities/identitymanager.h>
00040 #include "kmmainwin.h"
00041 #include "composer.h"
00042 #include "kmreaderwin.h"
00043 #include "secondarywindow.h"
00044 
00045 #include <mimelib/enum.h>
00046 
00047 #include <kinputdialog.h>
00048 #include <klocale.h>
00049 #include <kdebug.h>
00050 
00051 using namespace KMail;
00052 
00053 
00054 Callback::Callback( KMMessage* msg, KMReaderWin* readerWin )
00055   : mMsg( msg ), mReaderWin( readerWin ), mReceiverSet( false )
00056 {
00057 }
00058 
00059 bool Callback::mailICal( const QString& to, const QString &iCal,
00060                          const QString& subject, const QString &status,
00061                          bool delMessage ) const
00062 {
00063   kdDebug(5006) << "Mailing message:\n" << iCal << endl;
00064 
00065   KMMessage *msg = new KMMessage;
00066   msg->initHeader();
00067   msg->setSubject( subject );
00068   if ( GlobalSettings::self()->exchangeCompatibleInvitations() ) {
00069     if ( status == QString("cancel") )
00070       msg->setSubject( QString("Declined: %1").arg(subject).replace("Answer: ","") );
00071     else if ( status == QString("tentative") )
00072       msg->setSubject(QString("Tentative: %1").arg(subject).replace("Answer: ","") );
00073     else if ( status == QString("accepted") )
00074       msg->setSubject( QString("Accepted: %1").arg(subject).replace("Answer: ","") );
00075     else if ( status == QString("delegated") )
00076       msg->setSubject( QString("Delegated: %1").arg(subject).replace("Answer: ","") );
00077   }
00078   msg->setTo( to );
00079   msg->setFrom( receiver() );
00080 
00081   if ( !GlobalSettings::self()->exchangeCompatibleInvitations() ) {
00082     msg->setHeaderField( "Content-Type",
00083                          "text/calendar; method=reply; charset=\"utf-8\"" );
00084     msg->setBody( iCal.utf8() );
00085   }
00086 
00087   if ( delMessage && deleteInvitationAfterReply() )
00088     /* We want the triggering mail to be moved to the trash once this one
00089     * has been sent successfully. Set a link header which accomplishes that. */
00090     msg->link( mMsg, KMMsgStatusDeleted );
00091 
00092   // Outlook will only understand the reply if the From: header is the
00093   // same as the To: header of the invitation message.
00094   KConfigGroup options( KMKernel::config(), "Groupware" );
00095   if( !options.readBoolEntry( "LegacyMangleFromToHeaders", true ) ) {
00096     // Try and match the receiver with an identity
00097     const KPIM::Identity& identity =
00098       kmkernel->identityManager()->identityForAddress( receiver() );
00099     if( identity != KPIM::Identity::null() ) {
00100       // Identity found. Use this
00101       msg->setFrom( identity.fullEmailAddr() );
00102       msg->setHeaderField("X-KMail-Identity", QString::number( identity.uoid() ));
00103     }
00104     // Remove BCC from identity on ical invitations (https://intevation.de/roundup/kolab/issue474)
00105     msg->setBcc( "" );
00106   }
00107 
00108   KMail::Composer * cWin = KMail::makeComposer();
00109   cWin->setMsg( msg, false /* mayAutoSign */ );
00110   // cWin->setCharset( "", true );
00111   cWin->disableWordWrap();
00112   cWin->setSigningAndEncryptionDisabled( true );
00113 
00114   if( GlobalSettings::self()->exchangeCompatibleInvitations() ) {
00115     // For Exchange, send ical as attachment, with proper
00116     // parameters
00117     msg->setSubject( status );
00118     msg->setCharset( "utf-8" );
00119     KMMessagePart *msgPart = new KMMessagePart;
00120     msgPart->setName( "cal.ics" );
00121     // msgPart->setCteStr( attachCte ); // "base64" ?
00122     msgPart->setBodyEncoded( iCal.utf8() );
00123     msgPart->setTypeStr( "text" );
00124     msgPart->setSubtypeStr( "calendar" );
00125     msgPart->setParameter( "method", "reply" );
00126     cWin->addAttach( msgPart );
00127   }
00128 
00129   if ( options.readBoolEntry( "AutomaticSending", true ) ) {
00130     cWin->setAutoDeleteWindow(  true );
00131     cWin->slotSendNow();
00132   } else {
00133     cWin->show();
00134   }
00135 
00136   return true;
00137 }
00138 
00139 QString Callback::receiver() const
00140 {
00141   if ( mReceiverSet )
00142     // Already figured this out
00143     return mReceiver;
00144 
00145   mReceiverSet = true;
00146 
00147   QStringList addrs = KPIM::splitEmailAddrList( mMsg->to() );
00148   int found = 0;
00149   for( QStringList::Iterator it = addrs.begin(); it != addrs.end(); ++it ) {
00150     if( kmkernel->identityManager()->identityForAddress( *it ) !=
00151         KPIM::Identity::null() ) {
00152       // Ok, this could be us
00153       ++found;
00154       mReceiver = *it;
00155     }
00156   }
00157   QStringList ccaddrs = KPIM::splitEmailAddrList( mMsg->cc() );
00158   for( QStringList::Iterator it = ccaddrs.begin(); it != ccaddrs.end(); ++it ) {
00159     if( kmkernel->identityManager()->identityForAddress( *it ) !=
00160         KPIM::Identity::null() ) {
00161       // Ok, this could be us
00162       ++found;
00163       mReceiver = *it;
00164     }
00165   }
00166   if( found != 1 ) {
00167     bool ok;
00168     QString selectMessage;
00169     if (found == 0) {
00170       selectMessage = i18n("<qt>None of your identities match the "
00171           "receiver of this message,<br>please "
00172           "choose which of the following addresses "
00173           "is yours, if any:");
00174       addrs += kmkernel->identityManager()->allEmails();
00175     } else {
00176       selectMessage = i18n("<qt>Several of your identities match the "
00177           "receiver of this message,<br>please "
00178           "choose which of the following addresses "
00179           "is yours:");
00180     }
00181 
00182     mReceiver =
00183       KInputDialog::getItem( i18n( "Select Address" ),
00184           selectMessage,
00185           addrs+ccaddrs, 0, FALSE, &ok, kmkernel->mainWin() );
00186     if( !ok )
00187       mReceiver = QString::null;
00188   }
00189 
00190   return mReceiver;
00191 }
00192 
00193 void Callback::closeIfSecondaryWindow() const
00194 {
00195   KMail::SecondaryWindow *window = dynamic_cast<KMail::SecondaryWindow*>( mReaderWin->mainWindow() );
00196   if ( window )
00197     window->close();
00198 }
00199 
00200 bool Callback::askForComment( KCal::Attendee::PartStat status ) const
00201 {
00202     if ( ( status != KCal::Attendee::Accepted
00203             && GlobalSettings::self()->askForCommentWhenReactingToInvitation()
00204             == GlobalSettings:: EnumAskForCommentWhenReactingToInvitation::AskForAllButAcceptance )
00205         || GlobalSettings::self()->askForCommentWhenReactingToInvitation()
00206         == GlobalSettings:: EnumAskForCommentWhenReactingToInvitation::AlwaysAsk )
00207         return true;
00208     return false;
00209 }
00210 
00211 bool Callback::deleteInvitationAfterReply() const
00212 {
00213     return GlobalSettings::self()->deleteInvitationEmailsAfterSendingReply();
00214 }
00215 
00216 QString Callback::sender() const
00217 {
00218   return mMsg->from();
00219 }