File indexing completed on 2024-05-12 05:46:43

0001 /*
0002     Copyright (c) 1998, 2008, 2009 David Faure <faure@kde.org>
0003 
0004     This library is free software; you can redistribute it and/or modify
0005     it under the terms of the GNU Lesser General Public License as published by
0006     the Free Software Foundation; either version 2 of the License or (at
0007     your option) version 3 or, at the discretion of KDE e.V. (which shall
0008     act as a proxy as in section 14 of the GPLv3), any later version.
0009 
0010     This library is distributed in the hope that it will be useful,
0011     but WITHOUT ANY WARRANTY; without even the implied warranty of
0012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013     Library General Public License for more details.
0014 
0015     You should have received a copy of the GNU Lesser General Public License
0016     along with this library; see the file COPYING.LIB.  If not, write to
0017     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018     Boston, MA 02110-1301, USA.
0019 */
0020 
0021 #include "knameandurlinputdialog.h"
0022 
0023 #include <klineedit.h>
0024 #include <kurlrequester.h>
0025 #include <kprotocolmanager.h>
0026 #include <QDialogButtonBox>
0027 #include <QFormLayout>
0028 #include <QVBoxLayout>
0029 
0030 class KNameAndUrlInputDialogPrivate
0031 {
0032 public:
0033     explicit KNameAndUrlInputDialogPrivate(KNameAndUrlInputDialog *qq)
0034         : m_fileNameEdited(false)
0035         , q(qq)
0036     {}
0037 
0038     void _k_slotNameTextChanged(const QString &);
0039     void _k_slotURLTextChanged(const QString &);
0040 
0041     /**
0042      * The line edit widget for the fileName
0043      */
0044     QLineEdit *m_leName;
0045     /**
0046      * The URL requester for the URL :)
0047      */
0048     KUrlRequester *m_urlRequester;
0049     /**
0050      * True if the filename was manually edited.
0051      */
0052     bool m_fileNameEdited;
0053 
0054     QDialogButtonBox *m_buttonBox;
0055 
0056     KNameAndUrlInputDialog * const q;
0057 };
0058 
0059 KNameAndUrlInputDialog::KNameAndUrlInputDialog(const QString &nameLabel, const QString &urlLabel, const QUrl &startDir, QWidget *parent)
0060     : QDialog(parent), d(new KNameAndUrlInputDialogPrivate(this))
0061 {
0062     QVBoxLayout *topLayout = new QVBoxLayout;
0063     setLayout(topLayout);
0064 
0065     QFormLayout *formLayout = new QFormLayout;
0066     formLayout->setContentsMargins(0, 0, 0, 0);
0067 
0068     // First line: filename
0069     d->m_leName = new QLineEdit(this);
0070     d->m_leName->setMinimumWidth(d->m_leName->sizeHint().width() * 3);
0071     d->m_leName->setSelection(0, d->m_leName->text().length()); // autoselect
0072     connect(d->m_leName, SIGNAL(textChanged(QString)),
0073             SLOT(_k_slotNameTextChanged(QString)));
0074     formLayout->addRow(nameLabel, d->m_leName);
0075 
0076     // Second line: url
0077     d->m_urlRequester = new KUrlRequester(this);
0078     d->m_urlRequester->setStartDir(startDir);
0079     d->m_urlRequester->setMode(KFile::File | KFile::Directory);
0080 
0081     d->m_urlRequester->setMinimumWidth(d->m_urlRequester->sizeHint().width() * 3);
0082     connect(d->m_urlRequester->lineEdit(), SIGNAL(textChanged(QString)),
0083             SLOT(_k_slotURLTextChanged(QString)));
0084     formLayout->addRow(urlLabel, d->m_urlRequester);
0085 
0086     topLayout->addLayout(formLayout);
0087 
0088     d->m_buttonBox = new QDialogButtonBox(this);
0089     d->m_buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
0090     connect(d->m_buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
0091     connect(d->m_buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
0092     topLayout->addWidget(d->m_buttonBox);
0093 
0094     d->m_fileNameEdited = false;
0095     d->m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!d->m_leName->text().isEmpty() && !d->m_urlRequester->url().isEmpty());
0096     d->m_leName->setFocus();
0097 }
0098 
0099 KNameAndUrlInputDialog::~KNameAndUrlInputDialog()
0100 {
0101     delete d;
0102 }
0103 
0104 QUrl KNameAndUrlInputDialog::url() const
0105 {
0106     return d->m_urlRequester->url();
0107 }
0108 
0109 QString KNameAndUrlInputDialog::urlText() const
0110 {
0111     return d->m_urlRequester->text();
0112 }
0113 
0114 QString KNameAndUrlInputDialog::name() const
0115 {
0116     return d->m_leName->text();
0117 }
0118 
0119 void KNameAndUrlInputDialogPrivate::_k_slotNameTextChanged(const QString &)
0120 {
0121     m_fileNameEdited = true;
0122     m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!m_leName->text().isEmpty() && !m_urlRequester->url().isEmpty());
0123 }
0124 
0125 void KNameAndUrlInputDialogPrivate::_k_slotURLTextChanged(const QString &)
0126 {
0127     if (!m_fileNameEdited) {
0128         // use URL as default value for the filename
0129         // (we copy only its filename if protocol supports listing,
0130         // but for HTTP we don't want tons of index.html links)
0131         QUrl url(m_urlRequester->url());
0132         if (KProtocolManager::supportsListing(url) && !url.fileName().isEmpty()) {
0133             m_leName->setText(url.fileName());
0134         } else {
0135             m_leName->setText(url.toString());
0136         }
0137         m_fileNameEdited = false; // slotNameTextChanged set it to true erroneously
0138     }
0139     m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!m_leName->text().isEmpty() && !m_urlRequester->url().isEmpty());
0140 }
0141 
0142 void KNameAndUrlInputDialog::setSuggestedName(const QString &name)
0143 {
0144     d->m_leName->setText(name);
0145     d->m_urlRequester->setFocus();
0146 }
0147 
0148 void KNameAndUrlInputDialog::setSuggestedUrl(const QUrl &url)
0149 {
0150     d->m_urlRequester->setUrl(url);
0151 }
0152 
0153 #include "moc_knameandurlinputdialog.cpp"