qt: retrieve the artwork when available in network models
[vlc.git] / modules / gui / qt / network / networkdevicemodel.hpp
blobe53c6a96d6b00e0f0c2279037d1e01fda2a60033
1 /*****************************************************************************
2 * Copyright (C) 2019 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 MLNETWORKDEVICEMODEL_HPP
20 #define MLNETWORKDEVICEMODEL_HPP
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
26 #include <QAbstractListModel>
28 #include <vlc_media_library.h>
29 #include <vlc_media_source.h>
30 #include <vlc_threads.h>
31 #include <vlc_cxx_helpers.hpp>
33 #include <util/qml_main_context.hpp>
34 #include "networksourcelistener.hpp"
36 #include <memory>
38 class NetworkDeviceModel : public QAbstractListModel, public NetworkSourceListener::SourceListenerCb
40 Q_OBJECT
41 public:
42 enum ItemType{
43 // qt version of input_item_type_e
44 TYPE_UNKNOWN = ITEM_TYPE_UNKNOWN,
45 TYPE_FILE,
46 TYPE_DIRECTORY,
47 TYPE_DISC,
48 TYPE_CARD,
49 TYPE_STREAM,
50 TYPE_PLAYLIST,
51 TYPE_NODE,
53 Q_ENUM( ItemType )
55 enum SDCatType{
56 // qt version of input_item_type_e
57 CAT_UNDEFINED = 0,
58 CAT_DEVICES = SD_CAT_DEVICES,
59 CAT_LAN,
60 CAT_INTERNET,
61 CAT_MYCOMPUTER,
63 Q_ENUM( SDCatType )
66 Q_PROPERTY(QmlMainContext* ctx READ getCtx WRITE setCtx NOTIFY ctxChanged)
67 Q_PROPERTY(SDCatType sd_source READ getSdSource WRITE setSdSource NOTIFY sdSourceChanged)
70 public:
71 NetworkDeviceModel( QObject* parent = nullptr );
73 QVariant data(const QModelIndex& index, int role) const override;
74 QHash<int, QByteArray> roleNames() const override;
75 int rowCount(const QModelIndex& parent) const override;
77 void setCtx(QmlMainContext* ctx);
78 void setSdSource(SDCatType s);
80 inline QmlMainContext* getCtx() { return m_ctx; }
81 inline SDCatType getSdSource() { return m_sdSource; }
83 Q_INVOKABLE bool addToPlaylist( int index );
84 Q_INVOKABLE bool addToPlaylist(const QVariantList& itemIdList);
85 Q_INVOKABLE bool addAndPlay( int index );
86 Q_INVOKABLE bool addAndPlay(const QVariantList& itemIdList);
88 signals:
89 void ctxChanged();
90 void sdSourceChanged();
92 private:
93 using MediaSourcePtr = vlc_shared_data_ptr_type(vlc_media_source_t,
94 vlc_media_source_Hold, vlc_media_source_Release);
96 using InputItemPtr = vlc_shared_data_ptr_type(input_item_t,
97 input_item_Hold,
98 input_item_Release);
100 struct Item
102 QString name;
103 QUrl mainMrl;
104 std::vector<QUrl> mrls;
105 QString protocol;
106 ItemType type;
107 MediaSourcePtr mediaSource;
108 InputItemPtr inputItem;
109 QUrl artworkUrl;
112 bool initializeMediaSources();
113 void onItemCleared( MediaSourcePtr mediaSource, input_item_node_t* node ) override;
114 void onItemAdded( MediaSourcePtr mediaSource, input_item_node_t* parent, input_item_node_t *const children[], size_t count ) override;
115 void onItemRemoved( MediaSourcePtr mediaSource, input_item_node_t * node, input_item_node_t *const children[], size_t count ) override;
116 inline void onItemPreparseEnded( MediaSourcePtr, input_item_node_t *, enum input_item_preparse_status ) override {}
118 void refreshDeviceList(MediaSourcePtr mediaSource, input_item_node_t* const children[], size_t count , bool clear);
120 private:
121 std::vector<Item> m_items;
122 QmlMainContext* m_ctx = nullptr;
123 vlc_medialibrary_t* m_ml = nullptr;
124 SDCatType m_sdSource = CAT_UNDEFINED;
126 std::vector<std::unique_ptr<NetworkSourceListener>> m_listeners;
129 #endif // MLNETWORKDEVICEMODEL_HPP