cfgserverpasswords.cpp
1 //------------------------------------------------------------------------------
2 // cfgserverpasswords.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program 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
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; 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/helpers/datetablewidgetitem.h"
29 #include "gui/commongui.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 : ConfigurationBaseBox(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  {
103  return i;
104  }
105  }
106  return -1;
107 }
108 
109 void CFGServerPasswords::hidePasswords()
110 {
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)
115  {
116  QTableWidgetItem* item = d->tablePasswords->item(i, COL_PASS_PASSWORD);
117  item->setText(HIDDEN_PASS);
118  item->setToolTip(HIDDEN_PASS);
119  }
120 }
121 
122 bool CFGServerPasswords::isPassphraseInTable(const QString& phrase)
123 {
124  return findPassphraseInTable(phrase) >= 0;
125 }
126 
127 void CFGServerPasswords::onPasswordTableCellChange(int currentRow, int currentColumn,
128  int previousRow, int previousColumn)
129 {
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  {
144  passwords << serverPasswordFromRow(i);
145  }
146  cfg.setServerPasswords(passwords);
147  cfg.setMaxNumberOfServersPerPassword(d->spinMaxPasswords->value());
148 }
149 
150 ServerPassword CFGServerPasswords::serverPasswordFromRow(int row)
151 {
152  if (row < 0 || row >= d->tablePasswords->rowCount())
153  {
154  return ServerPassword();
155  }
156  return ServerPassword::deserializeQVariant(
157  d->tablePasswords->item(row, COL_PASS_PASSWORD)->data(Qt::UserRole));
158 }
159 
160 void CFGServerPasswords::setPasswordInRow(int row, const ServerPassword& password)
161 {
162  // Disable sorting or bad things may happen.
163  bool wasSortingEnabled = d->tablePasswords->isSortingEnabled();
164  d->tablePasswords->setSortingEnabled(false);
165 
166  QString phrase;
167  if (d->bHidingPasswordsMode)
168  {
169  phrase = HIDDEN_PASS;
170  }
171  else
172  {
173  phrase = password.phrase();
174  }
175 
176  QTableWidgetItem* phraseItem = toolTipItem(phrase);
177  phraseItem->setData(Qt::UserRole, password.serializeQVariant());
178  d->tablePasswords->setItem(row, COL_PASS_PASSWORD, phraseItem);
179 
180  d->tablePasswords->setItem(row, COL_PASS_LAST_GAME, toolTipItem(password.lastGame()));
181  d->tablePasswords->setItem(row, COL_PASS_LAST_SERVER, toolTipItem(password.lastServerName()));
182 
183  DateTableWidgetItem* dateItem = new DateTableWidgetItem(password.lastTime());
184  dateItem->setToolTip(dateItem->displayedText());
185  d->tablePasswords->setItem(row, COL_PASS_LAST_TIME, dateItem);
186 
187  // Re-enable sorting if was enabled before.
188  d->tablePasswords->setSortingEnabled(wasSortingEnabled);
189  d->tablePasswords->resizeRowsToContents();
190 }
191 
192 void CFGServerPasswords::setServersInTable(const ServerPassword& password)
193 {
194  clearTable(d->tableServers);
195  // Disable sorting or bad things may happen.
196  d->tableServers->setSortingEnabled(false);
197  foreach (const ServerPasswordSummary& server, password.servers())
198  {
199  int rowIndex = d->tableServers->rowCount();
200  d->tableServers->insertRow(rowIndex);
201 
202  QTableWidgetItem* gameItem = toolTipItem(server.game());
203  gameItem->setData(Qt::UserRole, server.serializeQVariant());
204  d->tableServers->setItem(rowIndex, COL_SERV_GAME, gameItem);
205 
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));
209 
210  DateTableWidgetItem* dateItem = new DateTableWidgetItem(server.time());
211  dateItem->setToolTip(dateItem->displayedText());
212  d->tableServers->setItem(rowIndex, COL_SERV_LAST_TIME, dateItem);
213  }
214  // Re-enable sorting.
215  d->tableServers->setSortingEnabled(true);
216  d->tableServers->resizeRowsToContents();
217 }
218 
219 void CFGServerPasswords::showServerLossWarningIfNecessary()
220 {
221  PasswordsCfg cfg;
222  d->lblServerLossWarning->setVisible(d->spinMaxPasswords->value() <
223  cfg.maxNumberOfServersPerPassword());
224 }
225 
227 {
228  clearTable(d->tablePasswords);
229  clearTable(d->tableServers);
230 
231  PasswordsCfg cfg;
232  foreach (const ServerPassword& pass, cfg.serverPasswords())
233  {
234  addServerPasswordToTable(pass);
235  }
236  d->spinMaxPasswords->setValue(cfg.maxNumberOfServersPerPassword());
237 }
238 
239 void CFGServerPasswords::removeSelectedPasswords()
240 {
241  CommonGUI::removeSelectedRowsFromQTableWidget(d->tablePasswords);
242 }
243 
244 void CFGServerPasswords::removeSelectedServers()
245 {
246  QList<ServerPasswordSummary> servers;
247  foreach (QTableWidgetItem* item, d->tableServers->selectedItems())
248  {
249  if (item->column() == COL_SERV_GAME)
250  {
251  servers << ServerPasswordSummary::deserializeQVariant(item->data(Qt::UserRole));
252  }
253  }
254  ServerPassword currentPassword = serverPasswordFromRow(d->tablePasswords->currentRow());
255  foreach (const ServerPasswordSummary& server, servers)
256  {
257  currentPassword.removeServer(server.game(), server.address(), server.port());
258  }
259  updatePassword(currentPassword);
260  CommonGUI::removeSelectedRowsFromQTableWidget(d->tableServers);
261 }
262 
263 void CFGServerPasswords::revealPasswords()
264 {
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)
269  {
270  QTableWidgetItem* item = d->tablePasswords->item(i, COL_PASS_PASSWORD);
271  ServerPassword password = serverPasswordFromRow(i);
272  item->setText(password.phrase());
273  item->setToolTip(password.phrase());
274  }
275 }
276 
277 void CFGServerPasswords::toggleRevealHide()
278 {
279  if (d->bHidingPasswordsMode)
280  {
281  revealPasswords();
282  }
283  else
284  {
285  hidePasswords();
286  }
287 }
288 
289 QTableWidgetItem* CFGServerPasswords::toolTipItem(const QString& contents)
290 {
291  QTableWidgetItem* item = new QTableWidgetItem(contents);
292  item->setToolTip(contents);
293  return item;
294 }
295 
296 void CFGServerPasswords::updatePassword(const ServerPassword& password)
297 {
298  int row = findPassphraseInTable(password.phrase());
299  if (row >= 0)
300  {
301  setPasswordInRow(row, password);
302  }
303  else
304  {
305  addServerPasswordToTable(password);
306  }
307 }
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.