]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/general/confirmationssettingspage.cpp
dd4d60f3b10fe24526098b8544fc6179867afdb7
[dolphin.git] / src / settings / general / confirmationssettingspage.cpp
1 /***************************************************************************
2 * Copyright (C) 2012 by Peter Penz <peter.penz19@gmail.com> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
19
20 #include "confirmationssettingspage.h"
21
22 #include "dolphin_generalsettings.h"
23 #include "global.h"
24
25 #include <KLocalizedString>
26
27 #include <QCheckBox>
28 #include <QComboBox>
29 #include <QLabel>
30 #include <QHBoxLayout>
31 #include <QVBoxLayout>
32
33 namespace {
34 enum ScriptExecution
35 {
36 AlwaysAsk = 0,
37 Open = 1,
38 Execute = 2
39 };
40
41 const bool ConfirmEmptyTrash = true;
42 const bool ConfirmTrash = false;
43 const bool ConfirmDelete = true;
44 const int ConfirmScriptExecution = ScriptExecution::AlwaysAsk;
45 }
46
47 ConfirmationsSettingsPage::ConfirmationsSettingsPage(QWidget* parent) :
48 SettingsPageBase(parent),
49 m_confirmMoveToTrash(nullptr),
50 m_confirmEmptyTrash(nullptr),
51 m_confirmDelete(nullptr),
52
53 #ifdef HAVE_TERMINAL
54 m_confirmClosingTerminalRunningProgram(nullptr),
55 #endif
56
57 m_confirmClosingMultipleTabs(nullptr)
58 {
59 QVBoxLayout* topLayout = new QVBoxLayout(this);
60
61 QLabel* confirmLabelKde = new QLabel(i18nc("@title:group", "Ask for confirmation in all KDE applications when:"), this);
62 confirmLabelKde->setWordWrap(true);
63
64 m_confirmMoveToTrash = new QCheckBox(i18nc("@option:check Ask for confirmation when",
65 "Moving files or folders to trash"), this);
66 m_confirmEmptyTrash = new QCheckBox(i18nc("@option:check Ask for confirmation when",
67 "Emptying trash"), this);
68 m_confirmDelete = new QCheckBox(i18nc("@option:check Ask for confirmation when",
69 "Deleting files or folders"), this);
70
71 QLabel* confirmLabelDolphin = new QLabel(i18nc("@title:group", "Ask for confirmation in Dolphin when:"), this);
72 confirmLabelDolphin->setWordWrap(true);
73
74 m_confirmClosingMultipleTabs = new QCheckBox(i18nc("@option:check Ask for confirmation in Dolphin when",
75 "Closing windows with multiple tabs"), this);
76
77 #ifdef HAVE_TERMINAL
78 m_confirmClosingTerminalRunningProgram = new QCheckBox(i18nc("@option:check Ask for confirmation when",
79 "Closing windows with a program running in the Terminal panel"), this);
80 #endif
81
82 QHBoxLayout* executableScriptLayout = new QHBoxLayout();
83 QLabel* executableScriptLabel = new QLabel(i18nc("@title:group", "When opening an executable file:"), this);
84 confirmLabelKde->setWordWrap(true);
85 executableScriptLayout->addWidget(executableScriptLabel);
86
87 m_confirmScriptExecution = new QComboBox(this);
88 m_confirmScriptExecution->addItems({i18n("Always ask"), i18n("Open in application"), i18n("Run script")});
89 executableScriptLayout->addWidget(m_confirmScriptExecution);
90
91 topLayout->addWidget(confirmLabelKde);
92 topLayout->addWidget(m_confirmMoveToTrash);
93 topLayout->addWidget(m_confirmEmptyTrash);
94 topLayout->addWidget(m_confirmDelete);
95 topLayout->addSpacing(Dolphin::VERTICAL_SPACER_HEIGHT);
96 topLayout->addWidget(confirmLabelDolphin);
97 topLayout->addWidget(m_confirmClosingMultipleTabs);
98
99 #ifdef HAVE_TERMINAL
100 topLayout->addWidget(m_confirmClosingTerminalRunningProgram);
101 #endif
102
103 topLayout->addSpacing(Dolphin::VERTICAL_SPACER_HEIGHT);
104 topLayout->addLayout(executableScriptLayout);
105
106 topLayout->addStretch();
107
108 loadSettings();
109
110 connect(m_confirmMoveToTrash, &QCheckBox::toggled, this, &ConfirmationsSettingsPage::changed);
111 connect(m_confirmEmptyTrash, &QCheckBox::toggled, this, &ConfirmationsSettingsPage::changed);
112 connect(m_confirmDelete, &QCheckBox::toggled, this, &ConfirmationsSettingsPage::changed);
113 connect(m_confirmScriptExecution, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &ConfirmationsSettingsPage::changed);
114 connect(m_confirmClosingMultipleTabs, &QCheckBox::toggled, this, &ConfirmationsSettingsPage::changed);
115
116 #ifdef HAVE_TERMINAL
117 connect(m_confirmClosingTerminalRunningProgram, &QCheckBox::toggled, this, &ConfirmationsSettingsPage::changed);
118 #endif
119 }
120
121 ConfirmationsSettingsPage::~ConfirmationsSettingsPage()
122 {
123 }
124
125 void ConfirmationsSettingsPage::applySettings()
126 {
127 KSharedConfig::Ptr kioConfig = KSharedConfig::openConfig(QStringLiteral("kiorc"), KConfig::NoGlobals);
128 KConfigGroup confirmationGroup(kioConfig, "Confirmations");
129 confirmationGroup.writeEntry("ConfirmTrash", m_confirmMoveToTrash->isChecked());
130 confirmationGroup.writeEntry("ConfirmEmptyTrash", m_confirmEmptyTrash->isChecked());
131 confirmationGroup.writeEntry("ConfirmDelete", m_confirmDelete->isChecked());
132
133 KConfigGroup scriptExecutionGroup(kioConfig, "Executable scripts");
134 const int index = m_confirmScriptExecution->currentIndex();
135 switch (index) {
136 case ScriptExecution::AlwaysAsk:
137 scriptExecutionGroup.writeEntry("behaviourOnLaunch", "alwaysAsk");
138 break;
139 case ScriptExecution::Open:
140 scriptExecutionGroup.writeEntry("behaviourOnLaunch", "dontAsk");
141 break;
142 case ScriptExecution::Execute:
143 scriptExecutionGroup.writeEntry("behaviourOnLaunch", "execute");
144 break;
145 }
146 kioConfig->sync();
147
148 GeneralSettings* settings = GeneralSettings::self();
149 settings->setConfirmClosingMultipleTabs(m_confirmClosingMultipleTabs->isChecked());
150
151 #ifdef HAVE_TERMINAL
152 settings->setConfirmClosingTerminalRunningProgram(m_confirmClosingTerminalRunningProgram->isChecked());
153 #endif
154
155 settings->save();
156 }
157
158 void ConfirmationsSettingsPage::restoreDefaults()
159 {
160 GeneralSettings* settings = GeneralSettings::self();
161 settings->useDefaults(true);
162 loadSettings();
163 settings->useDefaults(false);
164
165 m_confirmMoveToTrash->setChecked(ConfirmTrash);
166 m_confirmEmptyTrash->setChecked(ConfirmEmptyTrash);
167 m_confirmDelete->setChecked(ConfirmDelete);
168 m_confirmScriptExecution->setCurrentIndex(ConfirmScriptExecution);
169 }
170
171 void ConfirmationsSettingsPage::loadSettings()
172 {
173 KSharedConfig::Ptr kioConfig = KSharedConfig::openConfig(QStringLiteral("kiorc"), KConfig::IncludeGlobals);
174 const KConfigGroup confirmationGroup(kioConfig, "Confirmations");
175 m_confirmMoveToTrash->setChecked(confirmationGroup.readEntry("ConfirmTrash", ConfirmTrash));
176 m_confirmEmptyTrash->setChecked(confirmationGroup.readEntry("ConfirmEmptyTrash", ConfirmEmptyTrash));
177 m_confirmDelete->setChecked(confirmationGroup.readEntry("ConfirmDelete", ConfirmDelete));
178
179 const KConfigGroup scriptExecutionGroup(KSharedConfig::openConfig(QStringLiteral("kiorc")), "Executable scripts");
180 const QString value = scriptExecutionGroup.readEntry("behaviourOnLaunch", "alwaysAsk");
181 if (value == QLatin1String("dontAsk")) {
182 m_confirmScriptExecution->setCurrentIndex(ScriptExecution::Open);
183 } else if (value == QLatin1String("execute")) {
184 m_confirmScriptExecution->setCurrentIndex(ScriptExecution::Execute);
185 } else /* if (value == QLatin1String("alwaysAsk"))*/ {
186 m_confirmScriptExecution->setCurrentIndex(ScriptExecution::AlwaysAsk);
187 }
188
189 m_confirmClosingMultipleTabs->setChecked(GeneralSettings::confirmClosingMultipleTabs());
190
191 #ifdef HAVE_TERMINAL
192 m_confirmClosingTerminalRunningProgram->setChecked(GeneralSettings::confirmClosingTerminalRunningProgram());
193 #endif
194 }
195