bugfixes, confirm dialogs for remove actions
[tomamp] / playlistmanager.cpp
index a172cdd..4488583 100644 (file)
@@ -3,9 +3,14 @@
 #include <QUrl>
 #include <QMap>
 
+QStringList allowedExtensions;
+
+
 PlaylistManager::PlaylistManager(QWidget* parent)
-    : parentWidget (parent)
+    : parentWidget (parent), lastMetaRead (-1)
 {
+    allowedExtensions << "mp3" << "ogg" << "wav" << "wmv" << "wma";
+//    qDebug () << Phonon::BackendCapabilities::availableMimeTypes();
     metaInformationResolver = new Phonon::MediaObject(parent);
     connect(metaInformationResolver, SIGNAL(stateChanged(Phonon::State,Phonon::State)),
         this, SLOT(metaStateChanged(Phonon::State,Phonon::State)));
@@ -53,8 +58,12 @@ void PlaylistManager::parseAndAddFolder(const QString &dir, bool recursive)
             items.append(PlaylistItem (PlaylistItem (fname)));
         }
     }
-    if (!items.isEmpty())
+//    if (!items.isEmpty())
+    if (items.size () > index)
+    {
         metaInformationResolver->setCurrentSource(items.at(index).source);
+        lastMetaRead = index;
+    }
     qDebug () << " SIZE: " << items.size ();
     emit playlistChanged (index);
 }
@@ -70,51 +79,44 @@ void PlaylistManager::addStringList(const QStringList& list)
             items.append(PlaylistItem (string));
         }
     }
-    if (!items.isEmpty())
+//    if (!items.isEmpty())
+    if (items.size () > index)
+    {
         metaInformationResolver->setCurrentSource(items.at(index).source);
+        lastMetaRead = index;
+    }
     emit playlistChanged(index);
 }
 
 void PlaylistManager::metaStateChanged(Phonon::State newState, Phonon::State oldState)
 {
     qDebug () << "Meta state now " << newState << " old state " << oldState;
-    if (oldState == Phonon::ErrorState)
+    // This is an ugly hack, since the metaInformationResolver doesn't properly load the assigned source when it's in the error state
+    // In order to properly read the next file we have to set it as current source again when the resolver entered the stopped state after the error
+    static bool wasError = false;
+    if (wasError && newState == Phonon::StoppedState)
     {
-/*        int index = indexOf (metaInformationResolver->currentSource());
-        metaInformationResolver->setCurrentSource(items[index].source);*/
+        metaInformationResolver->setCurrentSource(items[lastMetaRead].source);
+        wasError = false;
+        return;
     }
     if (newState == Phonon::ErrorState)
     {
-        int index = indexOf (metaInformationResolver->currentSource());
-        if (index >= 0 && items.size () > index + 1)
-        {
-/*            metaInformationResolver->disconnect();
-            delete metaInformationResolver;
-            metaInformationResolver = new Phonon::MediaObject(parent());
-            connect(metaInformationResolver, SIGNAL(stateChanged(Phonon::State,Phonon::State)),
-                this, SLOT(metaStateChanged(Phonon::State,Phonon::State)));*/
-            metaInformationResolver->setCurrentSource(items[index + 1].source);
-//            metaInformationResolver->clear();
-            qDebug () << "Set " << items[index + 1].source.fileName() << " error type: " << metaInformationResolver->errorString() << " (" << metaInformationResolver->errorType() << ")";
-        }
-        qDebug () << "Error for item " << index;
-        return;
+        wasError = true;
     }
 
-    if (newState != Phonon::StoppedState/* && newState != Phonon::PausedState*/)
+    if (newState != Phonon::StoppedState && newState != Phonon::ErrorState)
     {
         return;
     }
 
-    if (metaInformationResolver->currentSource().type() == Phonon::MediaSource::Invalid)
-        return;
-    int index = indexOf (metaInformationResolver->currentSource());
+    int index = lastMetaRead;
     qDebug () << "Reading meta info of " << metaInformationResolver->currentSource().fileName() << " => " << index;
 
     QMap<QString, QString> metaData = metaInformationResolver->metaData();
 
 
-    if (index >= 0)
+    if (index >= 0 && newState != Phonon::ErrorState)
     {
         items[index].artist = metaData.value("ARTIST");
         items[index].title = metaData.value("TITLE");
@@ -125,8 +127,11 @@ void PlaylistManager::metaStateChanged(Phonon::State newState, Phonon::State old
         else
             items[index].playable = true;
         emit itemUpdated (index);
-        if (index >= 0 && items.size () > index + 1)
-            metaInformationResolver->setCurrentSource(items[index + 1].source);
+    }
+    if (index >= 0 && items.size () > index + 1)
+    {
+        metaInformationResolver->setCurrentSource(items[index + 1].source);
+        lastMetaRead = index + 1;
     }
 }
 
@@ -185,6 +190,7 @@ void PlaylistManager::loadPlaylist(const QString& filename)
     if (!items.isEmpty())
     {
         metaInformationResolver->setCurrentSource(items.at(0).source);
+        lastMetaRead = 0;
     }
     emit playlistChanged (0);
 }
@@ -196,9 +202,11 @@ void PlaylistManager::addPlaylist(const QString& filename)
         appendPlaylist(filename);
     else if (filename.right(4).toLower() == ".pls")
         appendPlaylistPLS(filename);
-    if (!items.isEmpty())
+    if (items.size () > index)
+//    if (!items.isEmpty())
     {
         metaInformationResolver->setCurrentSource(items.at(index).source);
+        lastMetaRead = index;
         emit playlistChanged (index);
     }
 }
@@ -304,7 +312,11 @@ void PlaylistManager::removeItem(int i)
 bool PlaylistManager::fileSupported (const QString& fname) const
 {
     QString ext = fname.right(3).toLower();
-//    if (ext != "mp3")
-//        return false;
-    return true;
+    foreach (QString e, allowedExtensions)
+    {
+        if (ext == e)
+            return true;
+    }
+
+    return false;
 }