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 <libemailfunctions/email.h>
00037 #include <libkpimidentities/identity.h>
00038 #include <libkpimidentities/identitymanager.h>
00039 #include "kmmainwin.h"
00040 #include "composer.h"
00041 #include "kmreaderwin.h"
00042 #include "secondarywindow.h"
00043 
00044 #include <mimelib/enum.h>
00045 
00046 #include <kinputdialog.h>
00047 #include <klocale.h>
00048 #include <kdebug.h>
00049 
00050 using namespace KMail;
00051 
00052 
00053 Callback::Callback( KMMessage* msg, KMReaderWin* readerWin )
00054   : mMsg( msg ), mReaderWin( readerWin ), mReceiverSet( false )
00055 {
00056 }
00057 
00058 bool Callback::mailICal( const QString& to, const QString iCal,
00059                          const QString& subject, bool delMessage ) const
00060 {
00061   kdDebug(5006) << "Mailing message:\n" << iCal << endl;
00062 
00063   KMMessage *msg = new KMMessage;
00064   msg->initHeader();
00065   msg->setHeaderField( "Content-Type",
00066                        "text/calendar; method=reply; charset=\"utf-8\"" );
00067   msg->setSubject( subject );
00068   msg->setTo( to );
00069   msg->setBody( iCal.utf8() );
00070   msg->setFrom( receiver() );
00071 
00072   if ( delMessage )
00073     /* We want the triggering mail to be moved to the trash once this one
00074     * has been sent successfully. Set a link header which accomplishes that. */
00075     msg->link( mMsg, KMMsgStatusDeleted );
00076 
00077   // Outlook will only understand the reply if the From: header is the
00078   // same as the To: header of the invitation message.
00079   KConfigGroup options( KMKernel::config(), "Groupware" );
00080   if( !options.readBoolEntry( "LegacyMangleFromToHeaders", true ) ) {
00081     // Try and match the receiver with an identity
00082     const KPIM::Identity& identity =
00083       kmkernel->identityManager()->identityForAddress( receiver() );
00084     if( identity != KPIM::Identity::null() )
00085       // Identity found. Use this
00086       msg->setFrom( identity.fullEmailAddr() );
00087       msg->setHeaderField("X-KMail-Identity", QString::number( identity.uoid() ));
00088       // Remove BCC from identity on ical invitations (https://intevation.de/roundup/kolab/issue474)
00089       msg->setBcc( "" );
00090   }
00091 
00092   KMail::Composer * cWin = KMail::makeComposer();
00093   cWin->setMsg( msg, false /* mayAutoSign */ );
00094   // cWin->setCharset( "", true );
00095   cWin->slotWordWrapToggled( false );
00096   cWin->setSigningAndEncryptionDisabled( true );
00097 
00098   if ( options.readBoolEntry( "AutomaticSending", true ) ) {
00099     cWin->setAutoDeleteWindow(  true );
00100     cWin->slotSendNow();
00101   } else {
00102     cWin->show();
00103   }
00104 
00105   return true;
00106 }
00107 
00108 QString Callback::receiver() const
00109 {
00110   if ( mReceiverSet )
00111     // Already figured this out
00112     return mReceiver;
00113 
00114   mReceiverSet = true;
00115 
00116   QStringList addrs = KPIM::splitEmailAddrList( mMsg->to() );
00117   int found = 0;
00118   for( QStringList::Iterator it = addrs.begin(); it != addrs.end(); ++it ) {
00119     if( kmkernel->identityManager()->identityForAddress( *it ) !=
00120         KPIM::Identity::null() ) {
00121       // Ok, this could be us
00122       ++found;
00123       mReceiver = *it;
00124     }
00125   }
00126   QStringList ccaddrs = KPIM::splitEmailAddrList( mMsg->cc() );
00127   for( QStringList::Iterator it = ccaddrs.begin(); it != ccaddrs.end(); ++it ) {
00128     if( kmkernel->identityManager()->identityForAddress( *it ) !=
00129         KPIM::Identity::null() ) {
00130       // Ok, this could be us
00131       ++found;
00132       mReceiver = *it;
00133     }
00134   }
00135   if( found != 1 ) {
00136     bool ok;
00137     QString selectMessage;
00138     if (found == 0) {
00139       selectMessage = i18n("<qt>None of your identities match the "
00140           "receiver of this message,<br>please "
00141           "choose which of the following addresses "
00142           "is yours, if any:");
00143     } else {
00144       selectMessage = i18n("<qt>Several of your identities match the "
00145           "receiver of this message,<br>please "
00146           "choose which of the following addresses "
00147           "is yours:");
00148     }
00149 
00150     mReceiver =
00151       KInputDialog::getItem( i18n( "Select Address" ),
00152           selectMessage,
00153           addrs+ccaddrs, 0, FALSE, &ok, kmkernel->mainWin() );
00154     if( !ok )
00155       mReceiver = QString::null;
00156   }
00157 
00158   return mReceiver;
00159 }
00160 
00161 void Callback::closeIfSecondaryWindow() const
00162 {
00163   KMail::SecondaryWindow *window = dynamic_cast<KMail::SecondaryWindow*>( mReaderWin->mainWindow() );
00164   if ( window )
00165     window->close();
00166 }
KDE Home | KDE Accessibility Home | Description of Access Keys