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)
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 void 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");
182 // QMessageBox::critical(this, "Write error", "Could not write playlist file", QMessageBox::Ok);
186 void PlaylistManager::loadPlaylist(const QString& filename)
189 if (filename.right(4).toLower() == ".m3u")
190 appendPlaylist(filename);
191 else if (filename.right(4).toLower() == ".pls")
192 appendPlaylistPLS(filename);
193 if (!items.isEmpty())
195 metaInformationResolver->setCurrentSource(items.at(0).source);
198 emit playlistChanged (0);
201 void PlaylistManager::addPlaylist(const QString& filename)
203 int index = items.size();
204 if (filename.right(4).toLower() == ".m3u")
205 appendPlaylist(filename);
206 else if (filename.right(4).toLower() == ".pls")
207 appendPlaylistPLS(filename);
208 if (items.size () > index)
209 // if (!items.isEmpty())
211 metaInformationResolver->setCurrentSource(items.at(index).source);
212 lastMetaRead = index;
213 emit playlistChanged (index);
217 void PlaylistManager::appendPlaylist(const QString& filename)
219 qDebug () << "Attempting to load playlist: " << filename;
221 if (!f.open (QFile::ReadOnly))
223 QString tmp = f.readAll();
225 QStringList lines = tmp.split("\n");
226 foreach (QString l, lines)
228 if (l.isEmpty() || (!QFileInfo (l).exists() && (l.indexOf("http") != 0)))
232 qDebug () << "Load " << l;
233 items.append(PlaylistItem (l));
237 void PlaylistManager::appendPlaylistPLS(const QString& filename)
239 qDebug () << "Attempting to load playlist: " << filename;
241 if (!f.open (QFile::ReadOnly))
243 QString tmp = f.readAll();
245 QStringList lines = tmp.split("\n");
246 QMap<int, int> filemap;
248 foreach (QString l, lines)
250 if (l.isEmpty() || l.trimmed().toLower() == "[playlist]" || l.trimmed().toLower() == "version=2")
254 qDebug () << "PLS " << l;
255 if (l.trimmed().toLower().left(4) == "file")
257 QStringList tokens = l.split('=');
258 if (tokens.size () < 2)
260 tokens[0] = tokens[0].mid (4);
261 filemap.insert(tokens[0].toInt (), items.size ());
263 items.append(PlaylistItem (tokens[1]));
265 else if (l.trimmed().toLower().left(5) == "title")
267 QStringList tokens = l.split('=');
268 if (tokens.size () < 2)
270 tokens[0] = tokens[0].mid (5);
271 int toupdate = filemap[tokens[0].toInt()];
272 qDebug () << "Need to update " << toupdate << " for " << l;
273 QStringList metatok = tokens[1].split (" - ");
274 qDebug () << metatok;
275 if (metatok.size() > 2 && toupdate >= 0 && toupdate < items.size ())
277 items[toupdate].artist = metatok[0];
278 items[toupdate].title = metatok[1];
279 metatok = metatok.mid (2);
280 items[toupdate].album = metatok.join (" - ");
284 items[toupdate].title = metatok.join (" - ");
291 void PlaylistManager::clearPlaylist()
294 emit playlistChanged(0);
295 /* while (musicTable->rowCount())
296 musicTable->removeRow(0);
297 mediaObject->clear();*/
300 QStringList PlaylistManager::playlistStrings() const
303 for (int i = 0; i < items.size (); ++i)
305 qDebug () << "Returning playlist " << ret << " SIZE: " << items.size ();
309 void PlaylistManager::removeItem(int i)
316 bool PlaylistManager::fileSupported (const QString& fname) const
318 if (fname.lastIndexOf('.') < 0)
320 QString ext = fname.right(fname.size() - fname.lastIndexOf('.') - 1).toLower();
321 foreach (QString e, allowedExtensions)
330 bool PlaylistManager::moveItemUp (int i)
334 PlaylistItem tmp = items[i - 1];
335 items[i - 1] = items[i];
338 // emit playlistChanged(i - 1);
343 bool PlaylistManager::moveItemDown (int i)
345 if (i < items.size () - 1)
347 PlaylistItem tmp = items[i + 1];
348 items[i + 1] = items[i];
351 // emit playlistChanged(i - 1);