23 #include "cfgserverpasswords.h" 24 #include "ui_cfgserverpasswords.h" 26 #include "configuration/passwordscfg.h" 27 #include "configuration/serverpassword.h" 28 #include "gui/commongui.h" 29 #include "gui/helpers/datetablewidgetitem.h" 32 const int COL_PASS_PASSWORD = 0;
33 const int COL_PASS_LAST_GAME = 1;
34 const int COL_PASS_LAST_SERVER = 2;
35 const int COL_PASS_LAST_TIME = 3;
37 const int COL_SERV_GAME = 0;
38 const int COL_SERV_NAME = 1;
39 const int COL_SERV_ADDRESS = 2;
40 const int COL_SERV_LAST_TIME = 3;
42 const QString HIDDEN_PASS =
"***";
44 DClass<CFGServerPasswords> :
public Ui::CFGServerPasswords
47 bool bHidingPasswordsMode;
56 d->bHidingPasswordsMode =
true;
58 d->tablePasswords->sortItems(COL_PASS_LAST_TIME, Qt::DescendingOrder);
59 d->tablePasswords->setColumnWidth(COL_PASS_PASSWORD, 90);
60 d->tableServers->sortItems(COL_SERV_GAME, Qt::AscendingOrder);
61 d->tableServers->setColumnWidth(COL_SERV_GAME, 90);
62 d->lblServerLossWarning->setVisible(
false);
65 CFGServerPasswords::~CFGServerPasswords()
69 void CFGServerPasswords::addPasswordFromLineEdit()
71 QString phrase = d->lePassword->text();
72 d->lePassword->clear();
73 if (!phrase.isEmpty() && !isPassphraseInTable(phrase))
76 password.setPhrase(phrase);
77 addServerPasswordToTable(password);
81 void CFGServerPasswords::addServerPasswordToTable(
const ServerPassword &password)
83 int rowIndex = d->tablePasswords->rowCount();
84 d->tablePasswords->insertRow(rowIndex);
85 setPasswordInRow(rowIndex, password);
88 void CFGServerPasswords::clearTable(QTableWidget *table)
90 while (table->rowCount() > 0)
96 int CFGServerPasswords::findPassphraseInTable(
const QString &phrase)
98 for (
int i = 0; i < d->tablePasswords->rowCount(); ++i)
101 if (password.phrase() == phrase)
107 void CFGServerPasswords::hidePasswords()
109 d->btnRevealHideToggle->setText(tr(
"Reveal"));
110 d->bHidingPasswordsMode =
true;
111 d->lePassword->setEchoMode(QLineEdit::Password);
112 for (
int i = 0; i < d->tablePasswords->rowCount(); ++i)
114 QTableWidgetItem *item = d->tablePasswords->item(i, COL_PASS_PASSWORD);
115 item->setText(HIDDEN_PASS);
116 item->setToolTip(HIDDEN_PASS);
120 bool CFGServerPasswords::isPassphraseInTable(
const QString &phrase)
122 return findPassphraseInTable(phrase) >= 0;
125 void CFGServerPasswords::onPasswordTableCellChange(
int currentRow,
int currentColumn,
126 int previousRow,
int previousColumn)
128 Q_UNUSED(currentColumn);
129 Q_UNUSED(previousColumn);
130 if (currentRow != previousRow)
134 setServersInTable(serverPasswordFromRow(currentRow));
141 QList<ServerPassword> passwords;
142 for (
int i = 0; i < d->tablePasswords->rowCount(); ++i)
143 passwords << serverPasswordFromRow(i);
144 cfg.setServerPasswords(passwords);
145 cfg.setMaxNumberOfServersPerPassword(d->spinMaxPasswords->value());
150 if (row < 0 || row >= d->tablePasswords->rowCount())
152 return ServerPassword::deserializeQVariant(
153 d->tablePasswords->item(row, COL_PASS_PASSWORD)->data(Qt::UserRole));
156 void CFGServerPasswords::setPasswordInRow(
int row,
const ServerPassword &password)
159 bool wasSortingEnabled = d->tablePasswords->isSortingEnabled();
160 d->tablePasswords->setSortingEnabled(
false);
163 if (d->bHidingPasswordsMode)
164 phrase = HIDDEN_PASS;
166 phrase = password.phrase();
168 QTableWidgetItem *phraseItem = toolTipItem(phrase);
169 phraseItem->setData(Qt::UserRole, password.serializeQVariant());
170 d->tablePasswords->setItem(row, COL_PASS_PASSWORD, phraseItem);
172 d->tablePasswords->setItem(row, COL_PASS_LAST_GAME, toolTipItem(password.lastGame()));
173 d->tablePasswords->setItem(row, COL_PASS_LAST_SERVER, toolTipItem(password.lastServerName()));
176 dateItem->setToolTip(dateItem->displayedText());
177 d->tablePasswords->setItem(row, COL_PASS_LAST_TIME, dateItem);
180 d->tablePasswords->setSortingEnabled(wasSortingEnabled);
181 d->tablePasswords->resizeRowsToContents();
184 void CFGServerPasswords::setServersInTable(
const ServerPassword &password)
186 clearTable(d->tableServers);
188 d->tableServers->setSortingEnabled(
false);
191 int rowIndex = d->tableServers->rowCount();
192 d->tableServers->insertRow(rowIndex);
194 QTableWidgetItem *gameItem = toolTipItem(server.game());
195 gameItem->setData(Qt::UserRole, server.serializeQVariant());
196 d->tableServers->setItem(rowIndex, COL_SERV_GAME, gameItem);
198 d->tableServers->setItem(rowIndex, COL_SERV_NAME, toolTipItem(server.name()));
199 QString address = QString(
"%1:%2").arg(server.address()).arg(server.port());
200 d->tableServers->setItem(rowIndex, COL_SERV_ADDRESS, toolTipItem(address));
203 dateItem->setToolTip(dateItem->displayedText());
204 d->tableServers->setItem(rowIndex, COL_SERV_LAST_TIME, dateItem);
207 d->tableServers->setSortingEnabled(
true);
208 d->tableServers->resizeRowsToContents();
211 void CFGServerPasswords::showServerLossWarningIfNecessary()
214 d->lblServerLossWarning->setVisible(d->spinMaxPasswords->value() <
215 cfg.maxNumberOfServersPerPassword());
220 clearTable(d->tablePasswords);
221 clearTable(d->tableServers);
226 addServerPasswordToTable(pass);
228 d->spinMaxPasswords->setValue(cfg.maxNumberOfServersPerPassword());
231 void CFGServerPasswords::removeSelectedPasswords()
233 CommonGUI::removeSelectedRowsFromQTableWidget(d->tablePasswords);
236 void CFGServerPasswords::removeSelectedServers()
238 QList<ServerPasswordSummary> servers;
239 for (QTableWidgetItem *item : d->tableServers->selectedItems())
241 if (item->column() == COL_SERV_GAME)
242 servers << ServerPasswordSummary::deserializeQVariant(item->data(Qt::UserRole));
244 ServerPassword currentPassword = serverPasswordFromRow(d->tablePasswords->currentRow());
247 currentPassword.removeServer(server.game(), server.address(), server.port());
249 updatePassword(currentPassword);
250 CommonGUI::removeSelectedRowsFromQTableWidget(d->tableServers);
253 void CFGServerPasswords::revealPasswords()
255 d->btnRevealHideToggle->setText(tr(
"Hide"));
256 d->lePassword->setEchoMode(QLineEdit::Normal);
257 d->bHidingPasswordsMode =
false;
258 for (
int i = 0; i < d->tablePasswords->rowCount(); ++i)
260 QTableWidgetItem *item = d->tablePasswords->item(i, COL_PASS_PASSWORD);
262 item->setText(password.phrase());
263 item->setToolTip(password.phrase());
267 void CFGServerPasswords::toggleRevealHide()
269 if (d->bHidingPasswordsMode)
275 QTableWidgetItem *CFGServerPasswords::toolTipItem(
const QString &contents)
277 auto item =
new QTableWidgetItem(contents);
278 item->setToolTip(contents);
282 void CFGServerPasswords::updatePassword(
const ServerPassword &password)
284 int row = findPassphraseInTable(password.phrase());
286 setPasswordInRow(row, password);
288 addServerPasswordToTable(password);
void readSettings() override
Reimplement this to read settings from config into widgets.
void saveSettings() override
Reimplement this to write settings to config from widgets.
Base class for configuration pages.