1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the examples of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include <QtGui>
|
---|
43 | #include "addressbook.h"
|
---|
44 |
|
---|
45 | AddressBook::AddressBook(QWidget *parent)
|
---|
46 | : QWidget(parent)
|
---|
47 | {
|
---|
48 | QLabel *nameLabel = new QLabel(tr("Name:"));
|
---|
49 | nameLine = new QLineEdit;
|
---|
50 | nameLine->setReadOnly(true);
|
---|
51 |
|
---|
52 | QLabel *addressLabel = new QLabel(tr("Address:"));
|
---|
53 | addressText = new QTextEdit;
|
---|
54 | addressText->setReadOnly(true);
|
---|
55 |
|
---|
56 | addButton = new QPushButton(tr("&Add"));
|
---|
57 |
|
---|
58 | editButton = new QPushButton(tr("&Edit"));
|
---|
59 | editButton->setEnabled(false);
|
---|
60 | removeButton = new QPushButton(tr("&Remove"));
|
---|
61 | removeButton->setEnabled(false);
|
---|
62 | findButton = new QPushButton(tr("&Find"));
|
---|
63 | findButton->setEnabled(false);
|
---|
64 | submitButton = new QPushButton(tr("&Submit"));
|
---|
65 | submitButton->hide();
|
---|
66 | cancelButton = new QPushButton(tr("&Cancel"));
|
---|
67 | cancelButton->hide();
|
---|
68 |
|
---|
69 | nextButton = new QPushButton(tr("&Next"));
|
---|
70 | nextButton->setEnabled(false);
|
---|
71 | previousButton = new QPushButton(tr("&Previous"));
|
---|
72 | previousButton->setEnabled(false);
|
---|
73 |
|
---|
74 | loadButton = new QPushButton(tr("&Load..."));
|
---|
75 | loadButton->setToolTip(tr("Load contacts from a file"));
|
---|
76 | saveButton = new QPushButton(tr("Sa&ve..."));
|
---|
77 | saveButton->setToolTip(tr("Save contacts to a file"));
|
---|
78 | saveButton->setEnabled(false);
|
---|
79 |
|
---|
80 | exportButton = new QPushButton(tr("E&xport"));
|
---|
81 | exportButton->setToolTip(tr("Export as vCard"));
|
---|
82 | exportButton->setEnabled(false);
|
---|
83 |
|
---|
84 | dialog = new FindDialog;
|
---|
85 |
|
---|
86 | connect(addButton, SIGNAL(clicked()), this, SLOT(addContact()));
|
---|
87 | connect(submitButton, SIGNAL(clicked()), this, SLOT(submitContact()));
|
---|
88 | connect(editButton, SIGNAL(clicked()), this, SLOT(editContact()));
|
---|
89 | connect(cancelButton, SIGNAL(clicked()), this, SLOT(cancel()));
|
---|
90 | connect(removeButton, SIGNAL(clicked()), this, SLOT(removeContact()));
|
---|
91 | connect(findButton, SIGNAL(clicked()), this, SLOT(findContact()));
|
---|
92 | connect(nextButton, SIGNAL(clicked()), this, SLOT(next()));
|
---|
93 | connect(previousButton, SIGNAL(clicked()), this, SLOT(previous()));
|
---|
94 | connect(loadButton, SIGNAL(clicked()), this, SLOT(loadFromFile()));
|
---|
95 | connect(saveButton, SIGNAL(clicked()), this, SLOT(saveToFile()));
|
---|
96 | connect(exportButton, SIGNAL(clicked()), this, SLOT(exportAsVCard()));
|
---|
97 |
|
---|
98 | QVBoxLayout *buttonLayout1 = new QVBoxLayout;
|
---|
99 | buttonLayout1->addWidget(addButton);
|
---|
100 | buttonLayout1->addWidget(editButton);
|
---|
101 | buttonLayout1->addWidget(removeButton);
|
---|
102 | buttonLayout1->addWidget(findButton);
|
---|
103 | buttonLayout1->addWidget(submitButton);
|
---|
104 | buttonLayout1->addWidget(cancelButton);
|
---|
105 | buttonLayout1->addWidget(loadButton);
|
---|
106 | buttonLayout1->addWidget(saveButton);
|
---|
107 | buttonLayout1->addWidget(exportButton);
|
---|
108 | buttonLayout1->addStretch();
|
---|
109 |
|
---|
110 | QHBoxLayout *buttonLayout2 = new QHBoxLayout;
|
---|
111 | buttonLayout2->addWidget(previousButton);
|
---|
112 | buttonLayout2->addWidget(nextButton);
|
---|
113 |
|
---|
114 | QGridLayout *mainLayout = new QGridLayout;
|
---|
115 | mainLayout->addWidget(nameLabel, 0, 0);
|
---|
116 | mainLayout->addWidget(nameLine, 0, 1);
|
---|
117 | mainLayout->addWidget(addressLabel, 1, 0, Qt::AlignTop);
|
---|
118 | mainLayout->addWidget(addressText, 1, 1);
|
---|
119 | mainLayout->addLayout(buttonLayout1, 1, 2);
|
---|
120 | mainLayout->addLayout(buttonLayout2, 2, 1);
|
---|
121 |
|
---|
122 | setLayout(mainLayout);
|
---|
123 | setWindowTitle(tr("Simple Address Book"));
|
---|
124 | }
|
---|
125 |
|
---|
126 | void AddressBook::addContact()
|
---|
127 | {
|
---|
128 | oldName = nameLine->text();
|
---|
129 | oldAddress = addressText->toPlainText();
|
---|
130 |
|
---|
131 | nameLine->clear();
|
---|
132 | addressText->clear();
|
---|
133 |
|
---|
134 | updateInterface(AddingMode);
|
---|
135 | }
|
---|
136 |
|
---|
137 | void AddressBook::editContact()
|
---|
138 | {
|
---|
139 | oldName = nameLine->text();
|
---|
140 | oldAddress = addressText->toPlainText();
|
---|
141 |
|
---|
142 | updateInterface(EditingMode);
|
---|
143 | }
|
---|
144 |
|
---|
145 | void AddressBook::submitContact()
|
---|
146 | {
|
---|
147 | QString name = nameLine->text();
|
---|
148 | QString address = addressText->toPlainText();
|
---|
149 |
|
---|
150 | if (name == "" || address == "") {
|
---|
151 | QMessageBox::information(this, tr("Empty Field"),
|
---|
152 | tr("Please enter a name and address."));
|
---|
153 | return;
|
---|
154 | }
|
---|
155 |
|
---|
156 | if (currentMode == AddingMode) {
|
---|
157 |
|
---|
158 | if (!contacts.contains(name)) {
|
---|
159 | contacts.insert(name, address);
|
---|
160 | QMessageBox::information(this, tr("Add Successful"),
|
---|
161 | tr("\"%1\" has been added to your address book.").arg(name));
|
---|
162 | } else {
|
---|
163 | QMessageBox::information(this, tr("Add Unsuccessful"),
|
---|
164 | tr("Sorry, \"%1\" is already in your address book.").arg(name));
|
---|
165 | return;
|
---|
166 | }
|
---|
167 | } else if (currentMode == EditingMode) {
|
---|
168 |
|
---|
169 | if (oldName != name) {
|
---|
170 | if (!contacts.contains(name)) {
|
---|
171 | QMessageBox::information(this, tr("Edit Successful"),
|
---|
172 | tr("\"%1\" has been edited in your address book.").arg(oldName));
|
---|
173 | contacts.remove(oldName);
|
---|
174 | contacts.insert(name, address);
|
---|
175 | } else {
|
---|
176 | QMessageBox::information(this, tr("Edit Unsuccessful"),
|
---|
177 | tr("Sorry, \"%1\" is already in your address book.").arg(name));
|
---|
178 | return;
|
---|
179 | }
|
---|
180 | } else if (oldAddress != address) {
|
---|
181 | QMessageBox::information(this, tr("Edit Successful"),
|
---|
182 | tr("\"%1\" has been edited in your address book.").arg(name));
|
---|
183 | contacts[name] = address;
|
---|
184 | }
|
---|
185 | }
|
---|
186 |
|
---|
187 | updateInterface(NavigationMode);
|
---|
188 | }
|
---|
189 |
|
---|
190 | void AddressBook::cancel()
|
---|
191 | {
|
---|
192 | nameLine->setText(oldName);
|
---|
193 | addressText->setText(oldAddress);
|
---|
194 | updateInterface(NavigationMode);
|
---|
195 | }
|
---|
196 |
|
---|
197 | void AddressBook::removeContact()
|
---|
198 | {
|
---|
199 | QString name = nameLine->text();
|
---|
200 | QString address = addressText->toPlainText();
|
---|
201 |
|
---|
202 | if (contacts.contains(name)) {
|
---|
203 |
|
---|
204 | int button = QMessageBox::question(this,
|
---|
205 | tr("Confirm Remove"),
|
---|
206 | tr("Are you sure you want to remove \"%1\"?").arg(name),
|
---|
207 | QMessageBox::Yes | QMessageBox::No);
|
---|
208 |
|
---|
209 | if (button == QMessageBox::Yes) {
|
---|
210 |
|
---|
211 | previous();
|
---|
212 | contacts.remove(name);
|
---|
213 |
|
---|
214 | QMessageBox::information(this, tr("Remove Successful"),
|
---|
215 | tr("\"%1\" has been removed from your address book.").arg(name));
|
---|
216 | }
|
---|
217 | }
|
---|
218 |
|
---|
219 | updateInterface(NavigationMode);
|
---|
220 | }
|
---|
221 |
|
---|
222 | void AddressBook::next()
|
---|
223 | {
|
---|
224 | QString name = nameLine->text();
|
---|
225 | QMap<QString, QString>::iterator i = contacts.find(name);
|
---|
226 |
|
---|
227 | if (i != contacts.end())
|
---|
228 | i++;
|
---|
229 |
|
---|
230 | if (i == contacts.end())
|
---|
231 | i = contacts.begin();
|
---|
232 |
|
---|
233 | nameLine->setText(i.key());
|
---|
234 | addressText->setText(i.value());
|
---|
235 | }
|
---|
236 |
|
---|
237 | void AddressBook::previous()
|
---|
238 | {
|
---|
239 | QString name = nameLine->text();
|
---|
240 | QMap<QString, QString>::iterator i = contacts.find(name);
|
---|
241 |
|
---|
242 | if (i == contacts.end()) {
|
---|
243 | nameLine->clear();
|
---|
244 | addressText->clear();
|
---|
245 | return;
|
---|
246 | }
|
---|
247 |
|
---|
248 | if (i == contacts.begin())
|
---|
249 | i = contacts.end();
|
---|
250 |
|
---|
251 | i--;
|
---|
252 | nameLine->setText(i.key());
|
---|
253 | addressText->setText(i.value());
|
---|
254 | }
|
---|
255 |
|
---|
256 | void AddressBook::findContact()
|
---|
257 | {
|
---|
258 | dialog->show();
|
---|
259 |
|
---|
260 | if (dialog->exec() == 1) {
|
---|
261 | QString contactName = dialog->getFindText();
|
---|
262 |
|
---|
263 | if (contacts.contains(contactName)) {
|
---|
264 | nameLine->setText(contactName);
|
---|
265 | addressText->setText(contacts.value(contactName));
|
---|
266 | } else {
|
---|
267 | QMessageBox::information(this, tr("Contact Not Found"),
|
---|
268 | tr("Sorry, \"%1\" is not in your address book.").arg(contactName));
|
---|
269 | return;
|
---|
270 | }
|
---|
271 | }
|
---|
272 |
|
---|
273 | updateInterface(NavigationMode);
|
---|
274 | }
|
---|
275 | void AddressBook::updateInterface(Mode mode)
|
---|
276 | {
|
---|
277 | currentMode = mode;
|
---|
278 |
|
---|
279 | switch (currentMode) {
|
---|
280 |
|
---|
281 | case AddingMode:
|
---|
282 | case EditingMode:
|
---|
283 |
|
---|
284 | nameLine->setReadOnly(false);
|
---|
285 | nameLine->setFocus(Qt::OtherFocusReason);
|
---|
286 | addressText->setReadOnly(false);
|
---|
287 |
|
---|
288 | addButton->setEnabled(false);
|
---|
289 | editButton->setEnabled(false);
|
---|
290 | removeButton->setEnabled(false);
|
---|
291 |
|
---|
292 | nextButton->setEnabled(false);
|
---|
293 | previousButton->setEnabled(false);
|
---|
294 |
|
---|
295 | submitButton->show();
|
---|
296 | cancelButton->show();
|
---|
297 |
|
---|
298 | loadButton->setEnabled(false);
|
---|
299 | saveButton->setEnabled(false);
|
---|
300 | exportButton->setEnabled(false);
|
---|
301 | break;
|
---|
302 |
|
---|
303 | case NavigationMode:
|
---|
304 |
|
---|
305 | if (contacts.isEmpty()) {
|
---|
306 | nameLine->clear();
|
---|
307 | addressText->clear();
|
---|
308 | }
|
---|
309 |
|
---|
310 | nameLine->setReadOnly(true);
|
---|
311 | addressText->setReadOnly(true);
|
---|
312 | addButton->setEnabled(true);
|
---|
313 |
|
---|
314 | int number = contacts.size();
|
---|
315 | editButton->setEnabled(number >= 1);
|
---|
316 | removeButton->setEnabled(number >= 1);
|
---|
317 | findButton->setEnabled(number > 2);
|
---|
318 | nextButton->setEnabled(number > 1);
|
---|
319 | previousButton->setEnabled(number > 1);
|
---|
320 |
|
---|
321 | submitButton->hide();
|
---|
322 | cancelButton->hide();
|
---|
323 |
|
---|
324 | exportButton->setEnabled(number >= 1);
|
---|
325 |
|
---|
326 | loadButton->setEnabled(true);
|
---|
327 | saveButton->setEnabled(number >= 1);
|
---|
328 | break;
|
---|
329 | }
|
---|
330 | }
|
---|
331 |
|
---|
332 | void AddressBook::saveToFile()
|
---|
333 | {
|
---|
334 | QString fileName = QFileDialog::getSaveFileName(this,
|
---|
335 | tr("Save Address Book"), "",
|
---|
336 | tr("Address Book (*.abk);;All Files (*)"));
|
---|
337 |
|
---|
338 | if (fileName.isEmpty())
|
---|
339 | return;
|
---|
340 | else {
|
---|
341 | QFile file(fileName);
|
---|
342 |
|
---|
343 | if (!file.open(QIODevice::WriteOnly)) {
|
---|
344 | QMessageBox::information(this, tr("Unable to open file"),
|
---|
345 | file.errorString());
|
---|
346 | return;
|
---|
347 | }
|
---|
348 |
|
---|
349 | QDataStream out(&file);
|
---|
350 | out.setVersion(QDataStream::Qt_4_3);
|
---|
351 | out << contacts;
|
---|
352 | }
|
---|
353 |
|
---|
354 | updateInterface(NavigationMode);
|
---|
355 | }
|
---|
356 |
|
---|
357 | void AddressBook::loadFromFile()
|
---|
358 | {
|
---|
359 | QString fileName = QFileDialog::getOpenFileName(this,
|
---|
360 | tr("Open Address Book"), "",
|
---|
361 | tr("Address Book (*.abk);;All Files (*)"));
|
---|
362 |
|
---|
363 | if (fileName.isEmpty())
|
---|
364 | return;
|
---|
365 | else {
|
---|
366 | QFile file(fileName);
|
---|
367 |
|
---|
368 | if (!file.open(QIODevice::ReadOnly)) {
|
---|
369 | QMessageBox::information(this, tr("Unable to open file"),
|
---|
370 | file.errorString());
|
---|
371 | return;
|
---|
372 | }
|
---|
373 |
|
---|
374 | QDataStream in(&file);
|
---|
375 | in.setVersion(QDataStream::Qt_4_3);
|
---|
376 | contacts.empty(); // empty existing contacts
|
---|
377 | in >> contacts;
|
---|
378 |
|
---|
379 | QMap<QString, QString>::iterator i = contacts.begin();
|
---|
380 | nameLine->setText(i.key());
|
---|
381 | addressText->setText(i.value());
|
---|
382 | }
|
---|
383 |
|
---|
384 | updateInterface(NavigationMode);
|
---|
385 | }
|
---|
386 |
|
---|
387 | //! [export function part1]
|
---|
388 | void AddressBook::exportAsVCard()
|
---|
389 | {
|
---|
390 | QString name = nameLine->text();
|
---|
391 | QString address = addressText->toPlainText();
|
---|
392 | QString firstName;
|
---|
393 | QString lastName;
|
---|
394 | QStringList nameList;
|
---|
395 |
|
---|
396 | int index = name.indexOf(" ");
|
---|
397 |
|
---|
398 | if (index != -1) {
|
---|
399 | nameList = name.split(QRegExp("\\s+"), QString::SkipEmptyParts);
|
---|
400 | firstName = nameList.first();
|
---|
401 | lastName = nameList.last();
|
---|
402 | } else {
|
---|
403 | firstName = name;
|
---|
404 | lastName = "";
|
---|
405 | }
|
---|
406 |
|
---|
407 | QString fileName = QFileDialog::getSaveFileName(this,
|
---|
408 | tr("Export Contact"), "",
|
---|
409 | tr("vCard Files (*.vcf);;All Files (*)"));
|
---|
410 |
|
---|
411 | if (fileName.isEmpty())
|
---|
412 | return;
|
---|
413 |
|
---|
414 | QFile file(fileName);
|
---|
415 | //! [export function part1]
|
---|
416 |
|
---|
417 | //! [export function part2]
|
---|
418 | if (!file.open(QIODevice::WriteOnly)) {
|
---|
419 | QMessageBox::information(this, tr("Unable to open file"),
|
---|
420 | file.errorString());
|
---|
421 | return;
|
---|
422 | }
|
---|
423 |
|
---|
424 | QTextStream out(&file);
|
---|
425 | //! [export function part2]
|
---|
426 |
|
---|
427 | //! [export function part3]
|
---|
428 | out << "BEGIN:VCARD" << "\n";
|
---|
429 | out << "VERSION:2.1" << "\n";
|
---|
430 | out << "N:" << lastName << ";" << firstName << "\n";
|
---|
431 |
|
---|
432 | if (!nameList.isEmpty())
|
---|
433 | out << "FN:" << nameList.join(" ") << "\n";
|
---|
434 | else
|
---|
435 | out << "FN:" << firstName << "\n";
|
---|
436 | //! [export function part3]
|
---|
437 |
|
---|
438 | //! [export function part4]
|
---|
439 | address.replace(";", "\\;", Qt::CaseInsensitive);
|
---|
440 | address.replace("\n", ";", Qt::CaseInsensitive);
|
---|
441 | address.replace(",", " ", Qt::CaseInsensitive);
|
---|
442 |
|
---|
443 | out << "ADR;HOME:;" << address << "\n";
|
---|
444 | out << "END:VCARD" << "\n";
|
---|
445 |
|
---|
446 | QMessageBox::information(this, tr("Export Successful"),
|
---|
447 | tr("\"%1\" has been exported as a vCard.").arg(name));
|
---|
448 | }
|
---|
449 | //! [export function part4]
|
---|