a50a681b7c6a437bcb97bec7da81038d65236b10
[speedfreak] / Client / settingsdialog.cpp
1 /*
2  * SettingsDialog class
3  *
4  * @author      Olavi Pulkkinen <olavi.pulkkinen@fudeco.com>
5  * @author      Toni Jussila    <toni.jussila@fudeco.com>
6  * @copyright   (c) 2010 Speed Freak team
7  * @license     http://opensource.org/licenses/gpl-license.php GNU Public License
8  */
9
10 #include "settingsdialog.h"
11 #include "ui_settingsdialog.h"
12 #include "usersettings.h"
13 #include <QMessageBox>
14 #include <QDebug>
15
16 SettingsDialog::SettingsDialog(QWidget *parent) :
17     QDialog(parent), ui(new Ui::SettingsDialog)
18 {
19     ui->setupUi(this);
20
21     helpSettingsDialog = NULL;
22
23     this->setWindowTitle("Settings");
24     this->ui->regEMailLineEdit->setText("@");
25
26     if (loginSaved())
27     {
28         QString uName, pWord;
29
30         getLoginInfo( &uName, &pWord);
31         this->username = uName;
32         this->password = pWord;
33
34         // Set line edit
35         ui->setUserPasswordLineEdit->setText(this->password);
36         ui->setUserUsernameLineEdit->setText(this->username);
37         ui->setUserPasswordLineEdit->setDisabled(1); // Disable because user logged
38         ui->setUserUsernameLineEdit->setDisabled(1); // Disable because user logged
39
40         // Already someone as user - change button text to "Change"
41         ui->setUserPushButton->setText("Log out");
42
43         // Button settings
44         ui->pushButtonInfo->setAutoFillBackground(true);
45         ui->pushButtonInfo->setStyleSheet("background-color: rgb(0, 0, 0); color: rgb(255, 255, 255)");
46     }
47 }
48
49 SettingsDialog::~SettingsDialog()
50 {
51     delete ui;
52 }
53
54 void SettingsDialog::changeEvent(QEvent *e)
55 {
56     QDialog::changeEvent(e);
57     switch (e->type()) {
58     case QEvent::LanguageChange:
59         ui->retranslateUi(this);
60         break;
61     default:
62         break;
63     }
64 }
65
66 //
67 // Registrate
68 //
69 void SettingsDialog::on_registratePushButton_clicked()
70 {
71     // Send username, password and email to SpeedFreak server
72     this->regUsername = ui->regUserNameLineEdit->text();
73     this->regPassword = ui->regPasswordLineEdit->text();
74     this->regEmail = ui->regEMailLineEdit->text();
75
76     if (this->regUsername.compare("") && this->regPassword.compare("") && this->regEmail.compare("") && this->regEmail.compare("@"))
77     {
78         emit sendregistration();
79
80     }
81     else
82     {
83         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");
84     }
85
86     //close();      //using close() hides popup-window which reports error from server
87 }
88
89 // Next 6 functions can be removed if Settingsdialog is implemented without
90 // own copy of username, password & email
91 void SettingsDialog::setRegUserName(QString username)
92 {
93     this->regUsername = username;
94 }
95
96 void SettingsDialog::setRegPassword(QString password)
97 {
98     this->regPassword = password;
99 }
100
101 void SettingsDialog::setRegEmail(QString email)
102 {
103     this->regEmail = email;
104 }
105
106 QString SettingsDialog::getRegUserName()
107 {
108     return this->regUsername;
109 }
110
111 QString SettingsDialog::getRegPassword()
112 {
113     return this->regPassword;
114 }
115
116 QString SettingsDialog::getRegEmail()
117 {
118     return this->regEmail;
119 }
120
121 //
122 // Set / Change user
123 //
124 void SettingsDialog::on_setUserPushButton_clicked()
125 {
126     if (!ui->setUserPushButton->text().compare("Log out"))
127     {
128         ui->setUserUsernameLineEdit->setDisabled(false);
129         ui->setUserPasswordLineEdit->setDisabled(false);
130         //ui->setUserUsernameLineEdit->setText("");
131         //ui->setUserPasswordLineEdit->setText("");
132         ui->setUserUsernameLineEdit->clear();
133         ui->setUserPasswordLineEdit->clear();
134         this->username = ui->setUserUsernameLineEdit->text();
135         this->password = ui->setUserPasswordLineEdit->text();
136         ui->setUserPushButton->setText("Log in");
137         saveLogin( this->username, this->password);
138         emit logout();
139     }
140     else
141     {
142         this->username = ui->setUserUsernameLineEdit->text();
143         this->password = ui->setUserPasswordLineEdit->text();
144         saveLogin( this->username, this->password);
145         ui->setUserPushButton->setText("Log out");
146
147         if(this->username.compare(""))
148         {
149             emit userNameChanged();
150             //ui->setUserPushButton->setText("Log out");
151         }
152
153         else
154         {
155             QMessageBox::about(this, "Username field is empty", "Set username and log in again");
156             ui->setUserPushButton->setText("Log in");
157         }
158     }
159     // Save these also to usersettings
160     //saveLogin( this->username, this->password);
161
162
163
164     /*
165     // Set "Set/Change User" button text
166     if (this->username.length() > 0)
167     {
168         ui->setUserPushButton->setText("Log out");
169     }
170     else
171     {   // Username "cleared"
172         ui->setUserPushButton->setText("Log in");
173     }
174
175     emit userNameChanged();
176     */
177     //close();  //using close() hides popup-window which reports error from server
178 }
179
180 // Next 4 functions can be removed if Settingsdialog is implemented without
181 // own copy of username & password
182 void SettingsDialog::setUserName(QString username)
183 {
184     this->username = username;
185 }
186
187 void SettingsDialog::setPassword(QString password)
188 {
189     this->password = password;
190 }
191
192 QString SettingsDialog::getUserName()
193 {
194     return this->username;
195 }
196
197 QString SettingsDialog::getPassword()
198 {
199     return this->password;
200 }
201
202 void SettingsDialog::setLabelInfoToUser(QString infoText)
203 {
204     this->ui->labelInfoToUser->setText(infoText);
205 }
206
207 void SettingsDialog::usernameOk(bool isOk)
208 {
209     if (isOk)
210     {
211         ui->setUserPushButton->setText("Log out");
212         ui->setUserUsernameLineEdit->setDisabled(true);
213         ui->setUserPasswordLineEdit->setDisabled(true);
214     }
215
216     else
217     {
218         ui->setUserPushButton->setText("Log in");
219         ui->setUserUsernameLineEdit->clear();
220         ui->setUserPasswordLineEdit->clear();
221         this->username = ui->setUserUsernameLineEdit->text();
222         this->password = ui->setUserPasswordLineEdit->text();
223         saveLogin( this->username, this->password);
224     }
225 }
226
227 void SettingsDialog::clearRegisterLineEdits()
228 {
229     ui->regEMailLineEdit->setText("@");
230     ui->regPasswordLineEdit->setText("");
231     ui->regUserNameLineEdit->setText("");
232 }
233
234 /**
235   * This slot function called when ever info button clicked.
236   */
237 void SettingsDialog::on_pushButtonInfo_clicked()
238 {
239     if(!helpSettingsDialog)
240     {
241         helpSettingsDialog = new HelpSettingsDialog;
242     }
243     connect(helpSettingsDialog, SIGNAL(rejected()), this, SLOT(killHelpDialog()));
244     helpSettingsDialog->show();
245 }
246
247 /**
248   * This slot function called when ever dialog rejected.
249   */
250 void SettingsDialog::killHelpDialog()
251 {
252     if(helpSettingsDialog)
253     {
254         qDebug() << "__Settings kill: helpSettingsDialog";
255         delete helpSettingsDialog;
256         helpSettingsDialog = NULL;
257     }
258 }