Edric Milaret | 43f3c1e | 2015-07-16 17:52:47 -0400 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * Copyright (C) 2015 by Savoir-faire Linux * |
| 3 | * Author: Edric Ladent Milaret <edric.ladent-milaret@savoirfairelinux.com>* |
| 4 | * * |
| 5 | * This program is free software; you can redistribute it and/or modify * |
| 6 | * it under the terms of the GNU General Public License as published by * |
| 7 | * the Free Software Foundation; either version 3 of the License, or * |
| 8 | * (at your option) any later version. * |
| 9 | * * |
| 10 | * This program is distributed in the hope that it will be useful, * |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 13 | * GNU General Public License for more details. * |
| 14 | * * |
| 15 | * You should have received a copy of the GNU General Public License * |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. * |
| 17 | **************************************************************************/ |
| 18 | |
| 19 | #include "contactpicker.h" |
| 20 | #include "ui_contactpicker.h" |
| 21 | |
Edric Milaret | 43f3c1e | 2015-07-16 17:52:47 -0400 | [diff] [blame] | 22 | |
Edric Milaret | be0aa5a | 2015-07-31 15:08:39 -0400 | [diff] [blame] | 23 | |
| 24 | #include "categorizedcontactmodel.h" |
| 25 | #include "personmodel.h" |
| 26 | |
| 27 | #include "utils.h" |
| 28 | |
| 29 | ContactPicker::ContactPicker(ContactMethod *number, QWidget *parent) : |
Edric Milaret | 43f3c1e | 2015-07-16 17:52:47 -0400 | [diff] [blame] | 30 | QDialog(parent), |
| 31 | ui(new Ui::ContactPicker), |
Edric Milaret | be0aa5a | 2015-07-31 15:08:39 -0400 | [diff] [blame] | 32 | personSelected_(nullptr), |
| 33 | number_(number) |
Edric Milaret | 43f3c1e | 2015-07-16 17:52:47 -0400 | [diff] [blame] | 34 | { |
| 35 | ui->setupUi(this); |
| 36 | |
| 37 | this->setWindowFlags(Qt::CustomizeWindowHint); |
Edric Milaret | be0aa5a | 2015-07-31 15:08:39 -0400 | [diff] [blame] | 38 | this->setWindowFlags(Qt::FramelessWindowHint | Qt::Popup); |
Edric Milaret | 43f3c1e | 2015-07-16 17:52:47 -0400 | [diff] [blame] | 39 | |
Edric Milaret | be0aa5a | 2015-07-31 15:08:39 -0400 | [diff] [blame] | 40 | contactProxyModel_ = new OnlyPersonProxyModel(PersonModel::instance()); |
| 41 | contactProxyModel_->setSortRole(static_cast<int>(Qt::DisplayRole)); |
| 42 | contactProxyModel_->sort(0,Qt::AscendingOrder); |
| 43 | contactProxyModel_->setFilterRole(Qt::DisplayRole); |
| 44 | |
| 45 | ui->contactView->setModel(contactProxyModel_); |
| 46 | ui->numberLineEdit->setText(number->uri()); |
Edric Milaret | 43f3c1e | 2015-07-16 17:52:47 -0400 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | ContactPicker::~ContactPicker() |
| 50 | { |
| 51 | delete ui; |
| 52 | } |
| 53 | |
| 54 | void |
| 55 | ContactPicker::on_contactView_doubleClicked(const QModelIndex &index) |
| 56 | { |
Edric Milaret | be0aa5a | 2015-07-31 15:08:39 -0400 | [diff] [blame] | 57 | Q_UNUSED(index) |
Edric Milaret | 43f3c1e | 2015-07-16 17:52:47 -0400 | [diff] [blame] | 58 | this->accept(); |
| 59 | } |
| 60 | |
Edric Milaret | be0aa5a | 2015-07-31 15:08:39 -0400 | [diff] [blame] | 61 | void |
| 62 | ContactPicker::accept() |
Edric Milaret | 43f3c1e | 2015-07-16 17:52:47 -0400 | [diff] [blame] | 63 | { |
Edric Milaret | be0aa5a | 2015-07-31 15:08:39 -0400 | [diff] [blame] | 64 | /* Force LRC to update contact model as adding a number |
| 65 | to a contact without one didn't render him reachable */ |
| 66 | CategorizedContactModel::instance()->setUnreachableHidden(false); |
| 67 | |
| 68 | auto idx = ui->contactView->currentIndex(); |
| 69 | |
| 70 | //There is only one collection on Windows |
| 71 | auto personCollection = PersonModel::instance()->collections().at(0); |
| 72 | |
| 73 | if (not ui->nameLineEdit->text().isEmpty()) { |
| 74 | auto *newPerson = new Person(); |
| 75 | newPerson->setFormattedName(ui->nameLineEdit->text()); |
| 76 | Person::ContactMethods cM; |
| 77 | cM.append(number_); |
| 78 | newPerson->setContactMethods(cM); |
| 79 | newPerson->setUid(Utils::GenGUID().toLocal8Bit()); |
| 80 | PersonModel::instance()->addNewPerson(newPerson, personCollection); |
| 81 | } else if (idx.isValid()) { |
| 82 | auto p = idx.data(static_cast<int>(Person::Role::Object)).value<Person*>(); |
| 83 | Person::ContactMethods cM (p->phoneNumbers()); |
| 84 | cM.append(number_); |
| 85 | p->setContactMethods(cM); |
| 86 | p->save(); |
| 87 | } |
| 88 | CategorizedContactModel::instance()->setUnreachableHidden(true); |
| 89 | |
| 90 | QDialog::accept(); |
Edric Milaret | 43f3c1e | 2015-07-16 17:52:47 -0400 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | void |
Edric Milaret | be0aa5a | 2015-07-31 15:08:39 -0400 | [diff] [blame] | 94 | ContactPicker::on_okButton_clicked() |
Edric Milaret | 43f3c1e | 2015-07-16 17:52:47 -0400 | [diff] [blame] | 95 | { |
Edric Milaret | be0aa5a | 2015-07-31 15:08:39 -0400 | [diff] [blame] | 96 | this->accept(); |
| 97 | } |
| 98 | |
| 99 | void |
| 100 | ContactPicker::on_createNewButton_clicked() |
| 101 | { |
| 102 | ui->stackedWidget->setCurrentIndex(1); |
| 103 | } |
| 104 | |
| 105 | void |
| 106 | ContactPicker::on_searchBar_textChanged(const QString &arg1) |
| 107 | { |
| 108 | contactProxyModel_->setFilterRegExp(QRegExp(arg1, Qt::CaseInsensitive, QRegExp::FixedString)); |
Edric Milaret | 43f3c1e | 2015-07-16 17:52:47 -0400 | [diff] [blame] | 109 | } |