]> git.sur5r.net Git - minitube/blob - src/playlistmodel.cpp
Update upstream source from tag 'upstream/3.5.1'
[minitube] / src / playlistmodel.cpp
1 /* $BEGIN_LICENSE
2
3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
5
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.
10
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.
15
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/>.
18
19 $END_LICENSE */
20
21 #include "playlistmodel.h"
22 #include "mediaview.h"
23 #include "searchparams.h"
24 #include "video.h"
25 #include "videomimedata.h"
26 #include "videosource.h"
27
28 #include "ivchannelsource.h"
29 #include "ivsearch.h"
30 #include "ytsearch.h"
31
32 namespace {
33 const QString recentKeywordsKey = "recentKeywords";
34 const QString recentChannelsKey = "recentChannels";
35 } // namespace
36
37 PlaylistModel::PlaylistModel(QWidget *parent) : QAbstractListModel(parent) {
38     videoSource = nullptr;
39     searching = false;
40     canSearchMore = true;
41     firstSearch = false;
42     m_activeVideo = nullptr;
43     m_activeRow = -1;
44     startIndex = 1;
45     max = 0;
46     hoveredRow = -1;
47     authorHovered = false;
48     authorPressed = false;
49 }
50
51 int PlaylistModel::rowCount(const QModelIndex & /*parent*/) const {
52     int count = videos.size();
53
54     // add the message item
55     if (videos.isEmpty() || !searching) count++;
56
57     return count;
58 }
59
60 QVariant PlaylistModel::data(const QModelIndex &index, int role) const {
61     int row = index.row();
62
63     if (row == videos.size()) {
64         QPalette palette;
65
66         switch (role) {
67         case ItemTypeRole:
68             return ItemTypeShowMore;
69         case Qt::DisplayRole:
70             if (!errorMessage.isEmpty()) return errorMessage;
71             if (searching) return QString(); // tr("Searching...");
72             if (canSearchMore) return tr("Show %1 More").arg("").simplified();
73             if (videos.isEmpty())
74                 return tr("No videos");
75             else
76                 return tr("No more videos");
77         case Qt::TextAlignmentRole:
78             return QVariant(int(Qt::AlignHCenter | Qt::AlignVCenter));
79         case Qt::ForegroundRole:
80             if (!errorMessage.isEmpty())
81                 return palette.color(QPalette::ToolTipText);
82             else
83                 return palette.color(QPalette::Dark);
84         case Qt::BackgroundColorRole:
85             if (!errorMessage.isEmpty())
86                 return palette.color(QPalette::ToolTipBase);
87             else
88                 return QVariant();
89         default:
90             return QVariant();
91         }
92
93     } else if (row < 0 || row >= videos.size())
94         return QVariant();
95
96     Video *video = videos.at(row);
97
98     switch (role) {
99     case ItemTypeRole:
100         return ItemTypeVideo;
101     case VideoRole:
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;
113         /*
114     case Qt::StatusTipRole:
115         return video->description();
116         */
117     }
118
119     return QVariant();
120 }
121
122 void PlaylistModel::setActiveRow(int row, bool notify) {
123     if (rowExists(row)) {
124         m_activeRow = row;
125         Video *previousVideo = m_activeVideo;
126         m_activeVideo = videoAt(row);
127
128         int oldactiverow = m_activeRow;
129
130         if (rowExists(oldactiverow))
131             emit dataChanged(createIndex(oldactiverow, 0),
132                              createIndex(oldactiverow, columnCount() - 1));
133
134         emit dataChanged(createIndex(m_activeRow, 0), createIndex(m_activeRow, columnCount() - 1));
135         if (notify) emit activeVideoChanged(m_activeVideo, previousVideo);
136
137     } else {
138         m_activeRow = -1;
139         m_activeVideo = nullptr;
140     }
141 }
142
143 int PlaylistModel::nextRow() const {
144     int nextRow = m_activeRow + 1;
145     if (rowExists(nextRow)) return nextRow;
146     return -1;
147 }
148
149 int PlaylistModel::previousRow() const {
150     int prevRow = m_activeRow - 1;
151     if (rowExists(prevRow)) return prevRow;
152     return -1;
153 }
154
155 Video *PlaylistModel::videoAt(int row) const {
156     if (rowExists(row)) return videos.at(row);
157     return nullptr;
158 }
159
160 Video *PlaylistModel::activeVideo() const {
161     return m_activeVideo;
162 }
163
164 void PlaylistModel::setVideoSource(VideoSource *videoSource) {
165     beginResetModel();
166
167     qDeleteAll(videos);
168     videos.clear();
169
170     qDeleteAll(deletedVideos);
171     deletedVideos.clear();
172
173     m_activeVideo = nullptr;
174     m_activeRow = -1;
175     startIndex = 1;
176     endResetModel();
177
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;
187                 }
188             },
189             Qt::UniqueConnection);
190
191     canSearchMore = true;
192     searchMore();
193 }
194
195 void PlaylistModel::searchMore() {
196     if (!canSearchMore || videoSource == nullptr || searching) return;
197     searching = true;
198     firstSearch = startIndex == 1;
199     max = videoSource->maxResults();
200     if (max == 0) max = 20;
201     errorMessage.clear();
202     videoSource->loadVideos(max, startIndex);
203     startIndex += max;
204 }
205
206 void PlaylistModel::searchNeeded() {
207     const int desiredRowsAhead = 10;
208     int remainingRows = videos.size() - m_activeRow;
209     if (remainingRows < desiredRowsAhead) searchMore();
210 }
211
212 void PlaylistModel::abortSearch() {
213     QMutexLocker locker(&mutex);
214     beginResetModel();
215     if (videoSource) videoSource->abort();
216     qDeleteAll(videos);
217     videos.clear();
218     videos.squeeze();
219     searching = false;
220     m_activeRow = -1;
221     m_activeVideo = nullptr;
222     startIndex = 1;
223     endResetModel();
224 }
225
226 void PlaylistModel::searchFinished(int total) {
227     Q_UNUSED(total);
228     searching = false;
229     canSearchMore = videoSource->hasMoreVideos();
230
231     // update the message item
232     emit dataChanged(createIndex(videos.size(), 0), createIndex(videos.size(), columnCount() - 1));
233
234     if (firstSearch && !videos.isEmpty()) handleFirstVideo(videos.at(0));
235 }
236
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));
241 }
242
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);
248     endInsertRows();
249     for (Video *video : newVideos) {
250         connect(video, SIGNAL(gotThumbnail()), SLOT(updateVideoSender()), Qt::UniqueConnection);
251         video->loadThumbnail();
252     }
253 }
254
255 void PlaylistModel::handleFirstVideo(Video *video) {
256     QSettings settings;
257     int currentVideoRow = rowForCloneVideo(MediaView::instance()->getCurrentVideoId());
258     if (currentVideoRow != -1)
259         setActiveRow(currentVideoRow, false);
260     else {
261         if (!settings.value("manualplay", false).toBool()) setActiveRow(0);
262     }
263
264     auto clazz = videoSource->metaObject()->className();
265     if (clazz == QLatin1String("YTSearch") || clazz == QLatin1String("IVSearch") ||
266         clazz == QLatin1String("IVChannelSource")) {
267         static const int maxRecentElements = 10;
268
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         }
280
281         // save keyword
282         QString query = searchParams->keywords();
283         if (!query.isEmpty() && !searchParams->isTransient()) {
284             if (query.startsWith("http://")) {
285                 // Save the video title
286                 query += "|" + videos.at(0)->getTitle();
287             }
288             QStringList keywords = settings.value(recentKeywordsKey).toStringList();
289             keywords.removeAll(query);
290             keywords.prepend(query);
291             while (keywords.size() > maxRecentElements)
292                 keywords.removeLast();
293             settings.setValue(recentKeywordsKey, keywords);
294         }
295
296         // save channel
297         QString channelId = searchParams->channelId();
298         if (!channelId.isEmpty() && !searchParams->isTransient()) {
299             QString value;
300             if (!video->getChannelId().isEmpty() &&
301                 video->getChannelId() != video->getChannelTitle())
302                 value = video->getChannelId() + "|" + video->getChannelTitle();
303             else
304                 value = video->getChannelTitle();
305             QStringList channels = settings.value(recentChannelsKey).toStringList();
306             channels.removeAll(value);
307             channels.removeAll(channelId);
308             channels.prepend(value);
309             while (channels.size() > maxRecentElements)
310                 channels.removeLast();
311             settings.setValue(recentChannelsKey, channels);
312         }
313     }
314 }
315
316 void PlaylistModel::updateVideoSender() {
317     Video *video = static_cast<Video *>(sender());
318     if (!video) {
319         qDebug() << "Cannot get sender";
320         return;
321     }
322     int row = rowForVideo(video);
323     emit dataChanged(createIndex(row, 0), createIndex(row, columnCount() - 1));
324 }
325
326 void PlaylistModel::emitDataChanged() {
327     QModelIndex index = createIndex(rowCount() - 1, 0);
328     emit dataChanged(index, index);
329 }
330
331 // --- item removal
332
333 bool PlaylistModel::removeRows(int position, int rows, const QModelIndex & /*parent*/) {
334     beginRemoveRows(QModelIndex(), position, position + rows - 1);
335     for (int row = 0; row < rows; ++row) {
336         Video *video = videos.takeAt(position);
337     }
338     endRemoveRows();
339     return true;
340 }
341
342 void PlaylistModel::removeIndexes(QModelIndexList &indexes) {
343     QVector<Video *> originalList(videos);
344     for (const QModelIndex &index : indexes) {
345         if (index.row() >= originalList.size()) continue;
346         Video *video = originalList.at(index.row());
347         int idx = videos.indexOf(video);
348         if (idx != -1) {
349             beginRemoveRows(QModelIndex(), idx, idx);
350             deletedVideos.append(video);
351             if (m_activeVideo == video) {
352                 m_activeVideo = nullptr;
353                 m_activeRow = -1;
354             }
355             videos.removeAll(video);
356             endRemoveRows();
357         }
358     }
359     videos.squeeze();
360 }
361
362 // --- Sturm und drang ---
363
364 Qt::DropActions PlaylistModel::supportedDropActions() const {
365     return Qt::CopyAction;
366 }
367
368 Qt::DropActions PlaylistModel::supportedDragActions() const {
369     return Qt::CopyAction;
370 }
371
372 Qt::ItemFlags PlaylistModel::flags(const QModelIndex &index) const {
373     if (index.isValid()) {
374         if (index.row() == videos.size()) {
375             // don't drag the "show more" item
376             return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
377         } else
378             return (Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled);
379     }
380     return Qt::ItemIsDropEnabled;
381 }
382
383 QStringList PlaylistModel::mimeTypes() const {
384     QStringList types;
385     types << "application/x-minitube-video";
386     return types;
387 }
388
389 QMimeData *PlaylistModel::mimeData(const QModelIndexList &indexes) const {
390     VideoMimeData *mime = new VideoMimeData();
391
392     for (const QModelIndex &it : indexes) {
393         int row = it.row();
394         if (row >= 0 && row < videos.size()) mime->addVideo(videos.at(it.row()));
395     }
396
397     return mime;
398 }
399
400 bool PlaylistModel::dropMimeData(const QMimeData *data,
401                                  Qt::DropAction action,
402                                  int row,
403                                  int column,
404                                  const QModelIndex &parent) {
405     if (action == Qt::IgnoreAction) return true;
406
407     if (!data->hasFormat("application/x-minitube-video")) return false;
408
409     if (column > 0) return false;
410
411     int beginRow;
412     if (row != -1)
413         beginRow = row;
414     else if (parent.isValid())
415         beginRow = parent.row();
416     else
417         beginRow = rowCount(QModelIndex());
418
419     const VideoMimeData *videoMimeData = qobject_cast<const VideoMimeData *>(data);
420     if (!videoMimeData) return false;
421
422     const QVector<Video *> &droppedVideos = videoMimeData->getVideos();
423     for (Video *video : droppedVideos) {
424         // remove videos
425         int videoRow = videos.indexOf(video);
426         removeRows(videoRow, 1, QModelIndex());
427
428         // and then add them again at the new position
429         beginInsertRows(QModelIndex(), beginRow, beginRow);
430         videos.insert(beginRow, video);
431         endInsertRows();
432     }
433
434     // fix m_activeRow after all this
435     m_activeRow = videos.indexOf(m_activeVideo);
436
437     // let the MediaView restore the selection
438     emit needSelectionFor(droppedVideos);
439
440     return true;
441 }
442
443 int PlaylistModel::rowForCloneVideo(const QString &videoId) const {
444     if (videoId.isEmpty()) return -1;
445     for (int i = 0; i < videos.size(); ++i) {
446         Video *v = videos.at(i);
447         // qDebug() << "Comparing" << v->id() << videoId;
448         if (v->getId() == videoId) return i;
449     }
450     return -1;
451 }
452
453 int PlaylistModel::rowForVideo(Video *video) {
454     return videos.indexOf(video);
455 }
456
457 QModelIndex PlaylistModel::indexForVideo(Video *video) {
458     return createIndex(videos.indexOf(video), 0);
459 }
460
461 void PlaylistModel::move(QModelIndexList &indexes, bool up) {
462     QVector<Video *> movedVideos;
463
464     for (const QModelIndex &index : indexes) {
465         int row = index.row();
466         if (row >= videos.size()) continue;
467         // qDebug() << "index row" << row;
468         Video *video = videoAt(row);
469         movedVideos << video;
470     }
471
472     int end = up ? -1 : rowCount() - 1, mod = up ? -1 : 1;
473     for (Video *video : movedVideos) {
474         int row = rowForVideo(video);
475         if (row + mod == end) {
476             end = row;
477             continue;
478         }
479         // qDebug() << "video row" << row;
480         removeRows(row, 1, QModelIndex());
481
482         if (up)
483             row--;
484         else
485             row++;
486
487         beginInsertRows(QModelIndex(), row, row);
488         videos.insert(row, video);
489         endInsertRows();
490     }
491
492     emit needSelectionFor(movedVideos);
493 }
494
495 /* row hovering */
496
497 void PlaylistModel::setHoveredRow(int row) {
498     int oldRow = hoveredRow;
499     hoveredRow = row;
500     emit dataChanged(createIndex(oldRow, 0), createIndex(oldRow, columnCount() - 1));
501     emit dataChanged(createIndex(hoveredRow, 0), createIndex(hoveredRow, columnCount() - 1));
502 }
503
504 void PlaylistModel::clearHover() {
505     int oldRow = hoveredRow;
506     hoveredRow = -1;
507     emit dataChanged(createIndex(oldRow, 0), createIndex(oldRow, columnCount() - 1));
508 }
509
510 void PlaylistModel::updateHoveredRow() {
511     emit dataChanged(createIndex(hoveredRow, 0), createIndex(hoveredRow, columnCount() - 1));
512 }
513
514 /* clickable author */
515
516 void PlaylistModel::enterAuthorHover() {
517     if (authorHovered) return;
518     authorHovered = true;
519     updateHoveredRow();
520 }
521
522 void PlaylistModel::exitAuthorHover() {
523     if (!authorHovered) return;
524     authorHovered = false;
525     updateHoveredRow();
526     setHoveredRow(hoveredRow);
527 }
528
529 void PlaylistModel::enterAuthorPressed() {
530     if (authorPressed) return;
531     authorPressed = true;
532     updateHoveredRow();
533 }
534
535 void PlaylistModel::exitAuthorPressed() {
536     if (!authorPressed) return;
537     authorPressed = false;
538     updateHoveredRow();
539 }