qt: add functions to insert into playlist in network models
[vlc.git] / modules / gui / qt / network / networksourcesmodel.cpp
blob15f20b55864d7541e23a84762ebf65f9435d4da9
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 #include "networksourcesmodel.hpp"
20 #include "networkmediamodel.hpp"
22 #include "playlist/media.hpp"
23 #include "playlist/playlist_controller.hpp"
25 NetworkSourcesModel::NetworkSourcesModel( QObject* parent )
26 : QAbstractListModel( parent )
27 , m_ml( nullptr )
31 QVariant NetworkSourcesModel::data( const QModelIndex& index, int role ) const
33 if (!m_ctx)
34 return {};
35 auto idx = index.row();
36 if ( idx < 0 || (size_t)idx >= m_items.size() )
37 return {};
38 const auto& item = m_items[idx];
39 switch ( role )
41 case SOURCE_NAME:
42 return item.name;
43 case SOURCE_LONGNAME:
44 return item.longName;
45 case SOURCE_TYPE:
46 return idx == 0 ? TYPE_DUMMY : TYPE_SOURCE;
47 case SOURCE_ARTWORK:
48 return item.artworkUrl;
49 default:
50 return {};
54 QHash<int, QByteArray> NetworkSourcesModel::roleNames() const
56 return {
57 { SOURCE_NAME, "name" },
58 { SOURCE_LONGNAME, "long_name" },
59 { SOURCE_TYPE, "type" },
60 { SOURCE_ARTWORK, "artwork" }
64 int NetworkSourcesModel::rowCount(const QModelIndex& parent) const
66 if ( parent.isValid() )
67 return 0;
68 return getCount();
72 void NetworkSourcesModel::setCtx(QmlMainContext* ctx)
74 if (ctx) {
75 m_ctx = ctx;
76 m_ml = vlc_ml_instance_get( m_ctx->getIntf() );
78 if (m_ctx) {
79 initializeMediaSources();
81 emit ctxChanged();
84 int NetworkSourcesModel::getCount() const
86 assert( m_items.size() < INT32_MAX );
87 return static_cast<int>( m_items.size() );
90 QMap<QString, QVariant> NetworkSourcesModel::getDataAt(int idx)
92 QMap<QString, QVariant> dataDict;
93 QHash<int,QByteArray> roles = roleNames();
94 for (auto role: roles.keys()) {
95 dataDict[roles[role]] = data(index(idx), role);
97 return dataDict;
100 bool NetworkSourcesModel::initializeMediaSources()
102 auto libvlc = vlc_object_instance(m_ctx->getIntf());
104 if (!m_items.empty()) {
105 beginResetModel();
106 endResetModel();
107 emit countChanged();
109 m_items = {Item{}}; // dummy item that UI uses to add entry for "add a service"
111 auto provider = vlc_media_source_provider_Get( libvlc );
113 using SourceMetaPtr = std::unique_ptr<vlc_media_source_meta_list_t,
114 decltype( &vlc_media_source_meta_list_Delete )>;
116 SourceMetaPtr providerList( vlc_media_source_provider_List( provider, static_cast<services_discovery_category_e>(m_sdSource) ),
117 &vlc_media_source_meta_list_Delete );
118 if ( providerList == nullptr )
119 return false;
121 auto nbProviders = vlc_media_source_meta_list_Count( providerList.get() );
123 beginResetModel();
124 for ( auto i = 0u; i < nbProviders; ++i )
126 auto meta = vlc_media_source_meta_list_Get( providerList.get(), i );
128 Item item;
129 item.name = qfu(meta->name);
130 item.longName = qfu(meta->longname);
132 if ( item.name.startsWith( "podcast" ) )
134 item.artworkUrl = QUrl::fromLocalFile(":/sidebar/podcast.svg");
136 else if ( item.name.startsWith("lua{") )
138 int i_head = item.name.indexOf( "sd='" ) + 4;
139 int i_tail = item.name.indexOf( '\'', i_head );
140 const QString iconName = QString( ":/sidebar/sd/%1.svg" ).arg( item.name.mid( i_head, i_tail - i_head ) );
141 item.artworkUrl = QFileInfo::exists( iconName ) ? QUrl::fromLocalFile( iconName )
142 : QUrl::fromLocalFile( ":/sidebar/network.svg" );
145 m_items.push_back( std::move(item) );
147 endResetModel();
148 emit countChanged();
150 return m_items.empty() == false;