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