23 #include "cfgserverpasswords.h"
24 #include "ui_cfgserverpasswords.h"
26 #include "configuration/passwordscfg.h"
27 #include "configuration/serverpassword.h"
28 #include "gui/helpers/datetablewidgetitem.h"
29 #include "gui/commongui.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)
109 void CFGServerPasswords::hidePasswords()
111 d->btnRevealHideToggle->setText(tr(
"Reveal"));
112 d->bHidingPasswordsMode =
true;
113 d->lePassword->setEchoMode(QLineEdit::Password);
114 for (
int i = 0; i < d->tablePasswords->rowCount(); ++i)
116 QTableWidgetItem* item = d->tablePasswords->item(i, COL_PASS_PASSWORD);
117 item->setText(HIDDEN_PASS);
118 item->setToolTip(HIDDEN_PASS);
122 bool CFGServerPasswords::isPassphraseInTable(
const QString& phrase)
124 return findPassphraseInTable(phrase) >= 0;
127 void CFGServerPasswords::onPasswordTableCellChange(
int currentRow,
int currentColumn,
128 int previousRow,
int previousColumn)
130 if (currentRow != previousRow)
134 setServersInTable(serverPasswordFromRow(currentRow));
141 QList<ServerPassword> passwords;
142 for (
int i = 0; i < d->tablePasswords->rowCount(); ++i)
144 passwords << serverPasswordFromRow(i);
146 cfg.setServerPasswords(passwords);
147 cfg.setMaxNumberOfServersPerPassword(d->spinMaxPasswords->value());
152 if (row < 0 || row >= d->tablePasswords->rowCount())
156 return ServerPassword::deserializeQVariant(
157 d->tablePasswords->item(row, COL_PASS_PASSWORD)->data(Qt::UserRole));
160 void CFGServerPasswords::setPasswordInRow(
int row,
const ServerPassword& password)
163 bool wasSortingEnabled = d->tablePasswords->isSortingEnabled();
164 d->tablePasswords->setSortingEnabled(
false);
167 if (d->bHidingPasswordsMode)
169 phrase = HIDDEN_PASS;
173 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);
196 d->tableServers->setSortingEnabled(
false);
199 int rowIndex = d->tableServers->rowCount();
200 d->tableServers->insertRow(rowIndex);
202 QTableWidgetItem* gameItem = toolTipItem(server.game());
203 gameItem->setData(Qt::UserRole, server.serializeQVariant());
204 d->tableServers->setItem(rowIndex, COL_SERV_GAME, gameItem);
206 d->tableServers->setItem(rowIndex, COL_SERV_NAME, toolTipItem(server.name()));
207 QString address = QString(
"%1:%2").arg(server.address()).arg(server.port());
208 d->tableServers->setItem(rowIndex, COL_SERV_ADDRESS, toolTipItem(address));
211 dateItem->setToolTip(dateItem->displayedText());
212 d->tableServers->setItem(rowIndex, COL_SERV_LAST_TIME, dateItem);
215 d->tableServers->setSortingEnabled(
true);
216 d->tableServers->resizeRowsToContents();
219 void CFGServerPasswords::showServerLossWarningIfNecessary()
222 d->lblServerLossWarning->setVisible(d->spinMaxPasswords->value() <
223 cfg.maxNumberOfServersPerPassword());
228 clearTable(d->tablePasswords);
229 clearTable(d->tableServers);
234 addServerPasswordToTable(pass);
236 d->spinMaxPasswords->setValue(cfg.maxNumberOfServersPerPassword());
239 void CFGServerPasswords::removeSelectedPasswords()
241 CommonGUI::removeSelectedRowsFromQTableWidget(d->tablePasswords);
244 void CFGServerPasswords::removeSelectedServers()
246 QList<ServerPasswordSummary> servers;
247 foreach (QTableWidgetItem* item, d->tableServers->selectedItems())
249 if (item->column() == COL_SERV_GAME)
251 servers << ServerPasswordSummary::deserializeQVariant(item->data(Qt::UserRole));
254 ServerPassword currentPassword = serverPasswordFromRow(d->tablePasswords->currentRow());
257 currentPassword.removeServer(server.game(), server.address(), server.port());
259 updatePassword(currentPassword);
260 CommonGUI::removeSelectedRowsFromQTableWidget(d->tableServers);
263 void CFGServerPasswords::revealPasswords()
265 d->btnRevealHideToggle->setText(tr(
"Hide"));
266 d->lePassword->setEchoMode(QLineEdit::Normal);
267 d->bHidingPasswordsMode =
false;
268 for (
int i = 0; i < d->tablePasswords->rowCount(); ++i)
270 QTableWidgetItem* item = d->tablePasswords->item(i, COL_PASS_PASSWORD);
272 item->setText(password.phrase());
273 item->setToolTip(password.phrase());
277 void CFGServerPasswords::toggleRevealHide()
279 if (d->bHidingPasswordsMode)
289 QTableWidgetItem* CFGServerPasswords::toolTipItem(
const QString& contents)
291 QTableWidgetItem* item =
new QTableWidgetItem(contents);
292 item->setToolTip(contents);
296 void CFGServerPasswords::updatePassword(
const ServerPassword& password)
298 int row = findPassphraseInTable(password.phrase());
301 setPasswordInRow(row, password);
305 addServerPasswordToTable(password);
void saveSettings()
Reimplement this to write settings to config from widgets.
void readSettings()
Reimplement this to read settings from config into widgets.
Base class for configuration pages.