00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
#include "kmgroupware.h"
00038
00039
#include "kmmainwin.h"
00040
#include "kmmainwidget.h"
00041
#include "kmfoldertree.h"
00042
#include "kmcomposewin.h"
00043
#include <libkpimidentities/identity.h>
00044
#include <libkpimidentities/identitymanager.h>
00045
#include <libkdepim/email.h>
00046
#include "kmkernel.h"
00047
00048
#include <kurl.h>
00049
#include <kmessagebox.h>
00050
#include <dcopclient.h>
00051
#include <kdcopservicestarter.h>
00052
#include <kconfig.h>
00053
#include <kapplication.h>
00054
#include <kinputdialog.h>
00055
#include <kdebug.h>
00056
#include <qregexp.h>
00057
00058
#include <mimelib/enum.h>
00059
00060
#include <assert.h>
00061
00062
#include "korganizeriface_stub.h"
00063
00064
00065
00066
static bool connectToKOrganizer();
00067
static KOrganizerIface_stub* mKOrganizerIfaceStub;
00068
00069
00070
00071 KMGroupware::KMGroupware(
QObject* parent,
const char* name )
00072 :
QObject( parent, name ), mUseGroupware( false )
00073 {
00074
00075 mKOrganizerIfaceStub = 0;
00076 kapp->dcopClient()->setNotifications(
true );
00077 connect( kapp->dcopClient(), SIGNAL( applicationRemoved(
const QCString& ) ),
00078
this, SLOT( unregisteredFromDCOP(
const QCString& ) ) );
00079
00080
00081 connect( kmkernel, SIGNAL( configChanged() ),
this, SLOT( readConfig() ) );
00082 }
00083
00084
00085 KMGroupware::~KMGroupware()
00086 {
00087 kapp->dcopClient()->setNotifications(
false );
00088
delete mKOrganizerIfaceStub;
00089 }
00090
00091
void KMGroupware::readConfig()
00092 {
00093 KConfigGroup options( KMKernel::config(),
"Groupware" );
00094 mUseGroupware = options.readBoolEntry(
"Enabled",
false );
00095 }
00096
00097
bool KMGroupware::vPartFoundAndDecoded( KMMessage* msg,
QString& s )
00098 {
00099 assert( msg );
00100
00101
if( ( DwMime::kTypeText == msg->type() && ( DwMime::kSubtypeVCal == msg->subtype() ||
00102 DwMime::kSubtypeXVCard == msg->subtype() ) ) ||
00103 ( DwMime::kTypeApplication == msg->type() &&
00104 DwMime::kSubtypeOctetStream == msg->subtype() ) )
00105 {
00106 s = QString::fromUtf8( msg->bodyDecoded() );
00107
return true;
00108 }
else if( DwMime::kTypeMultipart == msg->type() &&
00109 (DwMime::kSubtypeMixed == msg->subtype() ) ||
00110 (DwMime::kSubtypeAlternative == msg->subtype() ))
00111 {
00112
00113 DwBodyPart* dwPart = msg->findDwBodyPart( DwMime::kTypeApplication,
00114 DwMime::kSubtypeMsTNEF );
00115
if( !dwPart )
00116 dwPart = msg->findDwBodyPart( DwMime::kTypeApplication,
00117 DwMime::kSubtypeOctetStream );
00118
if( dwPart ){
00119
00120 KMMessagePart msgPart;
00121 KMMessage::bodyPart(dwPart, &msgPart);
00122
if ( !connectToKOrganizer() )
00123 kdError() <<
"DCOP error during KMGroupware::vPartFoundAndDecoded()\n";
00124
else {
00125 s = mKOrganizerIfaceStub->msTNEFToVPart( msgPart.bodyDecodedBinary() );
00126
return !s.isEmpty();
00127 }
00128 }
else {
00129 dwPart = msg->findDwBodyPart( DwMime::kTypeText, DwMime::kSubtypeVCal );
00130
if (dwPart) {
00131 KMMessagePart msgPart;
00132 KMMessage::bodyPart(dwPart, &msgPart);
00133 s = msgPart.body();
00134
return true;
00135 }
00136 }
00137 }
else if( DwMime::kTypeMultipart == msg->type() &&
00138 DwMime::kSubtypeMixed == msg->subtype() ) {
00139
00140 }
00141
00142
return false;
00143 }
00144
00145
00146
00147
00148
00149
QString KMGroupware::vPartToHTML(
const QString& )
00150 {
00151
return "<b>This is an iCalendar attachment handled by the wrong bodypart"
00152
"formatterplugin.</b><p/>";
00153
#if 0
00154
QString html;
00155
00156
if( mUseGroupware ) {
00157
00158
if ( !connectToKOrganizer() ) {
00159 kdError() <<
"DCOP error during KMGroupware::processVCalRequest()\n";
00160 }
else {
00161 html = mKOrganizerIfaceStub->formatICal( iCal );
00162 kdDebug(5006) <<
"KOrganizer call succeeded, html = " << html << endl;
00163 }
00164 }
00165
00166
return html;
00167
#endif
00168
}
00169
00170
QString KMGroupware::msTNEFToHTML(
const QByteArray& tnef )
00171 {
00172
QString html;
00173
00174
if( mUseGroupware ) {
00175
00176
if ( !connectToKOrganizer() ) {
00177 kdError() <<
"DCOP error during KMGroupware::processVCalRequest()\n";
00178 }
else {
00179 html = mKOrganizerIfaceStub->formatTNEF( tnef );
00180 kdDebug(5006) <<
"KOrganizer call succeeded, html = " << html << endl;
00181 }
00182 }
00183
00184
return html;
00185 }
00186
00187
00188
00189
00190
00191
static void iCalRequest(
const QString& receiver,
const QString& iCal,
00192
const QString& choice )
00193 {
00194
#if 0
00195
00196
00197
00198 KMMessage* msgOld = mMainWidget->headers()->currentMsg();
00199 KConfigGroup options( KMKernel::config(),
"Groupware" );
00200
QString fromAddress;
00201
if( options.readBoolEntry(
"LegacyMangleFromToHeaders",
false ) ) {
00202
QStringList toAddresses = KPIM::splitEmailAddrList( msgOld->to() );
00203
if( toAddresses.count() <= 1 )
00204
00205
00206 fromAddress = msgOld->to();
00207
else {
00208
00209
00210
00211
for( QStringList::Iterator sit = toAddresses.begin();
00212 sit != toAddresses.end(); ++sit ) {
00213
if( KPIM::getEmailAddr( *sit ) ==
00214 kmkernel->identityManager()->defaultIdentity().emailAddr().local8Bit() ) {
00215
00216
00217 fromAddress = *sit;
00218
break;
00219 }
00220 }
00221
00222
00223
00224
if( fromAddress.isEmpty() ) {
00225
bool bOk;
00226 fromAddress = KInputDialog::getItem( i18n(
"Select Address" ),
00227 i18n(
"In order to let Outlook recognize you as the receiver, you need to indicate which one of the following addresses is your email address:" ),
00228 toAddresses, 0,
false, &bOk,
00229 kmkernel->mainWin() );
00230
if( !bOk )
00231
00232
00233 fromAddress = toAddresses.first();
00234 }
00235 }
00236 }
00237
#endif
00238
00239
if ( !connectToKOrganizer() )
00240 kdError() <<
"DCOP error during KMGroupware::processVCalRequest()\n";
00241
else {
00242
bool rc = mKOrganizerIfaceStub->eventRequest( choice, receiver, iCal );
00243 kdDebug(5006) <<
"KOrganizer call succeeded, rc = " << rc << endl;
00244
00245
#if 0
00246
00247
if( rc && mMainWidget ) mMainWidget->slotTrashMsg();
00248
#endif
00249
}
00250 }
00251
00252
static void iCalReply(
const QString& iCal )
00253 {
00254
bool event;
00255
if( iCal.find(
QRegExp(
"BEGIN:\\s*VEVENT" ) ) != -1 )
00256 event =
true;
00257
else if( iCal.find(
QRegExp(
"BEGIN:\\s*VTODO" ) ) != -1 )
00258 event =
false;
00259
else {
00260 kdDebug(5006) <<
"processVCalReply called with something that is not a iCal\n";
00261
return;
00262 }
00263
00264
00265
if ( !connectToKOrganizer() )
00266 kdError() <<
"DCOP error during KMGroupware::processVCalReply()\n";
00267
else {
00268
bool rc = mKOrganizerIfaceStub->eventReply( iCal );
00269 kdDebug(5006) <<
"KOrganizer call succeeded, rc = " << rc << endl;
00270
00271
00272 KMessageBox::information( kmkernel->mainWin(),
00273 (event ?
00274 i18n(
"The answer was registered in your calendar.") :
00275 i18n("The answer was registered in your task list.")),
00276
QString::null, "groupwareBox");
00277
00278
#if 0
00279
00280
if( mMainWidget ) mMainWidget->slotTrashMsg();
00281
#endif
00282
}
00283 }
00284
00285
static void iCalCancel(
const QString& iCal )
00286 {
00287
if ( !connectToKOrganizer() )
00288 kdError() <<
"DCOP error during KMGroupware::iCalCancel()\n";
00289
else {
00290
bool rc = mKOrganizerIfaceStub->cancelEvent( iCal );
00291 kdDebug(5006) <<
"KOrganizer call succeeded, rc = " << rc << endl;
00292
00293
#if 0
00294
00295
if( mMainWidget ) mMainWidget->slotTrashMsg();
00296
#endif
00297
}
00298 }
00299
00300
00301
00302
static QString urlPart(
QString& aUrl )
00303 {
00304
QString result;
00305
int i = aUrl.find(
'_' );
00306
if( i < 0 ) i = aUrl.find(
'#' );
00307
if( i < 0 ) {
00308
00309 result = aUrl;
00310 aUrl.truncate( 0 );
00311 }
else {
00312 result = aUrl.left( i );
00313 aUrl = aUrl.mid( i + 1 );
00314 }
00315
return result;
00316 }
00317
00318
bool KMGroupware::handleLink(
const KURL &url, KMMessage* msg )
00319 {
00320
QString aUrl = url.path();
00321
if( urlPart( aUrl ) !=
"groupware" )
return false;
00322
QString gwAction = urlPart( aUrl );
00323
00324
00325
QString iCal;
00326
if( !vPartFoundAndDecoded( msg, iCal ) ) {
00327 kdDebug(5006) <<
"Could not find the iCal for this link\n";
00328
return false;
00329 }
00330
00331
if( gwAction ==
"request" ) {
00332
00333
QString receiver;
00334
if( msg ) {
00335 KPIM::Identity ident =
00336 kmkernel->identityManager()->identityForAddress( msg->to() );
00337
if( ident != KPIM::Identity::null ) {
00338 receiver = ident.emailAddr();
00339 }
else {
00340
QStringList addrs = KPIM::splitEmailAddrList( msg->to() );
00341
if( addrs.count() == 1 )
00342
00343 receiver = addrs[0];
00344
else {
00345
bool ok;
00346 receiver = KInputDialog::
00347 getItem( i18n(
"Select Address"),
00348 i18n(
"None of your identities match the receiver "
00349
"of this message,<br> please choose which of "
00350
"the following addresses is yours:"),
00351 addrs, 0, FALSE, &ok, kmkernel->mainWin() );
00352
if( !ok )
return false;
00353 }
00354 }
00355 }
00356 iCalRequest( receiver, iCal, urlPart( aUrl ) );
00357 }
else if( gwAction ==
"reply" )
00358 iCalReply( iCal );
00359
else if( gwAction ==
"cancel" )
00360 iCalCancel( iCal );
00361
else {
00362
00363 kdDebug(5006) <<
"Unknown groupware action " << gwAction << endl;
00364
return false;
00365 }
00366
00367
return true;
00368 }
00369
00370
00371
00372
00373
00374
static const QCString dcopObjectId =
"KOrganizerIface";
00375
00376
static bool connectToKOrganizer()
00377 {
00378
if ( !mKOrganizerIfaceStub ) {
00379
QString error;
00380
QCString dcopService;
00381
int result = KDCOPServiceStarter::self()->
00382 findServiceFor(
"DCOP/Organizer", QString::null,
00383 QString::null, &error, &dcopService );
00384
if ( result != 0 ) {
00385 kdDebug(5800) <<
"Could not connect to KOrganizer\n";
00386
00387
00388
return false;
00389 }
00390
00391
QCString dummy;
00392
00393
if ( !kapp->dcopClient()->findObject( dcopService, dcopObjectId,
"",
QByteArray(), dcopService, dummy ) ) {
00394 KDCOPServiceStarter::self()->startServiceFor(
"DCOP/Organizer", QString::null,
00395 QString::null, &error, &dcopService );
00396
if( !kapp->dcopClient()->findObject( dcopService, dcopObjectId,
"",
QByteArray(), dcopService, dummy ) )
00397
return false;
00398 }
00399
00400 mKOrganizerIfaceStub =
new KOrganizerIface_stub( kapp->dcopClient(),
00401 dcopService,
00402 dcopObjectId );
00403 }
00404
00405
return ( mKOrganizerIfaceStub != 0 );
00406 }
00407
00408
void KMGroupware::unregisteredFromDCOP(
const QCString& appId )
00409 {
00410
if ( mKOrganizerIfaceStub && mKOrganizerIfaceStub->app() == appId ) {
00411
00412
00413
delete mKOrganizerIfaceStub;
00414 mKOrganizerIfaceStub = 0;
00415 }
00416 }
00417
00418
00419
#include "kmgroupware.moc"