kmail

callback.cpp

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 ) const
00061 {
00062   kdDebug(5006) << "Mailing message:\n" << iCal << endl;
00063   KMMessage *msg = new KMMessage;
00064   msg->initHeader();
00065   if ( GlobalSettings::self()->exchangeCompatibleInvitations() ) {
00066     if ( status == QString("cancel") ) msg->setSubject( QString("Declined: %1").arg(subject).replace("Answer: ","") );
00067     else if ( status == QString("tentative") ) msg->setSubject(QString("Tentative: %1").arg(subject).replace("Answer: ","") );
00068     else if ( status == QString("accepted") ) msg->setSubject( QString("Accepted: %1").arg(subject).replace("Answer: ","") );
00069     else msg->setSubject( subject );
00070   } else {
00071     msg->setSubject( subject );
00072   }
00073   msg->setTo( to );
00074   msg->setFrom( receiver() );
00075   /* We want the triggering mail to be moved to the trash once this one
00076    * has been sent successfully. Set a link header which accomplishes that. */
00077   msg->link( mMsg, KMMsgStatusDeleted );
00078 
00079   // Outlook will only understand the reply if the From: header is the
00080   // same as the To: header of the invitation message.
00081   KConfigGroup options( KMKernel::config(), "Groupware" );
00082   if( !options.readBoolEntry( "LegacyMangleFromToHeaders", true ) )
00083   {
00084     // Try and match the receiver with an identity
00085     const KPIM::Identity& identity =
00086       kmkernel->identityManager()->identityForAddress( receiver() );
00087     if( identity != KPIM::Identity::null() ) {
00088       // Identity found. Use this
00089       msg->setFrom( identity.fullEmailAddr() );
00090       msg->setHeaderField("X-KMail-Identity", QString::number( identity.uoid() ));
00091     }
00092     // Remove BCC from identity on ical invitations (https://intevation.de/roundup/kolab/issue474)
00093     msg->setBcc( "" );
00094   }
00095 
00096   KMail::Composer * cWin = KMail::makeComposer();
00097   cWin->setMsg( msg, false /* mayAutoSign */ );
00098   // cWin->setCharset( "", true );
00099   cWin->slotWordWrapToggled( false );
00100   cWin->setSigningAndEncryptionDisabled( true );
00101 
00102   if( GlobalSettings::self()->exchangeCompatibleInvitations() ) {
00103     // For Exchange, send ical as attachment, with proper
00104     // parameters
00105     msg->setSubject( status );
00106     msg->setCharset( "utf-8" );
00107     KMMessagePart *msgPart = new KMMessagePart;
00108     msgPart->setName( "cal.ics" );
00109     // msgPart->setCteStr( attachCte ); // "base64" ?
00110     msgPart->setBodyEncoded( iCal.utf8() );
00111     msgPart->setTypeStr( "text" );
00112     msgPart->setSubtypeStr( "calendar" );
00113     msgPart->setParameter( "method", "reply" );
00114     cWin->addAttach( msgPart );
00115   } else {
00116     msg->setHeaderField( "Content-Type",
00117                          "text/calendar; method=reply; charset=\"utf-8\"" );
00118     msg->setBody( iCal.utf8() );
00119   }
00120 
00121   if ( options.readBoolEntry( "AutomaticSending", true ) ) {
00122     cWin->setAutoDeleteWindow(  true );
00123     cWin->slotSendNow();
00124   } else {
00125     cWin->show();
00126   }
00127 
00128   return true;
00129 }
00130 
00131 QString Callback::receiver() const
00132 {
00133   if ( mReceiverSet )
00134     // Already figured this out
00135     return mReceiver;
00136 
00137   mReceiverSet = true;
00138 
00139   QStringList addrs = KPIM::splitEmailAddrList( mMsg->to() );
00140   int found = 0;
00141   for( QStringList::Iterator it = addrs.begin(); it != addrs.end(); ++it ) {
00142     if( kmkernel->identityManager()->identityForAddress( *it ) !=
00143         KPIM::Identity::null() ) {
00144       // Ok, this could be us
00145       ++found;
00146       mReceiver = *it;
00147     }
00148   }
00149   QStringList ccaddrs = KPIM::splitEmailAddrList( mMsg->cc() );
00150   for( QStringList::Iterator it = ccaddrs.begin(); it != ccaddrs.end(); ++it ) {
00151     if( kmkernel->identityManager()->identityForAddress( *it ) !=
00152         KPIM::Identity::null() ) {
00153       // Ok, this could be us
00154       ++found;
00155       mReceiver = *it;
00156     }
00157   }
00158   if( found != 1 ) {
00159     bool ok;
00160     QString selectMessage;
00161     if (found == 0) {
00162       selectMessage = i18n("<qt>None of your identities match the "
00163           "receiver of this message,<br>please "
00164           "choose which of the following addresses "
00165           "is yours, if any:");
00166     } else {
00167       selectMessage = i18n("<qt>Several of your identities match the "
00168           "receiver of this message,<br>please "
00169           "choose which of the following addresses "
00170           "is yours:");
00171     }
00172 
00173     mReceiver =
00174       KInputDialog::getItem( i18n( "Select Address" ),
00175           selectMessage,
00176           addrs+ccaddrs, 0, FALSE, &ok, kmkernel->mainWin() );
00177     if( !ok )
00178       mReceiver = QString::null;
00179   }
00180 
00181   return mReceiver;
00182 }
00183 
00184 void Callback::closeIfSecondaryWindow() const
00185 {
00186   KMail::SecondaryWindow *window = dynamic_cast<KMail::SecondaryWindow*>( mReaderWin->mainWindow() );
00187   if ( window )
00188     window->close();
00189 }
KDE Home | KDE Accessibility Home | Description of Access Keys