contactpicker.cpp 3.68 KB
Newer Older
1
/***************************************************************************
2
 * Copyright (C) 2015-2016 by Savoir-faire Linux                                *
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * Author: Edric Ladent Milaret <edric.ladent-milaret@savoirfairelinux.com>*
 *                                                                         *
 * This program is free software; you can redistribute it and/or modify    *
 * it under the terms of the GNU General Public License as published by    *
 * the Free Software Foundation; either version 3 of the License, or       *
 * (at your option) any later version.                                     *
 *                                                                         *
 * This program is distributed in the hope that it will be useful,         *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
 * GNU General Public License for more details.                            *
 *                                                                         *
 * You should have received a copy of the GNU General Public License       *
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.   *
 **************************************************************************/

#include "contactpicker.h"
#include "ui_contactpicker.h"

22 23


24
#include "categorizedcontactmodel.h"
25 26 27
#include "personmodel.h"

#include "utils.h"
28

29
ContactPicker::ContactPicker(ContactMethod *number, QWidget *parent) :
30 31
    QDialog(parent),
    ui(new Ui::ContactPicker),
32 33
    personSelected_(nullptr),
    number_(number)
34 35 36 37
{
    ui->setupUi(this);

    this->setWindowFlags(Qt::CustomizeWindowHint);
38
    this->setWindowFlags(Qt::FramelessWindowHint | Qt::Popup);
39

Edric Milaret's avatar
Edric Milaret committed
40
    contactProxyModel_ = new OnlyPersonProxyModel(&PersonModel::instance());
41 42 43 44 45 46
    contactProxyModel_->setSortRole(static_cast<int>(Qt::DisplayRole));
    contactProxyModel_->sort(0,Qt::AscendingOrder);
    contactProxyModel_->setFilterRole(Qt::DisplayRole);

    ui->contactView->setModel(contactProxyModel_);
    ui->numberLineEdit->setText(number->uri());
47 48 49 50 51 52 53 54 55 56
}

ContactPicker::~ContactPicker()
{
    delete ui;
}

void
ContactPicker::on_contactView_doubleClicked(const QModelIndex &index)
{
57 58 59 60 61 62 63 64 65
    Q_UNUSED(index)
    this->accept();
}

void
ContactPicker::accept()
{
    /* Force LRC to update contact model as adding a number
    to a contact without one didn't render him reachable */
Edric Milaret's avatar
Edric Milaret committed
66
    CategorizedContactModel::instance().setUnreachableHidden(false);
67 68 69 70

    auto idx = ui->contactView->currentIndex();

    //There is only one collection on Windows
Edric Milaret's avatar
Edric Milaret committed
71
    auto personCollection = PersonModel::instance().collections().at(0);
72 73 74 75 76 77 78 79

    if (not ui->nameLineEdit->text().isEmpty()) {
        auto *newPerson = new Person();
        newPerson->setFormattedName(ui->nameLineEdit->text());
        Person::ContactMethods cM;
        cM.append(number_);
        newPerson->setContactMethods(cM);
        newPerson->setUid(Utils::GenGUID().toLocal8Bit());
Edric Milaret's avatar
Edric Milaret committed
80
        PersonModel::instance().addNewPerson(newPerson, personCollection);
81 82 83 84 85 86 87
    } else if (idx.isValid()) {
        auto p = idx.data(static_cast<int>(Person::Role::Object)).value<Person*>();
        Person::ContactMethods cM (p->phoneNumbers());
        cM.append(number_);
        p->setContactMethods(cM);
        p->save();
    }
Edric Milaret's avatar
Edric Milaret committed
88
    CategorizedContactModel::instance().setUnreachableHidden(true);
89 90 91 92 93 94 95

    QDialog::accept();
}

void
ContactPicker::on_okButton_clicked()
{
96 97 98
    this->accept();
}

99 100
void
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));
109
}