3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
6 Minitube is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 Minitube is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Minitube. If not, see <http://www.gnu.org/licenses/>.
21 #include "playlistmodel.h"
22 #include "mediaview.h"
23 #include "searchparams.h"
25 #include "videomimedata.h"
26 #include "videosource.h"
28 #include "ivchannelsource.h"
32 #include "ytjschannelsource.h"
33 #include "ytjssearch.h"
36 const QString recentKeywordsKey = "recentKeywords";
37 const QString recentChannelsKey = "recentChannels";
40 PlaylistModel::PlaylistModel(QWidget *parent) : QAbstractListModel(parent) {
41 videoSource = nullptr;
45 m_activeVideo = nullptr;
50 authorHovered = false;
51 authorPressed = false;
54 int PlaylistModel::rowCount(const QModelIndex & /*parent*/) const {
55 int count = videos.size();
57 // add the message item
58 if (videos.isEmpty() || !searching) count++;
63 QVariant PlaylistModel::data(const QModelIndex &index, int role) const {
64 int row = index.row();
66 if (row == videos.size()) {
71 return ItemTypeShowMore;
73 if (!errorMessage.isEmpty()) return errorMessage;
74 if (searching) return QString(); // tr("Searching...");
75 if (canSearchMore) return tr("Show %1 More").arg("").simplified();
77 return tr("No videos");
79 return tr("No more videos");
80 case Qt::TextAlignmentRole:
81 return QVariant(int(Qt::AlignHCenter | Qt::AlignVCenter));
82 case Qt::ForegroundRole:
83 return palette.color(QPalette::Dark);
84 case Qt::BackgroundRole:
85 if (!errorMessage.isEmpty())
86 return palette.color(QPalette::ToolTipBase);
93 } else if (row < 0 || row >= videos.size())
96 Video *video = videos.at(row);
100 return ItemTypeVideo;
102 return QVariant::fromValue(QPointer<Video>(video));
103 case ActiveTrackRole:
104 return video == m_activeVideo;
105 case Qt::DisplayRole:
106 return video->getTitle();
107 case HoveredItemRole:
108 return hoveredRow == index.row();
109 case AuthorHoveredRole:
110 return authorHovered;
111 case AuthorPressedRole:
112 return authorPressed;
114 case Qt::StatusTipRole:
115 return video->description();
122 void PlaylistModel::setActiveRow(int row, bool notify) {
123 if (rowExists(row)) {
125 Video *previousVideo = m_activeVideo;
126 m_activeVideo = videoAt(row);
128 int oldactiverow = m_activeRow;
130 if (rowExists(oldactiverow))
131 emit dataChanged(createIndex(oldactiverow, 0),
132 createIndex(oldactiverow, columnCount() - 1));
134 emit dataChanged(createIndex(m_activeRow, 0), createIndex(m_activeRow, columnCount() - 1));
135 if (notify) emit activeVideoChanged(m_activeVideo, previousVideo);
139 m_activeVideo = nullptr;
143 int PlaylistModel::nextRow() const {
144 int nextRow = m_activeRow + 1;
145 if (rowExists(nextRow)) return nextRow;
149 int PlaylistModel::previousRow() const {
150 int prevRow = m_activeRow - 1;
151 if (rowExists(prevRow)) return prevRow;
155 Video *PlaylistModel::videoAt(int row) const {
156 if (rowExists(row)) return videos.at(row);
160 Video *PlaylistModel::activeVideo() const {
161 return m_activeVideo;
164 void PlaylistModel::setVideoSource(VideoSource *videoSource) {
170 qDeleteAll(deletedVideos);
171 deletedVideos.clear();
173 m_activeVideo = nullptr;
178 this->videoSource = videoSource;
179 connect(videoSource, SIGNAL(gotVideos(QVector<Video *>)), SLOT(addVideos(QVector<Video *>)),
180 Qt::UniqueConnection);
181 connect(videoSource, SIGNAL(finished(int)), SLOT(searchFinished(int)), Qt::UniqueConnection);
182 connect(videoSource, SIGNAL(error(QString)), SLOT(searchError(QString)), Qt::UniqueConnection);
183 connect(videoSource, &QObject::destroyed, this,
184 [this, videoSource] {
185 if (this->videoSource == videoSource) {
186 this->videoSource = nullptr;
189 Qt::UniqueConnection);
191 canSearchMore = true;
195 void PlaylistModel::searchMore() {
196 if (!canSearchMore || videoSource == nullptr || searching) return;
198 firstSearch = startIndex == 1;
199 max = videoSource->maxResults();
200 if (max == 0) max = 20;
201 errorMessage.clear();
202 videoSource->loadVideos(max, startIndex);
206 void PlaylistModel::searchNeeded() {
207 const int desiredRowsAhead = 10;
208 int remainingRows = videos.size() - m_activeRow;
209 if (remainingRows < desiredRowsAhead) searchMore();
212 void PlaylistModel::abortSearch() {
213 QMutexLocker locker(&mutex);
215 if (videoSource) videoSource->abort();
221 m_activeVideo = nullptr;
226 void PlaylistModel::searchFinished(int total) {
229 canSearchMore = videoSource->hasMoreVideos();
231 // update the message item
232 emit dataChanged(createIndex(videos.size(), 0), createIndex(videos.size(), columnCount() - 1));
234 if (firstSearch && !videos.isEmpty()) handleFirstVideo(videos.at(0));
237 void PlaylistModel::searchError(const QString &message) {
238 errorMessage = message;
239 // update the message item
240 emit dataChanged(createIndex(videos.size(), 0), createIndex(videos.size(), columnCount() - 1));
243 void PlaylistModel::addVideos(const QVector<Video *> &newVideos) {
244 if (newVideos.isEmpty()) return;
245 videos.reserve(videos.size() + newVideos.size());
246 beginInsertRows(QModelIndex(), videos.size(), videos.size() + newVideos.size() - 2);
247 videos.append(newVideos);
249 for (Video *video : newVideos) {
250 connect(video, SIGNAL(gotThumbnail()), SLOT(updateVideoSender()), Qt::UniqueConnection);
251 video->loadThumbnail();
255 void PlaylistModel::handleFirstVideo(Video *video) {
257 int currentVideoRow = rowForCloneVideo(MediaView::instance()->getCurrentVideoId());
258 if (currentVideoRow != -1)
259 setActiveRow(currentVideoRow, false);
261 if (!settings.value("manualplay", false).toBool()) setActiveRow(0);
264 auto clazz = videoSource->metaObject()->className();
265 if (clazz == QLatin1String("YTSearch") || clazz == QLatin1String("IVSearch") ||
266 clazz == QLatin1String("IVChannelSource")) {
267 static const int maxRecentElements = 10;
269 SearchParams *searchParams;
270 if (clazz == QLatin1String("YTSearch")) {
271 auto search = qobject_cast<YTSearch *>(videoSource);
272 searchParams = search->getSearchParams();
273 } else if (clazz == QLatin1String("IVSearch")) {
274 auto search = qobject_cast<IVSearch *>(videoSource);
275 searchParams = search->getSearchParams();
276 } else if (clazz == QLatin1String("IVChannelSource")) {
277 auto search = qobject_cast<IVChannelSource *>(videoSource);
278 searchParams = search->getSearchParams();
279 } else if (clazz == QLatin1String("YTJSSearch")) {
280 auto search = qobject_cast<YTJSSearch *>(videoSource);
281 searchParams = search->getSearchParams();
282 } else if (clazz == QLatin1String("YTJSChannelSource")) {
283 auto search = qobject_cast<YTJSChannelSource *>(videoSource);
284 searchParams = search->getSearchParams();
288 QString query = searchParams->keywords();
289 if (!query.isEmpty() && !searchParams->isTransient()) {
290 if (query.startsWith("http://")) {
291 // Save the video title
292 query += "|" + videos.at(0)->getTitle();
294 QStringList keywords = settings.value(recentKeywordsKey).toStringList();
295 keywords.removeAll(query);
296 keywords.prepend(query);
297 while (keywords.size() > maxRecentElements)
298 keywords.removeLast();
299 settings.setValue(recentKeywordsKey, keywords);
303 QString channelId = searchParams->channelId();
304 if (!channelId.isEmpty() && !searchParams->isTransient()) {
306 if (!video->getChannelId().isEmpty() &&
307 video->getChannelId() != video->getChannelTitle())
308 value = video->getChannelId() + "|" + video->getChannelTitle();
310 value = video->getChannelTitle();
311 QStringList channels = settings.value(recentChannelsKey).toStringList();
312 channels.removeAll(value);
313 channels.removeAll(channelId);
314 channels.prepend(value);
315 while (channels.size() > maxRecentElements)
316 channels.removeLast();
317 settings.setValue(recentChannelsKey, channels);
322 void PlaylistModel::updateVideoSender() {
323 Video *video = static_cast<Video *>(sender());
325 qDebug() << "Cannot get sender";
328 int row = rowForVideo(video);
329 emit dataChanged(createIndex(row, 0), createIndex(row, columnCount() - 1));
332 void PlaylistModel::emitDataChanged() {
333 QModelIndex index = createIndex(rowCount() - 1, 0);
334 emit dataChanged(index, index);
339 bool PlaylistModel::removeRows(int position, int rows, const QModelIndex & /*parent*/) {
340 beginRemoveRows(QModelIndex(), position, position + rows - 1);
341 for (int row = 0; row < rows; ++row) {
342 Video *video = videos.takeAt(position);
348 void PlaylistModel::removeIndexes(QModelIndexList &indexes) {
349 QVector<Video *> originalList(videos);
350 for (const QModelIndex &index : indexes) {
351 if (index.row() >= originalList.size()) continue;
352 Video *video = originalList.at(index.row());
353 int idx = videos.indexOf(video);
355 beginRemoveRows(QModelIndex(), idx, idx);
356 deletedVideos.append(video);
357 if (m_activeVideo == video) {
358 m_activeVideo = nullptr;
361 videos.removeAll(video);
368 // --- Sturm und drang ---
370 Qt::DropActions PlaylistModel::supportedDropActions() const {
371 return Qt::CopyAction;
374 Qt::DropActions PlaylistModel::supportedDragActions() const {
375 return Qt::CopyAction;
378 Qt::ItemFlags PlaylistModel::flags(const QModelIndex &index) const {
379 if (index.isValid()) {
380 if (index.row() == videos.size()) {
381 // don't drag the "show more" item
382 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
384 return (Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled);
386 return Qt::ItemIsDropEnabled;
389 QStringList PlaylistModel::mimeTypes() const {
391 types << "application/x-minitube-video";
395 QMimeData *PlaylistModel::mimeData(const QModelIndexList &indexes) const {
396 VideoMimeData *mime = new VideoMimeData();
398 for (const QModelIndex &it : indexes) {
400 if (row >= 0 && row < videos.size()) mime->addVideo(videos.at(it.row()));
406 bool PlaylistModel::dropMimeData(const QMimeData *data,
407 Qt::DropAction action,
410 const QModelIndex &parent) {
411 if (action == Qt::IgnoreAction) return true;
413 if (!data->hasFormat("application/x-minitube-video")) return false;
415 if (column > 0) return false;
420 else if (parent.isValid())
421 beginRow = parent.row();
423 beginRow = rowCount(QModelIndex());
425 const VideoMimeData *videoMimeData = qobject_cast<const VideoMimeData *>(data);
426 if (!videoMimeData) return false;
428 const QVector<Video *> &droppedVideos = videoMimeData->getVideos();
429 for (Video *video : droppedVideos) {
431 int videoRow = videos.indexOf(video);
432 removeRows(videoRow, 1, QModelIndex());
434 // and then add them again at the new position
435 beginInsertRows(QModelIndex(), beginRow, beginRow);
436 videos.insert(beginRow, video);
440 // fix m_activeRow after all this
441 m_activeRow = videos.indexOf(m_activeVideo);
443 // let the MediaView restore the selection
444 emit needSelectionFor(droppedVideos);
449 int PlaylistModel::rowForCloneVideo(const QString &videoId) const {
450 if (videoId.isEmpty()) return -1;
451 for (int i = 0; i < videos.size(); ++i) {
452 Video *v = videos.at(i);
453 // qDebug() << "Comparing" << v->id() << videoId;
454 if (v->getId() == videoId) return i;
459 int PlaylistModel::rowForVideo(Video *video) {
460 return videos.indexOf(video);
463 QModelIndex PlaylistModel::indexForVideo(Video *video) {
464 return createIndex(videos.indexOf(video), 0);
467 void PlaylistModel::move(QModelIndexList &indexes, bool up) {
468 QVector<Video *> movedVideos;
470 for (const QModelIndex &index : indexes) {
471 int row = index.row();
472 if (row >= videos.size()) continue;
473 // qDebug() << "index row" << row;
474 Video *video = videoAt(row);
475 movedVideos << video;
478 int end = up ? -1 : rowCount() - 1, mod = up ? -1 : 1;
479 for (Video *video : movedVideos) {
480 int row = rowForVideo(video);
481 if (row + mod == end) {
485 // qDebug() << "video row" << row;
486 removeRows(row, 1, QModelIndex());
493 beginInsertRows(QModelIndex(), row, row);
494 videos.insert(row, video);
498 emit needSelectionFor(movedVideos);
503 void PlaylistModel::setHoveredRow(int row) {
504 int oldRow = hoveredRow;
506 emit dataChanged(createIndex(oldRow, 0), createIndex(oldRow, columnCount() - 1));
507 emit dataChanged(createIndex(hoveredRow, 0), createIndex(hoveredRow, columnCount() - 1));
510 void PlaylistModel::clearHover() {
511 int oldRow = hoveredRow;
513 emit dataChanged(createIndex(oldRow, 0), createIndex(oldRow, columnCount() - 1));
516 void PlaylistModel::updateHoveredRow() {
517 emit dataChanged(createIndex(hoveredRow, 0), createIndex(hoveredRow, columnCount() - 1));
520 /* clickable author */
522 void PlaylistModel::enterAuthorHover() {
523 if (authorHovered) return;
524 authorHovered = true;
528 void PlaylistModel::exitAuthorHover() {
529 if (!authorHovered) return;
530 authorHovered = false;
532 setHoveredRow(hoveredRow);
535 void PlaylistModel::enterAuthorPressed() {
536 if (authorPressed) return;
537 authorPressed = true;
541 void PlaylistModel::exitAuthorPressed() {
542 if (!authorPressed) return;
543 authorPressed = false;