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