]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/startup/startupsettingspage.cpp
Merge branch 'release/20.12'
[dolphin.git] / src / settings / startup / startupsettingspage.cpp
1 /*
2 * SPDX-FileCopyrightText: 2008 Peter Penz <peter.penz19@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #include "startupsettingspage.h"
8
9 #include "dolphin_generalsettings.h"
10 #include "dolphinmainwindow.h"
11 #include "dolphinviewcontainer.h"
12 #include "global.h"
13
14 #include <KLocalizedString>
15 #include <KMessageBox>
16 #include <KProtocolManager>
17
18 #include <QButtonGroup>
19 #include <QCheckBox>
20 #include <QFileDialog>
21 #include <QLineEdit>
22 #include <QPushButton>
23 #include <QRadioButton>
24 #include <QFormLayout>
25 #include <QGridLayout>
26 #include <QHBoxLayout>
27
28 StartupSettingsPage::StartupSettingsPage(const QUrl& url, QWidget* parent) :
29 SettingsPageBase(parent),
30 m_url(url),
31 m_homeUrl(nullptr),
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)
42 {
43 QFormLayout* topLayout = new QFormLayout(this);
44
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);
49
50 QButtonGroup* initialViewGroup = new QButtonGroup(this);
51 initialViewGroup->addButton(m_rememberOpenedTabsRadioButton);
52 initialViewGroup->addButton(m_homeUrlRadioButton);
53
54
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);
59
60 m_homeUrl = new QLineEdit();
61 m_homeUrl->setClearButtonEnabled(true);
62 homeUrlBoxLayout->addWidget(m_homeUrl);
63
64 QPushButton* selectHomeUrlButton = new QPushButton(QIcon::fromTheme(QStringLiteral("folder-open")), QString());
65 homeUrlBoxLayout->addWidget(selectHomeUrlButton);
66
67 #ifndef QT_NO_ACCESSIBILITY
68 selectHomeUrlButton->setAccessibleName(i18nc("@action:button", "Select Home Location"));
69 #endif
70
71 connect(selectHomeUrlButton, &QPushButton::clicked,
72 this, &StartupSettingsPage::selectHomeUrl);
73
74 m_buttonBoxLayoutContainer = new QWidget(this);
75 QHBoxLayout* buttonBoxLayout = new QHBoxLayout(m_buttonBoxLayoutContainer);
76 buttonBoxLayout->setContentsMargins(0, 0, 0, 0);
77
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);
86
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);
93
94 topLayout->addRow(i18nc("@label:textbox", "Show on startup:"), m_rememberOpenedTabsRadioButton);
95 topLayout->addRow(QString(), startInLocationLayout);
96
97
98 topLayout->addItem(new QSpacerItem(0, Dolphin::VERTICAL_SPACER_HEIGHT, QSizePolicy::Fixed, QSizePolicy::Fixed));
99
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);
106
107 topLayout->addItem(new QSpacerItem(0, Dolphin::VERTICAL_SPACER_HEIGHT, QSizePolicy::Fixed, QSizePolicy::Fixed));
108
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);
115
116 loadSettings();
117
118 updateInitialViewOptions();
119
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);
123
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);
127
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);
131 }
132
133 StartupSettingsPage::~StartupSettingsPage()
134 {
135 }
136
137 void StartupSettingsPage::applySettings()
138 {
139 GeneralSettings* settings = GeneralSettings::self();
140
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));
147 } else {
148 showSetDefaultDirectoryError();
149 }
150 });
151 } else {
152 showSetDefaultDirectoryError();
153 }
154
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();
160 }
161 }
162
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());
170 settings->save();
171 }
172
173 void StartupSettingsPage::restoreDefaults()
174 {
175 GeneralSettings* settings = GeneralSettings::self();
176 settings->useDefaults(true);
177 loadSettings();
178 settings->useDefaults(false);
179 }
180
181 void StartupSettingsPage::slotSettingsChanged()
182 {
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);
187
188 // Enable and disable home URL controls appropriately
189 updateInitialViewOptions();
190 Q_EMIT changed();
191 }
192
193 void StartupSettingsPage::updateInitialViewOptions()
194 {
195 m_homeUrlBoxLayoutContainer->setEnabled(m_homeUrlRadioButton->isChecked());
196 m_buttonBoxLayoutContainer->setEnabled(m_homeUrlRadioButton->isChecked());
197 }
198
199 void StartupSettingsPage::selectHomeUrl()
200 {
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();
206 }
207 }
208
209 void StartupSettingsPage::useCurrentLocation()
210 {
211 m_homeUrl->setText(m_url.toDisplayString(QUrl::PreferLocalFile));
212 }
213
214 void StartupSettingsPage::useDefaultLocation()
215 {
216 m_homeUrl->setText(QDir::homePath());
217 }
218
219 void StartupSettingsPage::loadSettings()
220 {
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());
231 }
232
233 void StartupSettingsPage::showSetDefaultDirectoryError()
234 {
235 KMessageBox::error(this, i18nc("@info", "The location for the home folder is invalid or does not exist, it will not be applied."));
236 }