Akonadi

resourcemanager.cpp
1 /*
2  Copyright (c) 2006 Volker Krause <[email protected]>
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 "resourcemanager.h"
21 #include "tracer.h"
22 #include "storage/datastore.h"
23 #include "storage/transaction.h"
24 #include "resourcemanageradaptor.h"
25 
26 #include <shared/akranges.h>
27 #include <private/capabilities_p.h>
28 
29 #include <QDBusConnection>
30 
31 using namespace Akonadi::Server;
32 using namespace AkRanges;
33 
34 ResourceManager::ResourceManager(Tracer &tracer)
35  : QObject()
36  , mTracer(tracer)
37 {
38  new ResourceManagerAdaptor(this);
39  QDBusConnection::sessionBus().registerObject(QStringLiteral("/ResourceManager"), this);
40 }
41 
42 void ResourceManager::addResourceInstance(const QString &name, const QStringList &capabilities)
43 {
44  Transaction transaction(DataStore::self(), QStringLiteral("ADD RESOURCE INSTANCE"));
45  Resource resource = Resource::retrieveByName(name);
46  if (resource.isValid()) {
47  mTracer.error("ResourceManager", QStringLiteral("Resource '%1' already exists.").arg(name));
48  return; // resource already exists
49  }
50 
51  // create the resource
52  resource.setName(name);
53  resource.setIsVirtual(capabilities.contains(QLatin1String(AKONADI_AGENT_CAPABILITY_VIRTUAL)));
54  if (!resource.insert()) {
55  mTracer.error("ResourceManager", QStringLiteral("Could not create resource '%1'.").arg(name));
56  }
57  transaction.commit();
58 }
59 
60 void ResourceManager::removeResourceInstance(const QString &name)
61 {
62  // remove items and collections
63  Resource resource = Resource::retrieveByName(name);
64  if (resource.isValid()) {
65  resource.collections() | Actions::forEach([](Collection col) {
66  DataStore::self()->cleanupCollection(col);
67  });
68 
69  // remove resource
70  resource.remove();
71  }
72 }
73 
74 QStringList ResourceManager::resourceInstances() const
75 {
76  return Resource::retrieveAll() | Views::transform(&Resource::name) | Actions::toQList;
77 }
78 
void setName(const QString &name)
Sets the value of the name column of this record.
Definition: entities.cpp:551
bool registerObject(const QString &path, QObject *object, QDBusConnection::RegisterOptions options)
bool contains(const QString &str, Qt::CaseSensitivity cs) const const
QDBusConnection sessionBus()
The global tracer instance where all akonadi components can send their tracing information to...
Definition: tracer.h:53
bool remove()
Deletes this record.
Definition: entities.cpp:1016
Helper class for DataStore transaction handling.
Definition: transaction.h:37
bool insert(qint64 *insertId=nullptr)
Inserts this record into the DataStore.
Definition: entities.cpp:928
void setIsVirtual(bool isVirtual)
Sets the value of the isVirtual column of this record.
Definition: entities.cpp:564
QVector< Collection > collections() const
Retrieve a list of all Collection records referring to this record in their column resourceId...
Definition: entities.cpp:898
QString name() const
Returns the name of the resource.
Representation of a record in the Resource table.
Definition: entities.h:246
Representation of a record in the Collection table.
Definition: entities.h:451
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon May 25 2020 22:46:11 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.