diff --git a/ui/CMakeLists.txt b/ui/CMakeLists.txt index 4e47abe..52913a8 100644 --- a/ui/CMakeLists.txt +++ b/ui/CMakeLists.txt @@ -24,6 +24,7 @@ set(squawkUI_SRC models/room.cpp models/abstractparticipant.cpp models/participant.cpp + models/reference.cpp utils/messageline.cpp utils//message.cpp utils/resizer.cpp diff --git a/ui/models/item.cpp b/ui/models/item.cpp index 1d0437f..b653702 100644 --- a/ui/models/item.cpp +++ b/ui/models/item.cpp @@ -26,7 +26,8 @@ Models::Item::Item(Type p_type, const QMap &p_data, Item *p_p type(p_type), name(""), childItems(), - parent(p_parent) + parent(p_parent), + references() { QMap::const_iterator itr = p_data.find("name"); if (itr != p_data.end()) { diff --git a/ui/models/item.h b/ui/models/item.h index a936e34..77f2824 100644 --- a/ui/models/item.h +++ b/ui/models/item.h @@ -26,10 +26,12 @@ #include #include "shared/enums.h" - namespace Models { +class Reference; + class Item : public QObject{ + friend class Reference; Q_OBJECT public: enum Type { @@ -39,7 +41,8 @@ class Item : public QObject{ room, presence, participant, - root + root, + reference }; explicit Item(Type p_type, const QMap &data, Item *parentItem = 0); @@ -62,8 +65,8 @@ class Item : public QObject{ QString getName() const; void setName(const QString& name); - Item *child(int row); - int childCount() const; + virtual Item *child(int row); + virtual int childCount() const; virtual int columnCount() const; virtual QVariant data(int column) const; int row() const; @@ -92,6 +95,7 @@ class Item : public QObject{ QString name; std::deque childItems; Item* parent; + std::deque references; protected slots: virtual void toOfflineState(); diff --git a/ui/models/reference.cpp b/ui/models/reference.cpp new file mode 100644 index 0000000..4ead77f --- /dev/null +++ b/ui/models/reference.cpp @@ -0,0 +1,93 @@ +/* + * 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 "reference.h" + +using namespace Models; + +Models::Reference::Reference(Models::Item* original, Models::Item* parent): + Models::Item(reference, {}, parent), + original(original) +{ + original->references.push_back(this); + + connect(original, &Item::childChanged, this, &Item::childChanged); + connect(original, &Item::childIsAboutToBeInserted, this, &Item::childIsAboutToBeInserted); + connect(original, &Item::childInserted, this, &Item::childInserted); + connect(original, &Item::childIsAboutToBeRemoved, this, &Item::childIsAboutToBeRemoved); + connect(original, &Item::childRemoved, this, &Item::childRemoved); + connect(original, &Item::childIsAboutToBeMoved, this, &Item::childIsAboutToBeMoved); + connect(original, &Item::childMoved, this, &Item::childMoved); +} + +Models::Reference::~Reference() +{ + disconnect(original, &Item::childIsAboutToBeInserted, this, &Item::childIsAboutToBeInserted); + disconnect(original, &Item::childInserted, this, &Item::childInserted); + disconnect(original, &Item::childIsAboutToBeRemoved, this, &Item::childIsAboutToBeRemoved); + disconnect(original, &Item::childRemoved, this, &Item::childRemoved); + disconnect(original, &Item::childIsAboutToBeMoved, this, &Item::childIsAboutToBeMoved); + disconnect(original, &Item::childMoved, this, &Item::childMoved); + + for (std::deque::const_iterator itr = original->references.begin(), end = original->references.end(); itr != end; itr++) { + if (*itr == this) { + original->references.erase(itr); + break; + } + } +} + +int Models::Reference::columnCount() const +{ + return original->columnCount(); +} + +QVariant Models::Reference::data(int column) const +{ + return original->data(column); +} + +QString Models::Reference::getDisplayedName() const +{ + return original->getDisplayedName(); +} + +Models::Item * Models::Reference::dereference() +{ + return original; +} + +const Models::Item * Models::Reference::dereferenceConst() const +{ + return original; +} + +void Models::Reference::appendChild(Models::Item* child) +{ + original->appendChild(child); +} + +void Models::Reference::removeChild(int index) +{ + original->removeChild(index); +} + +void Models::Reference::toOfflineState() +{ + original->toOfflineState(); +} diff --git a/ui/models/reference.h b/ui/models/reference.h new file mode 100644 index 0000000..84d0b07 --- /dev/null +++ b/ui/models/reference.h @@ -0,0 +1,54 @@ +/* + * 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 MODELS_REFERENCE_H +#define MODELS_REFERENCE_H + +#include "item.h" + +namespace Models { + +/** + * @todo write docs + */ +class Reference : public Models::Item +{ + Q_OBJECT +public: + Reference(Models::Item* original, Models::Item* parent); + ~Reference(); + + int columnCount() const override; + QVariant data(int column) const override; + QString getDisplayedName() const override; + void appendChild(Models::Item * child) override; + void removeChild(int index) override; + Item* dereference(); + const Item* dereferenceConst() const; + +protected slots: + void toOfflineState() override; + +private: + Models::Item* original; + +}; + +} + +#endif // MODELS_REFERENCE_H diff --git a/ui/models/roster.cpp b/ui/models/roster.cpp index 42ea9f8..f1e6efa 100644 --- a/ui/models/roster.cpp +++ b/ui/models/roster.cpp @@ -480,7 +480,9 @@ void Models::Roster::addContact(const QString& account, const QString& jid, cons if (item->type == Item::contact) { Contact* ca = static_cast(item); if (ca->getJid() == jid) { - qDebug() << "An attempt to add a already existing contact " << jid << " to the group " << group << ", contact will be moved from ungrouped contacts of " << account; + qDebug() << "An attempt to add a already existing contact " << jid + << " to the group " << group + << ", contact will be moved from ungrouped contacts of " << account; parent->appendChild(ca); return;