d06ef3ac0647fa8c9368ac92758f6379199b6e76
[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         // Set line edit
31         ui->setUserPasswordLineEdit->setText(this->password);
32         ui->setUserUsernameLineEdit->setText(this->username);
33         ui->setUserPasswordLineEdit->setDisabled(1); // Disable because user logged
34         ui->setUserUsernameLineEdit->setDisabled(1); // Disable because user logged
35
36         // Already someone as user - change button text to "Change"
37         ui->setUserPushButton->setText("Log out");
38     }
39 }
40
41 SettingsDialog::~SettingsDialog()
42 {
43     delete ui;
44 }
45
46 void SettingsDialog::changeEvent(QEvent *e)
47 {
48     QDialog::changeEvent(e);
49     switch (e->type()) {
50     case QEvent::LanguageChange:
51         ui->retranslateUi(this);
52         break;
53     default:
54         break;
55     }
56 }
57
58 //
59 // Registrate
60 //
61 void SettingsDialog::on_registratePushButton_clicked()
62 {
63     // Send username, password and email to SpeedFreak server
64     this->regUsername = ui->regUserNameLineEdit->text();
65     this->regPassword = ui->regPasswordLineEdit->text();
66     this->regEmail = ui->regEMailLineEdit->text();
67
68     if (this->regUsername.compare("") && this->regPassword.compare("") && this->regEmail.compare("") && this->regEmail.compare("@"))
69     {
70         emit sendregistration();
71
72     }
73     else
74     {
75         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");
76     }
77
78     //close();      //using close() hides popup-window which reports error from server
79 }
80
81 // Next 6 functions can be removed if Settingsdialog is implemented without
82 // own copy of username, password & email
83 void SettingsDialog::setRegUserName(QString username)
84 {
85     this->regUsername = username;
86 }
87
88 void SettingsDialog::setRegPassword(QString password)
89 {
90     this->regPassword = password;
91 }
92
93 void SettingsDialog::setRegEmail(QString email)
94 {
95     this->regEmail = email;
96 }
97
98 QString SettingsDialog::getRegUserName()
99 {
100     return this->regUsername;
101 }
102
103 QString SettingsDialog::getRegPassword()
104 {
105     return this->regPassword;
106 }
107
108 QString SettingsDialog::getRegEmail()
109 {
110     return this->regEmail;
111 }
112
113 //
114 // Set / Change user
115 //
116 void SettingsDialog::on_setUserPushButton_clicked()
117 {
118     if (!ui->setUserPushButton->text().compare("Log out"))
119     {
120         ui->setUserUsernameLineEdit->setDisabled(false);
121         ui->setUserPasswordLineEdit->setDisabled(false);
122         //ui->setUserUsernameLineEdit->setText("");
123         //ui->setUserPasswordLineEdit->setText("");
124         ui->setUserUsernameLineEdit->clear();
125         ui->setUserPasswordLineEdit->clear();
126         this->username = ui->setUserUsernameLineEdit->text();
127         this->password = ui->setUserPasswordLineEdit->text();
128         ui->setUserPushButton->setText("Log in");
129         saveLogin( this->username, this->password);
130         emit logout();
131     }
132     else
133     {
134         this->username = ui->setUserUsernameLineEdit->text();
135         this->password = ui->setUserPasswordLineEdit->text();
136         saveLogin( this->username, this->password);
137         ui->setUserPushButton->setText("Log out");
138
139         if(this->username.compare(""))
140         {
141             emit userNameChanged();
142             //ui->setUserPushButton->setText("Log out");
143         }
144
145         else
146         {
147             QMessageBox::about(this, "Username field is empty", "Set username and log in again");
148             ui->setUserPushButton->setText("Log in");
149         }
150     }
151     // Save these also to usersettings
152     //saveLogin( this->username, this->password);
153
154
155
156     /*
157     // Set "Set/Change User" button text
158     if (this->username.length() > 0)
159     {
160         ui->setUserPushButton->setText("Log out");
161     }
162     else
163     {   // Username "cleared"
164         ui->setUserPushButton->setText("Log in");
165     }
166
167     emit userNameChanged();
168     */
169     //close();  //using close() hides popup-window which reports error from server
170 }
171
172 // Next 4 functions can be removed if Settingsdialog is implemented without
173 // own copy of username & password
174 void SettingsDialog::setUserName(QString username)
175 {
176     this->username = username;
177 }
178
179 void SettingsDialog::setPassword(QString password)
180 {
181     this->password = password;
182 }
183
184 QString SettingsDialog::getUserName()
185 {
186     return this->username;
187 }
188
189 QString SettingsDialog::getPassword()
190 {
191     return this->password;
192 }
193
194 void SettingsDialog::setLabelInfoToUser(QString infoText)
195 {
196     this->ui->labelInfoToUser->setText(infoText);
197 }
198
199 void SettingsDialog::usernameOk(bool isOk)
200 {
201     if (isOk)
202     {
203         ui->setUserPushButton->setText("Log out");
204         ui->setUserUsernameLineEdit->setDisabled(true);
205         ui->setUserPasswordLineEdit->setDisabled(true);
206     }
207
208     else
209     {
210         ui->setUserPushButton->setText("Log in");
211         ui->setUserUsernameLineEdit->clear();
212         ui->setUserPasswordLineEdit->clear();
213         this->username = ui->setUserUsernameLineEdit->text();
214         this->password = ui->setUserPasswordLineEdit->text();
215         saveLogin( this->username, this->password);
216     }
217 }