korganizer

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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 #include <libkcal/incidenceformatter.h>
00040 
00041 #include "version.h"
00042 #include "koprefs.h"
00043 
00044 #include "komailclient.h"
00045 
00046 KOMailClient::KOMailClient()
00047 {
00048 }
00049 
00050 KOMailClient::~KOMailClient()
00051 {
00052 }
00053 
00054 bool KOMailClient::mailAttendees(IncidenceBase *incidence,const QString &attachment)
00055 {
00056   Attendee::List attendees = incidence->attendees();
00057   if (attendees.count() == 0) return false;
00058 
00059   const QString from = incidence->organizer().fullName();
00060   const QString organizerEmail = incidence->organizer().email();
00061   QStringList toList;
00062   for(uint i=0; i<attendees.count();++i) {
00063     const QString email = (*attendees.at(i))->email();
00064     // In case we (as one of our identities) are the organizer we are sending this
00065     // mail. We could also have added ourselves as an attendee, in which case we
00066     // don't want to send ourselves a notification mail.
00067     if( organizerEmail !=  email )
00068       toList << email;
00069   }
00070   if( toList.count() == 0 )
00071     // Not really to be called a groupware meeting, eh
00072     return false;
00073   QString to = toList.join( ", " );
00074 
00075   QString subject;
00076   if(incidence->type()!="FreeBusy") {
00077     Incidence *inc = static_cast<Incidence *>(incidence);
00078     subject = inc->summary();
00079   } else {
00080     subject = "Free Busy Object";
00081   }
00082 
00083   QString body = IncidenceFormatter::mailBodyString(incidence);
00084 
00085   bool bcc = KOPrefs::instance()->mBcc;
00086 
00087   return send(from,to,subject,body,bcc,attachment);
00088 }
00089 
00090 bool KOMailClient::mailOrganizer(IncidenceBase *incidence,const QString &attachment, const QString &sub)
00091 {
00092   QString to = incidence->organizer().fullName();
00093 
00094   QString from = KOPrefs::instance()->email();
00095 
00096   QString subject = sub;
00097   if(incidence->type()!="FreeBusy") {
00098     Incidence *inc = static_cast<Incidence *>(incidence);
00099     if ( subject.isEmpty() )
00100       subject = inc->summary();
00101   } else {
00102     subject = "Free Busy Message";
00103   }
00104 
00105   QString body = IncidenceFormatter::mailBodyString(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 = IncidenceFormatter::mailBodyString(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         const int newline = attachment.find('\n',idx);
00201         meth = attachment.mid(idx, newline - idx - 1);
00202         meth = meth.lower().stripWhiteSpace();
00203       } else {
00204         meth = "publish";
00205       }
00206       if (!kMailOpenComposer(to,"",bcc ? from : "",subject,body,0,"cal.ics","7bit",
00207                              attachment.utf8(),"text","calendar","method",meth,
00208                              "attachment","utf-8")) return false;
00209     }
00210   }
00211   return true;
00212 }
00213 
00214 int KOMailClient::kMailOpenComposer(const QString& arg0,const QString& arg1,
00215   const QString& arg2,const QString& arg3,const QString& arg4,int arg5,
00216   const KURL& arg6)
00217 {
00218   //kdDebug(5850) << "KOMailClient::kMailOpenComposer( "
00219   //  << arg0 << " , " << arg1 << arg2 << " , " << arg3
00220   //  << arg4 << " , " << arg5 << " , " << arg6 << " )" << endl;
00221   int result = 0;
00222 
00223   QByteArray data, replyData;
00224   QCString replyType;
00225   QDataStream arg( data, IO_WriteOnly );
00226   arg << arg0;
00227   arg << arg1;
00228   arg << arg2;
00229   arg << arg3;
00230   arg << arg4;
00231   arg << arg5;
00232   arg << arg6;
00233 #if KDE_IS_VERSION( 3, 2, 90 )
00234   kapp->updateRemoteUserTimestamp( "kmail" );
00235 #endif
00236   if (kapp->dcopClient()->call("kmail","KMailIface","openComposer(QString,QString,QString,QString,QString,int,KURL)", data, replyType, replyData ) ) {
00237     if ( replyType == "int" ) {
00238       QDataStream _reply_stream( replyData, IO_ReadOnly );
00239       _reply_stream >> result;
00240     } else {
00241       kdDebug(5850) << "kMailOpenComposer() call failed." << endl;
00242     }
00243   } else {
00244     kdDebug(5850) << "kMailOpenComposer() call failed." << endl;
00245   }
00246   return result;
00247 }
00248 
00249 int KOMailClient::kMailOpenComposer( const QString& arg0, const QString& arg1,
00250                                      const QString& arg2, const QString& arg3,
00251                                      const QString& arg4, int arg5, const QString& arg6,
00252                                      const QCString& arg7, const QCString& arg8,
00253                                      const QCString& arg9, const QCString& arg10,
00254                                      const QCString& arg11, const QString& arg12,
00255                                      const QCString& arg13, const QCString& arg14 )
00256 {
00257     //kdDebug(5850) << "KOMailClient::kMailOpenComposer( "
00258     //    << arg0 << " , " << arg1 << arg2 << " , " << arg3
00259     //   << arg4 << " , " << arg5 << " , " << arg6
00260     //    << arg7 << " , " << arg8 << " , " << arg9
00261     //    << arg10<< " , " << arg11<< " , " << arg12
00262     //    << arg13<< " , " << arg14<< " )" << endl;
00263 
00264     int result = 0;
00265 
00266     QByteArray data, replyData;
00267     QCString replyType;
00268     QDataStream arg( data, IO_WriteOnly );
00269     arg << arg0;
00270     arg << arg1;
00271     arg << arg2;
00272     arg << arg3;
00273     arg << arg4;
00274     arg << arg5;
00275     arg << arg6;
00276     arg << arg7;
00277     arg << arg8;
00278     arg << arg9;
00279     arg << arg10;
00280     arg << arg11;
00281     arg << arg12;
00282     arg << arg13;
00283     arg << arg14;
00284 #if KDE_IS_VERSION( 3, 2, 90 )
00285     kapp->updateRemoteUserTimestamp("kmail");
00286 #endif
00287     if ( kapp->dcopClient()->call("kmail","KMailIface",
00288           "openComposer(QString,QString,QString,QString,QString,int,QString,QCString,QCString,QCString,QCString,QCString,QString,QCString,QCString)", data, replyType, replyData ) ) {
00289         if ( replyType == "int" ) {
00290             QDataStream _reply_stream( replyData, IO_ReadOnly );
00291             _reply_stream >> result;
00292         } else {
00293             kdDebug(5850) << "kMailOpenComposer() call failed." << endl;
00294         }
00295     } else {
00296         kdDebug(5850) << "kMailOpenComposer() call failed." << endl;
00297     }
00298     return result;
00299 }
00300 
00301 
KDE Home | KDE Accessibility Home | Description of Access Keys