Skip to content

Commit

Permalink
fix: can't get subpath in tools
Browse files Browse the repository at this point in the history
  support to list subpath for applications.
  • Loading branch information
18202781743 committed Jan 26, 2024
1 parent 97df47d commit 048c0ab
Showing 1 changed file with 33 additions and 12 deletions.
45 changes: 33 additions & 12 deletions dconfig-center/common/helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <QTranslator>

using ResourceId = QString;
using ResourcePath = QString;
using AppId = QString;
using SubpathKey = QString;
using ResourceList = QList<ResourceId>;
Expand Down Expand Up @@ -72,7 +73,7 @@ static AppList applications(const QString &localPrefix = QString())
return result;
}

static QSet<ResourceId> resourcesForDirectory(const QString &dir)
static QSet<ResourcePath> resourcePathsForDirectory(const QString &dir)
{
QSet<ResourceId> result;
QDirIterator iterator(dir, QDir::Files);
Expand All @@ -83,19 +84,31 @@ static QSet<ResourceId> resourcesForDirectory(const QString &dir)
if (!file.fileName().endsWith(SUFFIX))
continue;

ResourceId resourceName = file.fileName().chopped(SUFFIX.size());
result.insert(resourceName);
result.insert(file.absoluteFilePath());
}
return result;
}

static ResourceList resourcesForApp(const QString &appid, const QString &localPrefix = QString())
static ResourceList resourcePathsForApp(const QString &appid, const QString &localPrefix = QString())
{
QSet<ResourceId> result;
QSet<ResourcePath> result;
result.reserve(50);
using namespace Dtk::Core;
for (auto item : DConfigMeta::applicationMetaDirs(localPrefix, appid)) {
result += resourcesForDirectory(item);
result += resourcePathsForDirectory(item);

Check warning on line 98 in dconfig-center/common/helper.hpp

View workflow job for this annotation

GitHub Actions / cppcheck

Consider using std::accumulate algorithm instead of a raw loop.
}
return result.values();
}

static ResourceList resourcesForApp(const QString &appid, const QString &localPrefix = QString())
{
QSet<ResourceId> result;
result.reserve(50);
for (auto item : resourcePathsForApp(appid, localPrefix)) {
const QFileInfo file(item);

ResourceId resourceName = file.fileName().chopped(SUFFIX.size());
result.insert(resourceName);
}
return result.values();
}
Expand All @@ -106,16 +119,22 @@ static ResourceList resourcesForAllApp(const QString &localPrefix = QString())
result.reserve(50);
using namespace Dtk::Core;
for (auto item : DConfigMeta::genericMetaDirs(localPrefix)) {
result += resourcesForDirectory(item);
for (auto resourcePath : resourcePathsForDirectory(item)) {

const QFileInfo file(resourcePath);

ResourceId resourceName = file.fileName().chopped(SUFFIX.size());
result.insert(resourceName);
}
}
return result.values();
}

static ResourceList subpathsForResource(const AppId &appid, const ResourceId &resourceId, const QString &localPrefix = QString())
static SubpathList subpathsForResource(const AppId &appid, const ResourceId &resourceId, const QString &localPrefix = QString())
{
SubpathList result;
for (auto item : resourcesForApp(appid, localPrefix)) {
QDir resourceDir(item);
for (auto item : resourcePathsForApp(appid, localPrefix)) {
QDir resourceDir(QFileInfo(item).absoluteDir());
auto filters = QDir::Dirs | QDir::NoDotAndDotDot;
resourceDir.setFilter(filters);
QDirIterator iterator(resourceDir, QDirIterator::Subdirectories);
Expand All @@ -125,6 +144,8 @@ static ResourceList subpathsForResource(const AppId &appid, const ResourceId &re
const QFileInfo &file(iterator.fileInfo());
if (QDir(file.absoluteFilePath()).exists(resourceId + SUFFIX)) {
auto subpath = file.absoluteFilePath().replace(resourceDir.absolutePath(), "");
if (result.contains(subpath))
continue;
result.append(subpath);
}
}
Expand All @@ -139,8 +160,8 @@ static bool existAppid(const QString &appid, const QString &localPrefix = QStrin

static bool existResource(const AppId &appid, const ResourceId &resourceId, const QString &localPrefix = QString())
{
const ResourceList &apps = resourcesForApp(appid, localPrefix);
if (apps.contains(resourceId))
const ResourceList &resources = resourcesForApp(appid, localPrefix);
if (resources.contains(resourceId))
return true;

const ResourceList &commons = resourcesForAllApp(localPrefix);
Expand Down

0 comments on commit 048c0ab

Please sign in to comment.