|
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 "contactlistpage.h" |
|
43 #ifdef BUILD_VERSIT |
|
44 #include "qversitreader.h" |
|
45 #include "qversitcontactimporter.h" |
|
46 #include "qversitwriter.h" |
|
47 #include "qversitcontactexporter.h" |
|
48 #endif |
|
49 |
|
50 #include <QtGui> |
|
51 |
|
52 ContactListPage::ContactListPage(QWidget *parent) |
|
53 : QWidget(parent) |
|
54 { |
|
55 m_manager = 0; |
|
56 m_currentFilter = QContactFilter(); |
|
57 |
|
58 m_backendsCombo = new QComboBox(this); |
|
59 QStringList availableManagers = QContactManager::availableManagers(); |
|
60 m_backendsCombo->addItems(availableManagers); |
|
61 connect(m_backendsCombo, SIGNAL(currentIndexChanged(QString)), this, SLOT(backendSelected())); |
|
62 m_filterActiveLabel = new QLabel("Inactive"); |
|
63 m_filterActiveLabel->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed)); |
|
64 |
|
65 m_contactsList = new QListWidget(this); |
|
66 |
|
67 QPushButton* m_addContactBtn = new QPushButton("Add", this); |
|
68 connect(m_addContactBtn, SIGNAL(clicked()), this, SLOT(addContactClicked())); |
|
69 QPushButton* m_editBtn = new QPushButton("Edit", this); |
|
70 connect(m_editBtn, SIGNAL(clicked()), this, SLOT(editClicked())); |
|
71 QPushButton* m_deleteBtn = new QPushButton("Delete", this); |
|
72 connect(m_deleteBtn, SIGNAL(clicked()), this, SLOT(deleteClicked())); |
|
73 QPushButton* m_filterBtn = new QPushButton("Filter", this); |
|
74 connect(m_filterBtn, SIGNAL(clicked()), this, SLOT(filterClicked())); |
|
75 QPushButton* m_importBtn = new QPushButton("Import"); |
|
76 connect(m_importBtn, SIGNAL(clicked()), this, SLOT(importClicked())); |
|
77 QPushButton* m_exportBtn = new QPushButton("Export"); |
|
78 connect(m_exportBtn, SIGNAL(clicked()), this, SLOT(exportClicked())); |
|
79 |
|
80 |
|
81 QFormLayout *backendLayout = new QFormLayout; |
|
82 backendLayout->addRow("Store:", m_backendsCombo); |
|
83 backendLayout->addRow("Filter:", m_filterActiveLabel); |
|
84 |
|
85 QHBoxLayout *btnLayout1 = new QHBoxLayout; |
|
86 btnLayout1->addWidget(m_addContactBtn); |
|
87 btnLayout1->addWidget(m_editBtn); |
|
88 btnLayout1->addWidget(m_deleteBtn); |
|
89 btnLayout1->addWidget(m_filterBtn); |
|
90 |
|
91 QHBoxLayout *btnLayout2 = new QHBoxLayout; |
|
92 btnLayout2->addWidget(m_importBtn); |
|
93 btnLayout2->addWidget(m_exportBtn); |
|
94 |
|
95 QVBoxLayout *bookLayout = new QVBoxLayout; |
|
96 bookLayout->addLayout(backendLayout); |
|
97 bookLayout->addWidget(m_contactsList); |
|
98 bookLayout->addLayout(btnLayout1); |
|
99 #ifdef BUILD_VERSIT |
|
100 bookLayout->addLayout(btnLayout2); |
|
101 #endif |
|
102 |
|
103 setLayout(bookLayout); |
|
104 |
|
105 // force update to backend. |
|
106 QTimer::singleShot(0, this, SLOT(backendSelected())); |
|
107 } |
|
108 |
|
109 ContactListPage::~ContactListPage() |
|
110 { |
|
111 QList<QContactManager*> initialisedManagers = m_initialisedManagers.values(); |
|
112 while (!initialisedManagers.isEmpty()) { |
|
113 QContactManager *deleteMe = initialisedManagers.takeFirst(); |
|
114 delete deleteMe; |
|
115 } |
|
116 } |
|
117 |
|
118 void ContactListPage::backendSelected() |
|
119 { |
|
120 QString backend = m_backendsCombo->currentText(); |
|
121 |
|
122 // first, check to see if they reselected the same backend. |
|
123 if (m_manager && m_manager->managerName() == backend) |
|
124 return; |
|
125 |
|
126 // the change is real. update. |
|
127 if (m_initialisedManagers.contains(backend)) { |
|
128 m_manager = m_initialisedManagers.value(backend); |
|
129 } else { |
|
130 m_manager = new QContactManager(backend); |
|
131 m_initialisedManagers.insert(backend, m_manager); |
|
132 } |
|
133 |
|
134 // signal that the manager has changed. |
|
135 emit managerChanged(m_manager); |
|
136 |
|
137 // and... rebuild the list. |
|
138 rebuildList(m_currentFilter); |
|
139 } |
|
140 |
|
141 void ContactListPage::rebuildList(const QContactFilter& filter) |
|
142 { |
|
143 // first, check to see whether the filter does anything |
|
144 if (filter == QContactFilter()) |
|
145 m_filterActiveLabel->setText("Inactive"); |
|
146 else |
|
147 m_filterActiveLabel->setText("Active"); |
|
148 |
|
149 QContact currContact; |
|
150 m_currentFilter = filter; |
|
151 m_contactsList->clear(); |
|
152 m_idToListIndex.clear(); |
|
153 QList<QContactLocalId> contactIds = m_manager->contactIds(m_currentFilter); |
|
154 foreach (const QContactLocalId& id, contactIds) { |
|
155 QListWidgetItem *currItem = new QListWidgetItem; |
|
156 currContact = m_manager->contact(id); |
|
157 currItem->setData(Qt::DisplayRole, currContact.displayLabel()); |
|
158 currItem->setData(Qt::UserRole, currContact.localId()); // also store the id of the contact. |
|
159 m_idToListIndex.insert(currContact.localId(), m_contactsList->count()); |
|
160 m_contactsList->addItem(currItem); |
|
161 } |
|
162 } |
|
163 |
|
164 void ContactListPage::addContactClicked() |
|
165 { |
|
166 emit showEditorPage(QContactLocalId(0)); |
|
167 } |
|
168 |
|
169 void ContactListPage::editClicked() |
|
170 { |
|
171 if (m_contactsList->currentItem()) |
|
172 emit showEditorPage(QContactLocalId(m_contactsList->currentItem()->data(Qt::UserRole).toUInt())); |
|
173 // else, nothing selected; ignore. |
|
174 } |
|
175 |
|
176 void ContactListPage::filterClicked() |
|
177 { |
|
178 emit showFilterPage(m_currentFilter); |
|
179 } |
|
180 |
|
181 void ContactListPage::deleteClicked() |
|
182 { |
|
183 if (!m_manager) { |
|
184 qWarning() << "No manager selected; cannot delete."; |
|
185 return; |
|
186 } |
|
187 |
|
188 if (!m_contactsList->currentItem()) { |
|
189 qWarning() << "Nothing to delete."; |
|
190 return; |
|
191 } |
|
192 |
|
193 QContactLocalId contactId = QContactLocalId(m_contactsList->currentItem()->data(Qt::UserRole).toUInt()); |
|
194 bool success = m_manager->removeContact(contactId); |
|
195 if (success) { |
|
196 delete m_contactsList->takeItem(m_contactsList->currentRow()); |
|
197 QMessageBox::information(this, "Success!", "Contact deleted successfully!"); |
|
198 } |
|
199 else |
|
200 QMessageBox::information(this, "Failed!", "Failed to delete contact!"); |
|
201 } |
|
202 |
|
203 void ContactListPage::importClicked() |
|
204 { |
|
205 #ifdef BUILD_VERSIT |
|
206 if (!m_manager) { |
|
207 qWarning() << "No manager selected; cannot import"; |
|
208 return; |
|
209 } |
|
210 QString fileName = QFileDialog::getOpenFileName(this, |
|
211 tr("Select vCard file"), ".", tr("vCard files (*.vcf)")); |
|
212 QFile file(fileName); |
|
213 file.open(QIODevice::ReadOnly); |
|
214 if (file.isReadable()) { |
|
215 QVersitReader reader; |
|
216 reader.setDevice(&file); |
|
217 if (reader.startReading() && reader.waitForFinished()) { |
|
218 QVersitContactImporter importer; |
|
219 QList<QContact> contacts = importer.importContacts(reader.results()); |
|
220 QMap<int, QContactManager::Error> errorMap; |
|
221 m_manager->saveContacts(&contacts, &errorMap); |
|
222 rebuildList(m_currentFilter); |
|
223 } |
|
224 } |
|
225 #endif |
|
226 } |
|
227 |
|
228 void ContactListPage::exportClicked() |
|
229 { |
|
230 #ifdef BUILD_VERSIT |
|
231 if (!m_manager) { |
|
232 qWarning() << "No manager selected; cannot import"; |
|
233 return; |
|
234 } |
|
235 QList<QContact> contacts = m_manager->contacts(QList<QContactSortOrder>(), QStringList()); |
|
236 QString fileName = QFileDialog::getSaveFileName(this, tr("Save vCard"), |
|
237 "./contacts.vcf", |
|
238 tr("vCards (*.vcf)")); |
|
239 QFile file(fileName); |
|
240 file.open(QIODevice::WriteOnly); |
|
241 if (file.isWritable()) { |
|
242 QVersitContactExporter exporter; |
|
243 QList<QVersitDocument> documents = exporter.exportContacts(contacts); |
|
244 QVersitWriter writer; |
|
245 writer.setDevice(&file); |
|
246 writer.startWriting(documents); |
|
247 writer.waitForFinished(); |
|
248 } |
|
249 #endif |
|
250 } |