23 #include "cfgircdefinenetworkdialog.h" 24 #include "ui_cfgircdefinenetworkdialog.h" 26 #include "irc/chatnetworknamer.h" 27 #include "irc/configuration/chatnetworkscfg.h" 28 #include "irc/entities/ircnetworkentity.h" 30 #include <QMessageBox> 33 DClass<CFGIRCDefineNetworkDialog> :
public Ui::CFGIRCDefineNetworkDialog
36 static const int MAX_IRC_COMMAND_LENGTH = 512;
38 QList<IRCNetworkEntity> existingNetworks;
39 QString originalDescription;
50 initFrom(initValuesEntity);
53 CFGIRCDefineNetworkDialog::CFGIRCDefineNetworkDialog(QWidget *parent)
59 CFGIRCDefineNetworkDialog::~CFGIRCDefineNetworkDialog()
63 void CFGIRCDefineNetworkDialog::accept()
65 if (!validateDescription())
67 QStringList offenders = validateAutojoinCommands();
68 if (!offenders.isEmpty())
70 if (!askToAcceptAnywayWhenCommandsBad(offenders))
76 bool CFGIRCDefineNetworkDialog::askToAcceptAnywayWhenCommandsBad(
const QStringList &offenders)
78 assert(!offenders.isEmpty() &&
"no offenders");
79 QString header = tr(
"Following commands have violated the IRC maximum byte " 81 QString footer = tr(
"\n\nIf saved, the script may not run properly.\n\n" 82 "Do you wish to save the script anyway?");
83 QStringList formattedOffenders = formatOffenders(offenders.mid(0, 10));
84 QString body = formattedOffenders.join(
"\n\n");
85 if (formattedOffenders.size() < offenders.size())
86 body += tr(
"\n\n... and %n more ...",
"", offenders.size() - formattedOffenders.size());
87 QString msg = header + body + footer;
88 QMessageBox::StandardButton result = QMessageBox::warning(
89 this, tr(
"Doomseeker - IRC Commands Problem"), msg,
90 QMessageBox::Yes | QMessageBox::Cancel);
91 return result == QMessageBox::Yes;
94 QStringList CFGIRCDefineNetworkDialog::autojoinCommands()
const 96 return d->teAutojoinCommands->toPlainText().split(
"\n");
99 void CFGIRCDefineNetworkDialog::buttonClicked(QAbstractButton *button)
101 auto pButton = (QPushButton *)button;
102 if (pButton == d->buttonBox->button(QDialogButtonBox::Ok))
108 void CFGIRCDefineNetworkDialog::construct()
112 connect(d->buttonBox, SIGNAL(clicked(QAbstractButton*)), SLOT(buttonClicked(QAbstractButton*)));
115 QStringList CFGIRCDefineNetworkDialog::formatOffenders(
const QStringList &offenders)
const 117 QStringList offendersFormatted;
118 for (
const QString &offender : offenders)
120 offendersFormatted << tr(
"\t%1 (...)").arg(offender.left(40));
122 return offendersFormatted;
129 QString autojoinChannels = d->teAutojoinChannels->toPlainText();
130 autojoinChannels.remove(
'\r').replace(
'\n',
' ');
132 entity.setAddress(d->leAddress->text().trimmed());
133 entity.setAutojoinChannels(autojoinChannels.split(
" ", QString::SkipEmptyParts));
134 entity.setAutojoinCommands(autojoinCommands());
135 entity.setDescription(d->leDescription->text().trimmed());
136 entity.setNickservCommand(d->leNickservCommand->text().trimmed());
137 entity.setNickservPassword(d->leNickservPassword->text());
138 entity.setPassword(d->leServerPassword->text());
139 entity.setPort(d->spinPort->value());
144 void CFGIRCDefineNetworkDialog::initFrom(
const IRCNetworkEntity &networkEntity)
146 d->originalDescription = networkEntity.
description();
147 d->leAddress->setText(networkEntity.
address());
148 d->teAutojoinChannels->setPlainText(networkEntity.
autojoinChannels().join(
" "));
149 d->teAutojoinCommands->setPlainText(networkEntity.
autojoinCommands().join(
"\n"));
150 d->leDescription->setText(networkEntity.
description());
151 d->leNickservCommand->setText(networkEntity.nickservCommand());
152 d->leNickservPassword->setText(networkEntity.nickservPassword());
153 d->leServerPassword->setText(networkEntity.
password());
154 d->spinPort->setValue(networkEntity.
port());
157 bool CFGIRCDefineNetworkDialog::isDescriptionUnique()
const 159 QString current = d->leDescription->text().trimmed().toLower();
160 if (d->originalDescription.trimmed().toLower() == current)
167 if (network.description().trimmed().toLower() == current)
173 bool CFGIRCDefineNetworkDialog::isValidDescription()
const 175 return ChatNetworkNamer::isValidName(d->leDescription->text());
178 QList<IRCNetworkEntity> CFGIRCDefineNetworkDialog::listExistingNetworks()
const 180 if (!d->existingNetworks.isEmpty())
181 return d->existingNetworks;
186 QStringList CFGIRCDefineNetworkDialog::validateAutojoinCommands()
const 188 QStringList offenders;
189 for (
const QString &command : autojoinCommands())
192 offenders << command;
199 d->existingNetworks = networks;
202 bool CFGIRCDefineNetworkDialog::validateDescription()
204 if (d->leDescription->text().trimmed().isEmpty())
206 QMessageBox::critical(
this, tr(
"Invalid IRC network description"),
207 tr(
"Network description cannot be empty."));
210 if (!isDescriptionUnique())
212 QMessageBox::critical(
this, tr(
"Invalid IRC network description"),
213 tr(
"There already is a network with such description."));
216 if (!isValidDescription())
218 QString msg = tr(
"Network description is invalid.\n\n" 219 "Only letters, digits, spaces and \"%1\" are allowed.")
220 .arg(ChatNetworkNamer::additionalAllowedChars());
221 QMessageBox::critical(
this, tr(
"Invalid IRC network description"), msg);
unsigned short port() const
Port of the server or network to connect to.
const QStringList & autojoinChannels() const
List of channels to which a /join command will be issued automatically when a connection with this ne...
const QString & description() const
A short, human-readable description for the network. (Preferably a single word).
Data structure that describes and defines a connection to an IRC network or server.
const QStringList & autojoinCommands() const
List of commands executed on network join.
void setExistingNetworks(const QList< IRCNetworkEntity > &networks)
Overrides extraction from config if not-empty list is set.
const QString & password() const
Password for the server or network. Ignored if empty.
const QString & address() const
Address of the server or network to connect to.