fb5d7c075550f43be7159cbcfc23195bd9ea800a
[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     emit sendregistration();
66
67     //close();      //using close() hides popup-window which reports error from server
68 }
69
70 // Next 6 functions can be removed if Settingsdialog is implemented without
71 // own copy of username, password & email
72 void SettingsDialog::setRegUserName(QString username)
73 {
74     this->regUsername = username;
75 }
76
77 void SettingsDialog::setRegPassword(QString password)
78 {
79     this->regPassword = password;
80 }
81
82 void SettingsDialog::setRegEmail(QString email)
83 {
84     this->regEmail = email;
85 }
86
87 QString SettingsDialog::getRegUserName()
88 {
89     return this->regUsername;
90 }
91
92 QString SettingsDialog::getRegPassword()
93 {
94     return this->regPassword;
95 }
96
97 QString SettingsDialog::getRegEmail()
98 {
99     return this->regEmail;
100 }
101
102 //
103 // Set / Change user
104 //
105 void SettingsDialog::on_setUserPushButton_clicked()
106 {
107     if (!ui->setUserPushButton->text().compare("Log out"))
108     {
109         ui->setUserUsernameLineEdit->setDisabled(false);
110         ui->setUserPasswordLineEdit->setDisabled(false);
111         //ui->setUserUsernameLineEdit->setText("");
112         //ui->setUserPasswordLineEdit->setText("");
113         ui->setUserUsernameLineEdit->clear();
114         ui->setUserPasswordLineEdit->clear();
115         this->username = ui->setUserUsernameLineEdit->text();
116         this->password = ui->setUserPasswordLineEdit->text();
117         ui->setUserPushButton->setText("Log in");
118         saveLogin( this->username, this->password);
119         emit logout();
120     }
121     else
122     {
123         this->username = ui->setUserUsernameLineEdit->text();
124         this->password = ui->setUserPasswordLineEdit->text();
125         saveLogin( this->username, this->password);
126         ui->setUserPushButton->setText("Log out");
127
128         if(this->username.compare(""))
129         {
130             emit userNameChanged();
131             //ui->setUserPushButton->setText("Log out");
132         }
133
134         else
135         {
136             QMessageBox::about(this, "Username field is empty", "Set username and log in again");
137             ui->setUserPushButton->setText("Log in");
138         }
139     }
140     // Save these also to usersettings
141     //saveLogin( this->username, this->password);
142
143
144
145     /*
146     // Set "Set/Change User" button text
147     if (this->username.length() > 0)
148     {
149         ui->setUserPushButton->setText("Log out");
150     }
151     else
152     {   // Username "cleared"
153         ui->setUserPushButton->setText("Log in");
154     }
155
156     emit userNameChanged();
157     */
158     //close();  //using close() hides popup-window which reports error from server
159 }
160
161 // Next 4 functions can be removed if Settingsdialog is implemented without
162 // own copy of username & password
163 void SettingsDialog::setUserName(QString username)
164 {
165     this->username = username;
166 }
167
168 void SettingsDialog::setPassword(QString password)
169 {
170     this->password = password;
171 }
172
173 QString SettingsDialog::getUserName()
174 {
175     return this->username;
176 }
177
178 QString SettingsDialog::getPassword()
179 {
180     return this->password;
181 }
182
183 void SettingsDialog::setLabelInfoToUser(QString infoText)
184 {
185     this->ui->labelInfoToUser->setText(infoText);
186 }
187
188 void SettingsDialog::usernameOk(bool isOk)
189 {
190     if (isOk)
191     {
192         ui->setUserPushButton->setText("Log out");
193         ui->setUserUsernameLineEdit->setDisabled(true);
194         ui->setUserPasswordLineEdit->setDisabled(true);
195     }
196
197     else
198     {
199         ui->setUserPushButton->setText("Log in");
200         ui->setUserUsernameLineEdit->clear();
201         ui->setUserPasswordLineEdit->clear();
202         this->username = ui->setUserUsernameLineEdit->text();
203         this->password = ui->setUserPasswordLineEdit->text();
204         saveLogin( this->username, this->password);
205     }
206 }