|
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 "qversitwriter.h" |
|
48 #include "qversitcontactimporter.h" |
|
49 #include "qversitcontactexporter.h" |
|
50 #include <QColor> |
|
51 #include <QHash> |
|
52 #include <QDebug> |
|
53 #include <QPixmap> |
|
54 #include <QFile> |
|
55 |
|
56 |
|
57 QMLContactModel::QMLContactModel(QObject *parent) : |
|
58 QAbstractListModel(parent), |
|
59 m_manager(0) |
|
60 { |
|
61 QHash<int, QByteArray> roleNames; |
|
62 roleNames = QAbstractItemModel::roleNames(); |
|
63 roleNames.insert(InterestLabelRole, "interestLabel"); |
|
64 roleNames.insert(InterestRole, "interest"); |
|
65 roleNames.insert(ContactRole, "contact"); |
|
66 roleNames.insert(ContactIdRole, "contactId"); |
|
67 roleNames.insert(AvatarRole, "avatar"); |
|
68 roleNames.insert(PresenceAvailableRole, "presenceSupported"); |
|
69 roleNames.insert(PresenceTextRole, "presenceText"); |
|
70 roleNames.insert(PresenceStateRole, "presenceState"); |
|
71 roleNames.insert(PresenceMessageRole, "presenceMessage"); |
|
72 setRoleNames(roleNames); |
|
73 |
|
74 m_fetchHint.setOptimizationHints(QContactFetchHint::NoActionPreferences | QContactFetchHint::NoRelationships); |
|
75 m_fetchHint.setDetailDefinitionsHint(QStringList() |
|
76 << QContactPhoneNumber::DefinitionName |
|
77 << QContactEmailAddress::DefinitionName |
|
78 << QContactThumbnail::DefinitionName |
|
79 << QContactAvatar::DefinitionName |
|
80 << QContactGlobalPresence::DefinitionName |
|
81 << QContactDisplayLabel::DefinitionName |
|
82 << QContactOnlineAccount::DefinitionName); |
|
83 m_sortOrder.setDetailDefinitionName(QContactDisplayLabel::DefinitionName, QContactDisplayLabel::FieldLabel); |
|
84 m_sortOrder.setCaseSensitivity(Qt::CaseSensitive); |
|
85 |
|
86 connect(&m_contactsRequest, SIGNAL(resultsAvailable()), this, SLOT(resultsReceived())); |
|
87 m_contactsRequest.setFetchHint(m_fetchHint); |
|
88 m_contactsRequest.setSorting(m_sortOrder); |
|
89 |
|
90 QContactDetailFilter d; |
|
91 d.setDetailDefinitionName(QContactType::DefinitionName, QContactType::FieldType); |
|
92 d.setValue(QContactType::TypeContact); |
|
93 |
|
94 m_contactsRequest.setFilter(d); |
|
95 |
|
96 setManager(QString()); |
|
97 |
|
98 connect(&m_reader, SIGNAL(stateChanged(QVersitReader::State)), this, SLOT(startImport(QVersitReader::State))); |
|
99 } |
|
100 |
|
101 QStringList QMLContactModel::availableManagers() const |
|
102 { |
|
103 return QContactManager::availableManagers(); |
|
104 } |
|
105 |
|
106 QString QMLContactModel::manager() const |
|
107 { |
|
108 return m_manager->managerName(); |
|
109 } |
|
110 QList<QObject*> QMLContactModel::details(int id) const |
|
111 { |
|
112 if (m_contactMap.contains(id)) |
|
113 return m_contactMap.value(id)->details(); |
|
114 return QList<QObject*>(); |
|
115 } |
|
116 void QMLContactModel::exposeContactsToQML() |
|
117 { |
|
118 foreach (const QContact& c, m_contacts) { |
|
119 if (!m_contactMap.contains(c.localId())) { |
|
120 QMLContact* qc = new QMLContact(this); |
|
121 qc->setContact(c); |
|
122 m_contactMap.insert(c.localId(), qc); |
|
123 } else { |
|
124 m_contactMap.value(c.localId())->setContact(c); |
|
125 } |
|
126 } |
|
127 } |
|
128 |
|
129 |
|
130 void QMLContactModel::importContacts(const QString& vcard) |
|
131 { |
|
132 qWarning() << "importing contacts from:" << vcard; |
|
133 QFile* file = new QFile(vcard); |
|
134 bool ok = file->open(QIODevice::ReadOnly); |
|
135 if (ok) { |
|
136 m_reader.setDevice(file); |
|
137 m_reader.startReading(); |
|
138 } |
|
139 } |
|
140 |
|
141 void QMLContactModel::exportContacts(const QString& vcard) |
|
142 { |
|
143 QVersitContactExporter exporter; |
|
144 exporter.exportContacts(m_contacts, QVersitDocument::VCard30Type); |
|
145 QList<QVersitDocument> documents = exporter.documents(); |
|
146 QFile* file = new QFile(vcard); |
|
147 bool ok = file->open(QIODevice::ReadWrite); |
|
148 if (ok) { |
|
149 m_writer.setDevice(file); |
|
150 m_writer.startWriting(documents); |
|
151 } |
|
152 } |
|
153 |
|
154 void QMLContactModel::contactsExported(QVersitWriter::State state) |
|
155 { |
|
156 if (state == QVersitWriter::FinishedState || state == QVersitWriter::CanceledState) { |
|
157 delete m_writer.device(); |
|
158 m_writer.setDevice(0); |
|
159 } |
|
160 } |
|
161 |
|
162 int QMLContactModel::rowCount(const QModelIndex &parent) const |
|
163 { |
|
164 Q_UNUSED(parent); |
|
165 return m_contacts.count(); |
|
166 } |
|
167 |
|
168 void QMLContactModel::setManager(const QString& managerName) |
|
169 { |
|
170 if (m_manager) |
|
171 delete m_manager; |
|
172 |
|
173 foreach (const QContactLocalId& id, m_contactMap.keys()) { |
|
174 delete m_contactMap.value(id); |
|
175 } |
|
176 m_contactMap.clear(); |
|
177 |
|
178 m_manager = new QContactManager(managerName); |
|
179 |
|
180 qWarning() << "Changed backend to: " << managerName; |
|
181 m_contactsRequest.setManager(m_manager); |
|
182 connect(m_manager, SIGNAL(dataChanged()), this, SLOT(fetchAgain())); |
|
183 fetchAgain(); |
|
184 emit managerChanged(); |
|
185 } |
|
186 |
|
187 void QMLContactModel::startImport(QVersitReader::State state) |
|
188 { |
|
189 if (state == QVersitReader::FinishedState || state == QVersitReader::CanceledState) { |
|
190 QVersitContactImporter importer; |
|
191 importer.importDocuments(m_reader.results()); |
|
192 QList<QContact> contacts = importer.contacts(); |
|
193 |
|
194 delete m_reader.device(); |
|
195 m_reader.setDevice(0); |
|
196 |
|
197 if (m_manager) { |
|
198 if (m_manager->saveContacts(&contacts, 0)) |
|
199 qWarning() << "contacts imported."; |
|
200 fetchAgain(); |
|
201 } |
|
202 } |
|
203 } |
|
204 |
|
205 void QMLContactModel::resultsReceived() |
|
206 { |
|
207 int oldCount = m_contacts.count(); |
|
208 |
|
209 int newCount = m_contactsRequest.contacts().count(); |
|
210 if (newCount > oldCount) { |
|
211 // Assuming the order is the same |
|
212 beginInsertRows(QModelIndex(), newCount - oldCount, newCount); |
|
213 m_contacts = m_contactsRequest.contacts(); |
|
214 endInsertRows(); |
|
215 } else { |
|
216 // Hmm, shouldn't happen |
|
217 reset(); |
|
218 beginInsertRows(QModelIndex(), 0, m_contactsRequest.contacts().count()); |
|
219 m_contacts = m_contactsRequest.contacts(); |
|
220 endInsertRows(); |
|
221 } |
|
222 |
|
223 exposeContactsToQML(); |
|
224 } |
|
225 |
|
226 void QMLContactModel::fetchAgain() |
|
227 { |
|
228 m_contacts.clear(); |
|
229 reset(); |
|
230 m_contactsRequest.start(); |
|
231 } |
|
232 |
|
233 QPair<QString, QString> QMLContactModel::interestingDetail(const QContact&c) const |
|
234 { |
|
235 // Try a phone number, then email, then online account |
|
236 // This does only check the first detail of each type |
|
237 QContactDetail p = c.details<QContactPhoneNumber>().value(0); |
|
238 if (!p.isEmpty()) |
|
239 return qMakePair(tr("Phone"), p.value(QContactPhoneNumber::FieldNumber)); |
|
240 |
|
241 p = c.details<QContactEmailAddress>().value(0); |
|
242 if (!p.isEmpty()) |
|
243 return qMakePair(tr("Email"), p.value(QContactEmailAddress::FieldEmailAddress)); |
|
244 |
|
245 p = c.details<QContactOnlineAccount>().value(0); |
|
246 if (!p.isEmpty()) |
|
247 return qMakePair(p.value(QContactOnlineAccount::FieldServiceProvider), p.value(QContactOnlineAccount::FieldAccountUri)); |
|
248 |
|
249 // Well, don't know. |
|
250 return qMakePair(QString(), QString()); |
|
251 } |
|
252 |
|
253 |
|
254 |
|
255 QVariant QMLContactModel::data(const QModelIndex &index, int role) const |
|
256 { |
|
257 QContact c = m_contacts.value(index.row()); |
|
258 switch(role) { |
|
259 case Qt::DisplayRole: |
|
260 return c.displayLabel(); |
|
261 case InterestLabelRole: |
|
262 return interestingDetail(c).first; |
|
263 case InterestRole: |
|
264 return interestingDetail(c).second; |
|
265 case ContactRole: |
|
266 if (m_contactMap.contains(c.localId())) { |
|
267 return m_contactMap.value(c.localId())->contactMap(); |
|
268 } |
|
269 case ContactIdRole: |
|
270 return c.localId(); |
|
271 case AvatarRole: |
|
272 //Just let the imager provider deal with it |
|
273 //return QString("image://thumbnail/%1.%2").arg(manager()).arg(c.localId()); //imageprovider.h |
|
274 return c.detail<QContactAvatar>().imageUrl(); |
|
275 case Qt::DecorationRole: |
|
276 { |
|
277 QContactThumbnail t = c.detail<QContactThumbnail>(); |
|
278 if (!t.thumbnail().isNull()) |
|
279 return QPixmap::fromImage(t.thumbnail()); |
|
280 } |
|
281 return QPixmap(); |
|
282 case PresenceAvailableRole: |
|
283 return !c.detail<QContactGlobalPresence>().isEmpty(); |
|
284 case PresenceMessageRole: |
|
285 return c.detail<QContactGlobalPresence>().customMessage(); |
|
286 case PresenceTextRole: |
|
287 return c.detail<QContactGlobalPresence>().presenceStateText(); |
|
288 case PresenceStateRole: |
|
289 return c.detail<QContactGlobalPresence>().presenceState(); |
|
290 } |
|
291 return QVariant(); |
|
292 } |
|
293 |