OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / doc / examples / addressbook-sdk / part7 / addressbook.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
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
19 **     distribution.
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
23 **     permission.
24 **
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."
36 **
37 **************************************************************************/
38
39 #include <QFileDialog>
40 #include <QTextStream>
41
42 #include "addressbook.h"
43 #include "ui_addressbook.h"
44
45 AddressBook::AddressBook(QWidget *parent)
46     : QWidget(parent), ui(new Ui::AddressBook)
47 {
48     ui->setupUi(this);
49
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);
58
59     connect(ui->addButton, SIGNAL(clicked()), this,
60                 SLOT(addContact()));
61     connect(ui->submitButton, SIGNAL(clicked()), this,
62                 SLOT(submitContact()));
63     connect(ui->cancelButton, SIGNAL(clicked()), this,
64                 SLOT(cancel()));
65     connect(ui->nextButton, SIGNAL(clicked()), this,
66                 SLOT(next()));
67     connect(ui->previousButton, SIGNAL(clicked()), this,
68                 SLOT(previous()));
69     connect(ui->editButton, SIGNAL(clicked()), this,
70                 SLOT(editContact()));
71     connect(ui->removeButton, SIGNAL(clicked()), this,
72                 SLOT(removeContact()));
73     connect(ui->findButton, SIGNAL(clicked()), this,
74                 SLOT(findContact()));
75     connect(ui->exportButton, SIGNAL(clicked()), this,
76                 SLOT(exportAsVCard()));
77
78     setWindowTitle(tr("Simple Address Book"));
79 }
80
81 AddressBook::~AddressBook()
82 {
83     delete ui;
84 }
85
86 void AddressBook::addContact()
87 {
88     oldName = ui->nameLine->text();
89     oldAddress = ui->addressText->toPlainText();
90
91     ui->nameLine->clear();
92     ui->addressText->clear();
93
94     updateInterface(AddingMode);
95 }
96
97 void AddressBook::submitContact()
98 {
99     QString name = ui->nameLine->text();
100     QString address = ui->addressText->toPlainText();
101
102     if (name == "" || address == "") {
103         QMessageBox::information(this, tr("Empty Field"),
104             tr("Please enter a name and address."));
105         updateInterface(NavigationMode);
106         return;
107     }
108
109     if (currentMode == AddingMode) {
110
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));
115         } else {
116             QMessageBox::information(this, tr("Add Unsuccessful"),
117                 tr("Sorry, \"%1\" is already in your address book.").arg(name));
118         }
119
120     } else if (currentMode == EditingMode) {
121
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);
128             } else  {
129                 QMessageBox::information(this, tr("Edit Unsuccessful"),
130                     tr("Sorry, \"%1\" is already in your address book.").arg(name));
131             }
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;
136         }
137     }
138     updateInterface(NavigationMode);
139 }
140
141 void AddressBook::cancel()
142 {
143     ui->nameLine->setText(oldName);
144     ui->nameLine->setReadOnly(true);
145
146     updateInterface(NavigationMode);
147 }
148
149 void AddressBook::next()
150 {
151     QString name = ui->nameLine->text();
152     QMap<QString, QString>::iterator i = contacts.find(name);
153
154     if (i != contacts.end())
155         i++;
156     if (i == contacts.end())
157         i = contacts.begin();
158
159     ui->nameLine->setText(i.key());
160     ui->addressText->setText(i.value());
161 }
162
163 void AddressBook::previous()
164 {
165     QString name = ui->nameLine->text();
166     QMap<QString, QString>::iterator i = contacts.find(name);
167
168     if (i == contacts.end()) {
169         ui->nameLine->clear();
170         ui->addressText->clear();
171         return;
172     }
173
174     if (i == contacts.begin())
175         i = contacts.end();
176
177     i--;
178     ui->nameLine->setText(i.key());
179     ui->addressText->setText(i.value());
180 }
181
182 void AddressBook::editContact()
183 {
184     oldName = ui->nameLine->text();
185     oldAddress = ui->addressText->toPlainText();
186
187     updateInterface(EditingMode);
188 }
189
190 void AddressBook::removeContact()
191 {
192     QString name = ui->nameLine->text();
193     QString address = ui->addressText->toPlainText();
194
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);
200
201         if (button == QMessageBox::Yes) {
202             previous();
203             contacts.remove(name);
204
205             QMessageBox::information(this, tr("Remove Successful"),
206                 tr("\"%1\" has been removed from your address book.").arg(name));
207         }
208     }
209
210     updateInterface(NavigationMode);
211 }
212
213 void AddressBook::updateInterface(Mode mode)
214 {
215     currentMode = mode;
216
217     switch (currentMode) {
218
219     case AddingMode:
220     case EditingMode:
221
222         ui->nameLine->setReadOnly(false);
223         ui->nameLine->setFocus(Qt::OtherFocusReason);
224         ui->addressText->setReadOnly(false);
225
226         ui->addButton->setEnabled(false);
227         ui->editButton->setEnabled(false);
228         ui->removeButton->setEnabled(false);
229
230         ui->nextButton->setEnabled(false);
231         ui->previousButton->setEnabled(false);
232
233         ui->submitButton->show();
234         ui->cancelButton->show();
235         break;
236
237     case NavigationMode:
238
239         if (contacts.isEmpty()) {
240             ui->nameLine->clear();
241             ui->addressText->clear();
242         }
243
244         ui->nameLine->setReadOnly(true);
245         ui->addressText->setReadOnly(true);
246         ui->addButton->setEnabled(true);
247
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);
254
255         ui->submitButton->hide();
256         ui->cancelButton->hide();
257         break;
258     }
259 }
260
261 void AddressBook::findContact()
262 {
263     FindDialog dialog;
264
265     if (dialog.exec() == QDialog::Accepted) {
266         QString contactName = dialog.findText();
267
268         if (contacts.contains(contactName)) {
269             ui->nameLine->setText(contactName);
270             ui->addressText->setText(contacts.value(contactName));
271         } else {
272             QMessageBox::information(this, tr("Contact Not Found"),
273                 tr("Sorry, \"%1\" is not in your address book.").arg(contactName));
274             return;
275         }
276     }
277
278     updateInterface(NavigationMode);
279 }
280
281 void AddressBook::saveToFile()
282 {
283     QString fileName = QFileDialog::getSaveFileName(this,
284         tr("Save Address Book"), "",
285         tr("Address book (*.abk);; AllFiles (*)"));
286
287     if (fileName.isEmpty())
288         return;
289     else {
290         QFile file(fileName);
291
292         if (!file.open(QIODevice::WriteOnly)) {
293             QMessageBox::information(this, tr("Unable to open file"),
294                 file.errorString());
295             return;
296         }
297
298         QDataStream out(&file);
299         out.setVersion(QDataStream::Qt_4_5);
300         out << contacts;
301     }
302 }
303
304 void AddressBook::loadFromFile()
305 {
306     QString fileName = QFileDialog::getOpenFileName(this,
307         tr("Open Address Book"), "",
308         tr("Address Book(*.abk);; All Files(*)"));
309
310     if (fileName.isEmpty())
311         return;
312     else {
313         QFile file(fileName);
314
315         if (!file.open(QIODevice::ReadOnly)) {
316             QMessageBox::information(this, tr("Unable to open file"),
317                 file.errorString());
318             return;
319         }
320
321         QDataStream in(&file);
322         in.setVersion(QDataStream::Qt_4_5);
323         contacts.empty();   // empty existing contacts
324         in >> contacts;
325
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."));
329         } else {
330             QMap<QString, QString>::iterator i = contacts.begin();
331             ui->nameLine->setText(i.key());
332             ui->addressText->setText(i.value());
333         }
334     }
335
336     updateInterface(NavigationMode);
337 }
338
339 void AddressBook::exportAsVCard()
340 {
341     QString name = ui->nameLine->text();
342     QString address = ui->addressText->toPlainText();
343     QString firstName;
344     QString lastName;
345     QStringList nameList;
346
347     int index = name.indexOf(" ");
348
349     if (index != -1) {
350         nameList = name.split(QRegExp("\\s+"), QString::SkipEmptyParts);
351         firstName = nameList.first();
352         lastName = nameList.last();
353     } else {
354         firstName = name;
355         lastName = "";
356     }
357
358     QString fileName = QFileDialog::getSaveFileName(this,
359         tr("Export Coontact"), "",
360         tr("vCard files (*.vcf);;All Files (*)"));
361
362     if (fileName.isEmpty())
363         return;
364
365     QFile file(fileName);
366
367     if (!file.open(QIODevice::WriteOnly)) {
368         QMessageBox::information(this, tr("Unable to open file"),
369             file.errorString());
370         return;
371     }
372
373     QTextStream out(&file);
374
375     out << "BEGIN:VCARD" << "\n";
376     out << "VERSION:2.1" << "\n";
377     out << "N:" << lastName << ";" << firstName << "\n";
378
379     if (!nameList.isEmpty())
380         out << "FN:" << nameList.join(" ") << "\n";
381     else
382         out << "FN:" << firstName << "\n";
383
384     address.replace(";", "\\;", Qt::CaseInsensitive);
385     address.replace("\n", ";", Qt::CaseInsensitive);
386     address.replace(",", " ", Qt::CaseInsensitive);
387
388     out << "ADR;HOME:;" << address << "\n";
389     out << "END;VCARD" << "\n";
390
391     QMessageBox::information(this, tr("Export Successful"),
392         tr("\"%1\" has been exported as a vCard.").arg(name));
393 }