forked from videolan/vlc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
qt/network: Create StandardPathModel
- Loading branch information
Showing
5 changed files
with
278 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters