Initial commit
[tietoopcom] / src / TocUi / TocUserInfoWidget.cpp
1 /** \file.cpp
2  * \brief Implementation of TocUserInfoWidget class
3  * 
4  * Tieto Open Communicator - Client for the Telepathy communications framework.
5  * Copyright (c) 2010, Tieto Corporation
6  *
7  * All rights reserved.
8  * Redistribution and use in source and binary forms, with or without modification,
9  * are permitted provided that the following conditions are met:
10  *
11  *      Redistributions of source code must retain the above copyright notice,
12  *      this list of conditions and the following disclaimer.
13  *      Redistributions in binary form must reproduce the above copyright notice,
14  *      this list of conditions and the following disclaimer in the documentation
15  *      and/or other materials provided with the distribution.
16  *      Neither the name of the Tieto Corporation nor the names of its contributors 
17  *      may be used to endorse or promote products derived from this software without
18  *      specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  * 
30  */
31
32 #include <QDebug>
33 #include <QFontMetrics>
34 #include <TocContactListWidget>
35 #include <QMessageBox>
36 #include <QShowEvent>
37 #include <QListWidgetItem>
38
39 #include "TocUserInfoWidget"
40 #include "IconProvider"
41 #include "ui_TocUserInfoWidget.h"
42 #include "uidefs.h"
43 #include "macros.h"
44
45 Q_DECLARE_METATYPE(TocContact*);
46
47
48 TocUserInfoWidget::TocUserInfoWidget(QListWidgetItem * contactItem, TocContactListWidget * parent, Qt::WindowFlags flags)
49         : QWidget(parent, flags), _pContactList(parent)
50 {
51
52         ui = new Ui_TocUserInfoWidgetClass();
53         ui->setupUi(this);
54         setContentsMargins(-10, -10, -10, -10 );
55
56         ui->resolveButton->hide(); //TODO: implement resolving contact's name functionality
57
58         //Adjust labels size to their content
59         QFontMetrics fm(ui->uidLabel->font());
60
61         //Find the widest 
62         QList<int> list;
63         list << fm.boundingRect(ui->uidLabel->text()).width();
64         list << fm.boundingRect(ui->nameLabel->text()).width();
65         list << fm.boundingRect(ui->genderLabel->text()).width();
66         qSort(list.begin(), list.end());
67
68         //Set minimum width
69         ui->uidLabel->setMinimumWidth(list.last());
70         ui->nameLabel->setMinimumWidth(list.last());
71         ui->genderLabel->setMinimumWidth(list.last());
72
73         _newCustomPicturePath = "";
74
75         _pContactItem = contactItem;
76
77         ui->genderComboBox->addItem(tr("Unknown") );
78         ui->genderComboBox->setItemData(0, NotSpecified);
79
80         ui->genderComboBox->addItem(tr("Male") );
81         ui->genderComboBox->setItemData(1, Male);
82
83         ui->genderComboBox->addItem(tr("Female"));
84         ui->genderComboBox->setItemData(2, Female);     
85
86         CONNECT(ui->genderComboBox,SIGNAL(currentIndexChanged(int)),this,SLOT(onCurrentIndexChanged(int)));
87         CONNECT(ui->okButton, SIGNAL(clicked()),this,SLOT(okClicked()));
88         CONNECT(ui->cancelButton,SIGNAL(clicked()),this,SIGNAL(finished()));
89
90         _pIconProvider = IconProvider::getInstance();
91
92         if (!contactItem) {
93                 ui->pictureButton->setIcon(_pIconProvider->matchIcon(NotSpecified));
94                 return;
95         }
96
97         ui->nameLineEdit->setText(contactItem->data(ContactRole).value<TocContact*>()->name);
98         ui->uidLineEdit->setText(contactItem->data(ContactRole).value<TocContact*>()->uid);
99
100
101         Gender gender = contactItem->data(ContactRole).value<TocContact*>()->gender;
102
103         int idx = ui->genderComboBox->findData(gender);
104
105         if (idx >= 0 && idx <= 2) {
106                 ui->genderComboBox->setCurrentIndex(idx);
107                 _currentIndex = idx;
108         }
109
110         _customPicturePath = _newCustomPicturePath = contactItem->data(ContactRole).value<TocContact*>()->customIconPath;
111
112         // Get picture
113         QIcon icon;
114         if(_customPicturePath.isEmpty())
115                 icon = _pIconProvider->matchIcon(gender);
116         else
117                 icon = QIcon(_customPicturePath);
118
119         ui->pictureButton->setIcon(icon);
120 }
121
122 TocUserInfoWidget::~TocUserInfoWidget() {
123         delete ui;
124 }
125
126 void TocUserInfoWidget::onCurrentIndexChanged (int index) {
127
128         Gender gender = static_cast<Gender>(ui->genderComboBox->itemData(index).value<int>());
129
130         // Get picture
131         QIcon icon;
132         if(_newCustomPicturePath.isEmpty())
133                 icon = _pIconProvider->matchIcon(gender);
134         else
135                 icon = QIcon(_customPicturePath);
136
137         ui->pictureButton->setIcon(icon);
138 }
139
140 void TocUserInfoWidget::okClicked() {
141
142         if(_pContactItem) { // Edit an existing item
143
144                 Gender gender;
145                 QString uid = ui->uidLineEdit->text();
146
147                 if(uid.isEmpty()) {
148                         QMessageBox::warning(this, tr("TEC"), tr("User ID is empty.\nCannot commit changes."));
149                         return; 
150                 }
151
152                 // Check if UID is not already on contact list
153                 if(_pContactList->findItem(uid, _pContactItem)) {
154                         QMessageBox::warning(this, tr("TEC"), tr("The given user ID already exists on Your contact list.\nCannot commit changes."));
155                         return;
156                 }
157
158                 QString name = ui->nameLineEdit->text();
159                 int idx = ui->genderComboBox->currentIndex();
160
161                 // Check if something has changed
162                 bool changed = false;
163                 if(_pContactItem->data(ContactRole).value<TocContact*>()->uid != uid)
164                         changed = true;
165                 else if(_pContactItem->text() != name)
166                         changed = true;
167                 else if(_currentIndex != idx)
168                         changed = true;
169                 else if(_customPicturePath != _newCustomPicturePath)
170                         changed = true;
171
172                 // If yes: save changes and emit signal to notify TocContactListWidget (commitEdit)
173                 if(changed) {
174                         gender = static_cast<Gender>(ui->genderComboBox->itemData(idx).value<int>());
175
176                         //Get contact from QListWidgetItem
177                         TocContact *pContact = _pContactItem->data(ContactRole).value<TocContact*>();
178
179                         if(name.isEmpty())
180                                 _pContactItem->setText(uid);
181                         else
182                                 _pContactItem->setText(name);
183
184                         pContact->uid = uid;
185                         pContact->name = name;
186                         pContact->gender = gender;
187
188                         _pContactItem->setData(Qt::DecorationRole, *(_pIconProvider->matchIcon(pContact)));
189                         emit contactChanged(_pContactItem);
190                 }
191         } else { // Create new item
192
193                 // Collect data for the new contact
194                 // Get uid
195                 QString uid = ui->uidLineEdit->text();
196
197                 if(uid.isEmpty()) {
198                         QMessageBox::warning(this, tr("TEC"), tr("User ID is empty.\nCannot commit changes."));
199                         return; 
200                 }
201
202                 // Check if UID is not already on contact list
203                 if(_pContactList->findItem(uid)) {
204                         QMessageBox::warning(this, tr("TEC"), tr("The given user ID already exists on Your contact list.\nCannot commit changes."));
205                         return;
206                 }
207
208                 // Get name
209                 QString name = ui->nameLineEdit->text();
210
211                 // ... and gender
212                 int idx = ui->genderComboBox->currentIndex();
213                 Gender gender = static_cast<Gender>(ui->genderComboBox->itemData(idx).value<int>());            
214
215                 // Create QListWidgetItem and emit it to notify TocContactListWidget (commitAdd)
216                 QString displayedText;
217                 if(name.isEmpty())
218                         displayedText = uid;
219                 else
220                         displayedText = name;
221                 
222                 QListWidgetItem* pNewItem = new QListWidgetItem(displayedText, 0);
223
224                 // Create new  contact structure...
225                 TocContact* pNewContact = new TocContact;
226
227                 pNewContact->uid = uid;
228                 pNewContact->name = name;
229                 pNewContact->gender = gender;
230                 pNewContact->customIconId = 0;
231                 pNewContact->customIconPath = "";
232                 pNewContact->presence = Offline;
233
234                 // ... and associate it with QListWidgetItem
235                 pNewItem->setData(ContactRole, QVariant::fromValue(pNewContact));
236
237                 pNewItem->setData(Qt::DecorationRole, *(_pIconProvider->matchIcon(pNewContact)));
238                 pNewItem->setFlags(Qt::ItemIsEnabled);
239
240                 emit contactChanged(pNewItem);
241         }
242
243         // Emit this to finish the job
244         emit finished();        
245 }
246
247 void TocUserInfoWidget::showEvent(QShowEvent* event) {
248         ui->uidLineEdit->setFocus();
249         event->accept();
250 }