| 1 |
// SPDX-FileCopyrightText: 2017 - 2022 UnionTech Software Technology Co., Ltd. |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-3.0-or-later |
| 4 |
|
| 5 |
#include <QDBusInterface> |
| 6 |
#include <QDBusPendingCall> |
| 7 |
#include <QDebug> |
| 8 |
#include <QFile> |
| 9 |
|
| 10 |
#include "dfileservices.h" |
| 11 |
|
| 12 |
DCORE_BEGIN_NAMESPACE |
| 13 |
|
| 14 |
#define EASY_CALL_DBUS(name)\ |
| 15 |
QDBusInterface *interface = fileManager1DBusInterface();\ |
| 16 |
return interface && interface->call(#name, urls2uris(urls), startupId).type() != QDBusMessage::ErrorMessage; |
| 17 |
|
| 18 |
static QDBusInterface *fileManager1DBusInterface() |
| 19 |
{ |
| 20 |
static QDBusInterface interface(QStringLiteral("org.freedesktop.FileManager1"), |
| 21 |
QStringLiteral("/org/freedesktop/FileManager1"), |
| 22 |
QStringLiteral("org.freedesktop.FileManager1")); |
| 23 |
return &interface; |
| 24 |
} |
| 25 |
|
| 26 |
static QStringList urls2uris(const QList<QUrl> &urls) |
| 27 |
{ |
| 28 |
QStringList list; |
| 29 |
|
| 30 |
list.reserve(urls.size()); |
| 31 |
|
| 32 |
for (const QUrl &url : urls) { |
| 33 |
list << url.toString(); |
| 34 |
} |
| 35 |
|
| 36 |
return list; |
| 37 |
} |
| 38 |
|
| 39 |
static QList<QUrl> path2urls(const QList<QString> &paths) |
| 40 |
{ |
| 41 |
QList<QUrl> list; |
| 42 |
|
| 43 |
list.reserve(paths.size()); |
| 44 |
|
| 45 |
for (const QString &path : paths) { |
| 46 |
list << QUrl::fromLocalFile(path); |
| 47 |
} |
| 48 |
|
| 49 |
return list; |
| 50 |
} |
| 51 |
|
| 52 |
bool DFileServices::showFolder(QString localFilePath, const QString &startupId) |
| 53 |
{ |
| 54 |
return showFolder(QUrl::fromLocalFile(localFilePath), startupId); |
| 55 |
} |
| 56 |
|
| 57 |
bool DFileServices::showFolders(const QList<QString> localFilePaths, const QString &startupId) |
| 58 |
{ |
| 59 |
return showFolders(path2urls(localFilePaths), startupId); |
| 60 |
} |
| 61 |
|
| 62 |
bool DFileServices::showFolder(QUrl url, const QString &startupId) |
| 63 |
{ |
| 64 |
return showFolders(QList<QUrl>() << url, startupId); |
| 65 |
} |
| 66 |
|
| 67 |
bool DFileServices::showFolders(const QList<QUrl> urls, const QString &startupId) |
| 68 |
{ |
| 69 |
EASY_CALL_DBUS(ShowFolders) |
| 70 |
} |
| 71 |
|
| 72 |
bool DFileServices::showFileItemPropertie(QString localFilePath, const QString &startupId) |
| 73 |
{ |
| 74 |
return showFileItemPropertie(QUrl::fromLocalFile(localFilePath), startupId); |
| 75 |
} |
| 76 |
|
| 77 |
bool DFileServices::showFileItemProperties(const QList<QString> localFilePaths, const QString &startupId) |
| 78 |
{ |
| 79 |
return showFileItemProperties(path2urls(localFilePaths), startupId); |
| 80 |
} |
| 81 |
|
| 82 |
bool DFileServices::showFileItemPropertie(QUrl url, const QString &startupId) |
| 83 |
{ |
| 84 |
return showFileItemProperties(QList<QUrl>() << url, startupId); |
| 85 |
} |
| 86 |
|
| 87 |
bool DFileServices::showFileItemProperties(const QList<QUrl> urls, const QString &startupId) |
| 88 |
{ |
| 89 |
EASY_CALL_DBUS(ShowItemProperties) |
| 90 |
} |
| 91 |
|
| 92 |
bool DFileServices::showFileItem(QString localFilePath, const QString &startupId) |
| 93 |
{ |
| 94 |
return showFileItem(QUrl::fromLocalFile(localFilePath), startupId); |
| 95 |
} |
| 96 |
|
| 97 |
bool DFileServices::showFileItems(const QList<QString> localFilePaths, const QString &startupId) |
| 98 |
{ |
| 99 |
return showFileItems(path2urls(localFilePaths), startupId); |
| 100 |
} |
| 101 |
|
| 102 |
bool DFileServices::showFileItem(QUrl url, const QString &startupId) |
| 103 |
{ |
| 104 |
return showFileItems(QList<QUrl>() << url, startupId); |
| 105 |
} |
| 106 |
|
| 107 |
bool DFileServices::showFileItems(const QList<QUrl> urls, const QString &startupId) |
| 108 |
{ |
| 109 |
EASY_CALL_DBUS(ShowItems) |
| 110 |
} |
| 111 |
|
| 112 |
bool DFileServices::trash(QString localFilePath) |
| 113 |
{ |
| 114 |
return trash(QUrl::fromLocalFile(localFilePath)); |
| 115 |
} |
| 116 |
|
| 117 |
bool DFileServices::trash(const QList<QString> localFilePaths) |
| 118 |
{ |
| 119 |
return trash(path2urls(localFilePaths)); |
| 120 |
} |
| 121 |
|
| 122 |
bool DFileServices::trash(QUrl url) |
| 123 |
{ |
| 124 |
return trash(QList<QUrl>() << url); |
| 125 |
} |
| 126 |
|
| 127 |
bool DFileServices::trash(const QList<QUrl> urls) |
| 128 |
{ |
| 129 |
QDBusInterface *interface = fileManager1DBusInterface(); |
| 130 |
return interface && interface->call("Trash", urls2uris(urls)).type() != QDBusMessage::ErrorMessage; |
| 131 |
} |
| 132 |
|
| 133 |
QString DFileServices::errorMessage() |
| 134 |
{ |
| 135 |
return fileManager1DBusInterface()->lastError().message(); |
| 136 |
} |
| 137 |
|
| 138 |
DCORE_END_NAMESPACE |