|
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 #include "qmlcontact.h" |
|
43 #include "qmlcontactdetail.h" |
|
44 #include "qmlcontactdetailfield.h" |
|
45 #include <QDebug> |
|
46 |
|
47 static QString normalizePropertyName(const QString& name) |
|
48 { |
|
49 if (!name.isEmpty()) |
|
50 return name.mid(1).prepend(name[0].toLower()); |
|
51 return QString(); |
|
52 } |
|
53 |
|
54 |
|
55 QMLContact::QMLContact(QObject *parent) |
|
56 :QObject(parent), |
|
57 m_contactMap(0) |
|
58 { |
|
59 connect(&m_saveRequest, SIGNAL(resultAvailable()), this, SLOT(onContactSaved())); |
|
60 } |
|
61 |
|
62 void QMLContact::setManager(QContactManager* manager) |
|
63 { |
|
64 m_manager = manager; |
|
65 } |
|
66 void QMLContact::setContact(const QContact& c) |
|
67 { |
|
68 m_contact = c; |
|
69 |
|
70 if (m_contactMap) { |
|
71 delete m_contactMap; |
|
72 m_detailMaps.clear(); |
|
73 } |
|
74 |
|
75 foreach (QObject* detail, m_details) { |
|
76 delete detail; |
|
77 } |
|
78 m_details.clear(); |
|
79 |
|
80 m_contactMap = new QDeclarativePropertyMap(this); |
|
81 |
|
82 |
|
83 QList<QContactDetail> details = m_contact.details(); |
|
84 foreach (const QContactDetail& detail, details) { |
|
85 QMLContactDetail* qd = new QMLContactDetail(this); |
|
86 |
|
87 QDeclarativePropertyMap* dm = new QDeclarativePropertyMap(m_contactMap); |
|
88 |
|
89 connect(dm, SIGNAL(valueChanged(QString,QVariant)), qd, SLOT(detailChanged(QString,QVariant))); |
|
90 |
|
91 |
|
92 QVariantMap values = detail.variantValues(); |
|
93 foreach (const QString& key, values.keys()) { |
|
94 dm->insert(normalizePropertyName(key), values.value(key)); |
|
95 } |
|
96 qd->setName(normalizePropertyName(detail.definitionName())); |
|
97 m_details.append(qd); |
|
98 qd->setDetailPropertyMap(dm); |
|
99 m_detailMaps.append(dm);; |
|
100 m_contactMap->insert(normalizePropertyName(detail.definitionName()), QVariant::fromValue(static_cast<QObject*>(dm))); |
|
101 } |
|
102 } |
|
103 |
|
104 QContact QMLContact::contact() const |
|
105 { |
|
106 QContact c(m_contact); |
|
107 foreach (QObject* o, m_details) { |
|
108 QMLContactDetail* d = qobject_cast<QMLContactDetail*>(o); |
|
109 if (d && d->detailChanged()) { |
|
110 QContactDetail detail = d->detail(); |
|
111 c.saveDetail(&detail); |
|
112 } |
|
113 } |
|
114 |
|
115 return c; |
|
116 } |
|
117 |
|
118 QList<QObject*> QMLContact::detailFields() const |
|
119 { |
|
120 QList<QObject*> fields; |
|
121 foreach (QObject* o, m_details) { |
|
122 QMLContactDetail* detail = qobject_cast<QMLContactDetail*>(o); |
|
123 fields << detail->fields(); |
|
124 } |
|
125 return fields; |
|
126 } |
|
127 QList<QObject*> QMLContact::details() const |
|
128 { |
|
129 return m_details; |
|
130 } |
|
131 |
|
132 QVariant QMLContact::contactMap() const |
|
133 { |
|
134 if (m_contactMap) |
|
135 return QVariant::fromValue(static_cast<QObject*>(m_contactMap)); |
|
136 return QVariant(); |
|
137 } |
|
138 |
|
139 |
|
140 void QMLContact::save() |
|
141 { |
|
142 if (contactChanged()) { |
|
143 m_saveRequest.setManager(m_manager); |
|
144 m_saveRequest.setContact(contact()); |
|
145 m_saveRequest.start(); |
|
146 } |
|
147 } |
|
148 |
|
149 |
|
150 bool QMLContact::contactChanged() const |
|
151 { |
|
152 foreach (QObject* o, m_details) { |
|
153 QMLContactDetail* detail = qobject_cast<QMLContactDetail*>(o); |
|
154 if (detail->detailChanged()) |
|
155 return true; |
|
156 } |
|
157 return false; |
|
158 } |
|
159 |
|
160 void QMLContact::onContactSaved() |
|
161 { |
|
162 if (m_saveRequest.isFinished() && m_saveRequest.error() == QContactManager::NoError) { |
|
163 foreach (QObject* o, m_details) { |
|
164 QMLContactDetail* detail = qobject_cast<QMLContactDetail*>(o); |
|
165 detail->setDetailChanged(false); |
|
166 } |
|
167 } |
|
168 } |
|
169 |