qt: add functions to insert into playlist in network models
[vlc.git] / modules / gui / qt / network / networkdevicemodel.hpp
blobaf43d46fee555286348f455845e0872236996b79
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:
43 enum Role {
44 NETWORK_NAME = Qt::UserRole + 1,
45 NETWORK_MRL,
46 NETWORK_TYPE,
47 NETWORK_PROTOCOL,
48 NETWORK_SOURCE,
49 NETWORK_TREE,
50 NETWORK_ARTWORK,
53 enum ItemType{
54 // qt version of input_item_type_e
55 TYPE_UNKNOWN = ITEM_TYPE_UNKNOWN,
56 TYPE_FILE,
57 TYPE_DIRECTORY,
58 TYPE_DISC,
59 TYPE_CARD,
60 TYPE_STREAM,
61 TYPE_PLAYLIST,
62 TYPE_NODE,
64 Q_ENUM( ItemType )
66 enum SDCatType{
67 // qt version of input_item_type_e
68 CAT_UNDEFINED = 0,
69 CAT_DEVICES = SD_CAT_DEVICES,
70 CAT_LAN,
71 CAT_INTERNET,
72 CAT_MYCOMPUTER,
74 Q_ENUM( SDCatType )
77 Q_PROPERTY(QmlMainContext* ctx READ getCtx WRITE setCtx NOTIFY ctxChanged)
78 Q_PROPERTY(SDCatType sd_source READ getSdSource WRITE setSdSource NOTIFY sdSourceChanged)
79 Q_PROPERTY(QString name READ getName NOTIFY nameChanged)
80 Q_PROPERTY(QString source_name READ getSourceName WRITE setSourceName NOTIFY sourceNameChanged)
81 Q_PROPERTY(int count READ getCount NOTIFY countChanged)
83 public:
84 NetworkDeviceModel( QObject* parent = nullptr );
86 QVariant data(const QModelIndex& index, int role) const override;
87 QHash<int, QByteArray> roleNames() const override;
88 int rowCount(const QModelIndex& parent = {}) const override;
90 void setCtx(QmlMainContext* ctx);
91 void setSdSource(SDCatType s);
92 void setSourceName(const QString& sourceName);
94 inline QmlMainContext* getCtx() { return m_ctx; }
95 inline SDCatType getSdSource() { return m_sdSource; }
96 inline QString getName() { return m_name; }
97 inline QString getSourceName() { return m_sourceName; }
99 int getCount() const;
101 Q_INVOKABLE bool insertIntoPlaylist( const QModelIndexList& itemIdList, ssize_t playlistIndex );
102 Q_INVOKABLE bool addToPlaylist( int index );
103 Q_INVOKABLE bool addToPlaylist(const QVariantList& itemIdList);
104 Q_INVOKABLE bool addToPlaylist(const QModelIndexList& itemIdList);
105 Q_INVOKABLE bool addAndPlay( int index );
106 Q_INVOKABLE bool addAndPlay(const QVariantList& itemIdList);
107 Q_INVOKABLE bool addAndPlay(const QModelIndexList& itemIdList);
109 Q_INVOKABLE QMap<QString, QVariant> getDataAt(int index);
111 signals:
112 void ctxChanged();
113 void sdSourceChanged();
114 void sourceNameChanged();
115 void nameChanged();
116 void countChanged();
118 private:
119 using MediaSourcePtr = vlc_shared_data_ptr_type(vlc_media_source_t,
120 vlc_media_source_Hold, vlc_media_source_Release);
122 using InputItemPtr = vlc_shared_data_ptr_type(input_item_t,
123 input_item_Hold,
124 input_item_Release);
126 struct Item
128 QString name;
129 QUrl mainMrl;
130 std::vector<QUrl> mrls;
131 QString protocol;
132 ItemType type;
133 MediaSourcePtr mediaSource;
134 InputItemPtr inputItem;
135 QUrl artworkUrl;
138 bool initializeMediaSources();
139 void onItemCleared( MediaSourcePtr mediaSource, input_item_node_t* node ) override;
140 void onItemAdded( MediaSourcePtr mediaSource, input_item_node_t* parent, input_item_node_t *const children[], size_t count ) override;
141 void onItemRemoved( MediaSourcePtr mediaSource, input_item_node_t * node, input_item_node_t *const children[], size_t count ) override;
142 inline void onItemPreparseEnded( MediaSourcePtr, input_item_node_t *, enum input_item_preparse_status ) override {}
144 void refreshDeviceList(MediaSourcePtr mediaSource, input_item_node_t* const children[], size_t count , bool clear);
146 private:
147 std::vector<Item> m_items;
148 QmlMainContext* m_ctx = nullptr;
149 vlc_medialibrary_t* m_ml = nullptr;
150 SDCatType m_sdSource = CAT_UNDEFINED;
151 QString m_sourceName; // '*' -> all sources
152 QString m_name; // source long name
154 std::vector<std::unique_ptr<NetworkSourceListener>> m_listeners;
157 #endif // MLNETWORKDEVICEMODEL_HPP