Skip to content

Commit b4ce15e

Browse files
Refactored and added comments
1 parent 6f89bce commit b4ce15e

2 files changed

Lines changed: 43 additions & 39 deletions

File tree

src/application/utility/automatic_login_helper.cpp

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ std::optional<UserData> AutomaticLoginHelper::tryAutomaticUserLoading()
3333
{
3434
auto jsonData = getAutomaticLoginFileData();
3535
// Check if the data contains the "firstName" property since it should be
36-
// set if the user was already saved. We can't check for "no saved data",
36+
// set if the user was already saved. We can't check for no-saved-data,
3737
// since the authentication data is saved before the user data, thus there
3838
// is already some data in the file, even though the user was not saved.
3939
if(!jsonData.has_value() || !jsonData.value().contains("firstName"))
@@ -62,59 +62,29 @@ std::optional<UserData> AutomaticLoginHelper::tryAutomaticUserLoading()
6262
void AutomaticLoginHelper::saveAuthenticationData(
6363
const AuthenticationData& authData)
6464
{
65-
auto currentFolder = QDir::current();
66-
auto automaticLoginFilePath = currentFolder.filePath(m_automaticLoginFile);
67-
65+
// Make sure to clear the previous authentication data before saving the new
6866
clearAutomaticLoginData();
69-
QFile file(automaticLoginFilePath);
70-
if(!file.open(QFile::WriteOnly | QIODevice::Text))
71-
{
72-
qDebug() << "...";
73-
return;
74-
}
67+
7568

7669
QJsonObject jsonAuthData {
7770
{ "email", authData.email },
7871
{ "token", authData.token },
7972
};
80-
81-
QJsonDocument jsonDoc(jsonAuthData);
82-
file.write(jsonDoc.toJson((QJsonDocument::Indented)));
73+
saveDataToAutomaticLoginFile(jsonAuthData);
8374
}
8475

8576
void AutomaticLoginHelper::saveUserData(const UserData& userData)
8677
{
87-
// User data can only be appended to the file, if the auth data already
88-
// exists. They would make no sense without each other.
78+
// User data can only be appended to the file, if the authentication data
79+
// already exists. So check if the file already contains the auth data.
8980
auto existingAuthData = getAutomaticLoginFileData();
9081
if(!existingAuthData.has_value())
9182
return;
9283

93-
auto currentFolder = QDir::current();
94-
auto automaticLoginFilePath = currentFolder.filePath(m_automaticLoginFile);
95-
QFile file(automaticLoginFilePath);
96-
if(!file.open(QFile::WriteOnly | QIODevice::Text))
97-
{
98-
qDebug() << "...";
99-
return;
100-
}
101-
102-
auto jsonAuthData = existingAuthData.value();
103-
jsonAuthData.insert("firstName", userData.firstName);
104-
jsonAuthData.insert("lastName", userData.lastName);
105-
jsonAuthData.insert("email", userData.email);
84+
auto jsonUserData = existingAuthData.value();
85+
appendUserDataToJsonObject(jsonUserData, std::move(userData));
10686

107-
// Add tags
108-
QJsonArray tags;
109-
for(const auto& tag : userData.tags)
110-
{
111-
auto obj = QJsonDocument::fromJson(tag.toJson()).object();
112-
tags.append(QJsonValue::fromVariant(obj));
113-
}
114-
jsonAuthData.insert("tags", tags);
115-
116-
QJsonDocument jsonDoc(jsonAuthData);
117-
file.write(jsonDoc.toJson((QJsonDocument::Indented)));
87+
saveDataToAutomaticLoginFile(jsonUserData);
11888
}
11989

12090
void AutomaticLoginHelper::clearAutomaticLoginData()
@@ -148,4 +118,36 @@ std::optional<QJsonObject> AutomaticLoginHelper::getAutomaticLoginFileData()
148118
return jsonDoc.object();
149119
}
150120

121+
void AutomaticLoginHelper::appendUserDataToJsonObject(QJsonObject& jsonObject,
122+
UserData userData)
123+
{
124+
jsonObject.insert("firstName", userData.firstName);
125+
jsonObject.insert("lastName", userData.lastName);
126+
jsonObject.insert("email", userData.email);
127+
128+
QJsonArray tags;
129+
for(const auto& tag : userData.tags)
130+
{
131+
auto obj = QJsonDocument::fromJson(tag.toJson()).object();
132+
tags.append(QJsonValue::fromVariant(obj));
133+
}
134+
jsonObject.insert("tags", tags);
135+
}
136+
137+
void AutomaticLoginHelper::saveDataToAutomaticLoginFile(const QJsonObject& data)
138+
{
139+
auto currentFolder = QDir::current();
140+
auto automaticLoginFilePath = currentFolder.filePath(m_automaticLoginFile);
141+
142+
QFile file(automaticLoginFilePath);
143+
if(!file.open(QFile::WriteOnly | QIODevice::Text))
144+
{
145+
qDebug() << "Failed Opening automatic login file for saving data";
146+
return;
147+
}
148+
149+
QJsonDocument jsonDoc(data);
150+
file.write(jsonDoc.toJson((QJsonDocument::Indented)));
151+
}
152+
151153
} // namespace application::utility

src/application/utility/automatic_login_helper.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ class AutomaticLoginHelper
4444

4545
private:
4646
std::optional<QJsonObject> getAutomaticLoginFileData();
47+
void appendUserDataToJsonObject(QJsonObject& jsonObject, UserData userData);
48+
void saveDataToAutomaticLoginFile(const QJsonObject& data);
4749

4850
QString m_automaticLoginFile = "autologin.txt";
4951
};

0 commit comments

Comments
 (0)