kmail

callback.cpp
1 /*
2  callback.cpp
3 
4  This file is used by KMail's plugin interface.
5  Copyright (c) 2004 Bo Thorsen <bo@sonofthor.dk>
6 
7  KMail is free software; you can redistribute it and/or modify it
8  under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  KMail is distributed in the hope that it will be useful, but
13  WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 
21  In addition, as a special exception, the copyright holders give
22  permission to link the code of this program with any edition of
23  the TQt library by Trolltech AS, Norway (or with modified versions
24  of TQt that use the same license as TQt), and distribute linked
25  combinations including the two. You must obey the GNU General
26  Public License in all respects for all of the code used other than
27  TQt. If you modify this file, you may extend this exception to
28  your version of the file, but you are not obligated to do so. If
29  you do not wish to do so, delete this exception statement from
30  your version.
31 */
32 
33 #include "callback.h"
34 #include "kmkernel.h"
35 #include "kmmessage.h"
36 #include "kmmsgpart.h"
37 #include <libemailfunctions/email.h>
38 #include <libkpimidentities/identity.h>
39 #include <libkpimidentities/identitymanager.h>
40 #include "kmmainwin.h"
41 #include "composer.h"
42 #include "kmreaderwin.h"
43 #include "secondarywindow.h"
44 #include "transportmanager.h"
45 
46 #include <mimelib/enum.h>
47 
48 #include <kinputdialog.h>
49 #include <tdelocale.h>
50 #include <kdebug.h>
51 
52 using namespace KMail;
53 
54 
55 Callback::Callback( KMMessage* msg, KMReaderWin* readerWin )
56  : mMsg( msg ), mReaderWin( readerWin ), mReceiverSet( false )
57 {
58 }
59 
60 TQString Callback::askForTransport( bool nullIdentity ) const
61 {
62  const TQStringList transports = KMail::TransportManager::transportNames();
63  if ( transports.size() == 1 )
64  return transports.first();
65 
66  const TQString defaultTransport = GlobalSettings::self()->defaultTransport();
67  const int defaultIndex = TQMAX( 0, transports.findIndex( defaultTransport ) );
68 
69  TQString text;
70  if ( nullIdentity )
71  text = i18n( "<qt>The receiver of this invitation doesn't match any of your identities.<br>"
72  "Please select the transport which should be used to send your reply.</qt>" );
73  else
74  text = i18n( "<qt>The identity matching the receiver of this invitation doesn't have an "
75  "associated transport configured.<br>"
76  "Please select the transport which should be used to send your reply.</qt>");
77  bool ok;
78  const TQString transport = KInputDialog::getItem( i18n( "Select Transport" ), text,
79  transports, defaultIndex, FALSE, &ok, kmkernel->mainWin() );
80  if ( !ok )
81  return TQString();
82 
83  return transport;
84 }
85 
86 bool Callback::mailICal( const TQString& to, const TQString &iCal,
87  const TQString& subject, const TQString &status,
88  bool delMessage ) const
89 {
90  kdDebug(5006) << "Mailing message:\n" << iCal << endl;
91 
92  KMMessage *msg = new KMMessage;
93  msg->initHeader();
94  msg->setSubject( subject );
95  if ( GlobalSettings::self()->exchangeCompatibleInvitations() ) {
96  if ( status == TQString("cancel") )
97  msg->setSubject( i18n( "Declined: %1" ).arg(subject).replace("Answer: ","") );
98  else if ( status == TQString("tentative") )
99  msg->setSubject( i18n( "Tentative: %1" ).arg(subject).replace("Answer: ","") );
100  else if ( status == TQString("accepted") )
101  msg->setSubject( i18n( "Accepted: %1" ).arg(subject).replace("Answer: ","") );
102  else if ( status == TQString("delegated") )
103  msg->setSubject( i18n( "Delegated: %1" ).arg(subject).replace("Answer: ","") );
104  }
105  msg->setTo( to );
106  msg->setFrom( receiver() );
107 
108  if ( !GlobalSettings::self()->exchangeCompatibleInvitations() ) {
109  msg->setHeaderField( "Content-Type",
110  "text/calendar; method=reply; charset=\"utf-8\"" );
111  msg->setBody( iCal.utf8() );
112  }
113 
114  if ( delMessage && deleteInvitationAfterReply() )
115  /* We want the triggering mail to be moved to the trash once this one
116  * has been sent successfully. Set a link header which accomplishes that. */
117  msg->link( mMsg, KMMsgStatusDeleted );
118 
119  // Try and match the receiver with an identity.
120  // Setting the identity here is important, as that is used to select the correct
121  // transport later
122  const KPIM::Identity& identity = kmkernel->identityManager()->identityForAddress( receiver() );
123  const bool nullIdentity = ( identity == KPIM::Identity::null() );
124  if ( !nullIdentity ) {
125  msg->setHeaderField("X-KMail-Identity", TQString::number( identity.uoid() ));
126  }
127 
128  const bool identityHasTransport = !identity.transport().isEmpty();
129  if ( !nullIdentity && identityHasTransport )
130  msg->setHeaderField( "X-KMail-Transport", identity.transport() );
131  else if ( !nullIdentity && identity.isDefault() )
132  msg->setHeaderField( "X-KMail-Transport", GlobalSettings::self()->defaultTransport() );
133  else {
134  const TQString transport = askForTransport( nullIdentity );
135  if ( transport.isEmpty() )
136  return false; // user canceled transport selection dialog
137  msg->setHeaderField( "X-KMail-Transport", transport );
138  }
139 
140  // Outlook will only understand the reply if the From: header is the
141  // same as the To: header of the invitation message.
142  TDEConfigGroup options( KMKernel::config(), "Groupware" );
143  if( !options.readBoolEntry( "LegacyMangleFromToHeaders", true ) ) {
144  if( identity != KPIM::Identity::null() ) {
145  msg->setFrom( identity.fullEmailAddr() );
146  }
147  // Remove BCC from identity on ical invitations (https://intevation.de/roundup/kolab/issue474)
148  msg->setBcc( "" );
149  }
150 
151  KMail::Composer * cWin = KMail::makeComposer();
152  cWin->ignoreStickyFields();
153  cWin->setMsg( msg, false /* mayAutoSign */ );
154  // cWin->setCharset( "", true );
155  cWin->disableWordWrap();
156  cWin->setSigningAndEncryptionDisabled( true );
157 
158  if( GlobalSettings::self()->exchangeCompatibleInvitations() ) {
159  // For Exchange, send ical as attachment, with proper
160  // parameters
161  msg->setSubject( status );
162  msg->setCharset( "utf-8" );
163  KMMessagePart *msgPart = new KMMessagePart;
164  msgPart->setName( "cal.ics" );
165  // msgPart->setCteStr( attachCte ); // "base64" ?
166  msgPart->setBodyEncoded( iCal.utf8() );
167  msgPart->setTypeStr( "text" );
168  msgPart->setSubtypeStr( "calendar" );
169  msgPart->setParameter( "method", "reply" );
170  cWin->addAttach( msgPart );
171  }
172 
173  cWin->disableRecipientNumberCheck();
174  cWin->disableForgottenAttachmentsCheck();
175  if ( options.readBoolEntry( "AutomaticSending", true ) ) {
176  cWin->setAutoDeleteWindow( true );
177  cWin->slotSendNow();
178  } else {
179  cWin->show();
180  }
181 
182  return true;
183 }
184 
185 TQString Callback::receiver() const
186 {
187  if ( mReceiverSet )
188  // Already figured this out
189  return mReceiver;
190 
191  mReceiverSet = true;
192 
193  TQStringList addrs = KPIM::splitEmailAddrList( mMsg->to() );
194  int found = 0;
195  for( TQStringList::Iterator it = addrs.begin(); it != addrs.end(); ++it ) {
196  if( kmkernel->identityManager()->identityForAddress( *it ) !=
197  KPIM::Identity::null() ) {
198  // Ok, this could be us
199  ++found;
200  mReceiver = *it;
201  }
202  }
203  TQStringList ccaddrs = KPIM::splitEmailAddrList( mMsg->cc() );
204  for( TQStringList::Iterator it = ccaddrs.begin(); it != ccaddrs.end(); ++it ) {
205  if( kmkernel->identityManager()->identityForAddress( *it ) !=
206  KPIM::Identity::null() ) {
207  // Ok, this could be us
208  ++found;
209  mReceiver = *it;
210  }
211  }
212  if( found != 1 ) {
213  bool ok;
214  TQString selectMessage;
215  if (found == 0) {
216  selectMessage = i18n("<qt>None of your identities match the "
217  "receiver of this message,<br>please "
218  "choose which of the following addresses "
219  "is yours, if any, or select one of your identities to use in the reply:");
220  addrs += kmkernel->identityManager()->allEmails();
221  } else {
222  selectMessage = i18n("<qt>Several of your identities match the "
223  "receiver of this message,<br>please "
224  "choose which of the following addresses "
225  "is yours:");
226  }
227 
228  // select default identity by default
229  const TQString defaultAddr = kmkernel->identityManager()->defaultIdentity().primaryEmailAddress();
230  const int defaultIndex = TQMAX( 0, addrs.findIndex( defaultAddr ) );
231 
232  mReceiver =
233  KInputDialog::getItem( i18n( "Select Address" ),
234  selectMessage,
235  addrs+ccaddrs, defaultIndex, FALSE, &ok, kmkernel->mainWin() );
236  if( !ok )
237  mReceiver = TQString();
238  }
239 
240  return mReceiver;
241 }
242 
244 {
245  KMail::SecondaryWindow *window = dynamic_cast<KMail::SecondaryWindow*>( mReaderWin->mainWindow() );
246  if ( window )
247  window->close();
248 }
249 
250 bool Callback::askForComment( KCal::Attendee::PartStat status ) const
251 {
252  if ( ( status != KCal::Attendee::Accepted
253  && GlobalSettings::self()->askForCommentWhenReactingToInvitation()
254  == GlobalSettings:: EnumAskForCommentWhenReactingToInvitation::AskForAllButAcceptance )
255  || GlobalSettings::self()->askForCommentWhenReactingToInvitation()
256  == GlobalSettings:: EnumAskForCommentWhenReactingToInvitation::AlwaysAsk )
257  return true;
258  return false;
259 }
260 
261 bool Callback::deleteInvitationAfterReply() const
262 {
263  return GlobalSettings::self()->deleteInvitationEmailsAfterSendingReply();
264 }
265 
266 bool Callback::exchangeCompatibleInvitations() const
267 {
268  return GlobalSettings::self()->exchangeCompatibleInvitations();
269 }
270 
271 bool Callback::outlookCompatibleInvitationReplyComments() const
272 {
273  return GlobalSettings::self()->outlookCompatibleInvitationReplyComments();
274 }
275 
276 TQString Callback::sender() const
277 {
278  return mMsg->from();
279 }
This is a Mime Message.
Definition: kmmessage.h:68
void link(const KMMessage *aMsg, KMMsgStatus aStatus)
Links this message to aMsg, setting link type to aStatus.
Definition: kmmessage.cpp:4190
void setBody(const TQCString &aStr)
Set the message body.
Definition: kmmessage.cpp:2774
TQString from() const
Get or set the 'From' header field.
Definition: kmmessage.cpp:2015
void setCharset(const TQCString &charset, DwEntity *entity=0)
Sets the charset of the message or a subpart of the message.
Definition: kmmessage.cpp:4114
TQString to() const
Get or set the 'To' header field.
Definition: kmmessage.cpp:1894
TQString cc() const
Get or set the 'Cc' header field.
Definition: kmmessage.cpp:1940
void initHeader(uint identity=0)
Initialize header fields.
Definition: kmmessage.cpp:1715
void setHeaderField(const TQCString &name, const TQString &value, HeaderFieldType type=Unstructured, bool prepend=false)
Set the header field with the given name to the given value.
Definition: kmmessage.cpp:2339
This class implements a "reader window", that is a window used for reading or viewing messages.
Definition: kmreaderwin.h:75
void closeIfSecondaryWindow() const
Close the main window showing this message, if it's a secondary window.
Definition: callback.cpp:243
TQString receiver() const
Get the receiver of the mail.
Definition: callback.cpp:185
bool mailICal(const TQString &to, const TQString &iCal, const TQString &subject, const TQString &status, bool delMessage=true) const
Mail a message @ param status can be accepted/cancel/tentative/delegated.
Definition: callback.cpp:86
TQString sender() const
Returns the sender of the mail.
Definition: callback.cpp:276
Window class for secondary KMail window like the composer window and the separate message window.
static TQStringList transportNames()
Returns the list for transport names.
folderdiaquotatab.h
Definition: aboutdata.cpp:40