kmail Library API Documentation

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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 <libkdepim/email.h>
00037 #include <libkpimidentities/identity.h>
00038 #include <libkpimidentities/identitymanager.h>
00039 #include "kmmainwin.h"
00040 #include "kmcomposewin.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 ) const
00060 {
00061   kdDebug(5006) << "Mailing message:\n" << iCal << endl;
00062 
00063   if ( receiver().isEmpty() )
00064     // This can't work
00065     return false;
00066 
00067   KMMessage *msg = new KMMessage;
00068   msg->initHeader();
00069   msg->setHeaderField( "Content-Type",
00070                        "text/calendar; method=reply; charset=\"utf-8\"" );
00071   msg->setSubject( subject );
00072   msg->setTo( to );
00073   msg->setBody( iCal.utf8() );
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     // Try and match the receiver with an identity
00084     const KPIM::Identity& identity =
00085       kmkernel->identityManager()->identityForAddress( receiver() );
00086     if( identity != KPIM::Identity::null() ) {
00087       // Identity found. Use this
00088       msg->setFrom( identity.fullEmailAddr() );
00089       msg->setHeaderField( "X-KMail-Identity",
00090                            QString::number( identity.uoid() ) );
00091 
00092       // Remove BCC from all replies on ical invitations
00093       msg->setBcc( "" );
00094     }
00095   }
00096 
00097   KMComposeWin *cWin = new KMComposeWin();
00098   cWin->setMsg( msg, false /* mayAutoSign */ );
00099   // cWin->setCharset( "", true );
00100   cWin->slotWordWrapToggled( false );
00101   cWin->setSigningAndEncryptionDisabled( true );
00102 
00103   if ( options.readBoolEntry( "AutomaticSending", true ) ) {
00104     cWin->setAutoDeleteWindow( true );
00105     cWin->slotSendNow();
00106   } else {
00107     cWin->show();
00108   }
00109 
00110   return true;
00111 }
00112 
00113 QString Callback::receiver() const
00114 {
00115   if ( mReceiverSet )
00116     // Already figured this out
00117     return mReceiver;
00118 
00119   // Don't ask again
00120   mReceiverSet = true;
00121 
00122   QStringList addrs = KPIM::splitEmailAddrList( mMsg->to() );
00123   int found = 0;
00124   for( QStringList::Iterator it = addrs.begin(); it != addrs.end(); ++it )
00125     if( kmkernel->identityManager()->identityForAddress( *it )
00126         != KPIM::Identity::null() ) {
00127       // Ok, this could be us
00128       ++found;
00129       mReceiver = *it;
00130     }
00131 
00132   if( found != 1 ) {
00133     bool ok;
00134     const QString message =
00135       i18n( "<qt>If you received this personally, please choose which of<br>"
00136             "the following addresses is yours.<br><br>"
00137             "If you received this from a distribution list, then choose<br>"
00138             "which of your own identities to reply with." );
00139 
00140     // Make the list of addresses
00141     QStringList toShow = addrs;
00142 
00143     // Remember how many original receivers we had
00144     const int receivers = addrs.count();
00145 
00146     // Add identities to the list
00147     KPIM::IdentityManager::ConstIterator it =
00148       kmkernel->identityManager()->begin();
00149     KPIM::IdentityManager::ConstIterator end =
00150         kmkernel->identityManager()->end();
00151     int defaultIdx = 0;
00152     for ( int i = 0; it != end; ++it, ++i ) {
00153       const QString addr = (*it).fullEmailAddr();
00154       addrs << addr;
00155       toShow << i18n( "Identity: %1" ).arg( addr );
00156       if ( (*it).isDefault() ) defaultIdx = i;
00157     }
00158 
00159     // Choose
00160     mReceiver =
00161       KInputDialog::getItem( i18n( "Select Address" ), message,
00162                              toShow, defaultIdx, false, &ok, kmkernel->mainWin() );
00163     if( !ok ) {
00164       mReceiver = QString::null;
00165 
00166       // Since the user cancelled the dialog, we do want to ask again
00167       mReceiverSet = false;
00168     } else {
00169       // If this is an identity, we need to set it to the right address
00170       const int i = toShow.findIndex( mReceiver );
00171       if ( i >= receivers )
00172         // Do the correction
00173         mReceiver = addrs[ i ];
00174     }
00175   }
00176 
00177   kdDebug(5006) << "Receiver: " << mReceiver << endl;
00178 
00179   return mReceiver;
00180 }
00181 
00182 void Callback::closeIfSecondaryWindow() const
00183 {
00184   KMail::SecondaryWindow *window = dynamic_cast<KMail::SecondaryWindow*>( mReaderWin->mainWindow() );
00185   if ( window )
00186     window->close();
00187 }
KDE Logo
This file is part of the documentation for kmail Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Oct 17 09:55:16 2007 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003