Akonadi

agentsearchinterface.cpp
1 /*
2  Copyright (c) 2009 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 "agentsearchinterface.h"
21 #include "akonadiagentbase_debug.h"
22 #include "agentsearchinterface_p.h"
23 #include "collection.h"
24 #include <QDBusConnection>
25 #include "searchresultjob_p.h"
26 #include "searchadaptor.h"
27 #include "collectionfetchjob.h"
28 #include "collectionfetchscope.h"
29 #include "servermanager.h"
30 #include "agentbase.h"
31 #include "private/imapset_p.h"
32 
33 using namespace Akonadi;
34 
35 AgentSearchInterfacePrivate::AgentSearchInterfacePrivate(AgentSearchInterface *qq)
36  : q(qq)
37 {
38  new Akonadi__SearchAdaptor(this);
39  QDBusConnection::sessionBus().registerObject(QStringLiteral("/Search"),
40  this, QDBusConnection::ExportAdaptors);
41 
42  QTimer::singleShot(0, this, &AgentSearchInterfacePrivate::delayedInit);
43 }
44 
45 void AgentSearchInterfacePrivate::delayedInit()
46 {
47  QDBusInterface iface(ServerManager::serviceName(ServerManager::Server),
48  QStringLiteral("/SearchManager"),
49  QStringLiteral("org.freedesktop.Akonadi.SearchManager"),
51  QDBusMessage msg = iface.call(QStringLiteral("registerInstance"), dynamic_cast<AgentBase *>(q)->identifier());
52  //TODO ?
53 }
54 
55 void AgentSearchInterfacePrivate::addSearch(const QString &query, const QString &queryLanguage, quint64 resultCollectionId)
56 {
57  q->addSearch(query, queryLanguage, Collection(resultCollectionId));
58 }
59 
60 void AgentSearchInterfacePrivate::removeSearch(quint64 resultCollectionId)
61 {
62  q->removeSearch(Collection(resultCollectionId));
63 }
64 
65 void AgentSearchInterfacePrivate::search(const QByteArray &searchId,
66  const QString &query,
67  quint64 collectionId)
68 {
69  mSearchId = searchId;
70  mCollectionId = collectionId;
71 
72  CollectionFetchJob *fetchJob = new CollectionFetchJob(Collection(mCollectionId), CollectionFetchJob::Base, this);
74  fetchJob->setProperty("query", query);
75  connect(fetchJob, &KJob::finished, this, &AgentSearchInterfacePrivate::collectionReceived);
76 }
77 
78 void AgentSearchInterfacePrivate::collectionReceived(KJob *job)
79 {
80  CollectionFetchJob *fetchJob = qobject_cast<CollectionFetchJob *>(job);
81  if (fetchJob->error()) {
82  qCCritical(AKONADIAGENTBASE_LOG) << fetchJob->errorString();
83  new SearchResultJob(fetchJob->property("searchId").toByteArray(), Collection(mCollectionId), this);
84  return;
85  }
86 
87  if (fetchJob->collections().count() != 1) {
88  qCDebug(AKONADIAGENTBASE_LOG) << "Server requested search in invalid collection, or collection was removed in the meanwhile";
89  // Tell server we are done
90  new SearchResultJob(fetchJob->property("searchId").toByteArray(), Collection(mCollectionId), this);
91  return;
92  }
93 
94  const Collection collection = fetchJob->collections().at(0);
95  q->search(fetchJob->property("query").toString(),
96  collection);
97 }
98 
100  : d(new AgentSearchInterfacePrivate(this))
101 {
102 }
103 
105 {
106  delete d;
107 }
108 
109 void AgentSearchInterface::searchFinished(const QVector<qint64> &result, ResultScope scope)
110 {
111  if (scope == Akonadi::AgentSearchInterface::Rid) {
112  QVector<QByteArray> rids;
113  rids.reserve(result.size());
114  for (qint64 rid : result) {
115  rids << QByteArray::number(rid);
116  }
117 
118  searchFinished(rids);
119  return;
120  }
121 
122  SearchResultJob *resultJob = new SearchResultJob(d->mSearchId, Collection(d->mCollectionId), d);
123  resultJob->setResult(result);
124 }
125 
126 void AgentSearchInterface::searchFinished(const ImapSet &result, ResultScope scope)
127 {
128  if (scope == Akonadi::AgentSearchInterface::Rid) {
129  QVector<QByteArray> rids;
130  const ImapInterval::List lstInterval = result.intervals();
131  for (const ImapInterval &interval : lstInterval) {
132  const int endInterval(interval.end());
133  for (int i = interval.begin(); i <= endInterval; ++i) {
134  rids << QByteArray::number(i);
135  }
136  }
137 
138  searchFinished(rids);
139  return;
140  }
141 
142  SearchResultJob *resultJob = new SearchResultJob(d->mSearchId, Collection(d->mCollectionId), d);
143  resultJob->setResult(result);
144 }
145 
146 void AgentSearchInterface::searchFinished(const QVector<QByteArray> &result)
147 {
148  SearchResultJob *resultJob = new SearchResultJob(d->mSearchId, Collection(d->mCollectionId), d);
149  resultJob->setResult(result);
150 }
151 
152 #include "moc_agentsearchinterface_p.cpp"
QByteArray toByteArray() const const
An interface for agents (or resources) that support searching in their backend.
void setAncestorRetrieval(AncestorRetrieval ancestorDepth)
Sets how many levels of ancestor collections should be included in the retrieval. ...
Collection::List collections() const
Returns the list of fetched collection.
QString errorString() const override
Returns the error string, if there has been an error, an empty string otherwise.
Definition: job.cpp:338
CollectionFetchScope & fetchScope()
Returns the collection fetch scope.
Represents a collection of PIM items.
Definition: collection.h:76
Job that fetches collections from the Akonadi storage.
bool registerObject(const QString &path, QObject *object, QDBusConnection::RegisterOptions options)
QDBusConnection sessionBus()
static QString serviceName(ServiceType serviceType)
Returns the namespaced D-Bus service name for serviceType.
AgentSearchInterface()
Creates a new agent search interface.
Only fetch the base collection.
QVariant property(const char *name) const const
void finished(KJob *job)
QByteArray number(int n, int base)
Retrieve all ancestors, up to Collection::root()
virtual ~AgentSearchInterface()
Destroys the agent search interface.
void reserve(int size)
const T & at(int i) const const
Helper integration between Akonadi and Qt.
int count(const T &value) const const
bool setProperty(const char *name, const QVariant &value)
int size() const const
QString toString() const const
int error() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Fri Jun 5 2020 23:08:53 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.