source: SMSSender/frontend/gui/ui/editcontact.cpp @ 337:02ee15b6736a

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