2 * SPDX-FileCopyrightText: 2008 Peter Penz <peter.penz19@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0-or-later
7 #include "startupsettingspage.h"
9 #include "dolphin_generalsettings.h"
10 #include "dolphinmainwindow.h"
11 #include "dolphinviewcontainer.h"
14 #include <KLocalizedString>
15 #include <KMessageBox>
16 #include <KProtocolManager>
18 #include <QButtonGroup>
20 #include <QFileDialog>
22 #include <QPushButton>
23 #include <QRadioButton>
24 #include <QFormLayout>
25 #include <QGridLayout>
26 #include <QHBoxLayout>
28 StartupSettingsPage::StartupSettingsPage(const QUrl
& url
, QWidget
* parent
) :
29 SettingsPageBase(parent
),
32 m_homeUrlBoxLayoutContainer(nullptr),
33 m_buttonBoxLayoutContainer(nullptr),
34 m_rememberOpenedTabsRadioButton(nullptr),
35 m_homeUrlRadioButton(nullptr),
37 m_editableUrl(nullptr),
38 m_showFullPath(nullptr),
40 m_showFullPathInTitlebar(nullptr),
41 m_openExternallyCalledFolderInNewTab(nullptr)
43 QFormLayout
* topLayout
= new QFormLayout(this);
45 m_rememberOpenedTabsRadioButton
= new QRadioButton(i18nc("@option:radio Startup Settings", "Folders, tabs, and window state from last time"));
46 m_homeUrlRadioButton
= new QRadioButton();
47 // HACK: otherwise the radio button has too much spacing in a grid layout
48 m_homeUrlRadioButton
->setMaximumWidth(24);
50 QButtonGroup
* initialViewGroup
= new QButtonGroup(this);
51 initialViewGroup
->addButton(m_rememberOpenedTabsRadioButton
);
52 initialViewGroup
->addButton(m_homeUrlRadioButton
);
55 // create 'Home URL' editor
56 m_homeUrlBoxLayoutContainer
= new QWidget(this);
57 QHBoxLayout
* homeUrlBoxLayout
= new QHBoxLayout(m_homeUrlBoxLayoutContainer
);
58 homeUrlBoxLayout
->setContentsMargins(0, 0, 0, 0);
60 m_homeUrl
= new QLineEdit();
61 m_homeUrl
->setClearButtonEnabled(true);
62 homeUrlBoxLayout
->addWidget(m_homeUrl
);
64 QPushButton
* selectHomeUrlButton
= new QPushButton(QIcon::fromTheme(QStringLiteral("folder-open")), QString());
65 homeUrlBoxLayout
->addWidget(selectHomeUrlButton
);
67 #ifndef QT_NO_ACCESSIBILITY
68 selectHomeUrlButton
->setAccessibleName(i18nc("@action:button", "Select Home Location"));
71 connect(selectHomeUrlButton
, &QPushButton::clicked
,
72 this, &StartupSettingsPage::selectHomeUrl
);
74 m_buttonBoxLayoutContainer
= new QWidget(this);
75 QHBoxLayout
* buttonBoxLayout
= new QHBoxLayout(m_buttonBoxLayoutContainer
);
76 buttonBoxLayout
->setContentsMargins(0, 0, 0, 0);
78 QPushButton
* useCurrentButton
= new QPushButton(i18nc("@action:button", "Use Current Location"));
79 buttonBoxLayout
->addWidget(useCurrentButton
);
80 connect(useCurrentButton
, &QPushButton::clicked
,
81 this, &StartupSettingsPage::useCurrentLocation
);
82 QPushButton
* useDefaultButton
= new QPushButton(i18nc("@action:button", "Use Default Location"));
83 buttonBoxLayout
->addWidget(useDefaultButton
);
84 connect(useDefaultButton
, &QPushButton::clicked
,
85 this, &StartupSettingsPage::useDefaultLocation
);
87 QGridLayout
* startInLocationLayout
= new QGridLayout();
88 startInLocationLayout
->setHorizontalSpacing(0);
89 startInLocationLayout
->setContentsMargins(0, 0, 0, 0);
90 startInLocationLayout
->addWidget(m_homeUrlRadioButton
, 0, 0);
91 startInLocationLayout
->addWidget(m_homeUrlBoxLayoutContainer
, 0, 1);
92 startInLocationLayout
->addWidget(m_buttonBoxLayoutContainer
, 1, 1);
94 topLayout
->addRow(i18nc("@label:textbox", "Show on startup:"), m_rememberOpenedTabsRadioButton
);
95 topLayout
->addRow(QString(), startInLocationLayout
);
98 topLayout
->addItem(new QSpacerItem(0, Dolphin::VERTICAL_SPACER_HEIGHT
, QSizePolicy::Fixed
, QSizePolicy::Fixed
));
100 m_splitView
= new QCheckBox(i18nc("@option:check Startup Settings", "Begin in split view mode"));
101 topLayout
->addRow(i18n("New windows:"), m_splitView
);
102 m_filterBar
= new QCheckBox(i18nc("@option:check Startup Settings", "Show filter bar"));
103 topLayout
->addRow(QString(), m_filterBar
);
104 m_editableUrl
= new QCheckBox(i18nc("@option:check Startup Settings", "Make location bar editable"));
105 topLayout
->addRow(QString(), m_editableUrl
);
107 topLayout
->addItem(new QSpacerItem(0, Dolphin::VERTICAL_SPACER_HEIGHT
, QSizePolicy::Fixed
, QSizePolicy::Fixed
));
109 m_openExternallyCalledFolderInNewTab
= new QCheckBox(i18nc("@option:check Startup Settings", "Open new folders in tabs"));
110 topLayout
->addRow(i18nc("@label:checkbox", "General:"), m_openExternallyCalledFolderInNewTab
);
111 m_showFullPath
= new QCheckBox(i18nc("@option:check Startup Settings", "Show full path inside location bar"));
112 topLayout
->addRow(QString(), m_showFullPath
);
113 m_showFullPathInTitlebar
= new QCheckBox(i18nc("@option:check Startup Settings", "Show full path in title bar"));
114 topLayout
->addRow(QString(), m_showFullPathInTitlebar
);
118 updateInitialViewOptions();
120 connect(m_homeUrl
, &QLineEdit::textChanged
, this, &StartupSettingsPage::slotSettingsChanged
);
121 connect(m_rememberOpenedTabsRadioButton
, &QRadioButton::toggled
, this, &StartupSettingsPage::slotSettingsChanged
);
122 connect(m_homeUrlRadioButton
, &QRadioButton::toggled
, this, &StartupSettingsPage::slotSettingsChanged
);
124 connect(m_splitView
, &QCheckBox::toggled
, this, &StartupSettingsPage::slotSettingsChanged
);
125 connect(m_editableUrl
, &QCheckBox::toggled
, this, &StartupSettingsPage::slotSettingsChanged
);
126 connect(m_filterBar
, &QCheckBox::toggled
, this, &StartupSettingsPage::slotSettingsChanged
);
128 connect(m_openExternallyCalledFolderInNewTab
, &QCheckBox::toggled
, this, &StartupSettingsPage::slotSettingsChanged
);
129 connect(m_showFullPath
, &QCheckBox::toggled
, this, &StartupSettingsPage::slotSettingsChanged
);
130 connect(m_showFullPathInTitlebar
, &QCheckBox::toggled
, this, &StartupSettingsPage::slotSettingsChanged
);
133 StartupSettingsPage::~StartupSettingsPage()
137 void StartupSettingsPage::applySettings()
139 GeneralSettings
* settings
= GeneralSettings::self();
141 const QUrl
url(QUrl::fromUserInput(m_homeUrl
->text(), QString(), QUrl::AssumeLocalFile
));
142 if (url
.isValid() && KProtocolManager::supportsListing(url
)) {
143 KIO::StatJob
* job
= KIO::statDetails(url
, KIO::StatJob::SourceSide
, KIO::StatDetail::StatBasic
, KIO::JobFlag::HideProgressInfo
);
144 connect(job
, &KJob::result
, this, [this, settings
, url
](KJob
* job
) {
145 if (job
->error() == 0 && qobject_cast
<KIO::StatJob
*>(job
)->statResult().isDir()) {
146 settings
->setHomeUrl(url
.toDisplayString(QUrl::PreferLocalFile
));
148 showSetDefaultDirectoryError();
152 showSetDefaultDirectoryError();
155 // Remove saved state if "remember open tabs" has been turned off
156 if (!m_rememberOpenedTabsRadioButton
->isChecked()) {
157 KConfigGroup windowState
{KSharedConfig::openConfig(QStringLiteral("dolphinrc")), "WindowState"};
158 if (windowState
.exists()) {
159 windowState
.deleteGroup();
163 settings
->setRememberOpenedTabs(m_rememberOpenedTabsRadioButton
->isChecked());
164 settings
->setSplitView(m_splitView
->isChecked());
165 settings
->setEditableUrl(m_editableUrl
->isChecked());
166 settings
->setFilterBar(m_filterBar
->isChecked());
167 settings
->setOpenExternallyCalledFolderInNewTab(m_openExternallyCalledFolderInNewTab
->isChecked());
168 settings
->setShowFullPath(m_showFullPath
->isChecked());
169 settings
->setShowFullPathInTitlebar(m_showFullPathInTitlebar
->isChecked());
173 void StartupSettingsPage::restoreDefaults()
175 GeneralSettings
* settings
= GeneralSettings::self();
176 settings
->useDefaults(true);
178 settings
->useDefaults(false);
181 void StartupSettingsPage::slotSettingsChanged()
183 // Provide a hint that the startup settings have been changed. This allows the views
184 // to apply the startup settings only if they have been explicitly changed by the user
185 // (see bug #254947).
186 GeneralSettings::setModifiedStartupSettings(true);
188 // Enable and disable home URL controls appropriately
189 updateInitialViewOptions();
193 void StartupSettingsPage::updateInitialViewOptions()
195 m_homeUrlBoxLayoutContainer
->setEnabled(m_homeUrlRadioButton
->isChecked());
196 m_buttonBoxLayoutContainer
->setEnabled(m_homeUrlRadioButton
->isChecked());
199 void StartupSettingsPage::selectHomeUrl()
201 const QUrl
homeUrl(QUrl::fromUserInput(m_homeUrl
->text(), QString(), QUrl::AssumeLocalFile
));
202 QUrl url
= QFileDialog::getExistingDirectoryUrl(this, QString(), homeUrl
);
203 if (!url
.isEmpty()) {
204 m_homeUrl
->setText(url
.toDisplayString(QUrl::PreferLocalFile
));
205 slotSettingsChanged();
209 void StartupSettingsPage::useCurrentLocation()
211 m_homeUrl
->setText(m_url
.toDisplayString(QUrl::PreferLocalFile
));
214 void StartupSettingsPage::useDefaultLocation()
216 m_homeUrl
->setText(QDir::homePath());
219 void StartupSettingsPage::loadSettings()
221 const QUrl
url(Dolphin::homeUrl());
222 m_homeUrl
->setText(url
.toDisplayString(QUrl::PreferLocalFile
));
223 m_rememberOpenedTabsRadioButton
->setChecked(GeneralSettings::rememberOpenedTabs());
224 m_homeUrlRadioButton
->setChecked(!GeneralSettings::rememberOpenedTabs());
225 m_splitView
->setChecked(GeneralSettings::splitView());
226 m_editableUrl
->setChecked(GeneralSettings::editableUrl());
227 m_showFullPath
->setChecked(GeneralSettings::showFullPath());
228 m_filterBar
->setChecked(GeneralSettings::filterBar());
229 m_showFullPathInTitlebar
->setChecked(GeneralSettings::showFullPathInTitlebar());
230 m_openExternallyCalledFolderInNewTab
->setChecked(GeneralSettings::openExternallyCalledFolderInNewTab());
233 void StartupSettingsPage::showSetDefaultDirectoryError()
235 KMessageBox::error(this, i18nc("@info", "The location for the home folder is invalid or does not exist, it will not be applied."));