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
)
31 QVariant
NetworkSourcesModel::data( const QModelIndex
& index
, int role
) const
35 auto idx
= index
.row();
36 if ( idx
< 0 || (size_t)idx
>= m_items
.size() )
38 const auto& item
= m_items
[idx
];
46 return idx
== 0 ? TYPE_DUMMY
: TYPE_SOURCE
;
48 return item
.artworkUrl
;
54 QHash
<int, QByteArray
> NetworkSourcesModel::roleNames() const
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() )
72 void NetworkSourcesModel::setCtx(QmlMainContext
* ctx
)
76 m_ml
= vlc_ml_instance_get( m_ctx
->getIntf() );
79 initializeMediaSources();
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
);
100 bool NetworkSourcesModel::initializeMediaSources()
102 auto libvlc
= vlc_object_instance(m_ctx
->getIntf());
104 if (!m_items
.empty()) {
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 )
121 auto nbProviders
= vlc_media_source_meta_list_Count( providerList
.get() );
124 for ( auto i
= 0u; i
< nbProviders
; ++i
)
126 auto meta
= vlc_media_source_meta_list_Get( providerList
.get(), i
);
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
) );
150 return m_items
.empty() == false;