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 = NULL;
48 QSettings *PasswordsCfg::settings = NULL;
61 assert(ini != NULL &&
"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 qSort(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 == NULL &&
"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)
129 storeServerPasswords(allPasswords);
135 if (phrase.isEmpty())
144 serverSummary.setTime(QDateTime::currentDateTime());
145 serverInfo.setServerSummary(serverSummary);
148 QList<ServerPassword> allPasswords = serverPasswords();
149 QMutableListIterator<ServerPassword> it(allPasswords);
154 if (existingPass.phrase() == phrase)
156 if (serverInfo.isValid())
158 existingPass.addServer(serverInfo);
160 setServerPasswords(allPasswords);
166 pass.setPhrase(phrase);
167 pass.addServer(serverInfo);
168 allPasswords << pass;
169 storeServerPasswords(allPasswords);
174 return s1.time() > s2.time();
177 void PasswordsCfg::setHidePasswords(
bool val)
179 gConfig.doomseeker.bHidePasswords = val;
182 QList<ServerPassword> PasswordsCfg::serverPasswords()
const
184 QList<ServerPassword> result;
185 QVariantList vars = d->section[SERVER_PASSWORDS_KEY].value().toList();
186 foreach (
const QVariant& var, vars)
188 result << ServerPassword::deserializeQVariant(var);
193 QStringList PasswordsCfg::serverPhrases()
const
198 result << pass.phrase();
203 void PasswordsCfg::setMaxNumberOfServersPerPassword(
int val)
205 bool shallCut = val < maxNumberOfServersPerPassword();
206 d->section.setValue(MAX_NUMBER_OF_SERVERS_PER_PASSWORD_KEY, val);
213 void PasswordsCfg::setRememberConnectPhrase(
bool val)
215 return d->section.setValue(REMEMBER_CONNECT_PASSWORD, val);
218 void PasswordsCfg::setServerPasswords(
const QList<ServerPassword>& val)
220 QList<ServerPassword> passwords = val;
221 cutServers(passwords);
222 storeServerPasswords(passwords);
225 void PasswordsCfg::storeServerPasswords(
const QList<ServerPassword>& val)
230 vars << obj.serializeQVariant();
232 d->section.setValue(SERVER_PASSWORDS_KEY, vars);
244 foreach (
const ServerPassword& potentialPassword, serverPasswords())
247 ServerPasswordSummary candidate = potentialPassword.mostSimilarServer(serverSummary, &newSimilarity);
248 if (candidate.isValid())
250 if (newSimilarity > bestFit.
similarity(serverSummary))
253 password = potentialPassword;
255 else if (qFuzzyCompare(newSimilarity, bestFit.
similarity(serverSummary))
256 && candidate.time() > bestFit.time())
259 password = potentialPassword;
A representation of a server for a given game.
void setType(const QString &val)
One of ServerPasswordType consts or custom.
float similarity(const ServerPasswordSummary &other) const
Similarity to the 'other' server; between 0.0 and 1.0.
INI section representation.
void saveServerPhrase(const QString &phrase, const Server *server, const QString &type)
Stores server phrase in persistence along its use case.
ServerPassword suggestPassword(const Server *server, const QString &type)
Suggests best password basing on several criteria.