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