From 75554c74511e1bb8b74f56cb2f1159a7f177527d Mon Sep 17 00:00:00 2001 From: blue Date: Tue, 14 Nov 2023 20:23:39 -0300 Subject: [PATCH] refactorng --- cmake/FindLMDB.cmake | 52 ---- cmake/FindSignal.cmake | 15 -- core/components/archive.cpp | 105 ++++---- core/components/archive.h | 5 +- core/components/clientcache.cpp | 32 +-- core/components/clientcache.h | 38 ++- core/components/networkaccess.cpp | 5 +- core/components/networkaccess.h | 8 +- core/components/urlstorage.cpp | 3 +- core/components/urlstorage.h | 5 +- core/delayManager/cardinternal.cpp | 32 +-- core/delayManager/cardinternal.h | 38 ++- core/delayManager/contact.cpp | 32 +-- core/delayManager/contact.h | 37 ++- core/delayManager/info.cpp | 32 +-- core/delayManager/info.h | 37 ++- core/delayManager/infoforuser.cpp | 32 +-- core/delayManager/infoforuser.h | 37 ++- core/delayManager/job.cpp | 32 +-- core/delayManager/job.h | 37 ++- core/delayManager/owncardinternal.cpp | 32 +-- core/delayManager/owncardinternal.h | 37 ++- core/delayManager/owninfoforuser.cpp | 32 +-- core/delayManager/owninfoforuser.h | 37 ++- core/handlers/messagehandler.cpp | 340 ++++++++++++-------------- core/handlers/messagehandler.h | 4 +- core/handlers/omemohandler.cpp | 14 +- core/handlers/rosterhandler.cpp | 14 +- core/handlers/rosterhandler.h | 9 +- shared/messageinfo.h | 9 +- 30 files changed, 515 insertions(+), 627 deletions(-) delete mode 100644 cmake/FindLMDB.cmake delete mode 100644 cmake/FindSignal.cmake diff --git a/cmake/FindLMDB.cmake b/cmake/FindLMDB.cmake deleted file mode 100644 index d6f2cd3..0000000 --- a/cmake/FindLMDB.cmake +++ /dev/null @@ -1,52 +0,0 @@ -#This file is taken from here https://gitlab.ralph.or.at/causal-rt/causal-cpp/, it was GPLv3 license -#Thank you so much, mr. Ralph Alexander Bariz, I hope you don't mind me using your code - -# Try to find LMDB headers and library. -# -# Usage of this module as follows: -# -# find_package(LMDB) -# -# Variables used by this module, they can change the default behaviour and need -# to be set before calling find_package: -# -# LMDB_ROOT_DIR Set this variable to the root installation of -# LMDB if the module has problems finding the -# proper installation path. -# -# Variables defined by this module: -# -# LMDB_FOUND System has LMDB library/headers. -# LMDB_LIBRARIES The LMDB library. -# LMDB_INCLUDE_DIRS The location of LMDB headers. - -find_path(LMDB_ROOT_DIR - NAMES include/lmdb.h - ) - -find_library(LMDB_LIBRARIES - NAMES liblmdb.a liblmdb.so liblmdb.so.a liblmdb.dll.a # We want lmdb to be static, if possible - HINTS ${LMDB_ROOT_DIR}/lib - ) - -add_library(lmdb UNKNOWN IMPORTED) -set_target_properties(lmdb PROPERTIES - IMPORTED_LOCATION ${LMDB_LIBRARIES} - ) - -find_path(LMDB_INCLUDE_DIRS - NAMES lmdb.h - HINTS ${LMDB_ROOT_DIR}/include - ) - -include(FindPackageHandleStandardArgs) -find_package_handle_standard_args(LMDB DEFAULT_MSG - LMDB_LIBRARIES - LMDB_INCLUDE_DIRS - ) - -mark_as_advanced( - LMDB_ROOT_DIR - LMDB_LIBRARIES - LMDB_INCLUDE_DIRS -) diff --git a/cmake/FindSignal.cmake b/cmake/FindSignal.cmake deleted file mode 100644 index 752fed7..0000000 --- a/cmake/FindSignal.cmake +++ /dev/null @@ -1,15 +0,0 @@ -find_path(Signal_INCLUDE_DIR NAMES signal/signal_protocol.h) -find_library(Signal_LIBRARY signal-protocol-c) -mark_as_advanced(Signal_INCLUDE_DIR Signal_LIBRARY) - -include(FindPackageHandleStandardArgs) -find_package_handle_standard_args(Signal REQUIRED_VARS Signal_LIBRARY Signal_INCLUDE_DIR) - -if (Signal_FOUND AND NOT TARGET Signal::Signal) - add_library(Signal::Signal UNKNOWN IMPORTED) - set_target_properties(Signal::Signal PROPERTIES - IMPORTED_LINK_INTERFACE_LANGUAGES "C" - IMPORTED_LOCATION "${Signal_LIBRARY}" - INTERFACE_INCLUDE_DIRECTORIES "${Signal_INCLUDE_DIR}" - ) -endif () diff --git a/core/components/archive.cpp b/core/components/archive.cpp index 67f6693..2f7139a 100644 --- a/core/components/archive.cpp +++ b/core/components/archive.cpp @@ -207,7 +207,6 @@ unsigned int Core::Archive::addElements(const std::list& messag ++success; } txn.commit(); - return success; } @@ -315,63 +314,63 @@ bool Core::Archive::setAvatar(const QByteArray& data, AvatarInfo& newInfo, bool avatars->removeRecord(res, txn); txn.commit(); return true; - } else { - QString currentPath = db.getPath(); - bool needToRemoveOld = false; - QCryptographicHash hash(QCryptographicHash::Sha1); - hash.addData(data); - QByteArray newHash(hash.result()); - if (haveAvatar) { - if (!generated && !oldInfo.autogenerated && oldInfo.hash == newHash) - return false; + } - QFile oldAvatar(currentPath + "/" + res + "." + oldInfo.type); - if (oldAvatar.exists()) { - if (oldAvatar.rename(currentPath + "/" + res + "." + oldInfo.type + ".bak")) { - needToRemoveOld = true; - } else { - qDebug() << "Can't change avatar: couldn't get rid of the old avatar" << oldAvatar.fileName(); - return false; - } - } - } - QMimeDatabase mimedb; - QMimeType type = mimedb.mimeTypeForData(data); - QString ext = type.preferredSuffix(); - QFile newAvatar(currentPath + "/" + res + "." + ext); - if (newAvatar.open(QFile::WriteOnly)) { - newAvatar.write(data); - newAvatar.close(); - - newInfo.type = ext; - newInfo.hash = newHash; - newInfo.autogenerated = generated; - try { - avatars->forceRecord(res, newInfo, txn); - txn.commit(); - } catch (...) { - qDebug() << "Can't change avatar: couldn't store changes to database for" << newAvatar.fileName() << "rolling back to the previous state"; - if (needToRemoveOld) { - QFile oldAvatar(currentPath + "/" + res + "." + oldInfo.type + ".bak"); - oldAvatar.rename(currentPath + "/" + res + "." + oldInfo.type); - } + QString currentPath = db.getPath(); + bool needToRemoveOld = false; + QCryptographicHash hash(QCryptographicHash::Sha1); + hash.addData(data); + QByteArray newHash(hash.result()); + if (haveAvatar) { + if (!generated && !oldInfo.autogenerated && oldInfo.hash == newHash) + return false; + + QFile oldAvatar(currentPath + "/" + res + "." + oldInfo.type); + if (oldAvatar.exists()) { + if (oldAvatar.rename(currentPath + "/" + res + "." + oldInfo.type + ".bak")) { + needToRemoveOld = true; + } else { + qDebug() << "Can't change avatar: couldn't get rid of the old avatar" << oldAvatar.fileName(); return false; } - - if (needToRemoveOld) { - QFile oldAvatar(currentPath + "/" + res + "." + oldInfo.type + ".bak"); - oldAvatar.remove(); - } - return true; - } else { - qDebug() << "Can't change avatar: cant open file to write" << newAvatar.fileName() << "rolling back to the previous state"; - if (needToRemoveOld) { - QFile oldAvatar(currentPath + "/" + res + "." + oldInfo.type + ".bak"); - oldAvatar.rename(currentPath + "/" + res + "." + oldInfo.type); - } - return false; } } + QMimeDatabase mimedb; + QMimeType type = mimedb.mimeTypeForData(data); + QString ext = type.preferredSuffix(); + QFile newAvatar(currentPath + "/" + res + "." + ext); + if (!newAvatar.open(QFile::WriteOnly)) { + qDebug() << "Can't change avatar: cant open file to write" << newAvatar.fileName() << "rolling back to the previous state"; + if (needToRemoveOld) { + QFile oldAvatar(currentPath + "/" + res + "." + oldInfo.type + ".bak"); + oldAvatar.rename(currentPath + "/" + res + "." + oldInfo.type); + } + return false; + } + + newAvatar.write(data); + newAvatar.close(); + + newInfo.type = ext; + newInfo.hash = newHash; + newInfo.autogenerated = generated; + try { + avatars->forceRecord(res, newInfo, txn); + txn.commit(); + } catch (...) { + qDebug() << "Can't change avatar: couldn't store changes to database for" << newAvatar.fileName() << "rolling back to the previous state"; + if (needToRemoveOld) { + QFile oldAvatar(currentPath + "/" + res + "." + oldInfo.type + ".bak"); + oldAvatar.rename(currentPath + "/" + res + "." + oldInfo.type); + } + return false; + } + + if (needToRemoveOld) { + QFile oldAvatar(currentPath + "/" + res + "." + oldInfo.type + ".bak"); + oldAvatar.remove(); + } + return true; } bool Core::Archive::readAvatarInfo(Core::Archive::AvatarInfo& target, const QString& resource) const { diff --git a/core/components/archive.h b/core/components/archive.h index 08f508a..6a2be73 100644 --- a/core/components/archive.h +++ b/core/components/archive.h @@ -16,8 +16,7 @@ * along with this program. If not, see . */ -#ifndef CORE_ARCHIVE_H -#define CORE_ARCHIVE_H +#pragma once #include #include @@ -101,5 +100,3 @@ private: QDataStream& operator << (QDataStream &out, const Core::Archive::AvatarInfo& info); QDataStream& operator >> (QDataStream &in, Core::Archive::AvatarInfo& info); - -#endif // CORE_ARCHIVE_H diff --git a/core/components/clientcache.cpp b/core/components/clientcache.cpp index fe1fa64..f1eb886 100644 --- a/core/components/clientcache.cpp +++ b/core/components/clientcache.cpp @@ -1,18 +1,20 @@ -// 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 . +/* + * 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 "clientcache.h" diff --git a/core/components/clientcache.h b/core/components/clientcache.h index b9dba73..c68dbfb 100644 --- a/core/components/clientcache.h +++ b/core/components/clientcache.h @@ -1,21 +1,22 @@ -// 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 . +/* + * 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 CORE_CLIENTCACHE_H -#define CORE_CLIENTCACHE_H +#pragma once #include #include @@ -60,6 +61,3 @@ private: }; } - - -#endif // CORE_CLIENTCACHE_H diff --git a/core/components/networkaccess.cpp b/core/components/networkaccess.cpp index 862c664..0771dfa 100644 --- a/core/components/networkaccess.cpp +++ b/core/components/networkaccess.cpp @@ -16,7 +16,6 @@ * along with this program. If not, see . */ - #include #include @@ -129,9 +128,9 @@ void Core::NetworkAccess::onDownloadError(QNetworkReply::NetworkError code) { void Core::NetworkAccess::onDownloadSSLError(const QList& errors) { qDebug() << "DEBUG: DOWNLOAD SSL ERRORS"; - for (const QSslError& err : errors) { + for (const QSslError& err : errors) qDebug() << err.errorString(); - } + QNetworkReply* rpl = static_cast(sender()); QString url = rpl->url().toString(); std::map::const_iterator itr = downloads.find(url); diff --git a/core/components/networkaccess.h b/core/components/networkaccess.h index 99a15ed..ddb5ba8 100644 --- a/core/components/networkaccess.h +++ b/core/components/networkaccess.h @@ -16,8 +16,7 @@ * along with this program. If not, see . */ -#ifndef CORE_NETWORKACCESS_H -#define CORE_NETWORKACCESS_H +#pragma once #include #include @@ -36,8 +35,7 @@ namespace Core { //TODO Need to describe how to get rid of records when file is no longer reachable; -class NetworkAccess : public QObject -{ +class NetworkAccess : public QObject { Q_OBJECT struct Transfer; public: @@ -100,5 +98,3 @@ private: }; } - -#endif // CORE_NETWORKACCESS_H diff --git a/core/components/urlstorage.cpp b/core/components/urlstorage.cpp index 31f36ad..5e34792 100644 --- a/core/components/urlstorage.cpp +++ b/core/components/urlstorage.cpp @@ -193,9 +193,8 @@ Core::UrlStorage::UrlInfo::~UrlInfo() {} bool Core::UrlStorage::UrlInfo::addMessage(const QString& acc, const QString& jid, const QString& id) { for (const Shared::MessageInfo& info : messages) { - if (info.account == acc && info.jid == jid && info.messageId == id) { + if (info.account == acc && info.jid == jid && info.messageId == id) return false; - } } messages.emplace_back(acc, jid, id); return true; diff --git a/core/components/urlstorage.h b/core/components/urlstorage.h index fc9d71d..6fb536a 100644 --- a/core/components/urlstorage.h +++ b/core/components/urlstorage.h @@ -16,8 +16,7 @@ * along with this program. If not, see . */ -#ifndef CORE_URLSTORAGE_H -#define CORE_URLSTORAGE_H +#pragma once #include #include @@ -93,5 +92,3 @@ public: QDataStream& operator >> (QDataStream &in, Core::UrlStorage::UrlInfo& info); QDataStream& operator << (QDataStream &out, const Core::UrlStorage::UrlInfo& info); - -#endif // CORE_URLSTORAGE_H diff --git a/core/delayManager/cardinternal.cpp b/core/delayManager/cardinternal.cpp index c9ed203..b5be472 100644 --- a/core/delayManager/cardinternal.cpp +++ b/core/delayManager/cardinternal.cpp @@ -1,18 +1,20 @@ -// 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 . +/* + * 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 "cardinternal.h" diff --git a/core/delayManager/cardinternal.h b/core/delayManager/cardinternal.h index 17dd1ba..6db7734 100644 --- a/core/delayManager/cardinternal.h +++ b/core/delayManager/cardinternal.h @@ -1,22 +1,22 @@ -// 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 CORE_DELAYMANAGER_CARDINTERNAL_H -#define CORE_DELAYMANAGER_CARDINTERNAL_H +/* + * 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 . + */ +#pragma once #include @@ -33,5 +33,3 @@ public: } } - -#endif // CORE_DELAYMANAGER_CARDINTERNAL_H diff --git a/core/delayManager/contact.cpp b/core/delayManager/contact.cpp index 286c8bd..3af1e3f 100644 --- a/core/delayManager/contact.cpp +++ b/core/delayManager/contact.cpp @@ -1,18 +1,20 @@ -// 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 . +/* + * 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 "contact.h" diff --git a/core/delayManager/contact.h b/core/delayManager/contact.h index c136525..3f1ab98 100644 --- a/core/delayManager/contact.h +++ b/core/delayManager/contact.h @@ -1,21 +1,22 @@ -// 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 . +/* + * 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 CORE_DELAYMANAGER_CONTACT_H -#define CORE_DELAYMANAGER_CONTACT_H +#pragma once #include @@ -35,5 +36,3 @@ public: } } - -#endif // CORE_DELAYMANAGER_CONTACT_H diff --git a/core/delayManager/info.cpp b/core/delayManager/info.cpp index b5b619d..9953fd4 100644 --- a/core/delayManager/info.cpp +++ b/core/delayManager/info.cpp @@ -1,18 +1,20 @@ -// 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 . +/* + * 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 "info.h" diff --git a/core/delayManager/info.h b/core/delayManager/info.h index a6601a1..f03e0c7 100644 --- a/core/delayManager/info.h +++ b/core/delayManager/info.h @@ -1,21 +1,22 @@ -// 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 . +/* + * 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 CORE_DELAYMANAGER_INFO_H -#define CORE_DELAYMANAGER_INFO_H +#pragma once #include "job.h" @@ -52,5 +53,3 @@ private: } } - -#endif // CORE_DELAYMANAGER_INFO_H diff --git a/core/delayManager/infoforuser.cpp b/core/delayManager/infoforuser.cpp index 067be80..7d0aed9 100644 --- a/core/delayManager/infoforuser.cpp +++ b/core/delayManager/infoforuser.cpp @@ -1,18 +1,20 @@ -// 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 . +/* + * 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 "infoforuser.h" diff --git a/core/delayManager/infoforuser.h b/core/delayManager/infoforuser.h index 651d741..c2f4692 100644 --- a/core/delayManager/infoforuser.h +++ b/core/delayManager/infoforuser.h @@ -1,21 +1,22 @@ -// 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 . +/* + * 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 CORE_DELAYMANAGER_INFOFORUSER_H -#define CORE_DELAYMANAGER_INFOFORUSER_H +#pragma once #include "contact.h" #include "info.h" @@ -31,5 +32,3 @@ public: } } - -#endif // CORE_DELAYMANAGER_INFOFORUSER_H diff --git a/core/delayManager/job.cpp b/core/delayManager/job.cpp index b2d74b2..37acc02 100644 --- a/core/delayManager/job.cpp +++ b/core/delayManager/job.cpp @@ -1,18 +1,20 @@ -// 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 . +/* + * 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 "job.h" diff --git a/core/delayManager/job.h b/core/delayManager/job.h index 6bfdc1d..26156c0 100644 --- a/core/delayManager/job.h +++ b/core/delayManager/job.h @@ -1,21 +1,22 @@ -// 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 . +/* + * 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 CORE_DELAYMANAGER_JOB_H -#define CORE_DELAYMANAGER_JOB_H +#pragma once #include @@ -51,5 +52,3 @@ public: } } - -#endif // CORE_DELAYMANAGER_JOB_H diff --git a/core/delayManager/owncardinternal.cpp b/core/delayManager/owncardinternal.cpp index 43ed540..d9729ba 100644 --- a/core/delayManager/owncardinternal.cpp +++ b/core/delayManager/owncardinternal.cpp @@ -1,18 +1,20 @@ -// 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 . +/* + * 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 "owncardinternal.h" diff --git a/core/delayManager/owncardinternal.h b/core/delayManager/owncardinternal.h index 7cca0a0..30d7c56 100644 --- a/core/delayManager/owncardinternal.h +++ b/core/delayManager/owncardinternal.h @@ -1,21 +1,22 @@ -// 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 . +/* + * 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 CORE_DELAYMANAGER_OWNCARDINTERNAL_H -#define CORE_DELAYMANAGER_OWNCARDINTERNAL_H +#pragma once #include "job.h" @@ -33,5 +34,3 @@ public: } } - -#endif // CORE_DELAYMANAGER_OWNCARDINTERNAL_H diff --git a/core/delayManager/owninfoforuser.cpp b/core/delayManager/owninfoforuser.cpp index 396dc49..98cbdfa 100644 --- a/core/delayManager/owninfoforuser.cpp +++ b/core/delayManager/owninfoforuser.cpp @@ -1,18 +1,20 @@ -// 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 . +/* + * 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 "owninfoforuser.h" diff --git a/core/delayManager/owninfoforuser.h b/core/delayManager/owninfoforuser.h index 80a13b6..4348b04 100644 --- a/core/delayManager/owninfoforuser.h +++ b/core/delayManager/owninfoforuser.h @@ -1,21 +1,22 @@ -// 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 . +/* + * 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 CORE_DELAYMANAGER_OWNINFOFORUSER_H -#define CORE_DELAYMANAGER_OWNINFOFORUSER_H +#pragma once #include "contact.h" #include "info.h" @@ -31,5 +32,3 @@ public: } } - -#endif // CORE_DELAYMANAGER_OWNINFOFORUSER_H diff --git a/core/handlers/messagehandler.cpp b/core/handlers/messagehandler.cpp index 8489dba..094f671 100644 --- a/core/handlers/messagehandler.cpp +++ b/core/handlers/messagehandler.cpp @@ -19,6 +19,10 @@ #include "messagehandler.h" #include "core/account.h" +static const QMap statePending({{"state", static_cast(Shared::Message::State::pending)}}); +static const QMap stateDelivered({{"state", static_cast(Shared::Message::State::delivered)}}); +static const QMap stateSent({{"state", static_cast(Shared::Message::State::sent)}}); + Core::MessageHandler::MessageHandler(Core::Account* account): QObject(), acc(account), @@ -85,102 +89,80 @@ void Core::MessageHandler::onMessageReceived(const QXmppMessage& msg) { } bool Core::MessageHandler::handlePendingMessageError(const QString& id, const QString& errorText) { - std::tuple ids = getOriginalPendingMessageId(id); - if (std::get<0>(ids)) { - QString id = std::get<1>(ids); - QString jid = std::get<2>(ids); - RosterItem* ri = acc->rh->getRosterItem(jid); - QMap cData = { - {"state", static_cast(Shared::Message::State::error)}, + return adjustPendingMessage(id, { + {"state", static_cast(Shared::Message::State::error)}, {"errorText", errorText} - }; - if (ri != nullptr) - ri->changeMessage(id, cData); - - emit acc->changeMessage(jid, id, cData); - return true; - } - - return false; + }, true); } - bool Core::MessageHandler::handleChatMessage(const QXmppMessage& msg, bool outgoing, bool forwarded, bool guessing) { - if (msg.body().size() != 0 || msg.outOfBandUrl().size() > 0) { - Shared::Message sMsg(Shared::Message::chat); - initializeMessage(sMsg, msg, outgoing, forwarded, guessing); - QString jid = sMsg.getPenPalJid(); - Contact* cnt = acc->rh->getContact(jid); - if (cnt == 0) { - cnt = acc->rh->addOutOfRosterContact(jid); - qDebug() << "appending message" << sMsg.getId() << "to an out of roster contact"; - } - if (sMsg.getOutgoing()) { - if (sMsg.getForwarded()) - sMsg.setState(Shared::Message::State::sent); - } else { - sMsg.setState(Shared::Message::State::delivered); - } - QString oId = msg.replaceId(); - if (oId.size() > 0) { - QMap cData = { - {"body", sMsg.getBody()}, - {"stamp", sMsg.getTime()} - }; - cnt->correctMessageInArchive(oId, sMsg); - emit acc->changeMessage(jid, oId, cData); - } else { - cnt->appendMessageToArchive(sMsg); - emit acc->message(sMsg); - } - - return true; + if (msg.body().isEmpty() && msg.outOfBandUrl().isEmpty()) + return false; + + Shared::Message sMsg(Shared::Message::chat); + initializeMessage(sMsg, msg, outgoing, forwarded, guessing); + QString jid = sMsg.getPenPalJid(); + Contact* cnt = acc->rh->getContact(jid); + if (cnt == 0) { + cnt = acc->rh->addOutOfRosterContact(jid); + qDebug() << "appending message" << sMsg.getId() << "to an out of roster contact"; } - return false; + if (sMsg.getOutgoing()) { + if (sMsg.getForwarded()) + sMsg.setState(Shared::Message::State::sent); + } else { + sMsg.setState(Shared::Message::State::delivered); + } + QString oId = msg.replaceId(); + if (oId.size() > 0) { + QMap cData = { + {"body", sMsg.getBody()}, + {"stamp", sMsg.getTime()} + }; + cnt->correctMessageInArchive(oId, sMsg); + emit acc->changeMessage(jid, oId, cData); + } else { + cnt->appendMessageToArchive(sMsg); + emit acc->message(sMsg); + } + + return true; } bool Core::MessageHandler::handleGroupMessage(const QXmppMessage& msg, bool outgoing, bool forwarded, bool guessing) { const QString& body(msg.body()); - if (body.size() != 0) { - - Shared::Message sMsg(Shared::Message::groupChat); - initializeMessage(sMsg, msg, outgoing, forwarded, guessing); - QString jid = sMsg.getPenPalJid(); - Conference* cnt = acc->rh->getConference(jid); - if (cnt == 0) - return false; + if (body.isEmpty()) + return false; - std::tuple ids = getOriginalPendingMessageId(msg.id()); - if (std::get<0>(ids)) { - QMap cData = {{"state", static_cast(Shared::Message::State::delivered)}}; - cnt->changeMessage(std::get<1>(ids), cData); - emit acc->changeMessage(std::get<2>(ids), std::get<1>(ids), cData); - } else { - QString oId = msg.replaceId(); - if (oId.size() > 0) { - QMap cData = { - {"body", sMsg.getBody()}, - {"stamp", sMsg.getTime()} - }; - cnt->correctMessageInArchive(oId, sMsg); - emit acc->changeMessage(jid, oId, cData); - } else { - cnt->appendMessageToArchive(sMsg); - QDateTime minAgo = QDateTime::currentDateTimeUtc().addSecs(-60); - if (sMsg.getTime() > minAgo) { //otherwise it's considered a delayed delivery, most probably MUC history receipt - emit acc->message(sMsg); - } else { - //qDebug() << "Delayed delivery: "; - } - } - } - - return true; - } - return false; + Shared::Message sMsg(Shared::Message::groupChat); + initializeMessage(sMsg, msg, outgoing, forwarded, guessing); + QString jid = sMsg.getPenPalJid(); + Conference* cnt = acc->rh->getConference(jid); + if (cnt == 0) + return false; + + bool result = adjustPendingMessage(msg.id(), stateDelivered, true); + if (result) //then it was an echo of my own sent message, nothing else needs to be done + return result; + + QString oId = msg.replaceId(); + if (oId.size() > 0) { + QMap cData = { + {"body", sMsg.getBody()}, + {"stamp", sMsg.getTime()} + }; + cnt->correctMessageInArchive(oId, sMsg); + emit acc->changeMessage(jid, oId, cData); + } else { + cnt->appendMessageToArchive(sMsg); + QDateTime minAgo = QDateTime::currentDateTimeUtc().addSecs(-60); + if (sMsg.getTime() > minAgo) //otherwise it's considered a delayed delivery, most probably MUC history initial fetch + emit acc->message(sMsg); + } + + return true; } - void Core::MessageHandler::initializeMessage(Shared::Message& target, const QXmppMessage& source, bool outgoing, bool forwarded, bool guessing) const { const QDateTime& time(source.stamp()); QString id; @@ -254,45 +236,31 @@ void Core::MessageHandler::onCarbonMessageSent(const QXmppMessage& msg) { } #endif -std::tuple Core::MessageHandler::getOriginalPendingMessageId(const QString& id, bool clear) { - std::tuple result({false, "", ""}); +std::optional Core::MessageHandler::getOriginalPendingMessageId(const QString& id, bool clear) { std::map::const_iterator itr = pendingStateMessages.find(id); if (itr != pendingStateMessages.end()) { - std::get<0>(result) = true; - std::get<2>(result) = itr->second; - + Shared::MessageInfo info(acc->name, itr->second, itr->first); std::map::const_iterator itrC = pendingCorrectionMessages.find(id); if (itrC != pendingCorrectionMessages.end()) { if (itrC->second.size() > 0) - std::get<1>(result) = itrC->second; - else - std::get<1>(result) = itr->first; + info.jid = itrC->second; if (clear) pendingCorrectionMessages.erase(itrC); - } else { - std::get<1>(result) = itr->first; } if (clear) pendingStateMessages.erase(itr); + + return info; } - return result; + return std::nullopt; } void Core::MessageHandler::onReceiptReceived(const QString& jid, const QString& id) { SHARED_UNUSED(jid); - std::tuple ids = getOriginalPendingMessageId(id); - if (std::get<0>(ids)) { - QMap cData = {{"state", static_cast(Shared::Message::State::delivered)}}; - RosterItem* ri = acc->rh->getRosterItem(std::get<2>(ids)); - - if (ri != nullptr) - ri->changeMessage(std::get<1>(ids), cData); - - emit acc->changeMessage(std::get<2>(ids), std::get<1>(ids), cData); - } + adjustPendingMessage(id, {{"state", static_cast(Shared::Message::State::delivered)}}, true); } void Core::MessageHandler::sendMessage(const Shared::Message& data, bool newMessage, QString originalId) { @@ -386,19 +354,8 @@ std::pair Core::MessageHandler::scheduleSending bool success = acc->client.sendPacket(*encrypted.get()); if (success) { qDebug() << "Successfully sent an encrypted message"; - std::tuple ids = getOriginalPendingMessageId(id, false); - if (std::get<0>(ids)) { - QString id = std::get<1>(ids); - QString jid = std::get<2>(ids); - RosterItem* ri = acc->rh->getRosterItem(jid); - QMap cData = {{"state", static_cast(Shared::Message::State::sent)}}; - if (ri != nullptr) - ri->changeMessage(id, cData); - - emit acc->changeMessage(jid, id, cData); - } else { + if (!adjustPendingMessage(id, stateSent, false)) qDebug() << "Encrypted message has been successfully sent, but it couldn't be found to update the sate"; - } } else { qDebug() << "Couldn't sent an encrypted message"; handlePendingMessageError(id, "Error sending successfully encrypted message"); @@ -425,6 +382,19 @@ std::pair Core::MessageHandler::scheduleSending } } +bool Core::MessageHandler::adjustPendingMessage(const QString& messageId, const QMap& data, bool final) { + std::optional info = getOriginalPendingMessageId(messageId, final); + if (info) { + RosterItem* ri = acc->rh->getRosterItem(info->jid); + if (ri != nullptr) + ri->changeMessage(info->messageId, data); + + emit acc->changeMessage(info->jid, info->messageId, data); + return true; + } + + return false; +} QMap Core::MessageHandler::getChanges(Shared::Message& data, const QDateTime& time, bool newMessage, const QString& originalId) const { QMap changes; @@ -481,51 +451,53 @@ QXmppMessage Core::MessageHandler::createPacket(const Shared::Message& data, con } void Core::MessageHandler::prepareUpload(const Shared::Message& data, bool newMessage) { - if (acc->state == Shared::ConnectionState::connected) { - QString jid = data.getPenPalJid(); - QString id = data.getId(); - RosterItem* ri = acc->rh->getRosterItem(jid); - if (ri == nullptr) { - qDebug() << "An attempt to initialize upload in" << acc->name << "for pal" << jid << "but the object for this pal wasn't found, something went terrebly wrong, skipping send"; - return; - } - QString path = data.getAttachPath(); - QString url = acc->network->getFileRemoteUrl(path); - if (url.size() != 0) { - sendMessageWithLocalUploadedFile(data, url, newMessage); - } else { - pendingStateMessages.insert(std::make_pair(id, jid)); - if (newMessage) { - ri->appendMessageToArchive(data); - } else { - QMap changes({ - {"state", (uint)Shared::Message::State::pending} - }); - ri->changeMessage(id, changes); - emit acc->changeMessage(jid, id, changes); - } - //this checks if the file is already uploading, and if so it subscribes to it's success, so, i need to do stuff only if the network knows nothing of this file - if (!acc->network->checkAndAddToUploading(acc->getName(), jid, id, path)) { - if (acc->um->serviceFound()) { - QFileInfo file(path); - if (file.exists() && file.isReadable()) { - pendingStateMessages.insert(std::make_pair(id, jid)); - uploadingSlotsQueue.emplace_back(path, id); - if (uploadingSlotsQueue.size() == 1) - acc->um->requestUploadSlot(file); - } else { - handleUploadError(jid, id, "Uploading file no longer exists or your system user has no permission to read it"); - qDebug() << "Requested upload slot in account" << acc->name << "for file" << path << "but the file doesn't exist or is not readable"; - } - } else { - handleUploadError(jid, id, "Your server doesn't support file upload service, or it's prohibited for your account"); - qDebug() << "Requested upload slot in account" << acc->name << "for file" << path << "but upload manager didn't discover any upload services"; - } - } - } - } else { + if (acc->state != Shared::ConnectionState::connected) { handleUploadError(data.getPenPalJid(), data.getId(), "Account is offline or reconnecting"); qDebug() << "An attempt to send message with not connected account " << acc->name << ", skipping"; + return; + } + + QString jid = data.getPenPalJid(); + QString id = data.getId(); + RosterItem* ri = acc->rh->getRosterItem(jid); + if (ri == nullptr) { + qDebug() << "An attempt to initialize upload in" << acc->name << "for pal" << jid << "but the object for this pal wasn't found, something went terrebly wrong, skipping send"; + return; + } + + QString path = data.getAttachPath(); + QString url = acc->network->getFileRemoteUrl(path); + if (url.size() != 0) + return sendMessageWithLocalUploadedFile(data, url, newMessage); + + pendingStateMessages.insert(std::make_pair(id, jid)); + if (newMessage) { + ri->appendMessageToArchive(data); + } else { + ri->changeMessage(id, statePending); + emit acc->changeMessage(jid, id, statePending); + } + + //this checks if the file is already uploading, and if so it subscribes to it's success, + //So, I need to do stuff only if the network knows nothing of this file + if (acc->network->checkAndAddToUploading(acc->getName(), jid, id, path)) + return; + + if (!acc->um->serviceFound()) { + handleUploadError(jid, id, "Your server doesn't support file upload service, or it's prohibited for your account"); + qDebug() << "Requested upload slot in account" << acc->name << "for file" << path << "but upload manager didn't discover any upload services"; + return; + } + + QFileInfo file(path); + if (file.exists() && file.isReadable()) { + pendingStateMessages.insert(std::make_pair(id, jid)); + uploadingSlotsQueue.emplace_back(path, id); + if (uploadingSlotsQueue.size() == 1) + acc->um->requestUploadSlot(file); + } else { + handleUploadError(jid, id, "Uploading file no longer exists or your system user has no permission to read it"); + qDebug() << "Requested upload slot in account" << acc->name << "for file" << path << "but the file doesn't exist or is not readable"; } } @@ -565,24 +537,25 @@ void Core::MessageHandler::onDownloadFileComplete(const std::listgetName()) { - RosterItem* cnt = acc->rh->getRosterItem(info.jid); - if (cnt != nullptr) { - bool changed = cnt->changeMessage(info.messageId, cData); - if (changed) - emit acc->changeMessage(info.jid, info.messageId, cData); - } + if (info.account != acc->getName()) + continue; + + RosterItem* cnt = acc->rh->getRosterItem(info.jid); + if (cnt != nullptr) { + bool changed = cnt->changeMessage(info.messageId, cData); + if (changed) + emit acc->changeMessage(info.jid, info.messageId, cData); } } } void Core::MessageHandler::onLoadFileError(const std::list& msgs, const QString& text, bool up) { - if (up) { - for (const Shared::MessageInfo& info : msgs) { - if (info.account == acc->getName()) - handleUploadError(info.jid, info.messageId, text); - } - } + if (!up) + return; + + for (const Shared::MessageInfo& info : msgs) + if (info.account == acc->getName()) + handleUploadError(info.jid, info.messageId, text); } void Core::MessageHandler::handleUploadError(const QString& jid, const QString& messageId, const QString& errorText) { @@ -597,15 +570,16 @@ void Core::MessageHandler::handleUploadError(const QString& jid, const QString& void Core::MessageHandler::onUploadFileComplete(const std::list& msgs, const QString& url, const QString& path) { for (const Shared::MessageInfo& info : msgs) { - if (info.account == acc->getName()) { - RosterItem* ri = acc->rh->getRosterItem(info.jid); - if (ri != nullptr) { - Shared::Message msg = ri->getMessage(info.messageId); - msg.setAttachPath(path); - sendMessageWithLocalUploadedFile(msg, url, false); - } else { - qDebug() << "A signal received about complete upload to" << acc->name << "for pal" << info.jid << "but the object for this pal wasn't found, something went terrebly wrong, skipping send"; - } + if (info.account != acc->getName()) + continue; + + RosterItem* ri = acc->rh->getRosterItem(info.jid); + if (ri != nullptr) { + Shared::Message msg = ri->getMessage(info.messageId); + msg.setAttachPath(path); + sendMessageWithLocalUploadedFile(msg, url, false); + } else { + qDebug() << "A signal received about complete upload to" << acc->name << "for pal" << info.jid << "but the object for this pal wasn't found, something went terrebly wrong, skipping send"; } } } @@ -654,7 +628,7 @@ void Core::MessageHandler::resendMessage(const QString& jid, const QString& id) try { Shared::Message msg = cnt->getMessage(id); if (msg.getState() == Shared::Message::State::error) { - if (msg.getEdited()){ + if (msg.getEdited()) { QString originalId = msg.getId(); msg.generateRandomId(); sendMessage(msg, false, originalId); diff --git a/core/handlers/messagehandler.h b/core/handlers/messagehandler.h index e3199dc..15f99bf 100644 --- a/core/handlers/messagehandler.h +++ b/core/handlers/messagehandler.h @@ -23,6 +23,7 @@ #include #include #include +#include #include #include @@ -71,9 +72,10 @@ private: void handleUploadError(const QString& jid, const QString& messageId, const QString& errorText); QXmppMessage createPacket(const Shared::Message& data, const QDateTime& time, const QString& originalId) const; QMap getChanges(Shared::Message& data, const QDateTime& time, bool newMessage, const QString& originalId) const; - std::tuple getOriginalPendingMessageId(const QString& id, bool clear = true); + std::optional getOriginalPendingMessageId(const QString& id, bool clear = true); bool handlePendingMessageError(const QString& id, const QString& errorText); std::pair scheduleSending(const Shared::Message& message, const QDateTime& sendTime, const QString& originalId); + bool adjustPendingMessage(const QString& messageId, const QMap& data, bool final); private: Account* acc; diff --git a/core/handlers/omemohandler.cpp b/core/handlers/omemohandler.cpp index eaa674f..b50bed8 100644 --- a/core/handlers/omemohandler.cpp +++ b/core/handlers/omemohandler.cpp @@ -54,22 +54,20 @@ QXmppTask Core::OmemoHandler::allData() { OmemoData data; data.ownDevice = ownDevice; - // LMDBAL::Transaction txn = db.beginReadOnlyTransaction(); TODO need to enable transaction after fixing LMDBAL - std::map pkeys = preKeyPairs->readAll(); - for (const std::pair& pair : pkeys) { + LMDBAL::Transaction txn = db.beginReadOnlyTransaction(); + std::map pkeys = preKeyPairs->readAll(txn); + for (const std::pair& pair : pkeys) data.preKeyPairs.insert(pair.first, pair.second); - } - std::map spre = signedPreKeyPairs->readAll(); + std::map spre = signedPreKeyPairs->readAll(txn); for (const std::pair& pair : spre) { QXmppOmemoStorage::SignedPreKeyPair qxpair = {pair.second.first, pair.second.second}; data.signedPreKeyPairs.insert(pair.first, qxpair); } - std::map> devs = devices->readAll(); - for (const std::pair>& pair : devs) { + std::map> devs = devices->readAll(txn); + for (const std::pair>& pair : devs) data.devices.insert(pair.first, pair.second); - } return Core::makeReadyTask(std::move(data)); } diff --git a/core/handlers/rosterhandler.cpp b/core/handlers/rosterhandler.cpp index 38425ba..940ddbf 100644 --- a/core/handlers/rosterhandler.cpp +++ b/core/handlers/rosterhandler.cpp @@ -60,7 +60,6 @@ void Core::RosterHandler::clear() { conferences.clear(); } - void Core::RosterHandler::onRosterReceived() { QStringList bj = acc->rm->getRosterBareJids(); for (int i = 0; i < bj.size(); ++i) { @@ -260,7 +259,6 @@ void Core::RosterHandler::onTrustChanged(const QString& jid, const Shared::Trust emit acc->changeContact(jid, {{"trust", QVariant::fromValue(trust)}}); } - void Core::RosterHandler::addToGroup(const QString& jid, const QString& group) { std::map>::iterator gItr = groups.find(group); if (gItr == groups.end()) { @@ -288,7 +286,7 @@ void Core::RosterHandler::removeFromGroup(const QString& jid, const QString& gro } } -Core::RosterItem * Core::RosterHandler::getRosterItem(const QString& jid) { +Core::RosterItem* Core::RosterHandler::getRosterItem(const QString& jid) { RosterItem* item = nullptr; QString lcJid = jid.toLower(); std::map::const_iterator citr = contacts.find(lcJid); @@ -302,7 +300,7 @@ Core::RosterItem * Core::RosterHandler::getRosterItem(const QString& jid) { return item; } -Core::Conference * Core::RosterHandler::getConference(const QString& jid) { +Core::Conference* Core::RosterHandler::getConference(const QString& jid) { Conference* item = 0; std::map::const_iterator coitr = conferences.find(jid.toLower()); if (coitr != conferences.end()) @@ -311,7 +309,7 @@ Core::Conference * Core::RosterHandler::getConference(const QString& jid) { return item; } -Core::Contact * Core::RosterHandler::getContact(const QString& jid) { +Core::Contact* Core::RosterHandler::getContact(const QString& jid) { Contact* item = 0; std::map::const_iterator citr = contacts.find(jid.toLower()); if (citr != contacts.end()) @@ -320,7 +318,7 @@ Core::Contact * Core::RosterHandler::getContact(const QString& jid) { return item; } -Core::Contact * Core::RosterHandler::addOutOfRosterContact(const QString& jid) { +Core::Contact* Core::RosterHandler::addOutOfRosterContact(const QString& jid) { QString lcJid = jid.toLower(); Contact* cnt = new Contact(lcJid, acc->name); contacts.insert(std::make_pair(lcJid, cnt)); @@ -446,9 +444,9 @@ void Core::RosterHandler::clearConferences() { void Core::RosterHandler::removeRoomRequest(const QString& jid) { QString lcJid = jid.toLower(); std::map::const_iterator itr = conferences.find(lcJid); - if (itr == conferences.end()) { + if (itr == conferences.end()) qDebug() << "An attempt to remove non existing room" << lcJid << "from account" << acc->name << ", skipping"; - } + itr->second->deleteLater(); conferences.erase(itr); emit acc->removeRoom(lcJid); diff --git a/core/handlers/rosterhandler.h b/core/handlers/rosterhandler.h index 1f8e480..5abc416 100644 --- a/core/handlers/rosterhandler.h +++ b/core/handlers/rosterhandler.h @@ -16,8 +16,7 @@ * along with this program. If not, see . */ -#ifndef CORE_ROSTERHANDLER_H -#define CORE_ROSTERHANDLER_H +#pragma once #include #include @@ -27,6 +26,7 @@ #include #include #include +#include #include #include @@ -45,8 +45,7 @@ namespace Core { class Account; -class RosterHandler : public QObject -{ +class RosterHandler : public QObject { Q_OBJECT public: RosterHandler(Account* account); @@ -119,5 +118,3 @@ private: }; } - -#endif // CORE_ROSTERHANDLER_H diff --git a/shared/messageinfo.h b/shared/messageinfo.h index 942d88c..3cf75bc 100644 --- a/shared/messageinfo.h +++ b/shared/messageinfo.h @@ -16,16 +16,11 @@ * along with this program. If not, see . */ -#ifndef SHARED_MESSAGEINFO_H -#define SHARED_MESSAGEINFO_H +#pragma once #include namespace Shared { - -/** - * @todo write docs - */ struct MessageInfo { MessageInfo(); MessageInfo(const QString& acc, const QString& j, const QString& id); @@ -39,5 +34,3 @@ struct MessageInfo { }; } - -#endif // SHARED_MESSAGEINFO_H