summaryrefslogtreecommitdiffstats
path: root/src/common-lib/utilities.h
blob: 2201a6e3a85f6be80ce6141b87edec6b024c0b54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright (C) 2021 The Qt Company Ltd.
// Copyright (C) 2019 Luxoft Sweden AB
// Copyright (C) 2018 Pelagicore AG
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#pragma once

#include <QtCore/QVector>
#include <QtCore/QPair>
#include <QtCore/QByteArray>
#include <QtCore/QVariant>
#include <QtCore/QString>
#include <QtCore/QUrl>
#include <QtCore/QDir>
#include <QtCore/QResource>
#include <QtCore/QLibrary>

#include <QtAppManCommon/global.h>

#include <functional>

QT_FORWARD_DECLARE_CLASS(QDir)
QT_FORWARD_DECLARE_CLASS(QJSEngine)

QT_BEGIN_NAMESPACE_AM

int timeoutFactor();

using YamlFormat = QPair<QString, int>;

YamlFormat checkYamlFormat(const QVector<QVariant> &docs, int numberOfDocuments,
                           const QVector<YamlFormat> &formatTypesAndVersions) noexcept(false);

/*! \internal
    Convenience function that makes it easy to accept a plain string where
    a stringlist is required - this is useful when parsing YAML config files
*/
inline QStringList variantToStringList(const QVariant &v)
{
    return (v.metaType() == QMetaType::fromType<QString>()) ? QStringList(v.toString())
                                                            : v.toStringList();
}

// Translate between QFile and QUrl (resource) representations.
// For some weird reason, QFile cannot cope with "qrc:" and QUrl cannot cope with ":".
inline QUrl filePathToUrl(const QString &path, const QString &baseDir)
{
    return path.startsWith(u":") ? QUrl(QStringLiteral("qrc") + path)
                                 : QUrl::fromUserInput(path, baseDir, QUrl::AssumeLocalFile);
}

// Used in {Package,Application,Intent}::name()
QString translateFromMap(const QMap<QString, QString> &languageToName, const QString &defaultName = {});

inline QString urlToLocalFilePath(const QUrl &url)
{
    if (url.isLocalFile())
        return url.toLocalFile();
    else if (url.scheme() == u"qrc")
        return u':' + url.path();
    return { };
}

inline QString toAbsoluteFilePath(const QString &path, const QString &baseDir = QDir::currentPath())
{
    return path.startsWith(u"qrc:") ? path.mid(3) : QDir(baseDir).absoluteFilePath(path);
}

/*! \internal
    Recursively merge the second QVariantMap into the first one
*/
void recursiveMergeVariantMap(QVariantMap &into, const QVariantMap &from);

enum class RecursiveOperationType
{
    EnterDirectory,
    LeaveDirectory,
    File
};

/*! \internal

    Recursively iterates over the file-system tree at \a path and calls the
    functor \a operation for each entry.

    \c path is always the file-path to the current entry. For files, \a
    operation will only be called once (\c{type == File}), whereas for
    directories, the functor will be triggered twice: once when entering the
    directory (\c{type == EnterDirectory}) and once when all sub-directories
    and files have been processed (\c{type == LeaveDirectory}).
 */
bool recursiveOperation(const QString &path, const std::function<bool(const QString &, RecursiveOperationType)> &operation);

// convenience
bool recursiveOperation(const QByteArray &path, const std::function<bool(const QString &, RecursiveOperationType)> &operation);
bool recursiveOperation(const QDir &path, const std::function<bool(const QString &, RecursiveOperationType)> &operation);

// makes files and directories writable, then deletes them
bool safeRemove(const QString &path, RecursiveOperationType type);

qint64 getParentPid(qint64 pid);

QVector<QObject *> loadPlugins_helper(const char *type, const QStringList &files, const char *iid) noexcept(false);

template <typename T>
QVector<T *> loadPlugins(const char *type, const QStringList &files) noexcept(false)
{
    QVector<T *> result;
    auto plugins = loadPlugins_helper(type, files, qobject_interface_iid<T *>());
    for (auto p : plugins)
        result << qobject_cast<T *>(p);
    return result;
}

// Load a Qt resource, either in the form of a resource file or a plugin
void loadResource(const QString &resource) noexcept(false);

// Qt6 removed v_cast, but the "replacement" QVariant::Private::get is const only
template <typename T> T *qt6_v_cast(QVariant::Private *vp)
{
    return static_cast<T *>(const_cast<void *>(vp->storage()));
}

// close all valid file descriptors and then clear the (non-const) vector
void closeAndClearFileDescriptors(QVector<int> &fdList);

// make sure that the given id can be used as a filename
void validateIdForFilesystemUsage(const QString &id) noexcept(false);

QJSEngine *getJSEngine(const QObject *obj);

QT_END_NAMESPACE_AM