0
|
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 examples of the Qt Toolkit.
|
|
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 <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 |
}
|
|
154 |
|
|
155 |
if (currentMode == AddingMode) {
|
|
156 |
|
|
157 |
if (!contacts.contains(name)) {
|
|
158 |
contacts.insert(name, address);
|
|
159 |
QMessageBox::information(this, tr("Add Successful"),
|
|
160 |
tr("\"%1\" has been added to your address book.").arg(name));
|
|
161 |
} else {
|
|
162 |
QMessageBox::information(this, tr("Add Unsuccessful"),
|
|
163 |
tr("Sorry, \"%1\" is already in your address book.").arg(name));
|
|
164 |
}
|
|
165 |
} else if (currentMode == EditingMode) {
|
|
166 |
|
|
167 |
if (oldName != name) {
|
|
168 |
if (!contacts.contains(name)) {
|
|
169 |
QMessageBox::information(this, tr("Edit Successful"),
|
|
170 |
tr("\"%1\" has been edited in your address book.").arg(oldName));
|
|
171 |
contacts.remove(oldName);
|
|
172 |
contacts.insert(name, address);
|
|
173 |
} else {
|
|
174 |
QMessageBox::information(this, tr("Edit Unsuccessful"),
|
|
175 |
tr("Sorry, \"%1\" is already in your address book.").arg(name));
|
|
176 |
}
|
|
177 |
} else if (oldAddress != address) {
|
|
178 |
QMessageBox::information(this, tr("Edit Successful"),
|
|
179 |
tr("\"%1\" has been edited in your address book.").arg(name));
|
|
180 |
contacts[name] = address;
|
|
181 |
}
|
|
182 |
}
|
|
183 |
|
|
184 |
updateInterface(NavigationMode);
|
|
185 |
}
|
|
186 |
|
|
187 |
void AddressBook::cancel()
|
|
188 |
{
|
|
189 |
nameLine->setText(oldName);
|
|
190 |
addressText->setText(oldAddress);
|
|
191 |
updateInterface(NavigationMode);
|
|
192 |
}
|
|
193 |
|
|
194 |
void AddressBook::removeContact()
|
|
195 |
{
|
|
196 |
QString name = nameLine->text();
|
|
197 |
QString address = addressText->toPlainText();
|
|
198 |
|
|
199 |
if (contacts.contains(name)) {
|
|
200 |
|
|
201 |
int button = QMessageBox::question(this,
|
|
202 |
tr("Confirm Remove"),
|
|
203 |
tr("Are you sure you want to remove \"%1\"?").arg(name),
|
|
204 |
QMessageBox::Yes | QMessageBox::No);
|
|
205 |
|
|
206 |
if (button == QMessageBox::Yes) {
|
|
207 |
|
|
208 |
previous();
|
|
209 |
contacts.remove(name);
|
|
210 |
|
|
211 |
QMessageBox::information(this, tr("Remove Successful"),
|
|
212 |
tr("\"%1\" has been removed from your address book.").arg(name));
|
|
213 |
}
|
|
214 |
}
|
|
215 |
|
|
216 |
updateInterface(NavigationMode);
|
|
217 |
}
|
|
218 |
|
|
219 |
void AddressBook::next()
|
|
220 |
{
|
|
221 |
QString name = nameLine->text();
|
|
222 |
QMap<QString, QString>::iterator i = contacts.find(name);
|
|
223 |
|
|
224 |
if (i != contacts.end())
|
|
225 |
i++;
|
|
226 |
|
|
227 |
if (i == contacts.end())
|
|
228 |
i = contacts.begin();
|
|
229 |
|
|
230 |
nameLine->setText(i.key());
|
|
231 |
addressText->setText(i.value());
|
|
232 |
}
|
|
233 |
|
|
234 |
void AddressBook::previous()
|
|
235 |
{
|
|
236 |
QString name = nameLine->text();
|
|
237 |
QMap<QString, QString>::iterator i = contacts.find(name);
|
|
238 |
|
|
239 |
if (i == contacts.end()) {
|
|
240 |
nameLine->clear();
|
|
241 |
addressText->clear();
|
|
242 |
return;
|
|
243 |
}
|
|
244 |
|
|
245 |
if (i == contacts.begin())
|
|
246 |
i = contacts.end();
|
|
247 |
|
|
248 |
i--;
|
|
249 |
nameLine->setText(i.key());
|
|
250 |
addressText->setText(i.value());
|
|
251 |
}
|
|
252 |
|
|
253 |
void AddressBook::findContact()
|
|
254 |
{
|
|
255 |
dialog->show();
|
|
256 |
|
|
257 |
if (dialog->exec() == 1) {
|
|
258 |
QString contactName = dialog->getFindText();
|
|
259 |
|
|
260 |
if (contacts.contains(contactName)) {
|
|
261 |
nameLine->setText(contactName);
|
|
262 |
addressText->setText(contacts.value(contactName));
|
|
263 |
} else {
|
|
264 |
QMessageBox::information(this, tr("Contact Not Found"),
|
|
265 |
tr("Sorry, \"%1\" is not in your address book.").arg(contactName));
|
|
266 |
return;
|
|
267 |
}
|
|
268 |
}
|
|
269 |
|
|
270 |
updateInterface(NavigationMode);
|
|
271 |
}
|
|
272 |
void AddressBook::updateInterface(Mode mode)
|
|
273 |
{
|
|
274 |
currentMode = mode;
|
|
275 |
|
|
276 |
switch (currentMode) {
|
|
277 |
|
|
278 |
case AddingMode:
|
|
279 |
case EditingMode:
|
|
280 |
|
|
281 |
nameLine->setReadOnly(false);
|
|
282 |
nameLine->setFocus(Qt::OtherFocusReason);
|
|
283 |
addressText->setReadOnly(false);
|
|
284 |
|
|
285 |
addButton->setEnabled(false);
|
|
286 |
editButton->setEnabled(false);
|
|
287 |
removeButton->setEnabled(false);
|
|
288 |
|
|
289 |
nextButton->setEnabled(false);
|
|
290 |
previousButton->setEnabled(false);
|
|
291 |
|
|
292 |
submitButton->show();
|
|
293 |
cancelButton->show();
|
|
294 |
|
|
295 |
loadButton->setEnabled(false);
|
|
296 |
saveButton->setEnabled(false);
|
|
297 |
exportButton->setEnabled(false);
|
|
298 |
break;
|
|
299 |
|
|
300 |
case NavigationMode:
|
|
301 |
|
|
302 |
if (contacts.isEmpty()) {
|
|
303 |
nameLine->clear();
|
|
304 |
addressText->clear();
|
|
305 |
}
|
|
306 |
|
|
307 |
nameLine->setReadOnly(true);
|
|
308 |
addressText->setReadOnly(true);
|
|
309 |
addButton->setEnabled(true);
|
|
310 |
|
|
311 |
int number = contacts.size();
|
|
312 |
editButton->setEnabled(number >= 1);
|
|
313 |
removeButton->setEnabled(number >= 1);
|
|
314 |
findButton->setEnabled(number > 2);
|
|
315 |
nextButton->setEnabled(number > 1);
|
|
316 |
previousButton->setEnabled(number > 1);
|
|
317 |
|
|
318 |
submitButton->hide();
|
|
319 |
cancelButton->hide();
|
|
320 |
|
|
321 |
exportButton->setEnabled(number >= 1);
|
|
322 |
|
|
323 |
loadButton->setEnabled(true);
|
|
324 |
saveButton->setEnabled(number >= 1);
|
|
325 |
break;
|
|
326 |
}
|
|
327 |
}
|
|
328 |
|
|
329 |
void AddressBook::saveToFile()
|
|
330 |
{
|
|
331 |
QString fileName = QFileDialog::getSaveFileName(this,
|
|
332 |
tr("Save Address Book"), "",
|
|
333 |
tr("Address Book (*.abk);;All Files (*)"));
|
|
334 |
|
|
335 |
if (fileName.isEmpty())
|
|
336 |
return;
|
|
337 |
else {
|
|
338 |
QFile file(fileName);
|
|
339 |
|
|
340 |
if (!file.open(QIODevice::WriteOnly)) {
|
|
341 |
QMessageBox::information(this, tr("Unable to open file"),
|
|
342 |
file.errorString());
|
|
343 |
return;
|
|
344 |
}
|
|
345 |
|
|
346 |
QDataStream out(&file);
|
|
347 |
out.setVersion(QDataStream::Qt_4_3);
|
|
348 |
out << contacts;
|
|
349 |
}
|
|
350 |
|
|
351 |
updateInterface(NavigationMode);
|
|
352 |
}
|
|
353 |
|
|
354 |
void AddressBook::loadFromFile()
|
|
355 |
{
|
|
356 |
QString fileName = QFileDialog::getOpenFileName(this,
|
|
357 |
tr("Open Address Book"), "",
|
|
358 |
tr("Address Book (*.abk);;All Files (*)"));
|
|
359 |
|
|
360 |
if (fileName.isEmpty())
|
|
361 |
return;
|
|
362 |
else {
|
|
363 |
QFile file(fileName);
|
|
364 |
|
|
365 |
if (!file.open(QIODevice::ReadOnly)) {
|
|
366 |
QMessageBox::information(this, tr("Unable to open file"),
|
|
367 |
file.errorString());
|
|
368 |
return;
|
|
369 |
}
|
|
370 |
|
|
371 |
QDataStream in(&file);
|
|
372 |
in.setVersion(QDataStream::Qt_4_3);
|
|
373 |
contacts.empty(); // empty existing contacts
|
|
374 |
in >> contacts;
|
|
375 |
|
|
376 |
QMap<QString, QString>::iterator i = contacts.begin();
|
|
377 |
nameLine->setText(i.key());
|
|
378 |
addressText->setText(i.value());
|
|
379 |
}
|
|
380 |
|
|
381 |
updateInterface(NavigationMode);
|
|
382 |
}
|
|
383 |
|
|
384 |
//! [export function part1]
|
|
385 |
void AddressBook::exportAsVCard()
|
|
386 |
{
|
|
387 |
QString name = nameLine->text();
|
|
388 |
QString address = addressText->toPlainText();
|
|
389 |
QString firstName;
|
|
390 |
QString lastName;
|
|
391 |
QStringList nameList;
|
|
392 |
|
|
393 |
int index = name.indexOf(" ");
|
|
394 |
|
|
395 |
if (index != -1) {
|
|
396 |
nameList = name.split(QRegExp("\\s+"), QString::SkipEmptyParts);
|
|
397 |
firstName = nameList.first();
|
|
398 |
lastName = nameList.last();
|
|
399 |
} else {
|
|
400 |
firstName = name;
|
|
401 |
lastName = "";
|
|
402 |
}
|
|
403 |
|
|
404 |
QString fileName = QFileDialog::getSaveFileName(this,
|
|
405 |
tr("Export Contact"), "",
|
|
406 |
tr("vCard Files (*.vcf);;All Files (*)"));
|
|
407 |
|
|
408 |
if (fileName.isEmpty())
|
|
409 |
return;
|
|
410 |
|
|
411 |
QFile file(fileName);
|
|
412 |
//! [export function part1]
|
|
413 |
|
|
414 |
//! [export function part2]
|
|
415 |
if (!file.open(QIODevice::WriteOnly)) {
|
|
416 |
QMessageBox::information(this, tr("Unable to open file"),
|
|
417 |
file.errorString());
|
|
418 |
return;
|
|
419 |
}
|
|
420 |
|
|
421 |
QTextStream out(&file);
|
|
422 |
//! [export function part2]
|
|
423 |
|
|
424 |
//! [export function part3]
|
|
425 |
out << "BEGIN:VCARD" << "\n";
|
|
426 |
out << "VERSION:2.1" << "\n";
|
|
427 |
out << "N:" << lastName << ";" << firstName << "\n";
|
|
428 |
|
|
429 |
if (!nameList.isEmpty())
|
|
430 |
out << "FN:" << nameList.join(" ") << "\n";
|
|
431 |
else
|
|
432 |
out << "FN:" << firstName << "\n";
|
|
433 |
//! [export function part3]
|
|
434 |
|
|
435 |
//! [export function part4]
|
|
436 |
address.replace(";", "\\;", Qt::CaseInsensitive);
|
|
437 |
address.replace("\n", ";", Qt::CaseInsensitive);
|
|
438 |
address.replace(",", " ", Qt::CaseInsensitive);
|
|
439 |
|
|
440 |
out << "ADR;HOME:;" << address << "\n";
|
|
441 |
out << "END:VCARD" << "\n";
|
|
442 |
|
|
443 |
QMessageBox::information(this, tr("Export Successful"),
|
|
444 |
tr("\"%1\" has been exported as a vCard.").arg(name));
|
|
445 |
}
|
|
446 |
//! [export function part4]
|