]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/dolphinsettingsdialog.cpp
Merge branch 'Applications/19.04'
[dolphin.git] / src / settings / dolphinsettingsdialog.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz *
3 * peter.penz@gmx.at *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
20
21 #include "dolphinsettingsdialog.h"
22
23 #include "dolphin_generalsettings.h"
24 #include "dolphinmainwindow.h"
25 #include "general/generalsettingspage.h"
26 #include "navigation/navigationsettingspage.h"
27 #include "services/servicessettingspage.h"
28 #include "startup/startupsettingspage.h"
29 #include "trash/trashsettingspage.h"
30 #include "viewmodes/viewsettingspage.h"
31
32 #include <KAuthorized>
33 #include <KLocalizedString>
34 #include <KWindowConfig>
35 #include <KMessageBox>
36
37 #include <QPushButton>
38
39 DolphinSettingsDialog::DolphinSettingsDialog(const QUrl& url, QWidget* parent) :
40 KPageDialog(parent),
41 m_pages(),
42 m_unsavedChanges(false)
43
44 {
45 const QSize minSize = minimumSize();
46 setMinimumSize(QSize(540, minSize.height()));
47
48 setFaceType(List);
49 setWindowTitle(i18nc("@title:window", "Dolphin Preferences"));
50 QDialogButtonBox* box = new QDialogButtonBox(QDialogButtonBox::Ok
51 | QDialogButtonBox::Apply | QDialogButtonBox::Cancel | QDialogButtonBox::RestoreDefaults);
52 box->button(QDialogButtonBox::Apply)->setEnabled(false);
53 box->button(QDialogButtonBox::Ok)->setDefault(true);
54 setButtonBox(box);
55
56 connect(box->button(QDialogButtonBox::Ok), &QAbstractButton::clicked, this, &DolphinSettingsDialog::applySettings);
57 connect(box->button(QDialogButtonBox::Apply), &QAbstractButton::clicked, this, &DolphinSettingsDialog::applySettings);
58 connect(box->button(QDialogButtonBox::RestoreDefaults), &QAbstractButton::clicked, this, &DolphinSettingsDialog::restoreDefaults);
59
60 // General
61 GeneralSettingsPage* generalSettingsPage = new GeneralSettingsPage(url, this);
62 KPageWidgetItem* generalSettingsFrame = addPage(generalSettingsPage,
63 i18nc("@title:group General settings", "General"));
64 generalSettingsFrame->setIcon(QIcon::fromTheme(QStringLiteral("view-preview")));
65 connect(generalSettingsPage, &GeneralSettingsPage::changed, this, &DolphinSettingsDialog::enableApply);
66
67 // Startup
68 StartupSettingsPage* startupSettingsPage = new StartupSettingsPage(url, this);
69 KPageWidgetItem* startupSettingsFrame = addPage(startupSettingsPage,
70 i18nc("@title:group", "Startup"));
71 startupSettingsFrame->setIcon(QIcon::fromTheme(QStringLiteral("go-home")));
72 connect(startupSettingsPage, &StartupSettingsPage::changed, this, &DolphinSettingsDialog::enableApply);
73
74 // View Modes
75 ViewSettingsPage* viewSettingsPage = new ViewSettingsPage(this);
76 KPageWidgetItem* viewSettingsFrame = addPage(viewSettingsPage,
77 i18nc("@title:group", "View Modes"));
78 viewSettingsFrame->setIcon(QIcon::fromTheme(QStringLiteral("view-choose")));
79 connect(viewSettingsPage, &ViewSettingsPage::changed, this, &DolphinSettingsDialog::enableApply);
80
81 // Navigation
82 NavigationSettingsPage* navigationSettingsPage = new NavigationSettingsPage(this);
83 KPageWidgetItem* navigationSettingsFrame = addPage(navigationSettingsPage,
84 i18nc("@title:group", "Navigation"));
85 navigationSettingsFrame->setIcon(QIcon::fromTheme(QStringLiteral("edit-select")));
86 connect(navigationSettingsPage, &NavigationSettingsPage::changed, this, &DolphinSettingsDialog::enableApply);
87
88 // Services
89 ServicesSettingsPage* servicesSettingsPage = new ServicesSettingsPage(this);
90 KPageWidgetItem* servicesSettingsFrame = addPage(servicesSettingsPage,
91 i18nc("@title:group", "Services"));
92 servicesSettingsFrame->setIcon(QIcon::fromTheme(QStringLiteral("flag")));
93 connect(servicesSettingsPage, &ServicesSettingsPage::changed, this, &DolphinSettingsDialog::enableApply);
94
95 // Trash
96 auto* trashSettingsPage = createTrashSettingsPage(this);
97 if (trashSettingsPage) {
98 KPageWidgetItem* trashSettingsFrame = addPage(trashSettingsPage,
99 i18nc("@title:group", "Trash"));
100 trashSettingsFrame->setIcon(QIcon::fromTheme(QStringLiteral("trash-empty")));
101 connect(trashSettingsPage, &TrashSettingsPage::changed, this, &DolphinSettingsDialog::enableApply);
102 }
103
104 m_pages.append(generalSettingsPage);
105 m_pages.append(startupSettingsPage);
106 m_pages.append(viewSettingsPage);
107 m_pages.append(navigationSettingsPage);
108 m_pages.append(servicesSettingsPage);
109 if (trashSettingsPage) {
110 m_pages.append(trashSettingsPage);
111 }
112
113 const KConfigGroup dialogConfig(KSharedConfig::openConfig(QStringLiteral("dolphinrc")), "SettingsDialog");
114 KWindowConfig::restoreWindowSize(windowHandle(), dialogConfig);
115 }
116
117 DolphinSettingsDialog::~DolphinSettingsDialog()
118 {
119 KConfigGroup dialogConfig(KSharedConfig::openConfig(QStringLiteral("dolphinrc")), "SettingsDialog");
120 KWindowConfig::saveWindowSize(windowHandle(), dialogConfig);
121 }
122
123 void DolphinSettingsDialog::enableApply()
124 {
125 buttonBox()->button(QDialogButtonBox::Apply)->setEnabled(true);
126 m_unsavedChanges = true;
127 }
128
129 void DolphinSettingsDialog::applySettings()
130 {
131 foreach (SettingsPageBase* page, m_pages) {
132 page->applySettings();
133 }
134
135 emit settingsChanged();
136
137 GeneralSettings* settings = GeneralSettings::self();
138 if (settings->modifiedStartupSettings()) {
139 // Reset the modified startup settings hint. The changed startup settings
140 // have been applied already due to emitting settingsChanged().
141 settings->setModifiedStartupSettings(false);
142 settings->save();
143 }
144 buttonBox()->button(QDialogButtonBox::Apply)->setEnabled(false);
145 m_unsavedChanges = false;
146 }
147
148 void DolphinSettingsDialog::restoreDefaults()
149 {
150 foreach (SettingsPageBase* page, m_pages) {
151 page->restoreDefaults();
152 }
153 }
154
155 void DolphinSettingsDialog::closeEvent(QCloseEvent* event)
156 {
157 if (!m_unsavedChanges) {
158 event->accept();
159 return;
160 }
161
162 const auto response = KMessageBox::warningYesNoCancel(this,
163 i18n("You have unsaved changes. Do you want to apply the changes or discard them?"),
164 i18n("Warning"),
165 KStandardGuiItem::save(),
166 KStandardGuiItem::discard(),
167 KStandardGuiItem::cancel());
168 switch (response) {
169 case KMessageBox::Yes:
170 applySettings();
171 Q_FALLTHROUGH();
172 case KMessageBox::No:
173 event->accept();
174 break;
175 case KMessageBox::Cancel:
176 event->ignore();
177 break;
178 default:
179 break;
180 }
181 }
182
183
184 SettingsPageBase *DolphinSettingsDialog::createTrashSettingsPage(QWidget *parent)
185 {
186 if (!KAuthorized::authorizeControlModule(QStringLiteral("kcmtrash.desktop"))) {
187 return nullptr;
188 }
189
190 return new TrashSettingsPage(parent);
191 }