Akonadi

utils.h
1 /*
2  * Copyright (C) 2010 Tobias Koenig <[email protected]>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Library General Public 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
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  *
19  */
20 
21 #ifndef UTILS_H
22 #define UTILS_H
23 
24 #include <QVariant>
25 #include <QDateTime>
26 
27 #include "storage/datastore.h"
28 #include "storage/dbtype.h"
29 
30 namespace Akonadi
31 {
32 namespace Server
33 {
34 namespace Utils
35 {
36 
40 static inline QString variantToString(const QVariant &variant)
41 {
42  if (variant.type() == QVariant::String) {
43  return variant.toString();
44  } else if (variant.type() == QVariant::ByteArray) {
45  return QString::fromUtf8(variant.toByteArray());
46  } else {
47  qWarning("Unable to convert variant of type %s to QString", variant.typeName());
48  Q_ASSERT(false);
49  return QString();
50  }
51 }
52 
56 static inline QByteArray variantToByteArray(const QVariant &variant)
57 {
58  if (variant.type() == QVariant::String) {
59  return variant.toString().toUtf8();
60  } else if (variant.type() == QVariant::ByteArray) {
61  return variant.toByteArray();
62  } else {
63  qWarning("Unable to convert variant of type %s to QByteArray", variant.typeName());
64  Q_ASSERT(false);
65  return QByteArray();
66  }
67 }
68 
69 static inline QDateTime variantToDateTime(const QVariant &variant)
70 {
71  if (variant.canConvert(QVariant::DateTime)) {
72  // MySQL and SQLite backends read the datetime from the database and
73  // assume it's local time. We stored it as UTC though, so we just need
74  // to change the interpretation in QDateTime.
75  // PostgreSQL on the other hand reads the datetime and assumes it's
76  // UTC(?) and converts it to local time via QDateTime::toLocalTime(),
77  // so we need to convert it back to UTC manually.
78  switch (DbType::type(DataStore::self()->database())) {
79  case DbType::MySQL:
80  case DbType::Sqlite: {
81  QDateTime dt = variant.toDateTime();
82  dt.setTimeSpec(Qt::UTC);
83  return dt;
84  }
85  case DbType::PostgreSQL:
86  return variant.toDateTime().toUTC();
87  default:
88  Q_UNREACHABLE();
89  }
90  } else {
91  qWarning("Unable to convert variant of type %s to QDateTime", variant.typeName());
92  Q_ASSERT(false);
93  return QDateTime();
94  }
95 }
96 
103 QString preferredSocketDirectory(const QString &directory, int fnLengthHint = -1);
104 
110 QString getDirectoryFileSystem(const QString &directory);
111 
119 void disableCoW(const QString &path);
120 
121 } // namespace Utils
122 } // namespace Server
123 } // namespace Akonadi
124 
125 #endif
bool canConvert(int targetTypeId) const const
QByteArray toByteArray() const const
QDateTime toUTC() const const
QDateTime toDateTime() const const
void setTimeSpec(Qt::TimeSpec spec)
QString fromUtf8(const char *str, int size)
Type type(const QSqlDatabase &db)
Returns the type of the given database object.
Definition: dbtype.cpp:24
static DataStore * self()
Per thread singleton.
Definition: datastore.cpp:235
Helper integration between Akonadi and Qt.
const char * typeName() const const
QVariant::Type type() const const
KDB_EXPORT QString variantToString(const QVariant &v)
QString toString() const const
QByteArray toUtf8() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Fri Jun 5 2020 23:08:56 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.