1 #include "playlistmanager.h"
6 QStringList allowedExtensions;
9 PlaylistManager::PlaylistManager(QWidget* parent)
10 : parentWidget (parent), lastMetaRead (-1)
12 allowedExtensions << "mp3" << "ogg" << "wav" << "wmv" << "wma";
13 // qDebug () << Phonon::BackendCapabilities::availableMimeTypes();
14 metaInformationResolver = new Phonon::MediaObject(parent);
15 connect(metaInformationResolver, SIGNAL(stateChanged(Phonon::State,Phonon::State)),
16 this, SLOT(metaStateChanged(Phonon::State,Phonon::State)));
19 int PlaylistManager::indexOf(const Phonon::MediaSource &s) const
21 for (int i = 0; i < items.size(); ++i)
23 if (items[i].source == s)
29 void PlaylistManager::parseAndAddFolder(const QString &dir, bool recursive)
32 // filters << "*.mp3";
34 QStringList files = QDir (dir).entryList(filters);
39 qDebug () << "Parsing folder " << dir;
41 //settings.setValue("LastFolder", dir);
42 int index = items.size();
43 foreach (QString string, files)
45 if (string == "." || string == "..")
47 QString fname = dir + "/" + string;
52 parseAndAddFolder(fname, true);
55 if (fileSupported(fname))
57 qDebug () << "Adding: " << fname;
58 items.append(PlaylistItem (PlaylistItem (fname)));
63 metaInformationResolver->setCurrentSource(items.at(index).source);
66 qDebug () << " SIZE: " << items.size ();
67 emit playlistChanged (index);
70 void PlaylistManager::addStringList(const QStringList& list)
72 int index = items.size();
73 foreach (QString string, list)
75 if (fileSupported(string) || string.toLower().startsWith("http"))
77 qDebug () << "Adding " << string;
78 items.append(PlaylistItem (string));
83 metaInformationResolver->setCurrentSource(items.at(index).source);
86 emit playlistChanged(index);
89 void PlaylistManager::metaStateChanged(Phonon::State newState, Phonon::State oldState)
91 qDebug () << "Meta state now " << newState << " old state " << oldState;
92 // This is an ugly hack, since the metaInformationResolver doesn't properly load the assigned source when it's in the error state
93 // 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
94 static bool wasError = false;
95 if (wasError && newState == Phonon::StoppedState)
97 metaInformationResolver->setCurrentSource(items[lastMetaRead].source);
101 if (newState == Phonon::ErrorState)
106 if (newState != Phonon::StoppedState && newState != Phonon::ErrorState)
111 int index = lastMetaRead;
112 qDebug () << "Reading meta info of " << metaInformationResolver->currentSource().fileName() << " => " << index;
114 QMap<QString, QString> metaData = metaInformationResolver->metaData();
117 if (index >= 0 && newState != Phonon::ErrorState)
119 items[index].artist = metaData.value("ARTIST");
120 items[index].title = metaData.value("TITLE");
121 items[index].album = metaData.value("ALBUM");
122 qDebug () << "Info is: " << items[index].artist << " - " << items[index].title;
123 if (metaData.isEmpty())
124 qDebug () << "Detected to be empty: " << items[index].uri;
126 items[index].playable = true;
127 emit itemUpdated (index);
129 if (index >= 0 && items.size () > index + 1)
131 metaInformationResolver->setCurrentSource(items[index + 1].source);
132 lastMetaRead = index + 1;
136 void PlaylistManager::savePlaylist(const QString& filenam)
138 QString filename = filenam;
139 if (filename.isEmpty())
141 bool writepls = false;
142 if (filename.length() < 4 || (filename.right(4).toLower() != ".m3u" && filename.right(4).toLower() != ".pls"))
147 else if (filename.right(4).toLower() == ".pls")
152 f.open(QFile::WriteOnly);
155 f.write ("[playlist]\n");
156 f.write (QString ("NumberOfEntries=%1\n").arg (items.size ()).toAscii());
158 for (int i = 0; i < items.size(); ++i)
162 f.write (QString ("File%1=%2\n").arg (i + 1).arg (items[i].uri).toAscii());
163 f.write (QString ("Title%1=%2 - %3 - %4\n").arg (i + 1).arg (items[i].artist).arg (items[i].title).arg (items[i].album).toAscii());
167 f.write (items[i].uri.toAscii());
172 f.write ("Version=2\n");
177 // QMessageBox::critical(this, "Write error", "Could not write playlist file", QMessageBox::Ok);
181 void PlaylistManager::loadPlaylist(const QString& filename)
184 if (filename.right(4).toLower() == ".m3u")
185 appendPlaylist(filename);
186 else if (filename.right(4).toLower() == ".pls")
187 appendPlaylistPLS(filename);
188 if (!items.isEmpty())
190 metaInformationResolver->setCurrentSource(items.at(0).source);
193 emit playlistChanged (0);
196 void PlaylistManager::addPlaylist(const QString& filename)
198 int index = items.size();
199 if (filename.right(4).toLower() == ".m3u")
200 appendPlaylist(filename);
201 else if (filename.right(4).toLower() == ".pls")
202 appendPlaylistPLS(filename);
203 if (!items.isEmpty())
205 metaInformationResolver->setCurrentSource(items.at(index).source);
206 lastMetaRead = index;
207 emit playlistChanged (index);
211 void PlaylistManager::appendPlaylist(const QString& filename)
213 qDebug () << "Attempting to load playlist: " << filename;
215 if (!f.open (QFile::ReadOnly))
217 QString tmp = f.readAll();
219 QStringList lines = tmp.split("\n");
220 foreach (QString l, lines)
222 if (l.isEmpty() || (!QFileInfo (l).exists() && (l.indexOf("http") != 0)))
226 qDebug () << "Load " << l;
227 items.append(PlaylistItem (l));
231 void PlaylistManager::appendPlaylistPLS(const QString& filename)
233 qDebug () << "Attempting to load playlist: " << filename;
235 if (!f.open (QFile::ReadOnly))
237 QString tmp = f.readAll();
239 QStringList lines = tmp.split("\n");
240 QMap<int, int> filemap;
242 foreach (QString l, lines)
244 if (l.isEmpty() || l.trimmed().toLower() == "[playlist]" || l.trimmed().toLower() == "version=2")
248 qDebug () << "PLS " << l;
249 if (l.trimmed().toLower().left(4) == "file")
251 QStringList tokens = l.split('=');
252 if (tokens.size () < 2)
254 tokens[0] = tokens[0].mid (4);
255 filemap.insert(tokens[0].toInt (), items.size ());
257 items.append(PlaylistItem (tokens[1]));
259 else if (l.trimmed().toLower().left(5) == "title")
261 QStringList tokens = l.split('=');
262 if (tokens.size () < 2)
264 tokens[0] = tokens[0].mid (5);
265 int toupdate = filemap[tokens[0].toInt()];
266 qDebug () << "Need to update " << toupdate << " for " << l;
267 QStringList metatok = tokens[1].split (" - ");
268 qDebug () << metatok;
269 if (metatok.size() > 2 && toupdate >= 0 && toupdate < items.size ())
271 items[toupdate].artist = metatok[0];
272 items[toupdate].title = metatok[1];
273 metatok = metatok.mid (2);
274 items[toupdate].album = metatok.join (" - ");
278 items[toupdate].title = metatok.join (" - ");
285 void PlaylistManager::clearPlaylist()
288 emit playlistChanged(0);
289 /* while (musicTable->rowCount())
290 musicTable->removeRow(0);
291 mediaObject->clear();*/
294 QStringList PlaylistManager::playlistStrings() const
297 for (int i = 0; i < items.size (); ++i)
299 qDebug () << "Returning playlist " << ret << " SIZE: " << items.size ();
303 void PlaylistManager::removeItem(int i)
306 emit playlistChanged(i);
309 bool PlaylistManager::fileSupported (const QString& fname) const
311 QString ext = fname.right(3).toLower();
312 foreach (QString e, allowedExtensions)