diff options
| author | Tobias Markmann <tm@ayena.de> | 2013-04-30 20:10:12 (GMT) | 
|---|---|---|
| committer | Swift Review <review@swift.im> | 2013-10-01 14:48:44 (GMT) | 
| commit | fd47dd7d5cec5155b9985959d2f0e0f3b386cd98 (patch) | |
| tree | d4da57ce3f1c90f56701cab8757d75e089473c9e /Swift/QtUI/UserSearch/ContactListModel.cpp | |
| parent | a3781c5b770c03ff32c5cf8f1280b21c2a8e2626 (diff) | |
| download | swift-contrib-fd47dd7d5cec5155b9985959d2f0e0f3b386cd98.zip swift-contrib-fd47dd7d5cec5155b9985959d2f0e0f3b386cd98.tar.bz2 | |
Adding support for impromptu MUCs.
Change-Id: I363e9d740bbec311454827645f4ea6df8bb60bed
License: This patch is BSD-licensed, see Documentation/Licenses/BSD-simplified.txt for details.
Diffstat (limited to 'Swift/QtUI/UserSearch/ContactListModel.cpp')
| -rw-r--r-- | Swift/QtUI/UserSearch/ContactListModel.cpp | 173 | 
1 files changed, 173 insertions, 0 deletions
| diff --git a/Swift/QtUI/UserSearch/ContactListModel.cpp b/Swift/QtUI/UserSearch/ContactListModel.cpp new file mode 100644 index 0000000..6523a4d --- /dev/null +++ b/Swift/QtUI/UserSearch/ContactListModel.cpp @@ -0,0 +1,173 @@ +/* + * Copyright (c) 2013 Tobias Markmann + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#include <Swift/QtUI/UserSearch/ContactListModel.h> + +#include <Swift/QtUI/QtSwiftUtil.h> +#include <Swiften/Base/Path.h> +#include <Swiften/Base/foreach.h> +#include <Swiften/Elements/StatusShow.h> + +#include <QMimeData> + +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(); +	} +} + +bool ContactListModel::dropMimeData(const QMimeData* data, Qt::DropAction /*action*/, int /*row*/, int /*column*/, const QModelIndex& /*parent*/) { +	if (!data->hasFormat("application/vnd.swift.contact-jid")) { +		return false; +	} + +	QByteArray dataBytes = data->data("application/vnd.swift.contact-jid"); +	QDataStream dataStream(&dataBytes, QIODevice::ReadOnly); +	QString jidString; +	QString displayName; +	QString statusText; +	StatusShow::Type statusType; +	QString avatarPath; + +	dataStream >> jidString; +	dataStream >> displayName; +	dataStream >> statusText; +	dataStream >> statusType; +	dataStream >> avatarPath; + +	JID jid = JID(Q2PSTRING(jidString)); + +	foreach(const Contact& contact, contacts_) { +		if (contact.jid == jid) { +			return false; +		} +	} + +	emit layoutAboutToBeChanged(); +	contacts_.push_back(Contact(Q2PSTRING(displayName), jid, statusType, Q2PSTRING(avatarPath))); +	emit layoutChanged(); + +	onJIDsDropped(std::vector<JID>(1, jid)); +	onListChanged(getList()); + +	return true; +} + +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 { +	QString iconString; +	switch (contact.statusType) { +		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"); +} + +} | 
 Swift
 Swift