forked from blue/squawk
Basic presence with subnodes
This commit is contained in:
parent
de21036456
commit
e8eaced6e9
20 changed files with 576 additions and 77 deletions
|
@ -1,9 +1,11 @@
|
|||
#include "contact.h"
|
||||
#include <QDebug>
|
||||
|
||||
Models::Contact::Contact(const QMap<QString, QVariant>& data, Models::Item* parentItem):
|
||||
Item(Item::contact, data, parentItem),
|
||||
jid(data.value("jid").toString()),
|
||||
state(data.value("state").toInt())
|
||||
state(Shared::offline),
|
||||
presences()
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -20,20 +22,20 @@ void Models::Contact::setJid(const QString p_jid)
|
|||
{
|
||||
if (jid != p_jid) {
|
||||
jid = p_jid;
|
||||
emit changed(1);
|
||||
changed(1);
|
||||
}
|
||||
}
|
||||
|
||||
int Models::Contact::getState() const
|
||||
Shared::Availability Models::Contact::getState() const
|
||||
{
|
||||
return state;
|
||||
}
|
||||
|
||||
void Models::Contact::setState(int p_state)
|
||||
void Models::Contact::setState(Shared::Availability p_state)
|
||||
{
|
||||
if (state != p_state) {
|
||||
state = p_state;
|
||||
emit changed(2);
|
||||
changed(2);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -67,6 +69,78 @@ void Models::Contact::update(const QString& field, const QVariant& value)
|
|||
} else if (field == "jid") {
|
||||
setJid(value.toString());
|
||||
} else if (field == "state") {
|
||||
setState(value.toInt());
|
||||
unsigned int iState = value.toUInt();
|
||||
if (iState <= Shared::availabilityHighest) {
|
||||
Shared::Availability state = static_cast<Shared::Availability>(iState);
|
||||
setState(state);
|
||||
} else {
|
||||
qDebug("An attempt to set wrong state to the contact");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Models::Contact::addPresence(const QString& p_name, const QMap<QString, QVariant>& data)
|
||||
{
|
||||
QMap<QString, Presence*>::iterator itr = presences.find(p_name);
|
||||
|
||||
if (itr == presences.end()) {
|
||||
Presence* pr = new Presence(data);
|
||||
pr->setName(p_name);
|
||||
presences.insert(p_name, pr);
|
||||
appendChild(pr);
|
||||
} else {
|
||||
Presence* pr = itr.value();
|
||||
for (QMap<QString, QVariant>::const_iterator itr = data.begin(), end = data.end(); itr != end; ++itr) {
|
||||
pr->update(itr.key(), itr.value());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Models::Contact::removePresence(const QString& name)
|
||||
{
|
||||
QMap<QString, Presence*>::iterator itr = presences.find(name);
|
||||
|
||||
if (itr == presences.end()) {
|
||||
} else {
|
||||
Presence* pr = itr.value();
|
||||
presences.erase(itr);
|
||||
removeChild(pr->row());
|
||||
}
|
||||
}
|
||||
|
||||
void Models::Contact::refresh()
|
||||
{
|
||||
QDateTime lastActivity;
|
||||
Presence* presence = 0;
|
||||
for (QMap<QString, Presence*>::iterator itr = presences.begin(), end = presences.end(); itr != end; ++itr) {
|
||||
Presence* pr = itr.value();
|
||||
QDateTime la = pr->getLastActivity();
|
||||
|
||||
if (la > lastActivity) {
|
||||
lastActivity = la;
|
||||
presence = pr;
|
||||
}
|
||||
}
|
||||
|
||||
if (presence != 0) {
|
||||
setState(presence->getAvailability());
|
||||
}
|
||||
}
|
||||
|
||||
void Models::Contact::_removeChild(int index)
|
||||
{
|
||||
Item::_removeChild(index);
|
||||
refresh();
|
||||
}
|
||||
|
||||
void Models::Contact::appendChild(Models::Item* child)
|
||||
{
|
||||
Item::appendChild(child);
|
||||
refresh();
|
||||
}
|
||||
|
||||
void Models::Contact::changed(int col)
|
||||
{
|
||||
Item::changed(col);
|
||||
refresh();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue