-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.cpp
432 lines (352 loc) · 13.6 KB
/
user.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#include "user.h"
#include <QCryptographicHash>
#include <QDebug>
#include <QDialog>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QSpacerItem>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
User::User(QObject *parent) : QObject(parent),
_username("Guest"),
_firstName(""),
_lastName(""),
_email(""),
_joinDateTime(QDateTime::currentDateTime()),
_db(NULL)
{
}
User::~User()
{
}
const QString & User::getUsername()
{
return _username;
}
const QString User::getJoinDateTime()
{
return _joinDateTime.toString("yyyy-MM-dd hh:mm:ss");
}
void User::setMediaDatabase(mediaDatabase *database)
{
_db = database;
}
void User::presentLoginWindow()
{
if(_db != NULL)
{
UserLoginDialog login(_db);
connect(&login, SIGNAL(userDataReceived(const QString &, const QString &,
const QString &, const QString &,
const QString &)),
this, SLOT(userDataReceived(const QString &, const QString &,
const QString &, const QString &,
const QString &)));
login.exec();
}
}
void User::presentCreateUserWindow()
{
if(_db != NULL)
{
CreateUserDialog create(_db);
connect(&create, SIGNAL(userDataReceived(const QString &, const QString &,
const QString &, const QString &,
const QString &)),
this, SLOT(userDataReceived(const QString &, const QString &,
const QString &, const QString &,
const QString &)));
create.exec();
}
}
void User::presentUserInformationWindow()
{
const QString joinDateTime = _joinDateTime.toString("dddd, MMMM dd, yyyy");
UserInformation userInfo(_username, _firstName + " " + _lastName, _email, joinDateTime, _db);
userInfo.exec();
}
void User::userDataReceived(const QString & username, const QString &firstName, const QString &lastName, const QString &email, const QString & joinDateTime)
{
_username = username;
_firstName = firstName;
_lastName = lastName;
_email = email;
_joinDateTime = QDateTime::fromString(joinDateTime, "yyyy-MM-dd hh:mm:ss");
}
UserLoginDialog::UserLoginDialog(mediaDatabase *database, QWidget *parent) : QDialog(parent),
_usernameLine(new QLineEdit("")),
_passwordLine(new QLineEdit("")),
_db(database)
{
prepareLoginLayout();
this->setWindowTitle("Login");
_usernameLine->setPlaceholderText("Username");
_passwordLine->setPlaceholderText("Password");
_passwordLine->setEchoMode(QLineEdit::Password);
}
UserLoginDialog::~UserLoginDialog()
{
}
void UserLoginDialog::loginButtonHasBeenClicked()
{
const QVector<QString> * results = _db->login(_usernameLine->text(),
QCryptographicHash::hash(_passwordLine->text().toStdString().c_str(),
QCryptographicHash::Sha3_512)
);
if(results->size())
{
QString fname = results->at(0), lname = results->at(1), email = results->at(2), joinDateTime = results->at(3);
emit userDataReceived(_usernameLine->text(), fname, lname, email, joinDateTime);
this->close();
}
else
{
qDebug() << "Could not log in.";
}
}
void UserLoginDialog::prepareLoginLayout()
{
//Initialize Layouts
QHBoxLayout * usernameLayout = new QHBoxLayout;
QHBoxLayout * passwordLayout = new QHBoxLayout;
QHBoxLayout * loginButtonLayout = new QHBoxLayout;
QVBoxLayout * mainLayout = new QVBoxLayout;
//Initialize objects needed
QLabel * usernameLabel = new QLabel("Username:");
QLabel * passwordLabel = new QLabel("Password:");
QPushButton * loginButton = new QPushButton("Login");
connect(loginButton, SIGNAL(clicked()), this, SLOT(loginButtonHasBeenClicked()));
//Set loginButton default width
loginButton->setMaximumWidth(40);
//Put stuff in layouts
usernameLayout->addWidget(usernameLabel);
usernameLayout->addWidget(_usernameLine);
passwordLayout->addWidget(passwordLabel);
passwordLayout->addWidget(_passwordLine);
loginButtonLayout->addSpacerItem(new QSpacerItem(20,20));
loginButtonLayout->addWidget(loginButton);
mainLayout->addLayout(usernameLayout);
mainLayout->addLayout(passwordLayout);
mainLayout->addLayout(loginButtonLayout);
this->setLayout(mainLayout);
}
CreateUserDialog::CreateUserDialog(mediaDatabase *database, QWidget *parent) : QDialog(parent),
_usernameLine(new QLineEdit("")),
_passwordLine(new QLineEdit("")),
_repeatPasswordLine(new QLineEdit("")),
_firstNameLine(new QLineEdit("")),
_lastNameLine(new QLineEdit("")),
_emailLine(new QLineEdit("")),
_db(database),
_usernameAvailableLabel(new QLabel)
{
prepareCreationLayout();
this->setWindowTitle("Create User");
_usernameLine->setPlaceholderText("Username");
_passwordLine->setPlaceholderText("Password");
_repeatPasswordLine->setPlaceholderText("Repeat password");
_firstNameLine->setPlaceholderText("First Name");
_lastNameLine->setPlaceholderText("Last Name");
_emailLine->setPlaceholderText("youremail@example.ninja");
_passwordLine->setEchoMode(QLineEdit::Password);
_repeatPasswordLine->setEchoMode(QLineEdit::Password);
}
CreateUserDialog::~CreateUserDialog()
{
}
void CreateUserDialog::checkUsernameButtonHasBeenClicked()
{
if(_db->userExists(_usernameLine->text()) || _usernameLine->text() == "")
{
_usernameAvailableLabel->setText(QString("<html>✖</html>"));
_usernameAvailableLabel->setStyleSheet("QLabel {color: red;}");
}
else
{
_usernameAvailableLabel->setText(QString("<html>✔</html>"));
_usernameAvailableLabel->setStyleSheet("QLabel {color: green;}");
}
}
void CreateUserDialog::createUserButtonHasBeenClicked()
{
if(passwordsAreEqual() && usernameNotEmpty())
{
const QString joinDateTime = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
if(_db->createUser(_usernameLine->text(),
QCryptographicHash::hash(_passwordLine->text().toStdString().c_str(), QCryptographicHash::Sha3_512),
_firstNameLine->text(),
_lastNameLine->text(),
_emailLine->text(),
joinDateTime))
{
emit userDataReceived(_usernameLine->text(), _firstNameLine->text(), _lastNameLine->text(), _emailLine->text(), joinDateTime);
}
this->close();
}
else
{
}
}
void CreateUserDialog::prepareCreationLayout()
{
//Initialize layouts
QHBoxLayout * usernameLayout = new QHBoxLayout;
QHBoxLayout * passwordLayout = new QHBoxLayout;
QHBoxLayout * repeatPasswordLayout = new QHBoxLayout;
QHBoxLayout * fNameLayout = new QHBoxLayout;
QHBoxLayout * lNameLayout = new QHBoxLayout;
QHBoxLayout * emailLayout = new QHBoxLayout;
QHBoxLayout * createUserButtonLayout = new QHBoxLayout;
QVBoxLayout * mainLayout = new QVBoxLayout;
//Initialize objects needed
QLabel * usernameLabel = new QLabel("Username:");
QLabel * passwordLabel = new QLabel("Password:");
QLabel * repeatPasswordLabel = new QLabel("Repeat Password:");
QLabel * fNameLabel = new QLabel("First Name:");
QLabel * lNameLabel = new QLabel("Last Name:");
QLabel * emailLabel = new QLabel("Email:");
QPushButton * checkUsernameButton = new QPushButton("Check It!");
connect(checkUsernameButton, SIGNAL(clicked()), this, SLOT(checkUsernameButtonHasBeenClicked()));
QPushButton * createUserButton = new QPushButton("Create");
connect(createUserButton, SIGNAL(clicked()), this, SLOT(createUserButtonHasBeenClicked()));
//Set widths for buttons
checkUsernameButton->setMaximumWidth(60);
createUserButton->setMaximumWidth(50);
//Put stuff in layouts
usernameLayout->addWidget(usernameLabel);
usernameLayout->addWidget(_usernameLine);
usernameLayout->addWidget(checkUsernameButton);
usernameLayout->addWidget(_usernameAvailableLabel);
passwordLayout->addWidget(passwordLabel);
passwordLayout->addWidget(_passwordLine);
repeatPasswordLayout->addWidget(repeatPasswordLabel);
repeatPasswordLayout->addWidget(_repeatPasswordLine);
fNameLayout->addWidget(fNameLabel);
fNameLayout->addWidget(_firstNameLine);
lNameLayout->addWidget(lNameLabel);
lNameLayout->addWidget(_lastNameLine);
emailLayout->addWidget(emailLabel);
emailLayout->addWidget(_emailLine);
createUserButtonLayout->addWidget(createUserButton);
mainLayout->addLayout(usernameLayout);
mainLayout->addLayout(passwordLayout);
mainLayout->addLayout(repeatPasswordLayout);
mainLayout->addLayout(fNameLayout);
mainLayout->addLayout(lNameLayout);
mainLayout->addLayout(emailLayout);
mainLayout->addLayout(createUserButtonLayout);
this->setLayout(mainLayout);
}
bool CreateUserDialog::passwordsAreEqual()
{
return (_passwordLine->text() == _repeatPasswordLine->text());
}
bool CreateUserDialog::usernameNotEmpty()
{
return _usernameLine->text().size();
}
UserInformation::UserInformation(const QString username, const QString name,
const QString email, const QString membersince, mediaDatabase * database, QWidget *parent) :
QDialog(parent),
_username(username),
_name(name),
_email(email),
_membersince(membersince),
_db(database)
{
prepareUserInfoLayout();
this->setWindowTitle("User Info");
}
UserInformation::~UserInformation()
{
}
void UserInformation::okButtonHasBeenPressed()
{
this->close();
}
void UserInformation::prepareUserInfoLayout()
{
//Initialize Layouts
QHBoxLayout * usernameLayout = new QHBoxLayout;
QHBoxLayout * nameLayout = new QHBoxLayout;
QHBoxLayout * emailLayout = new QHBoxLayout;
QHBoxLayout * membersinceLayout = new QHBoxLayout;
QHBoxLayout * okButtonLayout = new QHBoxLayout;
QVBoxLayout * leftLayout = new QVBoxLayout;
QHBoxLayout * mainLayout = new QHBoxLayout;
//Initialize Objects Needed
QLabel * usernameLabel = new QLabel("Username: " + _username);
QLabel * nameLabel = new QLabel("Name: " + _name);
QLabel * emailLabel = new QLabel("Email: " + _email);
QLabel * membersinceLabel = new QLabel("Member Since: " + _membersince);
QPushButton * okButton = new QPushButton("Ok");
connect(okButton, SIGNAL(clicked()), this, SLOT(okButtonHasBeenPressed()));
//Put stuff in layouts
usernameLayout->addWidget(usernameLabel);
nameLayout->addWidget(nameLabel);
emailLayout->addWidget(emailLabel);
membersinceLayout->addWidget(membersinceLabel);
okButtonLayout->addWidget(okButton);
leftLayout->addLayout(usernameLayout);
leftLayout->addLayout(nameLayout);
leftLayout->addLayout(emailLayout);
leftLayout->addLayout(membersinceLayout);
leftLayout->addLayout(okButtonLayout);
mainLayout->addLayout(leftLayout);
mainLayout->addLayout(prepareFavoritesLayout());
this->setLayout(mainLayout);
}
QHBoxLayout * UserInformation::prepareFavoritesLayout()
{
QHBoxLayout * favoritesLayout = new QHBoxLayout;
if(_db != NULL)
{
favoritesLayout->addLayout(prepareFavoriteArtistLayout());
favoritesLayout->addLayout(prepareFavoriteAlbumLayout());
favoritesLayout->addLayout(prepareFavoriteSongLayout());
}
return favoritesLayout;
}
QVBoxLayout * UserInformation::prepareFavoriteArtistLayout()
{
const QVector<QString> * info = _db->getFavoriteArtist(_username);
QVBoxLayout * favoriteArtistLayout = new QVBoxLayout;
QLabel * titleLabel = new QLabel("<b><u>Favorite Artist:</u></b>");
QLabel * artistLabel = new QLabel("Artist: " + info->at(0));
QLabel * playsLabel = new QLabel("Total Plays: " + info->at(1));
favoriteArtistLayout->addWidget(titleLabel);
favoriteArtistLayout->addWidget(artistLabel);
favoriteArtistLayout->addWidget(playsLabel);
return favoriteArtistLayout;
}
QVBoxLayout * UserInformation::prepareFavoriteAlbumLayout()
{
const QVector<QString> * info = _db->getFavoriteAlbum(_username);
QVBoxLayout * favoriteAlbumLayout = new QVBoxLayout;
QLabel * titleLabel = new QLabel("<b><u>Favorite Album:</u></b>");
QLabel * artistLabel = new QLabel("Artist: " + info->at(0));
QLabel * albumLabel = new QLabel("Album: " + info->at(1));
QLabel * playsLabel = new QLabel("Total Plays: " + info->at(2));
favoriteAlbumLayout->addWidget(titleLabel);
favoriteAlbumLayout->addWidget(artistLabel);
favoriteAlbumLayout->addWidget(albumLabel);
favoriteAlbumLayout->addWidget(playsLabel);
return favoriteAlbumLayout;
}
QVBoxLayout * UserInformation::prepareFavoriteSongLayout()
{
const QVector<QString> * info = _db->getFavoriteSong(_username);
QVBoxLayout * favoriteSongLayout = new QVBoxLayout;
QLabel * titleLabel = new QLabel("<b><u>Favorite Song:</u></b>");
QLabel * artistLabel = new QLabel("Artist: " + info->at(0));
QLabel * albumLabel = new QLabel("Album: " + info->at(1));
QLabel * songLabel = new QLabel("Song: " + info->at(2));
QLabel * playsLabel = new QLabel("Total Plays: " + info->at(3));
favoriteSongLayout->addWidget(titleLabel);
favoriteSongLayout->addWidget(artistLabel);
favoriteSongLayout->addWidget(albumLabel);
favoriteSongLayout->addWidget(songLabel);
favoriteSongLayout->addWidget(playsLabel);
return favoriteSongLayout;
}