1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the examples of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:BSD$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** BSD License Usage |
18 | ** Alternatively, you may use this file under the terms of the BSD license |
19 | ** as follows: |
20 | ** |
21 | ** "Redistribution and use in source and binary forms, with or without |
22 | ** modification, are permitted provided that the following conditions are |
23 | ** met: |
24 | ** * Redistributions of source code must retain the above copyright |
25 | ** notice, this list of conditions and the following disclaimer. |
26 | ** * Redistributions in binary form must reproduce the above copyright |
27 | ** notice, this list of conditions and the following disclaimer in |
28 | ** the documentation and/or other materials provided with the |
29 | ** distribution. |
30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
31 | ** contributors may be used to endorse or promote products derived |
32 | ** from this software without specific prior written permission. |
33 | ** |
34 | ** |
35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
46 | ** |
47 | ** $QT_END_LICENSE$ |
48 | ** |
49 | ****************************************************************************/ |
50 | |
51 | #include <QtWidgets> |
52 | #include "addressbook.h" |
53 | |
54 | AddressBook::AddressBook(QWidget *parent) |
55 | : QWidget(parent) |
56 | { |
57 | QLabel *nameLabel = new QLabel(tr(s: "Name:" )); |
58 | nameLine = new QLineEdit; |
59 | nameLine->setReadOnly(true); |
60 | |
61 | QLabel *addressLabel = new QLabel(tr(s: "Address:" )); |
62 | addressText = new QTextEdit; |
63 | addressText->setReadOnly(true); |
64 | |
65 | addButton = new QPushButton(tr(s: "&Add" )); |
66 | |
67 | editButton = new QPushButton(tr(s: "&Edit" )); |
68 | editButton->setEnabled(false); |
69 | removeButton = new QPushButton(tr(s: "&Remove" )); |
70 | removeButton->setEnabled(false); |
71 | findButton = new QPushButton(tr(s: "&Find" )); |
72 | findButton->setEnabled(false); |
73 | submitButton = new QPushButton(tr(s: "&Submit" )); |
74 | submitButton->hide(); |
75 | cancelButton = new QPushButton(tr(s: "&Cancel" )); |
76 | cancelButton->hide(); |
77 | |
78 | nextButton = new QPushButton(tr(s: "&Next" )); |
79 | nextButton->setEnabled(false); |
80 | previousButton = new QPushButton(tr(s: "&Previous" )); |
81 | previousButton->setEnabled(false); |
82 | |
83 | loadButton = new QPushButton(tr(s: "&Load..." )); |
84 | loadButton->setToolTip(tr(s: "Load contacts from a file" )); |
85 | saveButton = new QPushButton(tr(s: "&Save..." )); |
86 | saveButton->setToolTip(tr(s: "Save contacts to a file" )); |
87 | saveButton->setEnabled(false); |
88 | |
89 | exportButton = new QPushButton(tr(s: "E&xport" )); |
90 | exportButton->setToolTip(tr(s: "Export as vCard" )); |
91 | exportButton->setEnabled(false); |
92 | |
93 | dialog = new FindDialog(this); |
94 | |
95 | connect(sender: addButton, signal: &QPushButton::clicked, |
96 | receiver: this, slot: &AddressBook::addContact); |
97 | connect(sender: submitButton, signal: &QPushButton::clicked, |
98 | receiver: this, slot: &AddressBook::submitContact); |
99 | connect(sender: editButton, signal: &QPushButton::clicked, |
100 | receiver: this, slot: &AddressBook::editContact); |
101 | connect(sender: removeButton, signal: &QPushButton::clicked, |
102 | receiver: this, slot: &AddressBook::removeContact); |
103 | connect(sender: cancelButton, signal: &QPushButton::clicked, |
104 | receiver: this, slot: &AddressBook::cancel); |
105 | connect(sender: findButton, signal: &QPushButton::clicked, |
106 | receiver: this, slot: &AddressBook::findContact); |
107 | connect(sender: nextButton, signal: &QPushButton::clicked, |
108 | receiver: this, slot: &AddressBook::next); |
109 | connect(sender: previousButton, signal: &QPushButton::clicked, |
110 | receiver: this, slot: &AddressBook::previous); |
111 | connect(sender: loadButton, signal: &QPushButton::clicked, |
112 | receiver: this, slot: &AddressBook::loadFromFile); |
113 | connect(sender: saveButton, signal: &QPushButton::clicked, |
114 | receiver: this, slot: &AddressBook::saveToFile); |
115 | connect(sender: exportButton, signal: &QPushButton::clicked, |
116 | receiver: this, slot: &AddressBook::exportAsVCard); |
117 | |
118 | QVBoxLayout *buttonLayout1 = new QVBoxLayout; |
119 | buttonLayout1->addWidget(addButton); |
120 | buttonLayout1->addWidget(editButton); |
121 | buttonLayout1->addWidget(removeButton); |
122 | buttonLayout1->addWidget(findButton); |
123 | buttonLayout1->addWidget(submitButton); |
124 | buttonLayout1->addWidget(cancelButton); |
125 | buttonLayout1->addWidget(loadButton); |
126 | buttonLayout1->addWidget(saveButton); |
127 | buttonLayout1->addWidget(exportButton); |
128 | buttonLayout1->addStretch(); |
129 | |
130 | QHBoxLayout *buttonLayout2 = new QHBoxLayout; |
131 | buttonLayout2->addWidget(previousButton); |
132 | buttonLayout2->addWidget(nextButton); |
133 | |
134 | QGridLayout *mainLayout = new QGridLayout; |
135 | mainLayout->addWidget(nameLabel, row: 0, column: 0); |
136 | mainLayout->addWidget(nameLine, row: 0, column: 1); |
137 | mainLayout->addWidget(addressLabel, row: 1, column: 0, Qt::AlignTop); |
138 | mainLayout->addWidget(addressText, row: 1, column: 1); |
139 | mainLayout->addLayout(buttonLayout1, row: 1, column: 2); |
140 | mainLayout->addLayout(buttonLayout2, row: 2, column: 1); |
141 | |
142 | setLayout(mainLayout); |
143 | setWindowTitle(tr(s: "Simple Address Book" )); |
144 | } |
145 | |
146 | void AddressBook::addContact() |
147 | { |
148 | oldName = nameLine->text(); |
149 | oldAddress = addressText->toPlainText(); |
150 | |
151 | nameLine->clear(); |
152 | addressText->clear(); |
153 | |
154 | updateInterface(mode: AddingMode); |
155 | } |
156 | |
157 | void AddressBook::editContact() |
158 | { |
159 | oldName = nameLine->text(); |
160 | oldAddress = addressText->toPlainText(); |
161 | |
162 | updateInterface(mode: EditingMode); |
163 | } |
164 | |
165 | void AddressBook::submitContact() |
166 | { |
167 | QString name = nameLine->text(); |
168 | QString address = addressText->toPlainText(); |
169 | |
170 | if (name.isEmpty() || address.isEmpty()) { |
171 | QMessageBox::information(parent: this, title: tr(s: "Empty Field" ), |
172 | text: tr(s: "Please enter a name and address." )); |
173 | return; |
174 | } |
175 | |
176 | if (currentMode == AddingMode) { |
177 | |
178 | if (!contacts.contains(akey: name)) { |
179 | contacts.insert(akey: name, avalue: address); |
180 | QMessageBox::information(parent: this, title: tr(s: "Add Successful" ), |
181 | text: tr(s: "\"%1\" has been added to your address book." ).arg(a: name)); |
182 | } else { |
183 | QMessageBox::information(parent: this, title: tr(s: "Add Unsuccessful" ), |
184 | text: tr(s: "Sorry, \"%1\" is already in your address book." ).arg(a: name)); |
185 | } |
186 | } else if (currentMode == EditingMode) { |
187 | |
188 | if (oldName != name) { |
189 | if (!contacts.contains(akey: name)) { |
190 | QMessageBox::information(parent: this, title: tr(s: "Edit Successful" ), |
191 | text: tr(s: "\"%1\" has been edited in your address book." ).arg(a: oldName)); |
192 | contacts.remove(akey: oldName); |
193 | contacts.insert(akey: name, avalue: address); |
194 | } else { |
195 | QMessageBox::information(parent: this, title: tr(s: "Edit Unsuccessful" ), |
196 | text: tr(s: "Sorry, \"%1\" is already in your address book." ).arg(a: name)); |
197 | } |
198 | } else if (oldAddress != address) { |
199 | QMessageBox::information(parent: this, title: tr(s: "Edit Successful" ), |
200 | text: tr(s: "\"%1\" has been edited in your address book." ).arg(a: name)); |
201 | contacts[name] = address; |
202 | } |
203 | } |
204 | |
205 | updateInterface(mode: NavigationMode); |
206 | } |
207 | |
208 | void AddressBook::cancel() |
209 | { |
210 | nameLine->setText(oldName); |
211 | addressText->setText(oldAddress); |
212 | updateInterface(mode: NavigationMode); |
213 | } |
214 | |
215 | void AddressBook::removeContact() |
216 | { |
217 | QString name = nameLine->text(); |
218 | QString address = addressText->toPlainText(); |
219 | |
220 | if (contacts.contains(akey: name)) { |
221 | |
222 | int button = QMessageBox::question(parent: this, |
223 | title: tr(s: "Confirm Remove" ), |
224 | text: tr(s: "Are you sure you want to remove \"%1\"?" ).arg(a: name), |
225 | buttons: QMessageBox::Yes | QMessageBox::No); |
226 | |
227 | if (button == QMessageBox::Yes) { |
228 | |
229 | previous(); |
230 | contacts.remove(akey: name); |
231 | |
232 | QMessageBox::information(parent: this, title: tr(s: "Remove Successful" ), |
233 | text: tr(s: "\"%1\" has been removed from your address book." ).arg(a: name)); |
234 | } |
235 | } |
236 | |
237 | updateInterface(mode: NavigationMode); |
238 | } |
239 | |
240 | void AddressBook::next() |
241 | { |
242 | QString name = nameLine->text(); |
243 | QMap<QString, QString>::iterator i = contacts.find(akey: name); |
244 | |
245 | if (i != contacts.end()) |
246 | i++; |
247 | |
248 | if (i == contacts.end()) |
249 | i = contacts.begin(); |
250 | |
251 | nameLine->setText(i.key()); |
252 | addressText->setText(i.value()); |
253 | } |
254 | |
255 | void AddressBook::previous() |
256 | { |
257 | QString name = nameLine->text(); |
258 | QMap<QString, QString>::iterator i = contacts.find(akey: name); |
259 | |
260 | if (i == contacts.end()) { |
261 | nameLine->clear(); |
262 | addressText->clear(); |
263 | return; |
264 | } |
265 | |
266 | if (i == contacts.begin()) |
267 | i = contacts.end(); |
268 | |
269 | i--; |
270 | nameLine->setText(i.key()); |
271 | addressText->setText(i.value()); |
272 | } |
273 | |
274 | void AddressBook::findContact() |
275 | { |
276 | dialog->show(); |
277 | |
278 | if (dialog->exec() == 1) { |
279 | QString contactName = dialog->getFindText(); |
280 | |
281 | if (contacts.contains(akey: contactName)) { |
282 | nameLine->setText(contactName); |
283 | addressText->setText(contacts.value(akey: contactName)); |
284 | } else { |
285 | QMessageBox::information(parent: this, title: tr(s: "Contact Not Found" ), |
286 | text: tr(s: "Sorry, \"%1\" is not in your address book." ).arg(a: contactName)); |
287 | return; |
288 | } |
289 | } |
290 | |
291 | updateInterface(mode: NavigationMode); |
292 | } |
293 | void AddressBook::updateInterface(Mode mode) |
294 | { |
295 | currentMode = mode; |
296 | |
297 | switch (currentMode) { |
298 | |
299 | case AddingMode: |
300 | case EditingMode: |
301 | |
302 | nameLine->setReadOnly(false); |
303 | nameLine->setFocus(Qt::OtherFocusReason); |
304 | addressText->setReadOnly(false); |
305 | |
306 | addButton->setEnabled(false); |
307 | editButton->setEnabled(false); |
308 | removeButton->setEnabled(false); |
309 | |
310 | nextButton->setEnabled(false); |
311 | previousButton->setEnabled(false); |
312 | |
313 | submitButton->show(); |
314 | cancelButton->show(); |
315 | |
316 | loadButton->setEnabled(false); |
317 | saveButton->setEnabled(false); |
318 | exportButton->setEnabled(false); |
319 | break; |
320 | |
321 | case NavigationMode: |
322 | |
323 | if (contacts.isEmpty()) { |
324 | nameLine->clear(); |
325 | addressText->clear(); |
326 | } |
327 | |
328 | nameLine->setReadOnly(true); |
329 | addressText->setReadOnly(true); |
330 | addButton->setEnabled(true); |
331 | |
332 | int number = contacts.size(); |
333 | editButton->setEnabled(number >= 1); |
334 | removeButton->setEnabled(number >= 1); |
335 | findButton->setEnabled(number > 2); |
336 | nextButton->setEnabled(number > 1); |
337 | previousButton->setEnabled(number > 1); |
338 | |
339 | submitButton->hide(); |
340 | cancelButton->hide(); |
341 | |
342 | exportButton->setEnabled(number >= 1); |
343 | |
344 | loadButton->setEnabled(true); |
345 | saveButton->setEnabled(number >= 1); |
346 | break; |
347 | } |
348 | } |
349 | |
350 | void AddressBook::saveToFile() |
351 | { |
352 | QString fileName = QFileDialog::getSaveFileName(parent: this, |
353 | caption: tr(s: "Save Address Book" ), dir: "" , |
354 | filter: tr(s: "Address Book (*.abk);;All Files (*)" )); |
355 | |
356 | if (fileName.isEmpty()) |
357 | return; |
358 | else { |
359 | QFile file(fileName); |
360 | |
361 | if (!file.open(flags: QIODevice::WriteOnly)) { |
362 | QMessageBox::information(parent: this, title: tr(s: "Unable to open file" ), |
363 | text: file.errorString()); |
364 | return; |
365 | } |
366 | |
367 | QDataStream out(&file); |
368 | out.setVersion(QDataStream::Qt_4_3); |
369 | out << contacts; |
370 | } |
371 | |
372 | updateInterface(mode: NavigationMode); |
373 | } |
374 | |
375 | void AddressBook::loadFromFile() |
376 | { |
377 | QString fileName = QFileDialog::getOpenFileName(parent: this, |
378 | caption: tr(s: "Open Address Book" ), dir: "" , |
379 | filter: tr(s: "Address Book (*.abk);;All Files (*)" )); |
380 | |
381 | if (fileName.isEmpty()) |
382 | return; |
383 | else { |
384 | QFile file(fileName); |
385 | |
386 | if (!file.open(flags: QIODevice::ReadOnly)) { |
387 | QMessageBox::information(parent: this, title: tr(s: "Unable to open file" ), |
388 | text: file.errorString()); |
389 | return; |
390 | } |
391 | |
392 | QDataStream in(&file); |
393 | in.setVersion(QDataStream::Qt_4_3); |
394 | in >> contacts; |
395 | |
396 | QMap<QString, QString>::iterator i = contacts.begin(); |
397 | nameLine->setText(i.key()); |
398 | addressText->setText(i.value()); |
399 | } |
400 | |
401 | updateInterface(mode: NavigationMode); |
402 | } |
403 | |
404 | //! [export function part1] |
405 | void AddressBook::exportAsVCard() |
406 | { |
407 | QString name = nameLine->text(); |
408 | QString address = addressText->toPlainText(); |
409 | QString firstName; |
410 | QString lastName; |
411 | QStringList nameList; |
412 | |
413 | int index = name.indexOf(s: " " ); |
414 | |
415 | if (index != -1) { |
416 | nameList = name.split(sep: QRegularExpression("\\s+" ), behavior: Qt::SkipEmptyParts); |
417 | firstName = nameList.first(); |
418 | lastName = nameList.last(); |
419 | } else { |
420 | firstName = name; |
421 | lastName = "" ; |
422 | } |
423 | |
424 | QString fileName = QFileDialog::getSaveFileName(parent: this, |
425 | caption: tr(s: "Export Contact" ), dir: "" , |
426 | filter: tr(s: "vCard Files (*.vcf);;All Files (*)" )); |
427 | |
428 | if (fileName.isEmpty()) |
429 | return; |
430 | |
431 | QFile file(fileName); |
432 | //! [export function part1] |
433 | |
434 | //! [export function part2] |
435 | if (!file.open(flags: QIODevice::WriteOnly)) { |
436 | QMessageBox::information(parent: this, title: tr(s: "Unable to open file" ), |
437 | text: file.errorString()); |
438 | return; |
439 | } |
440 | |
441 | QTextStream out(&file); |
442 | //! [export function part2] |
443 | |
444 | //! [export function part3] |
445 | out << "BEGIN:VCARD" << '\n'; |
446 | out << "VERSION:2.1" << '\n'; |
447 | out << "N:" << lastName << ';' << firstName << '\n'; |
448 | |
449 | if (!nameList.isEmpty()) |
450 | out << "FN:" << nameList.join(sep: ' ') << '\n'; |
451 | else |
452 | out << "FN:" << firstName << '\n'; |
453 | //! [export function part3] |
454 | |
455 | //! [export function part4] |
456 | address.replace(before: ";" , after: "\\;" , cs: Qt::CaseInsensitive); |
457 | address.replace(c: '\n', after: ";" , cs: Qt::CaseInsensitive); |
458 | address.replace(before: "," , after: " " , cs: Qt::CaseInsensitive); |
459 | |
460 | out << "ADR;HOME:;" << address << '\n'; |
461 | out << "END:VCARD" << '\n'; |
462 | |
463 | QMessageBox::information(parent: this, title: tr(s: "Export Successful" ), |
464 | text: tr(s: "\"%1\" has been exported as a vCard." ).arg(a: name)); |
465 | } |
466 | //! [export function part4] |
467 | |