]> cloud.milkyroute.net Git - dolphin.git/blob - src/viewpropertiesdialog.cpp
fixed dangerous usage of a const reference as member
[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 <kcomponentdata.h>
30 #include <klocale.h>
31 #include <kiconloader.h>
32 #include <kio/netaccess.h>
33 #include <kmessagebox.h>
34 #include <kstandarddirs.h>
35 #include <kurl.h>
36
37 #include <QButtonGroup>
38 #include <QCheckBox>
39 #include <QComboBox>
40 #include <QGridLayout>
41 #include <QGroupBox>
42 #include <QLabel>
43 #include <QRadioButton>
44 #include <QVBoxLayout>
45
46 ViewPropertiesDialog::ViewPropertiesDialog(DolphinView* dolphinView) :
47 KDialog(dolphinView),
48 m_isDirty(false),
49 m_dolphinView(dolphinView),
50 m_viewProps(0),
51 m_viewMode(0),
52 m_sorting(0),
53 m_sortOrder(0),
54 m_categorizedSorting(0),
55 m_additionalInfo(0),
56 m_showPreview(0),
57 m_showHiddenFiles(0),
58 m_applyToCurrentFolder(0),
59 m_applyToSubFolders(0),
60 m_useAsDefault(0)
61 {
62 Q_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(KIcon("view-icon"), i18n("Icons"));
84 m_viewMode->addItem(KIcon("fileview-text"), i18n("Details"));
85 m_viewMode->addItem(KIcon("fileview-column"), i18n("Column"));
86 const int index = static_cast<int>(m_viewProps->viewMode());
87 m_viewMode->setCurrentIndex(index);
88
89 QLabel* sortingLabel = new QLabel(i18n("Sorting:"), propsBox);
90 QWidget* sortingBox = new QWidget(propsBox);
91
92 m_sortOrder = new QComboBox(sortingBox);
93 m_sortOrder->addItem(i18n("Ascending"));
94 m_sortOrder->addItem(i18n("Descending"));
95 const int sortOrderIdx = (m_viewProps->sortOrder() == Qt::AscendingOrder) ? 0 : 1;
96 m_sortOrder->setCurrentIndex(sortOrderIdx);
97
98 m_categorizedSorting = new QComboBox(sortingBox);
99 m_categorizedSorting->addItem(i18n("Uncategorized"));
100 m_categorizedSorting->addItem(i18n("Categorized"));
101 m_categorizedSorting->setCurrentIndex(m_viewProps->categorizedSorting() ? 1 : 0);
102
103 m_sorting = new QComboBox(sortingBox);
104 m_sorting->addItem("By Name");
105 m_sorting->addItem("By Size");
106 m_sorting->addItem("By Date");
107 m_sorting->addItem("By Permissions");
108 m_sorting->addItem("By Owner");
109 m_sorting->addItem("By Group");
110 m_sorting->setCurrentIndex(m_viewProps->sorting());
111
112 QHBoxLayout* sortingLayout = new QHBoxLayout();
113 sortingLayout->setMargin(0);
114 sortingLayout->addWidget(m_sortOrder);
115 sortingLayout->addWidget(m_sorting);
116 sortingLayout->addWidget(m_categorizedSorting);
117 sortingBox->setLayout(sortingLayout);
118
119 QLabel* additionalInfoLabel = new QLabel(i18n("Additional information:"), propsBox);
120 m_additionalInfo = new QComboBox(propsBox);
121 m_additionalInfo->addItem(i18n("No Information"), KFileItemDelegate::NoInformation);
122 m_additionalInfo->addItem(i18n("Type"), KFileItemDelegate::FriendlyMimeType);
123 m_additionalInfo->addItem(i18n("Size"), KFileItemDelegate::Size);
124 m_additionalInfo->addItem(i18n("Date"), KFileItemDelegate::ModificationTime);
125 const int addInfoIndex = m_additionalInfo->findData(m_viewProps->additionalInfo());
126 m_additionalInfo->setCurrentIndex(addInfoIndex);
127 m_additionalInfo->setEnabled(m_viewProps->viewMode() == DolphinView::IconsView);
128
129 m_showPreview = new QCheckBox(i18n("Show preview"), propsBox);
130 m_showPreview->setChecked(m_viewProps->showPreview());
131
132 m_showHiddenFiles = new QCheckBox(i18n("Show hidden files"), propsBox);
133 m_showHiddenFiles->setChecked(m_viewProps->showHiddenFiles());
134
135 QGridLayout* propsBoxLayout = new QGridLayout(propsBox);
136 propsBoxLayout->addWidget(viewModeLabel, 0, 0);
137 propsBoxLayout->addWidget(m_viewMode, 0, 1);
138 propsBoxLayout->addWidget(sortingLabel, 1, 0);
139 propsBoxLayout->addWidget(sortingBox, 1, 1);
140 propsBoxLayout->addWidget(additionalInfoLabel, 2, 0);
141 propsBoxLayout->addWidget(m_additionalInfo, 2, 1);
142 propsBoxLayout->addWidget(m_showPreview, 3, 0);
143 propsBoxLayout->addWidget(m_showHiddenFiles, 4, 0);
144
145 topLayout->addWidget(propsBox);
146
147 connect(m_viewMode, SIGNAL(activated(int)),
148 this, SLOT(slotViewModeChanged(int)));
149 connect(m_sorting, SIGNAL(activated(int)),
150 this, SLOT(slotSortingChanged(int)));
151 connect(m_sortOrder, SIGNAL(activated(int)),
152 this, SLOT(slotSortOrderChanged(int)));
153 connect(m_categorizedSorting, SIGNAL(activated(int)),
154 this, SLOT(slotCategorizedSortingChanged(int)));
155 connect(m_additionalInfo, SIGNAL(activated(int)),
156 this, SLOT(slotAdditionalInfoChanged(int)));
157 connect(m_showPreview, SIGNAL(clicked()),
158 this, SLOT(slotShowPreviewChanged()));
159 connect(m_showHiddenFiles, SIGNAL(clicked()),
160 this, SLOT(slotShowHiddenFilesChanged()));
161
162 connect(this, SIGNAL(okClicked()), this, SLOT(slotOk()));
163 connect(this, SIGNAL(applyClicked()), this, SLOT(slotApply()));
164
165 // Only show the following settings if the view properties are remembered
166 // for each directory:
167 if (!useGlobalViewProps) {
168 // create 'Apply View Properties To' group
169 QGroupBox* applyBox = new QGroupBox(i18n("Apply View Properties To"), main);
170
171 m_applyToCurrentFolder = new QRadioButton(i18n("Current folder"), applyBox);
172 m_applyToCurrentFolder->setChecked(true);
173 m_applyToSubFolders = new QRadioButton(i18n("Current folder including all sub folders"), applyBox);
174 m_applyToAllFolders = new QRadioButton(i18n("All folders"), applyBox);
175
176 QButtonGroup* applyGroup = new QButtonGroup(this);
177 applyGroup->addButton(m_applyToCurrentFolder);
178 applyGroup->addButton(m_applyToSubFolders);
179 applyGroup->addButton(m_applyToAllFolders);
180
181 QVBoxLayout* applyBoxLayout = new QVBoxLayout(applyBox);
182 applyBoxLayout->addWidget(m_applyToCurrentFolder);
183 applyBoxLayout->addWidget(m_applyToSubFolders);
184 applyBoxLayout->addWidget(m_applyToAllFolders);
185
186 m_useAsDefault = new QCheckBox(i18n("Use as default for new folders"), main);
187
188 topLayout->addWidget(applyBox);
189 topLayout->addWidget(m_useAsDefault);
190
191 connect(m_applyToCurrentFolder, SIGNAL(clicked()),
192 this, SLOT(markAsDirty()));
193 connect(m_applyToSubFolders, SIGNAL(clicked()),
194 this, SLOT(markAsDirty()));
195 connect(m_applyToAllFolders, SIGNAL(clicked()),
196 this, SLOT(markAsDirty()));
197 connect(m_useAsDefault, SIGNAL(clicked()),
198 this, SLOT(markAsDirty()));
199 }
200
201 main->setLayout(topLayout);
202 setMainWidget(main);
203 }
204
205 ViewPropertiesDialog::~ViewPropertiesDialog()
206 {
207 m_isDirty = false;
208 delete m_viewProps;
209 m_viewProps = 0;
210 }
211
212 void ViewPropertiesDialog::slotOk()
213 {
214 applyViewProperties();
215 accept();
216 }
217
218 void ViewPropertiesDialog::slotApply()
219 {
220 applyViewProperties();
221 }
222
223 void ViewPropertiesDialog::slotViewModeChanged(int index)
224 {
225 m_viewProps->setViewMode(static_cast<DolphinView::Mode>(index));
226 m_isDirty = true;
227
228 const bool iconsViewEnabled = (m_viewProps->viewMode() == DolphinView::IconsView);
229 m_categorizedSorting->setEnabled(iconsViewEnabled);
230 m_additionalInfo->setEnabled(iconsViewEnabled);
231 }
232
233 void ViewPropertiesDialog::slotSortingChanged(int index)
234 {
235 const DolphinView::Sorting sorting = DolphinSortFilterProxyModel::sortingForColumn(index);
236 m_viewProps->setSorting(sorting);
237 m_isDirty = true;
238 }
239
240 void ViewPropertiesDialog::slotSortOrderChanged(int index)
241 {
242 Qt::SortOrder sortOrder = (index == 0) ? Qt::AscendingOrder : Qt::DescendingOrder;
243 m_viewProps->setSortOrder(sortOrder);
244 m_isDirty = true;
245 }
246
247 void ViewPropertiesDialog::slotCategorizedSortingChanged(int index)
248 {
249 m_viewProps->setCategorizedSorting(index == 1);
250 m_isDirty = true;
251 }
252
253 void ViewPropertiesDialog::slotAdditionalInfoChanged(int index)
254 {
255 KFileItemDelegate::AdditionalInformation info = KFileItemDelegate::NoInformation;
256 switch (index) {
257 case 1: info = KFileItemDelegate::FriendlyMimeType; break;
258 case 2: info = KFileItemDelegate::Size; break;
259 case 3: info = KFileItemDelegate::ModificationTime; break;
260 default: break;
261 }
262 m_viewProps->setAdditionalInfo(info);
263 m_isDirty = true;
264 }
265
266 void ViewPropertiesDialog::slotShowPreviewChanged()
267 {
268 const bool show = m_showPreview->isChecked();
269 m_viewProps->setShowPreview(show);
270 m_isDirty = true;
271 }
272
273 void ViewPropertiesDialog::slotShowHiddenFilesChanged()
274 {
275 const bool show = m_showHiddenFiles->isChecked();
276 m_viewProps->setShowHiddenFiles(show);
277 m_isDirty = true;
278 }
279
280 void ViewPropertiesDialog::markAsDirty()
281 {
282 m_isDirty = true;
283 }
284
285 void ViewPropertiesDialog::applyViewProperties()
286 {
287 const bool applyToSubFolders = m_isDirty &&
288 (m_applyToSubFolders != 0) &&
289 m_applyToSubFolders->isChecked();
290 if (applyToSubFolders) {
291 const QString text(i18n("The view properties of all sub folders will be changed. Do you want to continue?"));
292 if (KMessageBox::questionYesNo(this, text) == KMessageBox::No) {
293 return;
294 }
295
296 ViewPropsProgressInfo* info = new ViewPropsProgressInfo(m_dolphinView,
297 m_dolphinView->url(),
298 *m_viewProps);
299 info->setWindowModality(Qt::NonModal);
300 info->show();
301 }
302
303 const bool applyToAllFolders = m_isDirty &&
304 (m_applyToAllFolders != 0) &&
305 m_applyToAllFolders->isChecked();
306 if (applyToAllFolders) {
307 const QString text(i18n("The view properties of all folders will be changed. Do you want to continue?"));
308 if (KMessageBox::questionYesNo(this, text) == KMessageBox::No) {
309 return;
310 }
311
312 // Updating the global view properties time stamp in the general settings makes
313 // all existing viewproperties invalid, as they have a smaller time stamp.
314 GeneralSettings* settings = DolphinSettings::instance().generalSettings();
315 settings->setViewPropsTimestamp(QDateTime::currentDateTime());
316
317 // This is also a good chance to make a cleanup of all mirrored view properties:
318 const KUrl mirroredDir = ViewProperties::mirroredDirectory();
319 KIO::NetAccess::del(mirroredDir, this);
320 }
321
322 m_viewProps->save();
323
324 m_dolphinView->setMode(m_viewProps->viewMode());
325 m_dolphinView->setSorting(m_viewProps->sorting());
326 m_dolphinView->setSortOrder(m_viewProps->sortOrder());
327 m_dolphinView->setCategorizedSorting(m_viewProps->categorizedSorting());
328 m_dolphinView->setAdditionalInfo(m_viewProps->additionalInfo());
329 m_dolphinView->setShowPreview(m_viewProps->showPreview());
330 m_dolphinView->setShowHiddenFiles(m_viewProps->showHiddenFiles());
331
332 m_isDirty = false;
333
334 if (m_useAsDefault->isChecked()) {
335 // For directories where no .directory file is available, the .directory
336 // file stored for the global view properties is used as fallback. To update
337 // this file we temporary turn on the global view properties mode.
338 GeneralSettings* settings = DolphinSettings::instance().generalSettings();
339 Q_ASSERT(!settings->globalViewProps());
340
341 settings->setGlobalViewProps(true);
342 ViewProperties defaultProps(m_dolphinView->url());
343 defaultProps.setDirProperties(*m_viewProps);
344 defaultProps.save();
345 settings->setGlobalViewProps(false);
346 }
347 }
348
349 #include "viewpropertiesdialog.moc"