miscserversetuppanel.cpp
1 //------------------------------------------------------------------------------
2 // miscserversetuppanel.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) 2014 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "miscserversetuppanel.h"
24 #include "ui_miscserversetuppanel.h"
25 
26 #include "configuration/passwordscfg.h"
27 #include "ini/ini.h"
28 #include "plugins/engineplugin.h"
29 #include "serverapi/gamecreateparams.h"
30 
31 DClass<MiscServerSetupPanel> : public Ui::MiscServerSetupPanel
32 {
33 public:
34  bool anythingAvailable;
35 };
36 
37 DPointered(MiscServerSetupPanel)
38 
40  : QWidget(parent)
41 {
42  d->setupUi(this);
43  d->anythingAvailable = false;
44 
45  PasswordsCfg passCfg;
46  setHidePasswords(passCfg.isHidingPasswords());
47 }
48 
49 MiscServerSetupPanel::~MiscServerSetupPanel()
50 {
51 }
52 
53 void MiscServerSetupPanel::fillInParams(GameCreateParams &params)
54 {
55  params.setEmail(d->leEmail->text());
56  params.setMotd(d->pteMOTD->toPlainText());
57  params.setConnectPassword(d->leConnectPassword->text());
58  params.setIngamePassword(d->leJoinPassword->text());
59  params.setRconPassword(d->leRConPassword->text());
60  params.setUrl(d->leURL->text());
61 }
62 
63 bool MiscServerSetupPanel::isAnythingAvailable() const
64 {
65  return d->anythingAvailable;
66 }
67 
68 void MiscServerSetupPanel::loadConfig(Ini &config)
69 {
70  IniSection misc = config.section("Misc");
71  d->leURL->setText(misc["URL"]);
72  d->leEmail->setText(misc["eMail"]);
73  d->leConnectPassword->setText(misc["connectPassword"]);
74  d->leJoinPassword->setText(misc["joinPassword"]);
75  d->leRConPassword->setText(misc["RConPassword"]);
76  d->pteMOTD->document()->setPlainText(misc["MOTD"]);
77 }
78 
79 void MiscServerSetupPanel::saveConfig(Ini &config)
80 {
81  IniSection misc = config.section("Misc");
82  misc["URL"] = d->leURL->text();
83  misc["eMail"] = d->leEmail->text();
84  misc["connectPassword"] = d->leConnectPassword->text();
85  misc["joinPassword"] = d->leJoinPassword->text();
86  misc["RConPassword"] = d->leRConPassword->text();
87  misc["MOTD"] = d->pteMOTD->toPlainText();
88 }
89 
90 void MiscServerSetupPanel::setupForEngine(const EnginePlugin *engine)
91 {
92  d->anythingAvailable = false;
93 
94  struct FormItem
95  {
96  bool visible;
97  QWidget *label;
98  QWidget *widget;
99  };
100 
101  // The order in this struct if the order of appearance.
102  const FormItem items[] =
103  {
104  { engine->data()->allowsURL, d->labelURL, d->leURL },
105  { engine->data()->allowsEmail, d->labelEmail, d->leEmail },
106  { engine->data()->allowsConnectPassword, d->labelConnectPassword, d->leConnectPassword },
107  { engine->data()->allowsJoinPassword, d->labelJoinPassword, d->leJoinPassword },
108  { engine->data()->allowsRConPassword, d->labelRConPassword, d->leRConPassword },
109  { engine->data()->allowsConnectPassword
110  || engine->data()->allowsJoinPassword
111  || engine->data()->allowsRConPassword, nullptr, d->cbHidePasswords },
112  };
113 
114  // Clear all widgets in the form.
115  while (d->formLayout->count() > 0)
116  d->formLayout->takeAt(0);
117 
118  // Now re-add only those widgets that are meant to be visible.
119  for (const FormItem &item : items)
120  {
121  if (item.label)
122  item.label->setVisible(item.visible);
123  if (item.widget)
124  item.widget->setVisible(item.visible);
125  if (item.visible)
126  d->formLayout->addRow(item.label, item.widget);
127  d->anythingAvailable = item.visible || d->anythingAvailable;
128  }
129 
130  const bool motd = engine->data()->allowsMOTD;
131  d->labelMOTD->setVisible(motd);
132  d->pteMOTD->setVisible(motd);
133  d->anythingAvailable = motd || d->anythingAvailable;
134 }
135 
136 void MiscServerSetupPanel::setHidePasswords(bool hide)
137 {
138  d->cbHidePasswords->blockSignals(true);
139  d->cbHidePasswords->setChecked(hide);
140  d->cbHidePasswords->blockSignals(false);
141  QLineEdit::EchoMode echoMode = hide ? QLineEdit::Password : QLineEdit::Normal;
142  d->leConnectPassword->setEchoMode(echoMode);
143  d->leJoinPassword->setEchoMode(echoMode);
144  d->leRConPassword->setEchoMode(echoMode);
145 }