]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/dolphinsettingsdialog.cpp
Merge remote-tracking branch 'upstream/master' into work/zakharafoniam/useful-groups
[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 "interface/interfacesettingspage.h"
14 #include "trash/trashsettingspage.h"
15 #include "viewmodes/viewsettingspage.h"
16 #if HAVE_KUSERFEEDBACK
17 #include "userfeedback/dolphinfeedbackprovider.h"
18 #include "userfeedback/userfeedbacksettingspage.h"
19 #endif
20
21 #include <KAuthorized>
22 #include <KLocalizedString>
23 #include <KMessageBox>
24 #include <KWindowConfig>
25
26 #include <kwidgetsaddons_version.h>
27
28 #include <QCloseEvent>
29 #include <QPushButton>
30
31 DolphinSettingsDialog::DolphinSettingsDialog(const QUrl &url, QWidget *parent, KActionCollection *actions)
32 : KPageDialog(parent)
33 , m_pages()
34 , m_unsavedChanges(false)
35
36 {
37 const QSize minSize = minimumSize();
38 setMinimumSize(QSize(540, minSize.height()));
39
40 setFaceType(List);
41 setWindowTitle(i18nc("@title:window", "Configure"));
42
43 // Interface
44 InterfaceSettingsPage *interfaceSettingsPage = new InterfaceSettingsPage(this);
45 KPageWidgetItem *interfaceSettingsFrame = addPage(interfaceSettingsPage, i18nc("@title:group Interface settings", "Interface"));
46 interfaceSettingsFrame->setIcon(QIcon::fromTheme(QStringLiteral("system-file-manager")));
47 connect(interfaceSettingsPage, &InterfaceSettingsPage::changed, this, &DolphinSettingsDialog::enableApply);
48
49 // View
50 ViewSettingsPage *viewSettingsPage = new ViewSettingsPage(url, this);
51 KPageWidgetItem *viewSettingsFrame = addPage(viewSettingsPage, i18nc("@title:group", "View"));
52 viewSettingsFrame->setIcon(QIcon::fromTheme(QStringLiteral("preferences-desktop-icons")));
53 connect(viewSettingsPage, &ViewSettingsPage::changed, this, &DolphinSettingsDialog::enableApply);
54
55 // Context Menu
56 auto contextMenuSettingsPage = new ContextMenuSettingsPage(this,
57 actions,
58 {QStringLiteral("add_to_places"),
59 QStringLiteral("sort"),
60 QStringLiteral("group"),
61 QStringLiteral("view_mode"),
62 QStringLiteral("open_in_new_tab"),
63 QStringLiteral("open_in_new_window"),
64 QStringLiteral("open_in_split_view"),
65 QStringLiteral("copy_location"),
66 QStringLiteral("duplicate"),
67 QStringLiteral("open_terminal_here"),
68 QStringLiteral("copy_to_inactive_split_view"),
69 QStringLiteral("move_to_inactive_split_view")});
70 KPageWidgetItem *contextMenuSettingsFrame = addPage(contextMenuSettingsPage, i18nc("@title:group", "Context Menu"));
71 contextMenuSettingsFrame->setIcon(QIcon::fromTheme(QStringLiteral("preferences-desktop-menu-edit")));
72 connect(contextMenuSettingsPage, &ContextMenuSettingsPage::changed, this, &DolphinSettingsDialog::enableApply);
73
74 // Trash
75 SettingsPageBase *trashSettingsPage = nullptr;
76 #ifndef Q_OS_WIN
77 trashSettingsPage = createTrashSettingsPage(this);
78 #endif
79 if (trashSettingsPage) {
80 trashSettings = addPage(trashSettingsPage, i18nc("@title:group", "Trash"));
81 trashSettings->setIcon(QIcon::fromTheme(QStringLiteral("user-trash")));
82 connect(trashSettingsPage, &TrashSettingsPage::changed, this, &DolphinSettingsDialog::enableApply);
83 }
84
85 #if HAVE_KUSERFEEDBACK
86 // User Feedback
87 UserFeedbackSettingsPage *feedbackSettingsPage = nullptr;
88 if (DolphinFeedbackProvider::instance()->isEnabled()) {
89 feedbackSettingsPage = new UserFeedbackSettingsPage(this);
90 auto feedbackSettingsFrame = addPage(feedbackSettingsPage, i18nc("@title:group", "User Feedback"));
91 feedbackSettingsFrame->setIcon(QIcon::fromTheme(QStringLiteral("preferences-desktop-locale")));
92 connect(feedbackSettingsPage, &UserFeedbackSettingsPage::changed, this, &DolphinSettingsDialog::enableApply);
93 }
94 #endif
95
96 m_pages.append(interfaceSettingsPage);
97 m_pages.append(viewSettingsPage);
98 m_pages.append(contextMenuSettingsPage);
99 if (trashSettingsPage) {
100 m_pages.append(trashSettingsPage);
101 }
102 #if HAVE_KUSERFEEDBACK
103 if (feedbackSettingsPage) {
104 m_pages.append(feedbackSettingsPage);
105 }
106 #endif
107
108 // Create the buttons last so they are also last in the keyboard Tab focus order.
109 QDialogButtonBox *box = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Apply | QDialogButtonBox::Cancel | QDialogButtonBox::RestoreDefaults);
110 box->button(QDialogButtonBox::Apply)->setEnabled(false);
111 box->button(QDialogButtonBox::Ok)->setDefault(true);
112 setButtonBox(box);
113
114 connect(box->button(QDialogButtonBox::Ok), &QAbstractButton::clicked, this, &DolphinSettingsDialog::applySettings);
115 connect(box->button(QDialogButtonBox::Apply), &QAbstractButton::clicked, this, &DolphinSettingsDialog::applySettings);
116 connect(box->button(QDialogButtonBox::RestoreDefaults), &QAbstractButton::clicked, this, &DolphinSettingsDialog::restoreDefaults);
117
118 const KConfigGroup dialogConfig(KSharedConfig::openStateConfig(), QStringLiteral("SettingsDialog"));
119 KWindowConfig::restoreWindowSize(windowHandle(), dialogConfig);
120 }
121
122 DolphinSettingsDialog::~DolphinSettingsDialog()
123 {
124 KConfigGroup dialogConfig(KSharedConfig::openStateConfig(), QStringLiteral("SettingsDialog"));
125 KWindowConfig::saveWindowSize(windowHandle(), dialogConfig);
126 }
127
128 void DolphinSettingsDialog::enableApply()
129 {
130 buttonBox()->button(QDialogButtonBox::Apply)->setEnabled(true);
131 m_unsavedChanges = true;
132 }
133
134 void DolphinSettingsDialog::applySettings()
135 {
136 for (SettingsPageBase *page : std::as_const(m_pages)) {
137 page->applySettings();
138 }
139
140 Q_EMIT settingsChanged();
141
142 GeneralSettings *settings = GeneralSettings::self();
143 if (settings->modifiedStartupSettings()) {
144 // Reset the modified startup settings hint. The changed startup settings
145 // have been applied already due to emitting settingsChanged().
146 settings->setModifiedStartupSettings(false);
147 settings->save();
148 }
149 buttonBox()->button(QDialogButtonBox::Apply)->setEnabled(false);
150 m_unsavedChanges = false;
151 }
152
153 void DolphinSettingsDialog::restoreDefaults()
154 {
155 for (SettingsPageBase *page : std::as_const(m_pages)) {
156 page->restoreDefaults();
157 }
158 }
159
160 void DolphinSettingsDialog::closeEvent(QCloseEvent *event)
161 {
162 if (!m_unsavedChanges) {
163 event->accept();
164 return;
165 }
166
167 const auto response = KMessageBox::warningTwoActionsCancel(this,
168 i18n("You have unsaved changes. Do you want to apply the changes or discard them?"),
169 i18n("Warning"),
170 KStandardGuiItem::save(),
171 KStandardGuiItem::discard(),
172 KStandardGuiItem::cancel());
173 switch (response) {
174 case KMessageBox::PrimaryAction:
175 applySettings();
176 Q_FALLTHROUGH();
177 case KMessageBox::SecondaryAction:
178 event->accept();
179 break;
180 case KMessageBox::Cancel:
181 event->ignore();
182 break;
183 default:
184 break;
185 }
186 }
187
188 SettingsPageBase *DolphinSettingsDialog::createTrashSettingsPage(QWidget *parent)
189 {
190 if (!KAuthorized::authorizeControlModule(QStringLiteral("kcmtrash.desktop"))) {
191 return nullptr;
192 }
193
194 return new TrashSettingsPage(parent);
195 }
196
197 #include "moc_dolphinsettingsdialog.cpp"