cf6e26d45431cb97b3f0c89222bf8221d31345eb
[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     profileDialog = NULL;
23
24     this->setWindowTitle("Settings");
25     this->ui->regEMailLineEdit->setText("@");
26
27     ui->pushButtonProfile->setDisabled(true);
28
29     if (loginSaved())
30     {
31         QString uName, pWord;
32
33         getLoginInfo( &uName, &pWord);
34         this->username = uName;
35         this->password = pWord;
36
37         // Set line edit
38         ui->setUserPasswordLineEdit->setText(this->password);
39         ui->setUserUsernameLineEdit->setText(this->username);
40         ui->setUserPasswordLineEdit->setDisabled(1); // Disable because user logged
41         ui->setUserUsernameLineEdit->setDisabled(1); // Disable because user logged
42
43         // Already someone as user - change button text to "Change"
44         ui->setUserPushButton->setText("Log out");
45
46         // Button settings
47         ui->pushButtonInfo->setAutoFillBackground(true);
48         ui->pushButtonInfo->setStyleSheet("background-color: rgb(0, 0, 0); color: rgb(255, 255, 255)");
49         ui->pushButtonProfile->setDisabled(false);
50     }
51
52     ui->pushButtonInfo->setAutoFillBackground(true);
53     ui->pushButtonInfo->setStyleSheet("background-color: rgb(0, 0, 0); color: rgb(255, 255, 255)");
54 }
55
56 SettingsDialog::~SettingsDialog()
57 {
58     delete ui;
59 }
60
61 void SettingsDialog::changeEvent(QEvent *e)
62 {
63     QDialog::changeEvent(e);
64     switch (e->type()) {
65     case QEvent::LanguageChange:
66         ui->retranslateUi(this);
67         break;
68     default:
69         break;
70     }
71 }
72
73 //
74 // Registrate
75 //
76 void SettingsDialog::on_registratePushButton_clicked()
77 {
78     // Send username, password and email to SpeedFreak server
79     this->regUsername = ui->regUserNameLineEdit->text();
80     this->regPassword = ui->regPasswordLineEdit->text();
81     this->regEmail = ui->regEMailLineEdit->text();
82
83     if (this->regUsername.compare("") && this->regPassword.compare("") && this->regEmail.compare("") && this->regEmail.compare("@"))
84     {
85         emit sendregistration();
86
87     }
88     else
89     {
90         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");
91     }
92
93     //close();      //using close() hides popup-window which reports error from server
94 }
95
96 // Next 6 functions can be removed if Settingsdialog is implemented without
97 // own copy of username, password & email
98 void SettingsDialog::setRegUserName(QString username)
99 {
100     this->regUsername = username;
101 }
102
103 void SettingsDialog::setRegPassword(QString password)
104 {
105     this->regPassword = password;
106 }
107
108 void SettingsDialog::setRegEmail(QString email)
109 {
110     this->regEmail = email;
111 }
112
113 QString SettingsDialog::getRegUserName()
114 {
115     return this->regUsername;
116 }
117
118 QString SettingsDialog::getRegPassword()
119 {
120     return this->regPassword;
121 }
122
123 QString SettingsDialog::getRegEmail()
124 {
125     return this->regEmail;
126 }
127
128 //
129 // Set / Change user
130 //
131 void SettingsDialog::on_setUserPushButton_clicked()
132 {
133     if (!ui->setUserPushButton->text().compare("Log out"))
134     {
135         ui->setUserUsernameLineEdit->setDisabled(false);
136         ui->setUserPasswordLineEdit->setDisabled(false);
137         //ui->setUserUsernameLineEdit->setText("");
138         //ui->setUserPasswordLineEdit->setText("");
139         ui->setUserUsernameLineEdit->clear();
140         ui->setUserPasswordLineEdit->clear();
141         this->username = ui->setUserUsernameLineEdit->text();
142         this->password = ui->setUserPasswordLineEdit->text();
143         ui->setUserPushButton->setText("Log in");
144         saveLogin( this->username, this->password);
145         ui->pushButtonProfile->setDisabled(true);
146         emit logout();
147     }
148     else
149     {
150         this->username = ui->setUserUsernameLineEdit->text();
151         this->password = ui->setUserPasswordLineEdit->text();
152         saveLogin( this->username, this->password);
153         ui->setUserPushButton->setText("Log out");
154
155         if(this->username.compare(""))
156         {
157             emit userNameChanged();
158             //ui->setUserPushButton->setText("Log out");
159         }
160
161         else
162         {
163             QMessageBox::about(this, "Username field is empty", "Set username and log in again");
164             ui->setUserPushButton->setText("Log in");
165         }
166     }
167     // Save these also to usersettings
168     //saveLogin( this->username, this->password);
169
170
171
172     /*
173     // Set "Set/Change User" button text
174     if (this->username.length() > 0)
175     {
176         ui->setUserPushButton->setText("Log out");
177     }
178     else
179     {   // Username "cleared"
180         ui->setUserPushButton->setText("Log in");
181     }
182
183     emit userNameChanged();
184     */
185     //close();  //using close() hides popup-window which reports error from server
186 }
187
188 // Next 4 functions can be removed if Settingsdialog is implemented without
189 // own copy of username & password
190 void SettingsDialog::setUserName(QString username)
191 {
192     this->username = username;
193 }
194
195 void SettingsDialog::setPassword(QString password)
196 {
197     this->password = password;
198 }
199
200 QString SettingsDialog::getUserName()
201 {
202     return this->username;
203 }
204
205 QString SettingsDialog::getPassword()
206 {
207     return this->password;
208 }
209
210 void SettingsDialog::setLabelInfoToUser(QString infoText)
211 {
212     this->ui->labelInfoToUser->setText(infoText);
213 }
214
215 void SettingsDialog::usernameOk(bool isOk)
216 {
217     if (isOk)
218     {
219         ui->setUserPushButton->setText("Log out");
220         ui->setUserUsernameLineEdit->setDisabled(true);
221         ui->setUserPasswordLineEdit->setDisabled(true);
222         ui->pushButtonProfile->setDisabled(false);
223     }
224
225     else
226     {
227         ui->setUserPushButton->setText("Log in");
228         ui->setUserUsernameLineEdit->clear();
229         ui->setUserPasswordLineEdit->clear();
230         this->username = ui->setUserUsernameLineEdit->text();
231         this->password = ui->setUserPasswordLineEdit->text();
232         saveLogin( this->username, this->password);
233         ui->pushButtonProfile->setDisabled(true);
234     }
235 }
236
237 void SettingsDialog::clearRegisterLineEdits()
238 {
239     ui->regEMailLineEdit->setText("@");
240     ui->regPasswordLineEdit->setText("");
241     ui->regUserNameLineEdit->setText("");
242 }
243
244 /**
245   * This slot function called when ever info button clicked.
246   */
247 void SettingsDialog::on_pushButtonInfo_clicked()
248 {
249     if(!helpSettingsDialog)
250     {
251         helpSettingsDialog = new HelpSettingsDialog;
252     }
253     connect(helpSettingsDialog, SIGNAL(rejected()), this, SLOT(killDialog()));
254     helpSettingsDialog->show();
255 }
256
257 /**
258   * This slot function called when ever dialog rejected.
259   */
260 void SettingsDialog::killDialog()
261 {
262     if(helpSettingsDialog)
263     {
264         qDebug() << "__Settings kill: helpSettingsDialog";
265         delete helpSettingsDialog;
266         helpSettingsDialog = NULL;
267     }
268     if(profileDialog)
269     {
270         qDebug() << "__Settings kill: profileDialog";
271         delete profileDialog;
272         profileDialog = NULL;
273     }
274 }
275
276 /**
277   * This slot function called when ever profile button clicked.
278   * Open profile dialog.
279   */
280 void SettingsDialog::on_pushButtonProfile_clicked()
281 {
282     if(!profileDialog)
283     {
284         profileDialog = new ProfileDialog(this);
285     }
286     connect(profileDialog, SIGNAL(saveprofile()), this, SLOT(saveProfile()));
287     connect(profileDialog, SIGNAL(rejected()), this, SLOT(killDialog()));
288     profileDialog->show();
289 }
290
291 void SettingsDialog::saveProfile()
292 {
293     emit saveprofile();
294 }