1/*
2 This file is part of the KDE libraries
3 SPDX-FileCopyrightText: 2001, 2002, 2003 Carsten Pfeiffer <pfeiffer@kde.org>
4 SPDX-FileCopyrightText: 2007 Kevin Ottens <ervin@kde.org>
5
6 SPDX-License-Identifier: LGPL-2.0-only
7*/
8
9#include "kfileplaceeditdialog.h"
10
11#include <KAboutData>
12#include <KConfig>
13#include <KIconButton>
14#include <KLineEdit> // For KUrlRequester::lineEdit()
15#include <KLocalizedString>
16#include <kio/global.h>
17#include <kprotocolinfo.h>
18#include <kurlrequester.h>
19
20#include <QApplication>
21#include <QCheckBox>
22#include <QDialogButtonBox>
23#include <QFormLayout>
24#include <qdrawutil.h>
25
26#include <KConfigGroup>
27#include <qplatformdefs.h>
28
29bool KFilePlaceEditDialog::getInformation(bool allowGlobal,
30 QUrl &url,
31 QString &label,
32 QString &icon,
33 bool isAddingNewPlace,
34 bool &appLocal,
35 int iconSize,
36 QWidget *parent)
37{
38 KFilePlaceEditDialog *dialog = new KFilePlaceEditDialog(allowGlobal, url, label, icon, isAddingNewPlace, appLocal, iconSize, parent);
39 if (dialog->exec() == QDialog::Accepted) {
40 // set the return parameters
41 url = dialog->url();
42 label = dialog->label();
43 icon = dialog->icon();
44 appLocal = dialog->applicationLocal();
45
46 delete dialog;
47 return true;
48 }
49
50 delete dialog;
51 return false;
52}
53
54KFilePlaceEditDialog::KFilePlaceEditDialog(bool allowGlobal,
55 const QUrl &url,
56 const QString &label,
57 const QString &icon,
58 bool isAddingNewPlace,
59 bool appLocal,
60 int iconSize,
61 QWidget *parent)
62 : QDialog(parent)
63 , m_iconButton(nullptr)
64{
65 if (isAddingNewPlace) {
66 setWindowTitle(i18n("Add Places Entry"));
67 } else {
68 setWindowTitle(i18n("Edit Places Entry"));
69 }
70 setModal(true);
71
72 QVBoxLayout *box = new QVBoxLayout(this);
73
74 QFormLayout *layout = new QFormLayout();
75 box->addLayout(layout);
76
77 QString whatsThisText = i18n(
78 "<qt>This is the text that will appear in the Places panel.<br /><br />"
79 "The label should consist of one or two words "
80 "that will help you remember what this entry refers to. "
81 "If you do not enter a label, it will be derived from "
82 "the location's URL.</qt>");
83 m_labelEdit = new QLineEdit(this);
84 layout->addRow(i18n("L&abel:"), field: m_labelEdit);
85 m_labelEdit->setText(label);
86 m_labelEdit->setPlaceholderText(i18n("Enter descriptive label here"));
87 m_labelEdit->setWhatsThis(whatsThisText);
88 layout->labelForField(field: m_labelEdit)->setWhatsThis(whatsThisText);
89
90 whatsThisText = i18n(
91 "<qt>This is the location associated with the entry. Any valid URL may be used. For example:<br /><br />"
92 "%1<br />http://www.kde.org<br />ftp://ftp.kde.org/pub/kde/stable<br /><br />"
93 "By clicking on the button next to the text edit box you can browse to an "
94 "appropriate URL.</qt>",
95 QDir::homePath());
96 m_urlEdit = new KUrlRequester(url, this);
97 m_urlEdit->setMode(KFile::Directory);
98 layout->addRow(i18n("&Location:"), field: m_urlEdit);
99 m_urlEdit->setWhatsThis(whatsThisText);
100 layout->labelForField(field: m_urlEdit)->setWhatsThis(whatsThisText);
101 // Room for at least 40 chars (average char width is half of height)
102 m_urlEdit->setMinimumWidth(m_urlEdit->fontMetrics().height() * (40 / 2));
103
104 whatsThisText = i18n(
105 "<qt>This is the icon that will appear in the Places panel.<br /><br />"
106 "Click on the button to select a different icon.</qt>");
107 m_iconButton = new KIconButton(this);
108 m_iconButton->setObjectName(QStringLiteral("icon button"));
109 m_iconButton->setIconSize(iconSize);
110 m_iconButton->setIconType(group: KIconLoader::NoGroup, context: KIconLoader::Place);
111 if (icon.isEmpty()) {
112 m_iconButton->setIcon(KIO::iconNameForUrl(url));
113 } else {
114 m_iconButton->setIcon(icon);
115 }
116 m_iconButton->setWhatsThis(whatsThisText);
117
118 if (url.scheme() == QLatin1String("trash")) {
119 // Since there are separate trash icons when it is empty/non-empty,
120 // the trash item's icon is made non-editable for simplicity
121 m_iconButton->hide();
122 // making the trash item's url editable misleads users into
123 // thinking that the actual trash location is configurable here
124 m_urlEdit->setDisabled(true);
125 } else {
126 layout->addRow(i18n("Choose an &icon:"), field: m_iconButton);
127 layout->labelForField(field: m_iconButton)->setWhatsThis(whatsThisText);
128 }
129
130 if (allowGlobal) {
131 QString appName;
132 appName = QGuiApplication::applicationDisplayName();
133 if (appName.isEmpty()) {
134 appName = QCoreApplication::applicationName();
135 }
136 m_appLocal = new QCheckBox(i18n("&Only show when using this application (%1)", appName), this);
137 m_appLocal->setChecked(appLocal);
138 m_appLocal->setWhatsThis(
139 i18n("<qt>Select this setting if you want this "
140 "entry to show only when using the current application (%1).<br /><br />"
141 "If this setting is not selected, the entry will be available in all "
142 "applications.</qt>",
143 appName));
144 box->addWidget(m_appLocal);
145 } else {
146 m_appLocal = nullptr;
147 }
148 connect(sender: m_urlEdit->lineEdit(), signal: &QLineEdit::textChanged, context: this, slot: &KFilePlaceEditDialog::urlChanged);
149 if (!label.isEmpty()) {
150 // editing existing entry
151 m_labelEdit->setFocus();
152 } else {
153 // new entry
154 m_urlEdit->setFocus();
155 }
156
157 m_buttonBox = new QDialogButtonBox(this);
158 m_buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
159 connect(sender: m_buttonBox, signal: &QDialogButtonBox::accepted, context: this, slot: &QDialog::accept);
160 connect(sender: m_buttonBox, signal: &QDialogButtonBox::rejected, context: this, slot: &QDialog::reject);
161 box->addWidget(m_buttonBox);
162}
163
164KFilePlaceEditDialog::~KFilePlaceEditDialog()
165{
166}
167
168void KFilePlaceEditDialog::urlChanged(const QString &text)
169{
170 m_buttonBox->button(which: QDialogButtonBox::Ok)->setEnabled(!text.isEmpty());
171}
172
173QUrl KFilePlaceEditDialog::url() const
174{
175 return m_urlEdit->url();
176}
177
178QString KFilePlaceEditDialog::label() const
179{
180 if (!m_labelEdit->text().isEmpty()) {
181 return m_labelEdit->text();
182 }
183
184 // derive descriptive label from the URL
185 QUrl url = m_urlEdit->url();
186 if (!url.fileName().isEmpty()) {
187 return url.fileName();
188 }
189 if (!url.host().isEmpty()) {
190 return url.host();
191 }
192 return url.scheme();
193}
194
195QString KFilePlaceEditDialog::icon() const
196{
197 return m_iconButton->icon();
198}
199
200bool KFilePlaceEditDialog::applicationLocal() const
201{
202 if (!m_appLocal) {
203 return true;
204 }
205
206 return m_appLocal->isChecked();
207}
208
209#include "moc_kfileplaceeditdialog.cpp"
210

source code of kio/src/filewidgets/kfileplaceeditdialog.cpp