1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 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 #include "qmlcontactsa.h" |
|
41 |
|
42 #include <QtDebug> |
|
43 #include <QFile> |
|
44 #include <QIODevice> |
|
45 #include <qcontactfetchrequest.h> |
|
46 #include <qcontactlocalidfilter.h> |
|
47 #include <qcontactdetails.h> |
|
48 #include <qversitreader.h> |
|
49 #include <qversitcontactimporter.h> |
|
50 |
|
51 QT_USE_NAMESPACE |
|
52 QTM_USE_NAMESPACE |
|
53 |
|
54 // ![0] |
|
55 QMLContactManagerAsync::QMLContactManagerAsync(QObject *parent) |
|
56 : QObject(parent) |
|
57 { |
|
58 qc = new QContactManager(); |
|
59 } |
|
60 |
|
61 QMLContactManagerAsync::~QMLContactManagerAsync() |
|
62 { |
|
63 delete qc; |
|
64 } |
|
65 |
|
66 QString QMLContactManagerAsync::availableManagers() const |
|
67 { |
|
68 return QContactManager::availableManagers().join(" "); |
|
69 } |
|
70 |
|
71 QString QMLContactManagerAsync::manager() |
|
72 { |
|
73 return qc->managerName(); |
|
74 } |
|
75 |
|
76 void QMLContactManagerAsync::fillContactsIntoMemoryEngine(QContactManager* manager) |
|
77 { |
|
78 QVersitReader reader; |
|
79 QFile file(":/contents/example.vcf"); |
|
80 bool ok = file.open(QIODevice::ReadOnly); |
|
81 if (ok) { |
|
82 reader.setDevice(&file); |
|
83 if (reader.startReading() && reader.waitForFinished()) { |
|
84 QVersitContactImporter importer; |
|
85 importer.importDocuments(reader.results()); |
|
86 QList<QContact> contacts = importer.contacts(); |
|
87 manager->saveContacts(&contacts, 0); |
|
88 } |
|
89 } |
|
90 } |
|
91 |
|
92 void QMLContactManagerAsync::setManager(QString manager) |
|
93 { |
|
94 delete qc; |
|
95 qc = new QContactManager(manager); |
|
96 connect(qc, SIGNAL(contactsAdded(QList<QContactLocalId>)), this, SIGNAL(contactsAdded(QList<QContactLocalId>))); |
|
97 connect(qc, SIGNAL(contactsChanged(QList<QContactLocalId>)), this, SIGNAL(contactsChanged(QList<QContactLocalId>))); |
|
98 connect(qc, SIGNAL(contactsRemoved(QList<QContactLocalId>)), this, SIGNAL(contactsRemoved(QList<QContactLocalId>))); |
|
99 connect(qc, SIGNAL(relationshipsAdded(QList<QContactLocalId>)), this, SIGNAL(relationshipsAdded(QList<QContactLocalId>))); |
|
100 connect(qc, SIGNAL(relationshipsRemoved(QList<QContactLocalId>)), this, SIGNAL(relationshipsRemoved(QList<QContactLocalId>))); |
|
101 |
|
102 if (manager == "memory" && qc->contactIds().isEmpty()) { |
|
103 fillContactsIntoMemoryEngine(qc); |
|
104 } |
|
105 |
|
106 qWarning() << "Changed backend to: " << manager; |
|
107 } |
|
108 |
|
109 |
|
110 QString QMLContactManagerAsync::contactListToQString(const QList<QContactLocalId>& contactIds) const |
|
111 { |
|
112 QString list; |
|
113 |
|
114 for (int i = 0; i < contactIds.count(); i++) { |
|
115 list += QString::number(contactIds.at(i)) + ' '; |
|
116 } |
|
117 |
|
118 return list; |
|
119 } |
|
120 |
|
121 QStringList QMLContactManagerAsync::contactListToQString(const QList<QContact>& contact) const |
|
122 { |
|
123 QStringList list; |
|
124 |
|
125 for (int i = 0; i < contact.count(); i++) { |
|
126 list += qc->synthesizedContactDisplayLabel(contact.at(i)); |
|
127 } |
|
128 |
|
129 return list; |
|
130 } |
|
131 |
|
132 int QMLContactManagerAsync::numContacts() |
|
133 { |
|
134 return qc->contactIds().count(); |
|
135 } |
|
136 |
|
137 void QMLContactManagerAsync::contacts() |
|
138 { |
|
139 m_contactIds.clear(); |
|
140 QContactFetchRequest* req = new QContactFetchRequest; |
|
141 QContactLocalIdFilter idFil; |
|
142 idFil.setIds(qc->contactIds()); |
|
143 req->setFilter(idFil); |
|
144 req->setManager(qc); |
|
145 connect(req, SIGNAL(stateChanged(QContactAbstractRequest::State)), this, SLOT(contactProgress(QContactAbstractRequest::State))); |
|
146 req->start(); |
|
147 } |
|
148 |
|
149 void QMLContactManagerAsync::contactProgress(QContactAbstractRequest::State newState) |
|
150 { |
|
151 QContactFetchRequest *request = qobject_cast<QContactFetchRequest*>(sender()); |
|
152 |
|
153 // first, check to make sure that the request is still valid. |
|
154 if (qc != request->manager() || |
|
155 newState == QContactAbstractRequest::CanceledState) { |
|
156 request->deleteLater(); |
|
157 return; // ignore these results. |
|
158 } |
|
159 |
|
160 if(request->contacts().count() > 0) { |
|
161 foreach(const QContact& c, request->contacts()) { |
|
162 QmlContact qmlc(c); |
|
163 emit contactsLoaded(&qmlc); |
|
164 } |
|
165 } |
|
166 |
|
167 // check to see if the request status is "finished" - clean up. |
|
168 if (newState == QContactAbstractRequest::FinishedState) { |
|
169 request->deleteLater(); |
|
170 emit contactsLoadedDone(); |
|
171 } |
|
172 } |
|
173 |
|
174 QString QMLContactManagerAsync::idToName(QString name) |
|
175 { |
|
176 QContact c = qc->contact(name.toInt()); |
|
177 return qc->synthesizedContactDisplayLabel(c); |
|
178 } |
|
179 |
|
180 // ![0] |
|
181 |
|
182 #include "moc_qmlcontactsa.cpp" |
|