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)
121 items[index].artist = metaData.value("ARTIST");
122 items[index].title = metaData.value("TITLE");
123 items[index].album = metaData.value("ALBUM");
124 qDebug () << "Info is: " << items[index].artist << " - " << items[index].title;
125 if (metaData.isEmpty())
126 qDebug () << "Detected to be empty: " << items[index].uri;
128 items[index].playable = true;
129 emit itemUpdated (index);
131 if (index >= 0 && items.size () > index + 1)
133 metaInformationResolver->setCurrentSource(items[index + 1].source);
134 lastMetaRead = index + 1;
138 void PlaylistManager::savePlaylist(const QString& filenam)
140 QString filename = filenam;
141 if (filename.isEmpty())
143 bool writepls = false;
144 if (filename.length() < 4 || (filename.right(4).toLower() != ".m3u" && filename.right(4).toLower() != ".pls"))
149 else if (filename.right(4).toLower() == ".pls")
154 f.open(QFile::WriteOnly);
157 f.write ("[playlist]\n");
158 f.write (QString ("NumberOfEntries=%1\n").arg (items.size ()).toAscii());
160 for (int i = 0; i < items.size(); ++i)
164 f.write (QString ("File%1=%2\n").arg (i + 1).arg (items[i].uri).toAscii());
165 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());
169 f.write (items[i].uri.toAscii());
174 f.write ("Version=2\n");
179 // QMessageBox::critical(this, "Write error", "Could not write playlist file", QMessageBox::Ok);
183 void PlaylistManager::loadPlaylist(const QString& filename)
186 if (filename.right(4).toLower() == ".m3u")
187 appendPlaylist(filename);
188 else if (filename.right(4).toLower() == ".pls")
189 appendPlaylistPLS(filename);
190 if (!items.isEmpty())
192 metaInformationResolver->setCurrentSource(items.at(0).source);
195 emit playlistChanged (0);
198 void PlaylistManager::addPlaylist(const QString& filename)
200 int index = items.size();
201 if (filename.right(4).toLower() == ".m3u")
202 appendPlaylist(filename);
203 else if (filename.right(4).toLower() == ".pls")
204 appendPlaylistPLS(filename);
205 if (items.size () > index)
206 // if (!items.isEmpty())
208 metaInformationResolver->setCurrentSource(items.at(index).source);
209 lastMetaRead = index;
210 emit playlistChanged (index);
214 void PlaylistManager::appendPlaylist(const QString& filename)
216 qDebug () << "Attempting to load playlist: " << filename;
218 if (!f.open (QFile::ReadOnly))
220 QString tmp = f.readAll();
222 QStringList lines = tmp.split("\n");
223 foreach (QString l, lines)
225 if (l.isEmpty() || (!QFileInfo (l).exists() && (l.indexOf("http") != 0)))
229 qDebug () << "Load " << l;
230 items.append(PlaylistItem (l));
234 void PlaylistManager::appendPlaylistPLS(const QString& filename)
236 qDebug () << "Attempting to load playlist: " << filename;
238 if (!f.open (QFile::ReadOnly))
240 QString tmp = f.readAll();
242 QStringList lines = tmp.split("\n");
243 QMap<int, int> filemap;
245 foreach (QString l, lines)
247 if (l.isEmpty() || l.trimmed().toLower() == "[playlist]" || l.trimmed().toLower() == "version=2")
251 qDebug () << "PLS " << l;
252 if (l.trimmed().toLower().left(4) == "file")
254 QStringList tokens = l.split('=');
255 if (tokens.size () < 2)
257 tokens[0] = tokens[0].mid (4);
258 filemap.insert(tokens[0].toInt (), items.size ());
260 items.append(PlaylistItem (tokens[1]));
262 else if (l.trimmed().toLower().left(5) == "title")
264 QStringList tokens = l.split('=');
265 if (tokens.size () < 2)
267 tokens[0] = tokens[0].mid (5);
268 int toupdate = filemap[tokens[0].toInt()];
269 qDebug () << "Need to update " << toupdate << " for " << l;
270 QStringList metatok = tokens[1].split (" - ");
271 qDebug () << metatok;
272 if (metatok.size() > 2 && toupdate >= 0 && toupdate < items.size ())
274 items[toupdate].artist = metatok[0];
275 items[toupdate].title = metatok[1];
276 metatok = metatok.mid (2);
277 items[toupdate].album = metatok.join (" - ");
281 items[toupdate].title = metatok.join (" - ");
288 void PlaylistManager::clearPlaylist()
291 emit playlistChanged(0);
292 /* while (musicTable->rowCount())
293 musicTable->removeRow(0);
294 mediaObject->clear();*/
297 QStringList PlaylistManager::playlistStrings() const
300 for (int i = 0; i < items.size (); ++i)
302 qDebug () << "Returning playlist " << ret << " SIZE: " << items.size ();
306 void PlaylistManager::removeItem(int i)
309 emit playlistChanged(i);
312 bool PlaylistManager::fileSupported (const QString& fname) const
314 QString ext = fname.right(3).toLower();
315 foreach (QString e, allowedExtensions)