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 "addressbook.h"
40 #include "ui_addressbook.h"
42 AddressBook::AddressBook(QWidget *parent)
43 : QWidget(parent), ui(new Ui::AddressBook)
47 ui->nameLine->setReadOnly(true);
48 ui->addressText->setReadOnly(true);
49 ui->submitButton->hide();
50 ui->cancelButton->hide();
51 ui->nextButton->setEnabled(false);
52 ui->previousButton->setEnabled(false);
53 ui->editButton->setEnabled(false);
54 ui->removeButton->setEnabled(false);
56 connect(ui->addButton, SIGNAL(clicked()), this,
58 connect(ui->submitButton, SIGNAL(clicked()), this,
59 SLOT(submitContact()));
60 connect(ui->cancelButton, SIGNAL(clicked()), this,
62 connect(ui->nextButton, SIGNAL(clicked()), this,
64 connect(ui->previousButton, SIGNAL(clicked()), this,
66 connect(ui->editButton, SIGNAL(clicked()), this,
68 connect(ui->removeButton, SIGNAL(clicked()), this,
69 SLOT(removeContact()));
71 connect(ui->findButton, SIGNAL(clicked()), this,
75 setWindowTitle(tr("Simple Address Book"));
78 AddressBook::~AddressBook()
83 void AddressBook::addContact()
85 oldName = ui->nameLine->text();
86 oldAddress = ui->addressText->toPlainText();
88 ui->nameLine->clear();
89 ui->addressText->clear();
91 updateInterface(AddingMode);
94 void AddressBook::submitContact()
96 QString name = ui->nameLine->text();
97 QString address = ui->addressText->toPlainText();
99 if (name == "" || address == "") {
100 QMessageBox::information(this, tr("Empty Field"),
101 tr("Please enter a name and address."));
102 updateInterface(NavigationMode);
106 if (currentMode == AddingMode) {
108 if (!contacts.contains(name)) {
109 contacts.insert(name, address);
110 QMessageBox::information(this, tr("Add Successful"),
111 tr("\"%1\" has been added to your address book.").arg(name));
113 QMessageBox::information(this, tr("Add Unsuccessful"),
114 tr("Sorry, \"%1\" is already in your address book.").arg(name));
117 } else if (currentMode == EditingMode) {
119 if (oldName != name) {
120 if (!contacts.contains(name)) {
121 QMessageBox::information(this, tr("Edit Successful"),
122 tr("\"%1\" has been edited in your address book.").arg(oldName));
123 contacts.remove(oldName);
124 contacts.insert(name, address);
126 QMessageBox::information(this, tr("Edit Unsuccessful"),
127 tr("Sorry, \"%1\" is already in your address book.").arg(name));
129 } else if (oldAddress != address) {
130 QMessageBox::information(this, tr("Edit Successful"),
131 tr("\"%1\" has been edited in your address book.").arg(name));
132 contacts[name] = address;
135 updateInterface(NavigationMode);
138 void AddressBook::cancel()
140 ui->nameLine->setText(oldName);
141 ui->nameLine->setReadOnly(true);
143 updateInterface(NavigationMode);
146 void AddressBook::next()
148 QString name = ui->nameLine->text();
149 QMap<QString, QString>::iterator i = contacts.find(name);
151 if (i != contacts.end())
153 if (i == contacts.end())
154 i = contacts.begin();
156 ui->nameLine->setText(i.key());
157 ui->addressText->setText(i.value());
160 void AddressBook::previous()
162 QString name = ui->nameLine->text();
163 QMap<QString, QString>::iterator i = contacts.find(name);
165 if (i == contacts.end()) {
166 ui->nameLine->clear();
167 ui->addressText->clear();
171 if (i == contacts.begin())
175 ui->nameLine->setText(i.key());
176 ui->addressText->setText(i.value());
179 void AddressBook::editContact()
181 oldName = ui->nameLine->text();
182 oldAddress = ui->addressText->toPlainText();
184 updateInterface(EditingMode);
187 void AddressBook::removeContact()
189 QString name = ui->nameLine->text();
190 QString address = ui->addressText->toPlainText();
192 if (contacts.contains(name)) {
193 int button = QMessageBox::question(this,
194 tr("Confirm Remove"),
195 tr("Are you sure you want to remove \"%1\"?").arg(name),
196 QMessageBox::Yes | QMessageBox::No);
198 if (button == QMessageBox::Yes) {
200 contacts.remove(name);
202 QMessageBox::information(this, tr("Remove Successful"),
203 tr("\"%1\" has been removed from your address book.").arg(name));
207 updateInterface(NavigationMode);
210 void AddressBook::updateInterface(Mode mode)
214 switch (currentMode) {
219 ui->nameLine->setReadOnly(false);
220 ui->nameLine->setFocus(Qt::OtherFocusReason);
221 ui->addressText->setReadOnly(false);
223 ui->addButton->setEnabled(false);
224 ui->editButton->setEnabled(false);
225 ui->removeButton->setEnabled(false);
227 ui->nextButton->setEnabled(false);
228 ui->previousButton->setEnabled(false);
230 ui->submitButton->show();
231 ui->cancelButton->show();
236 if (contacts.isEmpty()) {
237 ui->nameLine->clear();
238 ui->addressText->clear();
241 ui->nameLine->setReadOnly(true);
242 ui->addressText->setReadOnly(true);
243 ui->addButton->setEnabled(true);
245 int number = contacts.size();
246 ui->editButton->setEnabled(number >= 1);
247 ui->removeButton->setEnabled(number >= 1);
249 ui->findButton->setEnabled(number > 2);
251 ui->nextButton->setEnabled(number > 1);
252 ui->previousButton->setEnabled(number >1);
254 ui->submitButton->hide();
255 ui->cancelButton->hide();
261 void AddressBook::findContact()
265 if (dialog.exec() == QDialog::Accepted) {
266 QString contactName = dialog.findText();
268 if (contacts.contains(contactName)) {
269 ui->nameLine->setText(contactName);
270 ui->addressText->setText(contacts.value(contactName));
272 QMessageBox::information(this, tr("Contact Not Found"),
273 tr("Sorry, \"%1\" is not in your address book.").arg(contactName));
278 updateInterface(NavigationMode);