Akonadi

utils.h
1 /*
2  * SPDX-FileCopyrightText: 2010 Tobias Koenig <[email protected]>
3  *
4  * SPDX-License-Identifier: LGPL-2.0-or-later
5  *
6  */
7 
8 #pragma once
9 
10 #include <QDateTime>
11 #include <QVariant>
12 
13 #include "storage/datastore.h"
14 #include "storage/dbtype.h"
15 
16 namespace Akonadi
17 {
18 namespace Server
19 {
20 namespace Utils
21 {
22 /**
23  * Converts a QVariant to a QString depending on its internal type.
24  */
25 static inline QString variantToString(const QVariant &variant)
26 {
27  if (variant.type() == QVariant::String) {
28  return variant.toString();
29  } else if (variant.type() == QVariant::ByteArray) {
30  return QString::fromUtf8(variant.toByteArray());
31  } else {
32  qWarning("Unable to convert variant of type %s to QString", variant.typeName());
33  Q_ASSERT(false);
34  return QString();
35  }
36 }
37 
38 /**
39  * Converts a QVariant to a QByteArray depending on its internal type.
40  */
41 static inline QByteArray variantToByteArray(const QVariant &variant)
42 {
43  if (variant.type() == QVariant::String) {
44  return variant.toString().toUtf8();
45  } else if (variant.type() == QVariant::ByteArray) {
46  return variant.toByteArray();
47  } else {
48  qWarning("Unable to convert variant of type %s to QByteArray", variant.typeName());
49  Q_ASSERT(false);
50  return QByteArray();
51  }
52 }
53 
54 static inline QDateTime variantToDateTime(const QVariant &variant)
55 {
56  if (variant.canConvert(QVariant::DateTime)) {
57  // MySQL and SQLite backends read the datetime from the database and
58  // assume it's local time. We stored it as UTC though, so we just need
59  // to change the interpretation in QDateTime.
60  // PostgreSQL on the other hand reads the datetime and assumes it's
61  // UTC(?) and converts it to local time via QDateTime::toLocalTime(),
62  // so we need to convert it back to UTC manually.
63  switch (DbType::type(DataStore::self()->database())) {
64  case DbType::MySQL:
65  case DbType::Sqlite: {
66  QDateTime dt = variant.toDateTime();
67  dt.setTimeSpec(Qt::UTC);
68  return dt;
69  }
70  case DbType::PostgreSQL:
71  return variant.toDateTime().toUTC();
72  default:
73  Q_UNREACHABLE();
74  }
75  } else {
76  qWarning("Unable to convert variant of type %s to QDateTime", variant.typeName());
77  Q_ASSERT(false);
78  return QDateTime();
79  }
80 }
81 
82 /**
83  * Returns the socket @p directory that is passed to this method or the one
84  * the user has overwritten via the config file.
85  * The passed @p fnLengthHint will also ensure the absolute file path length of the
86  * directory + separator + hint would not overflow the system limitation.
87  */
88 QString preferredSocketDirectory(const QString &directory, int fnLengthHint = -1);
89 
90 /**
91  * Returns name of filesystem that @p directory is stored on. This
92  * only works on Linux and returns empty string on other platforms or when it's
93  * unable to detect the filesystem.
94  */
95 QString getDirectoryFileSystem(const QString &directory);
96 
97 /**
98  * Disables filesystem copy-on-write feature on given file or directory.
99  * Only works on Linux and does nothing on other platforms.
100  *
101  * It was tested only with Btrfs but in theory can be called on any FS that
102  * supports NOCOW.
103  */
104 void disableCoW(const QString &path);
105 
106 } // namespace Utils
107 } // namespace Server
108 } // namespace Akonadi
109 
static DataStore * self()
Per thread singleton.
Definition: datastore.cpp:215
QString fromUtf8(const char *str, int size)
Type type(const QSqlDatabase &db)
Returns the type of the given database object.
Definition: dbtype.cpp:11
QByteArray toByteArray() const const
QVariant::Type type() const const
QByteArray toUtf8() const const
void setTimeSpec(Qt::TimeSpec spec)
QDateTime toUTC() const const
bool canConvert(int targetTypeId) const const
QDateTime toDateTime() const const
KDB_EXPORT QString variantToString(const QVariant &v)
const char * typeName() const const
QString toString() const const
Helper integration between Akonadi and Qt.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Jul 2 2022 06:41:49 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.