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