|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the Qt Mobility Components. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:BSD$ |
|
10 ** You may use this file under the terms of the BSD license as follows: |
|
11 ** |
|
12 ** "Redistribution and use in source and binary forms, with or without |
|
13 ** modification, are permitted provided that the following conditions are |
|
14 ** met: |
|
15 ** * Redistributions of source code must retain the above copyright |
|
16 ** notice, this list of conditions and the following disclaimer. |
|
17 ** * Redistributions in binary form must reproduce the above copyright |
|
18 ** notice, this list of conditions and the following disclaimer in |
|
19 ** the documentation and/or other materials provided with the |
|
20 ** distribution. |
|
21 ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor |
|
22 ** the names of its contributors may be used to endorse or promote |
|
23 ** products derived from this software without specific prior written |
|
24 ** permission. |
|
25 ** |
|
26 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
27 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
28 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
29 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
30 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
31 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
32 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
33 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
34 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
35 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
36 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
|
37 ** $QT_END_LICENSE$ |
|
38 ** |
|
39 ****************************************************************************/ |
|
40 |
|
41 #include <qcontactdetails.h> |
|
42 |
|
43 #include "qmlcontactmodel.h" |
|
44 #include "qcontactmanager.h" |
|
45 #include "qcontactdetailfilter.h" |
|
46 #include "qversitreader.h" |
|
47 #include "qversitcontactimporter.h" |
|
48 |
|
49 #include <QColor> |
|
50 #include <QHash> |
|
51 #include <QDebug> |
|
52 #include <QPixmap> |
|
53 #include <QFile> |
|
54 |
|
55 QMLContactModel::QMLContactModel(QObject *parent) : |
|
56 QAbstractListModel(parent), |
|
57 m_manager(0) |
|
58 { |
|
59 QHash<int, QByteArray> roleNames; |
|
60 roleNames = QAbstractItemModel::roleNames(); |
|
61 roleNames.insert(InterestLabelRole, "interestLabel"); |
|
62 roleNames.insert(InterestRole, "interest"); |
|
63 roleNames.insert(AvatarRole, "avatar"); |
|
64 roleNames.insert(PresenceAvailableRole, "presenceSupported"); |
|
65 roleNames.insert(PresenceTextRole, "presenceText"); |
|
66 roleNames.insert(PresenceStateRole, "presenceState"); |
|
67 roleNames.insert(PresenceMessageRole, "presenceMessage"); |
|
68 setRoleNames(roleNames); |
|
69 |
|
70 m_fetchHint.setOptimizationHints(QContactFetchHint::NoActionPreferences | QContactFetchHint::NoRelationships); |
|
71 m_fetchHint.setDetailDefinitionsHint(QStringList() |
|
72 << QContactPhoneNumber::DefinitionName |
|
73 << QContactEmailAddress::DefinitionName |
|
74 << QContactThumbnail::DefinitionName |
|
75 << QContactAvatar::DefinitionName |
|
76 << QContactGlobalPresence::DefinitionName |
|
77 << QContactDisplayLabel::DefinitionName |
|
78 << QContactOnlineAccount::DefinitionName); |
|
79 m_sortOrder.setDetailDefinitionName(QContactDisplayLabel::DefinitionName, QContactDisplayLabel::FieldLabel); |
|
80 m_sortOrder.setCaseSensitivity(Qt::CaseSensitive); |
|
81 |
|
82 connect(&m_contactsRequest, SIGNAL(resultsAvailable()), this, SLOT(resultsReceived())); |
|
83 m_contactsRequest.setFetchHint(m_fetchHint); |
|
84 m_contactsRequest.setSorting(m_sortOrder); |
|
85 |
|
86 QContactDetailFilter d; |
|
87 d.setDetailDefinitionName(QContactType::DefinitionName, QContactType::FieldType); |
|
88 d.setValue(QContactType::TypeContact); |
|
89 |
|
90 m_contactsRequest.setFilter(d); |
|
91 |
|
92 setManager(QString()); |
|
93 } |
|
94 |
|
95 QStringList QMLContactModel::availableManagers() const |
|
96 { |
|
97 return QContactManager::availableManagers(); |
|
98 } |
|
99 |
|
100 QString QMLContactModel::manager() |
|
101 { |
|
102 return m_manager->managerName(); |
|
103 } |
|
104 |
|
105 void QMLContactModel::fillContactsIntoMemoryEngine(QContactManager* manager) |
|
106 { |
|
107 QVersitReader reader; |
|
108 QFile file(":/contents/example.vcf"); |
|
109 bool ok = file.open(QIODevice::ReadOnly); |
|
110 if (ok) { |
|
111 reader.setDevice(&file); |
|
112 if (reader.startReading() && reader.waitForFinished()) { |
|
113 QVersitContactImporter importer; |
|
114 importer.importDocuments(reader.results()); |
|
115 QList<QContact> contacts = importer.contacts(); |
|
116 manager->saveContacts(&contacts, 0); |
|
117 } |
|
118 } |
|
119 } |
|
120 |
|
121 int QMLContactModel::rowCount(const QModelIndex &parent) const |
|
122 { |
|
123 Q_UNUSED(parent); |
|
124 return m_contacts.count(); |
|
125 } |
|
126 |
|
127 void QMLContactModel::setManager(const QString& managerName) |
|
128 { |
|
129 delete m_manager; |
|
130 m_manager = new QContactManager(managerName); |
|
131 |
|
132 if (managerName == "memory" && m_manager->contactIds().isEmpty()) { |
|
133 fillContactsIntoMemoryEngine(m_manager); |
|
134 } |
|
135 |
|
136 qWarning() << "Changed backend to: " << managerName; |
|
137 m_contactsRequest.setManager(m_manager); |
|
138 connect(m_manager, SIGNAL(dataChanged()), this, SLOT(fetchAgain())); |
|
139 fetchAgain(); |
|
140 } |
|
141 |
|
142 void QMLContactModel::resultsReceived() |
|
143 { |
|
144 int oldCount = m_contacts.count(); |
|
145 int newCount = m_contactsRequest.contacts().count(); |
|
146 if (newCount > oldCount) { |
|
147 // Assuming the order is the same |
|
148 beginInsertRows(QModelIndex(), newCount - oldCount, newCount); |
|
149 m_contacts = m_contactsRequest.contacts(); |
|
150 endInsertRows(); |
|
151 } else { |
|
152 // Hmm, shouldn't happen |
|
153 reset(); |
|
154 beginInsertRows(QModelIndex(), 0, m_contactsRequest.contacts().count()); |
|
155 m_contacts = m_contactsRequest.contacts(); |
|
156 endInsertRows(); |
|
157 } |
|
158 } |
|
159 |
|
160 void QMLContactModel::fetchAgain() |
|
161 { |
|
162 m_contacts.clear(); |
|
163 reset(); |
|
164 m_contactsRequest.start(); |
|
165 } |
|
166 |
|
167 QPair<QString, QString> QMLContactModel::interestingDetail(const QContact&c) const |
|
168 { |
|
169 // Try a phone number, then email, then online account |
|
170 // This does only check the first detail of each type |
|
171 QContactDetail p = c.details<QContactPhoneNumber>().value(0); |
|
172 if (!p.isEmpty()) |
|
173 return qMakePair(tr("Phone"), p.value(QContactPhoneNumber::FieldNumber)); |
|
174 |
|
175 p = c.details<QContactEmailAddress>().value(0); |
|
176 if (!p.isEmpty()) |
|
177 return qMakePair(tr("Email"), p.value(QContactEmailAddress::FieldEmailAddress)); |
|
178 |
|
179 p = c.details<QContactOnlineAccount>().value(0); |
|
180 if (!p.isEmpty()) |
|
181 return qMakePair(p.value(QContactOnlineAccount::FieldServiceProvider), p.value(QContactOnlineAccount::FieldAccountUri)); |
|
182 |
|
183 // Well, don't know. |
|
184 return qMakePair(QString(), QString()); |
|
185 } |
|
186 |
|
187 QVariant QMLContactModel::data(const QModelIndex &index, int role) const |
|
188 { |
|
189 QContact c = m_contacts.value(index.row()); |
|
190 switch(role) { |
|
191 case Qt::DisplayRole: |
|
192 return c.displayLabel(); |
|
193 case InterestLabelRole: |
|
194 return interestingDetail(c).first; |
|
195 case InterestRole: |
|
196 return interestingDetail(c).second; |
|
197 case AvatarRole: |
|
198 if (c.detail<QContactThumbnail>().isEmpty()) { |
|
199 QContactAvatar a = c.detail<QContactAvatar>(); |
|
200 if (!a.imageUrl().isEmpty()) |
|
201 return a.imageUrl(); |
|
202 else |
|
203 return QString("qrc:/default.svg"); |
|
204 } else { |
|
205 // We have a thumbnail, so return empty |
|
206 return QString(""); |
|
207 } |
|
208 case Qt::DecorationRole: |
|
209 { |
|
210 QContactThumbnail t = c.detail<QContactThumbnail>(); |
|
211 if (!t.thumbnail().isNull()) |
|
212 return QPixmap::fromImage(t.thumbnail()); |
|
213 } |
|
214 return QPixmap(); |
|
215 case PresenceAvailableRole: |
|
216 return !c.detail<QContactGlobalPresence>().isEmpty(); |
|
217 case PresenceMessageRole: |
|
218 return c.detail<QContactGlobalPresence>().customMessage(); |
|
219 case PresenceTextRole: |
|
220 return c.detail<QContactGlobalPresence>().presenceStateText(); |
|
221 case PresenceStateRole: |
|
222 return c.detail<QContactGlobalPresence>().presenceState(); |
|
223 } |
|
224 return QVariant(); |
|
225 } |
|
226 |