2 This file is part of KSendEmail. Some of the code has been taken from KMail (kmkernel.cpp)
3 and akonadi (control.h)
4 Copyright (c) 2008 Pradeepto Bhattacharya <pradeepto@kde.org>
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
22 #include "mailerservice.h"
24 #include <QtCore/QObject>
25 #include <QtDBus/QtDBus>
26 #include <QtCore/QProcess>
29 #include <kdbusservicestarter.h>
30 #include <kmessagebox.h>
33 #include "kmailinterface.h"
35 MailerService::MailerService()
36 : mSuccess( false ), mEventLoop( 0 )
38 connect( QDBusConnection::sessionBus().interface(), SIGNAL( serviceOwnerChanged( QString
, QString
, QString
) ),
39 SLOT( serviceOwnerChanged( QString
, QString
, QString
) ) );
43 MailerService::~MailerService()
48 bool MailerService::start()
50 if ( QDBusConnection::sessionBus().interface()->isServiceRegistered( "org.kde.kmail" ) || mEventLoop
) {
54 //Check if Kontact is already running and if not ...
55 int result
= KDBusServiceStarter::self()->findServiceFor( "DBUS/Mailer", QString(),
56 &mError
, &mDBusService
);
59 result
= KDBusServiceStarter::self()->startServiceFor( "DBUS/Mailer", QString(),
60 &mError
, &mDBusService
);
62 const bool ok
= QProcess::startDetached( QLatin1String("kontact") );
64 kWarning() << "Error: unable to execute binary kontact";
72 mEventLoop
= new QEventLoop( this );
74 QTimer::singleShot( 10000, mEventLoop
, SLOT(quit()) );
76 mEventLoop
->deleteLater();
80 kWarning() << "Could not start Mailer Service!";
86 void MailerService::serviceOwnerChanged( const QString
& name
, const QString
& oldOwner
, const QString
& newOwner
)
89 if ( name
== "org.kde.kmail" && !newOwner
.isEmpty() && mEventLoop
&& mEventLoop
->isRunning() ) {
95 void MailerService::processArgs( KCmdLineArgs
*args
)
97 QString to
, cc
, bcc
, subj
, body
;
98 QStringList customHeaders
;
100 QStringList attachURLs
;
102 bool calledWithSession
= false; // for ignoring '-session foo'
104 if (args
->isSet("subject"))
106 subj
= args
->getOption("subject");
107 // if kmail is called with 'kmail -session abc' then this doesn't mean
108 // that the user wants to send a message with subject "ession" but
109 // (most likely) that the user clicked on KMail's system tray applet
110 // which results in KMKernel::raise() calling "kmail kmail newInstance"
111 // via D-Bus which apparently executes the application with the original
112 // command line arguments and those include "-session ..." if
113 // kmail/kontact was restored by session management
114 if ( subj
== "ession" ) {
116 calledWithSession
= true;
122 if (args
->isSet("cc"))
125 cc
= args
->getOption("cc");
128 if (args
->isSet("bcc"))
131 bcc
= args
->getOption("bcc");
134 if (args
->isSet("msg"))
137 messageFile
.setPath( args
->getOption("msg") );
140 if (args
->isSet("body"))
143 body
= args
->getOption("body");
146 const QStringList attachList
= args
->getOptionList("attach");
147 if (!attachList
.isEmpty())
150 for ( QStringList::ConstIterator it
= attachList
.constBegin() ; it
!= attachList
.constEnd() ; ++it
)
152 if ( !(*it
).isEmpty() )
156 if ( url
.protocol().isEmpty() )
158 const QString newUrl
= QDir::currentPath () + QDir::separator () + url
.fileName();
159 attachURLs
.append( newUrl
);
162 attachURLs
.append( *it
);
167 customHeaders
= args
->getOptionList("header");
169 if (args
->isSet("composer"))
173 if ( !calledWithSession
) {
174 // only read additional command line arguments if kmail/kontact is
175 // not called with "-session foo"
176 for(int i
= 0; i
< args
->count(); i
++)
178 if (args
->arg(i
).startsWith(QLatin1String("mailto:"), Qt::CaseInsensitive
))
179 to
+= args
->url(i
).path() + ", ";
181 QString tmpArg
= args
->arg(i
);
183 if (url
.isValid() && !url
.protocol().isEmpty())
184 attachURLs
.append( url
.url() );
190 if ( !to
.isEmpty() ) {
191 // cut off the superfluous trailing ", "
192 to
.truncate( to
.length() - 2 );
196 if ( !calledWithSession
)
200 QDBusInterface
kmailObj( "org.kde.kmail", "/KMail", "org.kde.kmail.kmail" );
202 QList
<QVariant
> messages
;
203 messages
<< to
<< cc
<< bcc
<< subj
<< body
<< false << messageFile
.url() << attachURLs
<< customHeaders
;
204 QDBusReply
<int> composerDbusPath
= kmailObj
.callWithArgumentList(QDBus::AutoDetect
, "openComposer", messages
);
206 if ( !composerDbusPath
.isValid() ) {
207 KMessageBox::error( 0, i18n( "Cannot connect to email service." ) );
210 KMessageBox::error( 0, i18n( "Unable to find or start email service." ) );