1/*
2 * Copyright (c) 2009 Igor Trindade Oliveira <igor_trindade@yahoo.com.br>
3 * Copyright (c) 2009 Volker Krause <vkrause@kde.org>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include "resource.h"
20
21#include "global.h"
22#include <akonadi/resourcesynchronizationjob.h>
23#include "test.h"
24
25#include <akonadi/agentmanager.h>
26#include <akonadi/agentinstancecreatejob.h>
27
28#include <KDebug>
29#include <qtest_kde.h>
30
31#include <QDBusConnection>
32#include <QDBusInterface>
33#include <QDBusReply>
34
35using namespace Akonadi;
36
37Resource::Resource(QObject* parent) :
38 QObject( parent )
39{
40 Q_ASSERT( parent );
41}
42
43Resource::~Resource()
44{
45 destroy();
46}
47
48void Resource::setType(const QString& type)
49{
50 mTypeIdentifier = type;
51}
52
53QString Resource::identifier() const
54{
55 return mInstance.identifier();
56}
57
58void Resource::setOption(const QString& key, const QVariant& value)
59{
60 mSettings.insert( key, value );
61}
62
63void Resource::setPathOption(const QString& key, const QString& path)
64{
65 if ( QFileInfo( path ).isAbsolute() )
66 setOption( key, path );
67 else
68 setOption( key, QString(Global::basePath() + QDir::separator() + path) );
69}
70
71
72bool Resource::createResource()
73{
74 if ( mInstance.isValid() )
75 return false;
76
77 const AgentType type = AgentManager::self()->type( mTypeIdentifier );
78 if ( !type.isValid() )
79 return false;
80
81 AgentInstanceCreateJob *job = new AgentInstanceCreateJob( type, this );
82 if ( !job->exec() ) {
83 kWarning() << job->errorText();
84 return false;
85 }
86 mInstance = job->instance();
87
88 QDBusInterface iface( "org.freedesktop.Akonadi.Resource." + identifier(), "/Settings" );
89 if ( !iface.isValid() )
90 return false;
91
92 // configure resource
93 for ( QHash<QString, QVariant>::const_iterator it = mSettings.constBegin(); it != mSettings.constEnd(); ++it ) {
94 kDebug() << "Setting up " << it.key() << " for agent " << identifier();
95 const QString methodName = QString::fromLatin1("set%1").arg( it.key() );
96 const QVariant arg = it.value();
97 QDBusReply<void> reply = iface.call( methodName, arg );
98 if ( !reply.isValid() )
99 kError() << "Setting " << it.key() << " failed for agent " << identifier() << ":" << reply.error().message();
100 }
101 mInstance.reconfigure();
102
103 ResourceSynchronizationJob *syncJob = new ResourceSynchronizationJob( mInstance, this );
104 if ( !syncJob->exec() )
105 kError() << "Synching resource failed: " << syncJob->errorString();
106
107 return true;
108}
109
110void Resource::create()
111{
112 if ( !createResource() )
113 Test::instance()->fail( "Creating resource failed." );
114}
115
116
117void Resource::destroy()
118{
119 if ( !mInstance.isValid() )
120 return;
121 AgentManager::self()->removeInstance( mInstance );
122 mInstance = AgentInstance();
123}
124
125void Resource::write()
126{
127 QDBusInterface iface( "org.freedesktop.Akonadi", "/notifications/debug", "org.freedesktop.Akonadi.NotificationManager" );
128 Q_ASSERT( iface.isValid() );
129 QDBusReply<void> result = iface.call( "emitPendingNotifications" );
130 if ( !result.isValid() )
131 Test::instance()->fail( result.error().message() );
132 ResourceSynchronizationJob *syncJob = new ResourceSynchronizationJob( mInstance, this );
133 if ( !syncJob->exec() )
134 kError() << "Synching resource failed: " << syncJob->errorString();
135}
136
137void Resource::recreate()
138{
139 write();
140 destroy();
141 create();
142}
143
144QObject* Resource::newInstance()
145{
146 return createNewInstance<Resource>( this );
147}
148
149QObject* Resource::newInstance(const QString& type)
150{
151 Resource* r = qobject_cast<Resource*>( createNewInstance<Resource>( this ) );
152 Q_ASSERT( r );
153 r->setType( type );
154 return r;
155}
156
157