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