┌   ┐
54
└   ┘

summaryrefslogtreecommitdiff
path: root/src/viewpropertiesdialog.cpp
blob: db543808bfb48d24e8dff1f803c84294720c2346 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/***************************************************************************
 *   Copyright (C) 2006 by Peter Penz                                      *
 *   [email protected]                                                     *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
 ***************************************************************************/

#include "viewpropertiesdialog.h"

#include "additionalinfodialog.h"
#include "dolphinview.h"
#include "dolphinsettings.h"
#include "dolphinsortfilterproxymodel.h"
#include "dolphin_generalsettings.h"
#include "dolphin_iconsmodesettings.h"
#include "viewproperties.h"
#include "viewpropsprogressinfo.h"

#include <config-nepomuk.h>
#ifdef HAVE_NEPOMUK
#include <nepomuk/resourcemanager.h>
#endif

#include <kcomponentdata.h>
#include <klocale.h>
#include <kiconloader.h>
#include <kio/netaccess.h>
#include <kmessagebox.h>
#include <kstandarddirs.h>
#include <kurl.h>

#include <QAction>
#include <QButtonGroup>
#include <QCheckBox>
#include <QComboBox>
#include <QGridLayout>
#include <QGroupBox>
#include <QLabel>
#include <QMenu>
#include <QPushButton>
#include <QRadioButton>
#include <QBoxLayout>

ViewPropertiesDialog::ViewPropertiesDialog(DolphinView* dolphinView) :
    KDialog(dolphinView),
    m_isDirty(false),
    m_dolphinView(dolphinView),
    m_viewProps(0),
    m_viewMode(0),
    m_sortOrder(0),
    m_sorting(0),
    m_showPreview(0),
    m_showInGroups(0),
    m_showHiddenFiles(0),
    m_additionalInfo(0),
    m_applyToCurrentFolder(0),
    m_applyToSubFolders(0),
    m_useAsDefault(0)
{
    Q_ASSERT(dolphinView != 0);
    const bool useGlobalViewProps = DolphinSettings::instance().generalSettings()->globalViewProps();

    setCaption(i18nc("@title:window", "View Properties"));
    setButtons(KDialog::Ok | KDialog::Cancel | KDialog::Apply);

    const KUrl& url = dolphinView->url();
    m_viewProps = new ViewProperties(url);
    m_viewProps->setAutoSaveEnabled(false);

    QWidget* main = new QWidget();
    QVBoxLayout* topLayout = new QVBoxLayout();

    // create 'Properties' group containing view mode, sorting, sort order and show hidden files
    QWidget* propsBox = main;
    if (!useGlobalViewProps) {
        propsBox = new QGroupBox(i18nc("@title:group", "Properties"), main);
    }

    QWidget* propsGrid = new QWidget();

    QLabel* viewModeLabel = new QLabel(i18nc("@label:listbox", "View mode:"), propsGrid);
    m_viewMode = new QComboBox(propsGrid);
    m_viewMode->addItem(KIcon("view-list-icons"), i18nc("@item:inlistbox", "Icons"));
    m_viewMode->addItem(KIcon("view-list-details"), i18nc("@item:inlistbox", "Details"));
    m_viewMode->addItem(KIcon("view-file-columns"), i18nc("@item:inlistbox", "Column"));

    QLabel* sortingLabel = new QLabel(i18nc("@label:listbox", "Sorting:"), propsGrid);
    QWidget* sortingBox = new QWidget(propsGrid);

    m_sortOrder = new QComboBox(sortingBox);
    m_sortOrder->addItem(i18nc("@item:inlistbox Sort", "Ascending"));
    m_sortOrder->addItem(i18nc("@item:inlistbox Sort", "Descending"));

    m_sorting = new QComboBox(sortingBox);
    m_sorting->addItem(i18nc("@item:inlistbox Sort", "By Name"));
    m_sorting->addItem(i18nc("@item:inlistbox Sort", "By Size"));
    m_sorting->addItem(i18nc("@item:inlistbox Sort", "By Date"));
    m_sorting->addItem(i18nc("@item:inlistbox Sort", "By Permissions"));
    m_sorting->addItem(i18nc("@item:inlistbox Sort", "By Owner"));
    m_sorting->addItem(i18nc("@item:inlistbox Sort", "By Group"));
    m_sorting->addItem(i18nc("@item:inlistbox Sort", "By Type"));
#ifdef HAVE_NEPOMUK
    // TODO: Hided "sort by rating" and "sort by tags" as without caching the performance
    // is too slow currently (Nepomuk will support caching in future releases).
    //
    // if (!Nepomuk::ResourceManager::instance()->init()) {
    //    m_sorting->addItem(i18nc("@item:inlistbox Sort", "By Rating"));
    //    m_sorting->addItem(i18nc("@item:inlistbox Sort", "By Tags"));
    // }
#endif
    m_showPreview = new QCheckBox(i18nc("@option:check", "Show preview"), propsBox);
    m_showInGroups = new QCheckBox(i18nc("@option:check", "Show in groups"), propsBox);
    m_showHiddenFiles = new QCheckBox(i18nc("@option:check", "Show hidden files"), propsBox);

    m_additionalInfo = new QPushButton(i18nc("@action:button", "Additional Information"), propsBox);

    QHBoxLayout* sortingLayout = new QHBoxLayout();
    sortingLayout->setMargin(0);
    sortingLayout->addWidget(m_sortOrder);
    sortingLayout->addWidget(m_sorting);
    sortingBox->setLayout(sortingLayout);

    QGridLayout* propsGridLayout = new QGridLayout(propsGrid);
    propsGridLayout->addWidget(viewModeLabel, 0, 0);
    propsGridLayout->addWidget(m_viewMode, 0, 1);
    propsGridLayout->addWidget(sortingLabel, 1, 0);
    propsGridLayout->addWidget(sortingBox, 1, 1);

    QVBoxLayout* propsBoxLayout = new QVBoxLayout(propsBox);
    propsBoxLayout->addWidget(propsGrid);
    propsBoxLayout->addWidget(m_showPreview);
    propsBoxLayout->addWidget(m_showInGroups);
    propsBoxLayout->addWidget(m_showHiddenFiles);
    propsBoxLayout->addWidget(m_additionalInfo);

    topLayout->addWidget(propsBox);

    connect(m_viewMode, SIGNAL(activated(int)),
            this, SLOT(slotViewModeChanged(int)));
    connect(m_sorting, SIGNAL(activated(int)),
            this, SLOT(slotSortingChanged(int)));
    connect(m_sortOrder, SIGNAL(activated(int)),
            this, SLOT(slotSortOrderChanged(int)));
    connect(m_additionalInfo, SIGNAL(clicked()),
            this, SLOT(configureAdditionalInfo()));
    connect(m_showPreview, SIGNAL(clicked()),
            this, SLOT(slotShowPreviewChanged()));
    connect(m_showInGroups, SIGNAL(clicked()),
            this, SLOT(slotCategorizedSortingChanged()));
    connect(m_showHiddenFiles, SIGNAL(clicked()),
            this, SLOT(slotShowHiddenFilesChanged()));

    connect(this, SIGNAL(okClicked()), this, SLOT(slotOk()));
    connect(this, SIGNAL(applyClicked()), this, SLOT(slotApply()));

    // Only show the following settings if the view properties are remembered
    // for each directory:
    if (!useGlobalViewProps) {
        // create 'Apply View Properties To' group
        QGroupBox* applyBox = new QGroupBox(i18nc("@title:group", "Apply View Properties To"), main);

        m_applyToCurrentFolder = new QRadioButton(i18nc("@option:radio Apply View Properties To",
                                                        "Current folder"), applyBox);
        m_applyToCurrentFolder->setChecked(true);
        m_applyToSubFolders = new QRadioButton(i18nc("@option:radio Apply View Properties To",
                                                     "Current folder including all sub folders"), applyBox);
        m_applyToAllFolders = new QRadioButton(i18nc("@option:radio Apply View Properties To",
                                                     "All folders"), applyBox);

        QButtonGroup* applyGroup = new QButtonGroup(this);
        applyGroup->addButton(m_applyToCurrentFolder);
        applyGroup->addButton(m_applyToSubFolders);
        applyGroup->addButton(m_applyToAllFolders);

        QVBoxLayout* applyBoxLayout = new QVBoxLayout(applyBox);
        applyBoxLayout->addWidget(m_applyToCurrentFolder);
        applyBoxLayout->addWidget(m_applyToSubFolders);
        applyBoxLayout->addWidget(m_applyToAllFolders);

        m_useAsDefault = new QCheckBox(i18nc("@option:check", "Use as default for new folders"), main);

        topLayout->addWidget(applyBox);
        topLayout->addWidget(m_useAsDefault);

        connect(m_applyToCurrentFolder, SIGNAL(clicked()),
                this, SLOT(markAsDirty()));
        connect(m_applyToSubFolders, SIGNAL(clicked()),
                this, SLOT(markAsDirty()));
        connect(m_applyToAllFolders, SIGNAL(clicked()),
                this, SLOT(markAsDirty()));
        connect(m_useAsDefault, SIGNAL(clicked()),
                this, SLOT(markAsDirty()));
    }

    main->setLayout(topLayout);
    setMainWidget(main);

    const KConfigGroup dialogConfig(KSharedConfig::openConfig("dolphinrc"),
                                    "ViewPropertiesDialog");
    restoreDialogSize(dialogConfig);

    loadSettings();
}

ViewPropertiesDialog::~ViewPropertiesDialog()
{
    m_isDirty = false;
    delete m_viewProps;
    m_viewProps = 0;

    KConfigGroup dialogConfig(KSharedConfig::openConfig("dolphinrc"),
                              "ViewPropertiesDialog");
    saveDialogSize(dialogConfig, KConfigBase::Persistent);
}

void ViewPropertiesDialog::slotOk()
{
    applyViewProperties();
    accept();
}

void ViewPropertiesDialog::slotApply()
{
    applyViewProperties();
}

void ViewPropertiesDialog::slotViewModeChanged(int index)
{
    m_viewProps->setViewMode(static_cast<DolphinView::Mode>(index));
    m_isDirty = true;

    const DolphinView::Mode mode = m_viewProps->viewMode();
    m_showInGroups->setEnabled(mode == DolphinView::IconsView);
    m_additionalInfo->setEnabled(mode != DolphinView::ColumnView);
}

void ViewPropertiesDialog::slotSortingChanged(int index)
{
    const DolphinView::Sorting sorting = DolphinSortFilterProxyModel::sortingForColumn(index);
    m_viewProps->setSorting(sorting);
    m_isDirty = true;
}

void ViewPropertiesDialog::slotSortOrderChanged(int index)
{
    const Qt::SortOrder sortOrder = (index == 0) ? Qt::AscendingOrder : Qt::DescendingOrder;
    m_viewProps->setSortOrder(sortOrder);
    m_isDirty = true;
}

void ViewPropertiesDialog::slotCategorizedSortingChanged()
{
    m_viewProps->setCategorizedSorting(m_showInGroups->isChecked());
    m_isDirty = true;
}

void ViewPropertiesDialog::slotShowPreviewChanged()
{
    const bool show = m_showPreview->isChecked();
    m_viewProps->setShowPreview(show);
    m_isDirty = true;
}

void ViewPropertiesDialog::slotShowHiddenFilesChanged()
{
    const bool show = m_showHiddenFiles->isChecked();
    m_viewProps->setShowHiddenFiles(show);
    m_isDirty = true;
}

void ViewPropertiesDialog::markAsDirty()
{
    m_isDirty = true;
}

void ViewPropertiesDialog::configureAdditionalInfo()
{
    KFileItemDelegate::InformationList info = m_viewProps->additionalInfo();
    const bool useDefaultInfo = (m_viewProps->viewMode() == DolphinView::DetailsView) &&
                                (info.isEmpty() || info.contains(KFileItemDelegate::NoInformation));
    if (useDefaultInfo) {
        // Using the details view without any additional information (-> additional column)
        // makes no sense and leads to a usability problem as no viewport area is available
        // anymore. Hence as fallback provide at least a size and date column.
        info.clear();
        info.append(KFileItemDelegate::Size);
        info.append(KFileItemDelegate::ModificationTime);
        m_viewProps->setAdditionalInfo(info);
    }

    AdditionalInfoDialog dialog(this, info);
    if (dialog.exec() == QDialog::Accepted) {
        m_viewProps->setAdditionalInfo(dialog.additionalInfo());
        m_isDirty = true;
    }
}

void ViewPropertiesDialog::applyViewProperties()
{
    // if nothing changed in the dialog, we have nothing to apply
    if (!m_isDirty) {
        return;
    }

    const bool applyToSubFolders = (m_applyToSubFolders != 0) &&
                                   m_applyToSubFolders->isChecked();
    if (applyToSubFolders) {
        const QString text(i18nc("@info", "The view properties of all sub folders will be changed. Do you want to continue?"));
        if (KMessageBox::questionYesNo(this, text) == KMessageBox::No) {
            return;
        }

        ViewPropsProgressInfo* info = new ViewPropsProgressInfo(m_dolphinView,
                                                                m_dolphinView->url(),
                                                                *m_viewProps);
        info->setAttribute(Qt::WA_DeleteOnClose);
        info->setWindowModality(Qt::NonModal);
        info->show();
    }

    const bool applyToAllFolders = (m_applyToAllFolders != 0) &&
                                   m_applyToAllFolders->isChecked();

    // If the user selected 'Apply To All Folders' the view properties implicitely
    // are also used as default for new folders.
    const bool useAsDefault = applyToAllFolders ||
                              (m_useAsDefault != 0) && m_useAsDefault->isChecked();
    if (useAsDefault) {
        // For directories where no .directory file is available, the .directory
        // file stored for the global view properties is used as fallback. To update
        // this file we temporary turn on the global view properties mode.
        GeneralSettings* settings = DolphinSettings::instance().generalSettings();
        Q_ASSERT(!settings->globalViewProps());

        settings->setGlobalViewProps(true);
        ViewProperties defaultProps(m_dolphinView->url());
        defaultProps.setDirProperties(*m_viewProps);
        defaultProps.save();
        settings->setGlobalViewProps(false);
    }

    if (applyToAllFolders) {
        const QString text(i18nc("@info", "The view properties of all folders will be changed. Do you want to continue?"));
        if (KMessageBox::questionYesNo(this, text) == KMessageBox::No) {
            return;
        }

        // Updating the global view properties time stamp in the general settings makes
        // all existing viewproperties invalid, as they have a smaller time stamp.
        GeneralSettings* settings = DolphinSettings::instance().generalSettings();
        settings->setViewPropsTimestamp(QDateTime::currentDateTime());
    }

    m_dolphinView->setMode(m_viewProps->viewMode());
    m_dolphinView->setSorting(m_viewProps->sorting());
    m_dolphinView->setSortOrder(m_viewProps->sortOrder());
    m_dolphinView->setCategorizedSorting(m_viewProps->categorizedSorting());
    m_dolphinView->setAdditionalInfo(m_viewProps->additionalInfo());
    m_dolphinView->setShowPreview(m_viewProps->showPreview());
    m_dolphinView->setShowHiddenFiles(m_viewProps->showHiddenFiles());

    m_viewProps->save();

    m_isDirty = false;
}

void ViewPropertiesDialog::loadSettings()
{
    // load view mode
    const int index = static_cast<int>(m_viewProps->viewMode());
    m_viewMode->setCurrentIndex(index);

    // load sort order and sorting
    const int sortOrderIndex = (m_viewProps->sortOrder() == Qt::AscendingOrder) ? 0 : 1;
    m_sortOrder->setCurrentIndex(sortOrderIndex);
    m_sorting->setCurrentIndex(m_viewProps->sorting());

    const bool enabled = (index == DolphinView::DetailsView) ||
                         (index == DolphinView::IconsView);
    m_additionalInfo->setEnabled(enabled);

    // load show preview, show in groups and show hidden files settings
    m_showPreview->setChecked(m_viewProps->showPreview());

    m_showInGroups->setChecked(m_viewProps->categorizedSorting());
    m_showInGroups->setEnabled(index == DolphinView::IconsView); // only the icons view supports categorized sorting

    m_showHiddenFiles->setChecked(m_viewProps->showHiddenFiles());
}

#include "viewpropertiesdialog.moc"