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 static const int COL_PASS_PASSWORD = 0;
33 static const int COL_PASS_LAST_GAME = 1;
34 static const int COL_PASS_LAST_SERVER = 2;
35 static const int COL_PASS_LAST_TIME = 3;
37 static const int COL_SERV_GAME = 0;
38 static const int COL_SERV_NAME = 1;
39 static const int COL_SERV_ADDRESS = 2;
40 static const int COL_SERV_LAST_TIME = 3;
42 static const QString HIDDEN_PASS =
"***";
44 DClass<CFGServerPasswords> :
public Ui::CFGServerPasswords
57 d->tablePasswords->sortItems(COL_PASS_LAST_TIME, Qt::DescendingOrder);
58 d->tablePasswords->setColumnWidth(COL_PASS_PASSWORD, 90);
59 d->tablePasswords->setColumnWidth(COL_PASS_LAST_TIME, 130);
60 QHeaderView *headerPasswords = d->tablePasswords->horizontalHeader();
61 headerPasswords->setSectionResizeMode(COL_PASS_LAST_SERVER, QHeaderView::Stretch);
63 d->tableServers->sortItems(COL_SERV_GAME, Qt::AscendingOrder);
64 d->tableServers->setColumnWidth(COL_SERV_GAME, 90);
65 d->tableServers->setColumnWidth(COL_SERV_ADDRESS, 120);
66 d->tableServers->setColumnWidth(COL_PASS_LAST_TIME, 130);
67 QHeaderView *headerServers = d->tableServers->horizontalHeader();
68 headerServers->setSectionResizeMode(COL_SERV_NAME, QHeaderView::Stretch);
70 d->selectedPasswordPanel->setEnabled(
false);
71 d->lblServerLossWarning->setVisible(
false);
74 CFGServerPasswords::~CFGServerPasswords()
78 void CFGServerPasswords::addPasswordFromLineEdit()
80 QString phrase = d->lePassword->text();
81 d->lePassword->clear();
82 if (!phrase.isEmpty() && !isPassphraseInTable(phrase))
85 password.setPhrase(phrase);
86 addServerPasswordToTable(password);
90 void CFGServerPasswords::addServerPasswordToTable(
const ServerPassword &password)
92 int rowIndex = d->tablePasswords->rowCount();
93 d->tablePasswords->insertRow(rowIndex);
94 setPasswordInRow(rowIndex, password);
97 void CFGServerPasswords::clearTable(QTableWidget *table)
99 while (table->rowCount() > 0)
105 int CFGServerPasswords::findPassphraseInTable(
const QString &phrase)
107 for (
int i = 0; i < d->tablePasswords->rowCount(); ++i)
110 if (password.phrase() == phrase)
116 void CFGServerPasswords::hidePasswords()
118 d->cbHide->setChecked(
true);
119 d->lePassword->setEchoMode(QLineEdit::Password);
120 for (
int i = 0; i < d->tablePasswords->rowCount(); ++i)
122 QTableWidgetItem *item = d->tablePasswords->item(i, COL_PASS_PASSWORD);
123 item->setText(HIDDEN_PASS);
124 item->setToolTip(HIDDEN_PASS);
128 bool CFGServerPasswords::isPassphraseInTable(
const QString &phrase)
130 return findPassphraseInTable(phrase) >= 0;
133 void CFGServerPasswords::onPasswordTableCellChange(
int currentRow,
int currentColumn,
134 int previousRow,
int previousColumn)
136 Q_UNUSED(currentColumn);
137 Q_UNUSED(previousColumn);
138 if (currentRow != previousRow)
142 setServersInTable(serverPasswordFromRow(currentRow));
149 QList<ServerPassword> passwords;
150 for (
int i = 0; i < d->tablePasswords->rowCount(); ++i)
151 passwords << serverPasswordFromRow(i);
152 cfg.setServerPasswords(passwords);
153 cfg.setMaxNumberOfServersPerPassword(d->spinMaxPasswords->value());
158 if (row < 0 || row >= d->tablePasswords->rowCount())
160 return ServerPassword::deserializeQVariant(
161 d->tablePasswords->item(row, COL_PASS_PASSWORD)->data(Qt::UserRole));
164 void CFGServerPasswords::setPasswordInRow(
int row,
const ServerPassword &password)
167 bool wasSortingEnabled = d->tablePasswords->isSortingEnabled();
168 d->tablePasswords->setSortingEnabled(
false);
171 if (d->cbHide->isChecked())
172 phrase = HIDDEN_PASS;
174 phrase = password.phrase();
176 QTableWidgetItem *phraseItem = toolTipItem(phrase);
177 phraseItem->setData(Qt::UserRole, password.serializeQVariant());
178 d->tablePasswords->setItem(row, COL_PASS_PASSWORD, phraseItem);
180 d->tablePasswords->setItem(row, COL_PASS_LAST_GAME, toolTipItem(password.lastGame()));
181 d->tablePasswords->setItem(row, COL_PASS_LAST_SERVER, toolTipItem(password.lastServerName()));
184 dateItem->setToolTip(dateItem->displayedText());
185 d->tablePasswords->setItem(row, COL_PASS_LAST_TIME, dateItem);
188 d->tablePasswords->setSortingEnabled(wasSortingEnabled);
189 d->tablePasswords->resizeRowsToContents();
192 void CFGServerPasswords::setServersInTable(
const ServerPassword &password)
194 clearTable(d->tableServers);
195 d->selectedPasswordPanel->setEnabled(password.
isValid());
200 d->tableServers->setSortingEnabled(
false);
203 int rowIndex = d->tableServers->rowCount();
204 d->tableServers->insertRow(rowIndex);
206 QTableWidgetItem *gameItem = toolTipItem(server.game());
207 gameItem->setData(Qt::UserRole, server.serializeQVariant());
208 d->tableServers->setItem(rowIndex, COL_SERV_GAME, gameItem);
210 d->tableServers->setItem(rowIndex, COL_SERV_NAME, toolTipItem(server.name()));
211 QString address = QString(
"%1:%2").arg(server.address()).arg(server.port());
212 d->tableServers->setItem(rowIndex, COL_SERV_ADDRESS, toolTipItem(address));
215 dateItem->setToolTip(dateItem->displayedText());
216 d->tableServers->setItem(rowIndex, COL_SERV_LAST_TIME, dateItem);
219 d->tableServers->setSortingEnabled(
true);
220 d->tableServers->resizeRowsToContents();
223 void CFGServerPasswords::showServerLossWarningIfNecessary()
226 d->lblServerLossWarning->setVisible(d->spinMaxPasswords->value() <
227 cfg.maxNumberOfServersPerPassword());
232 clearTable(d->tablePasswords);
233 clearTable(d->tableServers);
238 addServerPasswordToTable(pass);
240 d->spinMaxPasswords->setValue(cfg.maxNumberOfServersPerPassword());
243 void CFGServerPasswords::removeSelectedPasswords()
245 CommonGUI::removeSelectedRowsFromQTableWidget(d->tablePasswords);
248 void CFGServerPasswords::removeSelectedServers()
250 QList<ServerPasswordSummary> servers;
251 for (QTableWidgetItem *item : d->tableServers->selectedItems())
253 if (item->column() == COL_SERV_GAME)
254 servers << ServerPasswordSummary::deserializeQVariant(item->data(Qt::UserRole));
256 ServerPassword currentPassword = serverPasswordFromRow(d->tablePasswords->currentRow());
259 currentPassword.removeServer(server.game(), server.address(), server.port());
261 updatePassword(currentPassword);
262 CommonGUI::removeSelectedRowsFromQTableWidget(d->tableServers);
265 void CFGServerPasswords::revealPasswords()
267 d->cbHide->setChecked(
false);
268 d->lePassword->setEchoMode(QLineEdit::Normal);
269 for (
int i = 0; i < d->tablePasswords->rowCount(); ++i)
271 QTableWidgetItem *item = d->tablePasswords->item(i, COL_PASS_PASSWORD);
273 item->setText(password.phrase());
274 item->setToolTip(password.phrase());
278 void CFGServerPasswords::setPasswordsHidden(
bool hidden)
286 QTableWidgetItem *CFGServerPasswords::toolTipItem(
const QString &contents)
288 auto item =
new QTableWidgetItem(contents);
289 item->setToolTip(contents);
293 void CFGServerPasswords::updatePassword(
const ServerPassword &password)
295 int row = findPassphraseInTable(password.phrase());
297 setPasswordInRow(row, password);
299 addServerPasswordToTable(password);