on xenial help sign in
Merged main branch.
commit95db08b25226d410060f48a5414c114bd56a56c9 authorPawel Stolowski <pawel.stolowski@canonical.com> dateWed 9 Oct 2013, 14:02 (13 years ago) parenta0b455ef4abdf5
1 file changed+23−18
plugins/Utils/albumartprovider.cpp+23−18
plugins/Utils/albumartprovider.cpp+23−18
@@ -24,14 +24,12 @@
2424#include <QNetworkRequest>
2525#include <QNetworkReply>
2626#include <QXmlQuery>
27+#include <QTemporaryFile>
2728#include <QBuffer>
28−#include <cstdio>
2929#include <QDebug>
3030#include <QFile>
3131#include <QStringList>
3232#include <QByteArray>
33−#include <unistd.h>
34−#include <memory>
3533#include <QImage>
3634#include <QUrl>
3735
@@ -171,25 +169,32 @@ std::string AlbumArtProvider::get_image(const std::string &artist, const std::st
171169 if(image_url.empty()) {
172170 return DEFAULT_ALBUM_ART;
173171 }
174− char tmpname[] = "/tmp/path/to/some/file/somewhere/maybe.jpg";
175− tmpnam(tmpname);
176− std::unique_ptr<char, int(*)(const char*)> deleter(tmpname, unlink);
172+ QTemporaryFile tempFile;
173+ tempFile.open();
174+ tempFile.setAutoRemove(true);
175+ QString fname = tempFile.fileName();
176+ std::string tmpname = fname.toUtf8().data();
177+
177178 if(!download_and_store(image_url, tmpname)) {
178179 return DEFAULT_ALBUM_ART;
179180 }
180− fix_format(tmpname);
181− FILE *f = fopen(tmpname, "r");
182− fseek(f, 0, SEEK_END);
183− long s = ftell(f);
184− fseek(f, 0, SEEK_SET);
185− char *buf = new char[s];
186− fread(buf, 1, s, f);
187− fclose(f);
181+ fix_format(tmpname.c_str());
182+ QFile f(fname);
183+ if(!f.open(QIODevice::ReadWrite)) {
184+ return DEFAULT_ALBUM_ART;
185+ }
186+ QByteArray arr = f.readAll();
187+ f.close();
188188
189− // Fixme, leaks buf if throws.
190− cache.add_art(info.artist, info.album, buf, s);
191− delete []buf;
192− return cache.get_art_file(info.artist, info.album);
189+ cache.add_art(info.artist, info.album, arr.data(), arr.size());
190+ try {
191+ std::string res = cache.get_art_file(info.artist, info.album);
192+ if(res.empty())
193+ return DEFAULT_ALBUM_ART;
194+ return res;
195+ } catch(...) {
196+ }
197+ return DEFAULT_ALBUM_ART;
193198}
194199
195200QImage AlbumArtProvider::requestImage(const QString &id, QSize *realSize, const QSize &requestedSize) {