[vlc-devel] [PATCH 16/18] qt: add MLUrlModel

Prince Gupta guptaprince8832 at gmail.com
Wed Sep 23 19:40:14 CEST 2020


---
 modules/gui/qt/Makefile.am                 |   3 +
 modules/gui/qt/maininterface/mainui.cpp    |   2 +
 modules/gui/qt/medialibrary/mlurlmodel.cpp | 149 +++++++++++++++++++++
 modules/gui/qt/medialibrary/mlurlmodel.hpp |  79 +++++++++++
 4 files changed, 233 insertions(+)
 create mode 100644 modules/gui/qt/medialibrary/mlurlmodel.cpp
 create mode 100644 modules/gui/qt/medialibrary/mlurlmodel.hpp

diff --git a/modules/gui/qt/Makefile.am b/modules/gui/qt/Makefile.am
index 2dd14ee319..ecce218951 100644
--- a/modules/gui/qt/Makefile.am
+++ b/modules/gui/qt/Makefile.am
@@ -157,6 +157,8 @@ libqt_plugin_la_SOURCES = \
 	gui/qt/medialibrary/mlqmltypes.hpp \
 	gui/qt/medialibrary/mlrecentsvideomodel.cpp \
 	gui/qt/medialibrary/mlrecentsvideomodel.hpp \
+	gui/qt/medialibrary/mlurlmodel.cpp \
+	gui/qt/medialibrary/mlurlmodel.hpp \
 	gui/qt/medialibrary/mlvideo.cpp \
 	gui/qt/medialibrary/mlvideo.hpp \
 	gui/qt/medialibrary/mlvideomodel.cpp \
@@ -315,6 +317,7 @@ nodist_libqt_plugin_la_SOURCES = \
 	gui/qt/medialibrary/mlgenremodel.moc.cpp \
 	gui/qt/medialibrary/mlqmltypes.moc.cpp \
 	gui/qt/medialibrary/mlrecentsvideomodel.moc.cpp \
+	gui/qt/medialibrary/mlurlmodel.moc.cpp \
 	gui/qt/medialibrary/mlvideo.moc.cpp \
 	gui/qt/medialibrary/mlvideomodel.moc.cpp \
 	gui/qt/menus/custom_menus.moc.cpp \
diff --git a/modules/gui/qt/maininterface/mainui.cpp b/modules/gui/qt/maininterface/mainui.cpp
index 80716c910f..624edb90c3 100644
--- a/modules/gui/qt/maininterface/mainui.cpp
+++ b/modules/gui/qt/maininterface/mainui.cpp
@@ -8,6 +8,7 @@
 #include "medialibrary/mlartistmodel.hpp"
 #include "medialibrary/mlalbumtrackmodel.hpp"
 #include "medialibrary/mlgenremodel.hpp"
+#include "medialibrary/mlurlmodel.hpp"
 #include "medialibrary/mlvideomodel.hpp"
 #include "medialibrary/mlrecentsvideomodel.hpp"
 #include "medialibrary/mlfoldersmodel.hpp"
@@ -162,6 +163,7 @@ void MainUI::registerQMLTypes()
         qmlRegisterType<MLArtistModel>( "org.videolan.medialib", 0, 1, "MLArtistModel" );
         qmlRegisterType<MLAlbumTrackModel>( "org.videolan.medialib", 0, 1, "MLAlbumTrackModel" );
         qmlRegisterType<MLGenreModel>( "org.videolan.medialib", 0, 1, "MLGenreModel" );
+        qmlRegisterType<MLUrlModel>( "org.videolan.medialib", 0, 1, "MLUrlModel" );
         qmlRegisterType<MLVideoModel>( "org.videolan.medialib", 0, 1, "MLVideoModel" );
         qmlRegisterType<MLRecentsVideoModel>( "org.videolan.medialib", 0, 1, "MLRecentsVideoModel" );
         qRegisterMetaType<NetworkTreeItem>();
diff --git a/modules/gui/qt/medialibrary/mlurlmodel.cpp b/modules/gui/qt/medialibrary/mlurlmodel.cpp
new file mode 100644
index 0000000000..5f48f3071e
--- /dev/null
+++ b/modules/gui/qt/medialibrary/mlurlmodel.cpp
@@ -0,0 +1,149 @@
+/*****************************************************************************
+ * Copyright (C) 2020 VLC authors and VideoLAN
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * ( at your option ) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#include "mlurlmodel.hpp"
+
+#include <QDateTime>
+
+MLUrlModel::MLUrlModel(QObject *parent)
+    : MLSlidingWindowModel<MLUrl>(parent)
+{
+}
+
+QVariant MLUrlModel::data(const QModelIndex &index, int role) const
+{
+    if (!index.isValid() || index.row() < 0)
+        return QVariant();
+
+    const MLUrl* ml_url = item(static_cast<unsigned int>(index.row()));
+    if ( !ml_url )
+        return QVariant();
+
+    switch (role)
+    {
+    case URL_ID:
+        return QVariant::fromValue( ml_url->getId() );
+    case URL_URL:
+        return QVariant::fromValue( ml_url->getUrl() );
+    case URL_LAST_PLAYED_DATE :
+        return QVariant::fromValue( ml_url->getLastPlayedDate() );
+    default :
+        return QVariant();
+    }
+}
+
+QHash<int, QByteArray> MLUrlModel::roleNames() const
+{
+    return {
+        { URL_ID, "id" },
+        { URL_URL, "url" },
+        { URL_LAST_PLAYED_DATE, "last_played_date" }
+    };
+}
+
+void MLUrlModel::addAndPlay( const QString &url )
+{
+    QMetaObject::invokeMethod(  this
+                              , [this, url]() {
+        ml_unique_ptr<vlc_ml_media_t> s{vlc_ml_get_media_by_mrl( m_ml, qtu( url ))};
+        if (!s) {
+            s.reset(vlc_ml_new_stream( m_ml, qtu( url ) ));
+        }
+        if (!s)
+            return;
+        MLParentId itemId( s->i_id, VLC_ML_PARENT_UNKNOWN );
+        m_mediaLib->addAndPlay(itemId);
+        emit resetRequested();
+    });
+}
+
+size_t MLUrlModel::countTotalElements() const
+{
+    auto queryParams = m_query_param;
+    queryParams.i_offset = 0;
+    queryParams.i_nbResults = 0;
+    auto s = vlc_ml_count_stream_history( m_ml, &queryParams );
+    return s;
+}
+
+std::vector<std::unique_ptr<MLUrl>> MLUrlModel::fetch()
+{
+    ml_unique_ptr<vlc_ml_media_list_t> media_list;
+    media_list.reset( vlc_ml_list_stream_history(m_ml, &m_query_param) );
+    if ( media_list == nullptr )
+        return {};
+
+    std::vector<std::unique_ptr<MLUrl>> res;
+    for( const vlc_ml_media_t& media: ml_range_iterate<vlc_ml_media_t>( media_list ) )
+        res.emplace_back( std::make_unique<MLUrl>( &media ) );
+    return res;
+}
+
+vlc_ml_sorting_criteria_t MLUrlModel::roleToCriteria(int role) const
+{
+    switch (role) {
+    case URL_URL :
+        return VLC_ML_SORTING_DEFAULT;
+    case URL_LAST_PLAYED_DATE :
+        return VLC_ML_SORTING_LASTMODIFICATIONDATE;
+    default:
+        return VLC_ML_SORTING_DEFAULT;
+    }
+}
+
+void MLUrlModel::onVlcMlEvent(const vlc_ml_event_t* event)
+{
+    switch (event->i_type)
+    {
+        case VLC_ML_EVENT_MEDIA_UPDATED:
+        case VLC_ML_EVENT_HISTORY_CHANGED:
+            m_need_reset = true;
+            break;
+    }
+    MLBaseModel::onVlcMlEvent( event );
+}
+
+MLUrl::MLUrl(const vlc_ml_media_t *_data)
+    : m_id( _data->i_id, VLC_ML_PARENT_UNKNOWN )
+    , m_url( _data->p_files->i_nb_items > 0 ? _data->p_files->p_items[0].psz_mrl : "" )
+    , m_lastPlayedDate(
+          QDateTime::fromTime_t( _data->i_last_played_date ).toString( QLocale::system().dateFormat( QLocale::ShortFormat ) )
+          )
+{
+}
+
+MLUrl::MLUrl(const MLUrl &url)
+    : m_id( url.m_id )
+    , m_url( url.m_url )
+    , m_lastPlayedDate( url.m_lastPlayedDate )
+{
+}
+
+QString MLUrl::getUrl() const
+{
+    return m_url;
+}
+
+QString MLUrl::getLastPlayedDate() const
+{
+    return m_lastPlayedDate;
+}
+
+MLUrl *MLUrl::clone() const {
+    return new MLUrl( *this );
+}
diff --git a/modules/gui/qt/medialibrary/mlurlmodel.hpp b/modules/gui/qt/medialibrary/mlurlmodel.hpp
new file mode 100644
index 0000000000..8feb87a4f8
--- /dev/null
+++ b/modules/gui/qt/medialibrary/mlurlmodel.hpp
@@ -0,0 +1,79 @@
+/*****************************************************************************
+ * Copyright (C) 2020 VLC authors and VideoLAN
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * ( at your option ) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#ifndef MLURLMODEL_H
+#define MLURLMODEL_H
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <QObject>
+#include "mlbasemodel.hpp"
+
+#include <vlc_media_library.h>
+#include "mlhelper.hpp"
+#include "mlqmltypes.hpp"
+
+class MLUrl {
+public:
+    MLUrl( const vlc_ml_media_t *_data );
+
+    MLUrl( const MLUrl& url );
+
+    QString getUrl() const;
+    QString getLastPlayedDate() const;
+    MLParentId getId() const { return m_id; }
+
+    MLUrl *clone() const;
+
+private:
+    MLParentId m_id;
+    QString m_url;
+    QString m_lastPlayedDate;
+};
+
+class MLUrlModel : public MLSlidingWindowModel<MLUrl>
+{
+    Q_OBJECT
+
+public:
+    enum Roles {
+        URL_ID = Qt::UserRole + 1,
+        URL_URL,
+        URL_LAST_PLAYED_DATE
+    };
+    Q_ENUM(Roles);
+
+    explicit MLUrlModel(QObject *parent = nullptr);
+
+    virtual ~MLUrlModel() = default;
+
+    QVariant data(const QModelIndex &index, int role) const override;
+    QHash<int, QByteArray> roleNames() const override;
+
+    Q_INVOKABLE void addAndPlay( const QString& url );
+
+private:
+    std::vector<std::unique_ptr<MLUrl>> fetch() override;
+    size_t countTotalElements() const override;
+    vlc_ml_sorting_criteria_t roleToCriteria(int role) const override;
+    virtual void onVlcMlEvent( const vlc_ml_event_t* event ) override;
+};
+
+#endif // MLURLMODEL_H
-- 
2.25.1



More information about the vlc-devel mailing list