Skip to content

Commit

Permalink
qt/network: Create StandardPathModel
Browse files Browse the repository at this point in the history
  • Loading branch information
3unjee authored and robUx4 committed Nov 24, 2022
1 parent 3318a93 commit 3819613
Show file tree
Hide file tree
Showing 5 changed files with 278 additions and 0 deletions.
3 changes: 3 additions & 0 deletions modules/gui/qt/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ libqt_plugin_la_SOURCES = \
gui/qt/network/networkmediamodel.hpp \
gui/qt/network/servicesdiscoverymodel.cpp \
gui/qt/network/servicesdiscoverymodel.hpp \
gui/qt/network/standardpathmodel.cpp \
gui/qt/network/standardpathmodel.hpp \
gui/qt/style/qtthemeprovider.hpp \
gui/qt/style/systempalette.cpp \
gui/qt/style/systempalette.hpp \
Expand Down Expand Up @@ -458,6 +460,7 @@ nodist_libqt_plugin_la_SOURCES = \
gui/qt/network/networksourcesmodel.moc.cpp \
gui/qt/network/networkmediamodel.moc.cpp \
gui/qt/network/servicesdiscoverymodel.moc.cpp \
gui/qt/network/standardpathmodel.moc.cpp \
gui/qt/style/systempalette.moc.cpp \
gui/qt/player/input_models.moc.cpp \
gui/qt/player/player_controller.moc.cpp \
Expand Down
2 changes: 2 additions & 0 deletions modules/gui/qt/maininterface/mainui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
#include "network/networkdevicemodel.hpp"
#include "network/networksourcesmodel.hpp"
#include "network/servicesdiscoverymodel.hpp"
#include "network/standardpathmodel.hpp"

#include "menus/qml_menu_wrapper.hpp"

Expand Down Expand Up @@ -254,6 +255,7 @@ void MainUI::registerQMLTypes()
qmlRegisterType<NetworkDeviceModel>( uri, versionMajor, versionMinor, "NetworkDeviceModel");
qmlRegisterType<NetworkSourcesModel>( uri, versionMajor, versionMinor, "NetworkSourcesModel");
qmlRegisterType<ServicesDiscoveryModel>( uri, versionMajor, versionMinor, "ServicesDiscoveryModel");
qmlRegisterType<StandardPathModel>( uri, versionMajor, versionMinor, "StandardPathModel");
qmlRegisterType<MLFoldersModel>( uri, versionMajor, versionMinor, "MLFolderModel");
qmlRegisterType<ImageLuminanceExtractor>( uri, versionMajor, versionMinor, "ImageLuminanceExtractor");

Expand Down
167 changes: 167 additions & 0 deletions modules/gui/qt/network/standardpathmodel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/*****************************************************************************
* Copyright (C) 2019 VLC authors and VideoLAN
*
* Authors: Benjamin Arnaud <[email protected]>
*
* 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 "standardpathmodel.hpp"

// VLC includes
#include "networkmediamodel.hpp"

// Ctor / dtor

StandardPathModel::StandardPathModel(QObject * parent)
: ClipListModel(parent)
{
m_comparator = ascendingName;

#ifdef Q_OS_UNIX
addItem(QVLCUserDir(VLC_HOME_DIR), qtr("Home"), QUrl());
#endif
addItem(QVLCUserDir(VLC_DESKTOP_DIR), qtr("Desktop"), QUrl());
addItem(QVLCUserDir(VLC_DOCUMENTS_DIR), qtr("Documents"), QUrl());
addItem(QVLCUserDir(VLC_MUSIC_DIR), qtr("Music"), QUrl());
addItem(QVLCUserDir(VLC_VIDEOS_DIR), qtr("Videos"), QUrl());
addItem(QVLCUserDir(VLC_DOWNLOAD_DIR), qtr("Download"), QUrl());

updateItems();
}

// QAbstractItemModel implementation

QHash<int, QByteArray> StandardPathModel::roleNames() const /* override */
{
return
{
{ PATH_NAME, "name" },
{ PATH_MRL, "mrl" },
{ PATH_PROTOCOL, "protocol" },
{ PATH_TYPE, "type" },
{ PATH_SOURCE, "source" },
{ PATH_TREE, "tree" },
{ PATH_ARTWORK, "artwork" }
};
}

QVariant StandardPathModel::data(const QModelIndex & index, int role) const /* override */
{
int row = index.row();

if (row < 0 || row >= count())
return QVariant();

const StandardPathItem & item = m_items[row];

switch (role)
{
case PATH_NAME:
return item.name;
case PATH_MRL:
return item.mrl;
case PATH_PROTOCOL:
return item.protocol;
case PATH_TYPE:
return item.type;
case PATH_TREE:
return QVariant::fromValue(NetworkTreeItem(item.tree, item.inputItem.get()));
case PATH_ARTWORK:
return item.artwork;
default:
return QVariant();
}
}

// Protected ClipListModel implementation

void StandardPathModel::onUpdateSort(const QString & criteria, Qt::SortOrder order) /* override */
{
if (criteria == "mrl")
{
if (order == Qt::AscendingOrder)
m_comparator = ascendingMrl;
else
m_comparator = descendingMrl;
}
else
{
if (order == Qt::AscendingOrder)
m_comparator = ascendingName;
else
m_comparator = descendingName;
}
}

// Private static function

/* static */ bool StandardPathModel::ascendingName(const StandardPathItem & a,
const StandardPathItem & b)
{
return (QString::compare(a.name, b.name, Qt::CaseInsensitive) <= 0);
}

/* static */ bool StandardPathModel::ascendingMrl(const StandardPathItem & a,
const StandardPathItem & b)
{
return (QString::compare(a.mrl.toString(), b.mrl.toString(), Qt::CaseInsensitive) <= 0);
}

/* static */ bool StandardPathModel::descendingName(const StandardPathItem & a,
const StandardPathItem & b)
{
return (QString::compare(a.name, b.name, Qt::CaseInsensitive) >= 0);
}

/* static */ bool StandardPathModel::descendingMrl(const StandardPathItem & a,
const StandardPathItem & b)
{
return (QString::compare(a.mrl.toString(), b.mrl.toString(), Qt::CaseInsensitive) >= 0);
}

// Private functions

void StandardPathModel::addItem(const QString & path, const QString & name, const QUrl & artwork)
{
QUrl url = QUrl::fromLocalFile(path);

StandardPathItem item;

item.name = name;
item.mrl = url;

item.protocol = url.scheme();

item.type = NetworkDeviceModel::TYPE_DIRECTORY;

input_item_t * inputItem = input_item_NewDirectory(qtu(url.toString()), qtu(name), ITEM_LOCAL);

item.inputItem = InputItemPtr(inputItem, false);

vlc_media_tree_t * tree = vlc_media_tree_New();

vlc_media_tree_Lock(tree);

vlc_media_tree_Add(tree, &(tree->root), inputItem);

vlc_media_tree_Unlock(tree);

item.tree = MediaTreePtr(tree, false);

item.artwork = artwork;

m_items.push_back(item);
}
104 changes: 104 additions & 0 deletions modules/gui/qt/network/standardpathmodel.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*****************************************************************************
* Copyright (C) 2019 VLC authors and VideoLAN
*
* Authors: Benjamin Arnaud <[email protected]>
*
* 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 STANDARDPATHMODEL_HPP
#define STANDARDPATHMODEL_HPP

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

// VLC includes
#include <vlc_media_source.h>
#include <vlc_cxx_helpers.hpp>
#include "networkdevicemodel.hpp"
#include "util/cliplistmodel.hpp"

// Qt includes
#include <QAbstractListModel>
#include <QStandardPaths>
#include <QUrl>

struct StandardPathItem;

class StandardPathModel : public ClipListModel<StandardPathItem>
{
Q_OBJECT

public: // Enums
// NOTE: Roles should be aligned with the NetworkDeviceModel.
enum Role
{
PATH_NAME = Qt::UserRole + 1,
PATH_MRL,
PATH_TYPE,
PATH_PROTOCOL,
PATH_SOURCE,
PATH_TREE,
PATH_ARTWORK
};

public: // Declarations
using InputItemPtr = vlc_shared_data_ptr_type(input_item_t,
input_item_Hold,
input_item_Release);

using MediaTreePtr = vlc_shared_data_ptr_type(vlc_media_tree_t,
vlc_media_tree_Hold,
vlc_media_tree_Release);

public:
StandardPathModel(QObject * parent = nullptr);

public: // QAbstractItemModel implementation
QHash<int, QByteArray> roleNames() const override;

QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const override;

protected: // ClipListModel implementation
void onUpdateSort(const QString & criteria, Qt::SortOrder order) override;

private: // Static functions
static bool ascendingName(const StandardPathItem & a, const StandardPathItem & b);
static bool ascendingMrl (const StandardPathItem & a, const StandardPathItem & b);

static bool descendingName(const StandardPathItem & a, const StandardPathItem & b);
static bool descendingMrl (const StandardPathItem & a, const StandardPathItem & b);

private: // Functions
void addItem(const QString & path, const QString & name, const QUrl & artwork);
};

struct StandardPathItem
{
QString name;
QUrl mrl;

QString protocol;

NetworkDeviceModel::ItemType type;

StandardPathModel::InputItemPtr inputItem;
StandardPathModel::MediaTreePtr tree;

QUrl artwork;
};

#endif // STANDARDPATHMODEL_HPP
2 changes: 2 additions & 0 deletions po/POTFILES.in
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,8 @@ modules/gui/qt/menus/menus.hpp
modules/gui/qt/menus/qml/Menubar.qml
modules/gui/qt/menus/qml_menu_wrapper.cpp
modules/gui/qt/menus/qml_menu_wrapper.hpp
modules/gui/qt/network/standardpathmodel.cpp
modules/gui/qt/network/standardpathmodel.hpp
modules/gui/qt/network/qml/BrowseDeviceHeader.qml
modules/gui/qt/network/qml/BrowseDeviceView.qml
modules/gui/qt/network/qml/BrowseDisplay.qml
Expand Down

0 comments on commit 3819613

Please sign in to comment.