First working prototype of history requesting, dates in messages, some screen scrolling handling
This commit is contained in:
parent
d09fef5a34
commit
da3151391b
19 changed files with 422 additions and 136 deletions
165
core/account.cpp
165
core/account.cpp
|
@ -1,6 +1,7 @@
|
|||
#include "account.h"
|
||||
#include <qxmpp/QXmppMessage.h>
|
||||
#include <QDateTime>
|
||||
#include <QTimer>
|
||||
|
||||
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(presenceReceived(const QXmppPresence&)), this, SLOT(onPresenceReceived(const QXmppPresence&)));
|
||||
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();
|
||||
|
||||
|
@ -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(subscriptionStateChanged(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());
|
||||
msg.setId(data.getId());
|
||||
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);
|
||||
} else {
|
||||
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)
|
||||
{
|
||||
QString body(msg.body());
|
||||
const QString& body(msg.body());
|
||||
if (body.size() != 0) {
|
||||
QString id(msg.id());
|
||||
QDateTime time(msg.stamp());
|
||||
const QString& id(msg.id());
|
||||
Shared::Message sMsg(Shared::Message::chat);
|
||||
sMsg.setId(id);
|
||||
sMsg.setFrom(msg.from());
|
||||
sMsg.setTo(msg.to());
|
||||
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);
|
||||
}
|
||||
initializeMessage(sMsg, msg, outgoing, forwarded, guessing);
|
||||
std::map<QString, Contact*>::const_iterator itr = contacts.find(sMsg.getPenPalJid());
|
||||
itr->second->appendMessageToArchive(sMsg);
|
||||
|
||||
emit message(sMsg);
|
||||
|
||||
if (!forwarded && !outgoing) {
|
||||
|
@ -422,13 +417,51 @@ bool Core::Account::handleChatMessage(const QXmppMessage& msg, bool outgoing, bo
|
|||
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::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";
|
||||
|
@ -436,40 +469,56 @@ void Core::Account::requestArchive(const QString& jid, int count, const QString&
|
|||
}
|
||||
Contact* contact = itr->second;
|
||||
|
||||
contact->requestHistory(count, before);
|
||||
Contact::ArchiveState as = contact->getArchiveState();
|
||||
switch (as) {
|
||||
case Contact::empty:
|
||||
break;
|
||||
case Contact::beginning:
|
||||
break;
|
||||
case Contact::chunk:
|
||||
break;
|
||||
case Contact::complete:
|
||||
break;
|
||||
case Contact::end:
|
||||
break;
|
||||
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;
|
||||
query.setMax(100);
|
||||
QDateTime from = QDateTime::currentDateTime().addDays(-7);
|
||||
|
||||
QString q = am->retrieveArchivedMessages("", "", jid, from, QDateTime(), query);
|
||||
achiveQueries.insert(std::make_pair(q, jid));
|
||||
if (before.size() > 0) {
|
||||
query.setBefore(before);
|
||||
}
|
||||
if (after.size() > 0) {
|
||||
query.setAfter(after);
|
||||
}
|
||||
|
||||
qDebug() << "Remote query from\"" << after << "\", to" << before;
|
||||
|
||||
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)
|
||||
{
|
||||
std::map<QString, QString>::const_iterator itr = achiveQueries.find(queryId);
|
||||
QString jid = itr->second;
|
||||
achiveQueries.erase(itr);
|
||||
if (!complete) {
|
||||
QXmppResultSetQuery q;
|
||||
q.setAfter(resultSetReply.last());
|
||||
q.setMax(100);
|
||||
QString nQ = am->retrieveArchivedMessages("", "", jid, QDateTime::currentDateTime().addDays(-7), QDateTime(), q);
|
||||
achiveQueries.insert(std::make_pair(nQ, jid));
|
||||
std::map<QString, Contact*>::const_iterator citr = contacts.find(jid);
|
||||
if (citr != contacts.end()) {
|
||||
Contact* cnt = citr->second;
|
||||
|
||||
qDebug() << "Flushing messages for" << jid;
|
||||
cnt->flushMessagesToArchive(complete, resultSetReply.first(), resultSetReply.last());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -556,3 +605,29 @@ Shared::SubscriptionState Core::Account::castSubscriptionState(QXmppRosterIq::It
|
|||
}
|
||||
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();
|
||||
}
|
||||
|
||||
|
|
|
@ -55,6 +55,7 @@ signals:
|
|||
void addPresence(const QString& jid, const QString& name, const QMap<QString, QVariant>& data);
|
||||
void removePresence(const QString& jid, const QString& name);
|
||||
void message(const Shared::Message& data);
|
||||
void responseArchive(const QString& jid, const std::list<Shared::Message>& list);
|
||||
|
||||
private:
|
||||
QString name;
|
||||
|
@ -71,6 +72,7 @@ private:
|
|||
private slots:
|
||||
void onClientConnected();
|
||||
void onClientDisconnected();
|
||||
void onClientError(QXmppClient::Error err);
|
||||
|
||||
void onRosterReceived();
|
||||
void onRosterItemAdded(const QString& bareJid);
|
||||
|
@ -91,12 +93,15 @@ private slots:
|
|||
void onContactGroupRemoved(const QString& group);
|
||||
void onContactNameChanged(const QString& name);
|
||||
void onContactSubscriptionStateChanged(Shared::SubscriptionState state);
|
||||
void onContactHistoryResponse(const std::list<Shared::Message>& list);
|
||||
void onContactNeedHistory(const QString& before, const QString& after);
|
||||
|
||||
private:
|
||||
void addedAccount(const QString &bareJid);
|
||||
bool handleChatMessage(const QXmppMessage& msg, bool outgoing = false, bool forwarded = false, bool guessing = false);
|
||||
void addToGroup(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;
|
||||
};
|
||||
|
||||
|
|
|
@ -192,7 +192,7 @@ QString Core::Archive::newestId()
|
|||
qDebug() << "Error geting newestId " << mdb_strerror(rc);
|
||||
mdb_cursor_close(cursor);
|
||||
mdb_txn_abort(txn);
|
||||
throw new Empty(jid.toStdString());
|
||||
throw Empty(jid.toStdString());
|
||||
} else {
|
||||
std::string sId((char*)lmdbData.mv_data, lmdbData.mv_size);
|
||||
mdb_cursor_close(cursor);
|
||||
|
@ -220,6 +220,7 @@ unsigned int Core::Archive::addElements(const std::list<Shared::Message>& messag
|
|||
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);
|
||||
|
@ -230,6 +231,7 @@ unsigned int Core::Archive::addElements(const std::list<Shared::Message>& messag
|
|||
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;
|
||||
|
@ -249,6 +251,7 @@ unsigned int Core::Archive::addElements(const std::list<Shared::Message>& messag
|
|||
qDebug() << "An element couldn't been added to the archive, aborting the transaction" << mdb_strerror(rc);
|
||||
}
|
||||
}
|
||||
itr++;
|
||||
}
|
||||
|
||||
if (rc != 0) {
|
||||
|
@ -279,7 +282,7 @@ QString Core::Archive::oldestId()
|
|||
qDebug() << "Error geting oldestId " << mdb_strerror(rc);
|
||||
mdb_cursor_close(cursor);
|
||||
mdb_txn_abort(txn);
|
||||
throw new Empty(jid.toStdString());
|
||||
throw Empty(jid.toStdString());
|
||||
} else {
|
||||
std::string sId((char*)lmdbData.mv_data, lmdbData.mv_size);
|
||||
mdb_cursor_close(cursor);
|
||||
|
@ -317,21 +320,21 @@ std::list<Shared::Message> Core::Archive::getBefore(int count, const QString& id
|
|||
rc = mdb_cursor_open(txn, order, &cursor);
|
||||
rc = mdb_cursor_get(cursor, &lmdbKey, &lmdbData, MDB_LAST);
|
||||
if (rc) {
|
||||
qDebug() << "Error geting before " << mdb_strerror(rc);
|
||||
qDebug() << "Error getting before " << mdb_strerror(rc) << ", id:" << id;
|
||||
mdb_cursor_close(cursor);
|
||||
mdb_txn_abort(txn);
|
||||
throw new Empty(jid.toStdString());
|
||||
} else {
|
||||
std::string sId((char*)lmdbData.mv_data, lmdbData.mv_size);
|
||||
throw Empty(jid.toStdString());
|
||||
}
|
||||
} else {
|
||||
lmdbKey.mv_size = id.size();
|
||||
lmdbKey.mv_data = (char*)id.toStdString().c_str();
|
||||
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: " << mdb_strerror(rc);
|
||||
qDebug() <<"Error getting before: no reference message" << mdb_strerror(rc) << ", id:" << id;
|
||||
mdb_txn_abort(txn);
|
||||
throw NotFound(id.toStdString(), jid.toStdString());
|
||||
printKeys();
|
||||
throw NotFound(stdId, jid.toStdString());
|
||||
} else {
|
||||
QByteArray ba((char*)lmdbData.mv_data, lmdbData.mv_size);
|
||||
QDataStream ds(&ba, QIODevice::ReadOnly);
|
||||
|
@ -346,29 +349,40 @@ std::list<Shared::Message> Core::Archive::getBefore(int count, const QString& id
|
|||
rc = mdb_cursor_get(cursor, &lmdbKey, &lmdbData, MDB_SET);
|
||||
|
||||
if (rc) {
|
||||
qDebug() << "Error getting before " << mdb_strerror(rc);
|
||||
qDebug() << "Error getting before: couldn't set " << mdb_strerror(rc);
|
||||
mdb_cursor_close(cursor);
|
||||
mdb_txn_abort(txn);
|
||||
throw new NotFound(id.toStdString(), jid.toStdString());
|
||||
throw NotFound(stdId, jid.toStdString());
|
||||
} else {
|
||||
rc = mdb_cursor_get(cursor, &lmdbKey, &lmdbData, MDB_PREV);
|
||||
if (rc) {
|
||||
qDebug() << "Error getting before " << mdb_strerror(rc);
|
||||
qDebug() << "Error getting before, couldn't prev " << mdb_strerror(rc);
|
||||
mdb_cursor_close(cursor);
|
||||
mdb_txn_abort(txn);
|
||||
throw new NotFound(id.toStdString(), jid.toStdString());
|
||||
throw NotFound(stdId, jid.toStdString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
do {
|
||||
QByteArray ba((char*)lmdbData.mv_data, lmdbData.mv_size);
|
||||
QDataStream ds(&ba, QIODevice::ReadOnly);
|
||||
|
||||
res.emplace_back();
|
||||
Shared::Message& msg = res.back();
|
||||
msg.deserialize(ds);
|
||||
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;
|
||||
|
||||
|
@ -420,7 +434,7 @@ bool Core::Archive::isFromTheBeginning()
|
|||
return fromTheBeginning;
|
||||
}
|
||||
|
||||
bool Core::Archive::setFromTheBeginning(bool is)
|
||||
void Core::Archive::setFromTheBeginning(bool is)
|
||||
{
|
||||
if (!opened) {
|
||||
throw Closed("setFromTheBeginning", jid.toStdString());
|
||||
|
@ -448,3 +462,44 @@ bool Core::Archive::setFromTheBeginning(bool is)
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
|
|
@ -48,7 +48,7 @@ public:
|
|||
long unsigned int size() const;
|
||||
std::list<Shared::Message> getBefore(int count, const QString& id);
|
||||
bool isFromTheBeginning();
|
||||
bool setFromTheBeginning(bool is);
|
||||
void setFromTheBeginning(bool is);
|
||||
|
||||
public:
|
||||
const QString jid;
|
||||
|
@ -109,6 +109,8 @@ private:
|
|||
MDB_dbi stats;
|
||||
|
||||
bool _isFromTheBeginning();
|
||||
void printOrder();
|
||||
void printKeys();
|
||||
};
|
||||
|
||||
}
|
||||
|
|
119
core/contact.cpp
119
core/contact.cpp
|
@ -17,6 +17,7 @@
|
|||
*/
|
||||
|
||||
#include "contact.h"
|
||||
#include <QDebug>
|
||||
|
||||
Core::Contact::Contact(const QString& pJid, const QString& account, QObject* parent):
|
||||
QObject(parent),
|
||||
|
@ -152,7 +153,7 @@ void Core::Contact::performRequest(int count, const QString& before)
|
|||
|
||||
switch (archiveState) {
|
||||
case empty:
|
||||
emit needHistory(before, "", QDateTime(), QDateTime());
|
||||
emit needHistory(before, "");
|
||||
break;
|
||||
case chunk:
|
||||
case beginning:
|
||||
|
@ -160,33 +161,48 @@ void Core::Contact::performRequest(int count, const QString& before)
|
|||
requestCache.emplace_back(requestedCount, before);
|
||||
requestedCount = -1;
|
||||
}
|
||||
emit needHistory("", archive->newestId(), QDateTime(), QDateTime());
|
||||
emit needHistory("", archive->newestId());
|
||||
break;
|
||||
case end:
|
||||
if (count != -1) {
|
||||
bool found = requestFromArchive(before);
|
||||
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(), "", QDateTime(), QDateTime());
|
||||
emit needHistory(responseCache.front().getId(), "");
|
||||
} else {
|
||||
emit needHistory(before, "", QDateTime(), QDateTime());
|
||||
emit needHistory(before, "");
|
||||
}
|
||||
} else {
|
||||
nextRequest();
|
||||
}
|
||||
} else {
|
||||
requestCache.emplace_back(requestedCount, before);
|
||||
requestedCount = -1;
|
||||
emit needHistory(archive->oldestId(), "", QDateTime(), QDateTime());
|
||||
}
|
||||
} else {
|
||||
emit needHistory(archive->oldestId(), "", QDateTime(), QDateTime());
|
||||
emit needHistory(archive->oldestId(), "");
|
||||
}
|
||||
break;
|
||||
case complete:
|
||||
if (!requestFromArchive(before)) {
|
||||
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();
|
||||
|
@ -194,38 +210,6 @@ void Core::Contact::performRequest(int count, const QString& before)
|
|||
}
|
||||
}
|
||||
|
||||
bool Core::Contact::requestFromArchive(const QString& before) {
|
||||
std::list<Shared::Message> arc;
|
||||
QString lBefore;
|
||||
if (responseCache.size() > 0) {
|
||||
lBefore = responseCache.front().getId();
|
||||
} else {
|
||||
lBefore = before;
|
||||
}
|
||||
if (requestedCount != -1) {
|
||||
try {
|
||||
arc = archive->getBefore(requestedCount - responseCache.size(), lBefore);
|
||||
responseCache.insert(responseCache.begin(), arc.begin(), arc.end());
|
||||
return true;
|
||||
} catch (Archive::NotFound e) {
|
||||
requestCache.emplace_back(requestedCount, before);
|
||||
requestedCount = -1;
|
||||
emit needHistory(archive->oldestId(), "", QDateTime(), QDateTime());
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
arc = archive->getBefore(1, lBefore);
|
||||
//just do nothing since response is not required
|
||||
//may be even it's a signal that the history is now complete?
|
||||
return true;
|
||||
} catch (Archive::NotFound e) {
|
||||
emit needHistory(archive->oldestId(), "", QDateTime(), QDateTime());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Core::Contact::appendMessageToArchive(const Shared::Message& msg)
|
||||
{
|
||||
const QString& id = msg.getId();
|
||||
|
@ -267,8 +251,10 @@ void Core::Contact::appendMessageToArchive(const Shared::Message& msg)
|
|||
|
||||
void Core::Contact::flushMessagesToArchive(bool finished, const QString& firstId, const QString& lastId)
|
||||
{
|
||||
unsigned int added(0);
|
||||
if (hisoryCache.size() > 0) {
|
||||
archive->addElements(hisoryCache);
|
||||
added = archive->addElements(hisoryCache);
|
||||
qDebug() << "Added" << added << "messages to the archive";
|
||||
hisoryCache.clear();
|
||||
}
|
||||
|
||||
|
@ -279,14 +265,14 @@ void Core::Contact::flushMessagesToArchive(bool finished, const QString& firstId
|
|||
archiveState = complete;
|
||||
nextRequest();
|
||||
} else {
|
||||
emit needHistory("", lastId, QDateTime(), QDateTime());
|
||||
emit needHistory("", lastId);
|
||||
}
|
||||
case chunk:
|
||||
if (finished) {
|
||||
archiveState = end;
|
||||
nextRequest();
|
||||
} else {
|
||||
emit needHistory("", lastId, QDateTime(), QDateTime());
|
||||
emit needHistory("", lastId);
|
||||
}
|
||||
break;
|
||||
case empty:
|
||||
|
@ -303,20 +289,28 @@ void Core::Contact::flushMessagesToArchive(bool finished, const QString& firstId
|
|||
} else {
|
||||
before = requestedBefore;
|
||||
}
|
||||
if (!requestFromArchive(before)) {
|
||||
qDebug("Something went terrible wrong flushing messages to the archive");
|
||||
}
|
||||
if (requestedCount < responseCache.size()) {
|
||||
|
||||
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, "", QDateTime(), QDateTime());
|
||||
emit needHistory(firstId, "");
|
||||
}
|
||||
} else {
|
||||
nextRequest();
|
||||
}
|
||||
} else {
|
||||
nextRequest();
|
||||
if (added != 0) {
|
||||
nextRequest();
|
||||
} else {
|
||||
emit needHistory(firstId, "");
|
||||
}
|
||||
}
|
||||
break;
|
||||
case complete:
|
||||
|
@ -324,3 +318,24 @@ void Core::Contact::flushMessagesToArchive(bool finished, const QString& firstId
|
|||
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, "");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -54,6 +54,7 @@ public:
|
|||
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:
|
||||
void groupAdded(const QString& name);
|
||||
|
@ -61,7 +62,7 @@ signals:
|
|||
void nameChanged(const QString& name);
|
||||
void subscriptionStateChanged(Shared::SubscriptionState state);
|
||||
void historyResponse(const std::list<Shared::Message>& messages);
|
||||
void needHistory(const QString& before, const QString& after, const QDateTime& from, const QDateTime& to);
|
||||
void needHistory(const QString& before, const QString& after);
|
||||
|
||||
public:
|
||||
const QString jid;
|
||||
|
@ -84,7 +85,6 @@ private:
|
|||
private:
|
||||
void nextRequest();
|
||||
void performRequest(int count, const QString& before);
|
||||
bool requestFromArchive(const QString& before);
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -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>&)));
|
||||
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(responseArchive(const QString&, const std::list<Shared::Message>&)),
|
||||
this, SLOT(onAccountResponseArchive(const QString&, const std::list<Shared::Message>&)));
|
||||
|
||||
QMap<QString, QVariant> map = {
|
||||
{"login", login},
|
||||
|
@ -221,12 +223,19 @@ void Core::Squawk::sendMessage(const QString& account, const Shared::Message& da
|
|||
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);
|
||||
if (itr == amap.end()) {
|
||||
qDebug("An attempt to request an archive of non existing account, skipping");
|
||||
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);
|
||||
}
|
||||
|
||||
|
|
|
@ -36,6 +36,7 @@ signals:
|
|||
void removePresence(const QString& account, const QString& jid, const QString& name);
|
||||
void stateChanged(int state);
|
||||
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:
|
||||
void start();
|
||||
|
@ -45,7 +46,7 @@ public slots:
|
|||
void disconnectAccount(const QString& account);
|
||||
void changeState(int state);
|
||||
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:
|
||||
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 onAccountRemovePresence(const QString& jid, const QString& name);
|
||||
void onAccountMessage(const Shared::Message& data);
|
||||
void onAccountResponseArchive(const QString& jid, const std::list<Shared::Message>& list);
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue