Verified Commit b16e9d5e authored by Linus Jahn's avatar Linus Jahn Committed by Linus Jahn

UploadHandler: Add message to database before uploading

To track the progress of the file upload in the UI, it is required that the
message is already in the locale database, before the file was uploaded.
parent 72457cd5
......@@ -44,7 +44,6 @@
// Kaidan
#include "MessageModel.h"
#include "Notifications.h"
#include "Enums.h"
QDateTime stringToQDateTime(std::string stamp)
{
......@@ -203,19 +202,19 @@ void MessageHandler::handleReceiptMessage(const gloox::Message *message,
}
void MessageHandler::sendMessage(QString toJid, QString body)
{
const std::string id = client->getID();
addMessageToDb(toJid, body, QString::fromStdString(id), MessageType::MessageText);
sendOnlyMessage(toJid, body, id);
}
void MessageHandler::sendOnlyMessage(QString &toJid, QString &body, const std::string &id)
{
// create a new message
gloox::Message message(gloox::Message::Chat, gloox::JID(toJid.toStdString()),
body.toStdString());
// add the message to the database
const QString timestamp = QDateTime::currentDateTime().toUTC().toString(Qt::ISODate);
const QString id = QString::fromStdString(message.id());
const QString fromJid = QString::fromStdString(client->jid().bare());
emit messageModel->addMessageRequested(
fromJid, toJid, timestamp, body, id, true, MessageType::MessageText
);
message.setID(id);
// XEP-0184: Message Delivery Receipts
// request a delivery receipt from the other client
......@@ -224,6 +223,18 @@ void MessageHandler::sendMessage(QString toJid, QString body)
// send the message
client->send(message);
}
void MessageHandler::addMessageToDb(QString &toJid, QString &body, QString id,
MessageType type)
{
// add the message to the database
const QString timestamp = QDateTime::currentDateTime().toUTC().toString(Qt::ISODate);
const QString fromJid = QString::fromStdString(client->jid().bare());
emit messageModel->addMessageRequested(
fromJid, toJid, timestamp, body, id, true, type
);
// update the last message for this contact
emit rosterModel->setLastMessageRequested(toJid, body);
......
......@@ -39,6 +39,7 @@
// Kaidan
#include "MessageModel.h"
#include "RosterModel.h"
#include "Enums.h"
namespace gloox {
class Client;
......@@ -46,6 +47,8 @@ namespace gloox {
class MessageSession;
}
using namespace Enums;
class MessageHandler : public QObject, public gloox::MessageHandler
{
Q_OBJECT
......@@ -67,6 +70,25 @@ public:
void newUnreadMessageForJid(const QString &jid);
void resetUnreadMessagesForJid(const QString &jid);
/**
* (Only) sends the message to the recipient
*
* @param toJid Recipient (bare JID)
* @param body Message body / text of the message
* @param id Custom id used for the message
*/
void sendOnlyMessage(QString &toJid, QString &body, const std::string &id);
/**
* Saves a message to the database
*
* @param toJid Recipient (bare JID)
* @param body Message body / text of the message
* @param id Custom id used for the message
* @param type Custom message type (useful for media sharing / file uploads)
*/
void addMessageToDb(QString &toJid, QString &body, QString id, MessageType type);
public slots:
void setChatPartner(QString chatPartner);
void sendMessage(QString toJid, QString body);
......
......@@ -41,6 +41,8 @@
#include "gloox-extensions/hash.h"
// Qt
#include <QMimeDatabase>
#include <QMimeType>
#include <QDateTime>
#include <QDebug>
UploadHandler::UploadHandler(gloox::Client *client, MessageHandler *msgHandler,
......@@ -57,10 +59,11 @@ void UploadHandler::uploadFile(QString jid, QString filePath)
{
// get MIME-type
QMimeDatabase mimeDb;
QString contentType = mimeDb.mimeTypeForFile(filePath).name();
QMimeType mimeType = mimeDb.mimeTypeForFile(filePath);
QString mimeTypeStr = mimeType.name();
int id = manager->uploadFile(filePath.toStdString(), true,
contentType.toStdString());
mimeTypeStr.toStdString());
if (id < 0) {
// failure
......@@ -69,8 +72,15 @@ void UploadHandler::uploadFile(QString jid, QString filePath)
// save for later processing/sending
MediaSharingMeta meta;
meta.jid = jid;
meta.msgId = client->getID();
mediaShares[id] = meta;
QString body = "";
msgHandler->addMessageToDb(
jid, body, QString::fromStdString(meta.msgId),
getMessageType(mimeType)
);
}
}
......@@ -89,7 +99,8 @@ void UploadHandler::handleUploadFinished(int id, std::string &name,
qDebug() << "[client] A file upload has finished.";
// TODO: send SIMS message (and optionally also create jingle object)
// for now only send a normal text message with the get URL.
msgHandler->sendMessage(mediaShares[id].jid, QString::fromStdString(getUrl));
QString body = QString::fromStdString(getUrl);
msgHandler->sendOnlyMessage(mediaShares[id].jid, body, mediaShares[id].msgId);
// the media meta isn't needed anymore, so delete it
mediaShares.remove(id);
......@@ -112,3 +123,21 @@ void UploadHandler::handleUploadServiceAdded(const gloox::JID &jid,
void UploadHandler::handleFileSizeLimitChanged(unsigned long maxFileSize)
{
}
MessageType UploadHandler::getMessageType(QMimeType& type)
{
if (type.inherits("image/jpeg") || type.inherits("image/png") ||
type.inherits("image/gif"))
return MessageType::MessageImage;
else if (type.inherits("audio/flac") || type.inherits("audio/mp4") ||
type.inherits("audio/ogg") || type.inherits("audio/wav") ||
type.inherits("audio/mpeg") || type.inherits("audio/webm"))
return MessageType::MessageAudio;
else if (type.inherits("video/mpeg") || type.inherits("video/x-msvideo") ||
type.inherits("video/quicktime") || type.inherits("video/mp4") ||
type.inherits("video/x-matroska"))
return MessageType::MessageVideo;
else if (type.inherits("text/plain"))
return MessageType::MessageDocument;
return MessageType::MessageFile;
}
......@@ -31,6 +31,8 @@
#ifndef UPLOADHANDLER_H
#define UPLOADHANDLER_H
// Kaidan
#include "Enums.h"
// gloox
#include "gloox-extensions/httpuploadhandler.h"
#include "gloox-extensions/httpuploadmanager.h"
......@@ -42,9 +44,12 @@ namespace gloox {
class Client;
}
class QMimeType;
class MessageHandler;
class QtHttpUploader;
using namespace Enums;
/**
* @class UploadHandler Class for handling and starting HTTP File Uploads
*/
......@@ -64,6 +69,12 @@ public:
return manager;
}
signals:
/**
* Connect to it to be notified about progress on uploads
*/
void uploadProgressMade(QString msgId, unsigned long sent, unsigned long total);
public slots:
/**
* Starts uploading a file
......@@ -128,13 +139,16 @@ protected:
const std::string &stamp = gloox::EmptyString);
private:
MessageType getMessageType(QMimeType &type);
struct MediaSharingMeta {
QString jid;
std::string msgId;
};
gloox::Client *client;
gloox::HttpUploadManager *manager;
MessageHandler* msgHandler;
MessageHandler *msgHandler;
QtHttpUploader *uploader;
QMap<int, MediaSharingMeta> mediaShares;
......
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