Skip to content

Commit

Permalink
BrowseFeature, Linux: don't show /media/[user] dir, only its children
Browse files Browse the repository at this point in the history
  • Loading branch information
ronso0 committed Mar 3, 2024
1 parent 145a690 commit 83b4513
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
31 changes: 20 additions & 11 deletions src/library/browse/browsefeature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ const QString kViewName = QStringLiteral("BROWSEHOME");

const QString kQuickLinksSeparator = QStringLiteral("-+-");

const QStringList removableDriveRootPaths() {
QStringList paths;
#if defined(__LINUX__)
paths.append("/media");
const QString user = QString::fromLocal8Bit(qgetenv("USER"));
paths.append(QStringLiteral("/media/") + user);
paths.append(QStringLiteral("/run/media/") + user);
#endif
return paths;
}

} // anonymous namespace

BrowseFeature::BrowseFeature(
Expand Down Expand Up @@ -342,21 +353,19 @@ std::vector<std::unique_ptr<TreeItem>> createRemovableDevices() {
drive.filePath())); // Displays C:/
}
#elif defined(__LINUX__)
// To get devices on Linux, we look for directories under /media and
// /run/media/$USER.
QFileInfoList devices;

// Add folders under /media to devices.
devices += QDir("/media").entryInfoList(
QDir::AllDirs | QDir::NoDotAndDotDot);

// Add folders under /run/media/$USER to devices.
QDir run_media_user_dir(QStringLiteral("/run/media/") + QString::fromLocal8Bit(qgetenv("USER")));
devices += run_media_user_dir.entryInfoList(
QDir::AllDirs | QDir::NoDotAndDotDot);
for (const QString& path : removableDriveRootPaths()) {
devices += QDir(path).entryInfoList(QDir::AllDirs | QDir::NoDotAndDotDot);
}

// Convert devices into a QList<TreeItem*> for display.
for (const QFileInfo& device : std::as_const(devices)) {
// On Linux, devices can be mounted in /media and /media/user and /run/media/[user]
// but there's no benefit of displaying the [user] dir in Devices.
// Show its children but skip the dir itself.
if (removableDriveRootPaths().contains(device.absoluteFilePath())) {
continue;
}
ret.push_back(std::make_unique<TreeItem>(
device.fileName(),
QVariant(device.filePath() + QStringLiteral("/"))));
Expand Down
6 changes: 5 additions & 1 deletion src/library/browse/foldertreemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ class FolderTreeModel : public TreeItemModel {
virtual ~FolderTreeModel();
virtual bool hasChildren(const QModelIndex& parent = QModelIndex()) const;
bool directoryHasChildren(const QString& path) const;
void removeChildDirsFromCache(const QStringList& rootPaths);

private:
// Used for memoizing the results of directoryHasChildren
// Used for memorizing the results of directoryHasChildren.
// Note: this means we won't see directory tree changes after the initial
// tree population after first expansion. I.e. newly added directories won't
// be displayed and removed dirs will remain in the sidebar tree.
mutable QHash<QString, bool> m_directoryCache;
};

0 comments on commit 83b4513

Please sign in to comment.