OSDN Git Service

e56caf353809a94821491da9fc4497cc1c58bea0
[qt-creator-jp/qt-creator-jp.git] / doc / examples / addressbook-sdk / part6 / 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
41 #include "addressbook.h"
42 #include "ui_addressbook.h"
43
44 AddressBook::AddressBook(QWidget *parent)
45     : QWidget(parent), ui(new Ui::AddressBook)
46 {
47     ui->setupUi(this);
48
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);
57
58     connect(ui->addButton, SIGNAL(clicked()), this,
59                 SLOT(addContact()));
60     connect(ui->submitButton, SIGNAL(clicked()), this,
61                 SLOT(submitContact()));
62     connect(ui->cancelButton, SIGNAL(clicked()), this,
63                 SLOT(cancel()));
64     connect(ui->nextButton, SIGNAL(clicked()), this,
65                 SLOT(next()));
66     connect(ui->previousButton, SIGNAL(clicked()), this,
67                 SLOT(previous()));
68     connect(ui->editButton, SIGNAL(clicked()), this,
69                 SLOT(editContact()));
70     connect(ui->removeButton, SIGNAL(clicked()), this,
71                 SLOT(removeContact()));
72     connect(ui->findButton, SIGNAL(clicked()), this,
73                 SLOT(findContact()));
74     //! [connectSlots]
75     connect(ui->loadButton, SIGNAL(clicked()), this,
76                 SLOT(loadFromFile()));
77     connect(ui->saveButton, SIGNAL(clicked()), this,
78                 SLOT(saveToFile()));
79     //! [connectSlots]
80
81     setWindowTitle(tr("Simple Address Book"));
82 }
83
84 AddressBook::~AddressBook()
85 {
86     delete ui;
87 }
88
89 void AddressBook::addContact()
90 {
91     oldName = ui->nameLine->text();
92     oldAddress = ui->addressText->toPlainText();
93
94     ui->nameLine->clear();
95     ui->addressText->clear();
96
97     updateInterface(AddingMode);
98 }
99
100 void AddressBook::submitContact()
101 {
102     QString name = ui->nameLine->text();
103     QString address = ui->addressText->toPlainText();
104
105     if (name.isEmpty() || address.isEmpty()) {
106         QMessageBox::information(this, tr("Empty Field"),
107             tr("Please enter a name and address."));
108         updateInterface(NavigationMode);
109         return;
110     }
111
112     if (currentMode == AddingMode) {
113
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));
118         } else {
119             QMessageBox::information(this, tr("Add Unsuccessful"),
120                 tr("Sorry, \"%1\" is already in your address book.").arg(name));
121         }
122
123     } else if (currentMode == EditingMode) {
124
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);
131             } else  {
132                 QMessageBox::information(this, tr("Edit Unsuccessful"),
133                     tr("Sorry, \"%1\" is already in your address book.").arg(name));
134             }
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;
139         }
140     }
141     updateInterface(NavigationMode);
142 }
143
144 void AddressBook::cancel()
145 {
146     ui->nameLine->setText(oldName);
147     ui->nameLine->setReadOnly(true);
148
149     updateInterface(NavigationMode);
150 }
151
152 void AddressBook::next()
153 {
154     QString name = ui->nameLine->text();
155     QMap<QString, QString>::iterator i = contacts.find(name);
156
157     if (i != contacts.end())
158         i++;
159     if (i == contacts.end())
160         i = contacts.begin();
161
162     ui->nameLine->setText(i.key());
163     ui->addressText->setText(i.value());
164 }
165
166 void AddressBook::previous()
167 {
168     QString name = ui->nameLine->text();
169     QMap<QString, QString>::iterator i = contacts.find(name);
170
171     if (i == contacts.end()) {
172         ui->nameLine->clear();
173         ui->addressText->clear();
174         return;
175     }
176
177     if (i == contacts.begin())
178         i = contacts.end();
179
180     i--;
181     ui->nameLine->setText(i.key());
182     ui->addressText->setText(i.value());
183 }
184
185 void AddressBook::editContact()
186 {
187     oldName = ui->nameLine->text();
188     oldAddress = ui->addressText->toPlainText();
189
190     updateInterface(EditingMode);
191 }
192
193 void AddressBook::removeContact()
194 {
195     QString name = ui->nameLine->text();
196     QString address = ui->addressText->toPlainText();
197
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);
203
204         if (button == QMessageBox::Yes) {
205             previous();
206             contacts.remove(name);
207
208             QMessageBox::information(this, tr("Remove Successful"),
209                 tr("\"%1\" has been removed from your address book.").arg(name));
210         }
211     }
212
213     updateInterface(NavigationMode);
214 }
215
216 void AddressBook::updateInterface(Mode mode)
217 {
218     currentMode = mode;
219
220     switch (currentMode) {
221
222     case AddingMode:
223     case EditingMode:
224
225         ui->nameLine->setReadOnly(false);
226         ui->nameLine->setFocus(Qt::OtherFocusReason);
227         ui->addressText->setReadOnly(false);
228
229         ui->addButton->setEnabled(false);
230         ui->editButton->setEnabled(false);
231         ui->removeButton->setEnabled(false);
232
233         ui->nextButton->setEnabled(false);
234         ui->previousButton->setEnabled(false);
235
236         ui->submitButton->show();
237         ui->cancelButton->show();
238         break;
239
240     case NavigationMode:
241
242         if (contacts.isEmpty()) {
243             ui->nameLine->clear();
244             ui->addressText->clear();
245         }
246
247         ui->nameLine->setReadOnly(true);
248         ui->addressText->setReadOnly(true);
249         ui->addButton->setEnabled(true);
250
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);
257
258         ui->submitButton->hide();
259         ui->cancelButton->hide();
260         break;
261     }
262 }
263
264 void AddressBook::findContact()
265 {
266     FindDialog dialog;
267
268     if (dialog.exec() == QDialog::Accepted) {
269         QString contactName = dialog.findText();
270
271         if (contacts.contains(contactName)) {
272             ui->nameLine->setText(contactName);
273             ui->addressText->setText(contacts.value(contactName));
274         } else {
275             QMessageBox::information(this, tr("Contact Not Found"),
276                 tr("Sorry, \"%1\" is not in your address book.").arg(contactName));
277             return;
278         }
279     }
280
281     updateInterface(NavigationMode);
282 }
283
284 //! [saveToFile part1]
285 void AddressBook::saveToFile()
286 {
287     QString fileName = QFileDialog::getSaveFileName(this,
288         tr("Save Address Book"), "",
289         tr("Address book (*.abk);; AllFiles (*)"));
290 //! [saveToFile part1]
291
292 //! [saveToFile part2]
293     if (fileName.isEmpty())
294         return;
295     else {
296         QFile file(fileName);
297
298         if (!file.open(QIODevice::WriteOnly)) {
299             QMessageBox::information(this, tr("Unable to open file"),
300                 file.errorString());
301             return;
302         }
303 //! [saveToFile part2]
304
305 //! [saveToFile part3]
306         QDataStream out(&file);
307         out.setVersion(QDataStream::Qt_4_5);
308         out << contacts;
309     }
310 }
311 //! [saveToFile part3]
312
313 //! [loadFromFile part1]
314 void AddressBook::loadFromFile()
315 {
316     QString fileName = QFileDialog::getOpenFileName(this,
317         tr("Open Address Book"), "",
318         tr("Address Book(*.abk);; All Files(*)"));
319 //! [loadFromFile part1]
320
321 //! [loadFromFile part2]
322     if (fileName.isEmpty())
323         return;
324     else {
325         QFile file(fileName);
326
327         if (!file.open(QIODevice::ReadOnly)) {
328             QMessageBox::information(this, tr("Unable to open file"),
329                 file.errorString());
330             return;
331         }
332
333         QDataStream in(&file);
334         in.setVersion(QDataStream::Qt_4_5);
335         contacts.clear();   // remove all items
336         in >> contacts;
337 //! [loadFromFile part2]
338
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."));
343         } else {
344             QMap<QString, QString>::iterator i = contacts.begin();
345             ui->nameLine->setText(i.key());
346             ui->addressText->setText(i.value());
347         }
348     }
349
350     updateInterface(NavigationMode);
351 }
352 //! [loadFromFile part3]