Akonadi

utils.h
1/*
2 * SPDX-FileCopyrightText: 2010 Tobias Koenig <tokoe@kde.org>
3 *
4 * SPDX-License-Identifier: LGPL-2.0-or-later
5 *
6 */
7
8#pragma once
9
10#include <QDateTime>
11#include <QTimeZone>
12#include <QVariant>
13
14#include "storage/datastore.h"
15#include "storage/dbtype.h"
16
17namespace Akonadi
18{
19namespace Server
20{
21namespace Utils
22{
23/**
24 * Converts a QVariant to a QString depending on its internal type.
25 */
26static inline QString variantToString(const QVariant &variant)
27{
28 if (variant.typeId() == QMetaType::QString) {
29 return variant.toString();
30 } else if (variant.typeId() == QMetaType::QByteArray) {
31 return QString::fromUtf8(variant.toByteArray());
32 } else {
33 qWarning("Unable to convert variant of type %s to QString", variant.typeName());
34 Q_ASSERT(false);
35 return QString();
36 }
37}
38
39/**
40 * Converts a QVariant to a QByteArray depending on its internal type.
41 */
42static inline QByteArray variantToByteArray(const QVariant &variant)
43{
44 if (variant.typeId() == QMetaType::QString) {
45 return variant.toString().toUtf8();
46 } else if (variant.typeId() == QMetaType::QByteArray) {
47 return variant.toByteArray();
48 } else {
49 qWarning("Unable to convert variant of type %s to QByteArray", variant.typeName());
50 Q_ASSERT(false);
51 return QByteArray();
52 }
53}
54
55/**
56 * Converts QDateTime to a QVariant suitable for storing in the database.
57 *
58 * It should come as no surprise that different database engines and their QtSql drivers have each a different
59 * approach to time and timezones.
60 * Here we make sure that whatever QDateTime we have, it will be stored in the database in a way
61 * that matches its (and its driver's) perception of timezones.
62 *
63 * Check the dbdatetimetest for more.
64 */
65static inline QVariant dateTimeToVariant(const QDateTime &dateTime, DataStore *dataStore)
66{
67 switch (DbType::type(dataStore->database())) {
68 case DbType::MySQL:
69 // The QMYSQL driver does not encode timezone information. The MySQL server, when given time
70 // without a timezones treats it as a local time. Thus, when we pass UTC time to QtSQL, it
71 // will get interpreted as local time by MySQL, converted to UTC (again) and stored.
72 // This causes two main problems:
73 // 1) The query fails if the time sent by the driver is not valid in the local timezone (DST change), and
74 // 2) It requires additional code when reading results from the database to re-interpret the returned
75 // "local time" as being UTC
76 // So, instead we make sure we always pass real local time to MySQL, exactly as it expects it.
77 return dateTime.toLocalTime();
78 case DbType::Sqlite:
79 // The QSQLITE converts QDateTime to string using QDateTime::ISODateWithMs and SQLite stores it as TEXT
80 // (since it doesn't really have any dedicated date/time data type).
81 // To stay consistent with other drivers, we convertr the dateTime to UTC here so SQLite stores UTC.
82 return dateTime.toUTC();
83 case DbType::PostgreSQL:
84 // The QPSQL driver automatically converts the date time to UTC and forcibly stores it with UTC timezone
85 // information, so we are good here. At least someone gets it right...
86 return QVariant(dateTime);
87 default:
88 Q_UNREACHABLE();
89 return {};
90 }
91}
92
93/**
94 * Returns the socket @p directory that is passed to this method or the one
95 * the user has overwritten via the config file.
96 * The passed @p fnLengthHint will also ensure the absolute file path length of the
97 * directory + separator + hint would not overflow the system limitation.
98 */
99QString preferredSocketDirectory(const QString &directory, int fnLengthHint = -1);
100
101/**
102 * Returns name of filesystem that @p directory is stored on. This
103 * only works on Linux and returns empty string on other platforms or when it's
104 * unable to detect the filesystem.
105 */
106QString getDirectoryFileSystem(const QString &directory);
107
108/**
109 * Disables filesystem copy-on-write feature on given file or directory.
110 * Only works on Linux and does nothing on other platforms.
111 *
112 * It was tested only with Btrfs but in theory can be called on any FS that
113 * supports NOCOW.
114 */
115void disableCoW(const QString &path);
116
117} // namespace Utils
118} // namespace Server
119} // namespace Akonadi
Type type(const QSqlDatabase &db)
Returns the type of the given database object.
Definition dbtype.cpp:11
Helper integration between Akonadi and Qt.
KDB_EXPORT QString variantToString(const QVariant &v)
QDateTime toLocalTime() const const
QDateTime toUTC() const const
QString fromUtf8(QByteArrayView str)
QByteArray toUtf8() const const
QByteArray toByteArray() const const
QString toString() const const
int typeId() const const
const char * typeName() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:08:30 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.