Allow specializations to decide when re-indexing is necessary.
[kdepim.git] / ksendemail / mailerservice.cpp
blob625a4a0d2a3e6b1381cc425f5100596bbddbefd6
1 /*
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>
28 #include <kdebug.h>
29 #include <kdbusservicestarter.h>
30 #include <kmessagebox.h>
31 #include <kurl.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 ) ) );
40 start();
43 MailerService::~MailerService()
48 bool MailerService::start()
50 if ( QDBusConnection::sessionBus().interface()->isServiceRegistered( "org.kde.kmail" ) || mEventLoop ) {
51 mSuccess = true;
52 return mSuccess;
54 //Check if Kontact is already running and if not ...
55 int result = KDBusServiceStarter::self()->findServiceFor( "DBUS/Mailer", QString(),
56 &mError, &mDBusService );
57 if ( result != 0 ) {
58 // ... start Kontact
59 result = KDBusServiceStarter::self()->startServiceFor( "DBUS/Mailer", QString(),
60 &mError, &mDBusService );
61 if( result != 0 ) {
62 const bool ok = QProcess::startDetached( QLatin1String("kontact") );
63 if ( !ok ) {
64 kWarning() << "Error: unable to execute binary kontact";
65 return false;
67 } else {
68 return false;
72 mEventLoop = new QEventLoop( this );
73 // safety timeout
74 QTimer::singleShot( 10000, mEventLoop, SLOT(quit()) );
75 mEventLoop->exec();
76 mEventLoop->deleteLater();
77 mEventLoop = 0;
79 if ( !mSuccess ) {
80 kWarning() << "Could not start Mailer Service!";
83 return mSuccess;
86 void MailerService::serviceOwnerChanged( const QString & name, const QString & oldOwner, const QString & newOwner )
88 Q_UNUSED( oldOwner );
89 if ( name == "org.kde.kmail" && !newOwner.isEmpty() && mEventLoop && mEventLoop->isRunning() ) {
90 mEventLoop->quit();
91 mSuccess = true;
95 void MailerService::processArgs( KCmdLineArgs *args )
97 QString to, cc, bcc, subj, body;
98 QStringList customHeaders;
99 KUrl messageFile;
100 QStringList attachURLs;
101 bool mailto = false;
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" ) {
115 subj.clear();
116 calledWithSession = true;
118 else
119 mailto = true;
122 if (args->isSet("cc"))
124 mailto = true;
125 cc = args->getOption("cc");
128 if (args->isSet("bcc"))
130 mailto = true;
131 bcc = args->getOption("bcc");
134 if (args->isSet("msg"))
136 mailto = true;
137 messageFile.setPath( args->getOption("msg") );
140 if (args->isSet("body"))
142 mailto = true;
143 body = args->getOption("body");
146 const QStringList attachList = args->getOptionList("attach");
147 if (!attachList.isEmpty())
149 mailto = true;
150 for ( QStringList::ConstIterator it = attachList.constBegin() ; it != attachList.constEnd() ; ++it )
152 if ( !(*it).isEmpty() )
154 KUrl url( *it );
156 if ( url.protocol().isEmpty() )
158 const QString newUrl = QDir::currentPath () + QDir::separator () + url.fileName();
159 attachURLs.append( newUrl );
161 else
162 attachURLs.append( *it );
167 customHeaders = args->getOptionList("header");
169 if (args->isSet("composer"))
170 mailto = true;
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() + ", ";
180 else {
181 QString tmpArg = args->arg(i);
182 KUrl url( tmpArg );
183 if (url.isValid() && !url.protocol().isEmpty())
184 attachURLs.append( url.url() );
185 else
186 to += tmpArg + ", ";
188 mailto = true;
190 if ( !to.isEmpty() ) {
191 // cut off the superfluous trailing ", "
192 to.truncate( to.length() - 2 );
196 if ( !calledWithSession )
197 args->clear();
199 if ( mSuccess ) {
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." ) );
209 } else {
210 KMessageBox::error( 0, i18n( "Unable to find or start email service." ) );