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
#include <unistd.h>
00026
#include <stdio.h>
00027
00028
#include <klocale.h>
00029
#include <kstandarddirs.h>
00030
#include <kdebug.h>
00031
#include <kmessagebox.h>
00032
#include <kurl.h>
00033
#include <kapplication.h>
00034
#include <dcopclient.h>
00035
#include <kprocess.h>
00036
00037
#include <libkcal/event.h>
00038
#include <libkcal/todo.h>
00039
00040
#include "version.h"
00041
#include "koprefs.h"
00042
00043
#include "komailclient.h"
00044
00045 KOMailClient::KOMailClient()
00046 {
00047 }
00048
00049 KOMailClient::~KOMailClient()
00050 {
00051 }
00052
00053
bool KOMailClient::mailAttendees(IncidenceBase *incidence,
const QString &attachment)
00054 {
00055 Attendee::List attendees = incidence->attendees();
00056
if (attendees.count() == 0)
return false;
00057
00058
const QString from = incidence->organizer();
00059
QStringList toList;
00060
for(uint i=0; i<attendees.count();++i) {
00061
const QString email = (*attendees.at(i))->email();
00062
if( !KOPrefs::instance()->thatIsMe( email ) )
00063
00064 toList << email;
00065 }
00066
if( toList.count() == 0 )
00067
00068
return false;
00069
QString to = toList.join(
", " );
00070
00071
QString subject;
00072
if(incidence->type()!=
"FreeBusy") {
00073 Incidence *inc = static_cast<Incidence *>(incidence);
00074 subject = inc->summary();
00075 }
else {
00076 subject =
"Free Busy Object";
00077 }
00078
00079
QString body = createBody(incidence);
00080
00081
bool bcc = KOPrefs::instance()->mBcc;
00082
00083
return send(from,to,subject,body,bcc,attachment);
00084 }
00085
00086
bool KOMailClient::mailOrganizer(IncidenceBase *incidence,
const QString &attachment)
00087 {
00088
QString to = incidence->organizer();
00089
00090
QString from = KOPrefs::instance()->email();
00091
00092
QString subject;
00093
if(incidence->type()!=
"FreeBusy") {
00094 Incidence *inc = static_cast<Incidence *>(incidence);
00095 subject = inc->summary();
00096 }
else {
00097 subject =
"Free Busy Message";
00098 }
00099
00100
QString body = createBody(incidence);
00101
00102
bool bcc = KOPrefs::instance()->mBcc;
00103
00104
return send(from,to,subject,body,bcc,attachment);
00105 }
00106
00107
bool KOMailClient::mailTo(IncidenceBase *incidence,
const QString &recipients,
00108
const QString &attachment)
00109 {
00110
QString from = KOPrefs::instance()->email();
00111
QString subject;
00112
if(incidence->type()!=
"FreeBusy") {
00113 Incidence *inc = static_cast<Incidence *>(incidence);
00114 subject = inc->summary();
00115 }
else {
00116 subject =
"Free Busy Message";
00117 }
00118
QString body = createBody(incidence);
00119
bool bcc = KOPrefs::instance()->mBcc;
00120 kdDebug () <<
"KOMailClient::mailTo " << recipients << endl;
00121
return send(from,recipients,subject,body,bcc,attachment);
00122 }
00123
00124
bool KOMailClient::send(
const QString &from,
const QString &to,
00125
const QString &subject,
const QString &body,
bool bcc,
00126
const QString &attachment)
00127 {
00128 kdDebug(5850) <<
"KOMailClient::sendMail():\nFrom: " << from <<
"\nTo: " << to
00129 <<
"\nSubject: " << subject <<
"\nBody: \n" << body
00130 <<
"\nAttachment:\n" << attachment << endl;
00131
00132
if (KOPrefs::instance()->mMailClient == KOPrefs::MailClientSendmail) {
00133
bool needHeaders =
true;
00134
00135
QString command = KStandardDirs::findExe(QString::fromLatin1(
"sendmail"),
00136 QString::fromLatin1(
"/sbin:/usr/sbin:/usr/lib"));
00137
if (!command.isNull()) command += QString::fromLatin1(
" -oi -t");
00138
else {
00139 command = KStandardDirs::findExe(QString::fromLatin1(
"mail"));
00140
if (command.isNull())
return false;
00141
00142 command.append(QString::fromLatin1(
" -s "));
00143 command.append(KProcess::quote(subject));
00144
00145
if (bcc) {
00146 command.append(QString::fromLatin1(
" -b "));
00147 command.append(KProcess::quote(from));
00148 }
00149
00150 command.append(
" ");
00151 command.append(KProcess::quote(to));
00152
00153 needHeaders =
false;
00154 }
00155
00156 FILE * fd = popen(command.local8Bit(),
"w");
00157
if (!fd)
00158 {
00159 kdError() <<
"Unable to open a pipe to " << command << endl;
00160
return false;
00161 }
00162
00163
QString textComplete;
00164
if (needHeaders)
00165 {
00166 textComplete += QString::fromLatin1(
"From: ") + from +
'\n';
00167 textComplete += QString::fromLatin1(
"To: ") + to +
'\n';
00168
if (bcc) textComplete += QString::fromLatin1(
"Bcc: ") + from +
'\n';
00169 textComplete += QString::fromLatin1(
"Subject: ") + subject +
'\n';
00170 textComplete += QString::fromLatin1(
"X-Mailer: KOrganizer") + korgVersion +
'\n';
00171 }
00172 textComplete +=
'\n';
00173 textComplete += body;
00174 textComplete +=
'\n';
00175 textComplete += attachment;
00176
00177 fwrite(textComplete.local8Bit(),textComplete.length(),1,fd);
00178
00179 pclose(fd);
00180 }
else {
00181
if (!kapp->dcopClient()->isApplicationRegistered(
"kmail")) {
00182
if (KApplication::startServiceByDesktopName(
"kmail")) {
00183 KMessageBox::error(0,i18n(
"No running instance of KMail found."));
00184
return false;
00185 }
00186 }
00187
00188
if (attachment.isEmpty()) {
00189
if (!kMailOpenComposer(to,
"",bcc ? from :
"",subject,body,0,KURL()))
return false;
00190 }
else {
00191
QString meth;
00192
int idx = attachment.find(
"METHOD");
00193
if (idx>=0) {
00194 idx = attachment.find(
':',idx)+1;
00195 meth = attachment.mid(idx,attachment.find(
'\n',idx)-idx);
00196 meth = meth.lower();
00197 }
else {
00198 meth =
"publish";
00199 }
00200
if (!kMailOpenComposer(to,
"",bcc ? from :
"",subject,body,0,
"cal.ics",
"7bit",
00201 attachment.utf8(),
"text",
"calendar",
"method",meth,
00202
"attachment",
"utf-8"))
return false;
00203 }
00204 }
00205
return true;
00206 }
00207
00208
int KOMailClient::kMailOpenComposer(
const QString& arg0,
const QString& arg1,
00209
const QString& arg2,
const QString& arg3,
const QString& arg4,
int arg5,
00210
const KURL& arg6)
00211 {
00212
00213
00214
00215
int result = 0;
00216
00217
QByteArray data, replyData;
00218
QCString replyType;
00219
QDataStream arg( data, IO_WriteOnly );
00220 arg << arg0;
00221 arg << arg1;
00222 arg << arg2;
00223 arg << arg3;
00224 arg << arg4;
00225 arg << arg5;
00226 arg << arg6;
00227
#if KDE_IS_VERSION( 3, 2, 90 )
00228
kapp->updateRemoteUserTimestamp(
"kmail" );
00229
#endif
00230
if (kapp->dcopClient()->call(
"kmail",
"KMailIface",
"openComposer(QString,QString,QString,QString,QString,int,KURL)", data, replyType, replyData ) ) {
00231
if ( replyType ==
"int" ) {
00232
QDataStream _reply_stream( replyData, IO_ReadOnly );
00233 _reply_stream >> result;
00234 }
else {
00235 kdDebug(5850) <<
"kMailOpenComposer() call failed." << endl;
00236 }
00237 }
else {
00238 kdDebug(5850) <<
"kMailOpenComposer() call failed." << endl;
00239 }
00240
return result;
00241 }
00242
00243
int KOMailClient::kMailOpenComposer(
const QString& arg0,
const QString& arg1,
00244
const QString& arg2,
const QString& arg3,
00245
const QString& arg4,
int arg5,
const QString& arg6,
00246
const QCString& arg7,
const QCString& arg8,
00247
const QCString& arg9,
const QCString& arg10,
00248
const QCString& arg11,
const QString& arg12,
00249
const QCString& arg13,
const QCString& arg14 )
00250 {
00251
00252
00253
00254
00255
00256
00257
00258
int result = 0;
00259
00260
QByteArray data, replyData;
00261
QCString replyType;
00262
QDataStream arg( data, IO_WriteOnly );
00263 arg << arg0;
00264 arg << arg1;
00265 arg << arg2;
00266 arg << arg3;
00267 arg << arg4;
00268 arg << arg5;
00269 arg << arg6;
00270 arg << arg7;
00271 arg << arg8;
00272 arg << arg9;
00273 arg << arg10;
00274 arg << arg11;
00275 arg << arg12;
00276 arg << arg13;
00277 arg << arg14;
00278
#if KDE_IS_VERSION( 3, 2, 90 )
00279
kapp->updateRemoteUserTimestamp(
"kmail");
00280
#endif
00281
if ( kapp->dcopClient()->call(
"kmail",
"KMailIface",
00282
"openComposer(QString,QString,QString,QString,QString,int,QString,QCString,QCString,QCString,QCString,QCString,QString,QCString,QCString)", data, replyType, replyData ) ) {
00283
if ( replyType ==
"int" ) {
00284
QDataStream _reply_stream( replyData, IO_ReadOnly );
00285 _reply_stream >> result;
00286 }
else {
00287 kdDebug(5850) <<
"kMailOpenComposer() call failed." << endl;
00288 }
00289 }
else {
00290 kdDebug(5850) <<
"kMailOpenComposer() call failed." << endl;
00291 }
00292
return result;
00293 }
00294
00295
00296
QString KOMailClient::createBody(IncidenceBase *incidence)
00297 {
00298
QString CR = (
"\n");
00299
00300
QString body;
00301
00302
00303
if (incidence->type()==
"Event") {
00304 Event *selectedEvent = static_cast<Event *>(incidence);
00305
QString recurrence[]= {i18n(
"no recurrence",
"None"),
00306 i18n(
"Minutely"), i18n(
"Hourly"), i18n(
"Daily"),
00307 i18n(
"Weekly"), i18n(
"Monthly Same Day"), i18n(
"Monthly Same Position"),
00308 i18n(
"Yearly"), i18n(
"Yearly"), i18n(
"Yearly")};
00309
00310
if (!selectedEvent->organizer().isEmpty()) {
00311 body += i18n(
"Organizer: %1").arg(selectedEvent->organizer());
00312 body += CR;
00313 }
00314 body += i18n(
"Summary: %1").arg(selectedEvent->summary());
00315 body += CR;
00316
if (!selectedEvent->location().isEmpty()) {
00317 body += i18n(
"Location: %1").arg(selectedEvent->location());
00318 body += CR;
00319 }
00320 body += i18n(
"Start Date: %1").arg(selectedEvent->dtStartDateStr());
00321 body += CR;
00322
if (!selectedEvent->doesFloat()) {
00323 body += i18n(
"Start Time: %1").arg(selectedEvent->dtStartTimeStr());
00324 body += CR;
00325 }
00326
if ( selectedEvent->dtStart()!=selectedEvent->dtEnd() ) {
00327 body += i18n(
"End Date: %1").arg(selectedEvent->dtEndDateStr());
00328 body += CR;
00329 }
00330
if (!selectedEvent->doesFloat()) {
00331 body += i18n(
"End Time: %1").arg(selectedEvent->dtEndTimeStr());
00332 body += CR;
00333 }
00334
if (selectedEvent->doesRecur()) {
00335 body += i18n(
"Recurs: %1")
00336 .arg(recurrence[selectedEvent->recurrence()->doesRecur()]);
00337 body += CR;
00338
00339
00340
00341
00342
00343
if (selectedEvent->recurrence()->duration() > 0 ) {
00344 body += i18n (
"Repeats %1 times")
00345 .arg(QString::number(selectedEvent->recurrence()->duration()));
00346 body += CR;
00347 }
else {
00348
if (selectedEvent->recurrence()->duration() != -1) {
00349
00350 body += i18n(
"End Date: %1")
00351 .arg(selectedEvent->recurrence()->endDateStr());
00352 body += CR;
00353 }
else {
00354 body += i18n(
"Repeats forever");
00355 body += CR;
00356 }
00357 }
00358 }
00359
QString details = selectedEvent->description();
00360
if (!details.isEmpty()) {
00361 body += i18n(
"Details:");
00362 body += CR;
00363 body += details;
00364 body += CR;
00365 }
00366 }
00367
00368
00369
if (incidence->type()==
"Todo") {
00370 Todo *selectedEvent = static_cast<Todo *>(incidence);
00371
if (!selectedEvent->organizer().isEmpty()) {
00372 body += i18n(
"Organizer: %1").arg(selectedEvent->organizer());
00373 body += CR;
00374 }
00375 body += i18n(
"Summary: %1").arg(selectedEvent->summary());
00376 body += CR;
00377
if (!selectedEvent->location().isEmpty()) {
00378 body += i18n(
"Location: %1").arg(selectedEvent->location());
00379 body += CR;
00380 }
00381
if (selectedEvent->hasStartDate()) {
00382 body += i18n(
"Start Date: %1").arg(selectedEvent->dtStartDateStr());
00383 body += CR;
00384
if (!selectedEvent->doesFloat()) {
00385 body += i18n(
"Start Time: %1").arg(selectedEvent->dtStartTimeStr());
00386 body += CR;
00387 }
00388 }
00389
if (selectedEvent->hasDueDate()) {
00390 body += i18n(
"Due Date: %1").arg(selectedEvent->dtDueDateStr());
00391 body += CR;
00392
if (!selectedEvent->doesFloat()) {
00393 body += i18n(
"Due Time: %1").arg(selectedEvent->dtDueTimeStr());
00394 body += CR;
00395 }
00396 }
00397
QString details = selectedEvent->description();
00398
if (!details.isEmpty()) {
00399 body += i18n(
"Details:");
00400 body += CR;
00401 body += details;
00402 body += CR;
00403 }
00404 }
00405
00406
00407
if(incidence->type()==
"FreeBusy") {
00408 body = i18n(
"This is a Free Busy Object");
00409 }
00410
00411
00412
if(incidence->type()==
"Journal") {
00413 Incidence *inc = static_cast<Incidence *>(incidence);
00414 body = inc->summary();
00415 body += CR;
00416 body += inc->description();
00417 body += CR;
00418 }
00419
00420
return body;
00421 }