1/*
2 Copyright (c) 2008 Volker Krause <vkrause@kde.org>
3
4 This library is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at your
7 option) any later version.
8
9 This library is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to the
16 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA.
18*/
19
20#include "akonadistarter.h"
21
22#include <akapplication.h>
23#include <akdbus.h>
24#include <akdebug.h>
25
26#include <QtCore/QCoreApplication>
27#include <QtCore/QDebug>
28#include <QtCore/QProcess>
29#include <QtCore/QTimer>
30#include <QtDBus/QDBusConnection>
31#include <QtDBus/QDBusServiceWatcher>
32
33AkonadiStarter::AkonadiStarter(QObject *parent)
34 : QObject(parent)
35 , mRegistered(false)
36{
37 QDBusServiceWatcher *watcher = new QDBusServiceWatcher(AkDBus::serviceName(AkDBus::ControlLock),
38 QDBusConnection::sessionBus(),
39 QDBusServiceWatcher::WatchForOwnerChange, this);
40
41 connect(watcher, SIGNAL(serviceOwnerChanged(QString,QString,QString)),
42 this, SLOT(serviceOwnerChanged(QString,QString,QString)));
43}
44
45bool AkonadiStarter::start()
46{
47 akDebug() << "Starting Akonadi Server...";
48
49 QStringList serverArgs;
50 if (AkApplication::hasInstanceIdentifier()) {
51 serverArgs << QLatin1String("--instance") << AkApplication::instanceIdentifier();
52 }
53
54 const bool ok = QProcess::startDetached(QLatin1String("akonadi_control"), serverArgs);
55 if (!ok) {
56 akError() << "Error: unable to execute binary akonadi_control";
57 return false;
58 }
59
60 // safety timeout
61 QTimer::singleShot(5000, QCoreApplication::instance(), SLOT(quit()));
62 // wait for the server to register with D-Bus
63 QCoreApplication::instance()->exec();
64
65 if (!mRegistered) {
66 akError() << "Error: akonadi_control was started but didn't register at D-Bus session bus.";
67 akError() << "Make sure your system is set up correctly!";
68 return false;
69 }
70
71 akDebug() << " done.";
72 return true;
73}
74
75void AkonadiStarter::serviceOwnerChanged(const QString &name, const QString &oldOwner, const QString &newOwner)
76{
77 Q_UNUSED(name);
78 Q_UNUSED(oldOwner);
79 if (newOwner.isEmpty()) {
80 return;
81 }
82
83 mRegistered = true;
84 QCoreApplication::instance()->quit();
85}
86