1/*
2 Copyright (c) 2008 Tobias Koenig <tokoe@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 "agentinstance.h"
21#include "agentinstance_p.h"
22
23#include "agentmanager.h"
24#include "agentmanager_p.h"
25#include "servermanager.h"
26
27#include <KDebug>
28
29using namespace Akonadi;
30
31AgentInstance::AgentInstance()
32 : d(new Private)
33{
34}
35
36AgentInstance::AgentInstance(const AgentInstance &other)
37 : d(other.d)
38{
39}
40
41AgentInstance::~AgentInstance()
42{
43}
44
45bool AgentInstance::isValid() const
46{
47 return !d->mIdentifier.isEmpty();
48}
49
50AgentType AgentInstance::type() const
51{
52 return d->mType;
53}
54
55QString AgentInstance::identifier() const
56{
57 return d->mIdentifier;
58}
59
60void AgentInstance::setName(const QString &name)
61{
62 AgentManager::self()->d->setName(*this, name);
63}
64
65QString AgentInstance::name() const
66{
67 return d->mName;
68}
69
70AgentInstance::Status AgentInstance::status() const
71{
72 switch (d->mStatus) {
73 case 0:
74 return Idle;
75 case 1:
76 return Running;
77 case 2:
78 default:
79 return Broken;
80 case 3:
81 return NotConfigured;
82 }
83}
84
85QString AgentInstance::statusMessage() const
86{
87 return d->mStatusMessage;
88}
89
90int AgentInstance::progress() const
91{
92 return d->mProgress;
93}
94
95bool AgentInstance::isOnline() const
96{
97 return d->mIsOnline;
98}
99
100void AgentInstance::setIsOnline(bool online)
101{
102 AgentManager::self()->d->setOnline(*this, online);
103}
104
105void AgentInstance::configure(QWidget *parent)
106{
107 AgentManager::self()->d->configure(*this, parent);
108}
109
110void AgentInstance::synchronize()
111{
112 AgentManager::self()->d->synchronize(*this);
113}
114
115void AgentInstance::synchronizeCollectionTree()
116{
117 AgentManager::self()->d->synchronizeCollectionTree(*this);
118}
119
120AgentInstance &AgentInstance::operator=(const AgentInstance &other)
121{
122 if (this != &other) {
123 d = other.d;
124 }
125
126 return *this;
127}
128
129bool AgentInstance::operator==(const AgentInstance &other) const
130{
131 return (d->mIdentifier == other.d->mIdentifier);
132}
133
134void AgentInstance::abortCurrentTask() const
135{
136 QDBusInterface iface(ServerManager::agentServiceName(ServerManager::Agent, identifier()),
137 QString::fromLatin1("/"),
138 QString::fromLatin1("org.freedesktop.Akonadi.Agent.Control"));
139 if (iface.isValid()) {
140 QDBusReply<void> reply = iface.call(QString::fromLatin1("abort"));
141 if (!reply.isValid()) {
142 kWarning() << "Failed to place D-Bus call.";
143 }
144 } else {
145 kWarning() << "Unable to obtain agent interface";
146 }
147}
148
149void AgentInstance::reconfigure() const
150{
151 QDBusInterface iface(ServerManager::agentServiceName(ServerManager::Agent, identifier()),
152 QString::fromLatin1("/"),
153 QString::fromLatin1("org.freedesktop.Akonadi.Agent.Control"));
154 if (iface.isValid()) {
155 QDBusReply<void> reply = iface.call(QString::fromLatin1("reconfigure"));
156 if (!reply.isValid()) {
157 kWarning() << "Failed to place D-Bus call.";
158 }
159 } else {
160 kWarning() << "Unable to obtain agent interface";
161 }
162}
163
164void Akonadi::AgentInstance::restart() const
165{
166 QDBusInterface iface(ServerManager::serviceName(Akonadi::ServerManager::Control),
167 QString::fromLatin1("/AgentManager"),
168 QString::fromLatin1("org.freedesktop.Akonadi.AgentManager"));
169 if (iface.isValid()) {
170 QDBusReply<void> reply = iface.call(QString::fromLatin1("restartAgentInstance"), identifier());
171 if (!reply.isValid()) {
172 kWarning() << "Failed to place D-Bus call.";
173 }
174 } else {
175 kWarning() << "Unable to obtain control interface" << iface.lastError().message();
176 }
177}
178