]> git.sur5r.net Git - minitube/blobdiff - src/playlistmodel.cpp
New upstream version 3.1
[minitube] / src / playlistmodel.cpp
index e2fcfe81a137388c15c1c66a647bc031d64e6f8b..c4a7d72b9fdfd096491c56de9ffa870fc4c8902e 100644 (file)
@@ -26,16 +26,18 @@ $END_LICENSE */
 #include "videosource.h"
 #include "ytsearch.h"
 
-static const int maxItems = 50;
-static const QString recentKeywordsKey = "recentKeywords";
-static const QString recentChannelsKey = "recentChannels";
+namespace {
+const int maxItems = 50;
+const QString recentKeywordsKey = "recentKeywords";
+const QString recentChannelsKey = "recentChannels";
+} // namespace
 
 PlaylistModel::PlaylistModel(QWidget *parent) : QAbstractListModel(parent) {
-    videoSource = 0;
+    videoSource = nullptr;
     searching = false;
     canSearchMore = true;
     firstSearch = false;
-    m_activeVideo = 0;
+    m_activeVideo = nullptr;
     m_activeRow = -1;
     startIndex = 1;
     max = 0;
@@ -64,7 +66,7 @@ QVariant PlaylistModel::data(const QModelIndex &index, int role) const {
             return ItemTypeShowMore;
         case Qt::DisplayRole:
             if (!errorMessage.isEmpty()) return errorMessage;
-            if (searching) return tr("Searching...");
+            if (searching) return QString(); // tr("Searching...");
             if (canSearchMore) return tr("Show %1 More").arg("").simplified();
             if (videos.isEmpty())
                 return tr("No videos");
@@ -118,6 +120,7 @@ QVariant PlaylistModel::data(const QModelIndex &index, int role) const {
 void PlaylistModel::setActiveRow(int row, bool notify) {
     if (rowExists(row)) {
         m_activeRow = row;
+        Video *previousVideo = m_activeVideo;
         m_activeVideo = videoAt(row);
 
         int oldactiverow = m_activeRow;
@@ -127,11 +130,11 @@ void PlaylistModel::setActiveRow(int row, bool notify) {
                              createIndex(oldactiverow, columnCount() - 1));
 
         emit dataChanged(createIndex(m_activeRow, 0), createIndex(m_activeRow, columnCount() - 1));
-        if (notify) emit activeRowChanged(row);
+        if (notify) emit activeVideoChanged(m_activeVideo, previousVideo);
 
     } else {
         m_activeRow = -1;
-        m_activeVideo = 0;
+        m_activeVideo = nullptr;
     }
 }
 
@@ -149,7 +152,7 @@ int PlaylistModel::previousRow() const {
 
 Video *PlaylistModel::videoAt(int row) const {
     if (rowExists(row)) return videos.at(row);
-    return 0;
+    return nullptr;
 }
 
 Video *PlaylistModel::activeVideo() const {
@@ -158,9 +161,14 @@ Video *PlaylistModel::activeVideo() const {
 
 void PlaylistModel::setVideoSource(VideoSource *videoSource) {
     beginResetModel();
-    while (!videos.isEmpty())
-        delete videos.takeFirst();
-    m_activeVideo = 0;
+
+    qDeleteAll(videos);
+    videos.clear();
+
+    qDeleteAll(deletedVideos);
+    deletedVideos.clear();
+
+    m_activeVideo = nullptr;
     m_activeRow = -1;
     startIndex = 1;
     endResetModel();
@@ -170,12 +178,19 @@ void PlaylistModel::setVideoSource(VideoSource *videoSource) {
             Qt::UniqueConnection);
     connect(videoSource, SIGNAL(finished(int)), SLOT(searchFinished(int)), Qt::UniqueConnection);
     connect(videoSource, SIGNAL(error(QString)), SLOT(searchError(QString)), Qt::UniqueConnection);
+    connect(videoSource, &QObject::destroyed, this,
+            [this, videoSource] {
+                if (this->videoSource == videoSource) {
+                    this->videoSource = nullptr;
+                }
+            },
+            Qt::UniqueConnection);
 
     searchMore();
 }
 
 void PlaylistModel::searchMore(int max) {
-    if (searching) return;
+    if (videoSource == nullptr || searching) return;
     searching = true;
     firstSearch = startIndex == 1;
     this->max = max;
@@ -203,7 +218,7 @@ void PlaylistModel::abortSearch() {
     videos.squeeze();
     searching = false;
     m_activeRow = -1;
-    m_activeVideo = 0;
+    m_activeVideo = nullptr;
     startIndex = 1;
     endResetModel();
 }
@@ -315,21 +330,21 @@ bool PlaylistModel::removeRows(int position, int rows, const QModelIndex & /*par
 
 void PlaylistModel::removeIndexes(QModelIndexList &indexes) {
     QVector<Video *> originalList(videos);
-    QVector<Video *> delitems;
-    delitems.reserve(indexes.size());
     for (const QModelIndex &index : indexes) {
         if (index.row() >= originalList.size()) continue;
         Video *video = originalList.at(index.row());
         int idx = videos.indexOf(video);
         if (idx != -1) {
             beginRemoveRows(QModelIndex(), idx, idx);
-            delitems.append(video);
+            deletedVideos.append(video);
+            if (m_activeVideo == video) {
+                m_activeVideo = nullptr;
+                m_activeRow = -1;
+            }
             videos.removeAll(video);
-            video->deleteLater();
             endRemoveRows();
         }
     }
-    qDeleteAll(delitems);
     videos.squeeze();
 }