#include "mainwindow.h"
#include "time.h"
-#define AVOID_INPUT_DIALOG 0
+//#define AVOID_INPUT_DIALOG 0
MainWindow::MainWindow()
: plman (this), settings (tr ("TomAmp"), "TomAmp")
connect(mediaObject, SIGNAL(currentSourceChanged(Phonon::MediaSource)),
this, SLOT(sourceChanged(Phonon::MediaSource)));
connect(mediaObject, SIGNAL(aboutToFinish()), this, SLOT(aboutToFinish()));
+ connect (&plman, SIGNAL (playlistChanged (int)), this, SLOT (playlistChanged(int)));
+ connect (&plman, SIGNAL (itemUpdated(int)), this, SLOT (itemUpdated (int)));
Phonon::createPath(mediaObject, audioOutput);
{
settings.setValue("shuffle", shuffle);
settings.setValue("repeat", repeat);
- settings.setValue("lastPlaylist", plman.playlist());
+ settings.setValue("lastPlaylist", plman.playlistStrings());
settings.setValue("volume", audioOutput->volume());
}
toadd.append (string);
}
plman.addStringList(toadd);
- setupShuffleList();
}
void MainWindow::addFolder()
if (files.size())
recursive = QMessageBox::question(this, "Add all folders", "Subfolders have been detected, add everything?", QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes;
plman.parseAndAddFolder(dir, recursive);
- setupShuffleList();
}
QStringList toadd;
toadd << url;
plman.addStringList(toadd);
- setupShuffleList();
}
void MainWindow::next()
{
bool wasPlaying = (mediaObject->state () == Phonon::PlayingState);
+ if (mediaObject->state () == Phonon::ErrorState)
+ wasPlaying = true;
int index = plman.indexOf(mediaObject->currentSource()) + 1;
if (shuffle)
{
- index = shuffleList.indexOf(plman.indexOf(mediaObject->currentSource())) + 1;
+ index = shuffleList.indexOf(plman.indexOf(mediaObject->currentSource()));
+ do
+ {
+ index += 1;
+ }
+ while (index < shuffleList.size () && !plman.getItem(index).playable);
if (index < shuffleList.size ())
{
mediaObject->setCurrentSource(plman.at (shuffleList[index]));
}
else if (repeat)
{
- mediaObject->setCurrentSource(plman.at (shuffleList[0]));
+ index = 0;
+ do
+ {
+ index += 1;
+ }
+ while (index < shuffleList.size () && !plman.getItem(index).playable);
+ if (index < shuffleList.size ())
+ mediaObject->setCurrentSource(plman.at (shuffleList[index]));
}
}
else
{
+ while (index < plman.size () && !plman.getItem(index).playable);
+ {
+ index += 1;
+ }
if (plman.size() > index)
{
mediaObject->setCurrentSource(plman.at(index));
}
else if (repeat)
{
- mediaObject->setCurrentSource(plman.at(0));
+ index = 0;
+ do
+ {
+ index += 1;
+ }
+ while (index < shuffleList.size () && !plman.getItem(index).playable);
+ mediaObject->setCurrentSource(plman.at(index));
}
}
if (wasPlaying)
if (row >= plman.size())
return;
- mediaObject->setCurrentSource(plman[row]);
+ int index = row;
+ while (index < plman.size () && !plman.getItem(index).playable);
+ {
+ index += 1;
+ }
+ if (plman.size() > index)
+ {
+ mediaObject->setCurrentSource(plman.at(index));
+ int ind = shuffleList.indexOf(index);
+ shuffleList.removeAt(ind);
+ shuffleList.insert(0, index);
+ qDebug () << "Modified shuffle list: " << shuffleList;
+ mediaObject->play();
+ }
+ else
+ {
+ next ();
+ }
- mediaObject->play();
- int ind = shuffleList.indexOf(row);
- shuffleList.removeAt(ind);
- shuffleList.insert(0, row);
- qDebug () << "Modified shuffle list: " << shuffleList;
}
void MainWindow::sourceChanged(const Phonon::MediaSource &source)
connect(addUrlAction, SIGNAL(triggered()), this, SLOT(addUrl()));
connect (savePlaylistAction, SIGNAL (triggered()), this, SLOT (savePlaylist()));
connect (loadPlaylistAction, SIGNAL (triggered()), this, SLOT (loadPlaylist()));
- connect (clearPlaylistAction, SIGNAL (triggered()), this, SLOT (clearPlaylist()));
+ connect (clearPlaylistAction, SIGNAL (triggered()), &plman, SLOT (clearPlaylist()));
connect (nextAction, SIGNAL(triggered()), this, SLOT(next()));
connect (previousAction, SIGNAL(triggered()), this, SLOT(previous()));
connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));
qDebug () << shuffleList;
qDebug () << shuffleList;
}
+
+void MainWindow::savePlaylist ()
+{
+ QString filename = QFileDialog::getSaveFileName(this, tr("Please select file name"), "", "Playlist Files (*.m3u)");
+ plman.loadPlaylist(filename);
+}
+
+void MainWindow::loadPlaylist ()
+{
+ QString filename = QFileDialog::getOpenFileName(this, tr("Select playlist file to load"), "", "*.m3u");
+ plman.loadPlaylist (filename);
+}
+
+void MainWindow::playlistChanged(int from)
+{
+ while (musicTable->rowCount() > from)
+ {
+ musicTable->removeRow(musicTable->rowCount () - 1);
+ }
+ for (int i = from; i < plman.size (); ++i)
+ {
+ int currentRow = musicTable->rowCount();
+ musicTable->insertRow(currentRow);
+ setRowFromItem (currentRow, plman.getItem(i));
+ }
+ setupShuffleList();
+}
+
+void MainWindow::setRowFromItem (int row, const PlaylistItem& item)
+{
+ if (row >= musicTable->rowCount())
+ return;
+ if (item.artist.isEmpty() && item.title.isEmpty())
+ {
+ QTableWidgetItem *item1 = new QTableWidgetItem(item.uri);
+ item1->setFlags(item1->flags() ^ Qt::ItemIsEditable);
+ musicTable->setItem(row, 1, item1);
+ }
+ else
+ {
+ QTableWidgetItem *item1 = new QTableWidgetItem(item.artist);
+ item1->setFlags(item1->flags() ^ Qt::ItemIsEditable);
+ musicTable->setItem(row, 0, item1);
+ QTableWidgetItem *item2 = new QTableWidgetItem(item.title);
+ item2->setFlags(item2->flags() ^ Qt::ItemIsEditable);
+ musicTable->setItem(row, 1, item2);
+ QTableWidgetItem *item3 = new QTableWidgetItem(item.album);
+ item3->setFlags(item3->flags() ^ Qt::ItemIsEditable);
+ musicTable->setItem(row, 2, item3);
+ }
+}
+
+void MainWindow::itemUpdated(int index)
+{
+ setRowFromItem (index, plman.getItem(index));
+}
void next();
void previous();
void cellClicked(int row, int column);
+ void savePlaylist ();
+ void loadPlaylist ();
+ void playlistChanged (int from);
+ void itemUpdated (int index);
private:
void setupActions();
void setupMenus();
void setupUi();
void setupShuffleList();
+ void setRowFromItem (int row, const PlaylistItem& item);
Phonon::SeekSlider *seekSlider;
Phonon::MediaObject *mediaObject;
#include "playlistmanager.h"
#include <QDir>
+#include <QUrl>
PlaylistManager::PlaylistManager(QWidget* parent)
: parentWidget (parent)
this, SLOT(metaStateChanged(Phonon::State,Phonon::State)));
}
+int PlaylistManager::indexOf(const Phonon::MediaSource &s) const
+{
+ for (int i = 0; i < items.size(); ++i)
+ {
+ if (items[i].source == s)
+ return i;
+ }
+ return -1;
+}
+
void PlaylistManager::parseAndAddFolder(const QString &dir, bool recursive)
{
QStringList filters;
qDebug () << "Parsing folder " << dir;
- settings.setValue("LastFolder", dir);
+ //settings.setValue("LastFolder", dir);
int index = items.size();
foreach (QString string, files)
{
parseAndAddFolder(fname, true);
continue;
}
- qDebug () << fname;
+ qDebug () << "Adding: " << fname;
items.append(PlaylistItem (PlaylistItem (fname)));
}
if (!items.isEmpty())
metaInformationResolver->setCurrentSource(items.at(index).source);
+ qDebug () << " SIZE: " << items.size ();
+ emit playlistChanged (index);
}
void PlaylistManager::addStringList(const QStringList& list)
int index = items.size();
foreach (QString string, list)
{
+ qDebug () << "Adding " << string;
items.append(PlaylistItem (string));
}
if (!items.isEmpty())
metaInformationResolver->setCurrentSource(items.at(index).source);
+ emit playlistChanged(index);
}
void PlaylistManager::metaStateChanged(Phonon::State newState, Phonon::State /* oldState */)
{
// QMessageBox::warning(this, tr("Error opening files"),
// metaInformationResolver->errorString());
- while (!items.isEmpty() &&
- !(items.takeLast().source == metaInformationResolver->currentSource())) {} /* loop */;
- qDebug () << items.size();
+// while (!items.isEmpty() &&
+// !(items.takeLast().source == metaInformationResolver->currentSource())) {} /* loop */;
+ int index = indexOf (metaInformationResolver->currentSource());
+ if (index >= 0 && items.size () > index - 1)
+ metaInformationResolver->setCurrentSource(items[index + 1].source);
+ qDebug () << "Error for item " << index;
/* int index = sources.indexOf(metaInformationResolver->currentSource());
if (index >= 0)
{
if (metaInformationResolver->currentSource().type() == Phonon::MediaSource::Invalid)
return;
- qDebug () << "Reading meta info of " << metaInformationResolver->currentSource().fileName() << " " << metaInformationResolver->currentSource().type();
+ int index = indexOf (metaInformationResolver->currentSource());
+ qDebug () << "Reading meta info of " << metaInformationResolver->currentSource().fileName() << " " << index;
- qDebug () << "Index of this source is " << items.indexOf(metaInformationResolver->currentSource());
+ qDebug () << "Index of this source is " << indexOf(metaInformationResolver->currentSource());
QMap<QString, QString> metaData = metaInformationResolver->metaData();
- QString title = metaData.value("TITLE");
+/* QString title = metaData.value("TITLE");
if (title == "")
title = metaInformationResolver->currentSource().fileName();
if (title == "")
- title = metaInformationResolver->currentSource().url().toString();
+ title = metaInformationResolver->currentSource().url().toString();*/
- QTableWidgetItem *titleItem = new QTableWidgetItem(title);
+ if (index >= 0)
+ {
+ items[index].artist = metaData.value("ARTIST");
+ items[index].title = metaData.value("TITLE");
+ items[index].album = metaData.value("ALBUM");
+ if (metaData.isEmpty())
+ qDebug () << "Detected to be empty: " << items[index].uri;
+ else
+ items[index].playable = true;
+ emit itemUpdated (index);
+ if (index >= 0 && items.size () > index + 1)
+ metaInformationResolver->setCurrentSource(items[index + 1].source);
+ }
+
+ /*QTableWidgetItem *titleItem = new QTableWidgetItem(title);
titleItem->setFlags(titleItem->flags() ^ Qt::ItemIsEditable);
QTableWidgetItem *artistItem = new QTableWidgetItem(metaData.value("ARTIST"));
artistItem->setFlags(artistItem->flags() ^ Qt::ItemIsEditable);
QTableWidgetItem *albumItem = new QTableWidgetItem(metaData.value("ALBUM"));
- albumItem->setFlags(albumItem->flags() ^ Qt::ItemIsEditable);
+ albumItem->setFlags(albumItem->flags() ^ Qt::ItemIsEditable);*/
/* int currentRow = musicTable->rowCount();
musicTable->insertRow(currentRow);
}*/
}
-void PlaylistManager::savePlaylist(const QString& filename)
+void PlaylistManager::savePlaylist(const QString& filenam)
{
-// QString filename = QFileDialog::getSaveFileName(parentWidget, tr("Please select file name"), "", "Playlist Files (*.m3u)");
+ QString filename = filenam;
if (filename.isEmpty())
return;
if (filename.length() < 4 || filename.right(4).toLower() != ".m3u")
void PlaylistManager::loadPlaylist(const QString& filename)
{
-// QString filename = QFileDialog::getOpenFileName(parentWidget, tr("Select playlist file to load"), "", "*.m3u");
+ qDebug () << "Attempting to load playlist: " << filename;
QFile f(filename);
f.open (QFile::ReadOnly);
QString tmp = f.readAll();
{
if (l.isEmpty() || (!QFileInfo (l).exists() && (l.indexOf("http") != 0)))
{
- qDebug () << "not loadable: " << l;\
continue;
}
qDebug () << "Load " << l;
}
if (!items.isEmpty())
metaInformationResolver->setCurrentSource(items.at(0).source);
+ emit playlistChanged (0);
}
-void MainWindow::clearPlaylist()
+void PlaylistManager::clearPlaylist()
{
items.clear();
+ emit playlistChanged(0);
/* while (musicTable->rowCount())
musicTable->removeRow(0);
mediaObject->clear();*/
}
+
+QStringList PlaylistManager::playlistStrings() const
+{
+ QStringList ret;
+ for (int i = 0; i < items.size (); ++i)
+ ret << items[i].uri;
+ qDebug () << "Returning playlist " << ret << " SIZE: " << items.size ();
+ return ret;
+}
bool operator ==(const Phonon::MediaSource& s) const { return source == s; }
};
-class PlaylistManager
+class PlaylistManager : public QObject
{
+ Q_OBJECT
public:
PlaylistManager(QWidget* parent);
void addStringList (const QStringList&);
void parseAndAddFolder (const QString& dir, bool recursive);
- QStringList playlist () const { return QStringList (); }
+ QStringList playlistStrings () const;
int size () const { return items.size (); }
- int indexOf (const Phonon::MediaSource& s) const { return items.indexOf (s); }
+ int indexOf (const Phonon::MediaSource& s) const;
const Phonon::MediaSource& at (int i) { return items[i].source; }
const PlaylistItem& getItem (int i) const { return items[i]; }
public slots:
void loadPlaylist(const QString& filename);
void clearPlaylist();
signals:
- void playlistChanged (QStringList newItems);
+ void playlistChanged (int from);
+ void itemUpdated (int index);
private slots:
void metaStateChanged(Phonon::State newState, Phonon::State oldState);
private: