|
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:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include "contacteditor.h" |
|
43 |
|
44 #include <QtGui> |
|
45 |
|
46 ContactEditor::ContactEditor(QWidget *parent) |
|
47 :QWidget(parent) |
|
48 { |
|
49 m_manager = 0; |
|
50 m_contactId = QContactLocalId(0); |
|
51 |
|
52 #ifdef Q_OS_SYMBIAN |
|
53 // In symbian "save" and "cancel" buttons are softkeys. |
|
54 m_saveBtn = new QAction("Save", this); |
|
55 m_saveBtn->setSoftKeyRole(QAction::PositiveSoftKey); |
|
56 addAction(m_saveBtn); |
|
57 connect(m_saveBtn, SIGNAL(triggered(bool)), this, SLOT(saveClicked())); |
|
58 m_cancelBtn = new QAction("Cancel", this); |
|
59 m_cancelBtn->setSoftKeyRole(QAction::NegativeSoftKey); |
|
60 addAction(m_cancelBtn); |
|
61 connect(m_cancelBtn, SIGNAL(triggered(bool)), this, SLOT(cancelClicked())); |
|
62 #else |
|
63 m_saveBtn = new QPushButton("Save", this); |
|
64 connect(m_saveBtn, SIGNAL(clicked()), this, SLOT(saveClicked())); |
|
65 m_cancelBtn = new QPushButton("Cancel", this); |
|
66 connect(m_cancelBtn, SIGNAL(clicked()), this, SLOT(cancelClicked())); |
|
67 #endif |
|
68 |
|
69 m_nameEdit = new QLineEdit(this); |
|
70 m_phoneEdit = new QLineEdit(this); |
|
71 m_emailEdit = new QLineEdit(this); |
|
72 m_addrEdit = new QLineEdit(this); |
|
73 m_avatarBtn = new QPushButton("Add image", this); |
|
74 m_avatarBtn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); |
|
75 connect(m_avatarBtn, SIGNAL(clicked()), this, SLOT(avatarClicked())); |
|
76 |
|
77 QFormLayout *detailsLayout = new QFormLayout; |
|
78 detailsLayout->addRow(new QLabel("Name", this)); |
|
79 detailsLayout->addRow(m_nameEdit); |
|
80 detailsLayout->addRow(new QLabel("Phone", this)); |
|
81 detailsLayout->addRow(m_phoneEdit); |
|
82 detailsLayout->addRow(new QLabel("Email", this)); |
|
83 detailsLayout->addRow(m_emailEdit); |
|
84 detailsLayout->addRow(new QLabel("Address", this)); |
|
85 detailsLayout->addRow(m_addrEdit); |
|
86 detailsLayout->addRow(new QLabel("Avatar", this)); |
|
87 detailsLayout->addRow(m_avatarBtn); |
|
88 detailsLayout->setFieldGrowthPolicy(QFormLayout::ExpandingFieldsGrow); |
|
89 detailsLayout->setSizeConstraint(QLayout::SetMinAndMaxSize); |
|
90 |
|
91 QScrollArea *detailsScrollArea = new QScrollArea(this); |
|
92 detailsScrollArea->setWidgetResizable(true); |
|
93 QWidget *detailsContainer = new QWidget(detailsScrollArea); |
|
94 detailsContainer->setLayout(detailsLayout); |
|
95 detailsScrollArea->setWidget(detailsContainer); |
|
96 |
|
97 #ifndef Q_OS_SYMBIAN |
|
98 QHBoxLayout *btnLayout = new QHBoxLayout; |
|
99 btnLayout->addWidget(m_saveBtn); |
|
100 btnLayout->addWidget(m_cancelBtn); |
|
101 #endif |
|
102 |
|
103 QVBoxLayout *editLayout = new QVBoxLayout; |
|
104 editLayout->addWidget(detailsScrollArea); |
|
105 #ifndef Q_OS_SYMBIAN |
|
106 editLayout->addLayout(btnLayout); |
|
107 #endif |
|
108 |
|
109 setLayout(editLayout); |
|
110 } |
|
111 |
|
112 ContactEditor::~ContactEditor() |
|
113 { |
|
114 } |
|
115 |
|
116 void ContactEditor::setCurrentContact(QContactManager* manager, QContactLocalId currentId) |
|
117 { |
|
118 m_manager = manager; |
|
119 m_contactId = currentId; |
|
120 m_newAvatarPath = QString(); |
|
121 |
|
122 if (manager == 0 || currentId == 0) { |
|
123 // clear the UI and return. |
|
124 m_nameEdit->setText(""); |
|
125 m_phoneEdit->setText(""); |
|
126 m_emailEdit->setText(""); |
|
127 m_addrEdit->setText(""); |
|
128 m_avatarBtn->setText("Add image"); |
|
129 m_avatarBtn->setIcon(QIcon()); |
|
130 |
|
131 if (manager == 0) |
|
132 m_saveBtn->setEnabled(false); |
|
133 |
|
134 return; |
|
135 } |
|
136 |
|
137 // enable the UI. |
|
138 m_saveBtn->setEnabled(true); |
|
139 |
|
140 // otherwise, build from the contact details. |
|
141 QContact curr = manager->contact(m_contactId); |
|
142 QContactName nm = curr.detail(QContactName::DefinitionName); |
|
143 QContactPhoneNumber phn = curr.detail(QContactPhoneNumber::DefinitionName); |
|
144 QContactEmailAddress em = curr.detail(QContactEmailAddress::DefinitionName); |
|
145 QContactAddress adr = curr.detail(QContactAddress::DefinitionName); |
|
146 QContactAvatar av = curr.detail(QContactAvatar::DefinitionName); |
|
147 |
|
148 m_nameEdit->setText(manager->synthesizedDisplayLabel(curr)); |
|
149 m_phoneEdit->setText(phn.value(QContactPhoneNumber::FieldNumber)); |
|
150 m_emailEdit->setText(em.value(QContactEmailAddress::FieldEmailAddress)); |
|
151 m_addrEdit->setText(adr.value(QContactAddress::FieldStreet)); // ugly hack. |
|
152 |
|
153 m_avatarBtn->setText(QString()); |
|
154 m_avatarBtn->setIcon(QIcon()); |
|
155 |
|
156 if (av.pixmap().isNull()) { |
|
157 if (av.avatar().isEmpty()) { |
|
158 m_avatarBtn->setText("Add image"); |
|
159 } else { |
|
160 m_avatarBtn->setIcon(QIcon(QPixmap(av.avatar()))); |
|
161 } |
|
162 } else { |
|
163 m_newAvatarPath = av.avatar(); |
|
164 m_avatarBtn->setIcon(QIcon(av.pixmap())); |
|
165 } |
|
166 } |
|
167 |
|
168 QString ContactEditor::nameField() |
|
169 { |
|
170 // return the field which the name data should be saved in. |
|
171 if (!m_manager) |
|
172 return QString(); |
|
173 |
|
174 QMap<QString, QContactDetailDefinition> defs = m_manager->detailDefinitions(QContactType::TypeContact); |
|
175 QContactDetailDefinition nameDef = defs.value(QContactName::DefinitionName); |
|
176 if (nameDef.fields().keys().contains(QContactName::FieldCustomLabel)) { |
|
177 return QString(QLatin1String(QContactName::FieldCustomLabel)); |
|
178 } else if (nameDef.fields().keys().contains(QContactName::FieldFirstName)) { |
|
179 return QString(QLatin1String(QContactName::FieldFirstName)); |
|
180 } else { |
|
181 return QString(); |
|
182 } |
|
183 } |
|
184 |
|
185 void ContactEditor::avatarClicked() |
|
186 { |
|
187 // put up a file dialog, and update the new avatar path. |
|
188 QString fileName = QFileDialog::getOpenFileName(this, |
|
189 tr("Select Avatar Image"), ".", tr("Image Files (*.png *.jpg *.bmp)")); |
|
190 |
|
191 if (!fileName.isEmpty()) { |
|
192 m_newAvatarPath = fileName; |
|
193 m_avatarBtn->setText(QString()); |
|
194 m_avatarBtn->setIcon(QIcon(m_newAvatarPath)); |
|
195 } |
|
196 } |
|
197 |
|
198 void ContactEditor::saveClicked() |
|
199 { |
|
200 if (!m_manager) { |
|
201 qWarning() << "No manager selected; cannot save."; |
|
202 } else { |
|
203 QContact curr; |
|
204 if (m_contactId != QContactLocalId(0)) |
|
205 curr = m_manager->contact(m_contactId); |
|
206 QContactName nm = curr.detail(QContactName::DefinitionName); |
|
207 QContactPhoneNumber phn = curr.detail(QContactPhoneNumber::DefinitionName); |
|
208 QContactEmailAddress em = curr.detail(QContactEmailAddress::DefinitionName); |
|
209 QContactAddress adr = curr.detail(QContactAddress::DefinitionName); |
|
210 QContactAvatar av = curr.detail(QContactAvatar::DefinitionName); |
|
211 |
|
212 QString saveNameField = nameField(); |
|
213 if (!saveNameField.isEmpty()) { |
|
214 // if the name has changed (ie, is different to the synthed label) then save it as a custom label. |
|
215 if (m_nameEdit->text() != m_manager->synthesizedDisplayLabel(curr)) { |
|
216 nm.setValue(nameField(), m_nameEdit->text()); |
|
217 } |
|
218 } |
|
219 phn.setNumber(m_phoneEdit->text()); |
|
220 em.setEmailAddress(m_emailEdit->text()); |
|
221 adr.setStreet(m_addrEdit->text()); |
|
222 av.setAvatar(m_newAvatarPath); |
|
223 |
|
224 QPixmap pix(m_newAvatarPath); |
|
225 av.setPixmap(pix); |
|
226 |
|
227 curr.saveDetail(&nm); |
|
228 curr.saveDetail(&phn); |
|
229 curr.saveDetail(&em); |
|
230 curr.saveDetail(&adr); |
|
231 curr.saveDetail(&av); |
|
232 |
|
233 bool success = m_manager->saveContact(&curr); |
|
234 if (success) |
|
235 QMessageBox::information(this, "Success!", "Contact saved successfully!"); |
|
236 else |
|
237 QMessageBox::information(this, "Failed!", "Failed to save contact!"); |
|
238 } |
|
239 |
|
240 emit showListPage(); |
|
241 } |
|
242 |
|
243 void ContactEditor::cancelClicked() |
|
244 { |
|
245 emit showListPage(); |
|
246 } |