| 1 | /* |
| 2 | SPDX-FileCopyrightText: 1998, 2008, 2009 David Faure <faure@kde.org> |
| 3 | |
| 4 | SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL |
| 5 | */ |
| 6 | |
| 7 | #include "knameandurlinputdialog.h" |
| 8 | |
| 9 | #include <KLineEdit> // For KUrlRequester::lineEdit() |
| 10 | #include <QDialogButtonBox> |
| 11 | #include <QFormLayout> |
| 12 | #include <QVBoxLayout> |
| 13 | #include <kprotocolmanager.h> |
| 14 | #include <kurlrequester.h> |
| 15 | |
| 16 | class KNameAndUrlInputDialogPrivate |
| 17 | { |
| 18 | public: |
| 19 | explicit KNameAndUrlInputDialogPrivate(KNameAndUrlInputDialog *qq) |
| 20 | : m_fileNameEdited(false) |
| 21 | , q(qq) |
| 22 | { |
| 23 | } |
| 24 | |
| 25 | void slotNameTextChanged(const QString &); |
| 26 | void slotURLTextChanged(const QString &); |
| 27 | |
| 28 | /* |
| 29 | * The line edit widget for the fileName |
| 30 | */ |
| 31 | QLineEdit *m_leName; |
| 32 | /* |
| 33 | * The URL requester for the URL :) |
| 34 | */ |
| 35 | KUrlRequester *m_urlRequester; |
| 36 | /* |
| 37 | * True if the filename was manually edited. |
| 38 | */ |
| 39 | bool m_fileNameEdited; |
| 40 | |
| 41 | QDialogButtonBox *m_buttonBox; |
| 42 | |
| 43 | KNameAndUrlInputDialog *const q; |
| 44 | }; |
| 45 | |
| 46 | KNameAndUrlInputDialog::KNameAndUrlInputDialog(const QString &nameLabel, const QString &urlLabel, const QUrl &startDir, QWidget *parent) |
| 47 | : QDialog(parent) |
| 48 | , d(new KNameAndUrlInputDialogPrivate(this)) |
| 49 | { |
| 50 | QVBoxLayout *topLayout = new QVBoxLayout(this); |
| 51 | |
| 52 | QFormLayout *formLayout = new QFormLayout; |
| 53 | formLayout->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0); |
| 54 | |
| 55 | // First line: filename |
| 56 | d->m_leName = new QLineEdit(this); |
| 57 | d->m_leName->setMinimumWidth(d->m_leName->sizeHint().width() * 3); |
| 58 | d->m_leName->setSelection(0, d->m_leName->text().length()); // autoselect |
| 59 | connect(sender: d->m_leName, signal: &QLineEdit::textChanged, context: this, slot: [this](const QString &text) { |
| 60 | d->slotNameTextChanged(text); |
| 61 | }); |
| 62 | formLayout->addRow(labelText: nameLabel, field: d->m_leName); |
| 63 | |
| 64 | // Second line: url |
| 65 | d->m_urlRequester = new KUrlRequester(this); |
| 66 | d->m_urlRequester->setStartDir(startDir); |
| 67 | d->m_urlRequester->setMode(KFile::File | KFile::Directory); |
| 68 | |
| 69 | d->m_urlRequester->setMinimumWidth(d->m_urlRequester->sizeHint().width() * 3); |
| 70 | connect(sender: d->m_urlRequester->lineEdit(), signal: &QLineEdit::textChanged, context: this, slot: [this](const QString &text) { |
| 71 | d->slotURLTextChanged(text); |
| 72 | }); |
| 73 | formLayout->addRow(labelText: urlLabel, field: d->m_urlRequester); |
| 74 | |
| 75 | topLayout->addLayout(layout: formLayout); |
| 76 | |
| 77 | d->m_buttonBox = new QDialogButtonBox(this); |
| 78 | d->m_buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); |
| 79 | connect(sender: d->m_buttonBox, signal: &QDialogButtonBox::accepted, context: this, slot: &QDialog::accept); |
| 80 | connect(sender: d->m_buttonBox, signal: &QDialogButtonBox::rejected, context: this, slot: &QDialog::reject); |
| 81 | topLayout->addWidget(d->m_buttonBox); |
| 82 | |
| 83 | d->m_fileNameEdited = false; |
| 84 | d->m_buttonBox->button(which: QDialogButtonBox::Ok)->setEnabled(!d->m_leName->text().isEmpty() && !d->m_urlRequester->url().isEmpty()); |
| 85 | d->m_leName->setFocus(); |
| 86 | } |
| 87 | |
| 88 | KNameAndUrlInputDialog::~KNameAndUrlInputDialog() = default; |
| 89 | |
| 90 | QUrl KNameAndUrlInputDialog::url() const |
| 91 | { |
| 92 | return d->m_urlRequester->url(); |
| 93 | } |
| 94 | |
| 95 | QString KNameAndUrlInputDialog::urlText() const |
| 96 | { |
| 97 | return d->m_urlRequester->text(); |
| 98 | } |
| 99 | |
| 100 | QString KNameAndUrlInputDialog::name() const |
| 101 | { |
| 102 | return d->m_leName->text(); |
| 103 | } |
| 104 | |
| 105 | void KNameAndUrlInputDialogPrivate::slotNameTextChanged(const QString &) |
| 106 | { |
| 107 | m_fileNameEdited = true; |
| 108 | m_buttonBox->button(which: QDialogButtonBox::Ok)->setEnabled(!m_leName->text().isEmpty() && !m_urlRequester->url().isEmpty()); |
| 109 | } |
| 110 | |
| 111 | void KNameAndUrlInputDialogPrivate::slotURLTextChanged(const QString &) |
| 112 | { |
| 113 | if (!m_fileNameEdited) { |
| 114 | // use URL as default value for the filename |
| 115 | // (we copy only its filename if protocol supports listing, |
| 116 | // but for HTTP we don't want tons of index.html links) |
| 117 | QUrl url(m_urlRequester->url()); |
| 118 | if (KProtocolManager::supportsListing(url) && !url.fileName().isEmpty()) { |
| 119 | m_leName->setText(url.fileName()); |
| 120 | } else { |
| 121 | m_leName->setText(url.toString()); |
| 122 | } |
| 123 | m_fileNameEdited = false; // slotNameTextChanged set it to true erroneously |
| 124 | } |
| 125 | m_buttonBox->button(which: QDialogButtonBox::Ok)->setEnabled(!m_leName->text().isEmpty() && !m_urlRequester->url().isEmpty()); |
| 126 | } |
| 127 | |
| 128 | void KNameAndUrlInputDialog::setSuggestedName(const QString &name) |
| 129 | { |
| 130 | d->m_leName->setText(name); |
| 131 | d->m_urlRequester->setFocus(); |
| 132 | } |
| 133 | |
| 134 | void KNameAndUrlInputDialog::setSuggestedUrl(const QUrl &url) |
| 135 | { |
| 136 | d->m_urlRequester->setUrl(url); |
| 137 | } |
| 138 | |
| 139 | #include "moc_knameandurlinputdialog.cpp" |
| 140 | |