cleanup & small fixes
[tomamp] / tomamp / playlistmanager.cpp
index 3c0cb95..5809d94 100644 (file)
@@ -9,8 +9,7 @@ QStringList allowedExtensions;
 PlaylistManager::PlaylistManager(QWidget* parent)
     : parentWidget (parent), lastMetaRead (-1)
 {
-    allowedExtensions << "mp3" << "ogg" << "wav" << "wmv" << "wma";
-//    qDebug () << Phonon::BackendCapabilities::availableMimeTypes();
+    allowedExtensions << "mp3" << "ogg" << "wav" << "wmv" << "wma" << "flac";
     metaInformationResolver = new Phonon::MediaObject(parent);
     connect(metaInformationResolver, SIGNAL(stateChanged(Phonon::State,Phonon::State)),
         this, SLOT(metaStateChanged(Phonon::State,Phonon::State)));
@@ -29,16 +28,13 @@ int PlaylistManager::indexOf(const Phonon::MediaSource &s) const
 void PlaylistManager::parseAndAddFolder(const QString &dir, bool recursive)
 {
     QStringList filters;
-//    filters << "*.mp3";
 
     QStringList files = QDir (dir).entryList(filters);
 
     if (files.isEmpty())
         return;
 
-    qDebug () << "Parsing folder " << dir;
 
-    //settings.setValue("LastFolder", dir);
     int index = items.size();
     foreach (QString string, files)
     {
@@ -58,13 +54,11 @@ void PlaylistManager::parseAndAddFolder(const QString &dir, bool recursive)
             items.append(PlaylistItem (PlaylistItem (fname)));
         }
     }
-//    if (!items.isEmpty())
     if (items.size () > index)
     {
         metaInformationResolver->setCurrentSource(items.at(index).source);
         lastMetaRead = index;
     }
-    qDebug () << " SIZE: " << items.size ();
     emit playlistChanged (index);
 }
 
@@ -75,11 +69,9 @@ void PlaylistManager::addStringList(const QStringList& list)
     {
         if (fileSupported(string) || string.toLower().startsWith("http"))
         {
-            qDebug () << "Adding " << string;
             items.append(PlaylistItem (string));
         }
     }
-//    if (!items.isEmpty())
     if (items.size () > index)
     {
         metaInformationResolver->setCurrentSource(items.at(index).source);
@@ -90,8 +82,7 @@ void PlaylistManager::addStringList(const QStringList& list)
 
 void PlaylistManager::metaStateChanged(Phonon::State newState, Phonon::State oldState)
 {
-    qDebug () << "Meta state now " << newState << " old state " << oldState;
-    // This is an ugly hack, since the metaInformationResolver doesn't properly load the assigned source when it's in the error state
+    // NOTE: 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)
@@ -111,12 +102,11 @@ void PlaylistManager::metaStateChanged(Phonon::State newState, Phonon::State old
     }
 
     int index = lastMetaRead;
-    qDebug () << "Reading meta info of " << metaInformationResolver->currentSource().fileName() << " => " << index;
 
     QMap<QString, QString> metaData = metaInformationResolver->metaData();
 
 
-    if (index >= 0 && newState != Phonon::ErrorState)
+    if (index >= 0 && newState != Phonon::ErrorState && index < items.size ())
     {
         items[index].artist = metaData.value("ARTIST");
         items[index].title = metaData.value("TITLE");
@@ -125,9 +115,7 @@ void PlaylistManager::metaStateChanged(Phonon::State newState, Phonon::State old
         items[index].genre = metaData.value("GENRE");
         qDebug () << "Meta " << metaData;
         qDebug () << "Info is: " << items[index].year << " - " << items[index].genre;*/
-        if (metaData.isEmpty())
-            qDebug () << "Detected to be empty: " << items[index].uri;
-        else
+        if (!metaData.isEmpty())
             items[index].playable = true;
         emit itemUpdated (index);
     }
@@ -138,11 +126,11 @@ void PlaylistManager::metaStateChanged(Phonon::State newState, Phonon::State old
     }
 }
 
-void PlaylistManager::savePlaylist(const QString& filenam)
+bool PlaylistManager::savePlaylist(const QString& filenam)
 {
     QString filename = filenam;
     if (filename.isEmpty())
-        return;
+        return false;
     bool writepls = false;
     if (filename.length() < 4 || (filename.right(4).toLower() != ".m3u" && filename.right(4).toLower() != ".pls"))
     {
@@ -176,11 +164,12 @@ void PlaylistManager::savePlaylist(const QString& filenam)
         if (writepls)
             f.write ("Version=2\n");
         f.close ();
+        return true;
     }
     catch (...)
     {
-//        QMessageBox::critical(this, "Write error", "Could not write playlist file", QMessageBox::Ok);
     }
+    return false;
 }
 
 void PlaylistManager::loadPlaylist(const QString& filename)
@@ -216,7 +205,6 @@ void PlaylistManager::addPlaylist(const QString& filename)
 
 void PlaylistManager::appendPlaylist(const QString& filename)
 {
-    qDebug () << "Attempting to load playlist: " << filename;
     QFile f(filename);
     if (!f.open (QFile::ReadOnly))
         return;
@@ -229,14 +217,12 @@ void PlaylistManager::appendPlaylist(const QString& filename)
         {
             continue;
         }
-        qDebug () << "Load " << l;
         items.append(PlaylistItem (l));
     }
 }
 
 void PlaylistManager::appendPlaylistPLS(const QString& filename)
 {
-    qDebug () << "Attempting to load playlist: " << filename;
     QFile f(filename);
     if (!f.open (QFile::ReadOnly))
         return;
@@ -251,7 +237,6 @@ void PlaylistManager::appendPlaylistPLS(const QString& filename)
         {
             continue;
         }
-        qDebug () << "PLS " << l;
         if (l.trimmed().toLower().left(4) == "file")
         {
             QStringList tokens = l.split('=');
@@ -259,7 +244,6 @@ void PlaylistManager::appendPlaylistPLS(const QString& filename)
                 continue;
             tokens[0] = tokens[0].mid (4);
             filemap.insert(tokens[0].toInt (), items.size ());
-            qDebug () << tokens;
             items.append(PlaylistItem (tokens[1]));
         }
         else if (l.trimmed().toLower().left(5) == "title")
@@ -269,9 +253,7 @@ void PlaylistManager::appendPlaylistPLS(const QString& filename)
                 continue;
             tokens[0] = tokens[0].mid (5);
             int toupdate = filemap[tokens[0].toInt()];
-            qDebug () << "Need to update " << toupdate << " for " << l;
             QStringList metatok = tokens[1].split (" - ");
-            qDebug () << metatok;
             if (metatok.size() > 2 && toupdate >= 0 && toupdate < items.size ())
             {
                 items[toupdate].artist = metatok[0];
@@ -292,9 +274,6 @@ void PlaylistManager::clearPlaylist()
 {
     items.clear();
     emit playlistChanged(0);
-/*    while (musicTable->rowCount())
-        musicTable->removeRow(0);
-    mediaObject->clear();*/
 }
 
 QStringList PlaylistManager::playlistStrings() const
@@ -302,7 +281,6 @@ QStringList PlaylistManager::playlistStrings() const
     QStringList ret;
     for (int i = 0; i < items.size (); ++i)
         ret << items[i].uri;
-    qDebug () << "Returning playlist " << ret << " SIZE: " << items.size ();
     return ret;
 }
 
@@ -315,7 +293,9 @@ void PlaylistManager::removeItem(int i)
 
 bool PlaylistManager::fileSupported (const QString& fname) const
 {
-    QString ext = fname.right(3).toLower();
+    if (fname.lastIndexOf('.') < 0)
+        return false;
+    QString ext = fname.right(fname.size() - fname.lastIndexOf('.') - 1).toLower();
     foreach (QString e, allowedExtensions)
     {
         if (ext == e)
@@ -333,7 +313,6 @@ bool PlaylistManager::moveItemUp (int i)
         items[i - 1] = items[i];
         items[i] = tmp;
         return true;
-//        emit playlistChanged(i - 1);
     }
     return false;
 }
@@ -346,7 +325,6 @@ bool PlaylistManager::moveItemDown (int i)
         items[i + 1] = items[i];
         items[i] = tmp;
         return true;
-//        emit playlistChanged(i - 1);
     }
     return false;
 }