QVariantMap TrackListDBusHandler::GetMetadata(int position)
{
    QVariantMap ret;
    if (position < 0 || position > m_tracks.size()-1) {
        return ret;
    }

    //FIXME: ugly and slow
    Phonon::MediaObject mediaObject;
    mediaObject.setCurrentSource(m_tracks[position]);

    QMultiMap<QString, QString> stringMap = mediaObject.metaData();
    QMultiMap<QString, QString>::const_iterator i = stringMap.constBegin();

    while (i != stringMap.constEnd()) {
        bool number = false;
        int value = i.value().toInt(&number);

        //tracknumber always string, according to MPRIS spec
        if (number && (i.key().toLower() != "tracknumber")) {
            ret[i.key().toLower()] = value;
        } else {
            ret[i.key().toLower()] = QVariant(i.value());
        }
        ++i;
    }

    ret["time"] = mediaObject.totalTime()/1000;

    ret["location"] = mediaObject.currentSource().url().toString();
    return ret;
}
Example #2
0
void APlayer::setDir(QString path)
{
	old = 0;
	m_media->clear();
	w_folderContents->clear();
	while (QTreeWidgetItem *item = w_folderContents->takeTopLevelItem(0))
	{
		delete item;
	}
	path += "/";
	QDir dir(path);
	QStringList files = dir.entryList(QStringList() << "*.mp3" << "*.ogg" << "*.wav" << "*.wma",
									  QDir::Files | QDir::NoDotAndDotDot,
									  QDir::Name);
	QString f = path + "%2";
	Phonon::MediaObject media;
	foreach (QString file, files)
	{
		QString filepath = f.arg(file);
		media.setCurrentSource(filepath);
		QMap <QString,QString> metaData = media.metaData();
		QStringList metaList;
		metaList << metaData.value("ARTIST")
				 << metaData.value("TITLE")
				 << metaData.value("ALBUM")
				 << metaData.value("DATE");
		qDebug() << metaList;
		if (metaList[0].isEmpty())
			metaList[0] = file;
		QTreeWidgetItem *item = new QTreeWidgetItem(metaList);
		item->setData(0, Qt::UserRole, filepath);
		w_folderContents->addTopLevelItem(item);
	}