1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
9 ** You may use this file under the terms of the BSD license as follows:
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
14 ** * Redistributions of source code must retain the above copyright
15 ** notice, this list of conditions and the following disclaimer.
16 ** * Redistributions in binary form must reproduce the above copyright
17 ** notice, this list of conditions and the following disclaimer in
18 ** the documentation and/or other materials provided with the
20 ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
21 ** the names of its contributors may be used to endorse or promote
22 ** products derived from this software without specific prior written
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37 **************************************************************************/
39 #include <QFileDialog>
41 #include "addressbook.h"
42 #include "ui_addressbook.h"
44 AddressBook::AddressBook(QWidget *parent)
45 : QWidget(parent), ui(new Ui::AddressBook)
49 ui->nameLine->setReadOnly(true);
50 ui->addressText->setReadOnly(true);
51 ui->submitButton->hide();
52 ui->cancelButton->hide();
53 ui->nextButton->setEnabled(false);
54 ui->previousButton->setEnabled(false);
55 ui->editButton->setEnabled(false);
56 ui->removeButton->setEnabled(false);
58 connect(ui->addButton, SIGNAL(clicked()), this,
60 connect(ui->submitButton, SIGNAL(clicked()), this,
61 SLOT(submitContact()));
62 connect(ui->cancelButton, SIGNAL(clicked()), this,
64 connect(ui->nextButton, SIGNAL(clicked()), this,
66 connect(ui->previousButton, SIGNAL(clicked()), this,
68 connect(ui->editButton, SIGNAL(clicked()), this,
70 connect(ui->removeButton, SIGNAL(clicked()), this,
71 SLOT(removeContact()));
72 connect(ui->findButton, SIGNAL(clicked()), this,
75 connect(ui->loadButton, SIGNAL(clicked()), this,
76 SLOT(loadFromFile()));
77 connect(ui->saveButton, SIGNAL(clicked()), this,
81 setWindowTitle(tr("Simple Address Book"));
84 AddressBook::~AddressBook()
89 void AddressBook::addContact()
91 oldName = ui->nameLine->text();
92 oldAddress = ui->addressText->toPlainText();
94 ui->nameLine->clear();
95 ui->addressText->clear();
97 updateInterface(AddingMode);
100 void AddressBook::submitContact()
102 QString name = ui->nameLine->text();
103 QString address = ui->addressText->toPlainText();
105 if (name.isEmpty() || address.isEmpty()) {
106 QMessageBox::information(this, tr("Empty Field"),
107 tr("Please enter a name and address."));
108 updateInterface(NavigationMode);
112 if (currentMode == AddingMode) {
114 if (!contacts.contains(name)) {
115 contacts.insert(name, address);
116 QMessageBox::information(this, tr("Add Successful"),
117 tr("\"%1\" has been added to your address book.").arg(name));
119 QMessageBox::information(this, tr("Add Unsuccessful"),
120 tr("Sorry, \"%1\" is already in your address book.").arg(name));
123 } else if (currentMode == EditingMode) {
125 if (oldName != name) {
126 if (!contacts.contains(name)) {
127 QMessageBox::information(this, tr("Edit Successful"),
128 tr("\"%1\" has been edited in your address book.").arg(oldName));
129 contacts.remove(oldName);
130 contacts.insert(name, address);
132 QMessageBox::information(this, tr("Edit Unsuccessful"),
133 tr("Sorry, \"%1\" is already in your address book.").arg(name));
135 } else if (oldAddress != address) {
136 QMessageBox::information(this, tr("Edit Successful"),
137 tr("\"%1\" has been edited in your address book.").arg(name));
138 contacts[name] = address;
141 updateInterface(NavigationMode);
144 void AddressBook::cancel()
146 ui->nameLine->setText(oldName);
147 ui->nameLine->setReadOnly(true);
149 updateInterface(NavigationMode);
152 void AddressBook::next()
154 QString name = ui->nameLine->text();
155 QMap<QString, QString>::iterator i = contacts.find(name);
157 if (i != contacts.end())
159 if (i == contacts.end())
160 i = contacts.begin();
162 ui->nameLine->setText(i.key());
163 ui->addressText->setText(i.value());
166 void AddressBook::previous()
168 QString name = ui->nameLine->text();
169 QMap<QString, QString>::iterator i = contacts.find(name);
171 if (i == contacts.end()) {
172 ui->nameLine->clear();
173 ui->addressText->clear();
177 if (i == contacts.begin())
181 ui->nameLine->setText(i.key());
182 ui->addressText->setText(i.value());
185 void AddressBook::editContact()
187 oldName = ui->nameLine->text();
188 oldAddress = ui->addressText->toPlainText();
190 updateInterface(EditingMode);
193 void AddressBook::removeContact()
195 QString name = ui->nameLine->text();
196 QString address = ui->addressText->toPlainText();
198 if (contacts.contains(name)) {
199 int button = QMessageBox::question(this,
200 tr("Confirm Remove"),
201 tr("Are you sure you want to remove \"%1\"?").arg(name),
202 QMessageBox::Yes | QMessageBox::No);
204 if (button == QMessageBox::Yes) {
206 contacts.remove(name);
208 QMessageBox::information(this, tr("Remove Successful"),
209 tr("\"%1\" has been removed from your address book.").arg(name));
213 updateInterface(NavigationMode);
216 void AddressBook::updateInterface(Mode mode)
220 switch (currentMode) {
225 ui->nameLine->setReadOnly(false);
226 ui->nameLine->setFocus(Qt::OtherFocusReason);
227 ui->addressText->setReadOnly(false);
229 ui->addButton->setEnabled(false);
230 ui->editButton->setEnabled(false);
231 ui->removeButton->setEnabled(false);
233 ui->nextButton->setEnabled(false);
234 ui->previousButton->setEnabled(false);
236 ui->submitButton->show();
237 ui->cancelButton->show();
242 if (contacts.isEmpty()) {
243 ui->nameLine->clear();
244 ui->addressText->clear();
247 ui->nameLine->setReadOnly(true);
248 ui->addressText->setReadOnly(true);
249 ui->addButton->setEnabled(true);
251 int number = contacts.size();
252 ui->editButton->setEnabled(number >= 1);
253 ui->removeButton->setEnabled(number >= 1);
254 ui->findButton->setEnabled(number > 2);
255 ui->nextButton->setEnabled(number > 1);
256 ui->previousButton->setEnabled(number >1);
258 ui->submitButton->hide();
259 ui->cancelButton->hide();
264 void AddressBook::findContact()
268 if (dialog.exec() == QDialog::Accepted) {
269 QString contactName = dialog.findText();
271 if (contacts.contains(contactName)) {
272 ui->nameLine->setText(contactName);
273 ui->addressText->setText(contacts.value(contactName));
275 QMessageBox::information(this, tr("Contact Not Found"),
276 tr("Sorry, \"%1\" is not in your address book.").arg(contactName));
281 updateInterface(NavigationMode);
284 //! [saveToFile part1]
285 void AddressBook::saveToFile()
287 QString fileName = QFileDialog::getSaveFileName(this,
288 tr("Save Address Book"), "",
289 tr("Address book (*.abk);; AllFiles (*)"));
290 //! [saveToFile part1]
292 //! [saveToFile part2]
293 if (fileName.isEmpty())
296 QFile file(fileName);
298 if (!file.open(QIODevice::WriteOnly)) {
299 QMessageBox::information(this, tr("Unable to open file"),
303 //! [saveToFile part2]
305 //! [saveToFile part3]
306 QDataStream out(&file);
307 out.setVersion(QDataStream::Qt_4_5);
311 //! [saveToFile part3]
313 //! [loadFromFile part1]
314 void AddressBook::loadFromFile()
316 QString fileName = QFileDialog::getOpenFileName(this,
317 tr("Open Address Book"), "",
318 tr("Address Book(*.abk);; All Files(*)"));
319 //! [loadFromFile part1]
321 //! [loadFromFile part2]
322 if (fileName.isEmpty())
325 QFile file(fileName);
327 if (!file.open(QIODevice::ReadOnly)) {
328 QMessageBox::information(this, tr("Unable to open file"),
333 QDataStream in(&file);
334 in.setVersion(QDataStream::Qt_4_5);
335 contacts.clear(); // remove all items
337 //! [loadFromFile part2]
339 //! [loadFromFile part3]
340 if (contacts.isEmpty()) {
341 QMessageBox::information(this, tr("No contacts in file"),
342 tr("The file you are attempting to open contains no contacts."));
344 QMap<QString, QString>::iterator i = contacts.begin();
345 ui->nameLine->setText(i.key());
346 ui->addressText->setText(i.value());
350 updateInterface(NavigationMode);
352 //! [loadFromFile part3]