10#include <QCoreApplication> 
   11#include <QRegularExpression> 
   12#include <QStandardPaths> 
   20template <
typename Func1>
 
   21inline void TimerSingleShot(
int msec,  Func1 slot)
 
   23#if QT_VERSION >= 0x050500 
   24    QTimer::singleShot(msec, slot);
 
   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); }
 
   38void SecureErase(T *p, 
size_t size)
 
   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");
 
   43    std::memset(p, 0, size);
 
   47void SecureErase(T &obj)
 
   49    static_assert(std::is_default_constructible<typename T::value_type>::value,
 
   50                  "container's value type must have a default constructor.");
 
   52    for (
typename T::iterator i = obj.begin(); i != obj.end(); ++i) {
 
   53        *i = 
typename T::value_type{};
 
   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)); 
 
   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)));
 
 
   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); })) {
 
   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()) {
 
  106    auto appId = QStringList{location + 1, components.cend()}.join(
'-');
 
 
  112    auto components = appId.split(
'-', Qt::SkipEmptyParts);
 
  113    auto appDirs = QStandardPaths::standardLocations(QStandardPaths::ApplicationsLocation);
 
  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));
 
  125            currentDir.append(QDir::separator() + *it);
 
 
QString escapeToObjectPath(const QString &str)
将字符串转义成符合DBus ObjectPath规则字符串
Definition dutil.h:57
 
QString unescapeFromObjectPath(const QString &str)
将DBus ObjectPath的部分路径转义成原来的字符串
Definition dutil.h:77
 
QStringList getAbsolutePathFromAppId(const QString &appId)
从appId中获取符合条件的Desktop文件路径
Definition dutil.h:110
 
QString getAppIdFromAbsolutePath(const QString &path)
从desktop文件的绝对路径中提取出AppId
Definition dutil.h:90