4729a01520c4c133678377aab230354cfbc0ebfc
[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   *
155   */
156 void SettingsDialog::setUserName(QString username)
157 {
158     this->username = username;
159 }
160
161 /**
162   *
163   */
164 void SettingsDialog::setPassword(QString password)
165 {
166     this->password = password;
167 }
168
169 /**
170   *
171   */
172 QString SettingsDialog::getUserName()
173 {
174     return this->username;
175 }
176
177 /**
178   *
179   */
180 QString SettingsDialog::getPassword()
181 {
182     return this->password;
183 }
184
185 /**
186   *
187   */
188 void SettingsDialog::setLabelInfoToUser(QString infoText)
189 {
190     this->ui->labelInfoToUser->setText(infoText);
191 }
192
193 /**
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         ui->pushButtonProfile->setDisabled(false);
204     }
205
206     else
207     {
208         ui->setUserPushButton->setText("Log in");
209         ui->setUserPasswordLineEdit->clear();
210         this->username = "";
211         this->password = "";
212         saveLogin( this->username, this->password);
213         ui->pushButtonProfile->setDisabled(true);
214     }
215 }
216
217 /**
218   * This slot function called when ever info button clicked.
219   */
220 void SettingsDialog::on_pushButtonInfo_clicked()
221 {
222     if(!helpSettingsDialog)
223     {
224         helpSettingsDialog = new HelpSettingsDialog;
225     }
226     connect(helpSettingsDialog, SIGNAL(rejected()), this, SLOT(killDialog()));
227     helpSettingsDialog->show();
228 }
229
230 /**
231   * This slot function called when ever dialog rejected.
232   */
233 void SettingsDialog::killDialog()
234 {
235     if(helpSettingsDialog)
236     {
237         qDebug() << "__Settings kill: helpSettingsDialog";
238         delete helpSettingsDialog;
239         helpSettingsDialog = NULL;
240     }
241     if(registerDialog)
242     {
243         qDebug() << "__Settings kill: registerDialog";
244         delete registerDialog;
245         registerDialog = NULL;
246     }
247 }
248
249 /**
250   * This slot function called when
251   */
252 void SettingsDialog::registrate()
253 {
254     emit sendregistration();
255 }