korganizer Library API Documentation

komailclient.cpp

00001 /*
00002     This file is part of KOrganizer.
00003     Copyright (c) 1998 Barry D Benowitz
00004     Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019 
00020     As a special exception, permission is given to link this program
00021     with any edition of Qt, and distribute the resulting executable,
00022     without including the source code for Qt in the source distribution.
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 <libkdepim/email.h>
00041 
00042 #include "version.h"
00043 #include "koprefs.h"
00044 
00045 #include "komailclient.h"
00046 
00047 KOMailClient::KOMailClient()
00048 {
00049 }
00050 
00051 KOMailClient::~KOMailClient()
00052 {
00053 }
00054 
00055 bool KOMailClient::mailAttendees(IncidenceBase *incidence,const QString &attachment)
00056 {
00057   Attendee::List attendees = incidence->attendees();
00058   if (attendees.count() == 0) return false;
00059 
00060   const QString from = incidence->organizer().fullName();
00061   const QString organizerEmail = incidence->organizer().email();
00062   QStringList toList;
00063   for(uint i=0; i<attendees.count();++i) {
00064     const QString email = (*attendees.at(i))->email();
00065     // In case we (as one of our identities) are the organizer we are sending this
00066     // mail. We could also have added ourselves as an attendee, in which case we 
00067     // don't want to send ourselves a notification mail.
00068     if( organizerEmail !=  email )
00069       toList << email;
00070   }
00071   if( toList.count() == 0 )
00072     // Not really to be called a groupware meeting, eh
00073     return false;
00074   QString to = toList.join( ", " );
00075 
00076   QString subject;
00077   if(incidence->type()!="FreeBusy") {
00078     Incidence *inc = static_cast<Incidence *>(incidence);
00079     subject = inc->summary();
00080   } else {
00081     subject = "Free Busy Object";
00082   }
00083 
00084   QString body = createBody(incidence);
00085 
00086   bool bcc = KOPrefs::instance()->mBcc;
00087 
00088   return send(from,to,subject,body,bcc,attachment);
00089 }
00090 
00091 bool KOMailClient::mailOrganizer(IncidenceBase *incidence,const QString &attachment)
00092 {
00093   QString to = incidence->organizer().fullName();
00094 
00095   QString from = KOPrefs::instance()->email();
00096 
00097   QString subject;
00098   if(incidence->type()!="FreeBusy") {
00099     Incidence *inc = static_cast<Incidence *>(incidence);
00100     subject = inc->summary();
00101   } else {
00102     subject = "Free Busy Message";
00103   }
00104 
00105   QString body = createBody(incidence);
00106 
00107   bool bcc = KOPrefs::instance()->mBcc;
00108 
00109   return send(from,to,subject,body,bcc,attachment);
00110 }
00111 
00112 bool KOMailClient::mailTo(IncidenceBase *incidence,const QString &recipients,
00113                           const QString &attachment)
00114 {
00115   QString from = KOPrefs::instance()->email();
00116   QString subject;
00117   if(incidence->type()!="FreeBusy") {
00118     Incidence *inc = static_cast<Incidence *>(incidence);
00119     subject = inc->summary();
00120   } else {
00121     subject = "Free Busy Message";
00122   }
00123   QString body = createBody(incidence);
00124   bool bcc = KOPrefs::instance()->mBcc;
00125   kdDebug () << "KOMailClient::mailTo " << recipients << endl;
00126   return send(from,recipients,subject,body,bcc,attachment);
00127 }
00128 
00129 bool KOMailClient::send(const QString &from,const QString &to,
00130                         const QString &subject,const QString &body,bool bcc,
00131                         const QString &attachment)
00132 {
00133   kdDebug(5850) << "KOMailClient::sendMail():\nFrom: " << from << "\nTo: " << to
00134             << "\nSubject: " << subject << "\nBody: \n" << body
00135             << "\nAttachment:\n" << attachment << endl;
00136 
00137   if (KOPrefs::instance()->mMailClient == KOPrefs::MailClientSendmail) {
00138     bool needHeaders = true;
00139 
00140     QString command = KStandardDirs::findExe(QString::fromLatin1("sendmail"),
00141         QString::fromLatin1("/sbin:/usr/sbin:/usr/lib"));
00142     if (!command.isNull()) command += QString::fromLatin1(" -oi -t");
00143     else {
00144       command = KStandardDirs::findExe(QString::fromLatin1("mail"));
00145       if (command.isNull()) return false; // give up
00146 
00147       command.append(QString::fromLatin1(" -s "));
00148       command.append(KProcess::quote(subject));
00149 
00150       if (bcc) {
00151         command.append(QString::fromLatin1(" -b "));
00152         command.append(KProcess::quote(from));
00153       }
00154 
00155       command.append(" ");
00156       command.append(KProcess::quote(to));
00157 
00158       needHeaders = false;
00159     }
00160 
00161     FILE * fd = popen(command.local8Bit(),"w");
00162     if (!fd)
00163     {
00164       kdError() << "Unable to open a pipe to " << command << endl;
00165       return false;
00166     }
00167 
00168     QString textComplete;
00169     if (needHeaders)
00170     {
00171       textComplete += QString::fromLatin1("From: ") + from + '\n';
00172       textComplete += QString::fromLatin1("To: ") + to + '\n';
00173       if (bcc) textComplete += QString::fromLatin1("Bcc: ") + from + '\n';
00174       textComplete += QString::fromLatin1("Subject: ") + subject + '\n';
00175       textComplete += QString::fromLatin1("X-Mailer: KOrganizer") + korgVersion + '\n';
00176     }
00177     textComplete += '\n'; // end of headers
00178     textComplete += body;
00179     textComplete += '\n';
00180     textComplete += attachment;
00181 
00182     fwrite(textComplete.local8Bit(),textComplete.length(),1,fd);
00183 
00184     pclose(fd);
00185   } else {
00186     if (!kapp->dcopClient()->isApplicationRegistered("kmail")) {
00187                         if (KApplication::startServiceByDesktopName("kmail")) {
00188         KMessageBox::error(0,i18n("No running instance of KMail found."));
00189         return false;
00190                         }
00191     }
00192 
00193     if (attachment.isEmpty()) {
00194       if (!kMailOpenComposer(to,"",bcc ? from : "",subject,body,0,KURL())) return false;
00195     } else {
00196       QString meth;
00197       int idx = attachment.find("METHOD");
00198       if (idx>=0) {
00199         idx = attachment.find(':',idx)+1;
00200         meth = attachment.mid(idx,attachment.find('\n',idx)-idx);
00201         meth = meth.lower();
00202       } else {
00203         meth = "publish";
00204       }
00205       if (!kMailOpenComposer(to,"",bcc ? from : "",subject,body,0,"cal.ics","7bit",
00206                              attachment.utf8(),"text","calendar","method",meth,
00207                              "attachment","utf-8")) return false;
00208     }
00209   }
00210   return true;
00211 }
00212 
00213 int KOMailClient::kMailOpenComposer(const QString& arg0,const QString& arg1,
00214   const QString& arg2,const QString& arg3,const QString& arg4,int arg5,
00215   const KURL& arg6)
00216 {
00217   //kdDebug(5850) << "KOMailClient::kMailOpenComposer( "
00218   //  << arg0 << " , " << arg1 << arg2 << " , " << arg3
00219   //  << arg4 << " , " << arg5 << " , " << arg6 << " )" << endl;
00220   int result = 0;
00221 
00222   QByteArray data, replyData;
00223   QCString replyType;
00224   QDataStream arg( data, IO_WriteOnly );
00225   arg << arg0;
00226   arg << arg1;
00227   arg << arg2;
00228   arg << arg3;
00229   arg << arg4;
00230   arg << arg5;
00231   arg << arg6;
00232 #if KDE_IS_VERSION( 3, 2, 90 )
00233   kapp->updateRemoteUserTimestamp( "kmail" );
00234 #endif
00235   if (kapp->dcopClient()->call("kmail","KMailIface","openComposer(QString,QString,QString,QString,QString,int,KURL)", data, replyType, replyData ) ) {
00236     if ( replyType == "int" ) {
00237       QDataStream _reply_stream( replyData, IO_ReadOnly );
00238       _reply_stream >> result;
00239     } else {
00240       kdDebug(5850) << "kMailOpenComposer() call failed." << endl;
00241     }
00242   } else {
00243     kdDebug(5850) << "kMailOpenComposer() call failed." << endl;
00244   }
00245   return result;
00246 }
00247 
00248 int KOMailClient::kMailOpenComposer( const QString& arg0, const QString& arg1,
00249                                      const QString& arg2, const QString& arg3,
00250                                      const QString& arg4, int arg5, const QString& arg6,
00251                                      const QCString& arg7, const QCString& arg8,
00252                                      const QCString& arg9, const QCString& arg10,
00253                                      const QCString& arg11, const QString& arg12,
00254                                      const QCString& arg13, const QCString& arg14 )
00255 {
00256     //kdDebug(5850) << "KOMailClient::kMailOpenComposer( "
00257     //    << arg0 << " , " << arg1 << arg2 << " , " << arg3
00258     //   << arg4 << " , " << arg5 << " , " << arg6
00259     //    << arg7 << " , " << arg8 << " , " << arg9
00260     //    << arg10<< " , " << arg11<< " , " << arg12
00261     //    << arg13<< " , " << arg14<< " )" << endl;
00262 
00263     int result = 0;
00264 
00265     QByteArray data, replyData;
00266     QCString replyType;
00267     QDataStream arg( data, IO_WriteOnly );
00268     arg << arg0;
00269     arg << arg1;
00270     arg << arg2;
00271     arg << arg3;
00272     arg << arg4;
00273     arg << arg5;
00274     arg << arg6;
00275     arg << arg7;
00276     arg << arg8;
00277     arg << arg9;
00278     arg << arg10;
00279     arg << arg11;
00280     arg << arg12;
00281     arg << arg13;
00282     arg << arg14;
00283 #if KDE_IS_VERSION( 3, 2, 90 )
00284     kapp->updateRemoteUserTimestamp("kmail");
00285 #endif
00286     if ( kapp->dcopClient()->call("kmail","KMailIface",
00287           "openComposer(QString,QString,QString,QString,QString,int,QString,QCString,QCString,QCString,QCString,QCString,QString,QCString,QCString)", data, replyType, replyData ) ) {
00288         if ( replyType == "int" ) {
00289             QDataStream _reply_stream( replyData, IO_ReadOnly );
00290             _reply_stream >> result;
00291         } else {
00292             kdDebug(5850) << "kMailOpenComposer() call failed." << endl;
00293         }
00294     } else {
00295         kdDebug(5850) << "kMailOpenComposer() call failed." << endl;
00296     }
00297     return result;
00298 }
00299 
00300 
00301 QString KOMailClient::createBody(IncidenceBase *incidence)
00302 {
00303   QString CR = ("\n");
00304 
00305   QString body;
00306 
00307   // mailbody for Event
00308   if (incidence->type()=="Event") {
00309     Event *selectedEvent = static_cast<Event *>(incidence);
00310     QString recurrence[]= {i18n("no recurrence", "None"),
00311       i18n("Minutely"), i18n("Hourly"), i18n("Daily"),
00312       i18n("Weekly"), i18n("Monthly Same Day"), i18n("Monthly Same Position"),
00313       i18n("Yearly"), i18n("Yearly"), i18n("Yearly")};
00314 
00315     if (!selectedEvent->organizer().isEmpty()) {
00316       body += i18n("Organizer: %1").arg(selectedEvent->organizer().fullName());
00317       body += CR;
00318     }
00319     body += i18n("Summary: %1").arg(selectedEvent->summary());
00320     body += CR;
00321     if (!selectedEvent->location().isEmpty()) {
00322       body += i18n("Location: %1").arg(selectedEvent->location());
00323       body += CR;
00324     }
00325     body += i18n("Start Date: %1").arg(selectedEvent->dtStartDateStr());
00326     body += CR;
00327     if (!selectedEvent->doesFloat()) {
00328       body += i18n("Start Time: %1").arg(selectedEvent->dtStartTimeStr());
00329       body += CR;
00330     }
00331     if ( selectedEvent->dtStart()!=selectedEvent->dtEnd() ) {
00332       body += i18n("End Date: %1").arg(selectedEvent->dtEndDateStr());
00333       body += CR;
00334     }
00335     if (!selectedEvent->doesFloat()) {
00336       body += i18n("End Time: %1").arg(selectedEvent->dtEndTimeStr());
00337       body += CR;
00338     }
00339     if (selectedEvent->doesRecur()) {
00340       body += i18n("Recurs: %1")
00341                .arg(recurrence[selectedEvent->recurrence()->doesRecur()]);
00342       body += CR;
00343 /* TODO: frequency
00344       body += i18n("Frequency: %1")
00345                .arg(recurrence[selectedEvent->recurrence()->frequency()]);
00346       body += CR;
00347 */
00348       if (selectedEvent->recurrence()->duration() > 0 ) {
00349         body += i18n ("Repeats %1 times")
00350                  .arg(QString::number(selectedEvent->recurrence()->duration()));
00351         body += CR;
00352       } else {
00353         if (selectedEvent->recurrence()->duration() != -1) {
00354 //          body += i18n("Repeat until: %1")
00355           body += i18n("End Date: %1")
00356                    .arg(selectedEvent->recurrence()->endDateStr());
00357           body += CR;
00358         } else {
00359           body += i18n("Repeats forever");
00360           body += CR;
00361         }
00362       }
00363     }
00364     QString details = selectedEvent->description();
00365     if (!details.isEmpty()) {
00366       body += i18n("Details:");
00367       body += CR;
00368       body += details;
00369       body += CR;
00370     }
00371   }
00372 
00373   // mailbody for Todo
00374   if (incidence->type()=="Todo") {
00375     Todo *selectedEvent = static_cast<Todo *>(incidence);
00376     if (!selectedEvent->organizer().isEmpty()) {
00377       body += i18n("Organizer: %1").arg(selectedEvent->organizer().fullName());
00378       body += CR;
00379     }
00380     body += i18n("Summary: %1").arg(selectedEvent->summary());
00381     body += CR;
00382     if (!selectedEvent->location().isEmpty()) {
00383       body += i18n("Location: %1").arg(selectedEvent->location());
00384       body += CR;
00385     }
00386     if (selectedEvent->hasStartDate()) {
00387       body += i18n("Start Date: %1").arg(selectedEvent->dtStartDateStr());
00388       body += CR;
00389       if (!selectedEvent->doesFloat()) {
00390         body += i18n("Start Time: %1").arg(selectedEvent->dtStartTimeStr());
00391         body += CR;
00392       }
00393     }
00394     if (selectedEvent->hasDueDate()) {
00395       body += i18n("Due Date: %1").arg(selectedEvent->dtDueDateStr());
00396       body += CR;
00397       if (!selectedEvent->doesFloat()) {
00398         body += i18n("Due Time: %1").arg(selectedEvent->dtDueTimeStr());
00399         body += CR;
00400       }
00401     }
00402     QString details = selectedEvent->description();
00403     if (!details.isEmpty()) {
00404       body += i18n("Details:");
00405       body += CR;
00406       body += details;
00407       body += CR;
00408     }
00409   }
00410 
00411   // mailbody for FreeBusy
00412   if(incidence->type()=="FreeBusy") {
00413     body = i18n("This is a Free Busy Object");
00414   }
00415 
00416   // mailbody for Journal
00417   if(incidence->type()=="Journal") {
00418     Incidence *inc = static_cast<Incidence *>(incidence);
00419     body = inc->summary();
00420     body += CR;
00421     body += inc->description();
00422     body += CR;
00423   }
00424 
00425   return body;
00426 }
KDE Logo
This file is part of the documentation for korganizer Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Jul 25 11:21:00 2007 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003