1/*
2 * Copyright (C) 2010 Tobias Koenig <tokoe@kde.org>
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 <QtCore/QVariant>
25
26namespace Akonadi {
27namespace Server {
28namespace Utils {
29
30/**
31 * Converts a QVariant to a QString depending on its internal type.
32 */
33static inline QString variantToString(const QVariant &variant)
34{
35 if (variant.type() == QVariant::String) {
36 return variant.toString();
37 } else if (variant.type() == QVariant::ByteArray) {
38 return QString::fromUtf8(variant.toByteArray());
39 } else {
40 qWarning("Unable to convert variant of type %s to QString", variant.typeName());
41 Q_ASSERT(false);
42 return QString();
43 }
44}
45
46/**
47 * Converts a QVariant to a QByteArray depending on its internal type.
48 */
49static inline QByteArray variantToByteArray(const QVariant &variant)
50{
51 if (variant.type() == QVariant::String) {
52 return variant.toString().toUtf8();
53 } else if (variant.type() == QVariant::ByteArray) {
54 return variant.toByteArray();
55 } else {
56 qWarning("Unable to convert variant of type %s to QByteArray", variant.typeName());
57 Q_ASSERT(false);
58 return QByteArray();
59 }
60}
61
62/**
63 * Returns the socket @p directory that is passed to this method or the one
64 * the user has overwritten via the config file.
65 */
66QString preferredSocketDirectory(const QString &directory);
67
68/**
69 * Returns name of filesystem that @p directory is stored on. This
70 * only works on Linux and returns empty string on other platforms or when it's
71 * unable to detect the filesystem.
72 */
73QString getDirectoryFileSystem(const QString &directory);
74
75/**
76 * Disables filesystem copy-on-write feature on given file or directory.
77 * Only works on Linux and does nothing on other platforms.
78 *
79 * It was tested only with Btrfs but in theory can be called on any FS that
80 * supports NOCOW.
81 */
82void disableCoW(const QString &path);
83
84} // namespace Utils
85} // namespace Server
86} // namespace Akonadi
87
88#endif
89