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)));
61 // if (!items.isEmpty())
62 if (items.size () > index)
64 metaInformationResolver->setCurrentSource(items.at(index).source);
67 qDebug () << " SIZE: " << items.size ();
68 emit playlistChanged (index);
71 void PlaylistManager::addStringList(const QStringList& list)
73 int index = items.size();
74 foreach (QString string, list)
76 if (fileSupported(string) || string.toLower().startsWith("http"))
78 qDebug () << "Adding " << string;
79 items.append(PlaylistItem (string));
82 // if (!items.isEmpty())
83 if (items.size () > index)
85 metaInformationResolver->setCurrentSource(items.at(index).source);
88 emit playlistChanged(index);
91 void PlaylistManager::metaStateChanged(Phonon::State newState, Phonon::State oldState)
93 qDebug () << "Meta state now " << newState << " old state " << oldState;
94 // This is an ugly hack, since the metaInformationResolver doesn't properly load the assigned source when it's in the error state
95 // 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
96 static bool wasError = false;
97 if (wasError && newState == Phonon::StoppedState)
99 metaInformationResolver->setCurrentSource(items[lastMetaRead].source);
103 if (newState == Phonon::ErrorState)
108 if (newState != Phonon::StoppedState && newState != Phonon::ErrorState)
113 int index = lastMetaRead;
114 qDebug () << "Reading meta info of " << metaInformationResolver->currentSource().fileName() << " => " << index;
116 QMap<QString, QString> metaData = metaInformationResolver->metaData();
119 if (index >= 0 && newState != Phonon::ErrorState && index < items.size ())
121 items[index].artist = metaData.value("ARTIST");
122 items[index].title = metaData.value("TITLE");
123 items[index].album = metaData.value("ALBUM");
124 /* items[index].year = metaData.value("DATE");
125 items[index].genre = metaData.value("GENRE");
126 qDebug () << "Meta " << metaData;
127 qDebug () << "Info is: " << items[index].year << " - " << items[index].genre;*/
128 if (metaData.isEmpty())
129 qDebug () << "Detected to be empty: " << items[index].uri;
131 items[index].playable = true;
132 emit itemUpdated (index);
134 if (index >= 0 && items.size () > index + 1)
136 metaInformationResolver->setCurrentSource(items[index + 1].source);
137 lastMetaRead = index + 1;
141 bool PlaylistManager::savePlaylist(const QString& filenam)
143 QString filename = filenam;
144 if (filename.isEmpty())
146 bool writepls = false;
147 if (filename.length() < 4 || (filename.right(4).toLower() != ".m3u" && filename.right(4).toLower() != ".pls"))
152 else if (filename.right(4).toLower() == ".pls")
157 f.open(QFile::WriteOnly);
160 f.write ("[playlist]\n");
161 f.write (QString ("NumberOfEntries=%1\n").arg (items.size ()).toAscii());
163 for (int i = 0; i < items.size(); ++i)
167 f.write (QString ("File%1=%2\n").arg (i + 1).arg (items[i].uri).toAscii());
168 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());
172 f.write (items[i].uri.toAscii());
177 f.write ("Version=2\n");
187 void PlaylistManager::loadPlaylist(const QString& filename)
190 if (filename.right(4).toLower() == ".m3u")
191 appendPlaylist(filename);
192 else if (filename.right(4).toLower() == ".pls")
193 appendPlaylistPLS(filename);
194 if (!items.isEmpty())
196 metaInformationResolver->setCurrentSource(items.at(0).source);
199 emit playlistChanged (0);
202 void PlaylistManager::addPlaylist(const QString& filename)
204 int index = items.size();
205 if (filename.right(4).toLower() == ".m3u")
206 appendPlaylist(filename);
207 else if (filename.right(4).toLower() == ".pls")
208 appendPlaylistPLS(filename);
209 if (items.size () > index)
210 // if (!items.isEmpty())
212 metaInformationResolver->setCurrentSource(items.at(index).source);
213 lastMetaRead = index;
214 emit playlistChanged (index);
218 void PlaylistManager::appendPlaylist(const QString& filename)
220 qDebug () << "Attempting to load playlist: " << filename;
222 if (!f.open (QFile::ReadOnly))
224 QString tmp = f.readAll();
226 QStringList lines = tmp.split("\n");
227 foreach (QString l, lines)
229 if (l.isEmpty() || (!QFileInfo (l).exists() && (l.indexOf("http") != 0)))
233 qDebug () << "Load " << l;
234 items.append(PlaylistItem (l));
238 void PlaylistManager::appendPlaylistPLS(const QString& filename)
240 qDebug () << "Attempting to load playlist: " << filename;
242 if (!f.open (QFile::ReadOnly))
244 QString tmp = f.readAll();
246 QStringList lines = tmp.split("\n");
247 QMap<int, int> filemap;
249 foreach (QString l, lines)
251 if (l.isEmpty() || l.trimmed().toLower() == "[playlist]" || l.trimmed().toLower() == "version=2")
255 qDebug () << "PLS " << l;
256 if (l.trimmed().toLower().left(4) == "file")
258 QStringList tokens = l.split('=');
259 if (tokens.size () < 2)
261 tokens[0] = tokens[0].mid (4);
262 filemap.insert(tokens[0].toInt (), items.size ());
264 items.append(PlaylistItem (tokens[1]));
266 else if (l.trimmed().toLower().left(5) == "title")
268 QStringList tokens = l.split('=');
269 if (tokens.size () < 2)
271 tokens[0] = tokens[0].mid (5);
272 int toupdate = filemap[tokens[0].toInt()];
273 qDebug () << "Need to update " << toupdate << " for " << l;
274 QStringList metatok = tokens[1].split (" - ");
275 qDebug () << metatok;
276 if (metatok.size() > 2 && toupdate >= 0 && toupdate < items.size ())
278 items[toupdate].artist = metatok[0];
279 items[toupdate].title = metatok[1];
280 metatok = metatok.mid (2);
281 items[toupdate].album = metatok.join (" - ");
285 items[toupdate].title = metatok.join (" - ");
292 void PlaylistManager::clearPlaylist()
295 emit playlistChanged(0);
296 /* while (musicTable->rowCount())
297 musicTable->removeRow(0);
298 mediaObject->clear();*/
301 QStringList PlaylistManager::playlistStrings() const
304 for (int i = 0; i < items.size (); ++i)
306 qDebug () << "Returning playlist " << ret << " SIZE: " << items.size ();
310 void PlaylistManager::removeItem(int i)
317 bool PlaylistManager::fileSupported (const QString& fname) const
319 QString ext = fname.right(3).toLower();
320 foreach (QString e, allowedExtensions)
329 bool PlaylistManager::moveItemUp (int i)
333 PlaylistItem tmp = items[i - 1];
334 items[i - 1] = items[i];
337 // emit playlistChanged(i - 1);
342 bool PlaylistManager::moveItemDown (int i)
344 if (i < items.size () - 1)
346 PlaylistItem tmp = items[i + 1];
347 items[i + 1] = items[i];
350 // emit playlistChanged(i - 1);