cfgserverpasswords.cpp
1 //------------------------------------------------------------------------------
2 // cfgserverpasswords.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301 USA
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2013 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "cfgserverpasswords.h"
24 #include "ui_cfgserverpasswords.h"
25 
26 #include "configuration/passwordscfg.h"
27 #include "configuration/serverpassword.h"
28 #include "gui/commongui.h"
29 #include "gui/helpers/datetablewidgetitem.h"
30 #include <QMap>
31 
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;
36 
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;
41 
42 const QString HIDDEN_PASS = "***";
43 
44 DClass<CFGServerPasswords> : public Ui::CFGServerPasswords
45 {
46 public:
47  bool bHidingPasswordsMode;
48 };
49 
50 DPointered(CFGServerPasswords)
51 
52 CFGServerPasswords::CFGServerPasswords(QWidget *parent)
53  : ConfigPage(parent)
54 {
55  d->setupUi(this);
56  d->bHidingPasswordsMode = true;
57  hidePasswords();
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);
63 }
64 
65 CFGServerPasswords::~CFGServerPasswords()
66 {
67 }
68 
69 void CFGServerPasswords::addPasswordFromLineEdit()
70 {
71  QString phrase = d->lePassword->text();
72  d->lePassword->clear();
73  if (!phrase.isEmpty() && !isPassphraseInTable(phrase))
74  {
75  ServerPassword password;
76  password.setPhrase(phrase);
77  addServerPasswordToTable(password);
78  }
79 }
80 
81 void CFGServerPasswords::addServerPasswordToTable(const ServerPassword &password)
82 {
83  int rowIndex = d->tablePasswords->rowCount();
84  d->tablePasswords->insertRow(rowIndex);
85  setPasswordInRow(rowIndex, password);
86 }
87 
88 void CFGServerPasswords::clearTable(QTableWidget *table)
89 {
90  while (table->rowCount() > 0)
91  {
92  table->removeRow(0);
93  }
94 }
95 
96 int CFGServerPasswords::findPassphraseInTable(const QString &phrase)
97 {
98  for (int i = 0; i < d->tablePasswords->rowCount(); ++i)
99  {
100  ServerPassword password = serverPasswordFromRow(i);
101  if (password.phrase() == phrase)
102  return i;
103  }
104  return -1;
105 }
106 
107 void CFGServerPasswords::hidePasswords()
108 {
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)
113  {
114  QTableWidgetItem *item = d->tablePasswords->item(i, COL_PASS_PASSWORD);
115  item->setText(HIDDEN_PASS);
116  item->setToolTip(HIDDEN_PASS);
117  }
118 }
119 
120 bool CFGServerPasswords::isPassphraseInTable(const QString &phrase)
121 {
122  return findPassphraseInTable(phrase) >= 0;
123 }
124 
125 void CFGServerPasswords::onPasswordTableCellChange(int currentRow, int currentColumn,
126  int previousRow, int previousColumn)
127 {
128  Q_UNUSED(currentColumn);
129  Q_UNUSED(previousColumn);
130  if (currentRow != previousRow)
131  {
132  // Setting an invalid password will clear the table which is
133  // what we want.
134  setServersInTable(serverPasswordFromRow(currentRow));
135  }
136 }
137 
139 {
140  PasswordsCfg cfg;
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());
146 }
147 
148 ServerPassword CFGServerPasswords::serverPasswordFromRow(int row)
149 {
150  if (row < 0 || row >= d->tablePasswords->rowCount())
151  return ServerPassword();
152  return ServerPassword::deserializeQVariant(
153  d->tablePasswords->item(row, COL_PASS_PASSWORD)->data(Qt::UserRole));
154 }
155 
156 void CFGServerPasswords::setPasswordInRow(int row, const ServerPassword &password)
157 {
158  // Disable sorting or bad things may happen.
159  bool wasSortingEnabled = d->tablePasswords->isSortingEnabled();
160  d->tablePasswords->setSortingEnabled(false);
161 
162  QString phrase;
163  if (d->bHidingPasswordsMode)
164  phrase = HIDDEN_PASS;
165  else
166  phrase = password.phrase();
167 
168  QTableWidgetItem *phraseItem = toolTipItem(phrase);
169  phraseItem->setData(Qt::UserRole, password.serializeQVariant());
170  d->tablePasswords->setItem(row, COL_PASS_PASSWORD, phraseItem);
171 
172  d->tablePasswords->setItem(row, COL_PASS_LAST_GAME, toolTipItem(password.lastGame()));
173  d->tablePasswords->setItem(row, COL_PASS_LAST_SERVER, toolTipItem(password.lastServerName()));
174 
175  DateTableWidgetItem *dateItem = new DateTableWidgetItem(password.lastTime());
176  dateItem->setToolTip(dateItem->displayedText());
177  d->tablePasswords->setItem(row, COL_PASS_LAST_TIME, dateItem);
178 
179  // Re-enable sorting if was enabled before.
180  d->tablePasswords->setSortingEnabled(wasSortingEnabled);
181  d->tablePasswords->resizeRowsToContents();
182 }
183 
184 void CFGServerPasswords::setServersInTable(const ServerPassword &password)
185 {
186  clearTable(d->tableServers);
187  // Disable sorting or bad things may happen.
188  d->tableServers->setSortingEnabled(false);
189  for (const ServerPasswordSummary &server : password.servers())
190  {
191  int rowIndex = d->tableServers->rowCount();
192  d->tableServers->insertRow(rowIndex);
193 
194  QTableWidgetItem *gameItem = toolTipItem(server.game());
195  gameItem->setData(Qt::UserRole, server.serializeQVariant());
196  d->tableServers->setItem(rowIndex, COL_SERV_GAME, gameItem);
197 
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));
201 
202  DateTableWidgetItem *dateItem = new DateTableWidgetItem(server.time());
203  dateItem->setToolTip(dateItem->displayedText());
204  d->tableServers->setItem(rowIndex, COL_SERV_LAST_TIME, dateItem);
205  }
206  // Re-enable sorting.
207  d->tableServers->setSortingEnabled(true);
208  d->tableServers->resizeRowsToContents();
209 }
210 
211 void CFGServerPasswords::showServerLossWarningIfNecessary()
212 {
213  PasswordsCfg cfg;
214  d->lblServerLossWarning->setVisible(d->spinMaxPasswords->value() <
215  cfg.maxNumberOfServersPerPassword());
216 }
217 
219 {
220  clearTable(d->tablePasswords);
221  clearTable(d->tableServers);
222 
223  PasswordsCfg cfg;
224  for (const ServerPassword &pass : cfg.serverPasswords())
225  {
226  addServerPasswordToTable(pass);
227  }
228  d->spinMaxPasswords->setValue(cfg.maxNumberOfServersPerPassword());
229 }
230 
231 void CFGServerPasswords::removeSelectedPasswords()
232 {
233  CommonGUI::removeSelectedRowsFromQTableWidget(d->tablePasswords);
234 }
235 
236 void CFGServerPasswords::removeSelectedServers()
237 {
238  QList<ServerPasswordSummary> servers;
239  for (QTableWidgetItem *item : d->tableServers->selectedItems())
240  {
241  if (item->column() == COL_SERV_GAME)
242  servers << ServerPasswordSummary::deserializeQVariant(item->data(Qt::UserRole));
243  }
244  ServerPassword currentPassword = serverPasswordFromRow(d->tablePasswords->currentRow());
245  for (const ServerPasswordSummary &server : servers)
246  {
247  currentPassword.removeServer(server.game(), server.address(), server.port());
248  }
249  updatePassword(currentPassword);
250  CommonGUI::removeSelectedRowsFromQTableWidget(d->tableServers);
251 }
252 
253 void CFGServerPasswords::revealPasswords()
254 {
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)
259  {
260  QTableWidgetItem *item = d->tablePasswords->item(i, COL_PASS_PASSWORD);
261  ServerPassword password = serverPasswordFromRow(i);
262  item->setText(password.phrase());
263  item->setToolTip(password.phrase());
264  }
265 }
266 
267 void CFGServerPasswords::toggleRevealHide()
268 {
269  if (d->bHidingPasswordsMode)
270  revealPasswords();
271  else
272  hidePasswords();
273 }
274 
275 QTableWidgetItem *CFGServerPasswords::toolTipItem(const QString &contents)
276 {
277  auto item = new QTableWidgetItem(contents);
278  item->setToolTip(contents);
279  return item;
280 }
281 
282 void CFGServerPasswords::updatePassword(const ServerPassword &password)
283 {
284  int row = findPassphraseInTable(password.phrase());
285  if (row >= 0)
286  setPasswordInRow(row, password);
287  else
288  addServerPasswordToTable(password);
289 }
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.
Definition: configpage.h:44