Akonadi

datastore.h
1 /***************************************************************************
2  * SPDX-FileCopyrightText: 2006 Andreas Gungl <[email protected]> *
3  * *
4  * SPDX-License-Identifier: LGPL-2.0-or-later *
5  ***************************************************************************/
6 
7 #ifndef DATASTORE_H
8 #define DATASTORE_H
9 
10 #include <QObject>
11 #include <QList>
12 #include <QVector>
13 #include <QThreadStorage>
14 #include <QSqlDatabase>
15 #include <QMutex>
16 
17 class QSqlQuery;
18 class QTimer;
19 
20 #include "entities.h"
21 #include "notificationcollector.h"
22 
23 #include <memory>
24 
25 namespace Akonadi
26 {
27 namespace Server
28 {
29 
30 class DataStore;
31 class DataStoreFactory
32 {
33 public:
34  virtual ~DataStoreFactory() = default;
35  virtual DataStore *createStore() = 0;
36 
37 protected:
38  explicit DataStoreFactory() = default;
39 private:
40  Q_DISABLE_COPY_MOVE(DataStoreFactory)
41 };
42 
43 class NotificationCollector;
44 
95 class DataStore : public QObject
96 {
97  Q_OBJECT
98 public:
99  const constexpr static bool Silent = true;
100 
101  static void setFactory(std::unique_ptr<DataStoreFactory> factory);
102 
106  ~DataStore() override;
107 
111  virtual void open();
112 
116  void close();
117 
121  virtual bool init();
122 
126  static DataStore *self();
127 
131  static bool hasDataStore();
132 
133  /* --- ItemFlags ----------------------------------------------------- */
134  virtual bool setItemsFlags(const PimItem::List &items, const QVector<Flag> &flags,
135  bool *flagsChanged = nullptr, const Collection &col = Collection(), bool silent = false);
136  virtual bool appendItemsFlags(const PimItem::List &items, const QVector<Flag> &flags, bool *flagsChanged = nullptr,
137  bool checkIfExists = true, const Collection &col = Collection(), bool silent = false);
138  virtual bool removeItemsFlags(const PimItem::List &items, const QVector<Flag> &flags, bool *tagsChanged = nullptr,
139  const Collection &collection = Collection(), bool silent = false);
140 
141  /* --- ItemTags ----------------------------------------------------- */
142  virtual bool setItemsTags(const PimItem::List &items, const Tag::List &tags,
143  bool *tagsChanged = nullptr, bool silent = false);
144  virtual bool appendItemsTags(const PimItem::List &items, const Tag::List &tags,
145  bool *tagsChanged = nullptr, bool checkIfExists = true,
146  const Collection &col = Collection(), bool silent = false);
147  virtual bool removeItemsTags(const PimItem::List &items, const Tag::List &tags,
148  bool *tagsChanged = nullptr, bool silent = false);
149  virtual bool removeTags(const Tag::List &tags, bool silent = false);
150 
151  /* --- ItemParts ----------------------------------------------------- */
152  virtual bool removeItemParts(const PimItem &item, const QSet<QByteArray> &parts);
153 
154  // removes all payload parts for this item.
155  virtual bool invalidateItemCache(const PimItem &item);
156 
157  /* --- Collection ------------------------------------------------------ */
158  virtual bool appendCollection(Collection &collection, const QStringList &mimeTypes, const QMap<QByteArray, QByteArray> &attributes);
159 
161  virtual bool cleanupCollection(Collection &collection);
163  virtual bool cleanupCollection_slow(Collection &collection);
164 
166  virtual bool moveCollection(Collection &collection, const Collection &newParent);
167 
168  virtual bool appendMimeTypeForCollection(qint64 collectionId, const QStringList &mimeTypes);
169 
170  static QString collectionDelimiter()
171  {
172  return QStringLiteral("/");
173  }
174 
179  virtual void activeCachePolicy(Collection &col);
180 
182  QVector<Collection> virtualCollections(const PimItem &item);
183 
184  QMap< Server::Entity::Id, QList< PimItem > > virtualCollections(const Akonadi::Server::PimItem::List &items);
185 
186  /* --- PimItem ------------------------------------------------------- */
187  virtual bool appendPimItem(QVector<Part> &parts,
188  const QVector<Flag> &flags,
189  const MimeType &mimetype,
190  const Collection &collection,
191  const QDateTime &dateTime,
192  const QString &remote_id,
193  const QString &remoteRevision,
194  const QString &gid,
195  PimItem &pimItem);
199  virtual bool cleanupPimItems(const PimItem::List &items, bool silent = false);
200 
211  virtual bool unhidePimItem(PimItem &pimItem);
212 
220  virtual bool unhideAllPimItems();
221 
222  /* --- Collection attributes ------------------------------------------ */
223  virtual bool addCollectionAttribute(const Collection &col, const QByteArray &key,
224  const QByteArray &value, bool silent = false);
230  virtual bool removeCollectionAttribute(const Collection &col, const QByteArray &key);
231 
232  /* --- Helper functions ---------------------------------------------- */
233 
239  virtual bool beginTransaction(const QString &name);
240 
244  virtual bool rollbackTransaction();
245 
251  virtual bool commitTransaction();
252 
256  bool inTransaction() const;
257 
262  NotificationCollector *notificationCollector();
263 
269  QSqlDatabase database();
270 
274  void setSessionId(const QByteArray &sessionId)
275  {
276  mSessionId = sessionId;
277  }
278 
282  bool isOpened() const
283  {
284  return m_dbOpened;
285  }
286 
287  bool doRollback();
288  void transactionKilledByDB();
289 
290 Q_SIGNALS:
294  void transactionCommitted();
298  void transactionRolledBack();
299 
300 protected:
304  DataStore(AkonadiServer &akonadi);
305 
306  void debugLastDbError(const char *actionDescription) const;
307  void debugLastQueryError(const QSqlQuery &query, const char *actionDescription) const;
308 
309 private:
310  bool doAppendItemsFlag(const PimItem::List &items, const Flag &flag,
311  const QSet<PimItem::Id> &existing, const Collection &col,
312  bool silent);
313 
314  bool doAppendItemsTag(const PimItem::List &items, const Tag &tag,
315  const QSet<Entity::Id> &existing, const Collection &col,
316  bool silent);
317 
324  static QString dateTimeFromQDateTime(const QDateTime &dateTime);
325 
331  static QDateTime dateTimeToQDateTime(const QByteArray &dateTime);
332 
333 private Q_SLOTS:
334  void sendKeepAliveQuery();
335 
336 protected:
337  static std::unique_ptr<DataStoreFactory> sFactory;
338  std::unique_ptr<NotificationCollector> mNotificationCollector;
339  AkonadiServer &m_akonadi;
340 
341 private:
342  Q_DISABLE_COPY_MOVE(DataStore)
343 
344  void cleanupAfterRollback();
345  QString m_connectionName;
346  QSqlDatabase m_database;
347  bool m_dbOpened;
348  bool m_transactionKilledByDB = false;
349  uint m_transactionLevel;
350  struct TransactionQuery {
351  QString query;
352  QVector<QVariant> boundValues;
353  bool isBatch;
354  };
355  QByteArray mSessionId;
356  QTimer *m_keepAliveTimer = nullptr;
357  static bool s_hasForeignKeyConstraints;
358  static QMutex sTransactionMutex;
359 
360  friend class DataStoreFactory;
361 
362 };
363 
364 } // namespace Server
365 } // namespace Akonadi
366 
367 #endif
bool isOpened() const
Returns if the database is currently open.
Definition: datastore.h:282
Represents a collection of PIM items.
Definition: collection.h:63
void setSessionId(const QByteArray &sessionId)
Sets the current session id.
Definition: datastore.h:274
Part of the DataStore, collects change notifications and emits them after the current transaction has...
QCA_EXPORT void init()
Definition: item.h:31
Helper integration between Akonadi and Qt.
An Akonadi Tag.
Definition: tag.h:26
This class handles all the database access.
Definition: datastore.h:95
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sun Jul 5 2020 23:16:46 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.