source: SMSSender/src/ui/vceditcontact/vceditcontact.cpp @ 217:1ed3ec64fc1f

Last change on this file since 217:1ed3ec64fc1f was 217:1ed3ec64fc1f, checked in by Sämy Zehnder <saemy.zehnder@…>, 7 years ago
  • Removed all Priv stuff. Using Qts parent mechanism for memory management now.
File size: 9.5 KB
Line 
1/*
2 smssender - A frontend for fast and easy SMS sending over different gateways.
3 Copyright (C) 2007-2012, gorrión. See http://smssender.gorrion.ch
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#include "vceditcontact.h"
19
20#include <QDebug>
21#include <QFileDialog>
22#include <QImageReader>
23#include <QInputDialog>
24#include <QMenu>
25#include <QMessageBox>
26#include <QSortFilterProxyModel>
27#include <QStringList>
28
29#include <snumber.h>
30
31#include "business/bccontactmanager.h"
32#include "business/bcgroupmanager.h"
33
34VCEditContact::VCEditContact(QWidget* parent, const SContact& contact, bool ignoreAlreadyExisting)
35    : QDialog(parent)
36    , contact_(contact)
37    , ignoreAlreadyExisting_(ignoreAlreadyExisting)
38{
39        ui.setupUi(this);
40
41        defaultIcon_ = ui.btnIcon->icon();
42
43        ui.edtName->setText(contact.name());
44        if (!contact.number().isValid()) {
45            ui.edtCountryCode->setText("+41");
46            ui.edtNumber->setText("");
47        } else {
48        ui.edtCountryCode->setText(contact.number().toString("'+'CC"));
49        ui.edtNumber->setText(contact.number().toString("aaa' 'u"));
50        }
51
52        // Load aliases
53        foreach (QString alias, contact.aliases()) {
54            addAliasItem(alias);
55        }
56        QSortFilterProxyModel* model = new QSortFilterProxyModel(this);
57        model->setSourceModel(ui.lstAliases->model());
58        model->setDynamicSortFilter(true);
59        model->setSortCaseSensitivity(Qt::CaseInsensitive);
60        model->sort(0, Qt::AscendingOrder);
61
62    setIcon(contact.image());
63
64    on_lstAliases_itemSelectionChanged();
65}
66
67
68// Icon
69
70void VCEditContact::addBtnIconMenu(){
71    QMenu* menu = new QMenu(ui.btnIcon);
72
73    QAction* action;
74    QIcon icon;
75
76    icon = QIcon();
77    icon.addPixmap(QPixmap(QString::fromUtf8(":/ico/replace.png")), QIcon::Normal, QIcon::Off);
78    action = new QAction(icon, tr("Change icon"), this);
79    menu->addAction(action);
80    connect(action, SIGNAL(activated()), this, SLOT(on_actionChangeIcon_activated()));
81
82    icon = QIcon();
83    icon.addPixmap(QPixmap(QString::fromUtf8(":/ico/remove.png")), QIcon::Normal, QIcon::Off);
84    action = new QAction(icon, tr("Remove icon"), this);
85    menu->addAction(action);
86    connect(action, SIGNAL(activated()), this, SLOT(on_actionRemoveIcon_activated()));
87
88    ui.btnIcon->setMenu(menu);
89}
90
91void VCEditContact::setIconFromFilename(const QString& filename){
92    if ((filename != "") && (QFileInfo(filename).exists())){
93        QImage img = QImage(filename);
94        setIcon(img);
95    }else{
96        setIcon(QImage());
97    }
98}
99void VCEditContact::setIcon(const QImage& image){
100    if (!image.isNull()){
101        ui.btnIcon->setIcon(QPixmap::fromImage(image));
102
103        contactImage_ = image;
104        addBtnIconMenu();
105    } else {
106        ui.btnIcon->setIcon(defaultIcon_);
107
108        contactImage_ = QImage();
109        removeBtnIconMenu();
110    }
111}
112
113void VCEditContact::removeBtnIconMenu(){
114    ui.btnIcon->setMenu(NULL);
115}
116
117void VCEditContact::on_btnIcon_clicked(){
118    on_actionChangeIcon_activated();
119}
120
121void VCEditContact::on_actionChangeIcon_activated(){
122    QList<QByteArray> formatList = QImageReader::supportedImageFormats();
123    QString formats = tr("Images") + " (";
124    for (QList<QByteArray>::iterator it = formatList.begin(); it != formatList.end(); ++it){
125        formats += "*." + *it + " ";
126    }
127    formats.chop(1);
128    formats += ")";
129
130    QString filename = QFileDialog::getOpenFileName(this, tr("Open File"), "", formats);
131    if ((filename != "") && (QFileInfo(filename).exists())){
132        setIconFromFilename(filename);
133    }
134}
135
136void VCEditContact::on_actionRemoveIcon_activated(){
137    setIcon(QImage());
138}
139
140
141// Aliases
142
143QListWidgetItem* VCEditContact::addAliasItem(const QString& alias) {
144    QListWidgetItem* item = new QListWidgetItem(alias);
145    item->setFlags(item->flags() | Qt::ItemIsEditable);
146    ui.lstAliases->addItem(item);
147    return item;
148}
149
150void VCEditContact::on_lstAliases_itemSelectionChanged(){
151    ui.btnRemoveAlias->setEnabled(ui.lstAliases->selectedItems().count() > 0);
152    ui.btnEditAlias->setEnabled(ui.lstAliases->selectedItems().count() > 0);
153}
154
155void VCEditContact::on_btnAddAlias_clicked(){
156    QList<QListWidgetItem*> emptyItems = ui.lstAliases->findItems("", Qt::MatchFixedString);
157    if (emptyItems.isEmpty()) {
158        emptyItems.append(addAliasItem(""));
159    }
160    ui.lstAliases->setFocus();
161    emptyItems.first()->setSelected(true);
162    on_btnEditAlias_clicked();
163}
164void VCEditContact::on_btnEditAlias_clicked() {
165    QList<QListWidgetItem*> selectedItems = ui.lstAliases->selectedItems();
166    if (!selectedItems.isEmpty()) {
167        ui.lstAliases->setFocus();
168        ui.lstAliases->editItem(selectedItems.first());
169    }
170}
171
172
173void VCEditContact::on_lstAliases_itemChanged(QListWidgetItem* item) {
174    QString alias = item->text().trimmed();
175
176    QList<QListWidgetItem*> items = ui.lstAliases->findItems(alias, Qt::MatchFixedString);
177    items.removeAll(item);
178    if (items.count() > 0){ // Is it already in the list?
179        QMessageBox::information(this, tr("SMSSender"),
180                                 tr("This contact is already known under this alias."),
181                                 QMessageBox::Ok | QMessageBox::Default | QMessageBox::Escape);
182        ui.lstAliases->editItem(item);
183        return;
184    }
185
186    ui.lstAliases->takeItem(ui.lstAliases->row(item));
187    foreach (QString newAlias, alias.split(",", QString::SkipEmptyParts)) {
188        newAlias = newAlias.trimmed();
189
190        if (ui.lstAliases->findItems(newAlias, Qt::MatchFixedString).count() == 0){ // Is it already in the list?
191            addAliasItem(newAlias);
192        }
193    }
194}
195
196void VCEditContact::on_btnRemoveAlias_clicked(){
197    if (ui.lstAliases->selectedItems().count() == 0){
198        return;
199    }
200
201    /*if ((ui.lstAliases->selectedItems().first()->text().trimmed() == "") ||
202        (QMessageBox::Yes == QMessageBox::warning(this, tr("SMSSender"),
203                                                  tr("Do you really want to remove this alias?"),
204                                                  QMessageBox::Yes,
205                                                  QMessageBox::No | QMessageBox::Default | QMessageBox::Escape))){*/
206        // Remove it from the list
207        ui.lstAliases->takeItem(ui.lstAliases->row(ui.lstAliases->selectedItems().first()));
208    /*}*/
209}
210
211
212bool VCEditContact::validate(){
213    ui.edtName->setText(ui.edtName->text().trimmed());
214    ui.edtNumber->setText(ui.edtNumber->text().trimmed());
215    ui.edtCountryCode->setText(ui.edtCountryCode->text().trimmed());
216
217    QString errorMessages = "";
218
219
220    // Validate the name
221    if (ui.edtName->text().isEmpty()) {
222        errorMessages += tr("Please enter a name for this contact.") + "\n";
223    }
224
225    QSet<SContact> contactList = BCContactManager::instance()->getContactList();
226
227    // Validate the number
228    SNumber number(ui.edtCountryCode->text() + " " + ui.edtNumber->text());
229    if (!number.isValid()) {
230        errorMessages += tr("The given phone number is not valid.") + "\n";
231    } else {
232        SContact contact = BCContactManager::instance()->getContactByNumber(number);
233        if (contact && (contact != contact_)) {
234            if (ignoreAlreadyExisting_) {
235                contactList.remove(contact);
236            } else {
237                errorMessages += tr("\"%1\" is already in the adressbook with the same number.").arg(contact.name()) + "\n";
238            }
239        }
240    }
241
242    // Validate the name and aliases for double usage
243    QStringList reservedNames;
244    foreach (SContact contact, contactList) {
245        if (contact == contact_)
246            continue;
247
248        reservedNames.append(contact.name());
249        reservedNames.append(contact.aliases());
250    }
251    foreach (SGroup group, BCGroupManager::instance()->getGroupList()) {
252        reservedNames.append(group.name());
253    }
254
255    QString invalidNames = "";
256    QStringList names;
257    names.append(ui.edtName->text());
258    for (int i = 0; i < ui.lstAliases->count(); ++i) {
259        names.append(ui.lstAliases->item(i)->text());
260    }
261    foreach (QString alias, names) {
262        if (reservedNames.contains(alias, Qt::CaseInsensitive)) {
263            invalidNames += " - " + alias + "\n";
264        }
265    }
266    if (!invalidNames.isEmpty()) {
267        errorMessages += tr("Some names of this contact are already in use:") + "\n" + invalidNames;
268    }
269
270
271    // Show message if validation failed
272    errorMessages.remove(QRegExp("[\n]+$"));
273    if (!errorMessages.isEmpty()) {
274        QMessageBox::warning(this, tr("Edit contact"), errorMessages, QMessageBox::Ok, QMessageBox::Ok);
275        return false;
276    }
277
278    return true;
279}
280
281void VCEditContact::on_btnsNavigation_accepted(){
282    if (!validate()){
283        return;
284    }
285
286    contact_.setName(ui.edtName->text());
287    contact_.setNumber(SNumber(ui.edtCountryCode->text() + " " + ui.edtNumber->text(), SNumber::GuessFormat));
288    QStringList aliases;
289    for (int x = 0; x < ui.lstAliases->count(); x++){
290        QString alias = ui.lstAliases->item(x)->text().trimmed();
291        if (alias != "") {
292            aliases.append(alias);
293        }
294    }
295    contact_.setAliases(aliases);
296
297    contact_.setImage(contactImage_);
298
299    done(QDialog::Accepted);
300}
Note: See TracBrowser for help on using the repository browser.