]> cloud.milkyroute.net Git - dolphin.git/blob - src/viewpropertiesdialog.cpp
oxygen icon naming fixes from Luca Gugelmann
[dolphin.git] / src / viewpropertiesdialog.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 "viewpropertiesdialog.h"
22 #include "viewpropsprogressinfo.h"
23 #include "dolphinview.h"
24 #include "dolphinsettings.h"
25 #include "dolphinsortfilterproxymodel.h"
26 #include "dolphin_generalsettings.h"
27 #include "viewproperties.h"
28
29 #include <assert.h>
30
31 #include <kcomponentdata.h>
32 #include <klocale.h>
33 #include <kiconloader.h>
34 #include <kio/netaccess.h>
35 #include <kmessagebox.h>
36 #include <kstandarddirs.h>
37 #include <kurl.h>
38
39 #include <QButtonGroup>
40 #include <QCheckBox>
41 #include <QComboBox>
42 #include <QGridLayout>
43 #include <QGroupBox>
44 #include <QLabel>
45 #include <QRadioButton>
46 #include <QVBoxLayout>
47
48 ViewPropertiesDialog::ViewPropertiesDialog(DolphinView* dolphinView) :
49 KDialog(dolphinView),
50 m_isDirty(false),
51 m_dolphinView(dolphinView),
52 m_viewProps(0),
53 m_viewMode(0),
54 m_sorting(0),
55 m_sortOrder(0),
56 m_showPreview(0),
57 m_showHiddenFiles(0),
58 m_applyToCurrentFolder(0),
59 m_applyToSubFolders(0),
60 m_useAsDefault(0)
61 {
62 assert(dolphinView != 0);
63 const bool useGlobalViewProps = DolphinSettings::instance().generalSettings()->globalViewProps();
64
65 setCaption(i18n("View Properties"));
66 setButtons(KDialog::Ok | KDialog::Cancel | KDialog::Apply);
67
68 const KUrl& url = dolphinView->url();
69 m_viewProps = new ViewProperties(url);
70 m_viewProps->setAutoSaveEnabled(false);
71
72 QWidget* main = new QWidget();
73 QVBoxLayout* topLayout = new QVBoxLayout();
74
75 // create 'Properties' group containing view mode, sorting, sort order and show hidden files
76 QWidget* propsBox = main;
77 if (!useGlobalViewProps) {
78 propsBox = new QGroupBox(i18n("Properties"), main);
79 }
80
81 QLabel* viewModeLabel = new QLabel(i18n("View mode:"), propsBox);
82 m_viewMode = new QComboBox(propsBox);
83 m_viewMode->addItem(SmallIcon("view_icon"), i18n("Icons"));
84 m_viewMode->addItem(SmallIcon("fileview-text"), i18n("Details"));
85 const int index = static_cast<int>(m_viewProps->viewMode());
86 m_viewMode->setCurrentIndex(index);
87
88 QLabel* sortingLabel = new QLabel(i18n("Sorting:"), propsBox);
89 m_sorting = new QComboBox(propsBox);
90 m_sorting->addItem("By Name");
91 m_sorting->addItem("By Size");
92 m_sorting->addItem("By Date");
93 m_sorting->addItem("By Permissions");
94 m_sorting->addItem("By Owner");
95 m_sorting->addItem("By Group");
96 m_sorting->setCurrentIndex(m_viewProps->sorting());
97
98 QLabel* sortOrderLabel = new QLabel(i18n("Sort order:"), propsBox);
99 m_sortOrder = new QComboBox(propsBox);
100 m_sortOrder->addItem(i18n("Ascending"));
101 m_sortOrder->addItem(i18n("Descending"));
102 const int sortOrderIdx = (m_viewProps->sortOrder() == Qt::Ascending) ? 0 : 1;
103 m_sortOrder->setCurrentIndex(sortOrderIdx);
104
105 m_showPreview = new QCheckBox(i18n("Show preview"), propsBox);
106 m_showPreview->setChecked(m_viewProps->showPreview());
107
108 m_showHiddenFiles = new QCheckBox(i18n("Show hidden files"), propsBox);
109 m_showHiddenFiles->setChecked(m_viewProps->showHiddenFiles());
110
111 QGridLayout* propsBoxLayout = new QGridLayout(propsBox);
112 propsBoxLayout->addWidget(viewModeLabel, 0, 0);
113 propsBoxLayout->addWidget(m_viewMode, 0, 1);
114 propsBoxLayout->addWidget(m_sorting, 1, 1);
115 propsBoxLayout->addWidget(sortingLabel, 1, 0);
116 propsBoxLayout->addWidget(m_sorting, 1, 1);
117 propsBoxLayout->addWidget(sortOrderLabel, 2, 0);
118 propsBoxLayout->addWidget(m_sortOrder, 2, 1);
119 propsBoxLayout->addWidget(m_showPreview, 3, 0);
120 propsBoxLayout->addWidget(m_showHiddenFiles, 4, 0);
121
122 topLayout->addWidget(propsBox);
123
124 connect(m_viewMode, SIGNAL(activated(int)),
125 this, SLOT(slotViewModeChanged(int)));
126 connect(m_sorting, SIGNAL(activated(int)),
127 this, SLOT(slotSortingChanged(int)));
128 connect(m_sortOrder, SIGNAL(activated(int)),
129 this, SLOT(slotSortOrderChanged(int)));
130 connect(m_showPreview, SIGNAL(clicked()),
131 this, SLOT(slotShowPreviewChanged()));
132 connect(m_showHiddenFiles, SIGNAL(clicked()),
133 this, SLOT(slotShowHiddenFilesChanged()));
134
135 connect(this, SIGNAL(okClicked()), this, SLOT(slotOk()));
136 connect(this, SIGNAL(applyClicked()), this, SLOT(slotApply()));
137
138 // Only show the following settings if the view properties are remembered
139 // for each directory:
140 if (!useGlobalViewProps) {
141 // create 'Apply view properties to:' group
142 QGroupBox* applyBox = new QGroupBox(i18n("Apply view properties to:"), main);
143
144 m_applyToCurrentFolder = new QRadioButton(i18n("Current folder"), applyBox);
145 m_applyToCurrentFolder->setChecked(true);
146 m_applyToSubFolders = new QRadioButton(i18n("Current folder including all sub folders"), applyBox);
147 m_applyToAllFolders = new QRadioButton(i18n("All folders"),applyBox);
148
149 QButtonGroup* applyGroup = new QButtonGroup(this);
150 applyGroup->addButton(m_applyToCurrentFolder);
151 applyGroup->addButton(m_applyToSubFolders);
152 applyGroup->addButton(m_applyToAllFolders);
153
154 QVBoxLayout* applyBoxLayout = new QVBoxLayout(applyBox);
155 applyBoxLayout->addWidget(m_applyToCurrentFolder);
156 applyBoxLayout->addWidget(m_applyToSubFolders);
157 applyBoxLayout->addWidget(m_applyToAllFolders);
158
159 m_useAsDefault = new QCheckBox(i18n("Use as default for new folders"), main);
160
161 topLayout->addWidget(applyBox);
162 topLayout->addWidget(m_useAsDefault);
163
164 connect(m_applyToCurrentFolder, SIGNAL(clicked()),
165 this, SLOT(markAsDirty()));
166 connect(m_applyToSubFolders, SIGNAL(clicked()),
167 this, SLOT(markAsDirty()));
168 connect(m_applyToAllFolders, SIGNAL(clicked()),
169 this, SLOT(markAsDirty()));
170 connect(m_useAsDefault, SIGNAL(clicked()),
171 this, SLOT(markAsDirty()));
172 }
173
174 main->setLayout(topLayout);
175 setMainWidget(main);
176 }
177
178 ViewPropertiesDialog::~ViewPropertiesDialog()
179 {
180 m_isDirty = false;
181 delete m_viewProps;
182 m_viewProps = 0;
183 }
184
185 void ViewPropertiesDialog::slotOk()
186 {
187 applyViewProperties();
188 accept();
189 }
190
191 void ViewPropertiesDialog::slotApply()
192 {
193 applyViewProperties();
194 }
195
196 void ViewPropertiesDialog::slotViewModeChanged(int index)
197 {
198 assert((index >= 0) && (index <= 2));
199 m_viewProps->setViewMode(static_cast<DolphinView::Mode>(index));
200 m_isDirty = true;
201 }
202
203 void ViewPropertiesDialog::slotSortingChanged(int index)
204 {
205 const DolphinView::Sorting sorting = DolphinSortFilterProxyModel::sortingForColumn(index);
206 m_viewProps->setSorting(sorting);
207 m_isDirty = true;
208 }
209
210 void ViewPropertiesDialog::slotSortOrderChanged(int index)
211 {
212 Qt::SortOrder sortOrder = (index == 0) ? Qt::Ascending : Qt::Descending;
213 m_viewProps->setSortOrder(sortOrder);
214 m_isDirty = true;
215 }
216
217 void ViewPropertiesDialog::slotShowPreviewChanged()
218 {
219 const bool show = m_showPreview->isChecked();
220 m_viewProps->setShowPreview(show);
221 m_isDirty = true;
222 }
223
224 void ViewPropertiesDialog::slotShowHiddenFilesChanged()
225 {
226 const bool show = m_showHiddenFiles->isChecked();
227 m_viewProps->setShowHiddenFiles(show);
228 m_isDirty = true;
229 }
230
231 void ViewPropertiesDialog::markAsDirty()
232 {
233 m_isDirty = true;
234 }
235
236 void ViewPropertiesDialog::applyViewProperties()
237 {
238 const bool applyToSubFolders = m_isDirty &&
239 (m_applyToSubFolders != 0) &&
240 m_applyToSubFolders->isChecked();
241 if (applyToSubFolders) {
242 const QString text(i18n("The view properties of all sub folders will be changed. Do you want to continue?"));
243 if (KMessageBox::questionYesNo(this, text) == KMessageBox::No) {
244 return;
245 }
246
247 ViewPropsProgressInfo* info = new ViewPropsProgressInfo(m_dolphinView,
248 m_dolphinView->url(),
249 *m_viewProps);
250 info->setWindowModality(Qt::NonModal);
251 info->show();
252 }
253
254 const bool applyToAllFolders = m_isDirty &&
255 (m_applyToAllFolders != 0) &&
256 m_applyToAllFolders->isChecked();
257 if (applyToAllFolders) {
258 const QString text(i18n("The view properties of all folders will be changed. Do you want to continue?"));
259 if (KMessageBox::questionYesNo(this, text) == KMessageBox::No) {
260 return;
261 }
262
263 // Updating the global view properties time stamp in the general settings makes
264 // all existing viewproperties invalid, as they have a smaller time stamp.
265 GeneralSettings* settings = DolphinSettings::instance().generalSettings();
266 settings->setViewPropsTimestamp(QDateTime::currentDateTime());
267
268 // This is also a good chance to make a cleanup of all mirrored view properties:
269 const KUrl mirroredDir = ViewProperties::mirroredDirectory();
270 KIO::NetAccess::del(mirroredDir, this);
271 }
272
273 m_viewProps->save();
274
275 m_dolphinView->setMode(m_viewProps->viewMode());
276 m_dolphinView->setSorting(m_viewProps->sorting());
277 m_dolphinView->setSortOrder(m_viewProps->sortOrder());
278 m_dolphinView->setShowPreview(m_viewProps->showPreview());
279 m_dolphinView->setShowHiddenFiles(m_viewProps->showHiddenFiles());
280
281 m_isDirty = false;
282
283 if (m_useAsDefault->isChecked()) {
284 // For directories where no .directory file is available, the .directory
285 // file stored for the global view properties is used as fallback. To update
286 // this file we temporary turn on the global view properties mode.
287 GeneralSettings* settings = DolphinSettings::instance().generalSettings();
288 assert(!settings->globalViewProps());
289
290 settings->setGlobalViewProps(true);
291 ViewProperties defaultProps(m_dolphinView->url());
292 defaultProps.setDirProperties(*m_viewProps);
293 defaultProps.save();
294 settings->setGlobalViewProps(false);
295 }
296 }
297
298 #include "viewpropertiesdialog.moc"