]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/dolphinsettingsdialog.cpp
Merge branch 'master' into kf6
[dolphin.git] / src / settings / dolphinsettingsdialog.cpp
1 /*
2 * SPDX-FileCopyrightText: 2006 Peter Penz <peter.penz@gmx.at>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #include "dolphinsettingsdialog.h"
8
9 #include "config-dolphin.h"
10 #include "contextmenu/contextmenusettingspage.h"
11 #include "dolphin_generalsettings.h"
12 #include "dolphinmainwindow.h"
13 #include "general/generalsettingspage.h"
14 #include "navigation/navigationsettingspage.h"
15 #include "startup/startupsettingspage.h"
16 #include "trash/trashsettingspage.h"
17 #include "viewmodes/viewsettingspage.h"
18 #if HAVE_KUSERFEEDBACK
19 #include "userfeedback/dolphinfeedbackprovider.h"
20 #include "userfeedback/userfeedbacksettingspage.h"
21 #endif
22
23 #include <KAuthorized>
24 #include <KLocalizedString>
25 #include <KMessageBox>
26 #include <KWindowConfig>
27
28 #include <kwidgetsaddons_version.h>
29
30 #include <QCloseEvent>
31 #include <QPushButton>
32
33 DolphinSettingsDialog::DolphinSettingsDialog(const QUrl &url, QWidget *parent, KActionCollection *actions)
34 : KPageDialog(parent)
35 , m_pages()
36 , m_unsavedChanges(false)
37
38 {
39 const QSize minSize = minimumSize();
40 setMinimumSize(QSize(540, minSize.height()));
41
42 setFaceType(List);
43 setWindowTitle(i18nc("@title:window", "Configure"));
44 QDialogButtonBox *box = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Apply | QDialogButtonBox::Cancel | QDialogButtonBox::RestoreDefaults);
45 box->button(QDialogButtonBox::Apply)->setEnabled(false);
46 box->button(QDialogButtonBox::Ok)->setDefault(true);
47 setButtonBox(box);
48
49 connect(box->button(QDialogButtonBox::Ok), &QAbstractButton::clicked, this, &DolphinSettingsDialog::applySettings);
50 connect(box->button(QDialogButtonBox::Apply), &QAbstractButton::clicked, this, &DolphinSettingsDialog::applySettings);
51 connect(box->button(QDialogButtonBox::RestoreDefaults), &QAbstractButton::clicked, this, &DolphinSettingsDialog::restoreDefaults);
52
53 // General
54 GeneralSettingsPage *generalSettingsPage = new GeneralSettingsPage(url, this);
55 KPageWidgetItem *generalSettingsFrame = addPage(generalSettingsPage, i18nc("@title:group General settings", "General"));
56 generalSettingsFrame->setIcon(QIcon::fromTheme(QStringLiteral("system-file-manager")));
57 connect(generalSettingsPage, &GeneralSettingsPage::changed, this, &DolphinSettingsDialog::enableApply);
58
59 // Startup
60 StartupSettingsPage *startupSettingsPage = new StartupSettingsPage(url, this);
61 KPageWidgetItem *startupSettingsFrame = addPage(startupSettingsPage, i18nc("@title:group", "Startup"));
62 startupSettingsFrame->setIcon(QIcon::fromTheme(QStringLiteral("preferences-desktop-launch-feedback")));
63 connect(startupSettingsPage, &StartupSettingsPage::changed, this, &DolphinSettingsDialog::enableApply);
64
65 // View Modes
66 ViewSettingsPage *viewSettingsPage = new ViewSettingsPage(this);
67 KPageWidgetItem *viewSettingsFrame = addPage(viewSettingsPage, i18nc("@title:group", "View Modes"));
68 viewSettingsFrame->setIcon(QIcon::fromTheme(QStringLiteral("preferences-desktop-icons")));
69 connect(viewSettingsPage, &ViewSettingsPage::changed, this, &DolphinSettingsDialog::enableApply);
70
71 // Navigation
72 NavigationSettingsPage *navigationSettingsPage = new NavigationSettingsPage(this);
73 KPageWidgetItem *navigationSettingsFrame = addPage(navigationSettingsPage, i18nc("@title:group", "Navigation"));
74 navigationSettingsFrame->setIcon(QIcon::fromTheme(QStringLiteral("preferences-desktop-navigation")));
75 connect(navigationSettingsPage, &NavigationSettingsPage::changed, this, &DolphinSettingsDialog::enableApply);
76
77 // Context Menu
78 auto contextMenuSettingsPage = new ContextMenuSettingsPage(this,
79 actions,
80 {QStringLiteral("add_to_places"),
81 QStringLiteral("sort"),
82 QStringLiteral("view_mode"),
83 QStringLiteral("open_in_new_tab"),
84 QStringLiteral("open_in_new_window"),
85 QStringLiteral("copy_location"),
86 QStringLiteral("duplicate"),
87 QStringLiteral("open_terminal_here"),
88 QStringLiteral("copy_to_inactive_split_view"),
89 QStringLiteral("move_to_inactive_split_view")});
90 KPageWidgetItem *contextMenuSettingsFrame = addPage(contextMenuSettingsPage, i18nc("@title:group", "Context Menu"));
91 contextMenuSettingsFrame->setIcon(QIcon::fromTheme(QStringLiteral("preferences-desktop-menu-edit")));
92 connect(contextMenuSettingsPage, &ContextMenuSettingsPage::changed, this, &DolphinSettingsDialog::enableApply);
93
94 // Trash
95 SettingsPageBase *trashSettingsPage = nullptr;
96 #ifndef Q_OS_WIN
97 trashSettingsPage = createTrashSettingsPage(this);
98 #endif
99 if (trashSettingsPage) {
100 trashSettings = addPage(trashSettingsPage, i18nc("@title:group", "Trash"));
101 trashSettings->setIcon(QIcon::fromTheme(QStringLiteral("user-trash")));
102 connect(trashSettingsPage, &TrashSettingsPage::changed, this, &DolphinSettingsDialog::enableApply);
103 }
104
105 #if HAVE_KUSERFEEDBACK
106 // User Feedback
107 UserFeedbackSettingsPage *feedbackSettingsPage = nullptr;
108 if (DolphinFeedbackProvider::instance()->isEnabled()) {
109 feedbackSettingsPage = new UserFeedbackSettingsPage(this);
110 auto feedbackSettingsFrame = addPage(feedbackSettingsPage, i18nc("@title:group", "User Feedback"));
111 feedbackSettingsFrame->setIcon(QIcon::fromTheme(QStringLiteral("preferences-desktop-locale")));
112 connect(feedbackSettingsPage, &UserFeedbackSettingsPage::changed, this, &DolphinSettingsDialog::enableApply);
113 }
114 #endif
115
116 m_pages.append(generalSettingsPage);
117 m_pages.append(startupSettingsPage);
118 m_pages.append(viewSettingsPage);
119 m_pages.append(navigationSettingsPage);
120 m_pages.append(contextMenuSettingsPage);
121 if (trashSettingsPage) {
122 m_pages.append(trashSettingsPage);
123 }
124 #if HAVE_KUSERFEEDBACK
125 if (feedbackSettingsPage) {
126 m_pages.append(feedbackSettingsPage);
127 }
128 #endif
129
130 const KConfigGroup dialogConfig(KSharedConfig::openStateConfig(), "SettingsDialog");
131 KWindowConfig::restoreWindowSize(windowHandle(), dialogConfig);
132 }
133
134 DolphinSettingsDialog::~DolphinSettingsDialog()
135 {
136 KConfigGroup dialogConfig(KSharedConfig::openStateConfig(), "SettingsDialog");
137 KWindowConfig::saveWindowSize(windowHandle(), dialogConfig);
138 }
139
140 void DolphinSettingsDialog::enableApply()
141 {
142 buttonBox()->button(QDialogButtonBox::Apply)->setEnabled(true);
143 m_unsavedChanges = true;
144 }
145
146 void DolphinSettingsDialog::applySettings()
147 {
148 for (SettingsPageBase *page : qAsConst(m_pages)) {
149 page->applySettings();
150 }
151
152 Q_EMIT settingsChanged();
153
154 GeneralSettings *settings = GeneralSettings::self();
155 if (settings->modifiedStartupSettings()) {
156 // Reset the modified startup settings hint. The changed startup settings
157 // have been applied already due to emitting settingsChanged().
158 settings->setModifiedStartupSettings(false);
159 settings->save();
160 }
161 buttonBox()->button(QDialogButtonBox::Apply)->setEnabled(false);
162 m_unsavedChanges = false;
163 }
164
165 void DolphinSettingsDialog::restoreDefaults()
166 {
167 for (SettingsPageBase *page : qAsConst(m_pages)) {
168 page->restoreDefaults();
169 }
170 }
171
172 void DolphinSettingsDialog::closeEvent(QCloseEvent *event)
173 {
174 if (!m_unsavedChanges) {
175 event->accept();
176 return;
177 }
178
179 #if KWIDGETSADDONS_VERSION >= QT_VERSION_CHECK(5, 100, 0)
180 const auto response = KMessageBox::warningTwoActionsCancel(this,
181 #else
182 const auto response = KMessageBox::warningYesNoCancel(this,
183 #endif
184 i18n("You have unsaved changes. Do you want to apply the changes or discard them?"),
185 i18n("Warning"),
186 KStandardGuiItem::save(),
187 KStandardGuiItem::discard(),
188 KStandardGuiItem::cancel());
189 switch (response) {
190 #if KWIDGETSADDONS_VERSION >= QT_VERSION_CHECK(5, 100, 0)
191 case KMessageBox::PrimaryAction:
192 #else
193 case KMessageBox::Yes:
194 #endif
195 applySettings();
196 Q_FALLTHROUGH();
197 #if KWIDGETSADDONS_VERSION >= QT_VERSION_CHECK(5, 100, 0)
198 case KMessageBox::SecondaryAction:
199 #else
200 case KMessageBox::No:
201 #endif
202 event->accept();
203 break;
204 case KMessageBox::Cancel:
205 event->ignore();
206 break;
207 default:
208 break;
209 }
210 }
211
212 SettingsPageBase *DolphinSettingsDialog::createTrashSettingsPage(QWidget *parent)
213 {
214 if (!KAuthorized::authorizeControlModule(QStringLiteral("kcmtrash.desktop"))) {
215 return nullptr;
216 }
217
218 return new TrashSettingsPage(parent);
219 }