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

Computer feature: add sidebar action "Refresh directory tree" #12908

Merged
merged 2 commits into from
May 13, 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
109 changes: 72 additions & 37 deletions src/library/browse/browsefeature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ BrowseFeature::BrowseFeature(
this,
&BrowseFeature::slotAddToLibrary);

m_pRefreshDirTreeAction = new QAction(tr("Refresh directory tree"), this);
connect(m_pRefreshDirTreeAction,
&QAction::triggered,
this,
&BrowseFeature::slotRefreshDirectoryTree);

m_proxyModel.setFilterCaseSensitivity(Qt::CaseInsensitive);
m_proxyModel.setSortCaseSensitivity(Qt::CaseInsensitive);
// BrowseThread sets the Qt::UserRole of every QStandardItem to the sort key
Expand Down Expand Up @@ -226,6 +232,24 @@ void BrowseFeature::slotRemoveQuickLink() {
saveQuickLinks();
}

void BrowseFeature::slotRefreshDirectoryTree() {
if (!m_pLastRightClickedItem) {
return;
}

const auto* pItem = m_pLastRightClickedItem;
if (!pItem->getData().isValid()) {
return;
}

const QString path = pItem->getData().toString();
m_pSidebarModel->removeChildDirsFromCache(QStringList{path});

// Update child items
const QModelIndex index = m_pSidebarModel->index(pItem->parentRow(), 0);
onLazyChildExpandation(index);
}

TreeItemModel* BrowseFeature::sidebarModel() const {
return m_pSidebarModel;
}
Expand Down Expand Up @@ -303,28 +327,29 @@ void BrowseFeature::onRightClickChild(const QPoint& globalPos, const QModelIndex

QMenu menu(m_pSidebarWidget);

// If this a QuickLink show only the Remove action
if (item->parent()->getData().toString() == QUICK_LINK_NODE) {
// This is a QuickLink
menu.addAction(m_pRemoveQuickLinkAction);
menu.addAction(m_pRefreshDirTreeAction);
menu.exec(globalPos);
onLazyChildExpandation(index);
return;
}

// If path is in the QuickLinks list show only the Remove action
foreach (const QString& str, m_quickLinkList) {
if (str == path) {
menu.addAction(m_pRemoveQuickLinkAction);
menu.exec(globalPos);
onLazyChildExpandation(index);
return;
}
}
if (m_quickLinkList.contains(path)) {
// Path is in the Quick Link list
menu.addAction(m_pRemoveQuickLinkAction);
menu.addAction(m_pRefreshDirTreeAction);
menu.exec(globalPos);
onLazyChildExpandation(index);
return;
}

menu.addAction(m_pAddQuickLinkAction);
menu.addAction(m_pAddtoLibraryAction);
menu.exec(globalPos);
onLazyChildExpandation(index);
menu.addAction(m_pAddQuickLinkAction);
menu.addAction(m_pAddtoLibraryAction);
menu.addAction(m_pRefreshDirTreeAction);
menu.exec(globalPos);
onLazyChildExpandation(index);
}

namespace {
Expand Down Expand Up @@ -423,34 +448,44 @@ void BrowseFeature::onLazyChildExpandation(const QModelIndex& index) {
#endif
folders = createRemovableDevices();
} else {
// we assume that the path refers to a folder in the file system
// populate children
const auto dirAccess = mixxx::FileAccess(mixxx::FileInfo(path));
folders = getChildDirectoryItems(path);
}

QFileInfoList all = dirAccess.info().toQDir().entryInfoList(
QDir::Dirs | QDir::NoDotAndDotDot);
if (!folders.empty()) {
m_pSidebarModel->insertTreeItemRows(std::move(folders), 0, index);
}
}

// loop through all the item and construct the children
foreach (QFileInfo one, all) {
// Skip folders that end with .app on OS X
std::vector<std::unique_ptr<TreeItem>> BrowseFeature::getChildDirectoryItems(
const QString& path) const {
std::vector<std::unique_ptr<TreeItem>> items;

if (path.isEmpty()) {
return items;
}
// we assume that the path refers to a folder in the file system
// populate children
const auto dirAccess = mixxx::FileAccess(mixxx::FileInfo(path));

QFileInfoList all = dirAccess.info().toQDir().entryInfoList(
QDir::Dirs | QDir::NoDotAndDotDot);

// loop through all the item and construct the children
foreach (QFileInfo one, all) {
// Skip folders that end with .app on OS X
#if defined(__APPLE__)
if (one.isDir() && one.fileName().endsWith(".app"))
continue;
if (one.isDir() && one.fileName().endsWith(".app"))
continue;
#endif
// We here create new items for the sidebar models
// Once the items are added to the TreeItemModel,
// the models takes ownership of them and ensures their deletion
folders.push_back(std::make_unique<TreeItem>(
one.fileName(),
QVariant(one.absoluteFilePath() + QStringLiteral("/"))));
}
}
// we need to check here if subfolders are found
// On Ubuntu 10.04, otherwise, this will draw an icon although the folder
// has no subfolders
if (!folders.empty()) {
m_pSidebarModel->insertTreeItemRows(std::move(folders), 0, index);
// We here create new items for the sidebar models
// Once the items are added to the TreeItemModel,
// the models takes ownership of them and ensures their deletion
items.push_back(std::make_unique<TreeItem>(
one.fileName(),
QVariant(one.absoluteFilePath() + QStringLiteral("/"))));
}

return items;
}

QString BrowseFeature::getRootViewHtml() const {
Expand Down
3 changes: 3 additions & 0 deletions src/library/browse/browsefeature.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class BrowseFeature : public LibraryFeature {
void slotAddQuickLink();
void slotRemoveQuickLink();
void slotAddToLibrary();
void slotRefreshDirectoryTree();
void activate() override;
void activateChild(const QModelIndex& index) override;
void onRightClickChild(const QPoint& globalPos, const QModelIndex& index) override;
Expand All @@ -58,6 +59,7 @@ class BrowseFeature : public LibraryFeature {
QString getRootViewHtml() const;
QString extractNameFromPath(const QString& spath);
QStringList getDefaultQuickLinks() const;
std::vector<std::unique_ptr<TreeItem>> getChildDirectoryItems(const QString& path) const;
void saveQuickLinks();
void loadQuickLinks();

Expand All @@ -69,6 +71,7 @@ class BrowseFeature : public LibraryFeature {
QAction* m_pAddQuickLinkAction;
QAction* m_pRemoveQuickLinkAction;
QAction* m_pAddtoLibraryAction;
QAction* m_pRefreshDirTreeAction;

// Caution: Make sure this is reset whenever the library tree is updated,
// so that the internalPointer() does not become dangling
Expand Down
Loading