Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sync: from linuxdeepin/dtkcore #71

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 38 additions & 41 deletions src/dci/private/ddcifileengine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#define DCI_FILE_SCHEME "dci:"
#define DCI_FILE_SUFFIX ".dci"

#if QT_VERSION >= QT_VERSION_CHECK(6, 8 ,0)
#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
std::unique_ptr<QAbstractFileEngine> DDciFileEngineHandler::create(const QString &fileName) const
#else
QAbstractFileEngine *DDciFileEngineHandler::create(const QString &fileName) const
Expand All @@ -39,7 +39,7 @@
if (!fileName.startsWith(QStringLiteral(DCI_FILE_SCHEME)))
return nullptr;

#if QT_VERSION >= QT_VERSION_CHECK(6, 8 ,0)
#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
std::unique_ptr<DDciFileEngine> engine(new DDciFileEngine(fileName));
#else
DDciFileEngine *engine = new DDciFileEngine(fileName);
Expand All @@ -56,7 +56,8 @@

// 共享同个线程内的同个 DDciFile
static thread_local QHash<QString, QWeakPointer<DDciFile>> sharedDciFile;
static void doDeleteSharedDciFile(const QString &path, DDciFile *file) {
static void doDeleteSharedDciFile(const QString &path, DDciFile *file)
{
int count = sharedDciFile.remove(path);
Q_ASSERT(count > 0);
delete file;
Expand All @@ -69,20 +70,18 @@
}

DDciFileShared shared(usePath ? new DDciFile(dciFilePath) : new DDciFile(),
std::bind(doDeleteSharedDciFile, dciFilePath,
std::placeholders::_1));
std::bind(doDeleteSharedDciFile, dciFilePath, std::placeholders::_1));
sharedDciFile[dciFilePath] = shared.toWeakRef();
return shared;
}

DDciFileEngineIterator::DDciFileEngineIterator(QDir::Filters filters, const QStringList &nameFilters)
#if QT_VERSION >= QT_VERSION_CHECK(6, 8 ,0)
#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
: QAbstractFileEngineIterator(nullptr, filters, nameFilters)
#else
: QAbstractFileEngineIterator(filters, nameFilters)
#endif
{

}

#if QT_VERSION < QT_VERSION_CHECK(6, 8, 0)
Expand All @@ -93,16 +92,15 @@
}
#endif

#if QT_VERSION >= QT_VERSION_CHECK(6, 8 ,0)
#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
bool DDciFileEngineIterator::advance()
#else
bool DDciFileEngineIterator::hasNext() const
#endif
{
if (!file) {
const auto paths = DDciFileEngine::resolvePath(path());
if (paths.first.isEmpty()
|| paths.second.isEmpty())
if (paths.first.isEmpty() || paths.second.isEmpty())
return false;

file = getDciFile(paths.first);
Expand All @@ -122,7 +120,7 @@
} else if (fileType == DDciFile::Symlink) {
if (filters.testFlag(QDir::NoSymLinks))
continue;
} else { // DDciFile::UnknowFile
} else { // DDciFile::UnknowFile
continue;
}

Expand All @@ -131,7 +129,7 @@
continue;

nextValid = i;
#if QT_VERSION >= QT_VERSION_CHECK(6, 8 ,0)
#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
current = nextValid;
#endif
return true;
Expand Down Expand Up @@ -200,8 +198,7 @@
}
}

if ((openMode & QIODevice::ExistingOnly)
|| !(openMode & QIODevice::WriteOnly)) {
if ((openMode & QIODevice::ExistingOnly) || !(openMode & QIODevice::WriteOnly)) {
if (!file->exists(subfilePath)) {
setError(QFile::OpenError, "The file is not exists");
return false;
Expand All @@ -222,8 +219,7 @@
#endif

// 不存在时尝试新建
if (!file->exists(subfilePath)
&& !file->writeFile(subfilePath, QByteArray())) {
if (!file->exists(subfilePath) && !file->writeFile(subfilePath, QByteArray())) {
return false;
}
}
Expand Down Expand Up @@ -513,24 +509,22 @@
QString DDciFileEngine::fileName(QAbstractFileEngine::FileName file) const
{
switch (file) {
case AbsoluteName:
case CanonicalName:
case DefaultName:
return QDir::cleanPath(DCI_FILE_SCHEME + dciFilePath + subfilePath);
case AbsolutePathName:
return QDir::cleanPath(DCI_FILE_SCHEME + dciFilePath);
case BaseName:
return QFileInfo(subfilePath).baseName();
case AbsoluteName:
case CanonicalName:
case DefaultName:
return QDir::cleanPath(DCI_FILE_SCHEME + dciFilePath + subfilePath);
case AbsolutePathName:
return QDir::cleanPath(DCI_FILE_SCHEME + dciFilePath);
case BaseName:
return QFileInfo(subfilePath).baseName();
#if QT_VERSION >= QT_VERSION_CHECK(6, 4, 0)
case AbsoluteLinkTarget:
case AbsoluteLinkTarget:
#else
case LinkName:
case LinkName:
#endif
return this->file->type(subfilePath) == DDciFile::Symlink
? this->file->symlinkTarget(subfilePath)
: QString();
default:
break;
return this->file->type(subfilePath) == DDciFile::Symlink ? this->file->symlinkTarget(subfilePath) : QString();
default:
break;
}

return QString();
Expand All @@ -545,8 +539,7 @@
subfilePath.clear();

const auto paths = resolvePath(fullPath, QString(), false);
if (paths.first.isEmpty()
|| paths.second.isEmpty())
if (paths.first.isEmpty() || paths.second.isEmpty())
return;

dciFilePath = paths.first;
Expand All @@ -566,19 +559,25 @@
}
#endif

#if QT_VERSION >= QT_VERSION_CHECK(6, 8 ,0)
QAbstractFileEngine::IteratorUniquePtr DDciFileEngine::beginEntryList(const QString &path, QDir::Filters filters, const QStringList &filterNames)
#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
QAbstractFileEngine::IteratorUniquePtr
DDciFileEngine::beginEntryList(const QString &path, QDir::Filters filters, const QStringList &filterNames)
{
return QAbstractFileEngine::IteratorUniquePtr(new DDciFileEngineIterator(filters, filterNames));
}
#else
DDciFileEngine::Iterator *DDciFileEngine::beginEntryList(QDir::Filters filters, const QStringList &filterNames)
#endif
{
#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
return QAbstractFileEngine::IteratorUniquePtr(new DDciFileEngineIterator(filters, filterNames));
#else
return new DDciFileEngineIterator(filters, filterNames);
#endif
}
#endif

Check warning on line 578 in src/dci/private/ddcifileengine.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

#endif without #if

#if QT_VERSION >= QT_VERSION_CHECK(6, 8 ,0)
#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
QAbstractFileEngine::IteratorUniquePtr DDciFileEngine::endEntryList()
#else
DDciFileEngine::Iterator *DDciFileEngine::endEntryList()
Expand Down Expand Up @@ -627,9 +626,7 @@
return flushToFile(&file, writeFile);
}

QPair<QString, QString> DDciFileEngine::resolvePath(const QString &fullPath,
const QString &realFilePath,
bool needRealFileExists)
QPair<QString, QString> DDciFileEngine::resolvePath(const QString &fullPath, const QString &realFilePath, bool needRealFileExists)
{
if (!fullPath.startsWith(QStringLiteral(DCI_FILE_SCHEME) + realFilePath))
return {};
Expand Down Expand Up @@ -671,8 +668,8 @@
return {};

subfilePath = QDir::cleanPath(formatFullPath.mid(schemeLength + dciFilePath.length()));
qCDebug(logFE(), "The DCI file path is: \"%s\", the subfile path is: \"%s\"",
qPrintable(dciFilePath), qPrintable(subfilePath));
qCDebug(
logFE(), "The DCI file path is: \"%s\", the subfile path is: \"%s\"", qPrintable(dciFilePath), qPrintable(subfilePath));
Q_ASSERT(!subfilePath.isEmpty());

return qMakePair(dciFilePath, subfilePath);
Expand Down
15 changes: 7 additions & 8 deletions src/dci/private/ddcifileengine_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ DCORE_BEGIN_NAMESPACE
class DDciFileEngineHandler : public QAbstractFileEngineHandler
{
public:
#if QT_VERSION >= QT_VERSION_CHECK(6, 8 ,0)
#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
std::unique_ptr<QAbstractFileEngine> create(const QString &fileName) const override;
#else
QAbstractFileEngine *create(const QString &fileName) const override;
Expand All @@ -36,10 +36,11 @@ using DDciFileShared = QSharedPointer<DDciFile>;
class DDciFileEngineIterator : public QAbstractFileEngineIterator
{
friend class DDciFileEngine;

public:
DDciFileEngineIterator(QDir::Filters filters, const QStringList &nameFilters);

#if QT_VERSION >= QT_VERSION_CHECK(6, 8 ,0)
#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
bool advance() override;
#else
QString next() override;
Expand All @@ -58,6 +59,7 @@ class DDciFileEngineIterator : public QAbstractFileEngineIterator
class DDciFileEngine : public QAbstractFileEngine
{
friend class DDciFileEngineIterator;

public:
explicit DDciFileEngine(const QString &fullPath);
~DDciFileEngine();
Expand Down Expand Up @@ -121,8 +123,7 @@ class DDciFileEngine : public QAbstractFileEngine
qint64 read(char *data, qint64 maxlen) override;
qint64 write(const char *data, qint64 len) override;

bool extension(Extension extension, const ExtensionOption *option = 0,
ExtensionReturn *output = 0) override;
bool extension(Extension extension, const ExtensionOption *option = 0, ExtensionReturn *output = 0) override;
bool supportsExtension(Extension extension) const override;

bool cloneTo(QAbstractFileEngine *target) override;
Expand All @@ -137,9 +138,8 @@ class DDciFileEngine : public QAbstractFileEngine
* 是 DCI 文件的内部路径。
* 函数返回的第一个数据是"真实文件路径"。
*/
static QPair<QString, QString> resolvePath(const QString &fullPath,
const QString &realFilePath = QString(),
bool needRealFileExists = true);
static QPair<QString, QString>
resolvePath(const QString &fullPath, const QString &realFilePath = QString(), bool needRealFileExists = true);

DDciFileShared file;
QString dciFilePath;
Expand All @@ -150,5 +150,4 @@ class DDciFileEngine : public QAbstractFileEngine
QBuffer *fileBuffer = nullptr;
};


DCORE_END_NAMESPACE
32 changes: 20 additions & 12 deletions src/filesystem/dcapfsfileengine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
extern QString _d_cleanPath(const QString &path);
extern bool _d_isSubFileOf(const QString &filePath, const QString &directoryPath);

#if QT_VERSION >= QT_VERSION_CHECK(6, 8 ,0)
#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
std::unique_ptr<QAbstractFileEngine> DCapFSFileEngineHandler::create(const QString &fileName) const
{
return std::unique_ptr<QAbstractFileEngine>(new DCapFSFileEngine(fileName));
Expand All @@ -34,14 +34,22 @@
return ret;
return DVtableHook::callOriginalFun(it, &QAbstractFileEngineIterator::hasNext);
}
#endif

#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
std::unique_ptr<QAbstractFileEngine> DCapFSFileEngineHandler::create(const QString &fileName) const
#else
QAbstractFileEngine *DCapFSFileEngineHandler::create(const QString &fileName) const
#endif
{
#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
return std::unique_ptr<QAbstractFileEngine>(new DCapFSFileEngine(fileName));
#else
return new DCapFSFileEngine(fileName);
#endif
}
#endif

Check warning on line 51 in src/filesystem/dcapfsfileengine.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

#endif without #if


class DCapFSFileEnginePrivate : public DObjectPrivate
{
D_DECLARE_PUBLIC(DCapFSFileEngine)
Expand All @@ -57,7 +65,6 @@
: DObjectPrivate(qq)
, file(file)
{

}

bool DCapFSFileEnginePrivate::canReadWrite(const QString &path) const
Expand All @@ -75,20 +82,16 @@
}

auto paths = DCapManager::instance()->paths();
return std::any_of(paths.cbegin(), paths.cend(),
std::bind(_d_isSubFileOf, target, std::placeholders::_1));
return std::any_of(paths.cbegin(), paths.cend(), std::bind(_d_isSubFileOf, target, std::placeholders::_1));
}

DCapFSFileEngine::DCapFSFileEngine(const QString &file)
: QFSFileEngine(file)
, DObject(*new DCapFSFileEnginePrivate(file, this))
{

}

DCapFSFileEngine::~DCapFSFileEngine()
{
}
DCapFSFileEngine::~DCapFSFileEngine() {}

#if QT_VERSION >= QT_VERSION_CHECK(6, 4, 0)
bool DCapFSFileEngine::open(QIODevice::OpenMode openMode, std::optional<QFile::Permissions> permissions)
Expand Down Expand Up @@ -217,17 +220,23 @@
return QFSFileEngine::entryList(filters, filterNames);
}

#if QT_VERSION >= QT_VERSION_CHECK(6, 8 ,0)
QAbstractFileEngine::IteratorUniquePtr DCapFSFileEngine::beginEntryList(const QString &path, QDir::Filters filters, const QStringList &filterNames)
#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
QAbstractFileEngine::IteratorUniquePtr
DCapFSFileEngine::beginEntryList(const QString &path, QDir::Filters filters, const QStringList &filterNames)
{
auto ret = QFSFileEngine::beginEntryList(path, filters, filterNames);
return ret;
}
#else
QAbstractFileEngine::Iterator *DCapFSFileEngine::beginEntryList(QDir::Filters filters, const QStringList &filterNames)
#endif
{
#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
auto ret = QFSFileEngine::beginEntryList(path, filters, filterNames);
#else
auto ret = QFSFileEngine::beginEntryList(filters, filterNames);
DVtableHook::overrideVfptrFun(ret, &QAbstractFileEngineIterator::hasNext, &capDirIteraterHasNext);
#endif
return ret;
}
#endif
Expand All @@ -239,4 +248,3 @@
}

DCORE_END_NAMESPACE

Loading