-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKFonbookModel.cpp
More file actions
310 lines (279 loc) · 9.09 KB
/
KFonbookModel.cpp
File metadata and controls
310 lines (279 loc) · 9.09 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
* KFritz
*
* Copyright (C) 2010-2012 Joachim Wilke <kfritz@joachim-wilke.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "KFonbookModel.h"
#include <QFont>
#include <KIcon>
#include <KLocalizedString>
#include "liblog++/Log.h"
/**
* KFonbookModel
*/
enum modelColumns {
COLUMN_NAME,
COLUMN_NUMBER_FIRST,
COLUMN_NUMBER_2,
COLUMN_NUMBER_LAST,
COLUMN_QUICKDIAL,
COLUMN_VANITY,
COLUMNS_COUNT
};
KFonbookModel::KFonbookModel(std::string techID) {
// get the fonbook resource
fritz::Fonbooks *books = fritz::FonbookManager::GetFonbookManager()->getFonbooks();
fonbook = (*books)[techID];
lastRows = 0;
}
KFonbookModel::~KFonbookModel() {
}
int KFonbookModel::rowCount(const QModelIndex & parent) const
{
if (parent.isValid())
return 0;
return fonbook->getFonbookSize();
}
int KFonbookModel::columnCount(const QModelIndex & parent) const
{
if (parent.isValid())
return 0;
return COLUMNS_COUNT;
}
QVariant KFonbookModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role != Qt::DisplayRole)
return QVariant();
if (orientation == Qt::Horizontal) {
switch (section) {
case COLUMN_NAME:
return i18n("Name");
case COLUMN_NUMBER_FIRST:
return i18n("Number 1");
case COLUMN_NUMBER_2:
return i18n("Number 2");
case COLUMN_NUMBER_LAST:
return i18n("Number 3");
case COLUMN_QUICKDIAL:
return i18n("Quickdial");
case COLUMN_VANITY:
return i18n("Vanity");
default:
return QVariant();
}
} else
return QVariant();
}
QVariant KFonbookModel::data(const QModelIndex & index, int role) const {
// Neither top level nor details table
if (index.parent().isValid())
return QVariant();
const fritz::FonbookEntry *fe = fonbook->retrieveFonbookEntry(index.row());
if (!fe) {
ERR("No FonbookEntry for index row " << index.row());
return QVariant();
}
// Indicate important contacts using icon and tooltip
if (role == Qt::DecorationRole && index.column() == COLUMN_NAME)
return QVariant(fe->isImportant() ? KIcon("emblem-important") : KIcon("x-office-contact"));
if (role == Qt::ToolTipRole && index.column() == COLUMN_NAME)
return QVariant(fe->isImportant() ? i18n("Important contact") : "");
// Indicate default number using bold font face and tooltip
if (role == Qt::FontRole || role == Qt::ToolTipRole) {
bool defaultNumber = false;
fritz::FonbookEntry::eType type = fritz::FonbookEntry::TYPE_NONE;
switch(index.column()) {
case COLUMN_NUMBER_FIRST ... COLUMN_NUMBER_LAST:
defaultNumber = (fe->getPriority(index.column() - COLUMN_NUMBER_FIRST) == 1);
type = fe->getType(index.column() - COLUMN_NUMBER_FIRST);
}
QString tooltip = getTypeName(type);
if (defaultNumber) {
if (role == Qt::FontRole) {
QFont font;
font.setBold(true);
return font;
}
tooltip += " ("+ (i18n("Default number")) + ")";
}
if (role == Qt::ToolTipRole)
return tooltip;
}
// Skip all other requests, except for displayed text
if (role != Qt::DisplayRole && role != Qt::EditRole)
return QVariant();
switch (index.column()) {
case COLUMN_NAME:
return QVariant(toUnicode(fe->getName()));
case COLUMN_NUMBER_FIRST ... COLUMN_NUMBER_LAST:
return QVariant(toUnicode(fe->getNumber(index.column() - COLUMN_NUMBER_FIRST)));
case COLUMN_QUICKDIAL:
if (role == Qt::EditRole)
return QVariant(toUnicode(fe->getQuickdial()));
else
return QVariant(toUnicode(fe->getQuickdialFormatted()));
case COLUMN_VANITY:
if (role == Qt::EditRole)
return QVariant(toUnicode(fe->getVanity()));
else
return QVariant(toUnicode(fe->getVanityFormatted()));
default:
return QVariant();
}
}
bool KFonbookModel::setData (const QModelIndex & index, const QVariant & value, int role) {
if (role == Qt::EditRole) {
const fritz::FonbookEntry *_fe = fonbook->retrieveFonbookEntry(index.row());
fritz::FonbookEntry fe(*_fe);
switch(index.column()) {
case COLUMN_NAME:
fe.setName(fromUnicode(value.toString()));
break;
case COLUMN_NUMBER_FIRST ... COLUMN_NUMBER_LAST:
fe.setNumber(fromUnicode(value.toString()), index.column() - COLUMN_NUMBER_FIRST);
break;
case COLUMN_QUICKDIAL:
fe.setQuickdial(fromUnicode(value.toString())); //TODO: check if unique
break;
case COLUMN_VANITY:
fe.setVanity(fromUnicode(value.toString())); //TODO: check if unique
break;
default:
return false;
}
fonbook->changeFonbookEntry(index.row(), fe);
emit dataChanged(index, index); // we changed one element
return true;
}
return false;
}
void KFonbookModel::setDefault(const QModelIndex &index) {
switch(index.column()) {
case COLUMN_NUMBER_FIRST ... COLUMN_NUMBER_LAST:
fonbook->setDefault(index.row(), index.column() - COLUMN_NUMBER_FIRST);
break;
default:
return;
}
QModelIndex indexLeft = createIndex(index.row(), COLUMN_NUMBER_FIRST);
QModelIndex indexRight = createIndex(index.row(), COLUMN_NUMBER_LAST);
emit dataChanged(indexLeft, indexRight); // we changed up to three elements
}
size_t KFonbookModel::mapColumnToNumberIndex(int column) {
return column-1;
}
void KFonbookModel::setType(const QModelIndex &index, fritz::FonbookEntry::eType type) {
const fritz::FonbookEntry *_fe = fonbook->retrieveFonbookEntry(index.row());
fritz::FonbookEntry fe(*_fe);
fe.setType(type, mapColumnToNumberIndex(index.column()));
fonbook->changeFonbookEntry(index.row(), fe);
emit dataChanged(index, index);
}
bool KFonbookModel::insertRows(int row, int count __attribute__((unused)), const QModelIndex &parent) {
beginInsertRows(parent, row, row);
fritz::FonbookEntry fe(i18n("New Entry").toStdString());
fonbook->addFonbookEntry(fe, row);
endInsertRows();
return true;
}
bool KFonbookModel::insertFonbookEntry(const QModelIndex &index, fritz::FonbookEntry &fe) {
beginInsertRows(QModelIndex(), index.row(), index.row());
fonbook->addFonbookEntry(fe, index.row());
endInsertRows();
return true;
}
const fritz::FonbookEntry *KFonbookModel::retrieveFonbookEntry(const QModelIndex &index) const {
return fonbook->retrieveFonbookEntry(index.row());
}
bool KFonbookModel::removeRows(int row, int count __attribute__((unused)), const QModelIndex &parent) {
beginRemoveRows(parent,row,row);
if(fonbook->deleteFonbookEntry(row)){
endRemoveRows();
return true;
} else
return false;
}
Qt::ItemFlags KFonbookModel::flags(const QModelIndex & index) const {
if (fonbook->isWriteable())
return Qt::ItemFlags(QAbstractItemModel::flags(index) | QFlag(Qt::ItemIsEditable));
else
return QAbstractItemModel::flags(index);
}
QString KFonbookModel::getTypeName(const fritz::FonbookEntry::eType type) {
switch (type){
case fritz::FonbookEntry::TYPE_HOME:
return i18n("Home");
case fritz::FonbookEntry::TYPE_MOBILE:
return i18n("Mobile");
case fritz::FonbookEntry::TYPE_WORK:
return i18n("Work");
default:
return "";
}
}
void KFonbookModel::sort(int column, Qt::SortOrder order) {
fritz::FonbookEntry::eElements element;
switch (column) {
case COLUMN_NAME:
element = fritz::FonbookEntry::ELEM_NAME;
break;
case COLUMN_NUMBER_FIRST ... COLUMN_NUMBER_LAST:
return; //TODO: sorting - does this need anybody?
case COLUMN_QUICKDIAL:
element = fritz::FonbookEntry::ELEM_QUICKDIAL;
break;
case COLUMN_VANITY:
element = fritz::FonbookEntry::ELEM_VANITY;
break;
default:
ERR("Invalid column addressed while sorting.");
return;
}
fonbook->sort(element, order == Qt::AscendingOrder);
emit dataChanged(index(0, 0, QModelIndex()),
index(rowCount(QModelIndex()), columnCount(QModelIndex()), QModelIndex()));
}
std::string KFonbookModel::number(const QModelIndex &i) const {
if (!i.parent().isValid()) {
const fritz::FonbookEntry *fe = fonbook->retrieveFonbookEntry(i.row());
switch (i.column()) {
case COLUMN_NUMBER_FIRST ... COLUMN_NUMBER_LAST:
return fe->getNumber(i.column() - COLUMN_NUMBER_FIRST);
default:
return "";
}
}
return "";
}
QModelIndex KFonbookModel::index(int row, int column, const QModelIndex &parent) const {
if (parent.isValid())
return QModelIndex();
return createIndex(row, column);
}
QModelIndex KFonbookModel::parent(const QModelIndex &child __attribute__((unused))) const {
return QModelIndex();
}
void KFonbookModel::check() {
if (lastRows != rowCount(QModelIndex())) {
reset();
emit updated();
// stop timer, because no more changes are expected
timer->stop();
lastRows = rowCount(QModelIndex());
}
}