Debian Codegraph @

include/util/dutil.h

raw
1 // SPDX-FileCopyrightText: 2016 - 2023 UnionTech Software Technology Co., Ltd.
2 //
3 // SPDX-License-Identifier: LGPL-3.0-or-later
4
5 #pragma once
6
7 #include <QTimer>
8 #include <QThread>
9 #include <QMetaObject>
10 #include <QCoreApplication>
11 #include <QRegularExpression>
12 #include <QStandardPaths>
13 #include <QDir>
14 #include <type_traits>
15 #include <cstring>
16
17 namespace DUtil
18 {
19
20 template <typename Func1>
21 inline void TimerSingleShot(int msec, Func1 slot)
22 {
23 #if QT_VERSION >= 0x050500
24 QTimer::singleShot(msec, slot);
25 #else
26 QTimer *timer = new QTimer;
27 timer->setSingleShot(true);
28 timer->setInterval(msec);
29 timer->moveToThread(qApp->thread());
30 QObject::connect(timer, &QTimer::timeout, slot);
31 QObject::connect(timer, &QTimer::timeout, timer, &QTimer::deleteLater);
32 if (QThread::currentThread() == qApp->thread()) { timer->start(); }
33 else { QMetaObject::invokeMethod(timer, "start", Qt::QueuedConnection); }
34 #endif
35 }
36
37 template <class T>
38 void SecureErase(T *p, size_t size)
39 {
40 static_assert(std::is_standard_layout<T>::value && std::is_trivially_destructible<T>::value,
41 "try to erase content of raw pointer, but type T isn't suitable");
42
43 std::memset(p, 0, size);
44 }
45
46 template <class T>
47 void SecureErase(T &obj)
48 {
49 static_assert(std::is_default_constructible<typename T::value_type>::value,
50 "container's value type must have a default constructor.");
51
52 for (typename T::iterator i = obj.begin(); i != obj.end(); ++i) {
53 *i = typename T::value_type{};
54 }
55 }
56
57 inline QString escapeToObjectPath(const QString &str)
58 {
59 if (str.isEmpty()) {
60 return "_";
61 }
62
63 auto ret = str;
64 QRegularExpression re{R"([^a-zA-Z0-9])"};
65 auto matcher = re.globalMatch(ret);
66 while (matcher.hasNext()) {
67 auto replaceList = matcher.next().capturedTexts();
68 replaceList.removeDuplicates();
69 for (const auto &c : replaceList) {
70 auto hexStr = QString::number(static_cast<uint>(c.front().toLatin1()), 16);
71 ret.replace(c, QString{R"(_%1)"}.arg(hexStr));
72 }
73 }
74 return ret;
75 }
76
77 inline QString unescapeFromObjectPath(const QString &str)
78 {
79 auto ret = str;
80 for (int i = 0; i < str.size(); ++i) {
81 if (str[i] == '_' && i + 2 < str.size()) {
82 auto hexStr = str.mid(i + 1, 2);
83 ret.replace(QString{"_%1"}.arg(hexStr), QChar::fromLatin1(hexStr.toUInt(nullptr, 16)));
84 i += 2;
85 }
86 }
87 return ret;
88 }
89
90 inline QString getAppIdFromAbsolutePath(const QString &path)
91 {
92 static QString desktopSuffix{u8".desktop"};
93 const auto &appDirs = QStandardPaths::standardLocations(QStandardPaths::ApplicationsLocation);
94 if (!path.endsWith(desktopSuffix) ||
95 !std::any_of(appDirs.cbegin(), appDirs.constEnd(), [&path](const QString &dir) { return path.startsWith(dir); })) {
96 return {};
97 }
98
99 auto tmp = path.chopped(desktopSuffix.size());
100 auto components = tmp.split(QDir::separator(), Qt::SkipEmptyParts);
101 auto location = std::find(components.cbegin(), components.cend(), "applications");
102 if (location == components.cend()) {
103 return {};
104 }
105
106 auto appId = QStringList{location + 1, components.cend()}.join('-');
107 return appId;
108 }
109
110 inline QStringList getAbsolutePathFromAppId(const QString &appId)
111 {
112 auto components = appId.split('-', Qt::SkipEmptyParts);
113 auto appDirs = QStandardPaths::standardLocations(QStandardPaths::ApplicationsLocation);
114
115 QStringList ret;
116 for (const auto &dirPath : appDirs) {
117 auto currentDir = dirPath;
118 for (auto it = components.cbegin(); it != components.cend(); ++it) {
119 auto currentName = QStringList{it, components.cend()}.join('-') + QString{".desktop"};
120 QDir dir{currentDir};
121 if (dir.exists(currentName)) {
122 ret.append(dir.filePath(currentName));
123 }
124
125 currentDir.append(QDir::separator() + *it);
126 }
127 }
128
129 return ret;
130 }
131 }