From 1c6c9e9c5a1dcb0c0d3be8bd41df6c4b000c7245 Mon Sep 17 00:00:00 2001 From: Andrzej Rybczak Date: Thu, 16 May 2013 20:55:29 +0200 Subject: [PATCH] browser: use boost::filesystem for local browsing --- configure.in | 10 ++++++++ src/actions.cpp | 10 +++----- src/browser.cpp | 68 +++++++++++++++++-------------------------------- src/browser.h | 2 +- 4 files changed, 38 insertions(+), 52 deletions(-) diff --git a/configure.in b/configure.in index 77f46750..76b04a07 100644 --- a/configure.in +++ b/configure.in @@ -107,6 +107,16 @@ AC_CHECK_HEADERS([boost/lexical_cast.hpp], , AC_CHECK_HEADERS([boost/algorithm/string.hpp], , AC_MSG_ERROR(boost/algorithm/string.hpp is missing)) +dnl ============================= +dnl = checking for boost.locale = +dnl ============================= +AC_CHECK_HEADERS([boost/filesystem.hpp], , + AC_MSG_ERROR(boost/filesystem.hpp is missing) +) +AC_CHECK_LIB(boost_filesystem$BOOST_LIB_SUFFIX, main, LDFLAGS="$LDFLAGS -lboost_filesystem$BOOST_LIB_SUFFIX", + AC_MSG_ERROR([no boost.filesystem library found]) +) + dnl ============================= dnl = checking for boost.locale = dnl ============================= diff --git a/src/actions.cpp b/src/actions.cpp index 6fc6fc1d..a93dab10 100644 --- a/src/actions.cpp +++ b/src/actions.cpp @@ -1976,13 +1976,9 @@ void ToggleBrowserSortMode::run() switch (Config.browser_sort_mode) { case smName: - if (!myBrowser->isLocal()) - { - Config.browser_sort_mode = smMTime; - Statusbar::msg("Sort songs by: Modification time"); - break; - } - // local browser doesn't support sorting by mtime, so we just skip it. + Config.browser_sort_mode = smMTime; + Statusbar::msg("Sort songs by: Modification time"); + break; case smMTime: Config.browser_sort_mode = smCustomFormat; Statusbar::msg("Sort songs by: Custom format"); diff --git a/src/browser.cpp b/src/browser.cpp index d9406774..b36644e8 100644 --- a/src/browser.cpp +++ b/src/browser.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include #include @@ -90,10 +91,6 @@ void Browser::switchTo() { SwitchTo::execute(this); - // local browser doesn't support sorting by mtime - if (isLocal() && Config.browser_sort_mode == smMTime) - Config.browser_sort_mode = smName; - if (w.empty()) GetDirectory(itsBrowsedDir); else @@ -420,7 +417,7 @@ void Browser::GetDirectory(std::string dir, std::string subdir) MPD::ItemList list; # ifndef WIN32 if (isLocal()) - GetLocalDirectory(list); + GetLocalDirectory(list, itsBrowsedDir, false); else Mpd.GetDirectory(dir, [&list](MPD::Item &&item) { list.push_back(item); @@ -471,61 +468,44 @@ void Browser::GetDirectory(std::string dir, std::string subdir) #ifndef WIN32 void Browser::GetLocalDirectory(MPD::ItemList &v, const std::string &directory, bool recursively) const { - DIR *dir = opendir((directory.empty() ? itsBrowsedDir : directory).c_str()); + namespace fs = boost::filesystem; - if (!dir) - return; - - dirent *file; - - struct stat file_stat; - std::string full_path; - - size_t old_size = v.size(); - while ((file = readdir(dir))) - { - // omit . and .. - if (file->d_name[0] == '.' && (file->d_name[1] == '\0' || (file->d_name[1] == '.' && file->d_name[2] == '\0'))) - continue; - - if (!Config.local_browser_show_hidden_files && file->d_name[0] == '.') - continue; - MPD::Item new_item; - full_path = directory.empty() ? itsBrowsedDir : directory; - if (itsBrowsedDir != "/") - full_path += "/"; - full_path += file->d_name; - stat(full_path.c_str(), &file_stat); - if (S_ISDIR(file_stat.st_mode)) + size_t start_size = v.size(); + fs::path dir(directory); + std::for_each(fs::directory_iterator(dir), fs::directory_iterator(), [&](fs::directory_entry &e) { + if (!Config.local_browser_show_hidden_files && e.path().filename().native()[0] == '.') + return; + MPD::Item item; + if (fs::is_directory(e)) { if (recursively) { - GetLocalDirectory(v, full_path, 1); - old_size = v.size(); + GetLocalDirectory(v, e.path().native(), true); + start_size = v.size(); } else { - new_item.type = itDirectory; - new_item.name = full_path; - v.push_back(new_item); + item.type = itDirectory; + item.name = e.path().native(); + v.push_back(item); } } - else if (hasSupportedExtension(file->d_name)) + else if (hasSupportedExtension(e.path().native())) { - new_item.type = itSong; - mpd_pair file_pair = { "file", full_path.c_str() }; + item.type = itSong; + mpd_pair file_pair = { "file", e.path().native().c_str() }; MPD::MutableSong *s = new MPD::MutableSong(mpd_song_begin(&file_pair)); - new_item.song = std::shared_ptr(s); + item.song = std::shared_ptr(s); # ifdef HAVE_TAGLIB_H if (!recursively) Tags::read(*s); # endif // HAVE_TAGLIB_H - v.push_back(new_item); + v.push_back(item); } - } - closedir(dir); - std::sort(v.begin()+old_size, v.end(), - LocaleBasedItemSorting(std::locale(), Config.ignore_leading_the, Config.browser_sort_mode)); + }); + + std::sort(v.begin()+start_size, v.end(), LocaleBasedItemSorting(std::locale(), + Config.ignore_leading_the, Config.browser_sort_mode)); } void Browser::ClearDirectory(const std::string &path) const diff --git a/src/browser.h b/src/browser.h index 81255d70..323c7ade 100644 --- a/src/browser.h +++ b/src/browser.h @@ -71,7 +71,7 @@ struct Browser: Screen>, Filterable, HasSongs, Searchable, T void LocateSong(const MPD::Song &); void GetDirectory(std::string, std::string = "/"); # ifndef WIN32 - void GetLocalDirectory(MPD::ItemList &, const std::string & = "", bool = 0) const; + void GetLocalDirectory(MPD::ItemList &, const std::string &, bool) const; void ClearDirectory(const std::string &) const; void ChangeBrowseMode(); bool deleteItem(const MPD::Item &);