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)
00091 {
00092   QString to = incidence->organizer().fullName();
00093 
00094   QString from = KOPrefs::instance()->email();
00095 
00096   QString subject;
00097   if(incidence->type()!="FreeBusy") {
00098     Incidence *inc = static_cast<Incidence *>(incidence);
00099     subject = inc->summary();
00100   } else {
00101     subject = "Free Busy Message";
00102   }
00103 
00104   QString body = IncidenceFormatter::mailBodyString(incidence);
00105 
00106   bool bcc = KOPrefs::instance()->mBcc;
00107 
00108   return send(from,to,subject,body,bcc,attachment);
00109 }
00110 
00111 bool KOMailClient::mailTo(IncidenceBase *incidence,const QString &recipients,
00112                           const QString &attachment)
00113 {
00114   QString from = KOPrefs::instance()->email();
00115   QString subject;
00116   if(incidence->type()!="FreeBusy") {
00117     Incidence *inc = static_cast<Incidence *>(incidence);
00118     subject = inc->summary();
00119   } else {
00120     subject = "Free Busy Message";
00121   }
00122   QString body = IncidenceFormatter::mailBodyString(incidence);
00123   bool bcc = KOPrefs::instance()->mBcc;
00124   kdDebug () << "KOMailClient::mailTo " << recipients << endl;
00125   return send(from,recipients,subject,body,bcc,attachment);
00126 }
00127 
00128 bool KOMailClient::send(const QString &from,const QString &to,
00129                         const QString &subject,const QString &body,bool bcc,
00130                         const QString &attachment)
00131 {
00132   kdDebug(5850) << "KOMailClient::sendMail():\nFrom: " << from << "\nTo: " << to
00133             << "\nSubject: " << subject << "\nBody: \n" << body
00134             << "\nAttachment:\n" << attachment << endl;
00135 
00136   if (KOPrefs::instance()->mMailClient == KOPrefs::MailClientSendmail) {
00137     bool needHeaders = true;
00138 
00139     QString command = KStandardDirs::findExe(QString::fromLatin1("sendmail"),
00140         QString::fromLatin1("/sbin:/usr/sbin:/usr/lib"));
00141     if (!command.isNull()) command += QString::fromLatin1(" -oi -t");
00142     else {
00143       command = KStandardDirs::findExe(QString::fromLatin1("mail"));
00144       if (command.isNull()) return false; // give up
00145 
00146       command.append(QString::fromLatin1(" -s "));
00147       command.append(KProcess::quote(subject));
00148 
00149       if (bcc) {
00150         command.append(QString::fromLatin1(" -b "));
00151         command.append(KProcess::quote(from));
00152       }
00153 
00154       command.append(" ");
00155       command.append(KProcess::quote(to));
00156 
00157       needHeaders = false;
00158     }
00159 
00160     FILE * fd = popen(command.local8Bit(),"w");
00161     if (!fd)
00162     {
00163       kdError() << "Unable to open a pipe to " << command << endl;
00164       return false;
00165     }
00166 
00167     QString textComplete;
00168     if (needHeaders)
00169     {
00170       textComplete += QString::fromLatin1("From: ") + from + '\n';
00171       textComplete += QString::fromLatin1("To: ") + to + '\n';
00172       if (bcc) textComplete += QString::fromLatin1("Bcc: ") + from + '\n';
00173       textComplete += QString::fromLatin1("Subject: ") + subject + '\n';
00174       textComplete += QString::fromLatin1("X-Mailer: KOrganizer") + korgVersion + '\n';
00175     }
00176     textComplete += '\n'; // end of headers
00177     textComplete += body;
00178     textComplete += '\n';
00179     textComplete += attachment;
00180 
00181     fwrite(textComplete.local8Bit(),textComplete.length(),1,fd);
00182 
00183     pclose(fd);
00184   } else {
00185     if (!kapp->dcopClient()->isApplicationRegistered("kmail")) {
00186                         if (KApplication::startServiceByDesktopName("kmail")) {
00187         KMessageBox::error(0,i18n("No running instance of KMail found."));
00188         return false;
00189                         }
00190     }
00191 
00192     if (attachment.isEmpty()) {
00193       if (!kMailOpenComposer(to,"",bcc ? from : "",subject,body,0,KURL())) return false;
00194     } else {
00195       QString meth;
00196       int idx = attachment.find("METHOD");
00197       if (idx>=0) {
00198         idx = attachment.find(':',idx)+1;
00199         meth = attachment.mid(idx,attachment.find('\n',idx)-idx);
00200         meth = meth.lower();
00201       } else {
00202         meth = "publish";
00203       }
00204       if (!kMailOpenComposer(to,"",bcc ? from : "",subject,body,0,"cal.ics","7bit",
00205                              attachment.utf8(),"text","calendar","method",meth,
00206                              "attachment","utf-8")) return false;
00207     }
00208   }
00209   return true;
00210 }
00211 
00212 int KOMailClient::kMailOpenComposer(const QString& arg0,const QString& arg1,
00213   const QString& arg2,const QString& arg3,const QString& arg4,int arg5,
00214   const KURL& arg6)
00215 {
00216   //kdDebug(5850) << "KOMailClient::kMailOpenComposer( "
00217   //  << arg0 << " , " << arg1 << arg2 << " , " << arg3
00218   //  << arg4 << " , " << arg5 << " , " << arg6 << " )" << endl;
00219   int result = 0;
00220 
00221   QByteArray data, replyData;
00222   QCString replyType;
00223   QDataStream arg( data, IO_WriteOnly );
00224   arg << arg0;
00225   arg << arg1;
00226   arg << arg2;
00227   arg << arg3;
00228   arg << arg4;
00229   arg << arg5;
00230   arg << arg6;
00231 #if KDE_IS_VERSION( 3, 2, 90 )
00232   kapp->updateRemoteUserTimestamp( "kmail" );
00233 #endif
00234   if (kapp->dcopClient()->call("kmail","KMailIface","openComposer(QString,QString,QString,QString,QString,int,KURL)", data, replyType, replyData ) ) {
00235     if ( replyType == "int" ) {
00236       QDataStream _reply_stream( replyData, IO_ReadOnly );
00237       _reply_stream >> result;
00238     } else {
00239       kdDebug(5850) << "kMailOpenComposer() call failed." << endl;
00240     }
00241   } else {
00242     kdDebug(5850) << "kMailOpenComposer() call failed." << endl;
00243   }
00244   return result;
00245 }
00246 
00247 int KOMailClient::kMailOpenComposer( const QString& arg0, const QString& arg1,
00248                                      const QString& arg2, const QString& arg3,
00249                                      const QString& arg4, int arg5, const QString& arg6,
00250                                      const QCString& arg7, const QCString& arg8,
00251                                      const QCString& arg9, const QCString& arg10,
00252                                      const QCString& arg11, const QString& arg12,
00253                                      const QCString& arg13, const QCString& arg14 )
00254 {
00255     //kdDebug(5850) << "KOMailClient::kMailOpenComposer( "
00256     //    << arg0 << " , " << arg1 << arg2 << " , " << arg3
00257     //   << arg4 << " , " << arg5 << " , " << arg6
00258     //    << arg7 << " , " << arg8 << " , " << arg9
00259     //    << arg10<< " , " << arg11<< " , " << arg12
00260     //    << arg13<< " , " << arg14<< " )" << endl;
00261 
00262     int result = 0;
00263 
00264     QByteArray data, replyData;
00265     QCString replyType;
00266     QDataStream arg( data, IO_WriteOnly );
00267     arg << arg0;
00268     arg << arg1;
00269     arg << arg2;
00270     arg << arg3;
00271     arg << arg4;
00272     arg << arg5;
00273     arg << arg6;
00274     arg << arg7;
00275     arg << arg8;
00276     arg << arg9;
00277     arg << arg10;
00278     arg << arg11;
00279     arg << arg12;
00280     arg << arg13;
00281     arg << arg14;
00282 #if KDE_IS_VERSION( 3, 2, 90 )
00283     kapp->updateRemoteUserTimestamp("kmail");
00284 #endif
00285     if ( kapp->dcopClient()->call("kmail","KMailIface",
00286           "openComposer(QString,QString,QString,QString,QString,int,QString,QCString,QCString,QCString,QCString,QCString,QString,QCString,QCString)", data, replyType, replyData ) ) {
00287         if ( replyType == "int" ) {
00288             QDataStream _reply_stream( replyData, IO_ReadOnly );
00289             _reply_stream >> result;
00290         } else {
00291             kdDebug(5850) << "kMailOpenComposer() call failed." << endl;
00292         }
00293     } else {
00294         kdDebug(5850) << "kMailOpenComposer() call failed." << endl;
00295     }
00296     return result;
00297 }
00298 
00299 
KDE Home | KDE Accessibility Home | Description of Access Keys