2 * Copyright (C) 2012-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
11 #include "DatabaseUtils.h"
12 #include "LabelFormatter.h"
13 #include "SortFileItem.h"
27 SortAttributeNone
= 0x0,
28 SortAttributeIgnoreArticle
= 0x1,
29 SortAttributeIgnoreFolders
= 0x2,
30 SortAttributeUseArtistSortName
= 0x4,
31 SortAttributeIgnoreLabel
= 0x8
37 SortSpecialOnBottom
= 2
41 /// \defgroup List_of_sort_methods List of sort methods
42 /// \addtogroup List_of_sort_methods
44 /// \brief These ID's can be used with the \ref built_in_functions_6 "Container.SetSortMethod(id)" function
45 /// \note The on field named part with <b>String</b> shows the string used on
46 /// GUI to set this sort type.
53 /// __1__ : Sort by Name <em>(String: <b><c>Label</c></b>)</em>
55 /// __2__ : Sort by Date <em>(String: <b><c>Date</c></b>)</em>
57 /// __3__ : Sort by Size <em>(String: <b><c>Size</c></b>)</em>
59 /// __4__ : Sort by filename <em>(String: <b><c>File</c></b>)</em>
61 /// __5__ : Sort by path <em>(String: <b><c>Path</c></b>)</em>
63 /// __6__ : Sort by drive type <em>(String: <b><c>DriveType</c></b>)</em>
65 /// __7__ : Sort by title <em>(String: <b><c>Title</c></b>)</em>
67 /// __8__ : Sort by track number <em>(String: <b><c>TrackNumber</c></b>)</em>
69 /// __9__ : Sort by time <em>(String: <b><c>Time</c></b>)</em>
71 /// __10__ : Sort by artist <em>(String: <b><c>Artist</c></b>)</em>
73 /// __11__ : Sort by first artist then year <em>(String: <b><c>ArtistYear</c></b>)</em>
75 /// __12__ : Sort by album <em>(String: <b><c>Album</c></b>)</em>
77 /// __13__ : Sort by album type <em>(String: <b><c>AlbumType</c></b>)</em>
79 /// __14__ : Sort by genre <em>(String: <b><c>Genre</c></b>)</em>
81 /// __15__ : Sort by country <em>(String: <b><c>Country</c></b>)</em>
83 /// __16__ : Sort by year <em>(String: <b><c>Year</c></b>)</em>
85 /// __17__ : Sort by rating <em>(String: <b><c>Rating</c></b>)</em>
87 /// __18__ : Sort by user rating <em>(String: <b><c>UserRating</c></b>)</em>
89 /// __19__ : Sort by votes <em>(String: <b><c>Votes</c></b>)</em>
91 /// __20__ : Sort by top 250 <em>(String: <b><c>Top250</c></b>)</em>
93 /// __21__ : Sort by program count <em>(String: <b><c>ProgramCount</c></b>)</em>
95 /// __22__ : Sort by playlist order <em>(String: <b><c>Playlist</c></b>)</em>
97 /// __23__ : Sort by episode number <em>(String: <b><c>Episode</c></b>)</em>
99 /// __24__ : Sort by season <em>(String: <b><c>Season</c></b>)</em>
101 /// __25__ : Sort by number of episodes <em>(String: <b><c>TotalEpisodes</c></b>)</em>
102 SortByNumberOfEpisodes
,
103 /// __26__ : Sort by number of watched episodes <em>(String: <b><c>WatchedEpisodes</c></b>)</em>
104 SortByNumberOfWatchedEpisodes
,
105 /// __27__ : Sort by TV show status <em>(String: <b><c>TvShowStatus</c></b>)</em>
107 /// __28__ : Sort by TV show title <em>(String: <b><c>TvShowTitle</c></b>)</em>
109 /// __29__ : Sort by sort title <em>(String: <b><c>SortTitle</c></b>)</em>
111 /// __30__ : Sort by production code <em>(String: <b><c>ProductionCode</c></b>)</em>
112 SortByProductionCode
,
113 /// __31__ : Sort by MPAA <em>(String: <b><c>MPAA</c></b>)</em>
115 /// __32__ : Sort by video resolution <em>(String: <b><c>VideoResolution</c></b>)</em>
116 SortByVideoResolution
,
117 /// __33__ : Sort by video codec <em>(String: <b><c>VideoCodec</c></b>)</em>
119 /// __34__ : Sort by video aspect ratio <em>(String: <b><c>VideoAspectRatio</c></b>)</em>
120 SortByVideoAspectRatio
,
121 /// __35__ : Sort by audio channels <em>(String: <b><c>AudioChannels</c></b>)</em>
123 /// __36__ : Sort by audio codec <em>(String: <b><c>AudioCodec</c></b>)</em>
125 /// __37__ : Sort by audio language <em>(String: <b><c>AudioLanguage</c></b>)</em>
127 /// __38__ : Sort by subtitle language <em>(String: <b><c>SubtitleLanguage</c></b>)</em>
128 SortBySubtitleLanguage
,
129 /// __39__ : Sort by studio <em>(String: <b><c>Studio</c></b>)</em>
131 /// __40__ : Sort by date added <em>(String: <b><c>DateAdded</c></b>)</em>
133 /// __41__ : Sort by last played <em>(String: <b><c>LastPlayed</c></b>)</em>
135 /// __42__ : Sort by playcount <em>(String: <b><c>PlayCount</c></b>)</em>
137 /// __43__ : Sort by listener <em>(String: <b><c>Listeners</c></b>)</em>
139 /// __44__ : Sort by bitrate <em>(String: <b><c>Bitrate</c></b>)</em>
141 /// __45__ : Sort by random <em>(String: <b><c>Random</c></b>)</em>
143 /// __46__ : Sort by channel <em>(String: <b><c>Channel</c></b>)</em>
145 /// __47__ : Sort by channel number <em>(String: <b><c>ChannelNumber</c></b>)</em>
147 /// __48__ : Sort by date taken <em>(String: <b><c>DateTaken</c></b>)</em>
149 /// __49__ : Sort by relevance
151 /// __50__ : Sort by installation date <en>(String: <b><c>installdate</c></b>)</em>
153 /// __51__ : Sort by last updated <en>(String: <b><c>lastupdated</c></b>)</em>
155 /// __52__ : Sort by last used <em>(String: <b><c>lastused</c></b>)</em>
157 /// __53__ : Sort by client channel order <em>(String: <b><c>ClientChannelOrder</c></b>)</em>
158 SortByClientChannelOrder
,
159 /// __54__ : Sort by total number of discs <em>(String: <b><c>totaldiscs</c></b>)</em>
161 /// __55__ : Sort by original release date <em>(String: <b><c>Originaldate</c></b>)</em>
163 /// __56__ : Sort by BPM <em>(String: <b><c>bpm</c></b>)</em>
165 /// __57__ : Sort by original title <em>(String: <b><c>OriginalTitle</c></b>)</em>
167 /// __58__ : Sort by provider <em>(String: <b><c>Provider</c></b>)</em>
168 /// @skinning_v20 <b>SortByProvider</b> New sort method added.
170 /// __59__ : Sort by user preference <em>(String: <b><c>UserPreference</c></b>)</em>
171 /// @skinning_v20 <b>SortByUserPreference</b> New sort method added.
172 SortByUserPreference
,
176 typedef struct SortDescription
{
177 SortBy sortBy
= SortByNone
;
178 SortOrder sortOrder
= SortOrderAscending
;
179 SortAttribute sortAttributes
= SortAttributeNone
;
184 typedef struct GUIViewSortDetails
186 SortDescription m_sortDescription
;
188 LABEL_MASKS m_labelMasks
;
189 } GUIViewSortDetails
;
191 typedef DatabaseResult SortItem
;
192 typedef std::shared_ptr
<SortItem
> SortItemPtr
;
193 typedef std::vector
<SortItemPtr
> SortItems
;
198 static SORT_METHOD
TranslateOldSortMethod(SortBy sortBy
, bool ignoreArticle
);
199 static SortDescription
TranslateOldSortMethod(SORT_METHOD sortBy
);
201 static SortBy
SortMethodFromString(const std::string
& sortMethod
);
202 static const std::string
& SortMethodToString(SortBy sortMethod
);
203 static SortOrder
SortOrderFromString(const std::string
& sortOrder
);
204 static const std::string
& SortOrderToString(SortOrder sortOrder
);
206 /*! \brief retrieve the label id associated with a sort method for displaying in the UI.
207 \param sortBy the sort method in question.
208 \return the label id of the sort method.
210 static int GetSortLabel(SortBy sortBy
);
212 static void Sort(SortBy sortBy
, SortOrder sortOrder
, SortAttribute attributes
, DatabaseResults
& items
, int limitEnd
= -1, int limitStart
= 0);
213 static void Sort(SortBy sortBy
, SortOrder sortOrder
, SortAttribute attributes
, SortItems
& items
, int limitEnd
= -1, int limitStart
= 0);
214 static void Sort(const SortDescription
&sortDescription
, DatabaseResults
& items
);
215 static void Sort(const SortDescription
&sortDescription
, SortItems
& items
);
216 static bool SortFromDataset(const SortDescription
&sortDescription
, const MediaType
&mediaType
, const std::unique_ptr
<dbiplus::Dataset
> &dataset
, DatabaseResults
&results
);
218 static void GetFieldsForSQLSort(const MediaType
& mediaType
, SortBy sortMethod
, FieldList
& fields
);
219 static const Fields
& GetFieldsForSorting(SortBy sortBy
);
220 static std::string
RemoveArticles(const std::string
&label
);
222 typedef std::string (*SortPreparator
) (SortAttribute
, const SortItem
&);
223 typedef bool (*Sorter
) (const DatabaseResult
&, const DatabaseResult
&);
224 typedef bool (*SorterIndirect
) (const SortItemPtr
&, const SortItemPtr
&);
227 static const SortPreparator
& getPreparator(SortBy sortBy
);
228 static Sorter
getSorter(SortOrder sortOrder
, SortAttribute attributes
);
229 static SorterIndirect
getSorterIndirect(SortOrder sortOrder
, SortAttribute attributes
);
231 static std::map
<SortBy
, SortPreparator
> m_preparators
;
232 static std::map
<SortBy
, Fields
> m_sortingFields
;