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>
21 #include <QFormLayout>
22 #include <QGridLayout>
23 #include <QHBoxLayout>
25 #include <QPushButton>
26 #include <QRadioButton>
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)
36 , m_splitView(nullptr)
37 , m_editableUrl(nullptr)
38 , m_showFullPath(nullptr)
39 , m_filterBar(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
);
54 // create 'Home URL' editor
55 m_homeUrlBoxLayoutContainer
= new QWidget(this);
56 QHBoxLayout
*homeUrlBoxLayout
= new QHBoxLayout(m_homeUrlBoxLayoutContainer
);
57 homeUrlBoxLayout
->setContentsMargins(0, 0, 0, 0);
59 m_homeUrl
= new QLineEdit();
60 m_homeUrl
->setClearButtonEnabled(true);
61 homeUrlBoxLayout
->addWidget(m_homeUrl
);
63 QPushButton
*selectHomeUrlButton
= new QPushButton(QIcon::fromTheme(QStringLiteral("folder-open")), QString());
64 homeUrlBoxLayout
->addWidget(selectHomeUrlButton
);
66 #ifndef QT_NO_ACCESSIBILITY
67 selectHomeUrlButton
->setAccessibleName(i18nc("@action:button", "Select Home Location"));
70 connect(selectHomeUrlButton
, &QPushButton::clicked
, this, &StartupSettingsPage::selectHomeUrl
);
72 m_buttonBoxLayoutContainer
= new QWidget(this);
73 QHBoxLayout
*buttonBoxLayout
= new QHBoxLayout(m_buttonBoxLayoutContainer
);
74 buttonBoxLayout
->setContentsMargins(0, 0, 0, 0);
76 QPushButton
*useCurrentButton
= new QPushButton(i18nc("@action:button", "Use Current Location"));
77 buttonBoxLayout
->addWidget(useCurrentButton
);
78 connect(useCurrentButton
, &QPushButton::clicked
, this, &StartupSettingsPage::useCurrentLocation
);
79 QPushButton
*useDefaultButton
= new QPushButton(i18nc("@action:button", "Use Default Location"));
80 buttonBoxLayout
->addWidget(useDefaultButton
);
81 connect(useDefaultButton
, &QPushButton::clicked
, this, &StartupSettingsPage::useDefaultLocation
);
83 QGridLayout
*startInLocationLayout
= new QGridLayout();
84 startInLocationLayout
->setHorizontalSpacing(0);
85 startInLocationLayout
->setContentsMargins(0, 0, 0, 0);
86 startInLocationLayout
->addWidget(m_homeUrlRadioButton
, 0, 0);
87 startInLocationLayout
->addWidget(m_homeUrlBoxLayoutContainer
, 0, 1);
88 startInLocationLayout
->addWidget(m_buttonBoxLayoutContainer
, 1, 1);
90 topLayout
->addRow(i18nc("@label:textbox", "Show on startup:"), m_rememberOpenedTabsRadioButton
);
91 topLayout
->addRow(QString(), startInLocationLayout
);
93 topLayout
->addItem(new QSpacerItem(0, Dolphin::VERTICAL_SPACER_HEIGHT
, QSizePolicy::Fixed
, QSizePolicy::Fixed
));
95 m_splitView
= new QCheckBox(i18nc("@option:check Startup Settings", "Begin in split view mode"));
96 topLayout
->addRow(i18n("New windows:"), m_splitView
);
97 m_filterBar
= new QCheckBox(i18nc("@option:check Startup Settings", "Show filter bar"));
98 topLayout
->addRow(QString(), m_filterBar
);
99 m_editableUrl
= new QCheckBox(i18nc("@option:check Startup Settings", "Make location bar editable"));
100 topLayout
->addRow(QString(), m_editableUrl
);
102 topLayout
->addItem(new QSpacerItem(0, Dolphin::VERTICAL_SPACER_HEIGHT
, QSizePolicy::Fixed
, QSizePolicy::Fixed
));
104 m_openExternallyCalledFolderInNewTab
= new QCheckBox(i18nc("@option:check Startup Settings", "Open new folders in tabs"));
105 topLayout
->addRow(i18nc("@label:checkbox", "General:"), m_openExternallyCalledFolderInNewTab
);
106 m_showFullPath
= new QCheckBox(i18nc("@option:check Startup Settings", "Show full path inside location bar"));
107 topLayout
->addRow(QString(), m_showFullPath
);
108 m_showFullPathInTitlebar
= new QCheckBox(i18nc("@option:check Startup Settings", "Show full path in title bar"));
109 topLayout
->addRow(QString(), m_showFullPathInTitlebar
);
113 updateInitialViewOptions();
115 connect(m_homeUrl
, &QLineEdit::textChanged
, this, &StartupSettingsPage::slotSettingsChanged
);
116 connect(m_rememberOpenedTabsRadioButton
, &QRadioButton::toggled
, this, &StartupSettingsPage::slotSettingsChanged
);
117 connect(m_homeUrlRadioButton
, &QRadioButton::toggled
, this, &StartupSettingsPage::slotSettingsChanged
);
119 connect(m_splitView
, &QCheckBox::toggled
, this, &StartupSettingsPage::slotSettingsChanged
);
120 connect(m_editableUrl
, &QCheckBox::toggled
, this, &StartupSettingsPage::slotSettingsChanged
);
121 connect(m_filterBar
, &QCheckBox::toggled
, this, &StartupSettingsPage::slotSettingsChanged
);
123 connect(m_openExternallyCalledFolderInNewTab
, &QCheckBox::toggled
, this, &StartupSettingsPage::slotSettingsChanged
);
124 connect(m_showFullPath
, &QCheckBox::toggled
, this, &StartupSettingsPage::slotSettingsChanged
);
125 connect(m_showFullPathInTitlebar
, &QCheckBox::toggled
, this, &StartupSettingsPage::slotSettingsChanged
);
128 StartupSettingsPage::~StartupSettingsPage()
132 void StartupSettingsPage::applySettings()
134 GeneralSettings
*settings
= GeneralSettings::self();
136 const QUrl
url(QUrl::fromUserInput(m_homeUrl
->text(), QString(), QUrl::AssumeLocalFile
));
137 if (url
.isValid() && KProtocolManager::supportsListing(url
)) {
138 KIO::StatJob
*job
= KIO::statDetails(url
, KIO::StatJob::SourceSide
, KIO::StatDetail::StatBasic
, KIO::JobFlag::HideProgressInfo
);
139 connect(job
, &KJob::result
, this, [this, settings
, url
](KJob
*job
) {
140 if (job
->error() == 0 && qobject_cast
<KIO::StatJob
*>(job
)->statResult().isDir()) {
141 settings
->setHomeUrl(url
.toDisplayString(QUrl::PreferLocalFile
));
143 showSetDefaultDirectoryError();
147 showSetDefaultDirectoryError();
150 // Remove saved state if "remember open tabs" has been turned off
151 if (!m_rememberOpenedTabsRadioButton
->isChecked()) {
152 KConfigGroup windowState
{KSharedConfig::openConfig(QStringLiteral("dolphinrc")), "WindowState"};
153 if (windowState
.exists()) {
154 windowState
.deleteGroup();
158 settings
->setRememberOpenedTabs(m_rememberOpenedTabsRadioButton
->isChecked());
159 settings
->setSplitView(m_splitView
->isChecked());
160 settings
->setEditableUrl(m_editableUrl
->isChecked());
161 settings
->setFilterBar(m_filterBar
->isChecked());
162 settings
->setOpenExternallyCalledFolderInNewTab(m_openExternallyCalledFolderInNewTab
->isChecked());
163 settings
->setShowFullPath(m_showFullPath
->isChecked());
164 settings
->setShowFullPathInTitlebar(m_showFullPathInTitlebar
->isChecked());
168 void StartupSettingsPage::restoreDefaults()
170 GeneralSettings
*settings
= GeneralSettings::self();
171 settings
->useDefaults(true);
173 settings
->useDefaults(false);
176 void StartupSettingsPage::slotSettingsChanged()
178 // Provide a hint that the startup settings have been changed. This allows the views
179 // to apply the startup settings only if they have been explicitly changed by the user
180 // (see bug #254947).
181 GeneralSettings::setModifiedStartupSettings(true);
183 // Enable and disable home URL controls appropriately
184 updateInitialViewOptions();
188 void StartupSettingsPage::updateInitialViewOptions()
190 m_homeUrlBoxLayoutContainer
->setEnabled(m_homeUrlRadioButton
->isChecked());
191 m_buttonBoxLayoutContainer
->setEnabled(m_homeUrlRadioButton
->isChecked());
194 void StartupSettingsPage::selectHomeUrl()
196 const QUrl
homeUrl(QUrl::fromUserInput(m_homeUrl
->text(), QString(), QUrl::AssumeLocalFile
));
197 QUrl url
= QFileDialog::getExistingDirectoryUrl(this, QString(), homeUrl
);
198 if (!url
.isEmpty()) {
199 m_homeUrl
->setText(url
.toDisplayString(QUrl::PreferLocalFile
));
200 slotSettingsChanged();
204 void StartupSettingsPage::useCurrentLocation()
206 m_homeUrl
->setText(m_url
.toDisplayString(QUrl::PreferLocalFile
));
209 void StartupSettingsPage::useDefaultLocation()
211 m_homeUrl
->setText(QDir::homePath());
214 void StartupSettingsPage::loadSettings()
216 const QUrl
url(Dolphin::homeUrl());
217 m_homeUrl
->setText(url
.toDisplayString(QUrl::PreferLocalFile
));
218 m_rememberOpenedTabsRadioButton
->setChecked(GeneralSettings::rememberOpenedTabs());
219 m_homeUrlRadioButton
->setChecked(!GeneralSettings::rememberOpenedTabs());
220 m_splitView
->setChecked(GeneralSettings::splitView());
221 m_editableUrl
->setChecked(GeneralSettings::editableUrl());
222 m_showFullPath
->setChecked(GeneralSettings::showFullPath());
223 m_filterBar
->setChecked(GeneralSettings::filterBar());
224 m_showFullPathInTitlebar
->setChecked(GeneralSettings::showFullPathInTitlebar());
225 m_openExternallyCalledFolderInNewTab
->setChecked(GeneralSettings::openExternallyCalledFolderInNewTab());
228 void StartupSettingsPage::showSetDefaultDirectoryError()
230 KMessageBox::error(this, i18nc("@info", "The location for the home folder is invalid or does not exist, it will not be applied."));
233 #include "moc_startupsettingspage.cpp"