qt: medialib: implement separate data loaders
[vlc.git] / modules / gui / qt / medialibrary / mlrecentsmodel.hpp
blobb58ebea0421e113e2d78cf91ab9b5bb6e9ed0d47
1 /*****************************************************************************
2 * Copyright (C) 2020 VLC authors and VideoLAN
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * ( at your option ) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
17 *****************************************************************************/
19 #ifndef ML_RECENTS_MODEL_H
20 #define ML_RECENTS_MODEL_H
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
26 #include <vlc_common.h>
27 #include <vlc_media_library.h>
29 #include "mlbasemodel.hpp"
30 #include "mlvideo.hpp"
32 #include <QObject>
33 #include <QDateTime>
35 class MLRecentMedia {
36 public:
37 MLRecentMedia( const vlc_ml_media_t *_data );
39 MLRecentMedia( const MLRecentMedia& url );
41 inline QUrl getUrl() const { return m_url; }
42 inline QDateTime getLastPlayedDate() const { return m_lastPlayedDate; }
43 inline MLParentId getId() const { return m_id; }
45 MLRecentMedia *clone() const;
47 private:
48 MLParentId m_id;
49 QUrl m_url;
50 QDateTime m_lastPlayedDate;
53 class MLRecentsModel : public MLSlidingWindowModel<MLRecentMedia>
55 Q_OBJECT
56 Q_PROPERTY(int numberOfItemsToShow READ getNumberOfItemsToShow WRITE setNumberOfItemsToShow)
58 public:
59 enum Roles {
60 RECENT_MEDIA_ID = Qt::UserRole + 1,
61 RECENT_MEDIA_URL,
62 RECENT_MEDIA_LAST_PLAYED_DATE
64 Q_ENUM(Roles)
66 explicit MLRecentsModel( QObject* parent = nullptr );
67 virtual ~MLRecentsModel() = default;
69 QVariant data( const QModelIndex& index , int role ) const override;
70 QHash<int, QByteArray> roleNames() const override;
71 int m_numberOfItemsToShow = -1;
73 Q_INVOKABLE void clearHistory();
75 void setNumberOfItemsToShow(int);
76 int getNumberOfItemsToShow() const;
78 protected:
79 ListCacheLoader<std::unique_ptr<MLRecentMedia>> *createLoader() const override;
81 private:
82 vlc_ml_sorting_criteria_t roleToCriteria( int /* role */ ) const override{
83 return VLC_ML_SORTING_DEFAULT;
85 vlc_ml_sorting_criteria_t nameToCriteria( QByteArray /* name */ ) const override{
86 return VLC_ML_SORTING_DEFAULT;
88 virtual void onVlcMlEvent( const MLEvent &event ) override;
90 struct Loader : public BaseLoader
92 Loader(const MLRecentsModel &model, int numberOfItemsToShow)
93 : BaseLoader(model)
94 , m_numberOfItemsToShow(numberOfItemsToShow)
98 size_t count() const override;
99 std::vector<std::unique_ptr<MLRecentMedia>> load(size_t index, size_t count) const override;
101 private:
102 int m_numberOfItemsToShow;
106 #endif // ML_RECENTS_MODEL_H