Skip to content

Commit

Permalink
[Feature] Add local cover art when importing tracks (#75)
Browse files Browse the repository at this point in the history
* [Feature] add initial addition for cover art if a jpg is found in the directory as tracks are added to the db. also, correct old Generic paths, and test with sailjail.

* PR: feedback integrated: 1. alternate logic for applying folder/cover image copy, basename check. 2. remove media indexing sailjail perms, 3. move cache dir creation from migrate to main

* [FlowPlayer.cpp] Improve style as suggested by @dcaliste

* [datareader.cpp] Improve style as suggested by @dcaliste

* [flowplayer.desktop] Omit SailJail sandboxing configuration for now

* [datareader.cpp] Improve style as suggested by @dcaliste

* Review: remove png processing

* Update datareader.cpp
  remove unused QImage and png matching.

* Fix:logic of the || on png

* [datareader.cpp] Indention, no `jpg` & `png` for now, iterator reuse
  Addresses comments dispersed over PR #75's lengthy discussion.

* [datareader.cpp] Rectify comment

* [datareader.cpp] Improve comments & break long code line in two

* [datareader.cpp] Break two more long lines in two

* [datareader.cpp] Remove superfluous backslashes "\"

* [datareader.cpp] Omit superfluous space character " "

* [datareader.cpp] Extend comment

* Don't recurse in subdirs when looking for covers.

* [datareader.cpp] Insert comment and align with current TS files

---------

Co-authored-by: olf <[email protected]>
Co-authored-by: Damien Caliste <[email protected]>
  • Loading branch information
3 people authored Mar 14, 2024
1 parent 6208fd1 commit db827fe
Show file tree
Hide file tree
Showing 17 changed files with 518 additions and 357 deletions.
3 changes: 3 additions & 0 deletions src/FlowPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ int main(int argc, char *argv[])

app->installTranslator(&translator);

// ensure the media cache dir is created
const QString mediaCacheDir = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/media-art";
QDir().mkpath(mediaCacheDir);

QScopedPointer<QQuickView> window(SailfishApp::createView());
window->setTitle("FlowPlayer");
Expand Down
2 changes: 1 addition & 1 deletion src/coversearch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ void CoverSearch::paintImg(QString image, int index)

void CoverSearch::saveImage(QString artist, QString album, QString imagepath)
{
QString th2 = QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + "/media-art/album-"+ doubleHash(artist, album) + ".jpeg";
QString th2 = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/media-art/album-"+ doubleHash(artist, album) + ".jpeg";

QImage image(imagepath);
image.save(th2, "JPEG");
Expand Down
31 changes: 31 additions & 0 deletions src/datareader.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "datareader.h"
#include "globalutils.h"

#include <mpegfile.h>
#include <flacfile.h>
Expand All @@ -25,6 +26,7 @@
#include <QStringList>
#include <QSettings>
#include <QDebug>
#include <QStandardPaths>

extern bool databaseWorking;
extern bool isDBOpened;
Expand Down Expand Up @@ -185,6 +187,10 @@ TagLib::File* DataReader::getFileByMimeType(QString file)

void DataReader::readFile(QString file)
{
// Is oFile used somewhere? (I failed to find a location.)
// If not, what is this new line good for? For details, see PR #75.
QString oFile = file;

file.remove("file://");
TagLib::File* tf = getFileByMimeType(file);

Expand All @@ -202,6 +208,31 @@ void DataReader::readFile(QString file)
m_tracknum = QString::number(tagFile->tag()->track());

if (m_title=="") m_title = QFileInfo(file).baseName();

// if we have artist and album, we check for a cover image.
if (m_artist != "" && m_album != "") {
QFileInfo info(file);
QDirIterator iterator(info.dir());
while (iterator.hasNext()) {
iterator.next();
// we are explicit about two common factors, the type JPEG (ToDo: add PNG
// throughout all C++ source files, see issue #78), and basename cover or folder
if (iterator.fileInfo().isFile()) {
if ( (iterator.fileInfo().suffix() == "jpeg" ||
iterator.fileInfo().suffix() == "jpg") &&
// See ToDo above: (… ||
// iterator.fileInfo().suffix() == "png") &&
(iterator.fileInfo().baseName() == "cover" ||
iterator.fileInfo().baseName() == "folder") ) {
QString th2 = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) +
"/media-art/album-" + doubleHash(m_artist, m_album) + ".jpeg";
qDebug() << "COPYING FILE ART: " << iterator.filePath() << m_artist << m_album;
QFile::copy(iterator.filePath(), th2);
}
}
}
}

if (m_artist=="") m_artist = tr("Unknown artist");
if (m_album=="") m_album = tr("Unknown album");

Expand Down
4 changes: 2 additions & 2 deletions src/datos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ bool namefileLessThan(const QStringList &d1, const QStringList &d2)

QString Datos::getThumbnail(QString data, int index)
{
QString th1 = QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + "/media-art/album-"+ data + ".jpeg";
QString th1 = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/media-art/album-"+ data + ".jpeg";

/*QString th2 = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/flowplayer/62/album-"+ data + ".jpeg";
Expand Down Expand Up @@ -320,7 +320,7 @@ void Datos::DatosPrivate::populateItems()
item->band = q->listado[i][3];
item->songs = q->listado[i][4];
item->hash = doubleHash(item->acount=="1"? item->artist : item->title, item->title);
item->coverart = QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + "/media-art/album-"+ item->hash + ".jpeg";
item->coverart = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/media-art/album-"+ item->hash + ".jpeg";
item->isSelected = false;
} else if (groupFilter=="artist") {
item->artist = q->listado[i][1]=="1"? tr("1 album") : tr("%1 albums").arg( q->listado[i][1].toInt());
Expand Down
16 changes: 8 additions & 8 deletions src/loadwebimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ bool WebThread::checkInternal()

if (QFileInfo(dir + "/folder.jpg").exists())
{
QString th2 = QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + "/media-art/album-"+ doubleHash(artist, album) + ".jpeg";
QString th2 = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/media-art/album-"+ doubleHash(artist, album) + ".jpeg";
QImage image(dir + "/folder.jpg");
image.save(th2, "JPEG");
emit imgLoaded(th2, files[0][2].toInt());
Expand All @@ -89,7 +89,7 @@ bool WebThread::checkInternal()

else if (QFileInfo(dir + "/folder.jpeg").exists())
{
QString th2 = QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + "/media-art/album-"+ doubleHash(artist, album) + ".jpeg";
QString th2 = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/media-art/album-"+ doubleHash(artist, album) + ".jpeg";
QImage image(dir + "/folder.jpeg");
image.save(th2, "JPEG");
emit imgLoaded(th2, files[0][2].toInt());
Expand All @@ -98,7 +98,7 @@ bool WebThread::checkInternal()

else if (QFileInfo(dir + "/cover.jpg").exists())
{
QString th2 = QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + "/media-art/album-"+ doubleHash(artist, album) + ".jpeg";
QString th2 = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/media-art/album-"+ doubleHash(artist, album) + ".jpeg";
QImage image(dir + "/cover.jpg");
image.save(th2, "JPEG");
emit imgLoaded(th2, files[0][2].toInt());
Expand All @@ -107,7 +107,7 @@ bool WebThread::checkInternal()

if (QFileInfo(dir + "/Folder.jpg").exists())
{
QString th2 = QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + "/media-art/album-"+ doubleHash(artist, album) + ".jpeg";
QString th2 = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/media-art/album-"+ doubleHash(artist, album) + ".jpeg";
QImage image(dir + "/Folder.jpg");
image.save(th2, "JPEG");
emit imgLoaded(th2, files[0][2].toInt());
Expand All @@ -116,7 +116,7 @@ bool WebThread::checkInternal()

else if (QFileInfo(dir + "/Folder.jpeg").exists())
{
QString th2 = QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + "/media-art/album-"+ doubleHash(artist, album) + ".jpeg";
QString th2 = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/media-art/album-"+ doubleHash(artist, album) + ".jpeg";
QImage image(dir + "/Folder.jpeg");
image.save(th2, "JPEG");
emit imgLoaded(th2, files[0][2].toInt());
Expand All @@ -125,7 +125,7 @@ bool WebThread::checkInternal()

else if (QFileInfo(dir + "/Cover.jpg").exists())
{
QString th2 = QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + "/media-art/album-"+ doubleHash(artist, album) + ".jpeg";
QString th2 = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/media-art/album-"+ doubleHash(artist, album) + ".jpeg";
QImage image(dir + "/Cover.jpg");
image.save(th2, "JPEG");
emit imgLoaded(th2, files[0][2].toInt());
Expand Down Expand Up @@ -292,7 +292,7 @@ QString WebThread::saveToDisk(QIODevice *reply)

QString art = files[0][0];
QString alb = files[0][1];
QString th2 = QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + "/media-art/album-"+ doubleHash(art, alb) + ".jpeg";
QString th2 = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/media-art/album-"+ doubleHash(art, alb) + ".jpeg";

QImage image = QImage::fromData(reply->readAll());
image.save(th2, "JPEG");
Expand All @@ -304,7 +304,7 @@ QString WebThread::saveToDiskExtern(QIODevice *reply)
{
QImage image = QImage::fromData(reply->readAll());

QString path = QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + "/flowplayer/" + hash(curImage) + ".jpeg";
QString path = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/flowplayer/" + hash(curImage) + ".jpeg";

image.save(path, "JPEG");

Expand Down
4 changes: 2 additions & 2 deletions src/missing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ void Missing::loadData()

if (dato1!=tr("Unknown album") && dato2!=tr("Unknown artist"))
{
QString th2 = QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + "/media-art/album-"+ doubleHash(dato2, dato1) + ".jpeg";
QString th2 = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/media-art/album-"+ doubleHash(dato2, dato1) + ".jpeg";
if ( ! QFileInfo(th2).exists() )
{
QString th3 = QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + "/media-art/album-"+ doubleHash(dato1, dato1); + ".jpeg";
QString th3 = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/media-art/album-"+ doubleHash(dato1, dato1); + ".jpeg";
if ( ! QFileInfo(th3).exists() )
{
//qDebug() << dato1 << " doesn't exist. Adding to list";
Expand Down
22 changes: 11 additions & 11 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ void Utils::readLyrics(QString artist, QString song)
QString sng = cleanItem(song);
if ( ( art!="" ) && ( sng!="" ) )
{
QString th1 = QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + "/lyrics/"+art+"-"+sng+".txt";
QString th1 = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/lyrics/"+art+"-"+sng+".txt";

if ( QFileInfo(th1).exists() )
{
Expand Down Expand Up @@ -116,10 +116,10 @@ QString Utils::thumbnail(QString artist, QString album, QString count)
QString art = count=="1"? artist : album;
QString alb = album;

QString th1 = QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + "/media-art/album-"+ doubleHash(art, alb) + ".jpeg";
QString th1 = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/media-art/album-"+ doubleHash(art, alb) + ".jpeg";

if (!QFileInfo(th1).exists()) {
QString th2 = QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + "/media-art/album-"+ doubleHash(alb, alb) + ".jpeg";
QString th2 = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/media-art/album-"+ doubleHash(alb, alb) + ".jpeg";
if (QFileInfo(th2).exists())
return th2;
}
Expand Down Expand Up @@ -355,11 +355,11 @@ void Utils::downloaded(QNetworkReply *respuesta)
void Utils::saveLyrics(QString artist, QString song, QString lyrics)
{
QDir d;
d.mkdir(QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + "/lyrics");
d.mkdir(QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/lyrics");

QString art = cleanItem(artist);
QString sng = cleanItem(song);
QString f = QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + "/lyrics/"+art+"-"+sng+".txt";
QString f = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/lyrics/"+art+"-"+sng+".txt";

if ( QFileInfo(f).exists() )
QFile::remove(f);
Expand All @@ -377,11 +377,11 @@ void Utils::saveLyrics(QString artist, QString song, QString lyrics)
void Utils::saveLyrics2(QString artist, QString song, QString lyrics)
{
QDir d;
d.mkdir(QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + "/lyrics");
d.mkdir(QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/lyrics");

QString art = cleanItem(artist);
QString sng = cleanItem(song);
QString f = QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + "/lyrics/"+art+"-"+sng+".txt";
QString f = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/lyrics/"+art+"-"+sng+".txt";

if ( QFileInfo(f).exists() )
QFile::remove(f);
Expand Down Expand Up @@ -421,10 +421,10 @@ void Utils::Finished(int requestId, bool)
QImage img;
img.loadFromData(bytes);

QString th1 = QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + "/media-art/album-" + doubleHash(albumArtArtist, albumArtAlbum) + ".jpeg";
if ( QFileInfo(QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + "/media-art/preview.jpeg").exists() )
QString th1 = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/media-art/album-" + doubleHash(albumArtArtist, albumArtAlbum) + ".jpeg";
if ( QFileInfo(QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/media-art/preview.jpeg").exists() )
removePreview();
img.save(QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + "/media-art/preview.jpeg");
img.save(QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/media-art/preview.jpeg");
downloadedAlbumArt = th1;
emit coverDownloaded();
}
Expand All @@ -434,7 +434,7 @@ void Utils::Finished(int requestId, bool)

void Utils::removePreview()
{
QFile f(QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + "/media-art/preview.jpeg");
QFile f(QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/media-art/preview.jpeg");
f.remove();
}

Expand Down
Loading

0 comments on commit db827fe

Please sign in to comment.