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 <libkpimidentities/identity.h>
00038 #include <libkpimidentities/identitymanager.h>
00039 
00040 #include <libkcal/event.h>
00041 #include <libkcal/todo.h>
00042 #include <libkcal/incidenceformatter.h>
00043 
00044 #include "version.h"
00045 #include "koprefs.h"
00046 #include "kocore.h"
00047 
00048 #include "komailclient.h"
00049 
00050 KOMailClient::KOMailClient()
00051 {
00052 }
00053 
00054 KOMailClient::~KOMailClient()
00055 {
00056 }
00057 
00058 bool KOMailClient::mailAttendees(IncidenceBase *incidence,const QString &attachment)
00059 {
00060   Attendee::List attendees = incidence->attendees();
00061   if (attendees.count() == 0) return false;
00062 
00063   const QString from = incidence->organizer().fullName();
00064   const QString organizerEmail = incidence->organizer().email();
00065   QStringList toList;
00066   for(uint i=0; i<attendees.count();++i) {
00067     const QString email = (*attendees.at(i))->email();
00068     // In case we (as one of our identities) are the organizer we are sending this
00069     // mail. We could also have added ourselves as an attendee, in which case we
00070     // don't want to send ourselves a notification mail.
00071     if( organizerEmail !=  email )
00072       toList << email;
00073   }
00074   if( toList.count() == 0 )
00075     // Not really to be called a groupware meeting, eh
00076     return false;
00077   QString to = toList.join( ", " );
00078 
00079   QString subject;
00080   if(incidence->type()!="FreeBusy") {
00081     Incidence *inc = static_cast<Incidence *>(incidence);
00082     subject = inc->summary();
00083   } else {
00084     subject = "Free Busy Object";
00085   }
00086 
00087   QString body = IncidenceFormatter::mailBodyString(incidence);
00088 
00089   bool bcc = KOPrefs::instance()->mBcc;
00090 
00091   return send(from,to,subject,body,bcc,attachment);
00092 }
00093 
00094 bool KOMailClient::mailOrganizer(IncidenceBase *incidence,const QString &attachment, const QString &sub)
00095 {
00096   QString to = incidence->organizer().fullName();
00097 
00098   QString from = KOPrefs::instance()->email();
00099 
00100   QString subject = sub;
00101   if(incidence->type()!="FreeBusy") {
00102     Incidence *inc = static_cast<Incidence *>(incidence);
00103     if ( subject.isEmpty() )
00104       subject = inc->summary();
00105   } else {
00106     subject = "Free Busy Message";
00107   }
00108 
00109   QString body = IncidenceFormatter::mailBodyString(incidence);
00110 
00111   bool bcc = KOPrefs::instance()->mBcc;
00112 
00113   return send(from,to,subject,body,bcc,attachment);
00114 }
00115 
00116 bool KOMailClient::mailTo(IncidenceBase *incidence,const QString &recipients,
00117                           const QString &attachment)
00118 {
00119   QString from = KOPrefs::instance()->email();
00120   QString subject;
00121   if(incidence->type()!="FreeBusy") {
00122     Incidence *inc = static_cast<Incidence *>(incidence);
00123     subject = inc->summary();
00124   } else {
00125     subject = "Free Busy Message";
00126   }
00127   QString body = IncidenceFormatter::mailBodyString(incidence);
00128   bool bcc = KOPrefs::instance()->mBcc;
00129   kdDebug () << "KOMailClient::mailTo " << recipients << endl;
00130   return send(from,recipients,subject,body,bcc,attachment);
00131 }
00132 
00133 bool KOMailClient::send(const QString &from,const QString &to,
00134                         const QString &subject,const QString &body,bool bcc,
00135                         const QString &attachment)
00136 {
00137   kdDebug(5850) << "KOMailClient::sendMail():\nFrom: " << from << "\nTo: " << to
00138             << "\nSubject: " << subject << "\nBody: \n" << body
00139             << "\nAttachment:\n" << attachment << endl;
00140 
00141   if (KOPrefs::instance()->mMailClient == KOPrefs::MailClientSendmail) {
00142     bool needHeaders = true;
00143 
00144     QString command = KStandardDirs::findExe(QString::fromLatin1("sendmail"),
00145         QString::fromLatin1("/sbin:/usr/sbin:/usr/lib"));
00146     if (!command.isNull()) command += QString::fromLatin1(" -oi -t");
00147     else {
00148       command = KStandardDirs::findExe(QString::fromLatin1("mail"));
00149       if (command.isNull()) return false; // give up
00150 
00151       command.append(QString::fromLatin1(" -s "));
00152       command.append(KProcess::quote(subject));
00153 
00154       if (bcc) {
00155         command.append(QString::fromLatin1(" -b "));
00156         command.append(KProcess::quote(from));
00157       }
00158 
00159       command.append(" ");
00160       command.append(KProcess::quote(to));
00161 
00162       needHeaders = false;
00163     }
00164 
00165     FILE * fd = popen(command.local8Bit(),"w");
00166     if (!fd)
00167     {
00168       kdError() << "Unable to open a pipe to " << command << endl;
00169       return false;
00170     }
00171 
00172     QString textComplete;
00173     if (needHeaders)
00174     {
00175       textComplete += QString::fromLatin1("From: ") + from + '\n';
00176       textComplete += QString::fromLatin1("To: ") + to + '\n';
00177       if (bcc) textComplete += QString::fromLatin1("Bcc: ") + from + '\n';
00178       textComplete += QString::fromLatin1("Subject: ") + subject + '\n';
00179       textComplete += QString::fromLatin1("X-Mailer: KOrganizer") + korgVersion + '\n';
00180     }
00181     textComplete += '\n'; // end of headers
00182     textComplete += body;
00183     textComplete += '\n';
00184     textComplete += attachment;
00185 
00186     fwrite(textComplete.local8Bit(),textComplete.length(),1,fd);
00187 
00188     pclose(fd);
00189   } else {
00190     if (!kapp->dcopClient()->isApplicationRegistered("kmail")) {
00191                         if (KApplication::startServiceByDesktopName("kmail")) {
00192         KMessageBox::error(0,i18n("No running instance of KMail found."));
00193         return false;
00194                         }
00195     }
00196 
00197     if (attachment.isEmpty()) {
00198       if (!kMailOpenComposer(to,"",bcc ? from : "",subject,body,0,KURL())) return false;
00199     } else {
00200       QString meth;
00201       int idx = attachment.find("METHOD");
00202       if (idx>=0) {
00203         idx = attachment.find(':',idx)+1;
00204         const int newline = attachment.find('\n',idx);
00205         meth = attachment.mid(idx, newline - idx - 1);
00206         meth = meth.lower().stripWhiteSpace();
00207       } else {
00208         meth = "publish";
00209       }
00210       if (!kMailOpenComposer(to,"",bcc ? from : "",subject,body,0,"cal.ics","7bit",
00211                              attachment.utf8(),"text","calendar","method",meth,
00212                              "attachment","utf-8",
00213                              KOCore::self()->identityManager()->identityForAddress( from ).uoid())) {
00214         return false;
00215       }
00216     }
00217   }
00218   return true;
00219 }
00220 
00221 int KOMailClient::kMailOpenComposer(const QString& arg0,const QString& arg1,
00222   const QString& arg2,const QString& arg3,const QString& arg4,int arg5,
00223   const KURL& arg6)
00224 {
00225   //kdDebug(5850) << "KOMailClient::kMailOpenComposer( "
00226   //  << arg0 << " , " << arg1 << arg2 << " , " << arg3
00227   //  << arg4 << " , " << arg5 << " , " << arg6 << " )" << endl;
00228   int result = 0;
00229 
00230   QByteArray data, replyData;
00231   QCString replyType;
00232   QDataStream arg( data, IO_WriteOnly );
00233   arg << arg0;
00234   arg << arg1;
00235   arg << arg2;
00236   arg << arg3;
00237   arg << arg4;
00238   arg << arg5;
00239   arg << arg6;
00240 #if KDE_IS_VERSION( 3, 2, 90 )
00241   kapp->updateRemoteUserTimestamp( "kmail" );
00242 #endif
00243   if (kapp->dcopClient()->call("kmail","KMailIface","openComposer(QString,QString,QString,QString,QString,int,KURL)", data, replyType, replyData ) ) {
00244     if ( replyType == "int" ) {
00245       QDataStream _reply_stream( replyData, IO_ReadOnly );
00246       _reply_stream >> result;
00247     } else {
00248       kdDebug(5850) << "kMailOpenComposer() call failed." << endl;
00249     }
00250   } else {
00251     kdDebug(5850) << "kMailOpenComposer() call failed." << endl;
00252   }
00253   return result;
00254 }
00255 
00256 int KOMailClient::kMailOpenComposer( const QString& arg0, const QString& arg1,
00257                                      const QString& arg2, const QString& arg3,
00258                                      const QString& arg4, int arg5, const QString& arg6,
00259                                      const QCString& arg7, const QCString& arg8,
00260                                      const QCString& arg9, const QCString& arg10,
00261                                      const QCString& arg11, const QString& arg12,
00262                                      const QCString& arg13, const QCString& arg14, uint identity )
00263 {
00264     //kdDebug(5850) << "KOMailClient::kMailOpenComposer( "
00265     //    << arg0 << " , " << arg1 << arg2 << " , " << arg3
00266     //   << arg4 << " , " << arg5 << " , " << arg6
00267     //    << arg7 << " , " << arg8 << " , " << arg9
00268     //    << arg10<< " , " << arg11<< " , " << arg12
00269     //    << arg13<< " , " << arg14<< " )" << endl;
00270 
00271     int result = 0;
00272 
00273     QByteArray data, replyData;
00274     QCString replyType;
00275     QDataStream arg( data, IO_WriteOnly );
00276     arg << arg0;
00277     arg << arg1;
00278     arg << arg2;
00279     arg << arg3;
00280     arg << arg4;
00281     arg << arg5;
00282     arg << arg6;
00283     arg << arg7;
00284     arg << arg8;
00285     arg << arg9;
00286     arg << arg10;
00287     arg << arg11;
00288     arg << arg12;
00289     arg << arg13;
00290     arg << arg14;
00291     arg << identity;
00292 #if KDE_IS_VERSION( 3, 2, 90 )
00293     kapp->updateRemoteUserTimestamp("kmail");
00294 #endif
00295     if ( kapp->dcopClient()->call("kmail","KMailIface",
00296           "openComposer(QString,QString,QString,QString,QString,int,QString,QCString,QCString,QCString,QCString,QCString,QString,QCString,QCString,uint)", data, replyType, replyData ) ) {
00297         if ( replyType == "int" ) {
00298             QDataStream _reply_stream( replyData, IO_ReadOnly );
00299             _reply_stream >> result;
00300         } else {
00301             kdDebug(5850) << "kMailOpenComposer() call failed." << endl;
00302         }
00303     } else {
00304         kdDebug(5850) << "kMailOpenComposer() call failed." << endl;
00305     }
00306     return result;
00307 }
00308 
00309 
KDE Home | KDE Accessibility Home | Description of Access Keys