2019-07-11 08:51:52 +00:00
|
|
|
/*
|
2019-09-01 19:46:12 +00:00
|
|
|
* Squawk messenger.
|
|
|
|
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
2019-07-11 08:51:52 +00:00
|
|
|
*
|
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "room.h"
|
|
|
|
#include <QIcon>
|
|
|
|
|
|
|
|
Models::Room::Room(const QString& p_jid, const QMap<QString, QVariant>& data, Models::Item* parentItem):
|
|
|
|
Item(room, data, parentItem),
|
|
|
|
autoJoin(false),
|
|
|
|
joined(false),
|
|
|
|
jid(p_jid),
|
|
|
|
nick(""),
|
|
|
|
messages()
|
|
|
|
{
|
|
|
|
QMap<QString, QVariant>::const_iterator itr = data.find("autoJoin");
|
|
|
|
if (itr != data.end()) {
|
|
|
|
setAutoJoin(itr.value().toBool());
|
|
|
|
}
|
|
|
|
|
|
|
|
itr = data.find("joined");
|
|
|
|
if (itr != data.end()) {
|
|
|
|
setJoined(itr.value().toBool());
|
|
|
|
}
|
|
|
|
|
|
|
|
itr = data.find("nick");
|
|
|
|
if (itr != data.end()) {
|
|
|
|
setNick(itr.value().toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Models::Room::~Room()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int Models::Room::getUnreadMessagesCount() const
|
|
|
|
{
|
|
|
|
return messages.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
int Models::Room::columnCount() const
|
|
|
|
{
|
2019-08-31 20:50:05 +00:00
|
|
|
return 6;
|
2019-07-11 08:51:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString Models::Room::getJid() const
|
|
|
|
{
|
|
|
|
return jid;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Models::Room::getAutoJoin() const
|
|
|
|
{
|
|
|
|
return autoJoin;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Models::Room::getJoined() const
|
|
|
|
{
|
|
|
|
return joined;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Models::Room::getNick() const
|
|
|
|
{
|
|
|
|
return nick;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Models::Room::getRoomName() const
|
|
|
|
{
|
|
|
|
if (name.size() == 0) {
|
|
|
|
return jid;
|
|
|
|
} else {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant Models::Room::data(int column) const
|
|
|
|
{
|
|
|
|
switch (column) {
|
|
|
|
case 0:
|
|
|
|
return getRoomName();
|
|
|
|
case 1:
|
|
|
|
return jid;
|
|
|
|
case 2:
|
|
|
|
return getJoined();
|
|
|
|
case 3:
|
|
|
|
return getAutoJoin();
|
|
|
|
case 4:
|
|
|
|
return getNick();
|
2019-08-31 20:50:05 +00:00
|
|
|
case 5:
|
|
|
|
return getMessagesCount();
|
2019-07-11 08:51:52 +00:00
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Models::Room::setAutoJoin(bool p_autoJoin)
|
|
|
|
{
|
|
|
|
if (autoJoin != p_autoJoin) {
|
|
|
|
autoJoin = p_autoJoin;
|
|
|
|
changed(3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Models::Room::setJid(const QString& p_jid)
|
|
|
|
{
|
|
|
|
if (jid != p_jid) {
|
|
|
|
jid = p_jid;
|
|
|
|
changed(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Models::Room::setJoined(bool p_joined)
|
|
|
|
{
|
|
|
|
if (joined != p_joined) {
|
|
|
|
joined = p_joined;
|
|
|
|
changed(2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Models::Room::setNick(const QString& p_nick)
|
|
|
|
{
|
|
|
|
if (nick != p_nick) {
|
|
|
|
nick = p_nick;
|
|
|
|
changed(4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Models::Room::update(const QString& field, const QVariant& value)
|
|
|
|
{
|
|
|
|
if (field == "name") {
|
|
|
|
setName(value.toString());
|
|
|
|
} else if (field == "jid") {
|
|
|
|
setJid(value.toString());
|
|
|
|
} else if (field == "joined") {
|
|
|
|
setJoined(value.toBool());
|
|
|
|
} else if (field == "autoJoin") {
|
|
|
|
setAutoJoin(value.toBool());
|
|
|
|
} else if (field == "nick") {
|
|
|
|
setNick(value.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QIcon Models::Room::getStatusIcon(bool big) const
|
|
|
|
{
|
2019-08-31 20:50:05 +00:00
|
|
|
if (messages.size() > 0) {
|
|
|
|
return Shared::icon("mail-message", big);
|
2019-07-11 08:51:52 +00:00
|
|
|
} else {
|
2019-08-31 20:50:05 +00:00
|
|
|
if (autoJoin) {
|
|
|
|
if (joined) {
|
|
|
|
return Shared::connectionStateIcon(Shared::connected, big);
|
|
|
|
} else {
|
|
|
|
return Shared::connectionStateIcon(Shared::disconnected, big);
|
|
|
|
}
|
2019-07-11 08:51:52 +00:00
|
|
|
} else {
|
2019-08-31 20:50:05 +00:00
|
|
|
if (joined) {
|
|
|
|
return Shared::connectionStateIcon(Shared::connecting, big);
|
|
|
|
} else {
|
|
|
|
return Shared::connectionStateIcon(Shared::error, big);
|
|
|
|
}
|
2019-07-11 08:51:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Models::Room::getStatusText() const
|
|
|
|
{
|
|
|
|
if (autoJoin) {
|
|
|
|
if (joined) {
|
|
|
|
return "Subscribed";
|
|
|
|
} else {
|
|
|
|
return "Temporarily unsubscribed";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (joined) {
|
|
|
|
return "Temporarily subscribed";
|
|
|
|
} else {
|
|
|
|
return "Unsubscribed";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-31 20:50:05 +00:00
|
|
|
|
|
|
|
unsigned int Models::Room::getMessagesCount() const
|
|
|
|
{
|
|
|
|
return messages.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Models::Room::addMessage(const Shared::Message& data)
|
|
|
|
{
|
|
|
|
messages.emplace_back(data);
|
|
|
|
changed(5);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Models::Room::dropMessages()
|
|
|
|
{
|
|
|
|
if (messages.size() > 0) {
|
|
|
|
messages.clear();
|
|
|
|
changed(5);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Models::Room::getMessages(Models::Room::Messages& container) const
|
|
|
|
{
|
|
|
|
for (Messages::const_iterator itr = messages.begin(), end = messages.end(); itr != end; ++itr) {
|
|
|
|
const Shared::Message& msg = *itr;
|
|
|
|
container.push_back(msg);
|
|
|
|
}
|
|
|
|
}
|