Bug fixed. Now user can't send register request if one or more of the registration...
[speedfreak] / Client / settingsdialog.cpp
1 /*
2  * SettingsDialog class
3  *
4  * @author     Olavi Pulkkinen <olavi.pulkkinen@fudeco.com>
5  * @copyright  (c) 2010 Speed Freak team
6  * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
7  */
8
9 #include "settingsdialog.h"
10 #include "ui_settingsdialog.h"
11 #include "usersettings.h"
12 #include <QMessageBox>
13
14 SettingsDialog::SettingsDialog(QWidget *parent) :
15     QDialog(parent),
16     ui(new Ui::SettingsDialog)
17 {
18     ui->setupUi(this);
19     this->setWindowTitle("Settings");
20     this->ui->regEMailLineEdit->setText("@");
21
22     if (loginSaved())
23     {
24         QString uName, pWord;
25
26         getLoginInfo( &uName, &pWord);
27         this->username = uName;
28         this->password = pWord;
29
30         ui->setUserPasswordLineEdit->setText(this->password);
31         ui->setUserUsernameLineEdit->setText(this->username);
32
33         // Already someone as user - change button text to "Change"
34         ui->setUserPushButton->setText("Log out");
35     }
36 }
37
38 SettingsDialog::~SettingsDialog()
39 {
40     delete ui;
41 }
42
43 void SettingsDialog::changeEvent(QEvent *e)
44 {
45     QDialog::changeEvent(e);
46     switch (e->type()) {
47     case QEvent::LanguageChange:
48         ui->retranslateUi(this);
49         break;
50     default:
51         break;
52     }
53 }
54
55 //
56 // Registrate
57 //
58 void SettingsDialog::on_registratePushButton_clicked()
59 {
60     // Send username, password and email to SpeedFreak server
61     this->regUsername = ui->regUserNameLineEdit->text();
62     this->regPassword = ui->regPasswordLineEdit->text();
63     this->regEmail = ui->regEMailLineEdit->text();
64
65     if (this->regUsername.compare("") && this->regPassword.compare("") && this->regEmail.compare("") && this->regEmail.compare("@"))
66     {
67         emit sendregistration();
68
69     }
70     else
71     {
72         QMessageBox::about(this, "One or more of the fields is empty", "Set username (3-12 characters), password (at least 6 characters) and valid email address");
73     }
74
75     //close();      //using close() hides popup-window which reports error from server
76 }
77
78 // Next 6 functions can be removed if Settingsdialog is implemented without
79 // own copy of username, password & email
80 void SettingsDialog::setRegUserName(QString username)
81 {
82     this->regUsername = username;
83 }
84
85 void SettingsDialog::setRegPassword(QString password)
86 {
87     this->regPassword = password;
88 }
89
90 void SettingsDialog::setRegEmail(QString email)
91 {
92     this->regEmail = email;
93 }
94
95 QString SettingsDialog::getRegUserName()
96 {
97     return this->regUsername;
98 }
99
100 QString SettingsDialog::getRegPassword()
101 {
102     return this->regPassword;
103 }
104
105 QString SettingsDialog::getRegEmail()
106 {
107     return this->regEmail;
108 }
109
110 //
111 // Set / Change user
112 //
113 void SettingsDialog::on_setUserPushButton_clicked()
114 {
115     if (!ui->setUserPushButton->text().compare("Log out"))
116     {
117         ui->setUserUsernameLineEdit->setDisabled(false);
118         ui->setUserPasswordLineEdit->setDisabled(false);
119         //ui->setUserUsernameLineEdit->setText("");
120         //ui->setUserPasswordLineEdit->setText("");
121         ui->setUserUsernameLineEdit->clear();
122         ui->setUserPasswordLineEdit->clear();
123         this->username = ui->setUserUsernameLineEdit->text();
124         this->password = ui->setUserPasswordLineEdit->text();
125         ui->setUserPushButton->setText("Log in");
126         saveLogin( this->username, this->password);
127         emit logout();
128     }
129     else
130     {
131         this->username = ui->setUserUsernameLineEdit->text();
132         this->password = ui->setUserPasswordLineEdit->text();
133         saveLogin( this->username, this->password);
134         ui->setUserPushButton->setText("Log out");
135
136         if(this->username.compare(""))
137         {
138             emit userNameChanged();
139             //ui->setUserPushButton->setText("Log out");
140         }
141
142         else
143         {
144             QMessageBox::about(this, "Username field is empty", "Set username and log in again");
145             ui->setUserPushButton->setText("Log in");
146         }
147     }
148     // Save these also to usersettings
149     //saveLogin( this->username, this->password);
150
151
152
153     /*
154     // Set "Set/Change User" button text
155     if (this->username.length() > 0)
156     {
157         ui->setUserPushButton->setText("Log out");
158     }
159     else
160     {   // Username "cleared"
161         ui->setUserPushButton->setText("Log in");
162     }
163
164     emit userNameChanged();
165     */
166     //close();  //using close() hides popup-window which reports error from server
167 }
168
169 // Next 4 functions can be removed if Settingsdialog is implemented without
170 // own copy of username & password
171 void SettingsDialog::setUserName(QString username)
172 {
173     this->username = username;
174 }
175
176 void SettingsDialog::setPassword(QString password)
177 {
178     this->password = password;
179 }
180
181 QString SettingsDialog::getUserName()
182 {
183     return this->username;
184 }
185
186 QString SettingsDialog::getPassword()
187 {
188     return this->password;
189 }
190
191 void SettingsDialog::setLabelInfoToUser(QString infoText)
192 {
193     this->ui->labelInfoToUser->setText(infoText);
194 }
195
196 void SettingsDialog::usernameOk(bool isOk)
197 {
198     if (isOk)
199     {
200         ui->setUserPushButton->setText("Log out");
201         ui->setUserUsernameLineEdit->setDisabled(true);
202         ui->setUserPasswordLineEdit->setDisabled(true);
203     }
204
205     else
206     {
207         ui->setUserPushButton->setText("Log in");
208         ui->setUserUsernameLineEdit->clear();
209         ui->setUserPasswordLineEdit->clear();
210         this->username = ui->setUserUsernameLineEdit->text();
211         this->password = ui->setUserPasswordLineEdit->text();
212         saveLogin( this->username, this->password);
213     }
214 }