summaryrefslogtreecommitdiffstats
blob: 2909c056947c899abbd15df9f2f5fe893d2f5393 (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
 * Copyright (c) 2010 Kevin Smith
 * Licensed under the GNU General Public License v3.
 * See Documentation/Licenses/GPLv3.txt for more information.
 */

#include "RosterModel.h"

#include <boost/bind.hpp>

#include <QColor>
#include <QIcon>
#include <qdebug.h>

#include "Swiften/Elements/StatusShow.h"
#include "Swift/Controllers/Roster/ContactRosterItem.h"
#include "Swift/Controllers/Roster/GroupRosterItem.h"
#include <Swift/Controllers/StatusUtil.h>

#include "QtSwiftUtil.h"
#include "Swift/QtUI/Roster/QtTreeWidget.h"

namespace Swift {

RosterModel::RosterModel(QtTreeWidget* view) : view_(view) {
	roster_ = NULL;
}

RosterModel::~RosterModel() {
}

void RosterModel::setRoster(Roster* roster) {
	roster_ = roster;
	if (roster_) {
		roster->onChildrenChanged.connect(boost::bind(&RosterModel::handleChildrenChanged, this, _1));
		roster->onDataChanged.connect(boost::bind(&RosterModel::handleDataChanged, this, _1));
	}
	reLayout();
}

void RosterModel::reLayout() {
	//emit layoutChanged();
	beginResetModel();
	endResetModel(); // TODO: Not sure if this isn't too early?
	if (!roster_) {
		return;
	}
	foreach (RosterItem* item, roster_->getRoot()->getDisplayedChildren()) {
		GroupRosterItem* child = dynamic_cast<GroupRosterItem*>(item);
		if (!child) continue;
		emit itemExpanded(index(child), child->isExpanded());
	}
}

void RosterModel::handleChildrenChanged(GroupRosterItem* /*group*/) {
	reLayout();
}								

void RosterModel::handleDataChanged(RosterItem* item) {
	Q_ASSERT(item);
	QModelIndex modelIndex = index(item);
	if (modelIndex.isValid()) {
		emit dataChanged(modelIndex, modelIndex);
	}
}

int RosterModel::columnCount(const QModelIndex& /*parent*/) const {
	return 1;
}

RosterItem* RosterModel::getItem(const QModelIndex& index) const {
	return index.isValid() ? static_cast<RosterItem*>(index.internalPointer()) : NULL;
}

QVariant RosterModel::data(const QModelIndex& index, int role) const {
	RosterItem* item = getItem(index);
	if (!item) return QVariant();

	switch (role) {
		case Qt::DisplayRole: return P2QSTRING(item->getDisplayName());
		case Qt::TextColorRole: return getTextColor(item);
		case Qt::BackgroundColorRole: return getBackgroundColor(item);
		case Qt::ToolTipRole: return getToolTip(item);
		case StatusTextRole: return getStatusText(item);
		case AvatarRole: return getAvatar(item);
		case PresenceIconRole: return getPresenceIcon(item);
		case ChildCountRole: return getChildCount(item);
		case IdleRole: return getIsIdle(item);
		default: return QVariant();
	}
}

int RosterModel::getChildCount(RosterItem* item) const {
	GroupRosterItem* group = dynamic_cast<GroupRosterItem*>(item);
	return group ? group->getDisplayedChildren().size() : 0; 
}

bool RosterModel::getIsIdle(RosterItem* item) const {
	ContactRosterItem* contact = dynamic_cast<ContactRosterItem*>(item);
	return contact ? !contact->getIdleText().empty() : false;
}

QColor RosterModel::intToColor(int color) const {
	return QColor(
		((color & 0xFF0000)>>16),
		((color & 0xFF00)>>8), 
		(color & 0xFF));
}

QColor RosterModel::getTextColor(RosterItem* item) const {
	ContactRosterItem* contact = dynamic_cast<ContactRosterItem*>(item);
	int color = 0;
	if (contact) {
		switch (contact->getStatusShow()) {
			case StatusShow::Online: color = 0x000000; break;
			case StatusShow::Away: color = 0x336699; break;
			case StatusShow::XA: color = 0x336699; break;
			case StatusShow::FFC: color = 0x000000; break;
			case StatusShow::DND: color = 0x990000; break;
			case StatusShow::None: color = 0x7F7F7F;break;
		}
	}
	return intToColor(color);
}

QColor RosterModel::getBackgroundColor(RosterItem* item) const {
	return dynamic_cast<ContactRosterItem*>(item) ? intToColor(0xFFFFFF) : intToColor(0x969696);
}

QString RosterModel::getToolTip(RosterItem* item) const {
	QString tip(P2QSTRING(item->getDisplayName()));
	ContactRosterItem* contact = dynamic_cast<ContactRosterItem*>(item);
	if (contact) {
		if (contact->getDisplayJID().isValid()) {
			tip += "\n" + P2QSTRING(contact->getDisplayJID().toBare().toString());
		}
		tip += "\n " + P2QSTRING(statusShowTypeToFriendlyName(contact->getStatusShow()));
		if (!getStatusText(item).isEmpty()) {
			tip += ": " + getStatusText(item);
		}
		if (!contact->getIdleText().empty()) {
			tip += "\n " + tr("Idle since ") + P2QSTRING(contact->getIdleText());
		}
	}
	return tip;
}

QString RosterModel::getAvatar(RosterItem* item) const {
	ContactRosterItem* contact = dynamic_cast<ContactRosterItem*>(item);
	if (!contact) {
		return "";
	}
	return QString(contact->getAvatarPath().c_str());
}

QString RosterModel::getStatusText(RosterItem* item) const {
	ContactRosterItem* contact = dynamic_cast<ContactRosterItem*>(item);
	if (!contact) return "";
	return P2QSTRING(contact->getStatusText());
}

QIcon RosterModel::getPresenceIcon(RosterItem* item) const {
	ContactRosterItem* contact = dynamic_cast<ContactRosterItem*>(item);
	if (!contact) return QIcon();
	QString iconString;
	switch (contact->getStatusShow()) {
		case StatusShow::Online: iconString = "online";break;
		case StatusShow::Away: iconString = "away";break;
		case StatusShow::XA: iconString = "away";break;
		case StatusShow::FFC: iconString = "online";break;
		case StatusShow::DND: iconString = "dnd";break;
		case StatusShow::None: iconString = "offline";break;
	}
	return QIcon(":/icons/" + iconString + ".png");
}


QModelIndex RosterModel::index(int row, int column, const QModelIndex& parent) const {
	if (!roster_) {
		return QModelIndex();
	}
	GroupRosterItem* parentItem;
	if (!parent.isValid()) {
		//top level
		parentItem = roster_->getRoot();
	} else {
		parentItem = dynamic_cast<GroupRosterItem*>(getItem(parent));
		if (!parentItem) return QModelIndex();
	}
	return static_cast<size_t>(row) < parentItem->getDisplayedChildren().size() ? createIndex(row, column, parentItem->getDisplayedChildren()[row]) : QModelIndex();
}

QModelIndex RosterModel::index(RosterItem* item) const {
	GroupRosterItem* parent = item->getParent();
	/* Recursive check that it's ok to create such an item 
		Assuming there are more contacts in a group than groups in a 
		group, this could save a decent chunk of search time at startup.*/
	if (parent == NULL || roster_ == NULL || (parent != roster_->getRoot() && !index(parent).isValid())) {
		return QModelIndex();
	}
	for (size_t i = 0; i < parent->getDisplayedChildren().size(); i++) {
		if (parent->getDisplayedChildren()[i] == item) {
			return createIndex(i, 0, item);
		}
	}
	return QModelIndex();
}

QModelIndex RosterModel::parent(const QModelIndex& child) const {
	if (!roster_ || !child.isValid()) {
		return QModelIndex();
	}
	
	GroupRosterItem* parent = getItem(child)->getParent();
	return (parent != roster_->getRoot()) ? index(parent) : QModelIndex();
}

int RosterModel::rowCount(const QModelIndex& parent) const {
	if (!roster_) return 0;
	RosterItem* item = parent.isValid() ? static_cast<RosterItem*>(parent.internalPointer()) : roster_->getRoot();
	Q_ASSERT(item);
	GroupRosterItem* group = dynamic_cast<GroupRosterItem*>(item);
	int count = group ? group->getDisplayedChildren().size() : 0;
//	qDebug() << "rowCount = " << count << " where parent.isValid() == " << parent.isValid() << ", group == " << (group ? P2QSTRING(group->getDisplayName()) : "*contact*");
	return count;
}

}