contactstreemodel.cpp 9.22 KB
Newer Older
1
2
3
4
5
6
/*
    This file is part of Akonadi Contact.

    Copyright (c) 2009 Stephen Kelly <steveire@gmail.com>
    Copyright (c) 2009 Tobias Koenig <tokoe@kde.org>

Tobias Koenig's avatar
Tobias Koenig committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    This library is free software; you can redistribute it and/or modify it
    under the terms of the GNU Library General Public License as published by
    the Free Software Foundation; either version 2 of the License, or (at your
    option) any later version.

    This library 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 Library General Public
    License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to the
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
21
22
23
24
*/

#include "contactstreemodel.h"

Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
25
26
#include <kcontacts/addressee.h>
#include <kcontacts/contactgroup.h>
Laurent Montel's avatar
Laurent Montel committed
27

28
#include <kiconloader.h>
Laurent Montel's avatar
Laurent Montel committed
29
#include <KLocalizedString>
30

Laurent Montel's avatar
Laurent Montel committed
31
#include <QIcon>
Laurent Montel's avatar
Laurent Montel committed
32
#include <QLocale>
Laurent Montel's avatar
Laurent Montel committed
33

34
35
using namespace Akonadi;

36
class Q_DECL_HIDDEN ContactsTreeModel::Private
37
{
Guy Maurel's avatar
Guy Maurel committed
38
public:
39
    Private()
Guy Maurel's avatar
Guy Maurel committed
40
41
        : mColumns(ContactsTreeModel::Columns() << ContactsTreeModel::FullName)
        , mIconSize(KIconLoader::global()->currentSize(KIconLoader::Small))
42
43
44
45
    {
    }

    Columns mColumns;
46
    const int mIconSize;
47
48
};

49
ContactsTreeModel::ContactsTreeModel(Monitor *monitor, QObject *parent)
Guy Maurel's avatar
Guy Maurel committed
50
51
    : EntityTreeModel(monitor, parent)
    , d(new Private)
52
53
54
55
56
{
}

ContactsTreeModel::~ContactsTreeModel()
{
Guy Maurel's avatar
Guy Maurel committed
57
    delete d;
58
59
}

Guy Maurel's avatar
Guy Maurel committed
60
void ContactsTreeModel::setColumns(const Columns &columns)
61
{
Laurent Montel's avatar
Laurent Montel committed
62
    Q_EMIT beginResetModel();
Guy Maurel's avatar
Guy Maurel committed
63
    d->mColumns = columns;
Laurent Montel's avatar
Laurent Montel committed
64
    Q_EMIT endResetModel();
65
66
67
68
}

ContactsTreeModel::Columns ContactsTreeModel::columns() const
{
Guy Maurel's avatar
Guy Maurel committed
69
    return d->mColumns;
70
71
}

Guy Maurel's avatar
Guy Maurel committed
72
QVariant ContactsTreeModel::entityData(const Item &item, int column, int role) const
73
{
Guy Maurel's avatar
Guy Maurel committed
74
75
    if (item.mimeType() == KContacts::Addressee::mimeType()) {
        if (!item.hasPayload<KContacts::Addressee>()) {
76

Guy Maurel's avatar
Guy Maurel committed
77
78
79
80
            // Pass modeltest
            if (role == Qt::DisplayRole) {
                return item.remoteId();
            }
81

Guy Maurel's avatar
Guy Maurel committed
82
83
            return QVariant();
        }
84

Guy Maurel's avatar
Guy Maurel committed
85
        const KContacts::Addressee contact = item.payload<KContacts::Addressee>();
86

Guy Maurel's avatar
Guy Maurel committed
87
88
89
90
91
92
        if (role == Qt::DecorationRole) {
            if (column == 0) {
                const KContacts::Picture picture = contact.photo();
                if (picture.isIntern()) {
                    return picture.data().scaled(QSize(d->mIconSize, d->mIconSize), Qt::KeepAspectRatio);
                } else {
Laurent Montel's avatar
Laurent Montel committed
93
                    return QIcon::fromTheme(QStringLiteral("user-identity"));
Guy Maurel's avatar
Guy Maurel committed
94
                }
95
            }
Guy Maurel's avatar
Guy Maurel committed
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
            return QVariant();
        } else if ((role == Qt::DisplayRole) || (role == Qt::EditRole)) {
            switch (d->mColumns.at(column)) {
            case FullName:
                if (contact.realName().isEmpty()) {
                    if (contact.preferredEmail().isEmpty()) {
                        return contact.familyName();
                    }
                    return contact.preferredEmail();
                }
                return contact.realName();
            case FamilyName:
                return contact.familyName();
            case GivenName:
                return contact.givenName();
            case Birthday:
                if (contact.birthday().date().isValid()) {
                    return QLocale().toString(contact.birthday().date(), QLocale::ShortFormat);
                }
                break;
            case HomeAddress: {
                const KContacts::Address address = contact.address(KContacts::Address::Home);
                if (!address.isEmpty()) {
                    return address.formattedAddress();
                }
                break;
122
            }
Guy Maurel's avatar
Guy Maurel committed
123
124
125
126
127
128
            case BusinessAddress: {
                const KContacts::Address address = contact.address(KContacts::Address::Work);
                if (!address.isEmpty()) {
                    return address.formattedAddress();
                }
                break;
129
            }
Guy Maurel's avatar
Guy Maurel committed
130
131
            case PhoneNumbers: {
                QStringList values;
132

Guy Maurel's avatar
Guy Maurel committed
133
                const KContacts::PhoneNumber::List numbers = contact.phoneNumbers();
Laurent Montel's avatar
Laurent Montel committed
134
                for (const KContacts::PhoneNumber &number : numbers) {
Guy Maurel's avatar
Guy Maurel committed
135
136
                    values += number.number();
                }
137

Laurent Montel's avatar
Laurent Montel committed
138
                return values.join(QLatin1Char('\n'));
Guy Maurel's avatar
Guy Maurel committed
139
140
141
142
143
                break;
            }
            case PreferredEmail:
                return contact.preferredEmail();
            case AllEmails:
Laurent Montel's avatar
Laurent Montel committed
144
                return contact.emails().join(QLatin1Char('\n'));
Guy Maurel's avatar
Guy Maurel committed
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
            case Organization:
                return contact.organization();
            case Role:
                return contact.role();
            case Homepage:
                return contact.url().url();
            case Note:
                return contact.note();
            }
        } else if (role == DateRole) {
            if (d->mColumns.at(column) == Birthday) {
                return contact.birthday();
            } else {
                return QDate();
            }
        }
    } else if (item.mimeType() == KContacts::ContactGroup::mimeType()) {
        if (!item.hasPayload<KContacts::ContactGroup>()) {
163

Guy Maurel's avatar
Guy Maurel committed
164
165
166
167
            // Pass modeltest
            if (role == Qt::DisplayRole) {
                return item.remoteId();
            }
168
169

            return QVariant();
Guy Maurel's avatar
Guy Maurel committed
170
171
172
173
        }

        if (role == Qt::DecorationRole) {
            if (column == 0) {
Laurent Montel's avatar
Laurent Montel committed
174
                return QIcon::fromTheme(QStringLiteral("x-mail-distribution-list"));
Guy Maurel's avatar
Guy Maurel committed
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
            } else {
                return QVariant();
            }
        } else if ((role == Qt::DisplayRole) || (role == Qt::EditRole)) {
            switch (d->mColumns.at(column)) {
            case FullName: {
                const KContacts::ContactGroup group = item.payload<KContacts::ContactGroup>();
                return group.name();
                break;
            }
            default:
                return QVariant();
                break;
            }
        }
190
191
    }

Guy Maurel's avatar
Guy Maurel committed
192
    return EntityTreeModel::entityData(item, column, role);
193
194
}

Guy Maurel's avatar
Guy Maurel committed
195
QVariant ContactsTreeModel::entityData(const Collection &collection, int column, int role) const
196
{
Guy Maurel's avatar
Guy Maurel committed
197
198
199
200
201
202
203
    if (role == Qt::DisplayRole) {
        switch (column) {
        case 0:
            return EntityTreeModel::entityData(collection, column, role);
        default:
            return QString(); // pass model test
        }
204
205
    }

Guy Maurel's avatar
Guy Maurel committed
206
    return EntityTreeModel::entityData(collection, column, role);
207
208
}

Guy Maurel's avatar
Guy Maurel committed
209
int ContactsTreeModel::entityColumnCount(HeaderGroup headerGroup) const
210
{
Guy Maurel's avatar
Guy Maurel committed
211
212
213
214
215
216
217
    if (headerGroup == EntityTreeModel::CollectionTreeHeaders) {
        return 1;
    } else if (headerGroup == EntityTreeModel::ItemListHeaders) {
        return d->mColumns.count();
    } else {
        return EntityTreeModel::entityColumnCount(headerGroup);
    }
218
219
}

Guy Maurel's avatar
Guy Maurel committed
220
QVariant ContactsTreeModel::entityHeaderData(int section, Qt::Orientation orientation, int role, HeaderGroup headerGroup) const
221
{
Guy Maurel's avatar
Guy Maurel committed
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
    if (role == Qt::DisplayRole) {
        if (orientation == Qt::Horizontal) {
            if (headerGroup == EntityTreeModel::CollectionTreeHeaders) {

                if (section >= 1) {
                    return QVariant();
                }

                switch (section) {
                case 0:
                    return i18nc("@title:column address books overview", "Address Books");
                    break;
                }
            } else if (headerGroup == EntityTreeModel::ItemListHeaders) {
                if (section < 0 || section >= d->mColumns.count()) {
                    return QVariant();
                }

                switch (d->mColumns.at(section)) {
                case FullName:
                    return i18nc("@title:column name of a person", "Name");
                case FamilyName:
                    return i18nc("@title:column family name of a person", "Family Name");
                case GivenName:
                    return i18nc("@title:column given name of a person", "Given Name");
                case Birthday:
                    return KContacts::Addressee::birthdayLabel();
                case HomeAddress:
                    return i18nc("@title:column home address of a person", "Home");
                case BusinessAddress:
                    return i18nc("@title:column work address of a person", "Work");
                case PhoneNumbers:
                    return i18nc("@title:column phone numbers of a person", "Phone Numbers");
                case PreferredEmail:
                    return i18nc("@title:column the preferred email addresses of a person", "Preferred EMail");
                case AllEmails:
                    return i18nc("@title:column all email addresses of a person", "All EMails");
                case Organization:
                    return KContacts::Addressee::organizationLabel();
                case Role:
                    return KContacts::Addressee::roleLabel();
                case Homepage:
                    return KContacts::Addressee::urlLabel();
                case Note:
                    return KContacts::Addressee::noteLabel();
                }
            }
269
270
271
        }
    }

Guy Maurel's avatar
Guy Maurel committed
272
    return EntityTreeModel::entityHeaderData(section, orientation, role, headerGroup);
273
}