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