passworddlg.cpp
1 //------------------------------------------------------------------------------
2 // passworddlg.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) 2009 Braden "Blzut3" Obrzut <admin@maniacsvault.net>
22 //------------------------------------------------------------------------------
23 #include "passworddlg.h"
24 
25 #include "ui_passworddlg.h"
26 
27 #include "configuration/doomseekerconfig.h"
28 #include "configuration/passwordscfg.h"
29 #include "configuration/serverpassword.h"
30 #include "gui/commongui.h"
31 #include "gui/helpers/comboboxex.h"
32 #include "serverapi/server.h"
33 
34 #include <QLineEdit>
35 
36 DClass<PasswordDlg> : public Ui::PasswordDlg
37 {
38 public:
39  ServerCPtr server;
40  ComboBoxEx *cboConnectPassEx;
41  ComboBoxEx *cboIngamePassEx;
42 };
43 
44 DPointered(PasswordDlg)
45 
46 PasswordDlg::PasswordDlg(ServerCPtr server, QWidget *parent)
47  : QDialog(parent)
48 {
49  d->setupUi(this);
51  d->cboConnectPassEx = new ComboBoxEx(*d->cboConnectPassword);
52  d->cboIngamePassEx = new ComboBoxEx(*d->cboIngamePassword);
53  d->server = server;
54 
55  applyInputsVisibility();
56 
57  // Adjust the size and prevent resizing.
58  adjustSize();
59  setMinimumHeight(height());
60  setMaximumHeight(height());
61 
62  loadConfiguration();
63 }
64 
65 PasswordDlg::~PasswordDlg()
66 {
67  delete d->cboConnectPassEx;
68 }
69 
70 void PasswordDlg::accept()
71 {
72  saveConfiguration();
73  QDialog::accept();
74 }
75 
76 void PasswordDlg::applyInputsVisibility()
77 {
78  d->connectPasswordWidget->setVisible(d->server->isLocked());
79  d->ingamePasswordWidget->setVisible(d->server->isLockedInGame());
80 }
81 
82 QString PasswordDlg::connectPassword() const
83 {
84  return d->cboConnectPassword->currentText();
85 }
86 
87 QString PasswordDlg::inGamePassword() const
88 {
89  return d->cboIngamePassword->currentText();
90 }
91 
92 void PasswordDlg::loadConfiguration()
93 {
94  PasswordsCfg cfg;
95  setHidePasswords(cfg.isHidingPasswords());
96  d->remember->setChecked(cfg.isRememberingConnectPhrase());
97  setPasswords(cfg.serverPhrases());
98  setCurrentConnectPassword(cfg.suggestPassword(
99  d->server.data(), ServerPasswordType::CONNECT).phrase());
100  setCurrentIngamePassword(cfg.suggestPassword(
101  d->server.data(), ServerPasswordType::INGAME).phrase());
102 }
103 
104 void PasswordDlg::removeCurrentConnectPassword()
105 {
106  PasswordsCfg cfg;
107  QString phrase = d->cboConnectPassword->currentText();
108  cfg.removeServerPhrase(d->cboConnectPassword->currentText());
109 
110  d->cboIngamePassEx->removeItem(phrase);
111  if (!d->cboConnectPassEx->removeCurrentItem())
112  {
113  d->cboConnectPassword->clearEditText();
114  d->cboConnectPassword->setFocus();
115  }
116 }
117 
118 void PasswordDlg::removeCurrentIngamePassword()
119 {
120  PasswordsCfg cfg;
121  QString phrase = d->cboIngamePassword->currentText();
122  cfg.removeServerPhrase(d->cboIngamePassword->currentText());
123 
124  d->cboConnectPassEx->removeItem(phrase);
125  if (!d->cboIngamePassEx->removeCurrentItem())
126  {
127  d->cboIngamePassword->clearEditText();
128  d->cboIngamePassword->setFocus();
129  }
130 }
131 
132 void PasswordDlg::saveConfiguration()
133 {
134  PasswordsCfg cfg;
135  cfg.setRememberConnectPhrase(d->remember->isChecked());
136  if (d->remember->isChecked())
137  {
138  cfg.saveServerPhrase(connectPassword(), d->server.data(), ServerPasswordType::CONNECT);
139  cfg.saveServerPhrase(inGamePassword(), d->server.data(), ServerPasswordType::INGAME);
140  }
141 }
142 
143 void PasswordDlg::setCurrentConnectPassword(const QString &password)
144 {
145  d->cboConnectPassEx->setCurrentOrAddNewAndSelect(password);
146 }
147 
148 void PasswordDlg::setCurrentIngamePassword(const QString &password)
149 {
150  d->cboIngamePassEx->setCurrentOrAddNewAndSelect(password);
151 }
152 
153 void PasswordDlg::setHidePasswords(bool hide)
154 {
155  d->cbHidePasswords->blockSignals(true);
156  d->cbHidePasswords->setChecked(hide);
157  d->cbHidePasswords->blockSignals(false);
158  d->cboConnectPassword->lineEdit()->setEchoMode(hide ? QLineEdit::Password : QLineEdit::Normal);
159  d->cboIngamePassword->lineEdit()->setEchoMode(hide ? QLineEdit::Password : QLineEdit::Normal);
160 }
161 
162 void PasswordDlg::setPasswords(const QStringList &passwords)
163 {
164  d->cboConnectPassEx->setItemsSorted(passwords);
165  d->cboIngamePassEx->setItemsSorted(passwords);
166 }