]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/viewpropertiesdialog.cpp
Delete obsolete class DolphinSettings
[dolphin.git] / src / settings / 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
23 #include "additionalinfodialog.h"
24 #include "views/dolphinview.h"
25 #include "dolphin_generalsettings.h"
26 #include "dolphin_iconsmodesettings.h"
27 #include "viewpropsprogressinfo.h"
28
29 #include <config-nepomuk.h>
30 #ifdef HAVE_NEPOMUK
31 #include <Nepomuk/ResourceManager>
32 #endif
33
34 #include <KComponentData>
35 #include <KLocale>
36 #include <KIconLoader>
37 #include <KIO/NetAccess>
38 #include <KMessageBox>
39 #include <KStandardDirs>
40 #include <KUrl>
41 #include <KComboBox>
42
43 #include <QAction>
44 #include <QButtonGroup>
45 #include <QCheckBox>
46 #include <QGridLayout>
47 #include <QGroupBox>
48 #include <QLabel>
49 #include <QMenu>
50 #include <QPushButton>
51 #include <QRadioButton>
52 #include <QBoxLayout>
53
54 #include <views/viewproperties.h>
55
56 ViewPropertiesDialog::ViewPropertiesDialog(DolphinView* dolphinView) :
57 KDialog(dolphinView),
58 m_isDirty(false),
59 m_dolphinView(dolphinView),
60 m_viewProps(0),
61 m_viewMode(0),
62 m_sortOrder(0),
63 m_sorting(0),
64 m_sortFoldersFirst(0),
65 m_previewsShown(0),
66 m_showInGroups(0),
67 m_showHiddenFiles(0),
68 m_additionalInfo(0),
69 m_applyToCurrentFolder(0),
70 m_applyToSubFolders(0),
71 m_applyToAllFolders(0),
72 m_useAsDefault(0)
73 {
74 Q_ASSERT(dolphinView);
75 const bool useGlobalViewProps = GeneralSettings::globalViewProps();
76
77 setCaption(i18nc("@title:window", "View Properties"));
78 setButtons(KDialog::Ok | KDialog::Cancel | KDialog::Apply);
79
80 const KUrl& url = dolphinView->url();
81 m_viewProps = new ViewProperties(url);
82 m_viewProps->setAutoSaveEnabled(false);
83
84 QWidget* main = new QWidget();
85 QVBoxLayout* topLayout = new QVBoxLayout();
86
87 // create 'Properties' group containing view mode, sorting, sort order and show hidden files
88 QWidget* propsBox = main;
89 if (!useGlobalViewProps) {
90 propsBox = new QGroupBox(i18nc("@title:group", "Properties"), main);
91 }
92
93 QWidget* propsGrid = new QWidget();
94
95 QLabel* viewModeLabel = new QLabel(i18nc("@label:listbox", "View mode:"), propsGrid);
96 m_viewMode = new KComboBox(propsGrid);
97 m_viewMode->addItem(KIcon("view-list-icons"), i18nc("@item:inlistbox", "Icons"), DolphinView::IconsView);
98 m_viewMode->addItem(KIcon("view-list-details"), i18nc("@item:inlistbox", "Compact"), DolphinView::CompactView);
99 m_viewMode->addItem(KIcon("view-list-tree"), i18nc("@item:inlistbox", "Details"), DolphinView::DetailsView);
100
101 QLabel* sortingLabel = new QLabel(i18nc("@label:listbox", "Sorting:"), propsGrid);
102 QWidget* sortingBox = new QWidget(propsGrid);
103
104 m_sortOrder = new KComboBox(sortingBox);
105 m_sortOrder->addItem(i18nc("@item:inlistbox Sort", "Ascending"));
106 m_sortOrder->addItem(i18nc("@item:inlistbox Sort", "Descending"));
107
108 // TODO: Provide a kind of SortingInfoAccessor similar to AdditionalInfoAccessor
109 // to assure that adding a sort-role requires to change only one file
110 m_sorting = new KComboBox(sortingBox);
111 m_sorting->addItem(i18nc("@item:inlistbox Sort", "By Name"));
112 m_sorting->addItem(i18nc("@item:inlistbox Sort", "By Size"));
113 m_sorting->addItem(i18nc("@item:inlistbox Sort", "By Date"));
114 m_sorting->addItem(i18nc("@item:inlistbox Sort", "By Permissions"));
115 m_sorting->addItem(i18nc("@item:inlistbox Sort", "By Owner"));
116 m_sorting->addItem(i18nc("@item:inlistbox Sort", "By Group"));
117 m_sorting->addItem(i18nc("@item:inlistbox Sort", "By Type"));
118 m_sorting->addItem(i18nc("@item:inlistbox Sort", "By Link Destination"));
119 m_sorting->addItem(i18nc("@item:inlistbox Sort", "By Path"));
120
121 m_sortFoldersFirst = new QCheckBox(i18nc("@option:check", "Show folders first"));
122 m_previewsShown = new QCheckBox(i18nc("@option:check", "Show preview"));
123 m_showInGroups = new QCheckBox(i18nc("@option:check", "Show in groups"));
124 m_showHiddenFiles = new QCheckBox(i18nc("@option:check", "Show hidden files"));
125
126 m_additionalInfo = new QPushButton(i18nc("@action:button", "Additional Information"));
127
128 QHBoxLayout* sortingLayout = new QHBoxLayout();
129 sortingLayout->setMargin(0);
130 sortingLayout->addWidget(m_sortOrder);
131 sortingLayout->addWidget(m_sorting);
132 sortingBox->setLayout(sortingLayout);
133
134 QGridLayout* propsGridLayout = new QGridLayout(propsGrid);
135 propsGridLayout->addWidget(viewModeLabel, 0, 0, Qt::AlignRight);
136 propsGridLayout->addWidget(m_viewMode, 0, 1);
137 propsGridLayout->addWidget(sortingLabel, 1, 0, Qt::AlignRight);
138 propsGridLayout->addWidget(sortingBox, 1, 1);
139
140 QVBoxLayout* propsBoxLayout = new QVBoxLayout(propsBox);
141 propsBoxLayout->addWidget(propsGrid);
142 propsBoxLayout->addWidget(m_sortFoldersFirst);
143 propsBoxLayout->addWidget(m_previewsShown);
144 propsBoxLayout->addWidget(m_showInGroups);
145 propsBoxLayout->addWidget(m_showHiddenFiles);
146 propsBoxLayout->addWidget(m_additionalInfo);
147
148 topLayout->addWidget(propsBox);
149
150 connect(m_viewMode, SIGNAL(currentIndexChanged(int)),
151 this, SLOT(slotViewModeChanged(int)));
152 connect(m_sorting, SIGNAL(currentIndexChanged(int)),
153 this, SLOT(slotSortingChanged(int)));
154 connect(m_sortOrder, SIGNAL(currentIndexChanged(int)),
155 this, SLOT(slotSortOrderChanged(int)));
156 connect(m_additionalInfo, SIGNAL(clicked()),
157 this, SLOT(configureAdditionalInfo()));
158 connect(m_sortFoldersFirst, SIGNAL(clicked()),
159 this, SLOT(slotSortFoldersFirstChanged()));
160 connect(m_previewsShown, SIGNAL(clicked()),
161 this, SLOT(slotShowPreviewChanged()));
162 connect(m_showInGroups, SIGNAL(clicked()),
163 this, SLOT(slotCategorizedSortingChanged()));
164 connect(m_showHiddenFiles, SIGNAL(clicked()),
165 this, SLOT(slotShowHiddenFilesChanged()));
166
167 connect(this, SIGNAL(okClicked()), this, SLOT(slotOk()));
168 connect(this, SIGNAL(applyClicked()), this, SLOT(slotApply()));
169
170 // Only show the following settings if the view properties are remembered
171 // for each directory:
172 if (!useGlobalViewProps) {
173 // create 'Apply View Properties To' group
174 QGroupBox* applyBox = new QGroupBox(i18nc("@title:group", "Apply View Properties To"), main);
175
176 m_applyToCurrentFolder = new QRadioButton(i18nc("@option:radio Apply View Properties To",
177 "Current folder"), applyBox);
178 m_applyToCurrentFolder->setChecked(true);
179 m_applyToSubFolders = new QRadioButton(i18nc("@option:radio Apply View Properties To",
180 "Current folder including all sub-folders"), applyBox);
181 m_applyToAllFolders = new QRadioButton(i18nc("@option:radio Apply View Properties To",
182 "All folders"), applyBox);
183
184 QButtonGroup* applyGroup = new QButtonGroup(this);
185 applyGroup->addButton(m_applyToCurrentFolder);
186 applyGroup->addButton(m_applyToSubFolders);
187 applyGroup->addButton(m_applyToAllFolders);
188
189 QVBoxLayout* applyBoxLayout = new QVBoxLayout(applyBox);
190 applyBoxLayout->addWidget(m_applyToCurrentFolder);
191 applyBoxLayout->addWidget(m_applyToSubFolders);
192 applyBoxLayout->addWidget(m_applyToAllFolders);
193
194 m_useAsDefault = new QCheckBox(i18nc("@option:check", "Use these view properties as default"), main);
195
196 topLayout->addWidget(applyBox);
197 topLayout->addWidget(m_useAsDefault);
198
199 connect(m_applyToCurrentFolder, SIGNAL(clicked(bool)),
200 this, SLOT(markAsDirty(bool)));
201 connect(m_applyToSubFolders, SIGNAL(clicked(bool)),
202 this, SLOT(markAsDirty(bool)));
203 connect(m_applyToAllFolders, SIGNAL(clicked(bool)),
204 this, SLOT(markAsDirty(bool)));
205 connect(m_useAsDefault, SIGNAL(clicked(bool)),
206 this, SLOT(markAsDirty(bool)));
207 }
208
209 main->setLayout(topLayout);
210 setMainWidget(main);
211
212 const KConfigGroup dialogConfig(KSharedConfig::openConfig("dolphinrc"),
213 "ViewPropertiesDialog");
214 restoreDialogSize(dialogConfig);
215
216 loadSettings();
217 }
218
219 ViewPropertiesDialog::~ViewPropertiesDialog()
220 {
221 m_isDirty = false;
222 delete m_viewProps;
223 m_viewProps = 0;
224
225 KConfigGroup dialogConfig(KSharedConfig::openConfig("dolphinrc"),
226 "ViewPropertiesDialog");
227 saveDialogSize(dialogConfig, KConfigBase::Persistent);
228 }
229
230 void ViewPropertiesDialog::slotOk()
231 {
232 applyViewProperties();
233 accept();
234 }
235
236 void ViewPropertiesDialog::slotApply()
237 {
238 applyViewProperties();
239 markAsDirty(false);
240 }
241
242 void ViewPropertiesDialog::slotViewModeChanged(int index)
243 {
244 const QVariant itemData = m_viewMode->itemData(index);
245 const DolphinView::Mode viewMode = static_cast<DolphinView::Mode>(itemData.toInt());
246 m_viewProps->setViewMode(viewMode);
247 markAsDirty(true);
248 }
249
250 void ViewPropertiesDialog::slotSortingChanged(int index)
251 {
252 m_viewProps->setSorting(static_cast<DolphinView::Sorting>(index));
253 markAsDirty(true);
254 }
255
256 void ViewPropertiesDialog::slotSortOrderChanged(int index)
257 {
258 const Qt::SortOrder sortOrder = (index == 0) ? Qt::AscendingOrder : Qt::DescendingOrder;
259 m_viewProps->setSortOrder(sortOrder);
260 markAsDirty(true);
261 }
262
263 void ViewPropertiesDialog::slotCategorizedSortingChanged()
264 {
265 m_viewProps->setGroupedSorting(m_showInGroups->isChecked());
266 markAsDirty(true);
267 }
268
269 void ViewPropertiesDialog::slotSortFoldersFirstChanged()
270 {
271 const bool foldersFirst = m_sortFoldersFirst->isChecked();
272 m_viewProps->setSortFoldersFirst(foldersFirst);
273 markAsDirty(true);
274 }
275
276 void ViewPropertiesDialog::slotShowPreviewChanged()
277 {
278 const bool show = m_previewsShown->isChecked();
279 m_viewProps->setPreviewsShown(show);
280 markAsDirty(true);
281 }
282
283 void ViewPropertiesDialog::slotShowHiddenFilesChanged()
284 {
285 const bool show = m_showHiddenFiles->isChecked();
286 m_viewProps->setHiddenFilesShown(show);
287 markAsDirty(true);
288 }
289
290 void ViewPropertiesDialog::markAsDirty(bool isDirty)
291 {
292 m_isDirty = isDirty;
293 enableButtonApply(isDirty);
294 }
295
296 void ViewPropertiesDialog::configureAdditionalInfo()
297 {
298 QList<DolphinView::AdditionalInfo> infoList = m_viewProps->additionalInfoList();
299 const bool useDefaultInfo = (m_viewProps->viewMode() == DolphinView::DetailsView) &&
300 (infoList.isEmpty() || infoList.contains(DolphinView::NoInfo));
301 if (useDefaultInfo) {
302 // Using the details view without any additional information (-> additional column)
303 // makes no sense and leads to a usability problem as no viewport area is available
304 // anymore. Hence as fallback provide at least a size and date column.
305 infoList.clear();
306 infoList.append(DolphinView::SizeInfo);
307 infoList.append(DolphinView::DateInfo);
308 m_viewProps->setAdditionalInfoList(infoList);
309 }
310
311 QPointer<AdditionalInfoDialog> dialog = new AdditionalInfoDialog(this, infoList);
312 if (dialog->exec() == QDialog::Accepted) {
313 m_viewProps->setAdditionalInfoList(dialog->informationList());
314 markAsDirty(true);
315 }
316 delete dialog;
317 }
318
319 void ViewPropertiesDialog::applyViewProperties()
320 {
321 // if nothing changed in the dialog, we have nothing to apply
322 if (!m_isDirty) {
323 return;
324 }
325
326 const bool applyToSubFolders = m_applyToSubFolders && m_applyToSubFolders->isChecked();
327 if (applyToSubFolders) {
328 const QString text(i18nc("@info", "The view properties of all sub-folders will be changed. Do you want to continue?"));
329 if (KMessageBox::questionYesNo(this, text) == KMessageBox::No) {
330 return;
331 }
332
333 ViewPropsProgressInfo* info = new ViewPropsProgressInfo(m_dolphinView,
334 m_dolphinView->url(),
335 *m_viewProps);
336 info->setAttribute(Qt::WA_DeleteOnClose);
337 info->setWindowModality(Qt::NonModal);
338 info->show();
339 }
340
341 const bool applyToAllFolders = m_applyToAllFolders && m_applyToAllFolders->isChecked();
342
343 // If the user selected 'Apply To All Folders' the view properties implicitely
344 // are also used as default for new folders.
345 const bool useAsDefault = applyToAllFolders || (m_useAsDefault && m_useAsDefault->isChecked());
346 if (useAsDefault) {
347 // For directories where no .directory file is available, the .directory
348 // file stored for the global view properties is used as fallback. To update
349 // this file we temporary turn on the global view properties mode.
350 Q_ASSERT(!GeneralSettings::globalViewProps());
351
352 GeneralSettings::setGlobalViewProps(true);
353 ViewProperties defaultProps(m_dolphinView->url());
354 defaultProps.setDirProperties(*m_viewProps);
355 defaultProps.save();
356 GeneralSettings::setGlobalViewProps(false);
357 }
358
359 if (applyToAllFolders) {
360 const QString text(i18nc("@info", "The view properties of all folders will be changed. Do you want to continue?"));
361 if (KMessageBox::questionYesNo(this, text) == KMessageBox::No) {
362 return;
363 }
364
365 // Updating the global view properties time stamp in the general settings makes
366 // all existing viewproperties invalid, as they have a smaller time stamp.
367 GeneralSettings* settings = GeneralSettings::self();
368 settings->setViewPropsTimestamp(QDateTime::currentDateTime());
369 settings->writeConfig();
370 }
371
372 m_dolphinView->setMode(m_viewProps->viewMode());
373 m_dolphinView->setSorting(m_viewProps->sorting());
374 m_dolphinView->setSortOrder(m_viewProps->sortOrder());
375 m_dolphinView->setSortFoldersFirst(m_viewProps->sortFoldersFirst());
376 m_dolphinView->setGroupedSorting(m_viewProps->groupedSorting());
377 m_dolphinView->setAdditionalInfoList(m_viewProps->additionalInfoList());
378 m_dolphinView->setPreviewsShown(m_viewProps->previewsShown());
379 m_dolphinView->setHiddenFilesShown(m_viewProps->hiddenFilesShown());
380
381 m_viewProps->save();
382
383 markAsDirty(false);
384 }
385
386 void ViewPropertiesDialog::loadSettings()
387 {
388 // Load view mode
389 switch (m_viewProps->viewMode()) {
390 case DolphinView::IconsView: m_viewMode->setCurrentIndex(0); break;
391 case DolphinView::CompactView: m_viewMode->setCurrentIndex(1); break;
392 case DolphinView::DetailsView: m_viewMode->setCurrentIndex(2); break;
393 default: break;
394 }
395
396 // Load sort order and sorting
397 const int sortOrderIndex = (m_viewProps->sortOrder() == Qt::AscendingOrder) ? 0 : 1;
398 m_sortOrder->setCurrentIndex(sortOrderIndex);
399 m_sorting->setCurrentIndex(m_viewProps->sorting());
400 m_sortFoldersFirst->setChecked(m_viewProps->sortFoldersFirst());
401
402 // Load show preview, show in groups and show hidden files settings
403 m_previewsShown->setChecked(m_viewProps->previewsShown());
404 m_showInGroups->setChecked(m_viewProps->groupedSorting());
405 m_showHiddenFiles->setChecked(m_viewProps->hiddenFilesShown());
406 markAsDirty(false);
407 }
408
409 #include "viewpropertiesdialog.moc"