2 * Copyright (C) 2011 Lukáš Karas <lukas.karas@centrum.cz>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include "../backend/telepathy-client.h"
22 #include "../backend/session.h"
23 #include "../backend/voip/element-factory/defaultelementfactory.h"
24 #include "../backend/voip/element-factory/deviceelementfactory.h"
25 #include "../backend/accounts-model-item.h"
26 #include "../backend/accounts-model.h"
28 #include "contacts-model-proxy.h"
30 ContactsModelProxy::ContactsModelProxy(
32 MaknetoBackend::AccountsModel
* accountsModel
)
33 : QAbstractItemModel(parent
) {
39 _accountsModel
= accountsModel
;
40 connect(_accountsModel
,
41 SIGNAL(accountCountChanged()),
42 SLOT(onAccountModelChanged()));
44 connect(_accountsModel
,
45 SIGNAL(rowsAboutToBeInserted(QModelIndex
, int, int)),
46 SLOT(onRowsAboutToBeInserted(QModelIndex
, int, int)));
48 connect(_accountsModel
,
49 SIGNAL(rowsInserted(QModelIndex
, int, int)),
50 SLOT(onRowsInserted(QModelIndex
, int, int)));
52 connect(_accountsModel
,
53 SIGNAL(rowsAboutToBeRemoved(QModelIndex
, int, int)),
54 SLOT(onRowsAboutToBeRemoved(QModelIndex
, int, int)));
56 connect(_accountsModel
,
57 SIGNAL(rowsRemoved(QModelIndex
, int, int)),
58 SLOT(onRowsRemoved(QModelIndex
, int, int)));
60 connect(_accountsModel
,
61 SIGNAL(dataChanged(QModelIndex
, QModelIndex
)),
62 SLOT(onDataChanged(QModelIndex
, QModelIndex
)));
64 setRoleNames(_accountsModel
->roleNames());
65 onAccountModelChanged();
68 int ContactsModelProxy::contactCount() const {
69 return _indexesMapping
.size();
72 QModelIndex
ContactsModelProxy::index(int row
, int column
, const QModelIndex
&parent
) const {
73 return createIndex(row
, column
, 0);
76 QModelIndex
ContactsModelProxy::parent(const QModelIndex
&index
) const {
77 // only one depth, return root node
81 int ContactsModelProxy::rowCount(const QModelIndex
&parent
) const {
82 return _indexesMapping
.size();
85 int ContactsModelProxy::columnCount(const QModelIndex
&parent
) const {
89 QVariant
ContactsModelProxy::data(const QModelIndex
&index
, int role
) const {
90 QModelIndex foreingIndex
= _indexesMapping
[ index
];
91 return _accountsModel
->data(foreingIndex
, role
);
94 QVariant
ContactsModelProxy::data(int row
, int role
) {
95 return data(index(row
, 0, QModelIndex()), role
);
98 void ContactsModelProxy::onAccountModelChanged() {
99 _indexesMapping
.clear();
100 _accountOffset
.clear();
102 for (int accountRow
= 0; accountRow
< _accountsModel
->accountCount(); accountRow
++) {
103 QModelIndex accountIndex
= _accountsModel
->index(accountRow
);
104 _accountOffset
.insert(accountIndex
, myRow
);
105 for (int contactRow
= 0; contactRow
< _accountsModel
->rowCount(accountIndex
); contactRow
++) {
106 QModelIndex contactIndex
= QPersistentModelIndex(_accountsModel
->index(contactRow
, 0, accountIndex
));
107 //QVariant var = _accountModel->data(contactIndex, MaknetoBackend::AccountsModel::IdRole);
108 //qDebug() << " " << var;
110 QModelIndex myIndex
= index(myRow
, 0);
111 _indexesMapping
.insert(myIndex
, contactIndex
);
115 emit
contactCountChanged();
118 void ContactsModelProxy::onRowsAboutToBeInserted(const QModelIndex
&parent
, int first
, int last
) {
119 if (parent
== QModelIndex()) // account insert... we ignore this event
123 int offset
= _accountOffset
.value(parent
);
124 last
= offset
+ first
+ last
;
125 first
= offset
+ first
;
127 emit
beginInsertRows(QModelIndex(), first
, last
);
130 void ContactsModelProxy::onRowsInserted(const QModelIndex
&parent
, int first
, int last
) {
135 onAccountModelChanged();
138 int offset
= _accountOffset
.value(parent
);
139 last
= offset
+ first
+ last
;
140 first
= offset
+ first
;
141 qDebug() << "ContactsModel: contacts rows inserted " << first
<< last
;
143 emit
endInsertRows();
146 void ContactsModelProxy::onRowsAboutToBeRemoved(const QModelIndex
&parent
, int first
, int last
) {
147 if (parent
== QModelIndex()){ // accounts remove... FIXME: what we should do? remove all related contacts?
148 qWarning() << "FIXME: accounts removes from model!? what we should do? remove all related contacts?";
153 int offset
= _accountOffset
.value(parent
);
154 last
= offset
+ first
+ last
;
155 first
= offset
+ first
;
156 emit
beginRemoveRows(QModelIndex(), first
, last
);
159 void ContactsModelProxy::onRowsRemoved(const QModelIndex
&parent
, int first
, int last
) {
164 onAccountModelChanged();
167 int offset
= _accountOffset
.value(parent
);
168 last
= offset
+ first
+ last
;
169 first
= offset
+ first
;
170 qDebug() << "ContactsModel: contacts rows removed " << first
<< last
;
172 emit
endRemoveRows();
175 void ContactsModelProxy::onDataChanged(const QModelIndex
&topLeft
, const QModelIndex
&bottomRight
) {
176 QModelIndex myFirst
= _indexesMapping
.key(topLeft
);
177 if (!myFirst
.isValid())
179 QModelIndex myLast
= _indexesMapping
.key(bottomRight
);
180 if (!myLast
.isValid())
182 emit
dataChanged(myFirst
, myLast
);
185 #include "contacts-model-proxy.moc"