Skip to content

Commit

Permalink
cleanup part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
jsmolka committed May 27, 2018
1 parent e307719 commit a6e83ed
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 33 deletions.
2 changes: 1 addition & 1 deletion egg-player.pri
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
RC_ICONS = resource/images/egg/egg.ico

VERSION = 0.1.3.13
VERSION = 0.1.3.14

QMAKE_TARGET = Egg Player
QMAKE_TARGET_PRODUCT = Egg Player
Expand Down
2 changes: 1 addition & 1 deletion src/core/audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Audio::Audio(const QString &path) :
}

/*
* Constructor. Used to read audios from the sql database.
* Constructor. Used to create an audio object without parsing a file.
*
* :param path: path
* :param title: title
Expand Down
70 changes: 46 additions & 24 deletions src/core/cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Cache::~Cache()

/*
* Loads an audio file from the cache. If it does not exist a nullptr will be
* returned instead.
* returned.
*
* :param path: path
* :return: audio, nullptr at failure
Expand All @@ -35,7 +35,10 @@ Audio * Cache::load(const QString &path)
Audio *audio = nullptr;

QSqlQuery query(db());
query.prepare("SELECT * FROM audios WHERE path = :path");
query.prepare(
"SELECT * FROM audios "
"WHERE path = :path"
);
query.bindValue(":path", path);

if (!query.exec())
Expand Down Expand Up @@ -100,7 +103,8 @@ bool Cache::insertAudio(Audio *audio)

/*
* Inserts an audio cover into the cache. This assumes that the audio already
* has an entry in the audio table.
* inside the audio table because it will also set the audios cover id to the
* covers id.
*
* :param audio: audio
* :param size: cover size, default 200
Expand Down Expand Up @@ -133,8 +137,8 @@ bool Cache::insertCover(Audio *audio, int size)

/*
* Checks if database contains audio. Also sets the cover id for later use. If
* an audio object has a valid cover id it definitely exists already because it
* only gets set inside the cache.
* the audios cover id is unequal to -1, the audio exist already because the
* cover id only exists inside the cache.
*
* :param audio: audio
* :return: contains
Expand All @@ -145,7 +149,10 @@ bool Cache::contains(Audio *audio)
return true;

QSqlQuery query(db());
query.prepare("SELECT coverid FROM audios WHERE path = :path");
query.prepare(
"SELECT coverid FROM audios "
"WHERE path = :path"
);
query.bindValue(":path", audio->path());

if (!query.exec())
Expand All @@ -167,7 +174,7 @@ bool Cache::contains(Audio *audio)
}

/*
* Retrieves cover from database. If the cover has already been loaded
* Retrieves a cover from the database. If the cover has already been loaded
* previously, a cached version will be used.
*
* :param audio: audio
Expand Down Expand Up @@ -210,8 +217,9 @@ QPixmap Cache::cover(Audio *audio, int size)
}

/*
* Gets the database name for the current thread. This is because connections
* must be unique for every thread as stated in the documentation:
* Gets the database name for the current thread by converting its pointer
* address to a number. This is because connections must be unique for every
* thread as stated in the documentation:
*
* "A connection can only be used from within the thread that created it. Moving
* connections between threads or creating queries from a different thread is
Expand Down Expand Up @@ -245,15 +253,15 @@ QSqlDatabase Cache::db()
*/
void Cache::createCovers()
{
QString createCovers =
QString create =
"CREATE TABLE IF NOT EXISTS covers("
" id INTEGER PRIMARY KEY,"
" len INTEGER,"
" cover BLOB"
")";

QSqlQuery query(db());
if (!query.exec(createCovers))
if (!query.exec(create))
handleError((query));
}

Expand All @@ -262,7 +270,7 @@ void Cache::createCovers()
*/
void Cache::createAudios()
{
QString createAudios =
QString create =
"CREATE TABLE IF NOT EXISTS audios("
" path TEXT PRIMARY KEY,"
" title TEXT,"
Expand All @@ -276,13 +284,13 @@ void Cache::createAudios()
")";

QSqlQuery query(db());
if (!query.exec(createAudios))
if (!query.exec(create))
handleError(query);
}

/*
* Either gets the cover id or inserts it into the covers tables and returns its
* id.
* Either gets the cover id or inserts the cover into the covers tables and
* returns its id.
*
* :param cover: cover
* :return: id, -1 at failure
Expand All @@ -293,23 +301,30 @@ int Cache::getOrInsertCover(const QPixmap &cover)

int id = coverId(bytes);
if (id == -1)
id = insertCover(bytes);
id = insertByteCover(bytes);

return id;
}

/*
* Inserts cover into database. Assumes that the cover does not exist already.
* Inserts the cover into the database. Assumes that the cover does not exist
* already.
*
* :param bytes: byte array cover
* :return: id, -1 at failure
*/
int Cache::insertCover(const QByteArray &bytes)
int Cache::insertByteCover(const QByteArray &bytes)
{
int id = lastCoverId() + 1;

QSqlQuery query(db());
query.prepare("INSERT INTO covers VALUES (:id, :len, :cover)");
query.prepare(
"INSERT INTO covers VALUES ("
" :id,"
" :len,"
" :cover"
")"
);
query.bindValue(":id", id);
query.bindValue(":len", bytes.length());
query.bindValue(":cover", bytes);
Expand All @@ -323,8 +338,9 @@ int Cache::insertCover(const QByteArray &bytes)
}

/*
* Returns cover id for byte array cover. For performance reasons it first tries
* to query based on the byte array length and then by blob comparison.
* Returns the cover id for a byte array cover. For performance reasons it first
* tries to query the cover based on the byte array length and then, if it gets
* multiple results, by blob comparison.
*
* :param bytes: byte array cover
* :return: id, -1 at failure
Expand Down Expand Up @@ -369,7 +385,10 @@ int Cache::queryCoverIdByLength(int length)
int id = -1;

QSqlQuery query(db());
query.prepare("SELECT id FROM covers WHERE len = :len");
query.prepare(
"SELECT id FROM covers "
"WHERE len = :len"
);
query.bindValue(":len", length);

if (!query.exec())
Expand All @@ -393,7 +412,10 @@ int Cache::queryCoverIdByLength(int length)
int Cache::queryCoverIdByBlob(const QByteArray &bytes)
{
QSqlQuery query(db());
query.prepare("SELECT id FROM covers WHERE cover = :cover");
query.prepare(
"SELECT id FROM covers "
"WHERE cover = :cover"
);
query.bindValue(":cover", bytes);

if (!query.exec())
Expand All @@ -413,7 +435,7 @@ int Cache::queryCoverIdByBlob(const QByteArray &bytes)
void Cache::handleError(const QSqlQuery &query)
{
QSqlError error = query.lastError();
if (error.isValid())
if (error.type() != QSqlError::NoError)
{
Logger::log(
"Cache: Querying \"%1\" failed with error \"%2\"",
Expand Down
2 changes: 1 addition & 1 deletion src/core/cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Cache
void createAudios();

int getOrInsertCover(const QPixmap &cover);
int insertCover(const QByteArray &bytes);
int insertByteCover(const QByteArray &bytes);

int coverId(const QByteArray &bytes);
int lastCoverId();
Expand Down
16 changes: 13 additions & 3 deletions src/core/threading/abstractthread.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
#include "abstractthread.hpp"

/*
* Constructor. It is advised to use classes derived from this one in
* combination with a thread pool because it manages their life time.
* Constructor.
*
* :param parent: parent, default nullptr
*/
AbstractThread::AbstractThread(QObject *parent) :
QThread(parent),
m_abort(false)
{

connect(this, SIGNAL(started()), this, SLOT(onStarted()));
}

/*
Expand All @@ -32,6 +31,14 @@ bool AbstractThread::isAbort() const
return m_abort;
}

/*
* Resets the abort property at every start.
*/
void AbstractThread::onStarted()
{
m_abort = false;
}

/*
* Aborts the thread. The abort property gets sets to true which indicates that
* the thread should be stopped. The abort property need to be checked inside
Expand All @@ -42,5 +49,8 @@ void AbstractThread::abort()
m_abort = true;

if (!wait(5000))
{
Logger::log("AbstractThread: Could not abort within 5 seconds");
terminate();
}
}
3 changes: 3 additions & 0 deletions src/core/threading/abstractthread.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class AbstractThread : public QThread
public slots:
void abort();

private slots:
void onStarted();

private:
bool m_abort;
};
Expand Down
3 changes: 2 additions & 1 deletion src/core/threading/audioloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ void AudioLoader::run()
if (!audio)
{
audio = new Audio(file);
cache.insertAudio(audio);
if (audio->isValid())
cache.insertAudio(audio);
}

if (audio->isValid())
Expand Down
10 changes: 8 additions & 2 deletions src/utils/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ QPixmap Util::cover(int size)
*/
QPixmap Util::resize(const QPixmap &pixmap, int size, bool fast)
{
return pixmap.scaled(size, size, Qt::KeepAspectRatio, fast ? Qt::FastTransformation : Qt::SmoothTransformation);
if (pixmap.height() == size && pixmap.width() == size)
return pixmap;
else
return pixmap.scaled(size, size, Qt::KeepAspectRatio, fast ? Qt::FastTransformation : Qt::SmoothTransformation);
}

/*
Expand All @@ -63,7 +66,10 @@ QPixmap Util::resize(const QPixmap &pixmap, int size, bool fast)
*/
QImage Util::resize(const QImage &image, int size, bool fast)
{
return image.scaled(size, size, Qt::KeepAspectRatio, fast ? Qt::FastTransformation : Qt::SmoothTransformation);
if (image.height() == size && image.width() == size)
return image;
else
return image.scaled(size, size, Qt::KeepAspectRatio, fast ? Qt::FastTransformation : Qt::SmoothTransformation);
}

/*
Expand Down

0 comments on commit a6e83ed

Please sign in to comment.