00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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
00069
00070
00071 if( organizerEmail != email )
00072 toList << email;
00073 }
00074 if( toList.count() == 0 )
00075
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;
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';
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
00226
00227
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
00265
00266
00267
00268
00269
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