blob: 2d753890e4d4d1ee419c397f9c2ac70a8ab1e9e5 [file] [log] [blame]
Edric Milaret43f3c1e2015-07-16 17:52:47 -04001/***************************************************************************
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 Milaret43f3c1e2015-07-16 17:52:47 -040022
Edric Milaretbe0aa5a2015-07-31 15:08:39 -040023
24#include "categorizedcontactmodel.h"
25#include "personmodel.h"
26
27#include "utils.h"
28
29ContactPicker::ContactPicker(ContactMethod *number, QWidget *parent) :
Edric Milaret43f3c1e2015-07-16 17:52:47 -040030 QDialog(parent),
31 ui(new Ui::ContactPicker),
Edric Milaretbe0aa5a2015-07-31 15:08:39 -040032 personSelected_(nullptr),
33 number_(number)
Edric Milaret43f3c1e2015-07-16 17:52:47 -040034{
35 ui->setupUi(this);
36
37 this->setWindowFlags(Qt::CustomizeWindowHint);
Edric Milaretbe0aa5a2015-07-31 15:08:39 -040038 this->setWindowFlags(Qt::FramelessWindowHint | Qt::Popup);
Edric Milaret43f3c1e2015-07-16 17:52:47 -040039
Edric Milaretbe0aa5a2015-07-31 15:08:39 -040040 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 Milaret43f3c1e2015-07-16 17:52:47 -040047}
48
49ContactPicker::~ContactPicker()
50{
51 delete ui;
52}
53
54void
55ContactPicker::on_contactView_doubleClicked(const QModelIndex &index)
56{
Edric Milaretbe0aa5a2015-07-31 15:08:39 -040057 Q_UNUSED(index)
Edric Milaret43f3c1e2015-07-16 17:52:47 -040058 this->accept();
59}
60
Edric Milaretbe0aa5a2015-07-31 15:08:39 -040061void
62ContactPicker::accept()
Edric Milaret43f3c1e2015-07-16 17:52:47 -040063{
Edric Milaretbe0aa5a2015-07-31 15:08:39 -040064 /* 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 Milaret43f3c1e2015-07-16 17:52:47 -040091}
92
93void
Edric Milaretbe0aa5a2015-07-31 15:08:39 -040094ContactPicker::on_okButton_clicked()
Edric Milaret43f3c1e2015-07-16 17:52:47 -040095{
Edric Milaretbe0aa5a2015-07-31 15:08:39 -040096 this->accept();
97}
98
99void
100ContactPicker::on_createNewButton_clicked()
101{
102 ui->stackedWidget->setCurrentIndex(1);
103}
104
105void
106ContactPicker::on_searchBar_textChanged(const QString &arg1)
107{
108 contactProxyModel_->setFilterRegExp(QRegExp(arg1, Qt::CaseInsensitive, QRegExp::FixedString));
Edric Milaret43f3c1e2015-07-16 17:52:47 -0400109}