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 #include "transportmanager.h"
00045 
00046 #include <mimelib/enum.h>
00047 
00048 #include <kinputdialog.h>
00049 #include <klocale.h>
00050 #include <kdebug.h>
00051 
00052 using namespace KMail;
00053 
00054 
00055 Callback::Callback( KMMessage* msg, KMReaderWin* readerWin )
00056   : mMsg( msg ), mReaderWin( readerWin ), mReceiverSet( false )
00057 {
00058 }
00059 
00060 QString Callback::askForTransport( bool nullIdentity ) const
00061 {
00062   const QStringList transports = KMail::TransportManager::transportNames();
00063   if ( transports.size() == 1 )
00064     return transports.first();
00065 
00066   const QString defaultTransport = GlobalSettings::self()->defaultTransport();
00067   const int defaultIndex = QMAX( 0, transports.findIndex( defaultTransport ) );
00068 
00069   QString text;
00070   if ( nullIdentity )
00071     text = i18n( "<qt>The receiver of this invitation doesn't match any of your identities.<br>"
00072                  "Please select the transport which should be used to send your reply.</qt>" );
00073   else
00074     text = i18n( "<qt>The identity matching the receiver of this invitation doesn't have an "
00075                  "associated transport configured.<br>"
00076                  "Please select the transport which should be used to send your reply.</qt>");
00077   bool ok;
00078   const QString transport = KInputDialog::getItem( i18n( "Select Transport" ), text,
00079                                         transports, defaultIndex, FALSE, &ok, kmkernel->mainWin() );
00080   if ( !ok )
00081     return QString();
00082 
00083   return transport;
00084 }
00085 
00086 bool Callback::mailICal( const QString& to, const QString &iCal,
00087                          const QString& subject, const QString &status,
00088                          bool delMessage ) const
00089 {
00090   kdDebug(5006) << "Mailing message:\n" << iCal << endl;
00091 
00092   KMMessage *msg = new KMMessage;
00093   msg->initHeader();
00094   msg->setSubject( subject );
00095   if ( GlobalSettings::self()->exchangeCompatibleInvitations() ) {
00096     if ( status == QString("cancel") )
00097       msg->setSubject( i18n( "Declined: %1" ).arg(subject).replace("Answer: ","") );
00098     else if ( status == QString("tentative") )
00099       msg->setSubject( i18n( "Tentative: %1" ).arg(subject).replace("Answer: ","") );
00100     else if ( status == QString("accepted") )
00101       msg->setSubject( i18n( "Accepted: %1" ).arg(subject).replace("Answer: ","") );
00102     else if ( status == QString("delegated") )
00103       msg->setSubject( i18n( "Delegated: %1" ).arg(subject).replace("Answer: ","") );
00104   }
00105   msg->setTo( to );
00106   msg->setFrom( receiver() );
00107 
00108   if ( !GlobalSettings::self()->exchangeCompatibleInvitations() ) {
00109     msg->setHeaderField( "Content-Type",
00110                          "text/calendar; method=reply; charset=\"utf-8\"" );
00111     msg->setBody( iCal.utf8() );
00112   }
00113 
00114   if ( delMessage && deleteInvitationAfterReply() ) {
00115     /* We want the triggering mail to be moved to the trash once this one
00116     * has been sent successfully. Set a link header which accomplishes that. */
00117     msg->link( mMsg, KMMsgStatusDeleted );
00118   } else {
00119     updateReaderWindow();
00120   }
00121   // Try and match the receiver with an identity.
00122   // Setting the identity here is important, as that is used to select the correct
00123   // transport later
00124   const KPIM::Identity& identity = kmkernel->identityManager()->identityForAddress( receiver() );
00125   const bool nullIdentity = ( identity == KPIM::Identity::null() );
00126   if ( !nullIdentity ) {
00127     msg->setHeaderField("X-KMail-Identity", QString::number( identity.uoid() ));
00128   }
00129 
00130   const bool identityHasTransport = !identity.transport().isEmpty();
00131   if ( !nullIdentity && identityHasTransport )
00132     msg->setHeaderField( "X-KMail-Transport", identity.transport() );
00133   else if ( !nullIdentity && identity.isDefault() )
00134     msg->setHeaderField( "X-KMail-Transport", GlobalSettings::self()->defaultTransport() );
00135   else {
00136     const QString transport = askForTransport( nullIdentity );
00137     if ( transport.isEmpty() )
00138       return false; // user canceled transport selection dialog
00139     msg->setHeaderField( "X-KMail-Transport", transport );
00140   }
00141 
00142   // Outlook will only understand the reply if the From: header is the
00143   // same as the To: header of the invitation message.
00144   KConfigGroup options( KMKernel::config(), "Groupware" );
00145   if( !options.readBoolEntry( "LegacyMangleFromToHeaders", true ) ) {
00146     if( identity != KPIM::Identity::null() ) {
00147       msg->setFrom( identity.fullEmailAddr() );
00148     }
00149     // Remove BCC from identity on ical invitations (https://intevation.de/roundup/kolab/issue474)
00150     msg->setBcc( "" );
00151   }
00152 
00153   KMail::Composer * cWin = KMail::makeComposer();
00154   cWin->ignoreStickyFields();
00155   cWin->setMsg( msg, false /* mayAutoSign */ );
00156   // cWin->setCharset( "", true );
00157   cWin->disableWordWrap();
00158   cWin->setSigningAndEncryptionDisabled( true );
00159 
00160   if( GlobalSettings::self()->exchangeCompatibleInvitations() ) {
00161     // For Exchange, send ical as attachment, with proper
00162     // parameters
00163     msg->setSubject( status );
00164     msg->setCharset( "utf-8" );
00165     KMMessagePart *msgPart = new KMMessagePart;
00166     msgPart->setName( "cal.ics" );
00167     // msgPart->setCteStr( attachCte ); // "base64" ?
00168     msgPart->setBodyEncoded( iCal.utf8() );
00169     msgPart->setTypeStr( "text" );
00170     msgPart->setSubtypeStr( "calendar" );
00171     msgPart->setParameter( "method", "reply" );
00172     cWin->addAttach( msgPart );
00173   }
00174 
00175   cWin->disableRecipientNumberCheck();
00176   cWin->disableForgottenAttachmentsCheck();
00177   if ( options.readBoolEntry( "AutomaticSending", true ) ) {
00178     cWin->setAutoDeleteWindow(  true );
00179     cWin->slotSendNow();
00180   } else {
00181     cWin->show();
00182   }
00183 
00184   return true;
00185 }
00186 
00187 QString Callback::receiver() const
00188 {
00189   if ( mReceiverSet )
00190     // Already figured this out
00191     return mReceiver;
00192 
00193   mReceiverSet = true;
00194 
00195   QStringList addrs = KPIM::splitEmailAddrList( mMsg->to() );
00196   int found = 0;
00197   for( QStringList::Iterator it = addrs.begin(); it != addrs.end(); ++it ) {
00198     if( kmkernel->identityManager()->identityForAddress( *it ) !=
00199         KPIM::Identity::null() ) {
00200       // Ok, this could be us
00201       ++found;
00202       mReceiver = *it;
00203     }
00204   }
00205   QStringList ccaddrs = KPIM::splitEmailAddrList( mMsg->cc() );
00206   for( QStringList::Iterator it = ccaddrs.begin(); it != ccaddrs.end(); ++it ) {
00207     if( kmkernel->identityManager()->identityForAddress( *it ) !=
00208         KPIM::Identity::null() ) {
00209       // Ok, this could be us
00210       ++found;
00211       mReceiver = *it;
00212     }
00213   }
00214   if( found != 1 ) {
00215     bool ok;
00216     QString selectMessage;
00217     if (found == 0) {
00218       selectMessage = i18n("<qt>None of your identities match the "
00219           "receiver of this message,<br>please "
00220           "choose which of the following addresses "
00221           "is yours, if any, or select one of your identities to use in the reply:");
00222       addrs += kmkernel->identityManager()->allEmails();
00223     } else {
00224       selectMessage = i18n("<qt>Several of your identities match the "
00225           "receiver of this message,<br>please "
00226           "choose which of the following addresses "
00227           "is yours:");
00228     }
00229 
00230     // select default identity by default
00231     const QString defaultAddr = kmkernel->identityManager()->defaultIdentity().primaryEmailAddress();
00232     const int defaultIndex = QMAX( 0, addrs.findIndex( defaultAddr ) );
00233 
00234     mReceiver =
00235       KInputDialog::getItem( i18n( "Select Address" ),
00236           selectMessage,
00237           addrs+ccaddrs, defaultIndex, FALSE, &ok, kmkernel->mainWin() );
00238     if( !ok )
00239       mReceiver = QString::null;
00240   }
00241 
00242   return mReceiver;
00243 }
00244 
00245 void Callback::updateReaderWindow() const
00246 {
00247   mReaderWin->update( true );
00248 }
00249 
00250 void Callback::closeIfSecondaryWindow() const
00251 {
00252   KMail::SecondaryWindow *window = dynamic_cast<KMail::SecondaryWindow*>( mReaderWin->mainWindow() );
00253   if ( window )
00254     window->close();
00255 }
00256 
00257 bool Callback::askForComment( KCal::Attendee::PartStat status ) const
00258 {
00259   if ( ( status != KCal::Attendee::Accepted &&
00260          GlobalSettings::self()->askForCommentWhenReactingToInvitation() ==
00261          GlobalSettings:: EnumAskForCommentWhenReactingToInvitation::AskForAllButAcceptance ) ||
00262        GlobalSettings::self()->askForCommentWhenReactingToInvitation() ==
00263        GlobalSettings:: EnumAskForCommentWhenReactingToInvitation::AlwaysAsk ) {
00264     return true;
00265   } else {
00266     return false;
00267   }
00268 }
00269 
00270 bool Callback::deleteInvitationAfterReply() const
00271 {
00272   return GlobalSettings::self()->deleteInvitationEmailsAfterSendingReply();
00273 }
00274 
00275 bool Callback::exchangeCompatibleInvitations() const
00276 {
00277   return GlobalSettings::self()->exchangeCompatibleInvitations();
00278 }
00279 
00280 bool Callback::outlookCompatibleInvitationReplyComments() const
00281 {
00282   return GlobalSettings::self()->outlookCompatibleInvitationReplyComments();
00283 }
00284 
00285 bool Callback::outlookCompatibleInvitationComparisons() const
00286 {
00287   return GlobalSettings::self()->outlookCompatibleInvitationComparisons();
00288 }
00289 
00290 QString Callback::sender() const
00291 {
00292   return mMsg->from();
00293 }
KDE Home | KDE Accessibility Home | Description of Access Keys