Initial commit
[tietoopcom] / src / TocUi / TocAccountSettingsWidget.cpp
1 /** \file TocAccountSettingsWidget.cpp
2  * \brief Implementation of TocAccountSettingsWidget 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 <QMessageBox>
33 #include <QtDebug>
34
35 #include "TocAccountSettingsWidget"
36 #include "ui_TocAccountSettingsWidget.h"
37 #include "TocSettings"
38 #include "macros.h"
39
40
41
42 TocAccountSettingsWidget::TocAccountSettingsWidget(QWidget * parent, Qt::WindowFlags flags)
43         : QWidget(parent, flags), _bCreateMode(false) {
44
45         ui = new Ui_TocAccountSettingsWidgetClass();
46         ui->setupUi( this );
47         setContentsMargins( -10, -10, -10, -10 );
48         setObjectName("accountSettings");
49
50         ui->editVCardButton->hide();
51         ui->changePasswordButton->hide();
52
53         TocSettings* pSettings = TocSettings::getInstance();
54         QStringList prot =  pSettings->protocols();
55         for(int i = 0; i < prot.count(); ++i)
56                 ui->protocolComboBox->addItem(prot[i]);
57         
58         CONNECT(ui->backButton, SIGNAL(clicked()), this, SIGNAL(backClicked()));
59 }
60
61 TocAccountSettingsWidget::~TocAccountSettingsWidget() {
62         delete ui;
63 }
64
65 void TocAccountSettingsWidget::loginEdited(const QString& newLogin) {
66         TocSettings* pSettings = TocSettings::getInstance();
67         ui->serverLineEdit->setText(pSettings->serverHint(newLogin, ui->protocolComboBox->currentText()));
68         ui->portLineEdit->setText(QString("%1").arg(pSettings->portHint(newLogin, ui->protocolComboBox->currentText())));
69 }
70
71 void TocAccountSettingsWidget::protocolEdited(const QString& newProtocol) {
72         TocSettings* pSettings = TocSettings::getInstance();
73         ui->serverLineEdit->setText(pSettings->serverHint(ui->loginLineEdit->text(), newProtocol));
74         ui->portLineEdit->setText(QString("%1").arg(pSettings->portHint(ui->loginLineEdit->text(), newProtocol)));
75 }
76
77 void TocAccountSettingsWidget::disconnectHints() {
78         disconnect(ui->loginLineEdit, SIGNAL(textEdited(QString)), this, SLOT(loginEdited(QString)));
79         disconnect(ui->protocolComboBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(protocolEdited(QString)));
80         disconnect(ui->serverLineEdit, SIGNAL(textEdited(QString)), this, SLOT(disconnectHints()));
81         disconnect(ui->portLineEdit, SIGNAL(textEdited(QString)), this, SLOT(disconnectHints()));
82 }
83
84 void TocAccountSettingsWidget::connectHints() {
85         // disconnect first to ensure double connections are not made..
86         disconnectHints();
87         CONNECT(ui->loginLineEdit, SIGNAL(textEdited(QString)), this, SLOT(loginEdited(QString)));
88         CONNECT(ui->protocolComboBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(protocolEdited(QString)));
89         CONNECT(ui->serverLineEdit, SIGNAL(textEdited(QString)), this, SLOT(disconnectHints()));
90         CONNECT(ui->portLineEdit, SIGNAL(textEdited(QString)), this, SLOT(disconnectHints()));
91
92 }
93
94 void TocAccountSettingsWidget::reload() {
95         connectHints();
96
97         if(createMode()) {
98                 ui->loginLineEdit->setText(     "" );
99                 ui->passwordLineEdit->setText( "" );
100                 ui->passwordConfirmLineEdit->setText( "" );
101                 ui->serverLineEdit->setText( "" );
102                 ui->protocolComboBox->setCurrentIndex( -1 );
103                 ui->portLineEdit->setText( "" );
104         } else {
105                 TocSettings* pSettings = TocSettings::getInstance();
106                 ui->loginLineEdit->setText(     pSettings->accountUid() );
107                 ui->passwordLineEdit->setText( pSettings->accountPasswd() );
108                 ui->serverLineEdit->setText( pSettings->server() );
109                 ui->portLineEdit->setText(QString("%1").arg( pSettings->port() ));
110                 QString s = pSettings->protocolCommonName();
111                 ui->protocolComboBox->setCurrentIndex(
112                                 ui->protocolComboBox->findText( s, Qt::MatchExactly ));
113         }
114 }
115
116 void TocAccountSettingsWidget::showEvent(QShowEvent *event){
117         reload();
118         QWidget::showEvent(event);
119 }
120
121 void TocAccountSettingsWidget::handleError(QString errorText) {
122         QMessageBox::information( this, "Invalid value", errorText);
123 }
124
125 bool TocAccountSettingsWidget::createMode() const {
126         return _bCreateMode;
127 }
128
129 void TocAccountSettingsWidget::setCreateMode(bool enable) {
130
131         _bCreateMode = enable;
132
133         ui->passwordConfirmLabel->setVisible(enable);
134         ui->passwordConfirmLineEdit->setVisible(enable);
135         ui->changePasswordButton->setVisible(!enable);
136
137         if(enable) {
138                 ui->okButton->setText(tr("Create"));
139                 ui->backButton->setText(tr("Back"));
140         } else {
141                 ui->okButton->setText(tr("Ok"));
142                 ui->backButton->setText(tr("Back"));
143         }
144 }
145
146 bool TocAccountSettingsWidget::dataVerified() {
147         if(!ui->loginLineEdit->text().length()) {
148                 handleError(tr("Please provide login"));
149                 return false;
150         }
151         if(!ui->passwordLineEdit->text().length()) {
152                 handleError(tr("Please provide password"));
153                 return false;
154         }
155         if(createMode() && ui->passwordLineEdit->text() != ui->passwordConfirmLineEdit->text()) {
156                 handleError(tr("Passwords do not match"));
157                 return false;
158         }
159         if(!ui->serverLineEdit->text().length()) {
160                 handleError(tr("Please provide server name"));
161                 return false;
162         }
163         if(!ui->portLineEdit->text().toInt()) {
164                 handleError(tr("Please provide port number"));
165                 return false;
166         }
167
168         return true;
169 }
170
171 void TocAccountSettingsWidget::onChangePasswordButton(){
172         // TODO: finish implementation
173 }
174
175 void TocAccountSettingsWidget::onOkButton(){
176         if(!dataVerified())
177                 return;
178
179         if(createMode())
180                 createNewAccount();
181         else {
182                 saveAccountData();
183                 emit finished(); // Close the view when not in `Create mode`
184         }
185 }
186
187 void TocAccountSettingsWidget::saveAccountData() {
188
189         bool settingsChanged = false;
190
191         TocSettings* pSettings = TocSettings::getInstance();
192
193         if(ui->loginLineEdit->text() != pSettings->accountUid()) {
194                 pSettings->setAccountUid(ui->loginLineEdit->text());
195                 settingsChanged = true;
196         }
197
198         if(ui->passwordLineEdit->text() != pSettings->accountPasswd()) {
199                 pSettings->setAccountPasswd(ui->passwordLineEdit->text());
200 //              settingsChanged = true; // This is not needed for only the password
201         }
202
203         if(ui->protocolComboBox->currentText() != pSettings->protocolCommonName()) {
204                 pSettings->setProtocolByName(ui->protocolComboBox->currentText());
205                 settingsChanged = true;
206         }
207
208         if(ui->serverLineEdit->text() != pSettings->server()) {
209                 pSettings->setServer(ui->serverLineEdit->text());
210                 settingsChanged = true;
211         }
212
213         if(ui->portLineEdit->text().toInt() != pSettings->port()) {
214                 pSettings->setPort(ui->portLineEdit->text().toInt());
215                 settingsChanged = true;
216         }
217
218         if(settingsChanged) {
219                 emit accountSettingsChanged();
220         }
221 }
222
223 void TocAccountSettingsWidget::createNewAccount() {
224
225         TocSettings* pSettings = TocSettings::getInstance();
226         pSettings->setAccountUid(ui->loginLineEdit->text());
227         pSettings->setAccountPasswd(ui->passwordLineEdit->text());
228         pSettings->setServer(ui->serverLineEdit->text());
229         pSettings->setPort(ui->portLineEdit->text().toInt());
230         pSettings->setProtocolByName(ui->protocolComboBox->currentText());
231         pSettings->setRegister();
232
233         emit newAccountSettingsCreated();
234 }