@@ -25,114 +25,20 @@
2525#include <QNetworkReply>
2626#include <QXmlQuery>
2727#include <QBuffer>
28−#include<cstdio>
29−#include<QDebug>
30−#include<QFile>
31−#include<QByteArray>
32−#include<gst/gst.h>
33−#include<unistd.h>
34−#include<memory>
35−#include<QImage>
28+#include <cstdio>
29+#include <QDebug>
30+#include <QFile>
31+#include <QStringList>
32+#include <QByteArray>
33+#include <unistd.h>
34+#include <memory>
35+#include <QImage>
36+#include <QUrl>
3637
3738using namespace std;
3839
3940const std::string AlbumArtProvider::DEFAULT_ALBUM_ART = "/usr/share/unity/icons/album_missing.png";
4041
41−static void on_new_pad (GstElement * /*dec*/, GstPad * pad, GstElement * fakesink) {
42− GstPad *sinkpad;
43−
44− sinkpad = gst_element_get_static_pad (fakesink, "sink");
45− if (!gst_pad_is_linked (sinkpad)) {
46− if (gst_pad_link (pad, sinkpad) != GST_PAD_LINK_OK)
47− g_error ("Failed to link pads!");
48− }
49− gst_object_unref (sinkpad);
50−}
51−
52−static void process_one_tag (const GstTagList * list, const gchar * tag, gpointer user_data) {
53− struct albuminfo *md = (struct albuminfo*) user_data;
54− int i, num;
55− string tagname(tag);
56−
57− num = gst_tag_list_get_tag_size (list, tag);
58− for (i = 0; i < num; ++i) {
59− gchar *val;
60− if (gst_tag_get_type(tag) == G_TYPE_STRING) {
61− if (gst_tag_list_get_string_index (list, tag, i, &val)) {
62− if(tagname == "artist")
63− md->artist = std::string(val);
64− if(tagname == "album")
65− md->album = std::string(val);
66− }}
67− }
68−}
69−
70−albuminfo AlbumArtProvider::get_album_info(const std::string &filename) throw (runtime_error)
71−{
72− albuminfo ai;
73− // FIXME: Need to do quoting. Files with %'s in their names seem to confuse gstreamer.
74−
75− const string uri = "file://" + filename;
76−
77− GstElement *dec = gst_element_factory_make ("uridecodebin", NULL);
78− if (dec == nullptr)
79− throw runtime_error("Failed to create uridecodebin element");
80−
81− GstElement *pipe = gst_pipeline_new ("pipeline");
82−
83− g_object_set (dec, "uri", uri.c_str(), NULL);
84− gst_bin_add (GST_BIN (pipe), dec);
85−
86− GstElement *sink = gst_element_factory_make ("fakesink", NULL);
87− gst_bin_add (GST_BIN (pipe), sink);
88− g_signal_connect (dec, "pad-added", G_CALLBACK (on_new_pad), sink);
89−
90− gst_element_set_state (pipe, GST_STATE_PAUSED);
91−
92− GstMessage *msg;
93− while (true) {
94− GstTagList *tags = NULL;
95−
96− msg = gst_bus_timed_pop_filtered(GST_ELEMENT_BUS (pipe),
97− GST_CLOCK_TIME_NONE, (GstMessageType) (GST_MESSAGE_ASYNC_DONE | GST_MESSAGE_TAG | GST_MESSAGE_ERROR));
98−
99− if (GST_MESSAGE_TYPE (msg) != GST_MESSAGE_TAG) /* error or async_done */
100− break;
101−
102− gst_message_parse_tag (msg, &tags);
103− gst_tag_list_foreach (tags, process_one_tag, &ai);
104− gst_tag_list_unref (tags);
105−
106− gst_message_unref (msg);
107− };
108−
109− gst_element_set_state (pipe, GST_STATE_NULL);
110− gst_object_unref (pipe);
111−
112− if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR) {
113− GError *err;
114− gchar *dbg_info;
115− gst_message_parse_error(msg, &err, &dbg_info);
116− string errortxt(err->message);
117− string dbtxt(dbg_info);
118− g_error_free(err);
119− g_free(dbg_info);
120−
121− gst_message_unref (msg);
122− string msg = "Extracting metadata of file ";
123− msg += filename;
124− msg += " failed: ";
125− msg += errortxt;
126− msg += " ";
127− msg += dbg_info;
128− throw runtime_error(msg);
129− }
130−
131− gst_message_unref (msg);
132−
133− return ai;
134−}
135−
13642std::string AlbumArtProvider::get_lastfm_url(const albuminfo &ai) {
13743 QString artist = QString::fromStdString(ai.artist);
13844 QString album = QString::fromStdString(ai.album);
@@ -154,8 +60,6 @@ std::string AlbumArtProvider::get_lastfm_url(const albuminfo &ai) {
15460 return "";
15561 }
15662
157−
158−
15963 QXmlQuery query;
16064 QBuffer tmp;
16165 tmp.setData(reply->readAll());
@@ -212,14 +116,12 @@ void AlbumArtProvider::fix_format(const std::string &fname) {
212116 im.save(fname.c_str(), "JPEG");
213117}
214118
215−std::string AlbumArtProvider::get_image(const std::string &filename) {
119+std::string AlbumArtProvider::get_image(const std::string &artist, const std::string &album) {
216120 albuminfo info;
217− try {
218− info = get_album_info(filename);
219− } catch(std::exception &e) {
220− qDebug() << "Could not determine album info: " << e.what();
221− return DEFAULT_ALBUM_ART;
222− }
121+
122+ info.artist = artist;
123+ info.album = album;
124+
223125 if(info.album.empty() || info.artist.empty()) {
224126 return DEFAULT_ALBUM_ART;
225127 }
@@ -256,10 +158,17 @@ std::string AlbumArtProvider::get_image(const std::string &filename) {
256158QImage AlbumArtProvider::requestImage(const QString &id, QSize *realSize, const QSize &requestedSize) {
257159 Q_UNUSED(requestedSize)
258160
259− std::string src_path(id.toUtf8().data());
161+ const QStringList parts = id.split("/");
162+ if (parts.size() != 2) {
163+ qWarning("Invalid albumart uri");
164+ return QImage(QString::fromStdString(DEFAULT_ALBUM_ART));
165+ }
166+ const std::string artist = QUrl::fromPercentEncoding(parts[0].toUtf8()).toStdString();
167+ const std::string album = QUrl::fromPercentEncoding(parts[1].toUtf8()).toStdString();
168+
260169 std::string tgt_path;
261170 try {
262− tgt_path = get_image(src_path);
171+ tgt_path = get_image(artist, album);
263172 if(!tgt_path.empty()) {
264173 QString tgt(tgt_path.c_str());
265174 QImage image;
@@ -275,5 +184,5 @@ QImage AlbumArtProvider::requestImage(const QString &id, QSize *realSize, const
275184 }
276185
277186 *realSize = QSize(0, 0);
278− return QImage();
187+ return QImage(QString::fromStdString(DEFAULT_ALBUM_ART));
279188}