File indexing completed on 2023-10-01 04:05:46

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2001, 2002, 2003 Carsten Pfeiffer <pfeiffer@kde.org>
0004     SPDX-FileCopyrightText: 2007 Kevin Ottens <ervin@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-only
0007 */
0008 
0009 #include "kfileplaceeditdialog.h"
0010 
0011 #include <KAboutData>
0012 #include <KConfig>
0013 #include <KIconButton>
0014 #include <KLineEdit> // For KUrlRequester::lineEdit()
0015 #include <KLocalizedString>
0016 #include <kio/global.h>
0017 #include <kprotocolinfo.h>
0018 #include <kurlrequester.h>
0019 
0020 #include <QApplication>
0021 #include <QCheckBox>
0022 #include <QDialogButtonBox>
0023 #include <QFormLayout>
0024 #include <qdrawutil.h>
0025 
0026 #include <KConfigGroup>
0027 #include <qplatformdefs.h>
0028 
0029 bool KFilePlaceEditDialog::getInformation(bool allowGlobal,
0030                                           QUrl &url,
0031                                           QString &label,
0032                                           QString &icon,
0033                                           bool isAddingNewPlace,
0034                                           bool &appLocal,
0035                                           int iconSize,
0036                                           QWidget *parent)
0037 {
0038     KFilePlaceEditDialog *dialog = new KFilePlaceEditDialog(allowGlobal, url, label, icon, isAddingNewPlace, appLocal, iconSize, parent);
0039     if (dialog->exec() == QDialog::Accepted) {
0040         // set the return parameters
0041         url = dialog->url();
0042         label = dialog->label();
0043         icon = dialog->icon();
0044         appLocal = dialog->applicationLocal();
0045 
0046         delete dialog;
0047         return true;
0048     }
0049 
0050     delete dialog;
0051     return false;
0052 }
0053 
0054 KFilePlaceEditDialog::KFilePlaceEditDialog(bool allowGlobal,
0055                                            const QUrl &url,
0056                                            const QString &label,
0057                                            const QString &icon,
0058                                            bool isAddingNewPlace,
0059                                            bool appLocal,
0060                                            int iconSize,
0061                                            QWidget *parent)
0062     : QDialog(parent)
0063     , m_iconButton(nullptr)
0064 {
0065     if (isAddingNewPlace) {
0066         setWindowTitle(i18n("Add Places Entry"));
0067     } else {
0068         setWindowTitle(i18n("Edit Places Entry"));
0069     }
0070     setModal(true);
0071 
0072     QVBoxLayout *box = new QVBoxLayout(this);
0073 
0074     QFormLayout *layout = new QFormLayout();
0075     box->addLayout(layout);
0076 
0077     QString whatsThisText = i18n(
0078         "<qt>This is the text that will appear in the Places panel.<br /><br />"
0079         "The label should consist of one or two words "
0080         "that will help you remember what this entry refers to. "
0081         "If you do not enter a label, it will be derived from "
0082         "the location's URL.</qt>");
0083     m_labelEdit = new QLineEdit(this);
0084     layout->addRow(i18n("L&abel:"), m_labelEdit);
0085     m_labelEdit->setText(label);
0086     m_labelEdit->setPlaceholderText(i18n("Enter descriptive label here"));
0087     m_labelEdit->setWhatsThis(whatsThisText);
0088     layout->labelForField(m_labelEdit)->setWhatsThis(whatsThisText);
0089 
0090     whatsThisText = i18n(
0091         "<qt>This is the location associated with the entry. Any valid URL may be used. For example:<br /><br />"
0092         "%1<br />http://www.kde.org<br />ftp://ftp.kde.org/pub/kde/stable<br /><br />"
0093         "By clicking on the button next to the text edit box you can browse to an "
0094         "appropriate URL.</qt>",
0095         QDir::homePath());
0096     m_urlEdit = new KUrlRequester(url, this);
0097     m_urlEdit->setMode(KFile::Directory);
0098     layout->addRow(i18n("&Location:"), m_urlEdit);
0099     m_urlEdit->setWhatsThis(whatsThisText);
0100     layout->labelForField(m_urlEdit)->setWhatsThis(whatsThisText);
0101     // Room for at least 40 chars (average char width is half of height)
0102     m_urlEdit->setMinimumWidth(m_urlEdit->fontMetrics().height() * (40 / 2));
0103 
0104     whatsThisText = i18n(
0105         "<qt>This is the icon that will appear in the Places panel.<br /><br />"
0106         "Click on the button to select a different icon.</qt>");
0107     m_iconButton = new KIconButton(this);
0108     m_iconButton->setObjectName(QStringLiteral("icon button"));
0109     m_iconButton->setIconSize(iconSize);
0110     m_iconButton->setIconType(KIconLoader::NoGroup, KIconLoader::Place);
0111     if (icon.isEmpty()) {
0112         m_iconButton->setIcon(KIO::iconNameForUrl(url));
0113     } else {
0114         m_iconButton->setIcon(icon);
0115     }
0116     m_iconButton->setWhatsThis(whatsThisText);
0117 
0118     if (url.scheme() == QLatin1String("trash")) {
0119         // Since there are separate trash icons when it is empty/non-empty,
0120         // the trash item's icon is made non-editable for simplicity
0121         m_iconButton->hide();
0122         // making the trash item's url editable misleads users into
0123         // thinking that the actual trash location is configurable here
0124         m_urlEdit->setDisabled(true);
0125     } else {
0126         layout->addRow(i18n("Choose an &icon:"), m_iconButton);
0127         layout->labelForField(m_iconButton)->setWhatsThis(whatsThisText);
0128     }
0129 
0130     if (allowGlobal) {
0131         QString appName;
0132         appName = QGuiApplication::applicationDisplayName();
0133         if (appName.isEmpty()) {
0134             appName = QCoreApplication::applicationName();
0135         }
0136         m_appLocal = new QCheckBox(i18n("&Only show when using this application (%1)", appName), this);
0137         m_appLocal->setChecked(appLocal);
0138         m_appLocal->setWhatsThis(
0139             i18n("<qt>Select this setting if you want this "
0140                  "entry to show only when using the current application (%1).<br /><br />"
0141                  "If this setting is not selected, the entry will be available in all "
0142                  "applications.</qt>",
0143                  appName));
0144         box->addWidget(m_appLocal);
0145     } else {
0146         m_appLocal = nullptr;
0147     }
0148     connect(m_urlEdit->lineEdit(), &QLineEdit::textChanged, this, &KFilePlaceEditDialog::urlChanged);
0149     if (!label.isEmpty()) {
0150         // editing existing entry
0151         m_labelEdit->setFocus();
0152     } else {
0153         // new entry
0154         m_urlEdit->setFocus();
0155     }
0156 
0157     m_buttonBox = new QDialogButtonBox(this);
0158     m_buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
0159     connect(m_buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
0160     connect(m_buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
0161     box->addWidget(m_buttonBox);
0162 }
0163 
0164 KFilePlaceEditDialog::~KFilePlaceEditDialog()
0165 {
0166 }
0167 
0168 void KFilePlaceEditDialog::urlChanged(const QString &text)
0169 {
0170     m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!text.isEmpty());
0171 }
0172 
0173 QUrl KFilePlaceEditDialog::url() const
0174 {
0175     return m_urlEdit->url();
0176 }
0177 
0178 QString KFilePlaceEditDialog::label() const
0179 {
0180     if (!m_labelEdit->text().isEmpty()) {
0181         return m_labelEdit->text();
0182     }
0183 
0184     // derive descriptive label from the URL
0185     QUrl url = m_urlEdit->url();
0186     if (!url.fileName().isEmpty()) {
0187         return url.fileName();
0188     }
0189     if (!url.host().isEmpty()) {
0190         return url.host();
0191     }
0192     return url.scheme();
0193 }
0194 
0195 QString KFilePlaceEditDialog::icon() const
0196 {
0197     return m_iconButton->icon();
0198 }
0199 
0200 bool KFilePlaceEditDialog::applicationLocal() const
0201 {
0202     if (!m_appLocal) {
0203         return true;
0204     }
0205 
0206     return m_appLocal->isChecked();
0207 }
0208 
0209 #if KIOFILEWIDGETS_BUILD_DEPRECATED_SINCE(5, 86)
0210 bool KFilePlaceEditDialog::isIconEditable() const
0211 {
0212     return url().scheme() != QLatin1String("trash");
0213 }
0214 #endif
0215 
0216 #include "moc_kfileplaceeditdialog.cpp"