]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/startup/startupsettingspage.cpp
GIT_SILENT Sync po/docbooks with svn
[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 <QFormLayout>
22 #include <QGridLayout>
23 #include <QHBoxLayout>
24 #include <QLineEdit>
25 #include <QPushButton>
26 #include <QRadioButton>
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 // create 'Home URL' editor
55 m_homeUrlBoxLayoutContainer = new QWidget(this);
56 QHBoxLayout *homeUrlBoxLayout = new QHBoxLayout(m_homeUrlBoxLayoutContainer);
57 homeUrlBoxLayout->setContentsMargins(0, 0, 0, 0);
58
59 m_homeUrl = new QLineEdit();
60 m_homeUrl->setClearButtonEnabled(true);
61 homeUrlBoxLayout->addWidget(m_homeUrl);
62
63 QPushButton *selectHomeUrlButton = new QPushButton(QIcon::fromTheme(QStringLiteral("folder-open")), QString());
64 homeUrlBoxLayout->addWidget(selectHomeUrlButton);
65
66 #ifndef QT_NO_ACCESSIBILITY
67 selectHomeUrlButton->setAccessibleName(i18nc("@action:button", "Select Home Location"));
68 #endif
69
70 connect(selectHomeUrlButton, &QPushButton::clicked, this, &StartupSettingsPage::selectHomeUrl);
71
72 m_buttonBoxLayoutContainer = new QWidget(this);
73 QHBoxLayout *buttonBoxLayout = new QHBoxLayout(m_buttonBoxLayoutContainer);
74 buttonBoxLayout->setContentsMargins(0, 0, 0, 0);
75
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);
82
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);
89
90 topLayout->addRow(i18nc("@label:textbox", "Show on startup:"), m_rememberOpenedTabsRadioButton);
91 topLayout->addRow(QString(), startInLocationLayout);
92
93 topLayout->addItem(new QSpacerItem(0, Dolphin::VERTICAL_SPACER_HEIGHT, QSizePolicy::Fixed, QSizePolicy::Fixed));
94
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);
101
102 topLayout->addItem(new QSpacerItem(0, Dolphin::VERTICAL_SPACER_HEIGHT, QSizePolicy::Fixed, QSizePolicy::Fixed));
103
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);
110
111 loadSettings();
112
113 updateInitialViewOptions();
114
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);
118
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);
122
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);
126 }
127
128 StartupSettingsPage::~StartupSettingsPage()
129 {
130 }
131
132 void StartupSettingsPage::applySettings()
133 {
134 GeneralSettings *settings = GeneralSettings::self();
135
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));
142 } else {
143 showSetDefaultDirectoryError();
144 }
145 });
146 } else {
147 showSetDefaultDirectoryError();
148 }
149
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();
155 }
156 }
157
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());
165 settings->save();
166 }
167
168 void StartupSettingsPage::restoreDefaults()
169 {
170 GeneralSettings *settings = GeneralSettings::self();
171 settings->useDefaults(true);
172 loadSettings();
173 settings->useDefaults(false);
174 }
175
176 void StartupSettingsPage::slotSettingsChanged()
177 {
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);
182
183 // Enable and disable home URL controls appropriately
184 updateInitialViewOptions();
185 Q_EMIT changed();
186 }
187
188 void StartupSettingsPage::updateInitialViewOptions()
189 {
190 m_homeUrlBoxLayoutContainer->setEnabled(m_homeUrlRadioButton->isChecked());
191 m_buttonBoxLayoutContainer->setEnabled(m_homeUrlRadioButton->isChecked());
192 }
193
194 void StartupSettingsPage::selectHomeUrl()
195 {
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();
201 }
202 }
203
204 void StartupSettingsPage::useCurrentLocation()
205 {
206 m_homeUrl->setText(m_url.toDisplayString(QUrl::PreferLocalFile));
207 }
208
209 void StartupSettingsPage::useDefaultLocation()
210 {
211 m_homeUrl->setText(QDir::homePath());
212 }
213
214 void StartupSettingsPage::loadSettings()
215 {
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());
226 }
227
228 void StartupSettingsPage::showSetDefaultDirectoryError()
229 {
230 KMessageBox::error(this, i18nc("@info", "The location for the home folder is invalid or does not exist, it will not be applied."));
231 }
232
233 #include "moc_startupsettingspage.cpp"