Skip to content

Commit

Permalink
Merge pull request #7882 from Montellese/guilib_content_sorting
Browse files Browse the repository at this point in the history
[guilib] add support for sorting dynamic directory listings from <content>
  • Loading branch information
Montellese committed Sep 1, 2015
2 parents 2e60078 + af897a6 commit ea9faae
Show file tree
Hide file tree
Showing 6 changed files with 229 additions and 154 deletions.
84 changes: 4 additions & 80 deletions xbmc/interfaces/json-rpc/JSONUtils.h
Expand Up @@ -101,88 +101,12 @@ namespace JSONRPC
else
sortAttributes = SortAttributeNone;

if (order == "ascending")
sortOrder = SortOrderAscending;
else if (order == "descending")
sortOrder = SortOrderDescending;
else
sortOrder = SortUtils::SortOrderFromString(order);
if (sortOrder == SortOrderNone)
return false;

if (method == "none")
sortBy = SortByNone;
else if (method == "label")
sortBy = SortByLabel;
else if (method == "date")
sortBy = SortByDate;
else if (method == "size")
sortBy = SortBySize;
else if (method == "file")
sortBy = SortByFile;
else if (method == "path")
sortBy = SortByPath;
else if (method == "drivetype")
sortBy = SortByDriveType;
else if (method == "title")
sortBy = SortByTitle;
else if (method == "track")
sortBy = SortByTrackNumber;
else if (method == "time")
sortBy = SortByTime;
else if (method == "artist")
sortBy = SortByArtist;
else if (method == "album")
sortBy = SortByAlbum;
else if (method == "albumtype")
sortBy = SortByAlbumType;
else if (method == "genre")
sortBy = SortByGenre;
else if (method == "country")
sortBy = SortByCountry;
else if (method == "year")
sortBy = SortByYear;
else if (method == "rating")
sortBy = SortByRating;
else if (method == "votes")
sortBy = SortByVotes;
else if (method == "top250")
sortBy = SortByTop250;
else if (method == "programcount")
sortBy = SortByProgramCount;
else if (method == "playlist")
sortBy = SortByPlaylistOrder;
else if (method == "episode")
sortBy = SortByEpisodeNumber;
else if (method == "season")
sortBy = SortBySeason;
else if (method == "totalepisodes")
sortBy = SortByNumberOfEpisodes;
else if (method == "watchedepisodes")
sortBy = SortByNumberOfWatchedEpisodes;
else if (method == "tvshowstatus")
sortBy = SortByTvShowStatus;
else if (method == "tvshowtitle")
sortBy = SortByTvShowTitle;
else if (method == "sorttitle")
sortBy = SortBySortTitle;
else if (method == "productioncode")
sortBy = SortByProductionCode;
else if (method == "mpaa")
sortBy = SortByMPAA;
else if (method == "studio")
sortBy = SortByStudio;
else if (method == "dateadded")
sortBy = SortByDateAdded;
else if (method == "lastplayed")
sortBy = SortByLastPlayed;
else if (method == "playcount")
sortBy = SortByPlaycount;
else if (method == "listeners")
sortBy = SortByListeners;
else if (method == "bitrate")
sortBy = SortByBitrate;
else if (method == "random")
sortBy = SortByRandom;
else
sortBy = SortUtils::SortMethodFromString(method);
if (sortBy == SortByNone)
return false;

return true;
Expand Down
55 changes: 49 additions & 6 deletions xbmc/listproviders/DirectoryProvider.cpp
Expand Up @@ -22,7 +22,9 @@
#include "filesystem/Directory.h"
#include "filesystem/FavouritesDirectory.h"
#include "guilib/GUIWindowManager.h"
#include "settings/Settings.h"
#include "utils/JobManager.h"
#include "utils/SortUtils.h"
#include "utils/XMLUtils.h"
#include "utils/URIUtils.h"
#include "utils/Variant.h"
Expand All @@ -43,11 +45,15 @@ using namespace KODI::MESSAGING;
class CDirectoryJob : public CJob
{
public:
CDirectoryJob(const std::string &url, int limit, int parentID)
: m_url(url), m_limit(limit), m_parentID(parentID) {};
virtual ~CDirectoryJob() {};

virtual const char* GetType() const { return "directory"; };
CDirectoryJob(const std::string &url, SortDescription sort, int limit, int parentID)
: m_url(url),
m_sort(sort),
m_limit(limit),
m_parentID(parentID)
{ }
virtual ~CDirectoryJob() { }

virtual const char* GetType() const { return "directory"; }
virtual bool operator==(const CJob *job) const
{
if (strcmp(job->GetType(),GetType()) == 0)
Expand All @@ -64,6 +70,10 @@ class CDirectoryJob : public CJob
CFileItemList items;
if (CDirectory::GetDirectory(m_url, items, ""))
{
// sort the items if necessary
if (m_sort.sortBy != SortByNone)
items.Sort(m_sort);

// limit must not exceed the number of items
int limit = (m_limit == 0) ? items.Size() : std::min((int) m_limit, items.Size());
// convert to CGUIStaticItem's and set visibility and targets
Expand Down Expand Up @@ -128,6 +138,7 @@ class CDirectoryJob : public CJob
private:
std::string m_url;
std::string m_target;
SortDescription m_sort;
unsigned int m_limit;
int m_parentID;
std::vector<CGUIStaticItemPtr> m_items;
Expand All @@ -148,9 +159,19 @@ CDirectoryProvider::CDirectoryProvider(const TiXmlElement *element, int parentID
const char *target = element->Attribute("target");
if (target)
m_target.SetLabel(target, "", parentID);

const char *sortMethod = element->Attribute("sortby");
if (sortMethod)
m_sortMethod.SetLabel(sortMethod, "", parentID);

const char *sortOrder = element->Attribute("sortorder");
if (sortOrder)
m_sortOrder.SetLabel(sortOrder, "", parentID);

const char *limit = element->Attribute("limit");
if (limit)
m_limit.SetLabel(limit, "", parentID);

m_url.SetLabel(element->FirstChild()->ValueStr(), "", parentID);
}
}
Expand All @@ -176,6 +197,7 @@ bool CDirectoryProvider::Update(bool forceRefresh)

// update the URL & limit and fire off a new job if needed
fireJob |= UpdateURL();
fireJob |= UpdateSort();
fireJob |= UpdateLimit();
if (fireJob)
FireJob();
Expand Down Expand Up @@ -240,6 +262,8 @@ void CDirectoryProvider::Reset(bool immediately /* = false */)
m_currentTarget.clear();
m_currentUrl.clear();
m_itemTypes.clear();
m_currentSort.sortBy = SortByNone;
m_currentSort.sortOrder = SortOrderAscending;
m_currentLimit = 0;
m_updateState = OK;
RegisterListProvider(false);
Expand Down Expand Up @@ -292,7 +316,7 @@ void CDirectoryProvider::FireJob()
CSingleLock lock(m_section);
if (m_jobID)
CJobManager::GetInstance().CancelJob(m_jobID);
m_jobID = CJobManager::GetInstance().AddJob(new CDirectoryJob(m_currentUrl, m_currentLimit, m_parentID), this);
m_jobID = CJobManager::GetInstance().AddJob(new CDirectoryJob(m_currentUrl, m_currentSort, m_currentLimit, m_parentID), this);
}

void CDirectoryProvider::RegisterListProvider(bool hasLibraryContent)
Expand Down Expand Up @@ -333,3 +357,22 @@ bool CDirectoryProvider::UpdateLimit()

return true;
}

bool CDirectoryProvider::UpdateSort()
{
SortBy sortMethod(SortUtils::SortMethodFromString(m_sortMethod.GetLabel(m_parentID, false)));
SortOrder sortOrder(SortUtils::SortOrderFromString(m_sortOrder.GetLabel(m_parentID, false)));
if (sortOrder == SortOrderNone)
sortOrder = SortOrderAscending;

if (sortMethod == m_currentSort.sortBy && sortOrder == m_currentSort.sortOrder)
return false;

m_currentSort.sortBy = sortMethod;
m_currentSort.sortOrder = sortOrder;

if (CSettings::GetInstance().GetBool(CSettings::SETTING_FILELISTS_IGNORETHEWHENSORTING))
m_currentSort.sortAttributes = static_cast<SortAttribute>(m_currentSort.sortAttributes | SortAttributeIgnoreArticle);

return true;
}
4 changes: 4 additions & 0 deletions xbmc/listproviders/DirectoryProvider.h
Expand Up @@ -70,9 +70,12 @@ class CDirectoryProvider :
unsigned int m_jobID;
CGUIInfoLabel m_url;
CGUIInfoLabel m_target;
CGUIInfoLabel m_sortMethod;
CGUIInfoLabel m_sortOrder;
CGUIInfoLabel m_limit;
std::string m_currentUrl;
std::string m_currentTarget; ///< \brief node.target property on the list as a whole
SortDescription m_currentSort;
unsigned int m_currentLimit;
std::vector<CGUIStaticItemPtr> m_items;
std::vector<InfoTagType> m_itemTypes;
Expand All @@ -82,4 +85,5 @@ class CDirectoryProvider :
void RegisterListProvider(bool hasLibraryContent);
bool UpdateURL();
bool UpdateLimit();
bool UpdateSort();
};

0 comments on commit ea9faae

Please sign in to comment.