2021-03-26 20:27:35 +00:00
|
|
|
/*
|
|
|
|
* Sailfin: a Jellyfin client written using Qt
|
|
|
|
* Copyright (C) 2021 Chris Josten and the Sailfin Contributors.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2024-06-03 20:11:10 +00:00
|
|
|
#include <JellyfinQt/viewmodel/itemmodel.h>
|
2021-03-26 20:27:35 +00:00
|
|
|
|
2024-06-03 20:11:10 +00:00
|
|
|
#include <JellyfinQt/viewmodel/item.h>
|
2022-07-29 23:16:40 +00:00
|
|
|
|
2024-06-03 20:11:10 +00:00
|
|
|
#include <JellyfinQt/loader/http/artists.h>
|
|
|
|
#include <JellyfinQt/loader/http/items.h>
|
|
|
|
#include <JellyfinQt/loader/http/livetv.h>
|
|
|
|
#include <JellyfinQt/loader/http/userlibrary.h>
|
|
|
|
#include <JellyfinQt/loader/http/userviews.h>
|
|
|
|
#include <JellyfinQt/loader/http/tvshows.h>
|
2021-03-29 21:48:16 +00:00
|
|
|
|
2024-06-03 20:11:10 +00:00
|
|
|
#include <JellyfinQt/viewmodel/userdata.h>
|
|
|
|
#include <JellyfinQt/viewmodel/utils.h>
|
2021-08-17 14:43:17 +00:00
|
|
|
|
2021-03-26 20:27:35 +00:00
|
|
|
#define JF_CASE(roleName) case roleName: \
|
2021-03-29 15:10:25 +00:00
|
|
|
try { \
|
2021-08-11 21:35:33 +00:00
|
|
|
return QVariant(item->roleName()); \
|
2021-07-31 13:06:17 +00:00
|
|
|
} catch(std::bad_optional_access &e) { \
|
2021-03-29 15:10:25 +00:00
|
|
|
return QVariant(); \
|
|
|
|
}
|
2021-03-26 20:27:35 +00:00
|
|
|
|
|
|
|
namespace Jellyfin {
|
|
|
|
|
|
|
|
namespace ViewModel {
|
|
|
|
|
|
|
|
UserViewsLoader::UserViewsLoader(QObject *parent)
|
2021-03-29 21:48:16 +00:00
|
|
|
: UserViewsLoaderBase(new Jellyfin::Loader::HTTP::GetUserViewsLoader(), parent) { }
|
2021-03-26 20:27:35 +00:00
|
|
|
|
2021-03-29 21:48:16 +00:00
|
|
|
LatestMediaLoader::LatestMediaLoader(QObject *parent)
|
|
|
|
: LatestMediaBase(new Jellyfin::Loader::HTTP::GetLatestMediaLoader(), parent){ }
|
2021-03-26 20:27:35 +00:00
|
|
|
|
2021-03-29 21:48:16 +00:00
|
|
|
UserItemsLoader::UserItemsLoader(QObject *parent)
|
|
|
|
: UserItemsLoaderBase(new Jellyfin::Loader::HTTP::GetItemsByUserIdLoader(), parent) {}
|
2021-03-28 02:00:00 +00:00
|
|
|
|
2021-08-11 21:35:33 +00:00
|
|
|
ResumeItemsLoader::ResumeItemsLoader(QObject *parent)
|
|
|
|
: ResumeItemsLoaderBase(new Jellyfin::Loader::HTTP::GetResumeItemsLoader(), parent) {}
|
|
|
|
|
|
|
|
ShowSeasonsLoader::ShowSeasonsLoader(QObject *parent)
|
|
|
|
: ShowSeasonsLoaderBase(new Jellyfin::Loader::HTTP::GetSeasonsLoader(), parent) {}
|
|
|
|
|
|
|
|
ShowEpisodesLoader::ShowEpisodesLoader(QObject *parent)
|
|
|
|
: ShowEpisodesLoaderBase(new Jellyfin::Loader::HTTP::GetEpisodesLoader(), parent) {}
|
|
|
|
|
2021-09-09 03:57:41 +00:00
|
|
|
NextUpLoader::NextUpLoader(QObject *parent)
|
|
|
|
: NextUpLoaderBase(new Jellyfin::Loader::HTTP::GetNextUpLoader(), parent) {}
|
|
|
|
|
2022-07-29 12:26:25 +00:00
|
|
|
AlbumArtistLoader::AlbumArtistLoader(QObject *parent)
|
|
|
|
: AlbumArtistLoaderBase(new Jellyfin::Loader::HTTP::GetAlbumArtistsLoader(), parent) {}
|
|
|
|
|
2024-06-03 20:11:10 +00:00
|
|
|
LiveTvChannelsLoader::LiveTvChannelsLoader(QObject *parent)
|
|
|
|
: LiveTvChannelsLoaderBase(new Jellyfin::Loader::HTTP::GetLiveTvChannelsLoader(), parent) {}
|
|
|
|
|
2021-03-26 20:27:35 +00:00
|
|
|
ItemModel::ItemModel(QObject *parent)
|
2021-09-10 03:17:28 +00:00
|
|
|
: ApiModel<Model::Item>(parent) {
|
|
|
|
connect(this, &QAbstractItemModel::rowsInserted, this, &ItemModel::onInsertItems);
|
|
|
|
}
|
2021-03-26 20:27:35 +00:00
|
|
|
|
|
|
|
QVariant ItemModel::data(const QModelIndex &index, int role) const {
|
|
|
|
if (role <= Qt::UserRole || !index.isValid()) return QVariant();
|
|
|
|
int row = index.row();
|
|
|
|
if (row < 0 || row >= m_array.size()) return QVariant();
|
2021-08-11 21:35:33 +00:00
|
|
|
QSharedPointer<Model::Item> item = m_array[row];
|
2021-03-26 20:27:35 +00:00
|
|
|
switch(role) {
|
|
|
|
JF_CASE(jellyfinId)
|
|
|
|
JF_CASE(name)
|
|
|
|
JF_CASE(originalTitle)
|
|
|
|
JF_CASE(serverId)
|
|
|
|
JF_CASE(etag)
|
|
|
|
JF_CASE(sourceType)
|
|
|
|
JF_CASE(playlistItemId)
|
|
|
|
JF_CASE(dateCreated)
|
|
|
|
JF_CASE(dateLastMediaAdded)
|
|
|
|
JF_CASE(extraType)
|
2021-03-29 21:48:16 +00:00
|
|
|
// Handpicked, important ones
|
|
|
|
JF_CASE(imageTags)
|
2021-07-31 13:06:17 +00:00
|
|
|
JF_CASE(imageBlurHashes)
|
|
|
|
JF_CASE(mediaType)
|
|
|
|
JF_CASE(type)
|
|
|
|
JF_CASE(collectionType)
|
2021-08-11 21:35:33 +00:00
|
|
|
case RoleNames::indexNumber:
|
|
|
|
return QVariant(item->indexNumber().value_or(0));
|
|
|
|
case RoleNames::runTimeTicks:
|
|
|
|
return QVariant(item->runTimeTicks().value_or(0));
|
|
|
|
JF_CASE(artists)
|
2022-07-29 23:16:40 +00:00
|
|
|
case RoleNames::artistItems: {
|
|
|
|
auto artists = item->artistItems();
|
2022-08-24 15:00:14 +00:00
|
|
|
return wrapQVariantList<NameGuidPair, DTO::NameGuidPair>(artists.cbegin(), artists.cend(), const_cast<ItemModel *>(this));
|
2022-07-29 23:16:40 +00:00
|
|
|
}
|
2021-08-11 21:35:33 +00:00
|
|
|
case RoleNames::isFolder:
|
|
|
|
return QVariant(item->isFolder().value_or(false));
|
2021-09-09 03:57:41 +00:00
|
|
|
JF_CASE(overview)
|
2021-08-11 21:35:33 +00:00
|
|
|
case RoleNames::parentIndexNumber:
|
|
|
|
return QVariant(item->parentIndexNumber().value_or(1));
|
2021-08-17 14:43:17 +00:00
|
|
|
// UserData
|
|
|
|
case RoleNames::userDataRating:
|
|
|
|
return QVariant(item->userData()->rating().value_or(0.0));
|
|
|
|
case RoleNames::userDataPlayedPercentage:
|
|
|
|
return QVariant(item->userData()->playedPercentage().value_or(0.0));
|
|
|
|
case RoleNames::userDataUnplayedItemCount:
|
|
|
|
return QVariant(item->userData()->unplayedItemCount().value_or(0));
|
|
|
|
case RoleNames::userDataPlaybackPositionTicks:
|
|
|
|
return QVariant(item->userData()->playbackPositionTicks());
|
|
|
|
case RoleNames::userDataPlayCount:
|
|
|
|
return QVariant(item->userData()->playCount());
|
|
|
|
case RoleNames::userDataFavorite:
|
|
|
|
return QVariant(item->userData()->isFavorite());
|
|
|
|
case RoleNames::userDataLikes:
|
|
|
|
return QVariant(item->userData()->likes().value_or(false));
|
|
|
|
case RoleNames::userDataLastPlayedDate:
|
|
|
|
return QVariant(item->userData()->lastPlayedDate());
|
|
|
|
case RoleNames::userDataPlayed:
|
|
|
|
return QVariant(item->userData()->played());
|
|
|
|
case RoleNames::userDataKey:
|
|
|
|
return QVariant(item->userData()->key());
|
2024-06-03 20:11:10 +00:00
|
|
|
case RoleNames::currentProgramName:
|
|
|
|
if (item->currentProgram()) {
|
|
|
|
return QVariant(item->currentProgram()->name());
|
|
|
|
} else {
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
case RoleNames::currentProgramOverview:
|
|
|
|
if (item->currentProgram()) {
|
|
|
|
return QVariant(item->currentProgram()->overview());
|
|
|
|
} else {
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
case RoleNames::currentProgramStartDate:
|
|
|
|
if (item->currentProgram()) {
|
|
|
|
return QVariant(item->currentProgram()->startDate());
|
|
|
|
} else {
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
case RoleNames::currentProgramEndDate:
|
|
|
|
if (item->currentProgram()) {
|
|
|
|
return QVariant(item->currentProgram()->endDate());
|
|
|
|
} else {
|
|
|
|
return QVariant();
|
|
|
|
}
|
2021-03-26 20:27:35 +00:00
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-07-31 13:06:17 +00:00
|
|
|
QSharedPointer<Model::Item> ItemModel::itemAt(int index) {
|
2021-08-11 21:35:33 +00:00
|
|
|
return m_array[index];
|
2021-07-31 13:06:17 +00:00
|
|
|
}
|
|
|
|
|
2021-09-10 03:17:28 +00:00
|
|
|
|
|
|
|
void ItemModel::onInsertItems(const QModelIndex &parent, int start, int end) {
|
|
|
|
if (parent.isValid()) return;
|
2022-07-29 12:26:25 +00:00
|
|
|
//qDebug() << "Connecting " << (end - start + 1) << "items!";
|
2021-09-10 03:17:28 +00:00
|
|
|
for (int i = start; i <= end; i++) {
|
|
|
|
connect(itemAt(i).data(), &Model::Item::userDataChanged, this, &ItemModel::onUserDataUpdated);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ItemModel::onUserDataUpdated(const DTO::UserItemDataDto &newUserData) {
|
|
|
|
const QString &itemId = newUserData.itemId();
|
|
|
|
qDebug() << "ApiModel: item updated: " << itemId;
|
|
|
|
for (int i = 0; i < rowCount(QModelIndex()); i++) {
|
|
|
|
if (itemAt(i)->jellyfinId() == itemId) {
|
|
|
|
QModelIndex index = this->index(i);
|
|
|
|
emit this->dataChanged(index, index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-26 20:27:35 +00:00
|
|
|
} // NS ViewModel
|
|
|
|
|
|
|
|
} // NS Jellyfin
|