]> cloud.milkyroute.net Git - dolphin.git/blob - src/viewpropertiesdialog.cpp
eceaf8048a594c4d28c3b84231e643a65c7d2850
[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 "generalsettings.h"
27 #include "viewproperties.h"
28
29 #include <assert.h>
30
31 #include <kcomponentdata.h>
32 #include <klocale.h>
33 #include <kiconloader.h>
34 #include <kio/netaccess.h>
35 #include <kmessagebox.h>
36 #include <kstandarddirs.h>
37 #include <kurl.h>
38
39 #include <QButtonGroup>
40 #include <QCheckBox>
41 #include <QComboBox>
42 #include <QGridLayout>
43 #include <QGroupBox>
44 #include <QLabel>
45 #include <QRadioButton>
46 #include <QVBoxLayout>
47
48 ViewPropertiesDialog::ViewPropertiesDialog(DolphinView* dolphinView) :
49 KDialog(dolphinView),
50 m_isDirty(false),
51 m_dolphinView(dolphinView),
52 m_viewProps(0),
53 m_viewMode(0),
54 m_sorting(0),
55 m_sortOrder(0),
56 m_showPreview(0),
57 m_showHiddenFiles(0),
58 m_applyToCurrentFolder(0),
59 m_applyToSubFolders(0),
60 m_useAsDefault(0)
61 {
62 assert(dolphinView != 0);
63
64 setCaption(i18n("View Properties"));
65 setButtons(KDialog::Ok | KDialog::Cancel | KDialog::Apply);
66
67 const KUrl& url = dolphinView->url();
68 m_viewProps = new ViewProperties(url);
69 m_viewProps->setAutoSaveEnabled(false);
70
71 QWidget* main = new QWidget();
72 QVBoxLayout* topLayout = new QVBoxLayout();
73
74 // create 'Properties' group containing view mode, sorting, sort order and show hidden files
75 QGroupBox* propsBox = new QGroupBox(i18n("Properties"), main);
76
77 QLabel* viewModeLabel = new QLabel(i18n("View mode:"), propsBox);
78 m_viewMode = new QComboBox(propsBox);
79 m_viewMode->addItem(SmallIcon("view_icon"), i18n("Icons"));
80 m_viewMode->addItem(SmallIcon("view_text"), i18n("Details"));
81 const int index = static_cast<int>(m_viewProps->viewMode());
82 m_viewMode->setCurrentIndex(index);
83
84 QLabel* sortingLabel = new QLabel(i18n("Sorting:"), propsBox);
85 m_sorting = new QComboBox(propsBox);
86 m_sorting->addItem("By Name");
87 m_sorting->addItem("By Size");
88 m_sorting->addItem("By Date");
89 m_sorting->addItem("By Permissions");
90 m_sorting->addItem("By Owner");
91 m_sorting->addItem("By Group");
92 m_sorting->setCurrentIndex(m_viewProps->sorting());
93
94 QLabel* sortOrderLabel = new QLabel(i18n("Sort order:"), propsBox);
95 m_sortOrder = new QComboBox(propsBox);
96 m_sortOrder->addItem(i18n("Ascending"));
97 m_sortOrder->addItem(i18n("Descending"));
98 const int sortOrderIdx = (m_viewProps->sortOrder() == Qt::Ascending) ? 0 : 1;
99 m_sortOrder->setCurrentIndex(sortOrderIdx);
100
101 m_showPreview = new QCheckBox(i18n("Show preview"), propsBox);
102 m_showPreview->setChecked(m_viewProps->showPreview());
103
104 m_showHiddenFiles = new QCheckBox(i18n("Show hidden files"), propsBox);
105 m_showHiddenFiles->setChecked(m_viewProps->showHiddenFiles());
106
107 QGridLayout* propsBoxLayout = new QGridLayout(propsBox);
108 propsBoxLayout->addWidget(viewModeLabel, 0, 0);
109 propsBoxLayout->addWidget(m_viewMode, 0, 1);
110 propsBoxLayout->addWidget(m_sorting, 1, 1);
111 propsBoxLayout->addWidget(sortingLabel, 1, 0);
112 propsBoxLayout->addWidget(m_sorting, 1, 1);
113 propsBoxLayout->addWidget(sortOrderLabel, 2, 0);
114 propsBoxLayout->addWidget(m_sortOrder, 2, 1);
115 propsBoxLayout->addWidget(m_showPreview, 3, 0);
116 propsBoxLayout->addWidget(m_showHiddenFiles, 4, 0);
117
118 topLayout->addWidget(propsBox);
119
120 connect(m_viewMode, SIGNAL(activated(int)),
121 this, SLOT(slotViewModeChanged(int)));
122 connect(m_sorting, SIGNAL(activated(int)),
123 this, SLOT(slotSortingChanged(int)));
124 connect(m_sortOrder, SIGNAL(activated(int)),
125 this, SLOT(slotSortOrderChanged(int)));
126 connect(m_showPreview, SIGNAL(clicked()),
127 this, SLOT(slotShowPreviewChanged()));
128 connect(m_showHiddenFiles, SIGNAL(clicked()),
129 this, SLOT(slotShowHiddenFilesChanged()));
130
131 connect(this, SIGNAL(okClicked()), this, SLOT(slotOk()));
132 connect(this, SIGNAL(applyClicked()), this, SLOT(slotApply()));
133
134 // Only show the following settings if the view properties are remembered
135 // for each directory:
136 if (!DolphinSettings::instance().generalSettings()->globalViewProps()) {
137 // create 'Apply view properties to:' group
138 QGroupBox* applyBox = new QGroupBox(i18n("Apply view properties to:"), main);
139
140 m_applyToCurrentFolder = new QRadioButton(i18n("Current folder"), applyBox);
141 m_applyToCurrentFolder->setChecked(true);
142 m_applyToSubFolders = new QRadioButton(i18n("Current folder including all sub folders"), applyBox);
143 m_applyToAllFolders = new QRadioButton(i18n("All folders"),applyBox);
144
145 QButtonGroup* applyGroup = new QButtonGroup(this);
146 applyGroup->addButton(m_applyToCurrentFolder);
147 applyGroup->addButton(m_applyToSubFolders);
148 applyGroup->addButton(m_applyToAllFolders);
149
150 QVBoxLayout* applyBoxLayout = new QVBoxLayout(applyBox);
151 applyBoxLayout->addWidget(m_applyToCurrentFolder);
152 applyBoxLayout->addWidget(m_applyToSubFolders);
153 applyBoxLayout->addWidget(m_applyToAllFolders);
154
155 m_useAsDefault = new QCheckBox(i18n("Use as default for new folders"), main);
156
157 topLayout->addWidget(applyBox);
158 topLayout->addWidget(m_useAsDefault);
159
160 connect(m_applyToCurrentFolder, SIGNAL(clicked()),
161 this, SLOT(markAsDirty()));
162 connect(m_applyToSubFolders, SIGNAL(clicked()),
163 this, SLOT(markAsDirty()));
164 connect(m_applyToAllFolders, SIGNAL(clicked()),
165 this, SLOT(markAsDirty()));
166 connect(m_useAsDefault, SIGNAL(clicked()),
167 this, SLOT(markAsDirty()));
168 }
169
170 main->setLayout(topLayout);
171 setMainWidget(main);
172 }
173
174 ViewPropertiesDialog::~ViewPropertiesDialog()
175 {
176 m_isDirty = false;
177 delete m_viewProps;
178 m_viewProps = 0;
179 }
180
181 void ViewPropertiesDialog::slotOk()
182 {
183 applyViewProperties();
184 accept();
185 }
186
187 void ViewPropertiesDialog::slotApply()
188 {
189 applyViewProperties();
190 }
191
192 void ViewPropertiesDialog::slotViewModeChanged(int index)
193 {
194 assert((index >= 0) && (index <= 2));
195 m_viewProps->setViewMode(static_cast<DolphinView::Mode>(index));
196 m_isDirty = true;
197 }
198
199 void ViewPropertiesDialog::slotSortingChanged(int index)
200 {
201 const DolphinView::Sorting sorting = DolphinSortFilterProxyModel::sortingForColumn(index);
202 m_viewProps->setSorting(sorting);
203 m_isDirty = true;
204 }
205
206 void ViewPropertiesDialog::slotSortOrderChanged(int index)
207 {
208 Qt::SortOrder sortOrder = (index == 0) ? Qt::Ascending : Qt::Descending;
209 m_viewProps->setSortOrder(sortOrder);
210 m_isDirty = true;
211 }
212
213 void ViewPropertiesDialog::slotShowPreviewChanged()
214 {
215 const bool show = m_showPreview->isChecked();
216 m_viewProps->setShowPreview(show);
217 m_isDirty = true;
218 }
219
220 void ViewPropertiesDialog::slotShowHiddenFilesChanged()
221 {
222 const bool show = m_showHiddenFiles->isChecked();
223 m_viewProps->setShowHiddenFiles(show);
224 m_isDirty = true;
225 }
226
227 void ViewPropertiesDialog::markAsDirty()
228 {
229 m_isDirty = true;
230 }
231
232 void ViewPropertiesDialog::applyViewProperties()
233 {
234 const bool applyToSubFolders = m_isDirty &&
235 (m_applyToSubFolders != 0) &&
236 m_applyToSubFolders->isChecked();
237 if (applyToSubFolders) {
238 const QString text(i18n("The view properties of all sub folders will be changed. Do you want to continue?"));
239 if (KMessageBox::questionYesNo(this, text) == KMessageBox::No) {
240 return;
241 }
242
243 ViewPropsProgressInfo* info = new ViewPropsProgressInfo(m_dolphinView,
244 m_dolphinView->url(),
245 *m_viewProps);
246 info->setWindowModality(Qt::NonModal);
247 info->show();
248 }
249
250 const bool applyToAllFolders = m_isDirty &&
251 (m_applyToAllFolders != 0) &&
252 m_applyToAllFolders->isChecked();
253 if (applyToAllFolders) {
254 const QString text(i18n("The view properties of all folders will be changed. Do you want to continue?"));
255 if (KMessageBox::questionYesNo(this, text) == KMessageBox::No) {
256 return;
257 }
258
259 // Updating the global view properties time stamp in the general settings makes
260 // all existing viewproperties invalid, as they have a smaller time stamp.
261 GeneralSettings* settings = DolphinSettings::instance().generalSettings();
262 settings->setViewPropsTimestamp(QDateTime::currentDateTime());
263
264 // This is also a good chance to make a cleanup of all mirrored view properties:
265 QString basePath = KGlobal::mainComponent().componentName();
266 basePath.append("/view_properties/");
267 const QString mirroredViewProps = KStandardDirs::locateLocal("data", basePath);
268 KIO::NetAccess::del(mirroredViewProps, this);
269 }
270
271 m_viewProps->save();
272
273 m_dolphinView->setMode(m_viewProps->viewMode());
274 m_dolphinView->setSorting(m_viewProps->sorting());
275 m_dolphinView->setSortOrder(m_viewProps->sortOrder());
276 m_dolphinView->setShowPreview(m_viewProps->showPreview());
277 m_dolphinView->setShowHiddenFiles(m_viewProps->showHiddenFiles());
278
279 m_isDirty = false;
280
281 if (m_useAsDefault->isChecked()) {
282 // For directories where no .directory file is available, the .directory
283 // file stored for the global view properties is used as fallback. To update
284 // this file we temporary turn on the global view properties mode.
285 GeneralSettings* settings = DolphinSettings::instance().generalSettings();
286 assert(!settings->globalViewProps());
287
288 settings->setGlobalViewProps(true);
289 ViewProperties defaultProps(m_dolphinView->url());
290 defaultProps.setDirProperties(*m_viewProps);
291 kDebug() << "saving global viewprops" << endl;
292 defaultProps.save();
293 settings->setGlobalViewProps(false);
294 }
295 }
296
297 #include "viewpropertiesdialog.moc"