23 #include "passwordscfg.h"
25 #include "configuration/doomseekerconfig.h"
26 #include "configuration/serverpassword.h"
27 #include "configuration/serverpasswordsummary.h"
29 #include "ini/inisection.h"
30 #include "ini/inivariable.h"
31 #include "ini/settingsproviderqt.h"
32 #include "serverapi/server.h"
33 #include "serverapi/serversummary.h"
37 const QString SECTION_NAME =
"Passwords";
39 const QString MAX_NUMBER_OF_SERVERS_PER_PASSWORD_KEY =
"MaxNumberOfServersPerPassword";
40 const QString REMEMBER_CONNECT_PASSWORD =
"RememberConnectPassword";
41 const QString SERVER_PASSWORDS_KEY =
"ServerPasswords";
47 Ini *PasswordsCfg::ini =
nullptr;
48 QSettings *PasswordsCfg::settings =
nullptr;
61 assert(ini !=
nullptr &&
"instantiated PasswordsCfg() without initing ini");
62 d->section = ini->section(SECTION_NAME);
65 PasswordsCfg::~PasswordsCfg()
69 void PasswordsCfg::cutServers(QList<ServerPassword> &passwords)
const
71 QMutableListIterator<ServerPassword> it(passwords);
75 QList<ServerPasswordSummary> sortedServers = password.servers();
76 std::sort(sortedServers.begin(), sortedServers.end(), serverDateDescending);
77 password.setServers(sortedServers.mid(0, maxNumberOfServersPerPassword()));
81 void PasswordsCfg::cutStoredServers()
83 QList<ServerPassword> passwords = serverPasswords();
84 cutServers(passwords);
85 storeServerPasswords(passwords);
88 void PasswordsCfg::initIni(
const QString &path)
90 assert(ini ==
nullptr &&
"tried to re-init password ini");
93 qDebug() <<
"Error: tried to re-init password ini";
96 settings =
new QSettings(path, QSettings::IniFormat);
102 bool PasswordsCfg::isHidingPasswords()
const
104 return gConfig.doomseeker.bHidePasswords;
107 bool PasswordsCfg::isRememberingConnectPhrase()
const
109 return d->section.value(REMEMBER_CONNECT_PASSWORD,
false).toBool();
112 int PasswordsCfg::maxNumberOfServersPerPassword()
const
114 return d->section.value(MAX_NUMBER_OF_SERVERS_PER_PASSWORD_KEY, 5).toInt();
117 void PasswordsCfg::removeServerPhrase(
const QString &phrase)
119 QList<ServerPassword> allPasswords = serverPasswords();
120 QMutableListIterator<ServerPassword> it(allPasswords);
124 if (existingPass.phrase() == phrase)
127 storeServerPasswords(allPasswords);
133 if (phrase.isEmpty())
137 if (server !=
nullptr)
140 serverSummary.setTime(QDateTime::currentDateTime());
141 serverInfo.setServerSummary(serverSummary);
144 QList<ServerPassword> allPasswords = serverPasswords();
145 QMutableListIterator<ServerPassword> it(allPasswords);
150 if (existingPass.phrase() == phrase)
152 if (serverInfo.isValid())
153 existingPass.addServer(serverInfo);
154 setServerPasswords(allPasswords);
160 pass.setPhrase(phrase);
161 pass.addServer(serverInfo);
162 allPasswords << pass;
163 storeServerPasswords(allPasswords);
168 return s1.time() > s2.time();
171 void PasswordsCfg::setHidePasswords(
bool val)
173 gConfig.doomseeker.bHidePasswords = val;
176 QList<ServerPassword> PasswordsCfg::serverPasswords()
const
178 QList<ServerPassword> result;
179 QVariantList vars = d->section[SERVER_PASSWORDS_KEY].value().toList();
180 for (
const QVariant &var : vars)
182 result << ServerPassword::deserializeQVariant(var);
187 QStringList PasswordsCfg::serverPhrases()
const
192 result << pass.phrase();
197 void PasswordsCfg::setMaxNumberOfServersPerPassword(
int val)
199 bool shallCut = val < maxNumberOfServersPerPassword();
200 d->section.setValue(MAX_NUMBER_OF_SERVERS_PER_PASSWORD_KEY, val);
205 void PasswordsCfg::setRememberConnectPhrase(
bool val)
207 return d->section.setValue(REMEMBER_CONNECT_PASSWORD, val);
210 void PasswordsCfg::setServerPasswords(
const QList<ServerPassword> &val)
212 QList<ServerPassword> passwords = val;
213 cutServers(passwords);
214 storeServerPasswords(passwords);
217 void PasswordsCfg::storeServerPasswords(
const QList<ServerPassword> &val)
222 vars << obj.serializeQVariant();
224 d->section.setValue(SERVER_PASSWORDS_KEY, vars);
239 ServerPasswordSummary candidate = potentialPassword.mostSimilarServer(serverSummary, &newSimilarity);
240 if (candidate.isValid())
242 if (newSimilarity > bestFit.
similarity(serverSummary))
245 password = potentialPassword;
247 else if (qFuzzyCompare(newSimilarity, bestFit.
similarity(serverSummary))
248 && candidate.time() > bestFit.time())
251 password = potentialPassword;