Changeset View
Changeset View
Standalone View
Standalone View
src/utils/directoryfindchilditem.cpp
- This file was added.
| 1 | #include <QtCore/QString> | ||||
|---|---|---|---|---|---|
| 2 | #include <QtCore/QUrl> | ||||
| 3 | #include <QtCore/QDir> | ||||
| 4 | #include "directoryfindchilditem.h" | ||||
| 5 | #include "QDebug" | ||||
| 6 | | ||||
| 7 | /** | ||||
| 8 | * Returns the item which is located in "directory" argument | ||||
| 9 | * when "filename" is an item located in it or one of its children. | ||||
| 10 | * Returns an empty QUrl when that is not the case | ||||
| 11 | * | ||||
| 12 | * <pre>directoryFindChildItem("~/dir", "~/dir/x/y/z") -> "~/dir/x"</pre> | ||||
| 13 | * | ||||
| 14 | * | ||||
| 15 | * @param directory | ||||
| 16 | * @param filename | ||||
| 17 | * @return | ||||
| 18 | */ | ||||
| 19 | QUrl directoryFindChildItem(const QUrl& directory, const QUrl& filename) | ||||
| 20 | { | ||||
| 21 | const QString dirStr = directory.adjusted(QUrl::StripTrailingSlash).toString(); | ||||
| 22 | const QString urlStr = filename.toString(); | ||||
| 23 | const int dirLen = dirStr.length() + 1; | ||||
| 24 | | ||||
| 25 | if (!urlStr.startsWith(dirStr)) { | ||||
| 26 | return QUrl(); | ||||
| 27 | } | ||||
| 28 | | ||||
| 29 | return QUrl(QStringLiteral("%1/%2").arg( | ||||
| 30 | dirStr, urlStr.mid(dirLen, urlStr.indexOf(QDir::separator(), dirLen + 1) - dirLen))); | ||||
| 31 | } | ||||