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