From 927bdf0dab1e38b8e5a79ee499d20ede4c570d0e Mon Sep 17 00:00:00 2001 From: blue Date: Fri, 10 Mar 2023 21:43:31 +0300 Subject: [PATCH] DONT TAKE, BROKEN! first application of delay manager in code, reception of bundles --- core/account.cpp | 25 ++++-- core/account.h | 6 ++ core/delayManager/CMakeLists.txt | 4 +- .../{delaymanager.cpp => manager.cpp} | 48 ++++++----- .../{delaymanager.h => manager.h} | 17 ++-- core/handlers/omemohandler.cpp | 48 +++++++++++ core/handlers/omemohandler.h | 10 ++- core/handlers/rosterhandler.cpp | 4 +- core/handlers/rosterhandler.h | 1 + core/handlers/trusthandler.cpp | 51 ++++++------ core/handlers/vcardhandler.cpp | 79 ++++--------------- core/handlers/vcardhandler.h | 4 - main/CMakeLists.txt | 16 +++- main/main.cpp | 16 ++-- main/root.cpp | 43 ++++++++++ main/root.h | 28 +++++++ 16 files changed, 261 insertions(+), 139 deletions(-) rename core/delayManager/{delaymanager.cpp => manager.cpp} (90%) rename core/delayManager/{delaymanager.h => manager.h} (90%) create mode 100644 main/root.cpp create mode 100644 main/root.h diff --git a/core/account.cpp b/core/account.cpp index d158970..c27f2ea 100644 --- a/core/account.cpp +++ b/core/account.cpp @@ -55,6 +55,7 @@ Account::Account(const QString& p_login, const QString& p_server, const QString& reconnectScheduled(false), reconnectTimer(new QTimer), network(p_net), + delay(nullptr), passwordType(Shared::AccountPassword::plain), lastError(Error::none), pepSupport(Shared::Support::unknown), @@ -67,6 +68,12 @@ Account::Account(const QString& p_login, const QString& p_server, const QString& config.setPassword(p_password); config.setAutoAcceptSubscriptions(true); //config.setAutoReconnectionEnabled(false); + delay = new DelayManager::Manager(getBareJid()); + QObject::connect(delay, &DelayManager::Manager::gotInfo, this, &Account::infoReady); + QObject::connect(delay, &DelayManager::Manager::gotOwnInfo, this, &Account::infoReady); + + QObject::connect(delay, &DelayManager::Manager::requestOwnVCard, vm, &QXmppVCardManager::requestClientVCard); + QObject::connect(delay, &DelayManager::Manager::requestVCard, vm, &QXmppVCardManager::requestVCard); rh->initialize(); vh->initialize(); @@ -104,6 +111,9 @@ Account::Account(const QString& p_login, const QString& p_server, const QString& client.addExtension(psm); #ifdef WITH_OMEMO + QObject::connect(delay, &DelayManager::Manager::requestBundles, oh, &OmemoHandler::requestBundles); + QObject::connect(delay, &DelayManager::Manager::requestOwnBundles, oh, &OmemoHandler::requestOwnBundles); + client.addExtension(tm); client.addExtension(om); om->setSecurityPolicy(QXmpp::Toakafa); @@ -164,6 +174,7 @@ Account::~Account() { delete mm; delete am; delete cm; + delete delay; } Shared::ConnectionState Core::Account::getState() const { @@ -313,7 +324,6 @@ void Core::Account::runDiscoveryService() { dm->requestInfo(getServer()); } - void Core::Account::onPresenceReceived(const QXmppPresence& p_presence) { QString id = p_presence.from(); QStringList comps = id.split("/"); @@ -642,9 +652,9 @@ void Core::Account::setPepSupport(Shared::Support support) { void Core::Account::handleDisconnection() { setPepSupport(Shared::Support::unknown); + delay->disconnected(); cm->setCarbonsEnabled(false); rh->handleOffline(); - vh->handleOffline(); archiveQueries.clear(); } @@ -702,7 +712,9 @@ void Core::Account::setPasswordType(Shared::AccountPassword pt) { passwordType = pt; } void Core::Account::setLogin(const QString& p_login) { - config.setUser(p_login);} + config.setUser(p_login); + delay->setOwnJid(getBareJid()); +} void Core::Account::setName(const QString& p_name) { name = p_name;} @@ -713,7 +725,9 @@ void Core::Account::setPassword(const QString& p_password) { } void Core::Account::setServer(const QString& p_server) { - config.setDomain(p_server);} + config.setDomain(p_server); + delay->setOwnJid(getBareJid()); +} void Core::Account::sendMessage(const Shared::Message& data) { mh->sendMessage(data);} @@ -730,7 +744,8 @@ void Core::Account::replaceMessage(const QString& originalId, const Shared::Mess void Core::Account::requestInfo(const QString& jid) { //TODO switch case of what kind of entity this info request is about //right now it could be only about myself or some contact - vh->requestVCard(jid); + delay->getInfo(jid); + //vh->requestVCard(jid); } void Core::Account::updateInfo(const Shared::Info& info) { diff --git a/core/account.h b/core/account.h index 598a06e..e18966d 100644 --- a/core/account.h +++ b/core/account.h @@ -50,6 +50,7 @@ #include "contact.h" #include "conference.h" #include +#include #include "handlers/messagehandler.h" #include "handlers/rosterhandler.h" @@ -73,6 +74,10 @@ class Account : public QObject friend class RosterHandler; friend class VCardHandler; friend class DiscoveryHandler; +#ifdef WITH_OMEMO + friend class OmemoHandler; + friend class TrustHandler; +#endif public: enum class Error { authentication, @@ -202,6 +207,7 @@ private: QTimer* reconnectTimer; NetworkAccess* network; + DelayManager::Manager* delay; Shared::AccountPassword passwordType; Error lastError; Shared::Support pepSupport; diff --git a/core/delayManager/CMakeLists.txt b/core/delayManager/CMakeLists.txt index 96f4cef..6d046c5 100644 --- a/core/delayManager/CMakeLists.txt +++ b/core/delayManager/CMakeLists.txt @@ -1,5 +1,5 @@ set(SOURCE_FILES - delaymanager.cpp + manager.cpp job.cpp cardinternal.cpp infoforuser.cpp @@ -10,7 +10,7 @@ set(SOURCE_FILES ) set(HEADER_FILES - delaymanager.h + manager.h job.h cardinternal.h infoforuser.h diff --git a/core/delayManager/delaymanager.cpp b/core/delayManager/manager.cpp similarity index 90% rename from core/delayManager/delaymanager.cpp rename to core/delayManager/manager.cpp index 0ecc9a4..6a15544 100644 --- a/core/delayManager/delaymanager.cpp +++ b/core/delayManager/manager.cpp @@ -14,7 +14,9 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -#include "delaymanager.h" +#include "manager.h" + +#include #include "cardinternal.h" #include "infoforuser.h" @@ -59,6 +61,9 @@ Core::DelayManager::Job::Id Core::DelayManager::Manager::getNextJobId() { } void Core::DelayManager::Manager::getInfo(const QString& jid) { + if (jid == ownJid) + return getOwnInfo(); + Job* job = nullptr; #ifdef WITH_OMEMO std::map::const_iterator bitr = requestedBundles.find(jid); @@ -91,10 +96,14 @@ void Core::DelayManager::Manager::getVCard(const QString& jid) { } void Core::DelayManager::Manager::getOwnVCard() { - if (ownInfoJobId == 0) + if (ownVCardJobId == 0) scheduleJob(new OwnCardInternal(getNextJobId())); } +bool Core::DelayManager::Manager::isOwnVCardPending() const { + return ownVCardJobId != 0; +} + Core::DelayManager::Job* Core::DelayManager::Manager::getVCardJob(const QString& jid) { Job* job = nullptr; std::map::const_iterator sitr = scheduledVCards.find(jid); @@ -207,7 +216,7 @@ void Core::DelayManager::Manager::jobIsCanceled(Job* job, bool wasRunning) { else scheduledVCards.erase(jb->jid); - emit receivedVCard(jb->jid, Shared::VCard()); + emit gotVCard(jb->jid, Shared::VCard()); } break; case Job::Type::infoForUser: { @@ -219,7 +228,7 @@ void Core::DelayManager::Manager::jobIsCanceled(Job* job, bool wasRunning) { else scheduledVCards.erase(jb->jid); - emit receivedVCard(jb->jid, Shared::VCard()); + emit gotVCard(jb->jid, Shared::VCard()); break; case InfoForUser::Stage::waitingForBundles: requestedBundles.erase(jb->jid); @@ -227,23 +236,23 @@ void Core::DelayManager::Manager::jobIsCanceled(Job* job, bool wasRunning) { default: break; } - emit receivedInfo(Shared::Info(jb->jid)); + emit gotInfo(Shared::Info(jb->jid)); } break; case Job::Type::ownInfoForUser: { OwnInfoForUser* jb = dynamic_cast(job); if (jb->getStage() == OwnInfoForUser::Stage::waitingForVCard) { ownVCardJobId = 0; - emit receivedOwnCard(Shared::VCard()); + emit gotOwnVCard(Shared::VCard()); } ownInfoJobId = 0; - emit receivedOwnInfo(Shared::Info (ownJid)); + emit gotOwnInfo(Shared::Info (ownJid)); } break; case Job::Type::ownCardInternal: ownVCardJobId = 0; - emit receivedOwnCard(Shared::VCard()); + emit gotOwnVCard(Shared::VCard()); break; } @@ -264,7 +273,8 @@ void Core::DelayManager::Manager::disconnected() { void Core::DelayManager::Manager::receivedVCard(const QString& jid, const Shared::VCard& card) { std::map::const_iterator cardItr = requestedVCards.find(jid); if (cardItr == requestedVCards.end()) { - throw 8575; //never supposed to happen, the state is not correct; + qDebug() << "received VCard for" << jid << "but it was never requested through manager, ignoring"; + return; } Job::Id jobId = cardItr->second; requestedVCards.erase(cardItr); @@ -272,7 +282,7 @@ void Core::DelayManager::Manager::receivedVCard(const QString& jid, const Shared switch (job->type) { case Job::Type::cardInternal: jobIsDone(jobId); - emit receivedCard(jid, card); + emit gotVCard(jid, card); break; case Job::Type::infoForUser: { #ifdef WITH_OMEMO @@ -283,10 +293,10 @@ void Core::DelayManager::Manager::receivedVCard(const QString& jid, const Shared #else Shared::Info info(jid); info.turnIntoContact(card); - emit receivedInfo(info); + emit gotInfo(info); jobIsDone(jobId); #endif - emit receivedCard(jid, card); + emit gotVCard(jid, card); } break; default: @@ -294,27 +304,27 @@ void Core::DelayManager::Manager::receivedVCard(const QString& jid, const Shared } } -void Core::DelayManager::Manager::ownVCardReceived(const Shared::VCard& card) { +void Core::DelayManager::Manager::receivedOwnVCard(const Shared::VCard& card) { Job::Id jobId = ownVCardJobId; ownVCardJobId = 0; Job* job = runningJobs.at(jobId); switch (job->type) { case Job::Type::ownCardInternal: jobIsDone(jobId); - emit receivedOwnCard(card); + emit gotOwnVCard(card); break; case Job::Type::ownInfoForUser: { #ifdef WITH_OMEMO OwnInfoForUser* jb = dynamic_cast(job); jb->receivedVCard(card); - emit requestOwnBundle(); + emit requestOwnBundles(); #else Shared::Info info(ownJid); info.turnIntoOwnAccount(card); - emit receivedOwnInfo(info); + emit gotOwnInfo(info); jobIsDone(jobId); #endif - emit receivedOwnCard(card); + emit gotOwnVCard(card); } break; default: @@ -335,7 +345,7 @@ void Core::DelayManager::Manager::receivedBundles(const QString& jid, const std: Shared::Info info(jid); info.turnIntoContact(job->claim(), new std::list(keys)); - emit receivedInfo(info); + emit gotInfo(info); jobIsDone(jobId); } @@ -347,7 +357,7 @@ void Core::DelayManager::Manager::receivedOwnBundles(const std::listclaim(), new std::list(keys)); - emit receivedOwnInfo(info); + emit gotOwnInfo(info); jobIsDone(jobId); } diff --git a/core/delayManager/delaymanager.h b/core/delayManager/manager.h similarity index 90% rename from core/delayManager/delaymanager.h rename to core/delayManager/manager.h index d859a18..61920ec 100644 --- a/core/delayManager/delaymanager.h +++ b/core/delayManager/manager.h @@ -43,26 +43,29 @@ public: Manager(const QString& ownJid, Job::Id maxParallelJobs = 5, QObject* parent = nullptr); ~Manager(); + void setOwnJid(const QString& jid); + bool isOwnVCardPending() const; + +public slots: void getOwnVCard(); void getOwnInfo(); void getVCard(const QString& jid); void getInfo(const QString& jid); - void setOwnJid(const QString& jid); signals: void requestVCard(const QString& jid); void requestOwnVCard(); void requestBundles(const QString& jid); - void requestOwnBundle(); + void requestOwnBundles(); - void receivedCard(const QString& jid, const Shared::VCard& info); - void receivedOwnCard(const Shared::VCard& info); - void receivedInfo(const Shared::Info& info); - void receivedOwnInfo(const Shared::Info& info); + void gotVCard(const QString& jid, const Shared::VCard& info); + void gotOwnVCard(const Shared::VCard& info); + void gotInfo(const Shared::Info& info); + void gotOwnInfo(const Shared::Info& info); public slots: void disconnected(); - void ownVCardReceived(const Shared::VCard& card); + void receivedOwnVCard(const Shared::VCard& card); void receivedVCard(const QString& jid, const Shared::VCard& card); void receivedBundles(const QString& jid, const std::list& keys); void receivedOwnBundles(const std::list& keys); diff --git a/core/handlers/omemohandler.cpp b/core/handlers/omemohandler.cpp index ce82a96..79eded9 100644 --- a/core/handlers/omemohandler.cpp +++ b/core/handlers/omemohandler.cpp @@ -19,7 +19,10 @@ #include "core/account.h" #include "core/adapterfunctions.h" +constexpr const char* ns_omemo_2 = "urn:xmpp:omemo:2"; + Core::OmemoHandler::OmemoHandler(Account* account) : + QObject(), QXmppOmemoStorage(), acc(account), ownDevice(std::nullopt), @@ -166,6 +169,51 @@ void Core::OmemoHandler::getDevices(const QString& jid, std::list task = acc->om->buildMissingSessions({jid}); + task.then(this, std::bind(&OmemoHandler::onBundlesReceived, this, jid)); +} + +void Core::OmemoHandler::requestOwnBundles() { + QXmppTask task = acc->om->buildMissingSessions({acc->getBareJid()}); + task.then(this, std::bind(&OmemoHandler::onOwnBundlesReceived, this)); +} + +void Core::OmemoHandler::onBundlesReceived(const QString& jid) { + std::list keys; + acc->oh->getDevices(jid, keys); + std::map trustLevels = acc->th->getKeys(ns_omemo_2, jid); + + qDebug() << "OMEMO info for " << jid << " devices:" << keys.size() << ", trustLevels:" << trustLevels.size(); + for (Shared::KeyInfo& key : keys) { + std::map::const_iterator itr = trustLevels.find(key.fingerPrint); + if (itr != trustLevels.end()) { + key.trustLevel = itr->second; + qDebug() << "Found a trust level for a device!"; + } + } + + acc->delay->receivedBundles(jid, keys); +} + +void Core::OmemoHandler::onOwnBundlesReceived() { + QString jid = acc->getBareJid(); + std::list keys; + acc->oh->getDevices(jid, keys); + std::map trustLevels = acc->th->getKeys(ns_omemo_2, jid); + + qDebug() << "OMEMO info for " << jid << " devices:" << keys.size() << ", trustLevels:" << trustLevels.size(); + for (Shared::KeyInfo& key : keys) { + std::map::const_iterator itr = trustLevels.find(key.fingerPrint); + if (itr != trustLevels.end()) { + key.trustLevel = itr->second; + qDebug() << "Found a trust level for a device!"; + } + } + + acc->delay->receivedOwnBundles(keys); +} + QDataStream & operator >> (QDataStream& in, QXmppOmemoStorage::Device& device) { in >> device.label; diff --git a/core/handlers/omemohandler.h b/core/handlers/omemohandler.h index 050677c..0d1021b 100644 --- a/core/handlers/omemohandler.h +++ b/core/handlers/omemohandler.h @@ -19,6 +19,7 @@ #include #include +#include #include #include @@ -32,8 +33,9 @@ Q_DECLARE_METATYPE(QXmppOmemoStorage::Device); namespace Core { class Account; -class OmemoHandler : public QXmppOmemoStorage +class OmemoHandler :public QObject, public QXmppOmemoStorage { + Q_OBJECT public: typedef std::pair SignedPreKeyPair; @@ -58,8 +60,14 @@ public: bool hasOwnDevice(); + void requestBundles(const QString& jid); + void requestOwnBundles(); void getDevices(const QString& jid, std::list& out) const; +private slots: + void onBundlesReceived(const QString& jid); + void onOwnBundlesReceived(); + private: Account* acc; std::optional ownDevice; diff --git a/core/handlers/rosterhandler.cpp b/core/handlers/rosterhandler.cpp index 0027514..cccd47d 100644 --- a/core/handlers/rosterhandler.cpp +++ b/core/handlers/rosterhandler.cpp @@ -156,7 +156,7 @@ void Core::RosterHandler::careAboutAvatar(Core::RosterItem* item, QMapvh->requestVCard(item->jid); + acc->delay->requestVCard(item->jid); } } @@ -197,7 +197,7 @@ void Core::RosterHandler::handleNewRosterItem(Core::RosterItem* contact) connect(contact, &RosterItem::historyResponse, this->acc, &Account::onContactHistoryResponse); connect(contact, &RosterItem::nameChanged, this, &RosterHandler::onContactNameChanged); connect(contact, &RosterItem::avatarChanged, this, &RosterHandler::onContactAvatarChanged); - connect(contact, &RosterItem::requestVCard, this->acc->vh, &VCardHandler::requestVCard); + connect(contact, &RosterItem::requestVCard, acc->delay, &DelayManager::Manager::getVCard); } void Core::RosterHandler::handleNewContact(Core::Contact* contact) diff --git a/core/handlers/rosterhandler.h b/core/handlers/rosterhandler.h index 11525be..62a7b8b 100644 --- a/core/handlers/rosterhandler.h +++ b/core/handlers/rosterhandler.h @@ -36,6 +36,7 @@ #include #include #include +#include namespace Core { diff --git a/core/handlers/trusthandler.cpp b/core/handlers/trusthandler.cpp index 50fbcbf..eb75219 100644 --- a/core/handlers/trusthandler.cpp +++ b/core/handlers/trusthandler.cpp @@ -89,8 +89,12 @@ QXmppTask Core::TrustHandler::trustLevel( const QString& keyOwnerJid, const QByteArray& keyId) { - Keys map = getCache(encryption)->getRecord(keyOwnerJid); - Shared::TrustLevel level = map.at(keyId); + KeyCache* cache = getCache(encryption); + Shared::TrustLevel level = Shared::TrustLevel::undecided; + try { + Keys map = cache->getRecord(keyOwnerJid); + level = map.at(keyId); + } catch (const DataBase::NotFound& e) {} return Core::makeReadyTask(std::move(convert(level))); } @@ -134,15 +138,22 @@ QXmppTask>> Core::TrustHandler::s for (MultySB::const_iterator itr = keyIds.begin(), end = keyIds.end(); itr != end; ++itr) { const QString& keyOwnerJid = itr.key(); const QByteArray& keyId = itr.value(); - Keys map = cache->getRecord(keyOwnerJid); - std::pair result = map.insert(std::make_pair(keyId, level)); - if (result.second) { + try { + Keys map = cache->getRecord(keyOwnerJid); + std::pair result = map.insert(std::make_pair(keyId, level)); + bool changed = result.second; + if (!changed && result.first->second != level) { + result.first->second = level; + changed = true; + } + if (changed) { + modifiedKeys[encryption].insert(keyOwnerJid, keyId); + cache->changeRecord(keyOwnerJid, map); + } + } catch (const DataBase::NotFound& e) { + Keys map({{keyId, level}}); modifiedKeys[encryption].insert(keyOwnerJid, keyId); - cache->changeRecord(keyOwnerJid, map); - } else if (result.first->second != level) { - result.first->second = level; - modifiedKeys[encryption].insert(keyOwnerJid, keyId); - cache->changeRecord(keyOwnerJid, map); + cache->addRecord(keyOwnerJid, map); } } return Core::makeReadyTask(std::move(modifiedKeys)); @@ -199,9 +210,8 @@ QXmppTask>> TrustHandle for (const std::pair& value : storage) { for (const std::pair& pair : value.second) { QXmpp::TrustLevel level = convert(pair.second); - if (!trustLevels || trustLevels.testFlag(level)) { + if (!trustLevels || trustLevels.testFlag(level)) res[level].insert(value.first, pair.first); - } } } return Core::makeReadyTask(std::move(res)); @@ -219,9 +229,8 @@ QXmppTask TrustHandler::removeKeys(const QString& encryption, const QStrin QXmppTask TrustHandler::removeKeys(const QString& encryption, const QList& keyIds) { std::set set; - for (const QByteArray& keyId : keyIds) { + for (const QByteArray& keyId : keyIds) set.insert(keyId); - } KeyCache* cache = getCache(encryption); std::map data = cache->readAll(); @@ -233,19 +242,16 @@ QXmppTask TrustHandler::removeKeys(const QString& encryption, const QList< if (set.erase(keyId)) { byOwner.erase(itr++); changed = true; - } else { + } else ++itr; - } } - if (byOwner.size() > 0) { + if (byOwner.size() > 0) data.erase(cItr++); - } else { + else ++cItr; - } } - if (changed) { + if (changed) cache->replaceAll(data); - } return Core::makeReadyTask(); } @@ -266,9 +272,8 @@ QXmppTask TrustHandler::addKeys( } catch (const DataBase::NotFound& e) {} for (const QByteArray& keyId : keyIds) { std::pair result = data.insert(std::make_pair(keyId, level)); - if (!result.second) { + if (!result.second) result.first->second = level; - } } if (had) { diff --git a/core/handlers/vcardhandler.cpp b/core/handlers/vcardhandler.cpp index 6c5cb5b..d4125e8 100644 --- a/core/handlers/vcardhandler.cpp +++ b/core/handlers/vcardhandler.cpp @@ -17,13 +17,9 @@ #include "vcardhandler.h" #include "core/account.h" -constexpr const char* ns_omemo_2 = "urn:xmpp:omemo:2"; - Core::VCardHandler::VCardHandler(Account* account): QObject(), acc(account), - ownVCardRequestInProgress(false), - pendingVCardRequests(), avatarHash(), avatarType() { @@ -90,36 +86,20 @@ void Core::VCardHandler::onVCardReceived(const QXmppVCardIq& card) { if (comps.size() > 1) { resource = comps.back(); } - pendingVCardRequests.erase(id); RosterItem* item = acc->rh->getRosterItem(jid); if (item == 0) { - if (jid == acc->getBareJid()) { + if (jid == acc->getBareJid()) onOwnVCardReceived(card); - } else { + else qDebug() << "received vCard" << jid << "doesn't belong to any of known contacts or conferences, skipping"; - } + return; } - Shared::Info info(jid, Shared::EntryType::contact); - item->handleResponseVCard(card, resource, info.getVCardRef()); -#ifdef WITH_OMEMO - std::list& aks = info.getActiveKeysRef(); - acc->oh->getDevices(jid, aks); - std::map trustLevels = acc->th->getKeys(ns_omemo_2, jid); - - qDebug() << "OMEMO info for " << jid << " devices:" << aks.size() << ", trustLevels:" << trustLevels.size(); - for (Shared::KeyInfo& key : aks) { - std::map::const_iterator itr = trustLevels.find(key.fingerPrint); - if (itr != trustLevels.end()) { - key.trustLevel = itr->second; - qDebug() << "Found a trust level for a device!"; - } - } -#endif - - emit acc->infoReady(info); + Shared::VCard vCard; + item->handleResponseVCard(card, resource, vCard); + acc->delay->receivedVCard(id, vCard); } void Core::VCardHandler::onOwnVCardReceived(const QXmppVCardIq& card) { @@ -201,10 +181,7 @@ void Core::VCardHandler::onOwnVCardReceived(const QXmppVCardIq& card) { emit acc->changed(change); } - ownVCardRequestInProgress = false; - - Shared::Info info(acc->getBareJid(), Shared::EntryType::ownAccount); - Shared::VCard& vCard = info.getVCardRef(); + Shared::VCard vCard; initializeVCard(vCard, card); if (avatarType.size() > 0) { @@ -214,52 +191,24 @@ void Core::VCardHandler::onOwnVCardReceived(const QXmppVCardIq& card) { vCard.setAvatarType(Shared::Avatar::empty); } - emit acc->infoReady(info); -} - -void Core::VCardHandler::handleOffline() { - pendingVCardRequests.clear(); - for (const QString& jid : pendingVCardRequests) { - Shared::Info info(jid, Shared::EntryType::none); - emit acc->infoReady(info); //need to show it better in the future, like with an error - } - pendingVCardRequests.clear(); - ownVCardRequestInProgress = false; -} - -void Core::VCardHandler::requestVCard(const QString& jid) { - if (pendingVCardRequests.find(jid) == pendingVCardRequests.end()) { - qDebug() << "requesting vCard" << jid; - if (jid == acc->getBareJid()) { - if (!ownVCardRequestInProgress) { - acc->vm->requestClientVCard(); - ownVCardRequestInProgress = true; - } - } else { - acc->vm->requestVCard(jid); - pendingVCardRequests.insert(jid); - } - } + if (acc->delay->isOwnVCardPending()) + acc->delay->receivedOwnVCard(vCard); } void Core::VCardHandler::handlePresenceOfMyAccountChange(const QXmppPresence& p_presence) { - if (!ownVCardRequestInProgress) { + if (!acc->delay->isOwnVCardPending()) { switch (p_presence.vCardUpdateType()) { case QXmppPresence::VCardUpdateNone: //this presence has nothing to do with photo break; case QXmppPresence::VCardUpdateNotReady: //let's say the photo didn't change here break; case QXmppPresence::VCardUpdateNoPhoto: //there is no photo, need to drop if any - if (avatarType.size() > 0) { - acc->vm->requestClientVCard(); - ownVCardRequestInProgress = true; - } + if (avatarType.size() > 0) + acc->delay->getOwnVCard(); break; case QXmppPresence::VCardUpdateValidPhoto: //there is a photo, need to load - if (avatarHash != p_presence.photoHash()) { - acc->vm->requestClientVCard(); - ownVCardRequestInProgress = true; - } + if (avatarHash != p_presence.photoHash()) + acc->delay->getOwnVCard(); break; } } diff --git a/core/handlers/vcardhandler.h b/core/handlers/vcardhandler.h index f774cd5..469398b 100644 --- a/core/handlers/vcardhandler.h +++ b/core/handlers/vcardhandler.h @@ -42,8 +42,6 @@ public: VCardHandler(Account* account); ~VCardHandler(); - void handleOffline(); - void requestVCard(const QString& jid); void handlePresenceOfMyAccountChange(const QXmppPresence& p_presence); void uploadVCard(const Shared::VCard& card); QString getAvatarPath() const; @@ -57,8 +55,6 @@ private slots: private: Account* acc; - bool ownVCardRequestInProgress; - std::set pendingVCardRequests; QString avatarHash; QString avatarType; }; diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 3c23932..b5bc725 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -1,7 +1,17 @@ -target_sources(squawk PRIVATE +set(SOURCE_FILES main.cpp application.cpp - application.h dialogqueue.cpp - dialogqueue.h + root.cpp +) + +set(HEADER_FILES + application.h + dialogqueue.h + root.h +) + +target_sources(squawk PRIVATE + ${SOURCE_FILES} + ${HEADER_FILES} ) diff --git a/main/main.cpp b/main/main.cpp index 9147ef0..5a7e034 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -16,6 +16,7 @@ * along with this program. If not, see . */ +#include "root.h" #include "shared/global.h" #include "shared/messageinfo.h" #include "shared/pathcheck.h" @@ -31,7 +32,6 @@ #include #include #include -#include #include #ifdef WITH_OMEMO @@ -59,13 +59,13 @@ int main(int argc, char *argv[]) qRegisterMetaType("QXmppOmemoStorage::Device"); #endif - QApplication app(argc, argv); + Root app(argc, argv); SignalCatcher sc(&app); - QApplication::setApplicationName("squawk"); - QApplication::setOrganizationName("macaw.me"); - QApplication::setApplicationDisplayName("Squawk"); - QApplication::setApplicationVersion("0.2.3"); + Root::setApplicationName("squawk"); + Root::setOrganizationName("macaw.me"); + Root::setApplicationDisplayName("Squawk"); + Root::setApplicationVersion("0.2.3"); app.setDesktopFileName("squawk"); QTranslator qtTranslator; @@ -97,7 +97,7 @@ int main(int argc, char *argv[]) icon.addFile(":images/logo.svg", QSize(128, 128)); icon.addFile(":images/logo.svg", QSize(256, 256)); icon.addFile(":images/logo.svg", QSize(512, 512)); - QApplication::setWindowIcon(icon); + Root::setWindowIcon(icon); new Shared::Global(); //translates enums @@ -139,7 +139,7 @@ int main(int argc, char *argv[]) //QObject::connect(&app, &QApplication::aboutToQuit, &w, &QMainWindow::close); QObject::connect(squawk, &Core::Squawk::quit, squawk, &Core::Squawk::deleteLater); QObject::connect(squawk, &Core::Squawk::destroyed, coreThread, &QThread::quit, Qt::QueuedConnection); - QObject::connect(coreThread, &QThread::finished, &app, &QApplication::quit, Qt::QueuedConnection); + QObject::connect(coreThread, &QThread::finished, &app, &Root::quit, Qt::QueuedConnection); coreThread->start(); int result = app.exec(); diff --git a/main/root.cpp b/main/root.cpp new file mode 100644 index 0000000..96b37b9 --- /dev/null +++ b/main/root.cpp @@ -0,0 +1,43 @@ +// Squawk messenger. +// Copyright (C) 2019 Yury Gubich +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +#include "root.h" +#include +#include + +Root::Root(int& argc, char *argv[]) : + QApplication(argc, argv) + {} + +bool Root::notify(QObject* receiver, QEvent* e) { + try { + return QApplication::notify(receiver, e); + } catch(const std::runtime_error& e) { + qDebug() << "std::runtime_error in thread : " << QThread::currentThreadId(); + qDebug() << e.what(); + } catch(const std::exception& e) { + qDebug() << "std::exception in thread : " << QThread::currentThreadId(); + qDebug() << e.what(); + } catch(const int& e) { + qDebug() << "int exception in thread : " << QThread::currentThreadId(); + qDebug() << e; + } catch(...) { + qDebug() << "exception thread : " << QThread::currentThreadId(); + } + + qDebug() << "catch in notify "; + return false; +} diff --git a/main/root.h b/main/root.h new file mode 100644 index 0000000..02a82bc --- /dev/null +++ b/main/root.h @@ -0,0 +1,28 @@ +// Squawk messenger. +// Copyright (C) 2019 Yury Gubich +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +#ifndef ROOT_H +#define ROOT_H + +#include + +class Root : public QApplication { +public: + Root(int& argc, char* argv[]); + bool notify(QObject* receiver, QEvent* e) override; +}; + +#endif // ROOT_H