summaryrefslogtreecommitdiffstats
blob: 907142f76dfb3c72d8b35a501cc60684981b6eb6 (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
/*
 * Copyright (c) 2013 Tobias Markmann
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

/*
 * Copyright (c) 2014 Kevin Smith and Remko Tronçon
 * Licensed under the GNU General Public License v3.
 * See Documentation/Licenses/GPLv3.txt for more information.
 */

#include <Swift/QtUI/UserSearch/ContactListModel.h>

#include <QMimeData>

#include <Swiften/Base/Path.h>
#include <Swiften/Base/foreach.h>
#include <Swiften/Elements/StatusShow.h>

#include <Swift/QtUI/QtSwiftUtil.h>
#include <Swift/QtUI/QtResourceHelper.h>

namespace Swift {

QDataStream& operator >>(QDataStream& in, StatusShow::Type& e){
	quint32 buffer;
	in >> buffer;
	switch(buffer) {
		case StatusShow::Online:
			e = StatusShow::Online;
			break;
		case StatusShow::Away:
			e = StatusShow::Away;
			break;
		case StatusShow::FFC:
			e = StatusShow::FFC;
			break;
		case StatusShow::XA:
			e = StatusShow::XA;
			break;
		case StatusShow::DND:
			e = StatusShow::DND;
			break;
		default:
			e = StatusShow::None;
			break;
	}
	return in;
}

ContactListModel::ContactListModel(bool editable) : QAbstractItemModel(), editable_(editable) {
}

void ContactListModel::setList(const std::vector<Contact>& list) {
	emit layoutAboutToBeChanged();
	contacts_ = list;
	emit layoutChanged();
}

const std::vector<Contact>& ContactListModel::getList() const {
	return contacts_;
}

Qt::ItemFlags ContactListModel::flags(const QModelIndex& index) const {
	Qt::ItemFlags flags = QAbstractItemModel::flags(index);
	if (index.isValid()) {
		flags = flags & ~Qt::ItemIsDropEnabled;
	} else {
		flags = Qt::ItemIsDropEnabled | flags;
	}
	return flags;
}

int ContactListModel::columnCount(const QModelIndex&) const {
	return editable_ ? 2 : 1;
}

QVariant ContactListModel::data(const QModelIndex& index, int role) const {
	if (boost::numeric_cast<size_t>(index.row()) < contacts_.size()) {
		const Contact& contact = contacts_[index.row()];
		if (role == Qt::EditRole) {
			return P2QSTRING(contact.jid.toString());
		}
		return dataForContact(contact, role);
	} else {
		return QVariant();
	}
}

QModelIndex ContactListModel::index(int row, int column, const QModelIndex& parent) const {
	if (!hasIndex(row, column, parent)) {
		return QModelIndex();
	}

	return boost::numeric_cast<size_t>(row) < contacts_.size() ? createIndex(row, column, (void*)&(contacts_[row])) : QModelIndex();
}

QModelIndex ContactListModel::parent(const QModelIndex& index) const {
	if (!index.isValid()) {
		return QModelIndex();
	}
	return QModelIndex();
}

int ContactListModel::rowCount(const QModelIndex& /*parent*/) const {
	return contacts_.size();
}

bool ContactListModel::removeRows(int row, int /*count*/, const QModelIndex& /*parent*/) {
	if (boost::numeric_cast<size_t>(row) < contacts_.size()) {
		emit layoutAboutToBeChanged();
		contacts_.erase(contacts_.begin() + row);
		emit layoutChanged();
		onListChanged(getList());
		return true;
	}
	return false;
}

QVariant ContactListModel::dataForContact(const Contact& contact, int role) const {
	switch (role) {
		case Qt::DisplayRole: return P2QSTRING(contact.name);
		case DetailTextRole: return P2QSTRING(contact.jid.toString());
		case AvatarRole: return QVariant(P2QSTRING(pathToString(contact.avatarPath)));
		case PresenceIconRole: return getPresenceIconForContact(contact);
		default: return QVariant();
	}
}

QIcon ContactListModel::getPresenceIconForContact(const Contact& contact) const {
	return QIcon(statusShowTypeToIconPath(contact.statusType));
}

}