1 #include "playlistmanager.h"
6 QStringList allowedExtensions;
9 PlaylistManager::PlaylistManager(QWidget* parent)
10 : parentWidget (parent), lastMetaRead (-1)
12 allowedExtensions << "mp3" << "ogg" << "wav" << "wmv" << "wma" << "flac";
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)
33 QStringList files = QDir (dir).entryList(filters);
38 qDebug () << "Parsing folder " << dir;
40 int index = items.size();
41 foreach (QString string, files)
43 if (string == "." || string == "..")
45 QString fname = dir + "/" + string;
50 parseAndAddFolder(fname, true);
53 if (fileSupported(fname))
55 qDebug () << "Adding: " << fname;
56 items.append(PlaylistItem (PlaylistItem (fname)));
59 if (items.size () > index)
61 metaInformationResolver->setCurrentSource(items.at(index).source);
64 qDebug () << " SIZE: " << items.size ();
65 emit playlistChanged (index);
68 void PlaylistManager::addStringList(const QStringList& list)
70 int index = items.size();
71 foreach (QString string, list)
73 if (fileSupported(string) || string.toLower().startsWith("http"))
75 qDebug () << "Adding " << string;
76 items.append(PlaylistItem (string));
79 if (items.size () > index)
81 metaInformationResolver->setCurrentSource(items.at(index).source);
84 emit playlistChanged(index);
87 void PlaylistManager::metaStateChanged(Phonon::State newState, Phonon::State oldState)
89 qDebug () << "Meta state now " << newState << " old state " << oldState;
90 // NOTE: This is an ugly hack, since the metaInformationResolver doesn't properly load the assigned source when it's in the error state
91 // 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
92 static bool wasError = false;
93 if (wasError && newState == Phonon::StoppedState)
95 metaInformationResolver->setCurrentSource(items[lastMetaRead].source);
99 if (newState == Phonon::ErrorState)
104 if (newState != Phonon::StoppedState && newState != Phonon::ErrorState)
109 int index = lastMetaRead;
111 QMap<QString, QString> metaData = metaInformationResolver->metaData();
114 if (index >= 0 && newState != Phonon::ErrorState && index < items.size ())
116 items[index].artist = metaData.value("ARTIST");
117 items[index].title = metaData.value("TITLE");
118 items[index].album = metaData.value("ALBUM");
119 /* items[index].year = metaData.value("DATE");
120 items[index].genre = metaData.value("GENRE");
121 qDebug () << "Meta " << metaData;
122 qDebug () << "Info is: " << items[index].year << " - " << items[index].genre;*/
123 if (!metaData.isEmpty())
124 items[index].playable = true;
125 emit itemUpdated (index);
127 if (index >= 0 && items.size () > index + 1)
129 metaInformationResolver->setCurrentSource(items[index + 1].source);
130 lastMetaRead = index + 1;
134 void PlaylistManager::savePlaylist(const QString& filenam)
136 QString filename = filenam;
137 if (filename.isEmpty())
139 bool writepls = false;
140 if (filename.length() < 4 || (filename.right(4).toLower() != ".m3u" && filename.right(4).toLower() != ".pls"))
145 else if (filename.right(4).toLower() == ".pls")
150 f.open(QFile::WriteOnly);
153 f.write ("[playlist]\n");
154 f.write (QString ("NumberOfEntries=%1\n").arg (items.size ()).toAscii());
156 for (int i = 0; i < items.size(); ++i)
160 f.write (QString ("File%1=%2\n").arg (i + 1).arg (items[i].uri).toAscii());
161 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());
165 f.write (items[i].uri.toAscii());
170 f.write ("Version=2\n");
175 // QMessageBox::critical(this, "Write error", "Could not write playlist file", QMessageBox::Ok);
179 void PlaylistManager::loadPlaylist(const QString& filename)
182 if (filename.right(4).toLower() == ".m3u")
183 appendPlaylist(filename);
184 else if (filename.right(4).toLower() == ".pls")
185 appendPlaylistPLS(filename);
186 if (!items.isEmpty())
188 metaInformationResolver->setCurrentSource(items.at(0).source);
191 emit playlistChanged (0);
194 void PlaylistManager::addPlaylist(const QString& filename)
196 int index = items.size();
197 if (filename.right(4).toLower() == ".m3u")
198 appendPlaylist(filename);
199 else if (filename.right(4).toLower() == ".pls")
200 appendPlaylistPLS(filename);
201 if (items.size () > index)
202 // if (!items.isEmpty())
204 metaInformationResolver->setCurrentSource(items.at(index).source);
205 lastMetaRead = index;
206 emit playlistChanged (index);
210 void PlaylistManager::appendPlaylist(const QString& filename)
212 qDebug () << "Attempting to load playlist: " << filename;
214 if (!f.open (QFile::ReadOnly))
216 QString tmp = f.readAll();
218 QStringList lines = tmp.split("\n");
219 foreach (QString l, lines)
221 if (l.isEmpty() || (!QFileInfo (l).exists() && (l.indexOf("http") != 0)))
225 qDebug () << "Load " << l;
226 items.append(PlaylistItem (l));
230 void PlaylistManager::appendPlaylistPLS(const QString& filename)
232 qDebug () << "Attempting to load playlist: " << filename;
234 if (!f.open (QFile::ReadOnly))
236 QString tmp = f.readAll();
238 QStringList lines = tmp.split("\n");
239 QMap<int, int> filemap;
241 foreach (QString l, lines)
243 if (l.isEmpty() || l.trimmed().toLower() == "[playlist]" || l.trimmed().toLower() == "version=2")
247 qDebug () << "PLS " << l;
248 if (l.trimmed().toLower().left(4) == "file")
250 QStringList tokens = l.split('=');
251 if (tokens.size () < 2)
253 tokens[0] = tokens[0].mid (4);
254 filemap.insert(tokens[0].toInt (), items.size ());
256 items.append(PlaylistItem (tokens[1]));
258 else if (l.trimmed().toLower().left(5) == "title")
260 QStringList tokens = l.split('=');
261 if (tokens.size () < 2)
263 tokens[0] = tokens[0].mid (5);
264 int toupdate = filemap[tokens[0].toInt()];
265 qDebug () << "Need to update " << toupdate << " for " << l;
266 QStringList metatok = tokens[1].split (" - ");
267 qDebug () << metatok;
268 if (metatok.size() > 2 && toupdate >= 0 && toupdate < items.size ())
270 items[toupdate].artist = metatok[0];
271 items[toupdate].title = metatok[1];
272 metatok = metatok.mid (2);
273 items[toupdate].album = metatok.join (" - ");
277 items[toupdate].title = metatok.join (" - ");
284 void PlaylistManager::clearPlaylist()
287 emit playlistChanged(0);
290 QStringList PlaylistManager::playlistStrings() const
293 for (int i = 0; i < items.size (); ++i)
295 qDebug () << "Returning playlist " << ret << " SIZE: " << items.size ();
299 void PlaylistManager::removeItem(int i)
306 bool PlaylistManager::fileSupported (const QString& fname) const
308 if (fname.lastIndexOf('.') < 0)
310 QString ext = fname.right(fname.size() - fname.lastIndexOf('.') - 1).toLower();
311 foreach (QString e, allowedExtensions)
320 bool PlaylistManager::moveItemUp (int i)
324 PlaylistItem tmp = items[i - 1];
325 items[i - 1] = items[i];
332 bool PlaylistManager::moveItemDown (int i)
334 if (i < items.size () - 1)
336 PlaylistItem tmp = items[i + 1];
337 items[i + 1] = items[i];