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 "playlistitemdelegate.h"
24 #include "searchparams.h"
26 #include "videomimedata.h"
27 #include "videosource.h"
29 #include "searchvideosource.h"
32 const QString recentKeywordsKey = "recentKeywords";
33 const QString recentChannelsKey = "recentChannels";
36 PlaylistModel::PlaylistModel(QWidget *parent) : QAbstractListModel(parent) {
37 videoSource = nullptr;
41 m_activeVideo = nullptr;
46 authorHovered = false;
47 authorPressed = false;
50 int PlaylistModel::rowCount(const QModelIndex & /*parent*/) const {
51 int count = videos.size();
53 // add the message item
54 if (videos.isEmpty() || !searching) count++;
59 QVariant PlaylistModel::data(const QModelIndex &index, int role) const {
60 int row = index.row();
62 if (row == videos.size()) {
67 return ItemTypeShowMore;
69 if (!errorMessage.isEmpty()) return errorMessage;
70 if (searching) return QString(); // tr("Searching...");
71 if (canSearchMore) return tr("Show %1 More").arg("").simplified();
73 return tr("No videos");
75 return tr("No more videos");
76 case Qt::TextAlignmentRole:
77 return QVariant(int(Qt::AlignHCenter | Qt::AlignVCenter));
78 case Qt::ForegroundRole:
79 return palette.color(QPalette::Dark);
80 case Qt::BackgroundRole:
81 if (!errorMessage.isEmpty())
82 return palette.color(QPalette::ToolTipBase);
89 } else if (row < 0 || row >= videos.size())
92 Video *video = videos.at(row);
98 return QVariant::fromValue(QPointer<Video>(video));
100 return video == m_activeVideo;
101 case Qt::DisplayRole:
102 return video->getTitle();
103 case HoveredItemRole:
104 return hoveredRow == index.row();
105 case AuthorHoveredRole:
106 return authorHovered;
107 case AuthorPressedRole:
108 return authorPressed;
110 case Qt::StatusTipRole:
111 return video->description();
118 void PlaylistModel::setActiveRow(int row, bool notify) {
119 if (rowExists(row)) {
121 Video *previousVideo = m_activeVideo;
122 m_activeVideo = videoAt(row);
124 int oldactiverow = m_activeRow;
126 if (rowExists(oldactiverow))
127 emit dataChanged(createIndex(oldactiverow, 0),
128 createIndex(oldactiverow, columnCount() - 1));
130 emit dataChanged(createIndex(m_activeRow, 0), createIndex(m_activeRow, columnCount() - 1));
131 if (notify) emit activeVideoChanged(m_activeVideo, previousVideo);
135 m_activeVideo = nullptr;
139 int PlaylistModel::nextRow() const {
140 int nextRow = m_activeRow + 1;
141 if (rowExists(nextRow)) return nextRow;
145 int PlaylistModel::previousRow() const {
146 int prevRow = m_activeRow - 1;
147 if (rowExists(prevRow)) return prevRow;
151 Video *PlaylistModel::videoAt(int row) const {
152 if (rowExists(row)) return videos.at(row);
156 Video *PlaylistModel::activeVideo() const {
157 return m_activeVideo;
160 void PlaylistModel::setVideoSource(VideoSource *videoSource) {
166 qDeleteAll(deletedVideos);
167 deletedVideos.clear();
169 m_activeVideo = nullptr;
174 this->videoSource = videoSource;
175 connect(videoSource, SIGNAL(gotVideos(QVector<Video *>)), SLOT(addVideos(QVector<Video *>)),
176 Qt::UniqueConnection);
177 connect(videoSource, SIGNAL(finished(int)), SLOT(searchFinished(int)), Qt::UniqueConnection);
178 connect(videoSource, SIGNAL(error(QString)), SLOT(searchError(QString)), Qt::UniqueConnection);
179 connect(videoSource, &QObject::destroyed, this,
180 [this, videoSource] {
181 if (this->videoSource == videoSource) {
182 this->videoSource = nullptr;
185 Qt::UniqueConnection);
187 canSearchMore = true;
191 void PlaylistModel::searchMore() {
192 if (!canSearchMore || videoSource == nullptr || searching) return;
194 firstSearch = startIndex == 1;
195 max = videoSource->maxResults();
196 if (max == 0) max = 20;
197 errorMessage.clear();
198 videoSource->loadVideos(max, startIndex);
202 void PlaylistModel::searchNeeded() {
203 const int desiredRowsAhead = 10;
204 int remainingRows = videos.size() - m_activeRow;
205 if (remainingRows < desiredRowsAhead) searchMore();
208 void PlaylistModel::abortSearch() {
209 QMutexLocker locker(&mutex);
211 if (videoSource) videoSource->abort();
217 m_activeVideo = nullptr;
222 void PlaylistModel::searchFinished(int total) {
225 canSearchMore = videoSource->hasMoreVideos();
227 // update the message item
228 emit dataChanged(createIndex(videos.size(), 0), createIndex(videos.size(), columnCount() - 1));
230 if (firstSearch && !videos.isEmpty()) handleFirstVideo(videos.at(0));
233 void PlaylistModel::searchError(const QString &message) {
234 errorMessage = message;
235 // update the message item
236 emit dataChanged(createIndex(videos.size(), 0), createIndex(videos.size(), columnCount() - 1));
239 void PlaylistModel::addVideos(const QVector<Video *> &newVideos) {
240 if (newVideos.isEmpty()) return;
241 videos.reserve(videos.size() + newVideos.size());
242 beginInsertRows(QModelIndex(), videos.size(), videos.size() + newVideos.size() - 2);
243 videos.append(newVideos);
245 for (Video *video : newVideos) {
246 connect(video, &Video::changed, this, [video, this] {
247 int row = rowForVideo(video);
248 emit dataChanged(createIndex(row, 0), createIndex(row, columnCount() - 1));
253 void PlaylistModel::handleFirstVideo(Video *video) {
255 int currentVideoRow = rowForCloneVideo(MediaView::instance()->getCurrentVideoId());
256 if (currentVideoRow != -1)
257 setActiveRow(currentVideoRow, false);
259 if (!settings.value("manualplay", false).toBool()) setActiveRow(0);
262 auto clazz = videoSource->metaObject()->className();
263 if (clazz == QLatin1String("SearchVideoSource")) {
264 auto search = qobject_cast<SearchVideoSource *>(videoSource);
265 SearchParams *searchParams = search->getSearchParams();
268 static const int maxRecentElements = 10;
269 QString query = searchParams->keywords();
270 if (!query.isEmpty() && !searchParams->isTransient()) {
271 if (query.startsWith("http://")) {
272 // Save the video title
273 query += "|" + videos.at(0)->getTitle();
275 QStringList keywords = settings.value(recentKeywordsKey).toStringList();
276 keywords.removeAll(query);
277 keywords.prepend(query);
278 while (keywords.size() > maxRecentElements)
279 keywords.removeLast();
280 settings.setValue(recentKeywordsKey, keywords);
284 QString channelId = searchParams->channelId();
285 if (!channelId.isEmpty() && !searchParams->isTransient()) {
287 if (!video->getChannelId().isEmpty() &&
288 video->getChannelId() != video->getChannelTitle())
289 value = video->getChannelId() + "|" + video->getChannelTitle();
291 value = video->getChannelTitle();
292 QStringList channels = settings.value(recentChannelsKey).toStringList();
293 channels.removeAll(value);
294 channels.removeAll(channelId);
295 channels.prepend(value);
296 while (channels.size() > maxRecentElements)
297 channels.removeLast();
298 settings.setValue(recentChannelsKey, channels);
303 void PlaylistModel::emitDataChanged() {
304 QModelIndex index = createIndex(rowCount() - 1, 0);
305 emit dataChanged(index, index);
310 bool PlaylistModel::removeRows(int position, int rows, const QModelIndex & /*parent*/) {
311 beginRemoveRows(QModelIndex(), position, position + rows - 1);
312 for (int row = 0; row < rows; ++row) {
313 Video *video = videos.takeAt(position);
319 void PlaylistModel::removeIndexes(QModelIndexList &indexes) {
320 QVector<Video *> originalList(videos);
321 for (const QModelIndex &index : indexes) {
322 if (index.row() >= originalList.size()) continue;
323 Video *video = originalList.at(index.row());
324 int idx = videos.indexOf(video);
326 beginRemoveRows(QModelIndex(), idx, idx);
327 deletedVideos.append(video);
328 if (m_activeVideo == video) {
329 m_activeVideo = nullptr;
332 videos.removeAll(video);
339 // --- Sturm und drang ---
341 Qt::DropActions PlaylistModel::supportedDropActions() const {
342 return Qt::CopyAction;
345 Qt::DropActions PlaylistModel::supportedDragActions() const {
346 return Qt::CopyAction;
349 Qt::ItemFlags PlaylistModel::flags(const QModelIndex &index) const {
350 if (index.isValid()) {
351 if (index.row() == videos.size()) {
352 // don't drag the "show more" item
353 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
355 return (Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled);
357 return Qt::ItemIsDropEnabled;
360 QStringList PlaylistModel::mimeTypes() const {
362 types << "application/x-minitube-video";
366 QMimeData *PlaylistModel::mimeData(const QModelIndexList &indexes) const {
367 VideoMimeData *mime = new VideoMimeData();
369 for (const QModelIndex &it : indexes) {
371 if (row >= 0 && row < videos.size()) mime->addVideo(videos.at(it.row()));
377 bool PlaylistModel::dropMimeData(const QMimeData *data,
378 Qt::DropAction action,
381 const QModelIndex &parent) {
382 if (action == Qt::IgnoreAction) return true;
384 if (!data->hasFormat("application/x-minitube-video")) return false;
386 if (column > 0) return false;
391 else if (parent.isValid())
392 beginRow = parent.row();
394 beginRow = rowCount(QModelIndex());
396 const VideoMimeData *videoMimeData = qobject_cast<const VideoMimeData *>(data);
397 if (!videoMimeData) return false;
399 const QVector<Video *> &droppedVideos = videoMimeData->getVideos();
400 for (Video *video : droppedVideos) {
402 int videoRow = videos.indexOf(video);
403 removeRows(videoRow, 1, QModelIndex());
405 // and then add them again at the new position
406 beginInsertRows(QModelIndex(), beginRow, beginRow);
407 if (beginRow >= videos.size()) {
408 videos.push_back(video);
410 videos.insert(beginRow, video);
415 // fix m_activeRow after all this
416 m_activeRow = videos.indexOf(m_activeVideo);
418 // let the MediaView restore the selection
419 emit needSelectionFor(droppedVideos);
424 int PlaylistModel::rowForCloneVideo(const QString &videoId) const {
425 if (videoId.isEmpty()) return -1;
426 for (int i = 0; i < videos.size(); ++i) {
427 Video *v = videos.at(i);
428 // qDebug() << "Comparing" << v->id() << videoId;
429 if (v->getId() == videoId) return i;
434 int PlaylistModel::rowForVideo(Video *video) {
435 return videos.indexOf(video);
438 QModelIndex PlaylistModel::indexForVideo(Video *video) {
439 return createIndex(videos.indexOf(video), 0);
442 void PlaylistModel::move(QModelIndexList &indexes, bool up) {
443 QVector<Video *> movedVideos;
445 for (const QModelIndex &index : indexes) {
446 int row = index.row();
447 if (row >= videos.size()) continue;
448 // qDebug() << "index row" << row;
449 Video *video = videoAt(row);
450 movedVideos << video;
453 int end = up ? -1 : rowCount() - 1, mod = up ? -1 : 1;
454 for (Video *video : movedVideos) {
455 int row = rowForVideo(video);
456 if (row + mod == end) {
460 // qDebug() << "video row" << row;
461 removeRows(row, 1, QModelIndex());
468 beginInsertRows(QModelIndex(), row, row);
469 videos.insert(row, video);
473 emit needSelectionFor(movedVideos);
478 void PlaylistModel::setHoveredRow(int row) {
479 int oldRow = hoveredRow;
481 emit dataChanged(createIndex(oldRow, 0), createIndex(oldRow, columnCount() - 1));
482 emit dataChanged(createIndex(hoveredRow, 0), createIndex(hoveredRow, columnCount() - 1));
485 void PlaylistModel::clearHover() {
486 int oldRow = hoveredRow;
488 emit dataChanged(createIndex(oldRow, 0), createIndex(oldRow, columnCount() - 1));
491 void PlaylistModel::updateHoveredRow() {
492 emit dataChanged(createIndex(hoveredRow, 0), createIndex(hoveredRow, columnCount() - 1));
495 /* clickable author */
497 void PlaylistModel::enterAuthorHover() {
498 if (authorHovered) return;
499 authorHovered = true;
503 void PlaylistModel::exitAuthorHover() {
504 if (!authorHovered) return;
505 authorHovered = false;
507 setHoveredRow(hoveredRow);
510 void PlaylistModel::enterAuthorPressed() {
511 if (authorPressed) return;
512 authorPressed = true;
516 void PlaylistModel::exitAuthorPressed() {
517 if (!authorPressed) return;
518 authorPressed = false;