summaryrefslogtreecommitdiffstats
blob: 6239033a6df8f0c961bfbb6ef49a33519f70a646 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
 * Copyright (c) 2010 Kevin Smith
 * Licensed under the GNU General Public License v3.
 * See Documentation/Licenses/GPLv3.txt for more information.
 */

#include <Swift/Controllers/Roster/ContactRosterItem.h>

#include <boost/date_time/posix_time/posix_time.hpp>

#include <Swiften/Base/foreach.h>
#include <Swiften/Base/DateTime.h>
#include <Swiften/Elements/Idle.h>

#include <Swift/Controllers/Roster/GroupRosterItem.h>

namespace Swift {


ContactRosterItem::ContactRosterItem(const JID& jid, const JID& displayJID, const std::string& name, GroupRosterItem* parent) : RosterItem(name, parent), jid_(jid), displayJID_(displayJID), blockState_(BlockingNotSupported) {
}

ContactRosterItem::~ContactRosterItem() {
}

StatusShow::Type ContactRosterItem::getStatusShow() const {
	return shownPresence_ ? shownPresence_->getShow() : StatusShow::None;
}

StatusShow::Type ContactRosterItem::getSimplifiedStatusShow() const {
	switch (shownPresence_ ? shownPresence_->getShow() : StatusShow::None) {
		case StatusShow::Online: return StatusShow::Online;
		case StatusShow::Away: return StatusShow::Away;
	 	case StatusShow::XA: return StatusShow::Away;
		case StatusShow::FFC: return StatusShow::Online;
		case StatusShow::DND: return StatusShow::DND;
		case StatusShow::None: return StatusShow::None;
	}
	assert(false);
	return StatusShow::None;
}

std::string ContactRosterItem::getStatusText() const {
	return shownPresence_ ? shownPresence_->getStatus() : "";
}

std::string ContactRosterItem::getIdleText() const {
	Idle::ref idle = shownPresence_ ? shownPresence_->getPayload<Idle>() : Idle::ref();
	if (!idle || idle->getSince().is_not_a_date_time()) {
		return "";
	} else {
		return dateTimeToLocalString(idle->getSince());
	}
}

std::string ContactRosterItem::getOfflineSinceText() const {
	if (offlinePresence_) {
		boost::optional<boost::posix_time::ptime> delay = offlinePresence_->getTimestamp();
		if (offlinePresence_->getType() == Presence::Unavailable && delay) {
			return dateTimeToLocalString(*delay);
		}
	}
	return "";
}

void ContactRosterItem::setAvatarPath(const boost::filesystem::path& path) {
	avatarPath_ = path;
	onDataChanged();
}
const boost::filesystem::path& ContactRosterItem::getAvatarPath() const {
	return avatarPath_;
}

const JID& ContactRosterItem::getJID() const {
	return jid_;
}

void ContactRosterItem::setDisplayJID(const JID& jid) {
	displayJID_ = jid;
}

const JID& ContactRosterItem::getDisplayJID() const {
	return displayJID_;
}


typedef std::pair<std::string, boost::shared_ptr<Presence> > StringPresencePair;

void ContactRosterItem::calculateShownPresence() {
	shownPresence_ = offlinePresence_;
	foreach (StringPresencePair presencePair, presences_) {
		boost::shared_ptr<Presence> presence = presencePair.second;
		if (!shownPresence_ || presence->getPriority() > shownPresence_->getPriority() || presence->getShow() < shownPresence_->getShow()) {
			shownPresence_ = presence;
		}
	}
}

void ContactRosterItem::clearPresence() {
	presences_.clear();
	calculateShownPresence();
	onDataChanged();
}

void ContactRosterItem::applyPresence(const std::string& resource, boost::shared_ptr<Presence> presence) {
	if (offlinePresence_) {
		offlinePresence_ = boost::shared_ptr<Presence>();
	}
	if (presence->getType() == Presence::Unavailable) {
		if (resource.empty()) {
			/* Unavailable from the bare JID means all resources are offline.*/
			presences_.clear();
		} else {
			if (presences_.find(resource) != presences_.end()) {
				presences_.erase(resource);
			}
		}
		if (presences_.empty()) {
			offlinePresence_ = presence;
		}
	} else {
		presences_[resource] = presence;
	}
	calculateShownPresence();
	onDataChanged();
}

const std::vector<std::string>& ContactRosterItem::getGroups() const {
	return groups_;
}

/** Only used so a contact can know about the groups it's in*/
void ContactRosterItem::addGroup(const std::string& group) {
	groups_.push_back(group);
}
void ContactRosterItem::removeGroup(const std::string& group) {
	groups_.erase(std::remove(groups_.begin(), groups_.end(), group), groups_.end());
}

void ContactRosterItem::setSupportedFeatures(const std::set<Feature>& features) {
	features_ = features;
}

bool ContactRosterItem::supportsFeature(const Feature feature) const {
	return features_.find(feature) != features_.end();
}

void ContactRosterItem::setBlockState(BlockState state) {
	blockState_ = state;
	onDataChanged();
}

ContactRosterItem::BlockState ContactRosterItem::blockState() const {
	return blockState_;
}

VCard::ref ContactRosterItem::getVCard() const {
	return vcard_;
}

void ContactRosterItem::setVCard(VCard::ref vcard) {
	vcard_ = vcard;
	onDataChanged();
}

}