forked from blue/squawk
Refactoring, account destruction fix, some thoughts about where to store contact settings (omemo enable status for instance)
This commit is contained in:
parent
283e9ebc4d
commit
fffef9876a
15 changed files with 352 additions and 380 deletions
|
@ -41,39 +41,33 @@ Core::RosterItem::RosterItem(const QString& pJid, const QString& pAccount, QObje
|
|||
archive->open(account);
|
||||
|
||||
if (archive->size() != 0) {
|
||||
if (archive->isFromTheBeginning()) {
|
||||
if (archive->isFromTheBeginning())
|
||||
archiveState = beginning;
|
||||
} else {
|
||||
else
|
||||
archiveState = chunk;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Core::RosterItem::~RosterItem()
|
||||
{
|
||||
Core::RosterItem::~RosterItem() {
|
||||
delete archive;
|
||||
}
|
||||
|
||||
Core::RosterItem::ArchiveState Core::RosterItem::getArchiveState() const
|
||||
{
|
||||
Core::RosterItem::ArchiveState Core::RosterItem::getArchiveState() const {
|
||||
return archiveState;
|
||||
}
|
||||
|
||||
QString Core::RosterItem::getName() const
|
||||
{
|
||||
QString Core::RosterItem::getName() const {
|
||||
return name;
|
||||
}
|
||||
|
||||
void Core::RosterItem::setName(const QString& n)
|
||||
{
|
||||
void Core::RosterItem::setName(const QString& n) {
|
||||
if (name != n) {
|
||||
name = n;
|
||||
emit nameChanged(name);
|
||||
}
|
||||
}
|
||||
|
||||
void Core::RosterItem::addMessageToArchive(const Shared::Message& msg)
|
||||
{
|
||||
void Core::RosterItem::addMessageToArchive(const Shared::Message& msg) {
|
||||
if (msg.storable()) {
|
||||
hisoryCache.push_back(msg);
|
||||
std::map<QString, Shared::Message>::iterator itr = toCorrect.find(msg.getId());
|
||||
|
@ -87,8 +81,7 @@ void Core::RosterItem::addMessageToArchive(const Shared::Message& msg)
|
|||
}
|
||||
}
|
||||
|
||||
void Core::RosterItem::correctMessageInArchive(const QString& originalId, const Shared::Message& msg)
|
||||
{
|
||||
void Core::RosterItem::correctMessageInArchive(const QString& originalId, const Shared::Message& msg) {
|
||||
if (msg.storable()) {
|
||||
QDateTime thisTime = msg.getTime();
|
||||
std::map<QString, Shared::Message>::iterator itr = toCorrect.find(originalId);
|
||||
|
@ -109,8 +102,7 @@ void Core::RosterItem::correctMessageInArchive(const QString& originalId, const
|
|||
}
|
||||
}
|
||||
|
||||
void Core::RosterItem::requestHistory(int count, const QString& before)
|
||||
{
|
||||
void Core::RosterItem::requestHistory(int count, const QString& before) {
|
||||
if (syncronizing) {
|
||||
requestCache.emplace_back(count, before);
|
||||
} else {
|
||||
|
@ -118,8 +110,7 @@ void Core::RosterItem::requestHistory(int count, const QString& before)
|
|||
}
|
||||
}
|
||||
|
||||
void Core::RosterItem::nextRequest()
|
||||
{
|
||||
void Core::RosterItem::nextRequest() {
|
||||
if (syncronizing) {
|
||||
if (requestedCount != -1) {
|
||||
bool last = false;
|
||||
|
@ -157,8 +148,7 @@ void Core::RosterItem::nextRequest()
|
|||
}
|
||||
}
|
||||
|
||||
void Core::RosterItem::performRequest(int count, const QString& before)
|
||||
{
|
||||
void Core::RosterItem::performRequest(int count, const QString& before) {
|
||||
syncronizing = true;
|
||||
requestedCount = count;
|
||||
requestedBefore = before;
|
||||
|
@ -246,8 +236,7 @@ void Core::RosterItem::performRequest(int count, const QString& before)
|
|||
}
|
||||
}
|
||||
|
||||
QString Core::RosterItem::getId(const Shared::Message& msg)
|
||||
{
|
||||
QString Core::RosterItem::getId(const Shared::Message& msg) {
|
||||
QString id;
|
||||
if (muc) {
|
||||
id = msg.getStanzaId();
|
||||
|
@ -257,8 +246,7 @@ QString Core::RosterItem::getId(const Shared::Message& msg)
|
|||
return id;
|
||||
}
|
||||
|
||||
void Core::RosterItem::appendMessageToArchive(const Shared::Message& msg)
|
||||
{
|
||||
void Core::RosterItem::appendMessageToArchive(const Shared::Message& msg) {
|
||||
if (msg.getId().size() > 0) {
|
||||
if (msg.storable()) {
|
||||
switch (archiveState) {
|
||||
|
@ -299,8 +287,7 @@ void Core::RosterItem::appendMessageToArchive(const Shared::Message& msg)
|
|||
}
|
||||
}
|
||||
|
||||
bool Core::RosterItem::changeMessage(const QString& id, const QMap<QString, QVariant>& data)
|
||||
{
|
||||
bool Core::RosterItem::changeMessage(const QString& id, const QMap<QString, QVariant>& data) {
|
||||
bool found = false;
|
||||
for (Shared::Message& msg : appendCache) {
|
||||
if (msg.getId() == id) {
|
||||
|
@ -341,8 +328,7 @@ bool Core::RosterItem::changeMessage(const QString& id, const QMap<QString, QVar
|
|||
return found;
|
||||
}
|
||||
|
||||
void Core::RosterItem::flushMessagesToArchive(bool finished, const QString& firstId, const QString& lastId)
|
||||
{
|
||||
void Core::RosterItem::flushMessagesToArchive(bool finished, const QString& firstId, const QString& lastId) {
|
||||
unsigned int added(0);
|
||||
if (hisoryCache.size() > 0) {
|
||||
added = archive->addElements(hisoryCache);
|
||||
|
@ -429,51 +415,43 @@ void Core::RosterItem::flushMessagesToArchive(bool finished, const QString& firs
|
|||
}
|
||||
}
|
||||
|
||||
QString Core::RosterItem::getServer() const
|
||||
{
|
||||
QString Core::RosterItem::getServer() const {
|
||||
QStringList lst = jid.split("@");
|
||||
return lst.back();
|
||||
}
|
||||
|
||||
bool Core::RosterItem::isMuc() const
|
||||
{
|
||||
bool Core::RosterItem::isMuc() const {
|
||||
return muc;
|
||||
}
|
||||
|
||||
QString Core::RosterItem::avatarPath(const QString& resource) const
|
||||
{
|
||||
QString Core::RosterItem::avatarPath(const QString& resource) const {
|
||||
QString path = folderPath() + "/" + (resource.size() == 0 ? jid : resource);
|
||||
return path;
|
||||
}
|
||||
|
||||
QString Core::RosterItem::folderPath() const
|
||||
{
|
||||
QString Core::RosterItem::folderPath() const {
|
||||
QString path(QStandardPaths::writableLocation(QStandardPaths::CacheLocation));
|
||||
path += "/" + account + "/" + jid;
|
||||
return path;
|
||||
}
|
||||
|
||||
bool Core::RosterItem::setAvatar(const QByteArray& data, Archive::AvatarInfo& info, const QString& resource)
|
||||
{
|
||||
bool Core::RosterItem::setAvatar(const QByteArray& data, Archive::AvatarInfo& info, const QString& resource) {
|
||||
bool result = archive->setAvatar(data, info, false, resource);
|
||||
if (resource.size() == 0 && result) {
|
||||
if (data.size() == 0) {
|
||||
if (data.size() == 0)
|
||||
emit avatarChanged(Shared::Avatar::empty, "");
|
||||
} else {
|
||||
else
|
||||
emit avatarChanged(Shared::Avatar::valid, avatarPath(resource) + "." + info.type);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
bool Core::RosterItem::setAutoGeneratedAvatar(const QString& resource)
|
||||
{
|
||||
bool Core::RosterItem::setAutoGeneratedAvatar(const QString& resource) {
|
||||
Archive::AvatarInfo info;
|
||||
return setAutoGeneratedAvatar(info, resource);
|
||||
}
|
||||
|
||||
bool Core::RosterItem::setAutoGeneratedAvatar(Archive::AvatarInfo& info, const QString& resource)
|
||||
{
|
||||
bool Core::RosterItem::setAutoGeneratedAvatar(Archive::AvatarInfo& info, const QString& resource) {
|
||||
QImage image(96, 96, QImage::Format_ARGB32_Premultiplied);
|
||||
QPainter painter(&image);
|
||||
quint8 colorIndex = rand() % Shared::colorPalette.size();
|
||||
|
@ -483,11 +461,11 @@ bool Core::RosterItem::setAutoGeneratedAvatar(Archive::AvatarInfo& info, const Q
|
|||
f.setBold(true);
|
||||
f.setPixelSize(72);
|
||||
painter.setFont(f);
|
||||
if (bg.lightnessF() > 0.5) {
|
||||
if (bg.lightnessF() > 0.5)
|
||||
painter.setPen(Qt::black);
|
||||
} else {
|
||||
else
|
||||
painter.setPen(Qt::white);
|
||||
}
|
||||
|
||||
painter.drawText(image.rect(), Qt::AlignCenter | Qt::AlignVCenter, resource.size() == 0 ? jid.at(0).toUpper() : resource.at(0).toUpper());
|
||||
QByteArray arr;
|
||||
QBuffer stream(&arr);
|
||||
|
@ -495,19 +473,17 @@ bool Core::RosterItem::setAutoGeneratedAvatar(Archive::AvatarInfo& info, const Q
|
|||
image.save(&stream, "PNG");
|
||||
stream.close();
|
||||
bool result = archive->setAvatar(arr, info, true, resource);
|
||||
if (resource.size() == 0 && result) {
|
||||
if (resource.size() == 0 && result)
|
||||
emit avatarChanged(Shared::Avatar::autocreated, avatarPath(resource) + ".png");
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool Core::RosterItem::readAvatarInfo(Archive::AvatarInfo& target, const QString& resource) const
|
||||
{
|
||||
bool Core::RosterItem::readAvatarInfo(Archive::AvatarInfo& target, const QString& resource) const {
|
||||
return archive->readAvatarInfo(target, resource);
|
||||
}
|
||||
|
||||
void Core::RosterItem::handleResponseVCard(const QXmppVCardIq& card, const QString& resource, Shared::VCard& vCard)
|
||||
{
|
||||
void Core::RosterItem::handleResponseVCard(const QXmppVCardIq& card, const QString& resource, Shared::VCard& vCard) {
|
||||
Archive::AvatarInfo info;
|
||||
Archive::AvatarInfo newInfo;
|
||||
bool hasAvatar = readAvatarInfo(info, resource);
|
||||
|
@ -532,9 +508,9 @@ void Core::RosterItem::handleResponseVCard(const QXmppVCardIq& card, const QStri
|
|||
}
|
||||
}
|
||||
} else {
|
||||
if (!hasAvatar || !info.autogenerated) {
|
||||
if (!hasAvatar || !info.autogenerated)
|
||||
setAutoGeneratedAvatar(resource);
|
||||
}
|
||||
|
||||
type = Shared::Avatar::autocreated;
|
||||
path = avatarPath(resource) + ".png";
|
||||
}
|
||||
|
@ -542,13 +518,11 @@ void Core::RosterItem::handleResponseVCard(const QXmppVCardIq& card, const QStri
|
|||
vCard.setAvatarType(type);
|
||||
vCard.setAvatarPath(path);
|
||||
|
||||
if (resource.size() == 0) {
|
||||
if (resource.size() == 0)
|
||||
emit avatarChanged(vCard.getAvatarType(), vCard.getAvatarPath());
|
||||
}
|
||||
}
|
||||
|
||||
void Core::RosterItem::clearArchiveRequests()
|
||||
{
|
||||
void Core::RosterItem::clearArchiveRequests() {
|
||||
syncronizing = false;
|
||||
requestedCount = 0;
|
||||
requestedBefore = "";
|
||||
|
@ -564,30 +538,72 @@ void Core::RosterItem::clearArchiveRequests()
|
|||
requestCache.clear();
|
||||
}
|
||||
|
||||
void Core::RosterItem::downgradeDatabaseState()
|
||||
{
|
||||
if (archiveState == ArchiveState::complete) {
|
||||
void Core::RosterItem::downgradeDatabaseState() {
|
||||
if (archiveState == ArchiveState::complete)
|
||||
archiveState = ArchiveState::beginning;
|
||||
}
|
||||
|
||||
|
||||
if (archiveState == ArchiveState::end) {
|
||||
if (archiveState == ArchiveState::end)
|
||||
archiveState = ArchiveState::chunk;
|
||||
}
|
||||
}
|
||||
|
||||
Shared::Message Core::RosterItem::getMessage(const QString& id)
|
||||
{
|
||||
Shared::Message Core::RosterItem::getMessage(const QString& id) {
|
||||
for (const Shared::Message& msg : appendCache) {
|
||||
if (msg.getId() == id) {
|
||||
if (msg.getId() == id)
|
||||
return msg;
|
||||
}
|
||||
}
|
||||
|
||||
for (Shared::Message& msg : hisoryCache) {
|
||||
if (msg.getId() == id) {
|
||||
if (msg.getId() == id)
|
||||
return msg;
|
||||
}
|
||||
}
|
||||
|
||||
return archive->getElement(id);
|
||||
}
|
||||
|
||||
bool Core::RosterItem::isEncryptionEnabled() const {
|
||||
return archive->isEncryptionEnabled();
|
||||
}
|
||||
|
||||
void Core::RosterItem::enableEncryption(bool value) {
|
||||
bool changed = archive->setEncryptionEnabled(value);
|
||||
if (changed)
|
||||
emit encryptionChanged(value);
|
||||
}
|
||||
|
||||
QMap<QString, QVariant> Core::RosterItem::getInfo() const {
|
||||
QMap<QString, QVariant> result({
|
||||
{"name", name},
|
||||
{"encryption", isEncryptionEnabled()},
|
||||
});
|
||||
Archive::AvatarInfo info;
|
||||
bool hasAvatar = readAvatarInfo(info);
|
||||
careAboutAvatar(hasAvatar, info, result);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
void Core::RosterItem::careAboutAvatar (
|
||||
bool hasAvatar,
|
||||
const Archive::AvatarInfo& info,
|
||||
QMap<QString, QVariant>& output,
|
||||
const QString& resource,
|
||||
const QString& subject
|
||||
) const {
|
||||
if (hasAvatar) {
|
||||
if (info.autogenerated)
|
||||
output.insert("avatarState", QVariant::fromValue(Shared::Avatar::autocreated));
|
||||
else
|
||||
output.insert("avatarState", QVariant::fromValue(Shared::Avatar::valid));
|
||||
|
||||
output.insert("avatarPath", avatarPath(resource) + "." + info.type);
|
||||
} else {
|
||||
output.insert("avatarState", QVariant::fromValue(Shared::Avatar::empty));
|
||||
output.insert("avatarPath", "");
|
||||
if (subject.size() == 0)
|
||||
emit requestVCard(jid);
|
||||
else
|
||||
emit requestVCard(subject);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue