summaryrefslogtreecommitdiffstats
blob: b7ce239757c3615f85404ac2a6705324da3995ef (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
/*
 * Copyright (c) 2010-2014 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <Swift/QtUI/ChatList/ChatListModel.h>

#include <QMimeData>
#include <QUrl>

#include <Swift/QtUI/ChatList/ChatListMUCItem.h>
#include <Swift/QtUI/ChatList/ChatListRecentItem.h>
#include <Swift/QtUI/ChatList/ChatListWhiteboardItem.h>
#include <Swift/QtUI/QtSwiftUtil.h>

namespace Swift {

ChatListModel::ChatListModel() : whiteboards_(NULL) {
	root_ = new ChatListGroupItem("", NULL, false);
	mucBookmarks_ = new ChatListGroupItem(tr("Bookmarked Rooms"), root_);
	recents_ = new ChatListGroupItem(tr("Recent Chats"), root_, false);
#ifdef SWIFT_EXPERIMENTAL_WB
	whiteboards_ = new ChatListGroupItem(tr("Opened Whiteboards"), root_, false);
	root_->addItem(whiteboards_);
#endif

	root_->addItem(recents_);
	root_->addItem(mucBookmarks_);

	QModelIndex idx = index(0, 0, QModelIndex());
	while (idx.isValid()) {
		if (idx.internalPointer() == mucBookmarks_) {
			mucBookmarksIndex_ = idx;
		} else if (idx.internalPointer() == recents_) {
			recentsIndex_ = idx;
		} else if (idx.internalPointer() == whiteboards_) {
			whiteboardsIndex_ = idx;
		}
		idx = index(idx.row() + 1, 0, QModelIndex());
	}
}

Qt::ItemFlags ChatListModel::flags(const QModelIndex& index) const {
	Qt::ItemFlags flags = QAbstractItemModel::flags(index);
	if (dynamic_cast<ChatListRecentItem*>(getItemForIndex(index))) {
		flags |= Qt::ItemIsDragEnabled;
	}
	return flags;
}

void ChatListModel::clearBookmarks() {
	beginRemoveRows(mucBookmarksIndex_, 0, mucBookmarks_->rowCount());
	mucBookmarks_->clear();
	endRemoveRows();
}

void ChatListModel::addMUCBookmark(const Swift::MUCBookmark& bookmark) {
	beginInsertRows(mucBookmarksIndex_, 0, mucBookmarks_->rowCount());
	mucBookmarks_->addItem(new ChatListMUCItem(bookmark, mucBookmarks_));
	endInsertRows();
}

void ChatListModel::removeMUCBookmark(const Swift::MUCBookmark& bookmark) {
	for (int i = 0; i < mucBookmarks_->rowCount(); i++) {
		ChatListMUCItem* item = dynamic_cast<ChatListMUCItem*>(mucBookmarks_->item(i));
		if (item->getBookmark() == bookmark) {
			beginRemoveRows(mucBookmarksIndex_, i, i+1);
			mucBookmarks_->remove(i);
			endRemoveRows();
			break;
		}
	}
}

void ChatListModel::addWhiteboardSession(const ChatListWindow::Chat& chat) {
	beginInsertRows(whiteboardsIndex_, 0, whiteboards_->rowCount());
	whiteboards_->addItem(new ChatListWhiteboardItem(chat, whiteboards_));
	endInsertRows();
}

void ChatListModel::removeWhiteboardSession(const JID& jid) {
	for (int i = 0; i < whiteboards_->rowCount(); i++) {
		ChatListWhiteboardItem* item = dynamic_cast<ChatListWhiteboardItem*>(whiteboards_->item(i));
		if (item->getChat().jid == jid) {
			beginRemoveRows(whiteboardsIndex_, i, i+1);
			whiteboards_->remove(i);
			endRemoveRows();
			break;
		}
	}
}

void ChatListModel::setRecents(const std::list<ChatListWindow::Chat>& recents) {
	beginRemoveRows(recentsIndex_, 0, recents_->rowCount());
	recents_->clear();
	endRemoveRows();
	beginInsertRows(recentsIndex_, 0, recents.size());
	foreach (const ChatListWindow::Chat chat, recents) {
		recents_->addItem(new ChatListRecentItem(chat, recents_));
//whiteboards_->addItem(new ChatListRecentItem(chat, whiteboards_));
	}
	endInsertRows();
}

QMimeData* ChatListModel::mimeData(const QModelIndexList& indexes) const {
	QMimeData* data = QAbstractItemModel::mimeData(indexes);
	ChatListRecentItem *item = dynamic_cast<ChatListRecentItem*>(getItemForIndex(indexes.first()));
	if (item == NULL) {
		return data;
	}

	QByteArray itemData;
	QDataStream dataStream(&itemData, QIODevice::WriteOnly);
	const ChatListWindow::Chat& chat = item->getChat();

	QString mimeType = "application/vnd.swift.contact-jid-list";
	if (!chat.impromptuJIDs.size()) {
		if (chat.isMUC) {
			mimeType = "application/vnd.swift.contact-jid-muc";
		}
		dataStream << P2QSTRING(chat.jid.toString());
	} else {
		typedef std::map<std::string, JID> JIDMap;
		foreach (const JIDMap::value_type& jid, chat.impromptuJIDs) {
			dataStream << P2QSTRING(jid.second.toString());
		}
	}

	data->setData(mimeType, itemData);
	return data;
}

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

ChatListItem* ChatListModel::getItemForIndex(const QModelIndex& index) const {
	return index.isValid() ? static_cast<ChatListItem*>(index.internalPointer()) : NULL;
}

QVariant ChatListModel::data(const QModelIndex& index, int role) const {
	ChatListItem* item = getItemForIndex(index);
	return item ? item->data(role) : QVariant();
}

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

	ChatListGroupItem *parentItem = parent.isValid() ? static_cast<ChatListGroupItem*>(parent.internalPointer()) : root_;

	return row < parentItem->rowCount() ? createIndex(row, column, parentItem->item(row)) : QModelIndex();
}

QModelIndex ChatListModel::parent(const QModelIndex& index) const {
	if (!index.isValid()) {
		return QModelIndex();
	}
	ChatListGroupItem* parent = static_cast<ChatListGroupItem*>(index.internalPointer())->parent();
	return (parent == root_) ? QModelIndex() : createIndex(parent->parent()->row(parent), 0, parent);
}

int ChatListModel::rowCount(const QModelIndex& parentIndex) const {
	ChatListGroupItem* parent = NULL;
	if (parentIndex.isValid()) {
		parent = dynamic_cast<ChatListGroupItem*>(static_cast<ChatListItem*>(parentIndex.internalPointer()));
	} else {
		parent = root_;
	}
	int count = (parent ? parent->rowCount() : 0);
	return count;
}

}