localHistoryStorage #5

Merged
blue merged 6 commits from localHistoryStorage into master 2019-05-15 17:39:54 +00:00
19 changed files with 840 additions and 58 deletions

View File

@ -1,6 +1,7 @@
#include "account.h" #include "account.h"
#include <qxmpp/QXmppMessage.h> #include <qxmpp/QXmppMessage.h>
#include <QDateTime> #include <QDateTime>
#include <QTimer>
using namespace Core; using namespace Core;
@ -25,6 +26,7 @@ Account::Account(const QString& p_login, const QString& p_server, const QString&
QObject::connect(&client, SIGNAL(disconnected()), this, SLOT(onClientDisconnected())); QObject::connect(&client, SIGNAL(disconnected()), this, SLOT(onClientDisconnected()));
QObject::connect(&client, SIGNAL(presenceReceived(const QXmppPresence&)), this, SLOT(onPresenceReceived(const QXmppPresence&))); QObject::connect(&client, SIGNAL(presenceReceived(const QXmppPresence&)), this, SLOT(onPresenceReceived(const QXmppPresence&)));
QObject::connect(&client, SIGNAL(messageReceived(const QXmppMessage&)), this, SLOT(onMessageReceived(const QXmppMessage&))); QObject::connect(&client, SIGNAL(messageReceived(const QXmppMessage&)), this, SLOT(onMessageReceived(const QXmppMessage&)));
QObject::connect(&client, SIGNAL(error(QXmppClient::Error)), this, SLOT(onClientError(QXmppClient::Error)));
QXmppRosterManager& rm = client.rosterManager(); QXmppRosterManager& rm = client.rosterManager();
@ -212,6 +214,8 @@ void Core::Account::addedAccount(const QString& jid)
QObject::connect(contact, SIGNAL(nameChanged(const QString&)), this, SLOT(onContactNameChanged(const QString&))); QObject::connect(contact, SIGNAL(nameChanged(const QString&)), this, SLOT(onContactNameChanged(const QString&)));
QObject::connect(contact, SIGNAL(subscriptionStateChanged(Shared::SubscriptionState)), QObject::connect(contact, SIGNAL(subscriptionStateChanged(Shared::SubscriptionState)),
this, SLOT(onContactSubscriptionStateChanged(Shared::SubscriptionState))); this, SLOT(onContactSubscriptionStateChanged(Shared::SubscriptionState)));
QObject::connect(contact, SIGNAL(needHistory(const QString&, const QString&)), this, SLOT(onContactNeedHistory(const QString&, const QString&)));
QObject::connect(contact, SIGNAL(historyResponse(const std::list<Shared::Message>&)), this, SLOT(onContactHistoryResponse(const std::list<Shared::Message>&)));
} }
} }
@ -368,6 +372,10 @@ void Core::Account::sendMessage(const Shared::Message& data)
QXmppMessage msg(data.getFrom(), data.getTo(), data.getBody(), data.getThread()); QXmppMessage msg(data.getFrom(), data.getTo(), data.getBody(), data.getThread());
msg.setId(data.getId()); msg.setId(data.getId());
msg.setType(static_cast<QXmppMessage::Type>(data.getType())); //it is safe here, my type is compatible msg.setType(static_cast<QXmppMessage::Type>(data.getType())); //it is safe here, my type is compatible
std::map<QString, Contact*>::const_iterator itr = contacts.find(data.getPenPalJid());
itr->second->appendMessageToArchive(data);
client.sendPacket(msg); client.sendPacket(msg);
} else { } else {
qDebug() << "An attempt to send message with not connected account " << name << ", skipping"; qDebug() << "An attempt to send message with not connected account " << name << ", skipping";
@ -386,27 +394,14 @@ void Core::Account::onCarbonMessageSent(const QXmppMessage& msg)
bool Core::Account::handleChatMessage(const QXmppMessage& msg, bool outgoing, bool forwarded, bool guessing) bool Core::Account::handleChatMessage(const QXmppMessage& msg, bool outgoing, bool forwarded, bool guessing)
{ {
QString body(msg.body()); const QString& body(msg.body());
if (body.size() != 0) { if (body.size() != 0) {
QString id(msg.id()); const QString& id(msg.id());
QDateTime time(msg.stamp());
Shared::Message sMsg(Shared::Message::chat); Shared::Message sMsg(Shared::Message::chat);
sMsg.setId(id); initializeMessage(sMsg, msg, outgoing, forwarded, guessing);
sMsg.setFrom(msg.from()); std::map<QString, Contact*>::const_iterator itr = contacts.find(sMsg.getPenPalJid());
sMsg.setTo(msg.to()); itr->second->appendMessageToArchive(sMsg);
sMsg.setBody(body);
sMsg.setForwarded(forwarded);
if (guessing) {
if (sMsg.getFromJid() == getLogin() + "@" + getServer()) {
outgoing = true;
} else {
outgoing = false;
}
}
sMsg.setOutgoing(outgoing);
if (time.isValid()) {
sMsg.setTime(time);
}
emit message(sMsg); emit message(sMsg);
if (!forwarded && !outgoing) { if (!forwarded && !outgoing) {
@ -422,32 +417,108 @@ bool Core::Account::handleChatMessage(const QXmppMessage& msg, bool outgoing, bo
return false; return false;
} }
void Core::Account::onMamMessageReceived(const QString& bareJid, const QXmppMessage& msg) void Core::Account::initializeMessage(Shared::Message& target, const QXmppMessage& source, bool outgoing, bool forwarded, bool guessing) const
{ {
handleChatMessage(msg, false, true, true); const QDateTime& time(source.stamp());
target.setId(source.id());
target.setFrom(source.from());
target.setTo(source.to());
target.setBody(source.body());
target.setForwarded(forwarded);
if (guessing) {
if (target.getFromJid() == getLogin() + "@" + getServer()) {
outgoing = true;
} else {
outgoing = false;
}
}
target.setOutgoing(outgoing);
if (time.isValid()) {
target.setTime(time);
} else {
target.setCurrentTime();
}
} }
void Core::Account::requestAchive(const QString& jid) void Core::Account::onMamMessageReceived(const QString& queryId, const QXmppMessage& msg)
{ {
std::map<QString, QString>::const_iterator itr = achiveQueries.find(queryId);
QString jid = itr->second;
std::map<QString, Contact*>::const_iterator citr = contacts.find(jid);
if (citr != contacts.end()) {
Contact* cnt = citr->second;
if (msg.id().size() > 0 && msg.body().size() > 0) {
Shared::Message sMsg(Shared::Message::chat);
initializeMessage(sMsg, msg, false, true, true);
cnt->addMessageToArchive(sMsg);
}
}
//handleChatMessage(msg, false, true, true);
}
void Core::Account::requestArchive(const QString& jid, int count, const QString& before)
{
qDebug() << "An archive request for " << jid << ", before " << before;
std::map<QString, Contact*>::const_iterator itr = contacts.find(jid);
if (itr == contacts.end()) {
qDebug() << "An attempt to request archive for" << jid << "in account" << name << ", but the contact with such id wasn't found, skipping";
return;
}
Contact* contact = itr->second;
if (contact->getArchiveState() == Contact::empty && before.size() == 0) {
QXmppMessage msg(getFullJid(), jid, "", "");
QString last = Shared::generateUUID();
msg.setId(last);
msg.setType(QXmppMessage::Chat);
msg.setState(QXmppMessage::Active);
client.sendPacket(msg);
QTimer* timer = new QTimer;
QObject::connect(timer, &QTimer::timeout, [timer, contact, count, last](){
contact->requestFromEmpty(count, last);
timer->deleteLater();
});
timer->setSingleShot(true);
timer->start(1000);
} else {
contact->requestHistory(count, before);
}
}
void Core::Account::onContactNeedHistory(const QString& before, const QString& after)
{
Contact* contact = static_cast<Contact*>(sender());
QXmppResultSetQuery query; QXmppResultSetQuery query;
query.setMax(100); query.setMax(100);
QDateTime from = QDateTime::currentDateTime().addDays(-7); if (before.size() > 0) {
query.setBefore(before);
}
if (after.size() > 0) {
query.setAfter(after);
}
QString q = am->retrieveArchivedMessages("", "", jid, from, QDateTime(), query); qDebug() << "Remote query from\"" << after << "\", to" << before;
achiveQueries.insert(std::make_pair(q, jid));
QString q = am->retrieveArchivedMessages("", "", contact->jid, QDateTime(), QDateTime(), query);
achiveQueries.insert(std::make_pair(q, contact->jid));
} }
void Core::Account::onMamResultsReceived(const QString& queryId, const QXmppResultSetReply& resultSetReply, bool complete) void Core::Account::onMamResultsReceived(const QString& queryId, const QXmppResultSetReply& resultSetReply, bool complete)
{ {
std::map<QString, QString>::const_iterator itr = achiveQueries.find(queryId); std::map<QString, QString>::const_iterator itr = achiveQueries.find(queryId);
QString jid = itr->second; QString jid = itr->second;
achiveQueries.erase(itr); achiveQueries.erase(itr);
if (!complete) { std::map<QString, Contact*>::const_iterator citr = contacts.find(jid);
QXmppResultSetQuery q; if (citr != contacts.end()) {
q.setAfter(resultSetReply.last()); Contact* cnt = citr->second;
q.setMax(100);
QString nQ = am->retrieveArchivedMessages("", "", jid, QDateTime::currentDateTime().addDays(-7), QDateTime(), q); qDebug() << "Flushing messages for" << jid;
achiveQueries.insert(std::make_pair(nQ, jid)); cnt->flushMessagesToArchive(complete, resultSetReply.first(), resultSetReply.last());
} }
} }
@ -534,3 +605,29 @@ Shared::SubscriptionState Core::Account::castSubscriptionState(QXmppRosterIq::It
} }
return state; return state;
} }
void Core::Account::onContactHistoryResponse(const std::list<Shared::Message>& list)
{
Contact* contact = static_cast<Contact*>(sender());
qDebug() << "Collected history for contact " << contact->jid << list.size() << "elements";
emit responseArchive(contact->jid, list);
}
void Core::Account::onClientError(QXmppClient::Error err)
{
switch (err) {
case QXmppClient::SocketError:
qDebug() << "Client socket error" << client.socketErrorString();
break;
case QXmppClient::XmppStreamError:
qDebug() << "Client stream error" << client.socketErrorString();
break;
case QXmppClient::KeepAliveError:
qDebug() << "Client keep alive error";
break;
}
//onClientDisconnected();
}

View File

@ -41,7 +41,7 @@ public:
void setAvailability(Shared::Availability avail); void setAvailability(Shared::Availability avail);
QString getFullJid() const; QString getFullJid() const;
void sendMessage(const Shared::Message& data); void sendMessage(const Shared::Message& data);
void requestAchive(const QString& jid); void requestArchive(const QString& jid, int count, const QString& before);
signals: signals:
void connectionStateChanged(int); void connectionStateChanged(int);
@ -55,6 +55,7 @@ signals:
void addPresence(const QString& jid, const QString& name, const QMap<QString, QVariant>& data); void addPresence(const QString& jid, const QString& name, const QMap<QString, QVariant>& data);
void removePresence(const QString& jid, const QString& name); void removePresence(const QString& jid, const QString& name);
void message(const Shared::Message& data); void message(const Shared::Message& data);
void responseArchive(const QString& jid, const std::list<Shared::Message>& list);
private: private:
QString name; QString name;
@ -71,6 +72,7 @@ private:
private slots: private slots:
void onClientConnected(); void onClientConnected();
void onClientDisconnected(); void onClientDisconnected();
void onClientError(QXmppClient::Error err);
void onRosterReceived(); void onRosterReceived();
void onRosterItemAdded(const QString& bareJid); void onRosterItemAdded(const QString& bareJid);
@ -91,12 +93,15 @@ private slots:
void onContactGroupRemoved(const QString& group); void onContactGroupRemoved(const QString& group);
void onContactNameChanged(const QString& name); void onContactNameChanged(const QString& name);
void onContactSubscriptionStateChanged(Shared::SubscriptionState state); void onContactSubscriptionStateChanged(Shared::SubscriptionState state);
void onContactHistoryResponse(const std::list<Shared::Message>& list);
void onContactNeedHistory(const QString& before, const QString& after);
private: private:
void addedAccount(const QString &bareJid); void addedAccount(const QString &bareJid);
bool handleChatMessage(const QXmppMessage& msg, bool outgoing = false, bool forwarded = false, bool guessing = false); bool handleChatMessage(const QXmppMessage& msg, bool outgoing = false, bool forwarded = false, bool guessing = false);
void addToGroup(const QString& jid, const QString& group); void addToGroup(const QString& jid, const QString& group);
void removeFromGroup(const QString& jid, const QString& group); void removeFromGroup(const QString& jid, const QString& group);
void initializeMessage(Shared::Message& target, const QXmppMessage& source, bool outgoing = false, bool forwarded = false, bool guessing = false) const;
Shared::SubscriptionState castSubscriptionState(QXmppRosterIq::Item::SubscriptionType qs) const; Shared::SubscriptionState castSubscriptionState(QXmppRosterIq::Item::SubscriptionType qs) const;
}; };

View File

@ -28,6 +28,7 @@ Core::Archive::Archive(const QString& p_jid, QObject* parent):
QObject(parent), QObject(parent),
jid(p_jid), jid(p_jid),
opened(false), opened(false),
fromTheBeginning(false),
environment(), environment(),
main(), main(),
order() order()
@ -54,14 +55,16 @@ void Core::Archive::open(const QString& account)
} }
} }
mdb_env_set_maxdbs(environment, 2); mdb_env_set_maxdbs(environment, 3);
mdb_env_open(environment, path.toStdString().c_str(), 0, 0664); mdb_env_open(environment, path.toStdString().c_str(), 0, 0664);
MDB_txn *txn; MDB_txn *txn;
mdb_txn_begin(environment, NULL, 0, &txn); mdb_txn_begin(environment, NULL, 0, &txn);
mdb_dbi_open(txn, "main", MDB_CREATE, &main); mdb_dbi_open(txn, "main", MDB_CREATE, &main);
mdb_dbi_open(txn, "order", MDB_CREATE | MDB_INTEGERKEY, &order); mdb_dbi_open(txn, "order", MDB_CREATE | MDB_INTEGERKEY, &order);
mdb_dbi_open(txn, "order", MDB_CREATE, &stats);
mdb_txn_commit(txn); mdb_txn_commit(txn);
fromTheBeginning = _isFromTheBeginning();
opened = true; opened = true;
} }
} }
@ -69,6 +72,7 @@ void Core::Archive::open(const QString& account)
void Core::Archive::close() void Core::Archive::close()
{ {
if (opened) { if (opened) {
mdb_dbi_close(environment, stats);
mdb_dbi_close(environment, order); mdb_dbi_close(environment, order);
mdb_dbi_close(environment, main); mdb_dbi_close(environment, main);
mdb_env_close(environment); mdb_env_close(environment);
@ -76,7 +80,7 @@ void Core::Archive::close()
} }
} }
void Core::Archive::addElement(const Shared::Message& message) bool Core::Archive::addElement(const Shared::Message& message)
{ {
if (!opened) { if (!opened) {
throw Closed("addElement", jid.toStdString()); throw Closed("addElement", jid.toStdString());
@ -95,7 +99,7 @@ void Core::Archive::addElement(const Shared::Message& message)
MDB_txn *txn; MDB_txn *txn;
mdb_txn_begin(environment, NULL, 0, &txn); mdb_txn_begin(environment, NULL, 0, &txn);
int rc; int rc;
rc = mdb_put(txn, main, &lmdbKey, &lmdbData, 0); rc = mdb_put(txn, main, &lmdbKey, &lmdbData, MDB_NOOVERWRITE);
if (rc == 0) { if (rc == 0) {
MDB_val orderKey; MDB_val orderKey;
orderKey.mv_size = 8; orderKey.mv_size = 8;
@ -105,15 +109,19 @@ void Core::Archive::addElement(const Shared::Message& message)
if (rc) { if (rc) {
qDebug() << "An element couldn't be inserted into the index" << mdb_strerror(rc); qDebug() << "An element couldn't be inserted into the index" << mdb_strerror(rc);
mdb_txn_abort(txn); mdb_txn_abort(txn);
return false;
} else { } else {
rc = mdb_txn_commit(txn); rc = mdb_txn_commit(txn);
if (rc) { if (rc) {
qDebug() << "A transaction error: " << mdb_strerror(rc); qDebug() << "A transaction error: " << mdb_strerror(rc);
return false;
} }
return true;
} }
} else { } else {
qDebug() << "An element couldn't been added to the archive, skipping" << mdb_strerror(rc); qDebug() << "An element couldn't been added to the archive, skipping" << mdb_strerror(rc);
mdb_txn_abort(txn); mdb_txn_abort(txn);
return false;
} }
} }
@ -184,7 +192,7 @@ QString Core::Archive::newestId()
qDebug() << "Error geting newestId " << mdb_strerror(rc); qDebug() << "Error geting newestId " << mdb_strerror(rc);
mdb_cursor_close(cursor); mdb_cursor_close(cursor);
mdb_txn_abort(txn); mdb_txn_abort(txn);
throw new Empty(jid.toStdString()); throw Empty(jid.toStdString());
} else { } else {
std::string sId((char*)lmdbData.mv_data, lmdbData.mv_size); std::string sId((char*)lmdbData.mv_data, lmdbData.mv_size);
mdb_cursor_close(cursor); mdb_cursor_close(cursor);
@ -198,6 +206,65 @@ Shared::Message Core::Archive::oldest()
return getElement(oldestId()); return getElement(oldestId());
} }
unsigned int Core::Archive::addElements(const std::list<Shared::Message>& messages)
{
if (!opened) {
throw Closed("addElements", jid.toStdString());
}
int success = 0;
int rc = 0;
MDB_val lmdbKey, lmdbData;
MDB_txn *txn;
mdb_txn_begin(environment, NULL, 0, &txn);
std::list<Shared::Message>::const_iterator itr = messages.begin();
while (rc == 0 && itr != messages.end()) {
const Shared::Message& message = *itr;
QByteArray ba;
QDataStream ds(&ba, QIODevice::WriteOnly);
message.serialize(ds);
quint64 stamp = message.getTime().toMSecsSinceEpoch();
const std::string& id = message.getId().toStdString();
lmdbKey.mv_size = id.size();
lmdbKey.mv_data = (char*)id.c_str();
lmdbData.mv_size = ba.size();
lmdbData.mv_data = (uint8_t*)ba.data();
rc = mdb_put(txn, main, &lmdbKey, &lmdbData, MDB_NOOVERWRITE);
if (rc == 0) {
MDB_val orderKey;
orderKey.mv_size = 8;
orderKey.mv_data = (uint8_t*) &stamp;
rc = mdb_put(txn, order, &orderKey, &lmdbKey, 0);
if (rc) {
qDebug() << "An element couldn't be inserted into the index, aborting the transaction" << mdb_strerror(rc);
} else {
success++;
}
} else {
if (rc == MDB_KEYEXIST) {
rc = 0;
} else {
qDebug() << "An element couldn't been added to the archive, aborting the transaction" << mdb_strerror(rc);
}
}
itr++;
}
if (rc != 0) {
mdb_txn_abort(txn);
success = 0;
} else {
mdb_txn_commit(txn);
}
return success;
}
QString Core::Archive::oldestId() QString Core::Archive::oldestId()
{ {
if (!opened) { if (!opened) {
@ -215,7 +282,7 @@ QString Core::Archive::oldestId()
qDebug() << "Error geting oldestId " << mdb_strerror(rc); qDebug() << "Error geting oldestId " << mdb_strerror(rc);
mdb_cursor_close(cursor); mdb_cursor_close(cursor);
mdb_txn_abort(txn); mdb_txn_abort(txn);
throw new Empty(jid.toStdString()); throw Empty(jid.toStdString());
} else { } else {
std::string sId((char*)lmdbData.mv_data, lmdbData.mv_size); std::string sId((char*)lmdbData.mv_data, lmdbData.mv_size);
mdb_cursor_close(cursor); mdb_cursor_close(cursor);
@ -237,3 +304,202 @@ long unsigned int Core::Archive::size() const
mdb_txn_abort(txn); mdb_txn_abort(txn);
return stat.ms_entries; return stat.ms_entries;
} }
std::list<Shared::Message> Core::Archive::getBefore(int count, const QString& id)
{
if (!opened) {
throw Closed("getBefore", jid.toStdString());
}
std::list<Shared::Message> res;
MDB_cursor* cursor;
MDB_txn *txn;
MDB_val lmdbKey, lmdbData;
int rc;
rc = mdb_txn_begin(environment, NULL, MDB_RDONLY, &txn);
if (id == "") {
rc = mdb_cursor_open(txn, order, &cursor);
rc = mdb_cursor_get(cursor, &lmdbKey, &lmdbData, MDB_LAST);
if (rc) {
qDebug() << "Error getting before " << mdb_strerror(rc) << ", id:" << id;
mdb_cursor_close(cursor);
mdb_txn_abort(txn);
throw Empty(jid.toStdString());
}
} else {
std::string stdId(id.toStdString());
lmdbKey.mv_size = stdId.size();
lmdbKey.mv_data = (char*)stdId.c_str();
rc = mdb_get(txn, main, &lmdbKey, &lmdbData);
if (rc) {
qDebug() <<"Error getting before: no reference message" << mdb_strerror(rc) << ", id:" << id;
mdb_txn_abort(txn);
printKeys();
throw NotFound(stdId, jid.toStdString());
} else {
QByteArray ba((char*)lmdbData.mv_data, lmdbData.mv_size);
QDataStream ds(&ba, QIODevice::ReadOnly);
Shared::Message msg;
msg.deserialize(ds);
quint64 stamp = msg.getTime().toMSecsSinceEpoch();
lmdbKey.mv_data = (quint8*)&stamp;
lmdbKey.mv_size = 8;
rc = mdb_cursor_open(txn, order, &cursor);
rc = mdb_cursor_get(cursor, &lmdbKey, &lmdbData, MDB_SET);
if (rc) {
qDebug() << "Error getting before: couldn't set " << mdb_strerror(rc);
mdb_cursor_close(cursor);
mdb_txn_abort(txn);
throw NotFound(stdId, jid.toStdString());
} else {
rc = mdb_cursor_get(cursor, &lmdbKey, &lmdbData, MDB_PREV);
if (rc) {
qDebug() << "Error getting before, couldn't prev " << mdb_strerror(rc);
mdb_cursor_close(cursor);
mdb_txn_abort(txn);
throw NotFound(stdId, jid.toStdString());
}
}
}
}
do {
MDB_val dKey, dData;
dKey.mv_size = lmdbData.mv_size;
dKey.mv_data = lmdbData.mv_data;
rc = mdb_get(txn, main, &dKey, &dData);
if (rc) {
qDebug() <<"Get error: " << mdb_strerror(rc);
std::string sId((char*)lmdbData.mv_data, lmdbData.mv_size);
mdb_txn_abort(txn);
throw NotFound(sId, jid.toStdString());
} else {
QByteArray ba((char*)dData.mv_data, dData.mv_size);
QDataStream ds(&ba, QIODevice::ReadOnly);
res.emplace_back();
Shared::Message& msg = res.back();
msg.deserialize(ds);
}
--count;
} while (count > 0 && mdb_cursor_get(cursor, &lmdbKey, &lmdbData, MDB_PREV) == 0);
mdb_cursor_close(cursor);
mdb_txn_abort(txn);
return res;
}
bool Core::Archive::_isFromTheBeginning()
{
std::string strKey = "beginning";
MDB_val lmdbKey, lmdbData;
lmdbKey.mv_size = strKey.size();
lmdbKey.mv_data = (char*)strKey.c_str();
MDB_txn *txn;
int rc;
mdb_txn_begin(environment, NULL, MDB_RDONLY, &txn);
rc = mdb_get(txn, stats, &lmdbKey, &lmdbData);
if (rc == MDB_NOTFOUND) {
mdb_txn_abort(txn);
return false;
} else if (rc) {
qDebug() <<"isFromTheBeginning error: " << mdb_strerror(rc);
mdb_txn_abort(txn);
throw NotFound(strKey, jid.toStdString());
} else {
uint8_t value = *(uint8_t*)(lmdbData.mv_data);
bool is;
if (value == 144) {
is = false;
} else if (value == 72) {
is = true;
} else {
qDebug() <<"isFromTheBeginning error: stored value doesn't match any magic number, the answer is most probably wrong";
}
return is;
}
}
bool Core::Archive::isFromTheBeginning()
{
if (!opened) {
throw Closed("isFromTheBeginning", jid.toStdString());
}
return fromTheBeginning;
}
void Core::Archive::setFromTheBeginning(bool is)
{
if (!opened) {
throw Closed("setFromTheBeginning", jid.toStdString());
}
if (fromTheBeginning != is) {
fromTheBeginning = is;
const std::string& id = "beginning";
uint8_t value = 144;
if (is) {
value = 72;
}
MDB_val lmdbKey, lmdbData;
lmdbKey.mv_size = id.size();
lmdbKey.mv_data = (char*)id.c_str();
lmdbData.mv_size = sizeof value;
lmdbData.mv_data = &value;
MDB_txn *txn;
mdb_txn_begin(environment, NULL, 0, &txn);
int rc;
rc = mdb_put(txn, stats, &lmdbKey, &lmdbData, 0);
if (rc != 0) {
qDebug() << "Couldn't store beginning key into stat database:" << mdb_strerror(rc);
mdb_txn_abort(txn);
}
}
}
void Core::Archive::printOrder()
{
qDebug() << "Printing order";
MDB_txn *txn;
int rc;
rc = mdb_txn_begin(environment, NULL, MDB_RDONLY, &txn);
MDB_cursor* cursor;
rc = mdb_cursor_open(txn, order, &cursor);
MDB_val lmdbKey, lmdbData;
rc = mdb_cursor_get(cursor, &lmdbKey, &lmdbData, MDB_FIRST);
do {
std::string sId((char*)lmdbData.mv_data, lmdbData.mv_size);
qDebug() << QString(sId.c_str());
} while (mdb_cursor_get(cursor, &lmdbKey, &lmdbData, MDB_NEXT) == 0);
mdb_cursor_close(cursor);
mdb_txn_abort(txn);
}
void Core::Archive::printKeys()
{
MDB_txn *txn;
int rc;
rc = mdb_txn_begin(environment, NULL, MDB_RDONLY, &txn);
MDB_cursor* cursor;
rc = mdb_cursor_open(txn, main, &cursor);
MDB_val lmdbKey, lmdbData;
rc = mdb_cursor_get(cursor, &lmdbKey, &lmdbData, MDB_FIRST);
do {
std::string sId((char*)lmdbKey.mv_data, lmdbKey.mv_size);
qDebug() << QString(sId.c_str());
} while (mdb_cursor_get(cursor, &lmdbKey, &lmdbData, MDB_NEXT) == 0);
mdb_cursor_close(cursor);
mdb_txn_abort(txn);
}

View File

@ -23,6 +23,7 @@
#include "../global.h" #include "../global.h"
#include <lmdb.h> #include <lmdb.h>
#include "../exception.h" #include "../exception.h"
#include <list>
namespace Core { namespace Core {
@ -36,7 +37,8 @@ public:
void open(const QString& account); void open(const QString& account);
void close(); void close();
void addElement(const Shared::Message& message); bool addElement(const Shared::Message& message);
unsigned int addElements(const std::list<Shared::Message>& messages);
Shared::Message getElement(const QString& id); Shared::Message getElement(const QString& id);
Shared::Message oldest(); Shared::Message oldest();
QString oldestId(); QString oldestId();
@ -44,6 +46,9 @@ public:
QString newestId(); QString newestId();
void clear(); void clear();
long unsigned int size() const; long unsigned int size() const;
std::list<Shared::Message> getBefore(int count, const QString& id);
bool isFromTheBeginning();
void setFromTheBeginning(bool is);
public: public:
const QString jid; const QString jid;
@ -97,9 +102,15 @@ public:
private: private:
bool opened; bool opened;
bool fromTheBeginning;
MDB_env* environment; MDB_env* environment;
MDB_dbi main; MDB_dbi main;
MDB_dbi order; MDB_dbi order;
MDB_dbi stats;
bool _isFromTheBeginning();
void printOrder();
void printKeys();
}; };
} }

View File

@ -17,6 +17,7 @@
*/ */
#include "contact.h" #include "contact.h"
#include <QDebug>
Core::Contact::Contact(const QString& pJid, const QString& account, QObject* parent): Core::Contact::Contact(const QString& pJid, const QString& account, QObject* parent):
QObject(parent), QObject(parent),
@ -25,9 +26,23 @@ Core::Contact::Contact(const QString& pJid, const QString& account, QObject* par
groups(), groups(),
archiveState(empty), archiveState(empty),
archive(new Archive(jid)), archive(new Archive(jid)),
subscriptionState(Shared::unknown) subscriptionState(Shared::unknown),
syncronizing(false),
requestedCount(0),
requestedBefore(),
hisoryCache(),
appendCache(),
responseCache(),
requestCache()
{ {
archive->open(account); archive->open(account);
if (archive->isFromTheBeginning()) {
archiveState = beginning;
} else {
if (archive->size() != 0) {
archiveState = chunk;
}
}
} }
Core::Contact::~Contact() Core::Contact::~Contact()
@ -91,3 +106,236 @@ unsigned int Core::Contact::groupsCount() const
{ {
return groups.size(); return groups.size();
} }
void Core::Contact::addMessageToArchive(const Shared::Message& msg)
{
if (msg.getId().size() > 0 && msg.getBody().size() > 0) {
hisoryCache.emplace_back(msg);
}
}
void Core::Contact::requestHistory(int count, const QString& before)
{
if (syncronizing) {
requestCache.emplace_back(count, before);
} else {
performRequest(count, before);
}
}
void Core::Contact::nextRequest()
{
if (syncronizing) {
if (requestedCount != -1) {
emit historyResponse(responseCache);
}
}
if (requestCache.size() > 0) {
std::pair<int, QString> request = requestCache.front();
requestCache.pop_front();
performRequest(request.first, request.second);
} else {
syncronizing = false;
requestedCount = 0;
requestedBefore = "";
hisoryCache.clear();
responseCache.clear();
}
}
void Core::Contact::performRequest(int count, const QString& before)
{
syncronizing = true;
requestedCount = count;
requestedBefore = before;
hisoryCache.clear();
responseCache.clear();
switch (archiveState) {
case empty:
emit needHistory(before, "");
break;
case chunk:
case beginning:
if (count != -1) {
requestCache.emplace_back(requestedCount, before);
requestedCount = -1;
}
emit needHistory("", archive->newestId());
break;
case end:
if (count != -1) {
QString lBefore;
if (responseCache.size() > 0) {
lBefore = responseCache.front().getId();
} else {
lBefore = before;
}
bool found = false;
try {
std::list<Shared::Message> arc = archive->getBefore(requestedCount - responseCache.size(), lBefore);
responseCache.insert(responseCache.begin(), arc.begin(), arc.end());
found = true;
} catch (Archive::NotFound e) {
requestCache.emplace_back(requestedCount, before);
requestedCount = -1;
emit needHistory(archive->oldestId(), "");
}
if (found) {
int rSize = responseCache.size();
if (rSize < count) {
if (rSize != 0) {
emit needHistory(responseCache.front().getId(), "");
} else {
emit needHistory(before, "");
}
} else {
nextRequest();
}
}
} else {
emit needHistory(archive->oldestId(), "");
}
break;
case complete:
try {
std::list<Shared::Message> arc = archive->getBefore(requestedCount - responseCache.size(), before);
responseCache.insert(responseCache.begin(), arc.begin(), arc.end());
} catch (Archive::NotFound e) {
qDebug("requesting id hasn't been found in archive, skipping");
}
nextRequest();
break;
}
}
void Core::Contact::appendMessageToArchive(const Shared::Message& msg)
{
const QString& id = msg.getId();
if (id.size() > 0) {
if (msg.getBody().size() > 0) {
switch (archiveState) {
case empty:
if (archive->addElement(msg)) {
archiveState = end;
};
if (!syncronizing) {
requestHistory(-1, id);
}
break;
case beginning:
appendCache.emplace_back(msg);
if (!syncronizing) {
requestHistory(-1, id);
}
break;
case end:
archive->addElement(msg);
break;
case chunk:
appendCache.emplace_back(msg);
if (!syncronizing) {
requestHistory(-1, id);
}
break;
case complete:
archive->addElement(msg);
break;
}
} else if (!syncronizing && archiveState == empty) {
requestHistory(-1, id);
}
}
}
void Core::Contact::flushMessagesToArchive(bool finished, const QString& firstId, const QString& lastId)
{
unsigned int added(0);
if (hisoryCache.size() > 0) {
added = archive->addElements(hisoryCache);
qDebug() << "Added" << added << "messages to the archive";
hisoryCache.clear();
}
switch (archiveState) {
break;
case beginning:
if (finished) {
archiveState = complete;
nextRequest();
} else {
emit needHistory("", lastId);
}
case chunk:
if (finished) {
archiveState = end;
nextRequest();
} else {
emit needHistory("", lastId);
}
break;
case empty:
archiveState = end;
case end:
if (finished) {
archiveState = complete;
archive->setFromTheBeginning(true);
}
if (requestedCount != -1) {
QString before;
if (responseCache.size() > 0) {
before = responseCache.front().getId();
} else {
before = requestedBefore;
}
bool found = false;
try {
std::list<Shared::Message> arc = archive->getBefore(requestedCount - responseCache.size(), before);
responseCache.insert(responseCache.begin(), arc.begin(), arc.end());
found = true;
} catch (Archive::NotFound e) {}
if (!found || requestedCount < responseCache.size()) {
if (archiveState == complete) {
nextRequest();
} else {
emit needHistory(firstId, "");
}
} else {
nextRequest();
}
} else {
if (added != 0) {
nextRequest();
} else {
emit needHistory(firstId, "");
}
}
break;
case complete:
nextRequest();
break;
}
}
void Core::Contact::requestFromEmpty(int count, const QString& before)
{
if (syncronizing) {
qDebug("perform from empty didn't work, another request queued");
} else {
if (archiveState != empty) {
qDebug("perform from empty didn't work, the state is not empty");
requestHistory(count, before);
} else {
syncronizing = true;
requestedCount = count;
requestedBefore = "";
hisoryCache.clear();
responseCache.clear();
emit needHistory(before, "");
}
}
}

View File

@ -21,8 +21,10 @@
#include <QObject> #include <QObject>
#include <QSet> #include <QSet>
#include <QString>
#include "archive.h" #include "archive.h"
#include "../global.h" #include "../global.h"
#include <list>
namespace Core { namespace Core {
@ -48,12 +50,19 @@ public:
void setSubscriptionState(Shared::SubscriptionState state); void setSubscriptionState(Shared::SubscriptionState state);
Shared::SubscriptionState getSubscriptionState() const; Shared::SubscriptionState getSubscriptionState() const;
unsigned int groupsCount() const; unsigned int groupsCount() const;
void addMessageToArchive(const Shared::Message& msg);
void appendMessageToArchive(const Shared::Message& msg);
void flushMessagesToArchive(bool finished, const QString& firstId, const QString& lastId);
void requestHistory(int count, const QString& before);
void requestFromEmpty(int count, const QString& before);
signals: signals:
void groupAdded(const QString& name); void groupAdded(const QString& name);
void groupRemoved(const QString& name); void groupRemoved(const QString& name);
void nameChanged(const QString& name); void nameChanged(const QString& name);
void subscriptionStateChanged(Shared::SubscriptionState state); void subscriptionStateChanged(Shared::SubscriptionState state);
void historyResponse(const std::list<Shared::Message>& messages);
void needHistory(const QString& before, const QString& after);
public: public:
const QString jid; const QString jid;
@ -64,6 +73,18 @@ private:
ArchiveState archiveState; ArchiveState archiveState;
Archive* archive; Archive* archive;
Shared::SubscriptionState subscriptionState; Shared::SubscriptionState subscriptionState;
bool syncronizing;
int requestedCount;
QString requestedBefore;
std::list<Shared::Message> hisoryCache;
std::list<Shared::Message> appendCache;
std::list<Shared::Message> responseCache;
std::list<std::pair<int, QString>> requestCache;
private:
void nextRequest();
void performRequest(int count, const QString& before);
}; };
} }

View File

@ -96,6 +96,8 @@ void Core::Squawk::addAccount(const QString& login, const QString& server, const
this, SLOT(onAccountAddPresence(const QString&, const QString&, const QMap<QString, QVariant>&))); this, SLOT(onAccountAddPresence(const QString&, const QString&, const QMap<QString, QVariant>&)));
connect(acc, SIGNAL(removePresence(const QString&, const QString&)), this, SLOT(onAccountRemovePresence(const QString&, const QString&))); connect(acc, SIGNAL(removePresence(const QString&, const QString&)), this, SLOT(onAccountRemovePresence(const QString&, const QString&)));
connect(acc, SIGNAL(message(const Shared::Message&)), this, SLOT(onAccountMessage(const Shared::Message&))); connect(acc, SIGNAL(message(const Shared::Message&)), this, SLOT(onAccountMessage(const Shared::Message&)));
connect(acc, SIGNAL(responseArchive(const QString&, const std::list<Shared::Message>&)),
this, SLOT(onAccountResponseArchive(const QString&, const std::list<Shared::Message>&)));
QMap<QString, QVariant> map = { QMap<QString, QVariant> map = {
{"login", login}, {"login", login},
@ -221,12 +223,19 @@ void Core::Squawk::sendMessage(const QString& account, const Shared::Message& da
itr->second->sendMessage(data); itr->second->sendMessage(data);
} }
void Core::Squawk::requestArchive(const QString& account, const QString& jid) void Core::Squawk::requestArchive(const QString& account, const QString& jid, int count, const QString& before)
{ {
AccountsMap::const_iterator itr = amap.find(account); AccountsMap::const_iterator itr = amap.find(account);
if (itr == amap.end()) { if (itr == amap.end()) {
qDebug("An attempt to request an archive of non existing account, skipping"); qDebug("An attempt to request an archive of non existing account, skipping");
return; return;
} }
itr->second->requestAchive(jid); itr->second->requestArchive(jid, count, before);
} }
void Core::Squawk::onAccountResponseArchive(const QString& jid, const std::list<Shared::Message>& list)
{
Account* acc = static_cast<Account*>(sender());
emit responseArchive(acc->getName(), jid, list);
}

View File

@ -36,6 +36,7 @@ signals:
void removePresence(const QString& account, const QString& jid, const QString& name); void removePresence(const QString& account, const QString& jid, const QString& name);
void stateChanged(int state); void stateChanged(int state);
void accountMessage(const QString& account, const Shared::Message& data); void accountMessage(const QString& account, const Shared::Message& data);
void responseArchive(const QString& account, const QString& jid, const std::list<Shared::Message>& list);
public slots: public slots:
void start(); void start();
@ -45,7 +46,7 @@ public slots:
void disconnectAccount(const QString& account); void disconnectAccount(const QString& account);
void changeState(int state); void changeState(int state);
void sendMessage(const QString& account, const Shared::Message& data); void sendMessage(const QString& account, const Shared::Message& data);
void requestArchive(const QString& account, const QString& jid); void requestArchive(const QString& account, const QString& jid, int count, const QString& before);
private: private:
typedef std::deque<Account*> Accounts; typedef std::deque<Account*> Accounts;
@ -70,6 +71,7 @@ private slots:
void onAccountAddPresence(const QString& jid, const QString& name, const QMap<QString, QVariant>& data); void onAccountAddPresence(const QString& jid, const QString& name, const QMap<QString, QVariant>& data);
void onAccountRemovePresence(const QString& jid, const QString& name); void onAccountRemovePresence(const QString& jid, const QString& name);
void onAccountMessage(const Shared::Message& data); void onAccountMessage(const Shared::Message& data);
void onAccountResponseArchive(const QString& jid, const std::list<Shared::Message>& list);
}; };
} }

View File

@ -176,12 +176,7 @@ bool Shared::Message::getForwarded() const
void Shared::Message::generateRandomId() void Shared::Message::generateRandomId()
{ {
uuid_t uuid; id = generateUUID();
uuid_generate(uuid);
char uuid_str[36];
uuid_unparse_lower(uuid, uuid_str);
id = uuid_str;
} }
QString Shared::Message::getThread() const QString Shared::Message::getThread() const
@ -241,3 +236,19 @@ void Shared::Message::deserialize(QDataStream& data)
data >> outgoing; data >> outgoing;
data >> forwarded; data >> forwarded;
} }
QString Shared::generateUUID()
{
uuid_t uuid;
uuid_generate(uuid);
char uuid_str[36];
uuid_unparse_lower(uuid, uuid_str);
return uuid_str;
}
void Shared::Message::setCurrentTime()
{
time = QDateTime::currentDateTime();
}

View File

@ -53,6 +53,8 @@ static const std::deque<QString> availabilityNames = {"Online", "Away", "Absent"
static const std::deque<QString> subscriptionStateThemeIcons = {"edit-none", "arrow-down-double", "arrow-up-double", "dialog-ok", "question"}; static const std::deque<QString> subscriptionStateThemeIcons = {"edit-none", "arrow-down-double", "arrow-up-double", "dialog-ok", "question"};
QString generateUUID();
class Message { class Message {
public: public:
enum Type { enum Type {
@ -78,6 +80,7 @@ public:
void setOutgoing(bool og); void setOutgoing(bool og);
void setForwarded(bool fwd); void setForwarded(bool fwd);
void setType(Type t); void setType(Type t);
void setCurrentTime();
QString getFrom() const; QString getFrom() const;
QString getFromJid() const; QString getFromJid() const;

View File

@ -9,6 +9,7 @@
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
qRegisterMetaType<Shared::Message>("Shared::Message"); qRegisterMetaType<Shared::Message>("Shared::Message");
qRegisterMetaType<std::list<Shared::Message>>("std::list<Shared::Message>");
QApplication app(argc, argv); QApplication app(argc, argv);
SignalCatcher sc(&app); SignalCatcher sc(&app);
@ -35,7 +36,7 @@ int main(int argc, char *argv[])
QObject::connect(&w, SIGNAL(disconnectAccount(const QString&)), squawk, SLOT(disconnectAccount(const QString&))); QObject::connect(&w, SIGNAL(disconnectAccount(const QString&)), squawk, SLOT(disconnectAccount(const QString&)));
QObject::connect(&w, SIGNAL(changeState(int)), squawk, SLOT(changeState(int))); QObject::connect(&w, SIGNAL(changeState(int)), squawk, SLOT(changeState(int)));
QObject::connect(&w, SIGNAL(sendMessage(const QString&, const Shared::Message&)), squawk, SLOT(sendMessage(const QString&, const Shared::Message&))); QObject::connect(&w, SIGNAL(sendMessage(const QString&, const Shared::Message&)), squawk, SLOT(sendMessage(const QString&, const Shared::Message&)));
QObject::connect(&w, SIGNAL(requestArchive(const QString&, const QString&)), squawk, SLOT(requestArchive(const QString&, const QString&))); QObject::connect(&w, SIGNAL(requestArchive(const QString&, const QString&, int, const QString&)), squawk, SLOT(requestArchive(const QString&, const QString&, int, const QString&)));
QObject::connect(squawk, SIGNAL(newAccount(const QMap<QString, QVariant>&)), &w, SLOT(newAccount(const QMap<QString, QVariant>&))); QObject::connect(squawk, SIGNAL(newAccount(const QMap<QString, QVariant>&)), &w, SLOT(newAccount(const QMap<QString, QVariant>&)));
QObject::connect(squawk, SIGNAL(accountAvailabilityChanged(const QString&, int)), &w, SLOT(accountAvailabilityChanged(const QString&, int))); QObject::connect(squawk, SIGNAL(accountAvailabilityChanged(const QString&, int)), &w, SLOT(accountAvailabilityChanged(const QString&, int)));
@ -53,6 +54,8 @@ int main(int argc, char *argv[])
QObject::connect(squawk, SIGNAL(removePresence(const QString&, const QString&, const QString&)), &w, SLOT(removePresence(const QString&, const QString&, const QString&))); QObject::connect(squawk, SIGNAL(removePresence(const QString&, const QString&, const QString&)), &w, SLOT(removePresence(const QString&, const QString&, const QString&)));
QObject::connect(squawk, SIGNAL(stateChanged(int)), &w, SLOT(stateChanged(int))); QObject::connect(squawk, SIGNAL(stateChanged(int)), &w, SLOT(stateChanged(int)));
QObject::connect(squawk, SIGNAL(accountMessage(const QString&, const Shared::Message&)), &w, SLOT(accountMessage(const QString&, const Shared::Message&))); QObject::connect(squawk, SIGNAL(accountMessage(const QString&, const Shared::Message&)), &w, SLOT(accountMessage(const QString&, const Shared::Message&)));
QObject::connect(squawk, SIGNAL(responseArchive(const QString&, const QString&, const std::list<Shared::Message>&)),
&w, SLOT(responseArchive(const QString&, const QString&, const std::list<Shared::Message>&)));
//qDebug() << QStandardPaths::writableLocation(QStandardPaths::CacheLocation); //qDebug() << QStandardPaths::writableLocation(QStandardPaths::CacheLocation);

View File

@ -8,6 +8,10 @@ set(CMAKE_AUTOUIC ON)
# Find the QtWidgets library # Find the QtWidgets library
find_package(Qt5Widgets CONFIG REQUIRED) find_package(Qt5Widgets CONFIG REQUIRED)
find_package(Qt5Qml CONFIG REQUIRED)
find_package(Qt5QuickCompiler)
find_package(Qt5Quick CONFIG REQUIRED)
find_package(Qt5QuickWidgets CONFIG REQUIRED)
set(squawkUI_SRC set(squawkUI_SRC
squawk.cpp squawk.cpp
@ -28,6 +32,9 @@ add_library(squawkUI ${squawkUI_SRC})
# Use the Widgets module from Qt 5. # Use the Widgets module from Qt 5.
target_link_libraries(squawkUI Qt5::Widgets) target_link_libraries(squawkUI Qt5::Widgets)
target_link_libraries(squawkUI Qt5::Quick)
target_link_libraries(squawkUI Qt5::Qml)
target_link_libraries(squawkUI Qt5::QuickWidgets)
# Install the executable # Install the executable
install(TARGETS squawkUI DESTINATION lib) install(TARGETS squawkUI DESTINATION lib)

View File

@ -20,6 +20,7 @@
#include "ui_conversation.h" #include "ui_conversation.h"
#include <QDebug> #include <QDebug>
#include <QScrollBar> #include <QScrollBar>
#include <QTimer>
Conversation::Conversation(Models::Contact* p_contact, QWidget* parent): Conversation::Conversation(Models::Contact* p_contact, QWidget* parent):
QWidget(parent), QWidget(parent),
@ -30,7 +31,9 @@ Conversation::Conversation(Models::Contact* p_contact, QWidget* parent):
activePalResource(), activePalResource(),
thread(), thread(),
scroll(down), scroll(down),
manualSliderChange(false) manualSliderChange(false),
requestingHistory(false),
everShown(false)
{ {
m_ui->setupUi(this); m_ui->setupUi(this);
m_ui->splitter->setSizes({300, 0}); m_ui->splitter->setSizes({300, 0});
@ -116,6 +119,7 @@ void Conversation::addMessage(const Shared::Message& data)
{ {
int pos = m_ui->scrollArea->verticalScrollBar()->sliderPosition(); int pos = m_ui->scrollArea->verticalScrollBar()->sliderPosition();
int max = m_ui->scrollArea->verticalScrollBar()->maximum(); int max = m_ui->scrollArea->verticalScrollBar()->maximum();
MessageLine::Position place = line->message(data); MessageLine::Position place = line->message(data);
if (place == MessageLine::invalid) { if (place == MessageLine::invalid) {
return; return;
@ -183,6 +187,7 @@ void Conversation::onEnterPressed()
msg.setBody(body); msg.setBody(body);
msg.setOutgoing(true); msg.setOutgoing(true);
msg.generateRandomId(); msg.generateRandomId();
msg.setCurrentTime();
addMessage(msg); addMessage(msg);
emit sendMessage(msg); emit sendMessage(msg);
} }
@ -195,6 +200,18 @@ void Conversation::onMessagesResize(int amount)
case down: case down:
m_ui->scrollArea->verticalScrollBar()->setValue(m_ui->scrollArea->verticalScrollBar()->maximum()); m_ui->scrollArea->verticalScrollBar()->setValue(m_ui->scrollArea->verticalScrollBar()->maximum());
break; break;
case keep: {
int max = m_ui->scrollArea->verticalScrollBar()->maximum();
int value = m_ui->scrollArea->verticalScrollBar()->value() + amount;
m_ui->scrollArea->verticalScrollBar()->setValue(value);
if (value == max) {
scroll = down;
} else {
scroll = nothing;
}
}
break;
default: default:
break; break;
} }
@ -207,7 +224,35 @@ void Conversation::onSliderValueChanged(int value)
if (value == m_ui->scrollArea->verticalScrollBar()->maximum()) { if (value == m_ui->scrollArea->verticalScrollBar()->maximum()) {
scroll = down; scroll = down;
} else { } else {
if (!requestingHistory && value == 0) {
m_ui->historyStatus->setPixmap(QIcon::fromTheme("view-refresh").pixmap(25));
requestingHistory = true;
emit requestArchive(line->firstMessageId());
}
scroll = nothing; scroll = nothing;
} }
} }
} }
void Conversation::responseArchive(const std::list<Shared::Message> list)
{
requestingHistory = false;
scroll = keep;
m_ui->historyStatus->clear();
for (std::list<Shared::Message>::const_iterator itr = list.begin(), end = list.end(); itr != end; ++itr) {
addMessage(*itr);
}
}
void Conversation::showEvent(QShowEvent* event)
{
if (!everShown) {
everShown = true;
m_ui->historyStatus->setPixmap(QIcon::fromTheme("view-refresh").pixmap(25));
requestingHistory = true;
emit requestArchive(line->firstMessageId());
}
QWidget::showEvent(event);
}

View File

@ -56,9 +56,12 @@ public:
void addMessage(const Shared::Message& data); void addMessage(const Shared::Message& data);
void setPalResource(const QString& res); void setPalResource(const QString& res);
void responseArchive(const std::list<Shared::Message> list);
void showEvent(QShowEvent * event) override;
signals: signals:
void sendMessage(const Shared::Message& message); void sendMessage(const Shared::Message& message);
void requestArchive(const QString& before);
protected: protected:
void setState(Shared::Availability state); void setState(Shared::Availability state);
@ -85,6 +88,8 @@ private:
QString thread; QString thread;
Scroll scroll; Scroll scroll;
bool manualSliderChange; bool manualSliderChange;
bool requestingHistory;
bool everShown;
}; };
#endif // CONVERSATION_H #endif // CONVERSATION_H

View File

@ -105,6 +105,13 @@
</property> </property>
</spacer> </spacer>
</item> </item>
<item>
<widget class="QLabel" name="historyStatus">
<property name="text">
<string/>
</property>
</widget>
</item>
<item> <item>
<widget class="QLabel" name="avatar"> <widget class="QLabel" name="avatar">
<property name="text"> <property name="text">
@ -134,7 +141,7 @@
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>572</width> <width>572</width>
<height>162</height> <height>95</height>
</rect> </rect>
</property> </property>
<layout class="QHBoxLayout" name="horizontalLayout_2"> <layout class="QHBoxLayout" name="horizontalLayout_2">

View File

@ -76,6 +76,12 @@ MessageLine::Position MessageLine::message(const Shared::Message& msg)
QLabel* body = new QLabel(msg.getBody()); QLabel* body = new QLabel(msg.getBody());
QLabel* sender = new QLabel(); QLabel* sender = new QLabel();
QLabel* time = new QLabel(msg.getTime().toLocalTime().toString());
QFont dFont = time->font();
dFont.setItalic(true);
dFont.setPointSize(dFont.pointSize() - 2);
time->setFont(dFont);
time->setForegroundRole(QPalette::ToolTipText);
QFont f; QFont f;
f.setBold(true); f.setBold(true);
sender->setFont(f); sender->setFont(f);
@ -84,10 +90,12 @@ MessageLine::Position MessageLine::message(const Shared::Message& msg)
vBox->addWidget(sender); vBox->addWidget(sender);
vBox->addWidget(body); vBox->addWidget(body);
vBox->addWidget(time);
if (msg.getOutgoing()) { if (msg.getOutgoing()) {
body->setAlignment(Qt::AlignRight); //body->setAlignment(Qt::AlignRight);
sender->setAlignment(Qt::AlignRight); sender->setAlignment(Qt::AlignRight);
time->setAlignment(Qt::AlignRight);
sender->setText(myName); sender->setText(myName);
hBox->addStretch(); hBox->addStretch();
hBox->addWidget(message); hBox->addWidget(message);
@ -132,3 +140,13 @@ void MessageLine::resizeEvent(QResizeEvent* event)
QWidget::resizeEvent(event); QWidget::resizeEvent(event);
emit resize(event->size().height() - event->oldSize().height()); emit resize(event->size().height() - event->oldSize().height());
} }
QString MessageLine::firstMessageId() const
{
if (messageOrder.size() == 0) {
return "";
} else {
return messageOrder.begin()->second->getId();
}
}

View File

@ -25,6 +25,7 @@
#include <QLabel> #include <QLabel>
#include <QResizeEvent> #include <QResizeEvent>
#include "../global.h" #include "../global.h"
#include <QtQuickWidgets/QQuickWidget>
class MessageLine : public QWidget class MessageLine : public QWidget
{ {
@ -42,6 +43,9 @@ public:
Position message(const Shared::Message& msg); Position message(const Shared::Message& msg);
void setMyName(const QString& name); void setMyName(const QString& name);
void setPalName(const QString& jid, const QString& name); void setPalName(const QString& jid, const QString& name);
QString firstMessageId() const;
void showBusyIndicator();
void hideBusyIndicator();
signals: signals:
void resize(int amount); void resize(int amount);
@ -67,6 +71,7 @@ private:
QString myName; QString myName;
std::map<QString, QString> palNames; std::map<QString, QString> palNames;
std::deque<QHBoxLayout*> views; std::deque<QHBoxLayout*> views;
QQuickWidget busy;
}; };
#endif // MESSAGELINE_H #endif // MESSAGELINE_H

View File

@ -189,6 +189,7 @@ void Squawk::onRosterItemDoubleClicked(const QModelIndex& item)
conv->setAttribute(Qt::WA_DeleteOnClose); conv->setAttribute(Qt::WA_DeleteOnClose);
connect(conv, SIGNAL(destroyed(QObject*)), this, SLOT(onConversationClosed(QObject*))); connect(conv, SIGNAL(destroyed(QObject*)), this, SLOT(onConversationClosed(QObject*)));
connect(conv, SIGNAL(sendMessage(const Shared::Message&)), this, SLOT(onConversationMessage(const Shared::Message&))); connect(conv, SIGNAL(sendMessage(const Shared::Message&)), this, SLOT(onConversationMessage(const Shared::Message&)));
connect(conv, SIGNAL(requestArchive(const QString&)), this, SLOT(onConversationRequestArchive(const QString&)));
conversations.insert(std::make_pair(id, conv)); conversations.insert(std::make_pair(id, conv));
rosterModel.dropMessages(account, jid); rosterModel.dropMessages(account, jid);
@ -198,7 +199,6 @@ void Squawk::onRosterItemDoubleClicked(const QModelIndex& item)
if (res.size() > 0) { if (res.size() > 0) {
itr->second->setPalResource(res); itr->second->setPalResource(res);
} }
requestArchive(account, jid);
} }
} }
} }
@ -234,3 +234,19 @@ void Squawk::onConversationMessage(const Shared::Message& msg)
emit sendMessage(conv->getAccount(), msg); emit sendMessage(conv->getAccount(), msg);
} }
void Squawk::onConversationRequestArchive(const QString& before)
{
Conversation* conv = static_cast<Conversation*>(sender());
requestArchive(conv->getAccount(), conv->getJid(), 20, before); //TODO amount as a settings value
}
void Squawk::responseArchive(const QString& account, const QString& jid, const std::list<Shared::Message>& list)
{
Models::Roster::ElId id(account, jid);
Conversations::const_iterator itr = conversations.find(id);
if (itr != conversations.end()) {
itr->second->responseArchive(list);
}
}

View File

@ -6,6 +6,7 @@
#include <QCloseEvent> #include <QCloseEvent>
#include <deque> #include <deque>
#include <map> #include <map>
#include <list>
#include "accounts.h" #include "accounts.h"
#include "conversation.h" #include "conversation.h"
@ -31,7 +32,7 @@ signals:
void disconnectAccount(const QString&); void disconnectAccount(const QString&);
void changeState(int state); void changeState(int state);
void sendMessage(const QString& account, const Shared::Message& data); void sendMessage(const QString& account, const Shared::Message& data);
void requestArchive(const QString& account, const QString& jid); void requestArchive(const QString& account, const QString& jid, int count, const QString& before);
public slots: public slots:
void newAccount(const QMap<QString, QVariant>& account); void newAccount(const QMap<QString, QVariant>& account);
@ -47,6 +48,7 @@ public slots:
void removePresence(const QString& account, const QString& jid, const QString& name); void removePresence(const QString& account, const QString& jid, const QString& name);
void stateChanged(int state); void stateChanged(int state);
void accountMessage(const QString& account, const Shared::Message& data); void accountMessage(const QString& account, const Shared::Message& data);
void responseArchive(const QString& account, const QString& jid, const std::list<Shared::Message>& list);
private: private:
typedef std::map<Models::Roster::ElId, Conversation*> Conversations; typedef std::map<Models::Roster::ElId, Conversation*> Conversations;
@ -66,6 +68,7 @@ private slots:
void onComboboxActivated(int index); void onComboboxActivated(int index);
void onRosterItemDoubleClicked(const QModelIndex& item); void onRosterItemDoubleClicked(const QModelIndex& item);
void onConversationMessage(const Shared::Message& msg); void onConversationMessage(const Shared::Message& msg);
void onConversationRequestArchive(const QString& before);
}; };