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