blob: f4793a86494374ec507c95ed37e2375cc0a7674a [file] [log] [blame]
Edric Milaret43f3c1e2015-07-16 17:52:47 -04001/***************************************************************************
Anthony LĂ©onard2fde81d2017-04-17 10:06:55 -04002 * Copyright (C) 2015-2017 by Savoir-faire Linux *
Edric Milaret43f3c1e2015-07-16 17:52:47 -04003 * 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
Edricd1c35b42016-05-19 10:04:50 -040022#include <QMouseEvent>
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 Milareta3e47282015-10-23 15:20:30 -040040 contactProxyModel_ = new OnlyPersonProxyModel(&PersonModel::instance());
Edric Milaretbe0aa5a2015-07-31 15:08:39 -040041 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 */
Edric Milareta3e47282015-10-23 15:20:30 -040066 CategorizedContactModel::instance().setUnreachableHidden(false);
Edric Milaretbe0aa5a2015-07-31 15:08:39 -040067
68 auto idx = ui->contactView->currentIndex();
69
70 //There is only one collection on Windows
Edric Milareta3e47282015-10-23 15:20:30 -040071 auto personCollection = PersonModel::instance().collections().at(0);
Edric Milaretbe0aa5a2015-07-31 15:08:39 -040072
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());
Edric Milareta3e47282015-10-23 15:20:30 -040080 PersonModel::instance().addNewPerson(newPerson, personCollection);
Edric Milaretbe0aa5a2015-07-31 15:08:39 -040081 } 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 }
Edric Milareta3e47282015-10-23 15:20:30 -040088 CategorizedContactModel::instance().setUnreachableHidden(true);
Edric Milaretbe0aa5a2015-07-31 15:08:39 -040089
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}
Edricd1c35b42016-05-19 10:04:50 -0400110
111void
112ContactPicker::mousePressEvent(QMouseEvent *event)
113{
114 mpos_ = event->pos();
115 if (not rect().contains(mpos_))
116 QDialog::reject();
117}
118
119void ContactPicker::mouseMoveEvent(QMouseEvent *event)
120{
121 if (event->buttons() & Qt::LeftButton) {
122 QPoint diff = event->pos() - mpos_;
123 QPoint newpos = this->pos() + diff;
124
125 this->move(newpos);
126 }
127}