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>
40 #include <QTextStream>
42 #include "addressbook.h"
43 #include "ui_addressbook.h"
45 AddressBook::AddressBook(QWidget *parent)
46 : QWidget(parent), ui(new Ui::AddressBook)
50 ui->nameLine->setReadOnly(true);
51 ui->addressText->setReadOnly(true);
52 ui->submitButton->hide();
53 ui->cancelButton->hide();
54 ui->nextButton->setEnabled(false);
55 ui->previousButton->setEnabled(false);
56 ui->editButton->setEnabled(false);
57 ui->removeButton->setEnabled(false);
59 connect(ui->addButton, SIGNAL(clicked()), this,
61 connect(ui->submitButton, SIGNAL(clicked()), this,
62 SLOT(submitContact()));
63 connect(ui->cancelButton, SIGNAL(clicked()), this,
65 connect(ui->nextButton, SIGNAL(clicked()), this,
67 connect(ui->previousButton, SIGNAL(clicked()), this,
69 connect(ui->editButton, SIGNAL(clicked()), this,
71 connect(ui->removeButton, SIGNAL(clicked()), this,
72 SLOT(removeContact()));
73 connect(ui->findButton, SIGNAL(clicked()), this,
75 connect(ui->exportButton, SIGNAL(clicked()), this,
76 SLOT(exportAsVCard()));
78 setWindowTitle(tr("Simple Address Book"));
81 AddressBook::~AddressBook()
86 void AddressBook::addContact()
88 oldName = ui->nameLine->text();
89 oldAddress = ui->addressText->toPlainText();
91 ui->nameLine->clear();
92 ui->addressText->clear();
94 updateInterface(AddingMode);
97 void AddressBook::submitContact()
99 QString name = ui->nameLine->text();
100 QString address = ui->addressText->toPlainText();
102 if (name.isEmpty() || address.isEmpty()) {
103 QMessageBox::information(this, tr("Empty Field"),
104 tr("Please enter a name and address."));
105 updateInterface(NavigationMode);
109 if (currentMode == AddingMode) {
111 if (!contacts.contains(name)) {
112 contacts.insert(name, address);
113 QMessageBox::information(this, tr("Add Successful"),
114 tr("\"%1\" has been added to your address book.").arg(name));
116 QMessageBox::information(this, tr("Add Unsuccessful"),
117 tr("Sorry, \"%1\" is already in your address book.").arg(name));
120 } else if (currentMode == EditingMode) {
122 if (oldName != name) {
123 if (!contacts.contains(name)) {
124 QMessageBox::information(this, tr("Edit Successful"),
125 tr("\"%1\" has been edited in your address book.").arg(oldName));
126 contacts.remove(oldName);
127 contacts.insert(name, address);
129 QMessageBox::information(this, tr("Edit Unsuccessful"),
130 tr("Sorry, \"%1\" is already in your address book.").arg(name));
132 } else if (oldAddress != address) {
133 QMessageBox::information(this, tr("Edit Successful"),
134 tr("\"%1\" has been edited in your address book.").arg(name));
135 contacts[name] = address;
138 updateInterface(NavigationMode);
141 void AddressBook::cancel()
143 ui->nameLine->setText(oldName);
144 ui->nameLine->setReadOnly(true);
146 updateInterface(NavigationMode);
149 void AddressBook::next()
151 QString name = ui->nameLine->text();
152 QMap<QString, QString>::iterator i = contacts.find(name);
154 if (i != contacts.end())
156 if (i == contacts.end())
157 i = contacts.begin();
159 ui->nameLine->setText(i.key());
160 ui->addressText->setText(i.value());
163 void AddressBook::previous()
165 QString name = ui->nameLine->text();
166 QMap<QString, QString>::iterator i = contacts.find(name);
168 if (i == contacts.end()) {
169 ui->nameLine->clear();
170 ui->addressText->clear();
174 if (i == contacts.begin())
178 ui->nameLine->setText(i.key());
179 ui->addressText->setText(i.value());
182 void AddressBook::editContact()
184 oldName = ui->nameLine->text();
185 oldAddress = ui->addressText->toPlainText();
187 updateInterface(EditingMode);
190 void AddressBook::removeContact()
192 QString name = ui->nameLine->text();
193 QString address = ui->addressText->toPlainText();
195 if (contacts.contains(name)) {
196 int button = QMessageBox::question(this,
197 tr("Confirm Remove"),
198 tr("Are you sure you want to remove \"%1\"?").arg(name),
199 QMessageBox::Yes | QMessageBox::No);
201 if (button == QMessageBox::Yes) {
203 contacts.remove(name);
205 QMessageBox::information(this, tr("Remove Successful"),
206 tr("\"%1\" has been removed from your address book.").arg(name));
210 updateInterface(NavigationMode);
213 void AddressBook::updateInterface(Mode mode)
217 switch (currentMode) {
222 ui->nameLine->setReadOnly(false);
223 ui->nameLine->setFocus(Qt::OtherFocusReason);
224 ui->addressText->setReadOnly(false);
226 ui->addButton->setEnabled(false);
227 ui->editButton->setEnabled(false);
228 ui->removeButton->setEnabled(false);
230 ui->nextButton->setEnabled(false);
231 ui->previousButton->setEnabled(false);
233 ui->submitButton->show();
234 ui->cancelButton->show();
239 if (contacts.isEmpty()) {
240 ui->nameLine->clear();
241 ui->addressText->clear();
244 ui->nameLine->setReadOnly(true);
245 ui->addressText->setReadOnly(true);
246 ui->addButton->setEnabled(true);
248 int number = contacts.size();
249 ui->editButton->setEnabled(number >= 1);
250 ui->removeButton->setEnabled(number >= 1);
251 ui->findButton->setEnabled(number > 2);
252 ui->nextButton->setEnabled(number > 1);
253 ui->previousButton->setEnabled(number >1);
255 ui->submitButton->hide();
256 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);
281 void AddressBook::saveToFile()
283 QString fileName = QFileDialog::getSaveFileName(this,
284 tr("Save Address Book"), "",
285 tr("Address book (*.abk);; AllFiles (*)"));
287 if (fileName.isEmpty())
290 QFile file(fileName);
292 if (!file.open(QIODevice::WriteOnly)) {
293 QMessageBox::information(this, tr("Unable to open file"),
298 QDataStream out(&file);
299 out.setVersion(QDataStream::Qt_4_5);
304 void AddressBook::loadFromFile()
306 QString fileName = QFileDialog::getOpenFileName(this,
307 tr("Open Address Book"), "",
308 tr("Address Book(*.abk);; All Files(*)"));
310 if (fileName.isEmpty())
313 QFile file(fileName);
315 if (!file.open(QIODevice::ReadOnly)) {
316 QMessageBox::information(this, tr("Unable to open file"),
321 QDataStream in(&file);
322 in.setVersion(QDataStream::Qt_4_5);
323 contacts.empty(); // empty existing contacts
326 if (contacts.isEmpty()) {
327 QMessageBox::information(this, tr("No contacts in file"),
328 tr("The file you are attempting to open contains no contacts."));
330 QMap<QString, QString>::iterator i = contacts.begin();
331 ui->nameLine->setText(i.key());
332 ui->addressText->setText(i.value());
336 updateInterface(NavigationMode);
339 void AddressBook::exportAsVCard()
341 QString name = ui->nameLine->text();
342 QString address = ui->addressText->toPlainText();
345 QStringList nameList;
347 int index = name.indexOf(" ");
350 nameList = name.split(QRegExp("\\s+"), QString::SkipEmptyParts);
351 firstName = nameList.first();
352 lastName = nameList.last();
358 QString fileName = QFileDialog::getSaveFileName(this,
359 tr("Export Coontact"), "",
360 tr("vCard files (*.vcf);;All Files (*)"));
362 if (fileName.isEmpty())
365 QFile file(fileName);
367 if (!file.open(QIODevice::WriteOnly)) {
368 QMessageBox::information(this, tr("Unable to open file"),
373 QTextStream out(&file);
375 out << "BEGIN:VCARD" << "\n";
376 out << "VERSION:2.1" << "\n";
377 out << "N:" << lastName << ";" << firstName << "\n";
379 if (!nameList.isEmpty())
380 out << "FN:" << nameList.join(" ") << "\n";
382 out << "FN:" << firstName << "\n";
384 address.replace(";", "\\;", Qt::CaseInsensitive);
385 address.replace("\n", ";", Qt::CaseInsensitive);
386 address.replace(",", " ", Qt::CaseInsensitive);
388 out << "ADR;HOME:;" << address << "\n";
389 out << "END;VCARD" << "\n";
391 QMessageBox::information(this, tr("Export Successful"),
392 tr("\"%1\" has been exported as a vCard.").arg(name));