File indexing completed on 2024-04-28 17:05:54

0001 /*
0002     SPDX-FileCopyrightText: 2002 Shie Erlich <erlich@users.sourceforge.net>
0003     SPDX-FileCopyrightText: 2002 Rafi Yanai <yanai@users.sourceforge.net>
0004     SPDX-FileCopyrightText: 2009 Fathi Boudra <fboudra@gmail.com>
0005     SPDX-FileCopyrightText: 2004-2022 Krusader Krew <https://krusader.org>
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #include "newftpgui.h"
0011 
0012 // QtCore
0013 #include <QEvent>
0014 #include <QStringList>
0015 // QtGui
0016 #include <QFont>
0017 // QtWidgets
0018 #include <QDialogButtonBox>
0019 #include <QGridLayout>
0020 #include <QHBoxLayout>
0021 #include <QPushButton>
0022 #include <QSizePolicy>
0023 
0024 #include <KConfigCore/KConfigGroup>
0025 #include <KConfigCore/KSharedConfig>
0026 #include <KI18n/KLocalizedString>
0027 #include <KIOCore/KProtocolInfo>
0028 
0029 #include "../compat.h"
0030 #include "../icon.h"
0031 #include "../krglobal.h"
0032 
0033 #define SIZE_MINIMUM QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed)
0034 
0035 const QStringList sProtocols = QStringList() << QStringLiteral("ftp") << QStringLiteral("ftps") << QStringLiteral("sftp") << QStringLiteral("fish")
0036                                              << QStringLiteral("nfs") << QStringLiteral("smb") << QStringLiteral("webdav") << QStringLiteral("svn")
0037                                              << QStringLiteral("svn+file") << QStringLiteral("svn+http") << QStringLiteral("svn+https")
0038                                              << QStringLiteral("svn+ssh");
0039 
0040 /**
0041  * Constructs a newFTPGUI which is a child of 'parent',
0042  * with the name 'name' and widget flags set to 'f'
0043  */
0044 newFTPGUI::newFTPGUI(QWidget *parent)
0045     : QDialog(parent)
0046 {
0047     setModal(true);
0048     setWindowTitle(i18n("New Network Connection"));
0049     resize(500, 240);
0050 
0051     auto *mainLayout = new QVBoxLayout;
0052     setLayout(mainLayout);
0053 
0054     QSizePolicy policy(QSizePolicy::Preferred, QSizePolicy::Preferred);
0055     policy.setHeightForWidth(sizePolicy().hasHeightForWidth());
0056     setSizePolicy(policy);
0057 
0058     iconLabel = new QLabel(this);
0059     iconLabel->setPixmap(Icon("network-wired").pixmap(32));
0060     iconLabel->setSizePolicy(SIZE_MINIMUM);
0061 
0062     aboutLabel = new QLabel(i18n("About to connect to..."), this);
0063     QFont font(aboutLabel->font());
0064     font.setBold(true);
0065     aboutLabel->setFont(font);
0066 
0067     protocolLabel = new QLabel(i18n("Protocol:"), this);
0068     hostLabel = new QLabel(i18n("Host:"), this);
0069     portLabel = new QLabel(i18n("Port:"), this);
0070 
0071     prefix = new KComboBox(this);
0072     prefix->setObjectName(QString::fromUtf8("protocol"));
0073     prefix->setSizePolicy(SIZE_MINIMUM);
0074 
0075     url = new KrHistoryComboBox(this);
0076     url->setMaxCount(50);
0077     url->setMinimumContentsLength(10);
0078 
0079     const QStringList availableProtocols = KProtocolInfo::protocols();
0080     for (const QString &protocol : sProtocols) {
0081         if (availableProtocols.contains(protocol))
0082             prefix->addItem(protocol + QStringLiteral("://"));
0083     }
0084 
0085     // load the history and completion list after creating the history combo
0086     KConfigGroup group(krConfig, "Private");
0087     QStringList list = group.readEntry("newFTP Completion list", QStringList());
0088     url->completionObject()->setItems(list);
0089     list = group.readEntry("newFTP History list", QStringList());
0090     url->setHistoryItems(list);
0091 
0092     // Select last used protocol
0093     const QString lastUsedProtocol = group.readEntry("newFTP Protocol", QString());
0094     if (!lastUsedProtocol.isEmpty()) {
0095         prefix->setCurrentItem(lastUsedProtocol);
0096     }
0097 
0098     port = new QSpinBox(this);
0099     port->setMaximum(65535);
0100     port->setValue(21);
0101     port->setSizePolicy(SIZE_MINIMUM);
0102 
0103     usernameLabel = new QLabel(i18n("Username:"), this);
0104     username = new KLineEdit(this);
0105     passwordLabel = new QLabel(i18n("Password:"), this);
0106     password = new KLineEdit(this);
0107     password->setEchoMode(QLineEdit::Password);
0108 
0109     auto *horizontalLayout = new QHBoxLayout();
0110     horizontalLayout->addWidget(iconLabel);
0111     horizontalLayout->addWidget(aboutLabel);
0112 
0113     auto *gridLayout = new QGridLayout();
0114     gridLayout->addWidget(protocolLabel, 0, 0, 1, 1);
0115     gridLayout->addWidget(hostLabel, 0, 1, 1, 1);
0116     gridLayout->addWidget(portLabel, 0, 2, 1, 1);
0117     gridLayout->addWidget(prefix, 1, 0, 1, 1);
0118     gridLayout->addWidget(url, 1, 1, 1, 1);
0119     gridLayout->addWidget(port, 1, 2, 1, 1);
0120     gridLayout->addWidget(usernameLabel, 2, 0, 1, 1);
0121     gridLayout->addWidget(username, 3, 0, 1, 3);
0122     gridLayout->addWidget(passwordLabel, 4, 0, 1, 1);
0123     gridLayout->addWidget(password, 5, 0, 1, 3);
0124 
0125     auto *widgetLayout = new QGridLayout();
0126     widgetLayout->addLayout(horizontalLayout, 0, 0, 1, 1);
0127     widgetLayout->addLayout(gridLayout, 1, 0, 1, 1);
0128     mainLayout->addLayout(widgetLayout);
0129 
0130     QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
0131     mainLayout->addWidget(buttonBox);
0132     QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
0133     okButton->setText(i18n("&Connect"));
0134     okButton->setDefault(true);
0135     okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
0136 
0137     connect(buttonBox, &QDialogButtonBox::accepted, this, &newFTPGUI::accept);
0138     connect(buttonBox, &QDialogButtonBox::rejected, this, &newFTPGUI::reject);
0139 
0140     connect(prefix, QOverload<const QString &>::of(&KComboBox::QCOMBOBOX_ACTIVATED), this, &newFTPGUI::slotTextChanged);
0141     connect(url, QOverload<const QString &>::of(&KrHistoryComboBox::QCOMBOBOX_ACTIVATED), url, &KrHistoryComboBox::addToHistory);
0142 
0143     if (!lastUsedProtocol.isEmpty()) {
0144         // update the port field
0145         slotTextChanged(lastUsedProtocol);
0146     }
0147 
0148     setTabOrder(url, username);
0149     setTabOrder(username, password);
0150     setTabOrder(password, prefix);
0151 }
0152 
0153 /**
0154  * Destroys the object and frees any allocated resources
0155  */
0156 newFTPGUI::~newFTPGUI()
0157 {
0158     // no need to delete child widgets, Qt does it all for us
0159 }
0160 
0161 void newFTPGUI::slotTextChanged(const QString &string)
0162 {
0163     if (string.startsWith(QLatin1String("ftp")) || string.startsWith(QLatin1String("sftp")) || string.startsWith(QLatin1String("fish"))) {
0164         if (port->value() == 21 || port->value() == 22) {
0165             port->setValue(string.startsWith(QLatin1String("ftp")) ? 21 : 22);
0166         }
0167         port->setEnabled(true);
0168     } else {
0169         port->setEnabled(false);
0170     }
0171 }
0172 
0173 /**
0174  * Main event handler. Reimplemented to handle application font changes
0175  */
0176 bool newFTPGUI::event(QEvent *ev)
0177 {
0178     bool ret = QDialog::event(ev);
0179     if (ev->type() == QEvent::ApplicationFontChange) {
0180         QFont font(aboutLabel->font());
0181         font.setBold(true);
0182         aboutLabel->setFont(font);
0183     }
0184     return ret;
0185 }