context menu for removing playlist items, lots of fixes, basic functions seem to...
[tomamp] / playlistmanager.cpp
1 #include "playlistmanager.h"
2 #include <QDir>
3 #include <QUrl>
4
5 PlaylistManager::PlaylistManager(QWidget* parent)
6     : parentWidget (parent)
7 {
8     metaInformationResolver = new Phonon::MediaObject(parent);
9     connect(metaInformationResolver, SIGNAL(stateChanged(Phonon::State,Phonon::State)),
10         this, SLOT(metaStateChanged(Phonon::State,Phonon::State)));
11 }
12
13 int PlaylistManager::indexOf(const Phonon::MediaSource &s) const
14 {
15     for (int i = 0; i < items.size(); ++i)
16     {
17         if (items[i].source == s)
18             return i;
19     }
20     return -1;
21 }
22
23 void PlaylistManager::parseAndAddFolder(const QString &dir, bool recursive)
24 {
25     QStringList filters;
26 //    filters << "*.mp3";
27
28     QStringList files = QDir (dir).entryList(filters);
29
30     if (files.isEmpty())
31         return;
32
33     qDebug () << "Parsing folder " << dir;
34
35     //settings.setValue("LastFolder", dir);
36     int index = items.size();
37     foreach (QString string, files)
38     {
39         if (string == "."  || string == "..")
40             continue;
41         QString fname = dir + "/" + string;
42         QFileInfo fi (fname);
43         if (fi.isDir())
44         {
45             if (recursive)
46                 parseAndAddFolder(fname, true);
47             continue;
48         }
49         qDebug () << "Adding: " << fname;
50         items.append(PlaylistItem (PlaylistItem (fname)));
51     }
52     if (!items.isEmpty())
53         metaInformationResolver->setCurrentSource(items.at(index).source);
54     qDebug () << " SIZE: " << items.size ();
55     emit playlistChanged (index);
56 }
57
58 void PlaylistManager::addStringList(const QStringList& list)
59 {
60     int index = items.size();
61     foreach (QString string, list)
62     {
63         qDebug () << "Adding " << string;
64         items.append(PlaylistItem (string));
65     }
66     if (!items.isEmpty())
67         metaInformationResolver->setCurrentSource(items.at(index).source);
68     emit playlistChanged(index);
69 }
70
71 void PlaylistManager::metaStateChanged(Phonon::State newState, Phonon::State /* oldState */)
72 {
73     if (newState == Phonon::ErrorState)
74     {
75 //        QMessageBox::warning(this, tr("Error opening files"),
76 //        metaInformationResolver->errorString());
77 //        while (!items.isEmpty() &&
78 //            !(items.takeLast().source == metaInformationResolver->currentSource())) {}  /* loop */;
79         int index = indexOf (metaInformationResolver->currentSource());
80         if (index >= 0 && items.size () > index - 1)
81             metaInformationResolver->setCurrentSource(items[index + 1].source);
82         qDebug () << "Error for item " << index;
83 /*        int index = sources.indexOf(metaInformationResolver->currentSource());
84         if (index >= 0)
85         {
86             sources.removeAt(index);
87             qDebug () << "Removing invalid file in " << index << ": " << metaInformationResolver->currentSource().fileName();
88             if (sources.size() > index)
89             {
90                 metaInformationResolver->setCurrentSource(sources.at(index));
91                 qDebug () << "Setting new info source " << sources.at(index).fileName();
92             }
93         }*/
94 //        int index = sources.indexOf(metaInformationResolver->currentSource()) + 1;
95 //        sources.removeAt(index - 1);
96 //        if (items.size())
97 //        {
98 //            metaInformationResolver->setCurrentSource(sources.at(0));
99 //        }
100         return;
101     }
102
103     if (newState != Phonon::StoppedState && newState != Phonon::PausedState)
104     {
105         return;
106     }
107
108     if (metaInformationResolver->currentSource().type() == Phonon::MediaSource::Invalid)
109         return;
110     int index = indexOf (metaInformationResolver->currentSource());
111     qDebug () << "Reading meta info of " << metaInformationResolver->currentSource().fileName() << " " << index;
112
113     qDebug () << "Index of this source is " << indexOf(metaInformationResolver->currentSource());
114
115     QMap<QString, QString> metaData = metaInformationResolver->metaData();
116
117 /*    QString title = metaData.value("TITLE");
118     if (title == "")
119         title = metaInformationResolver->currentSource().fileName();
120
121     if (title == "")
122         title = metaInformationResolver->currentSource().url().toString();*/
123
124     if (index >= 0)
125     {
126         items[index].artist = metaData.value("ARTIST");
127         items[index].title = metaData.value("TITLE");
128         items[index].album = metaData.value("ALBUM");
129         if (metaData.isEmpty())
130             qDebug () << "Detected to be empty: " << items[index].uri;
131         else
132             items[index].playable = true;
133         emit itemUpdated (index);
134         if (index >= 0 && items.size () > index + 1)
135             metaInformationResolver->setCurrentSource(items[index + 1].source);
136     }
137
138     /*QTableWidgetItem *titleItem = new QTableWidgetItem(title);
139     titleItem->setFlags(titleItem->flags() ^ Qt::ItemIsEditable);
140     QTableWidgetItem *artistItem = new QTableWidgetItem(metaData.value("ARTIST"));
141     artistItem->setFlags(artistItem->flags() ^ Qt::ItemIsEditable);
142     QTableWidgetItem *albumItem = new QTableWidgetItem(metaData.value("ALBUM"));
143     albumItem->setFlags(albumItem->flags() ^ Qt::ItemIsEditable);*/
144
145 /*    int currentRow = musicTable->rowCount();
146     musicTable->insertRow(currentRow);
147     musicTable->setItem(currentRow, 0, artistItem);
148     musicTable->setItem(currentRow, 1, titleItem);
149     musicTable->setItem(currentRow, 2, albumItem);*/
150
151
152 /*    if (musicTable->selectedItems().isEmpty())
153     {
154         musicTable->selectRow(0);
155         qDebug () << "Setting current media " + metaInformationResolver->currentSource().fileName();
156         mediaObject->setCurrentSource(metaInformationResolver->currentSource());
157     }
158
159     Phonon::MediaSource source = metaInformationResolver->currentSource();
160     int index = sources.indexOf(metaInformationResolver->currentSource()) + 1;
161     if (sources.size() > index)
162     {
163         metaInformationResolver->setCurrentSource(sources.at(index));
164     }
165     else
166     {
167         musicTable->resizeColumnsToContents();
168     }*/
169 }
170
171 void PlaylistManager::savePlaylist(const QString& filenam)
172 {
173     QString filename = filenam;
174     if (filename.isEmpty())
175         return;
176     if (filename.length() < 4 || filename.right(4).toLower() != ".m3u")
177         filename += ".m3u";
178     QFile f (filename);
179     try
180     {
181         f.open(QFile::WriteOnly);
182         for (int i = 0; i < items.size(); ++i)
183         {
184             f.write (items[i].uri.toAscii());
185             f.write ("\n");
186         }
187         f.close ();
188     }
189     catch (...)
190     {
191 //        QMessageBox::critical(this, "Write error", "Could not write playlist file", QMessageBox::Ok);
192     }
193 }
194
195 void PlaylistManager::loadPlaylist(const QString& filename)
196 {
197     qDebug () << "Attempting to load playlist: " << filename;
198     QFile f(filename);
199     if (!f.open (QFile::ReadOnly))
200         return;
201     QString tmp = f.readAll();
202     f.close ();
203     QStringList lines = tmp.split("\n");
204     clearPlaylist();
205     foreach (QString l, lines)
206     {
207         if (l.isEmpty() || (!QFileInfo (l).exists() && (l.indexOf("http") != 0)))
208         {
209             continue;
210         }
211         qDebug () << "Load " << l;
212         items.append(PlaylistItem (l));
213     }
214     if (!items.isEmpty())
215         metaInformationResolver->setCurrentSource(items.at(0).source);
216     emit playlistChanged (0);
217 }
218
219 void PlaylistManager::clearPlaylist()
220 {
221     items.clear();
222     emit playlistChanged(0);
223 /*    while (musicTable->rowCount())
224         musicTable->removeRow(0);
225     mediaObject->clear();*/
226 }
227
228 QStringList PlaylistManager::playlistStrings() const
229 {
230     QStringList ret;
231     for (int i = 0; i < items.size (); ++i)
232         ret << items[i].uri;
233     qDebug () << "Returning playlist " << ret << " SIZE: " << items.size ();
234     return ret;
235 }
236
237 void PlaylistManager::removeItem(int i)
238 {
239     items.removeAt (i);
240     emit playlistChanged(i);
241 }