Commit 142541b5 authored by Linus Jahn's avatar Linus Jahn

back-end: More code clean up / refactoring

Thanks to segal (XMPP Sprint 2019 Berlin) for looking at some of Kaidan's
code and suggesting improvements.
parent 330ca8ee
Pipeline #3309 passed with stages
in 13 minutes and 47 seconds
......@@ -92,7 +92,6 @@ AvatarFileStorage::AddAvatarResult AvatarFileStorage::addAvatar(const QString &j
if (oldHash != result.hash) {
jidAvatarMap[jid] = result.hash;
result.hasChanged = true;
emit avatarIdsChanged();
// delete the avatar if it isn't used anymore
cleanUp(oldHash);
......@@ -100,11 +99,9 @@ AvatarFileStorage::AddAvatarResult AvatarFileStorage::addAvatar(const QString &j
// abort if the avatar with this hash is already saved
// only update GUI, if avatar really has changed
bool hasAvatar = hasAvatarHash(result.hash);
if (hasAvatar && result.hasChanged) {
emit avatarIdsChanged();
return result;
} else if (hasAvatar) {
if (hasAvatarHash(result.hash)) {
if (result.hasChanged)
emit avatarIdsChanged();
return result;
}
......
......@@ -74,13 +74,11 @@ void DownloadManager::startDownload(const QString msgId, const QString url)
}
// we want to save files to 'Downloads/Kaidan/'
QString filePath = QStandardPaths::writableLocation(
QStandardPaths::DownloadLocation);
filePath += "/";
filePath += APPLICATION_DISPLAY_NAME;
filePath += "/";
QString dirPath =
QStandardPaths::writableLocation(QStandardPaths::DownloadLocation)
+ QDir::separator() + APPLICATION_DISPLAY_NAME + QDir::separator();
DownloadJob *dl = new DownloadJob(msgId, QUrl(url), filePath, netMngr,
DownloadJob *dl = new DownloadJob(msgId, QUrl(url), dirPath, netMngr,
transferCache, kaidan);
dl->moveToThread(thread);
downloads[msgId] = dl;
......@@ -129,16 +127,15 @@ void DownloadJob::startDownload()
file.setFileName(filePath + source.fileName());
int counter = 1;
while (file.exists()) {
QString newName = filePath + source.fileName() + "-"
+ QString::number(counter);
file.setFileName(newName);
counter++;
file.setFileName(filePath + source.fileName() + "-"
+ QString::number(counter++));
}
if (!file.open(QIODevice::WriteOnly)) {
qWarning() << "Could not open file for writing";
emit kaidan->passiveNotificationRequested("Could not open file for "
"writing");
qWarning() << "Could not open file for writing:"
<< file.errorString();
emit kaidan->passiveNotificationRequested(
tr("Could not save file: %1").arg(file.errorString()));
emit failed();
return;
}
......@@ -157,9 +154,11 @@ void DownloadJob::startDownload()
emit finished();
});
connect(reply, QOverload<QNetworkReply::NetworkError>::of(&QNetworkReply::error),
[this] () {
[this, reply] () {
emit transferCache->removeJobRequested(msgId);
emit kaidan->passiveNotificationRequested(tr("Download failed."));
qWarning() << "Couldn't download file:" << reply->errorString();
emit kaidan->passiveNotificationRequested(
tr("Download failed: %1").arg(reply->errorString()));
emit finished();
});
connect(reply, &QNetworkReply::readyRead, this, [this, reply](){
......
......@@ -153,9 +153,10 @@ void RosterManager::handleMessage(const QXmppMessage &msg)
// TODO: Check if it's a carbon message (will need QXmpp v0.10)
// msg.from() can be our JID, if it's a carbon/forward from another client
bool sentByMe = QXmppUtils::jidToBareJid(msg.from()) == client->configuration().jidBare();
QString fromJid = QXmppUtils::jidToBareJid(msg.from());
bool sentByMe = fromJid == client->configuration().jidBare();
QString contactJid = sentByMe ? QXmppUtils::jidToBareJid(msg.to())
: QXmppUtils::jidToBareJid(msg.from());
: fromJid;
// update last message of the contact
emit model->setLastMessageRequested(contactJid, msg.body());
......@@ -168,6 +169,6 @@ void RosterManager::handleMessage(const QXmppMessage &msg)
if (sentByMe)
emit model->setUnreadMessageCountRequested(contactJid, 0);
// update unread message counter, if chat is not active
else if (!sentByMe && chatPartner != contactJid)
else if (chatPartner != contactJid)
emit model->newUnreadMessageRequested(contactJid);
}
......@@ -51,7 +51,8 @@ void TransferJob::setBytesSent(qint64 bytesSent)
return;
this->bytesSent = bytesSent;
emit bytesSentChanged();
setProgress((qreal) bytesSent / (qreal) bytesTotal);
if (bytesTotal != 0)
setProgress((qreal) bytesSent / (qreal) bytesTotal);
}
void TransferJob::setBytesTotal(qint64 bytesTotal)
......@@ -60,7 +61,8 @@ void TransferJob::setBytesTotal(qint64 bytesTotal)
return;
this->bytesTotal = bytesTotal;
emit bytesTotalChanged();
setProgress((qreal) bytesSent / (qreal) bytesTotal);
if (bytesTotal != 0)
setProgress((qreal) bytesSent / (qreal) bytesTotal);
}
TransferCache::TransferCache(QObject *parent)
......
......@@ -339,7 +339,7 @@ void QXmppUploadManager::handleRequestError(const QXmppHttpUploadRequestIq &requ
void QXmppUploadManager::handleUploadFinished()
{
m_runningJobs--;
auto *upload = (QXmppHttpUpload*) sender();
auto *upload = static_cast<QXmppHttpUpload*>(sender());
if (upload) {
m_uploads.removeAll(upload);
emit uploadSucceeded(upload);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment