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